abstractjdbc1connection.java

来自「PostgreSQL7.4.6 for Linux」· Java 代码 · 共 1,864 行 · 第 1/4 页

JAVA
1,864
字号
	}	/*	 * Method getUserName() brings back the User Name (again, we	 * saved it)	 *	 * @return the user name	 * @exception SQLException just in case...	 */	int lastMessage = 0;	public String getUserName() throws SQLException	{		return PG_USER;	}	/*	 * Get the character encoding to use for this connection.	 */	public Encoding getEncoding() throws SQLException	{		return encoding;	}	/*	 * This returns the Fastpath API for the current connection.	 *	 * <p><b>NOTE:</b> This is not part of JDBC, but allows access to	 * functions on the org.postgresql backend itself.	 *	 * <p>It is primarily used by the LargeObject API	 *	 * <p>The best way to use this is as follows:	 *	 * <p><pre>	 * import org.postgresql.fastpath.*;	 * ...	 * Fastpath fp = ((org.postgresql.Connection)myconn).getFastpathAPI();	 * </pre>	 *	 * <p>where myconn is an open Connection to org.postgresql.	 *	 * @return Fastpath object allowing access to functions on the org.postgresql	 * backend.	 * @exception SQLException by Fastpath when initialising for first time	 */	public Fastpath getFastpathAPI() throws SQLException	{		if (fastpath == null)			fastpath = new Fastpath(this, pgStream);		return fastpath;	}	// This holds a reference to the Fastpath API if already open	private Fastpath fastpath = null;	/*	 * This returns the LargeObject API for the current connection.	 *	 * <p><b>NOTE:</b> This is not part of JDBC, but allows access to	 * functions on the org.postgresql backend itself.	 *	 * <p>The best way to use this is as follows:	 *	 * <p><pre>	 * import org.postgresql.largeobject.*;	 * ...	 * LargeObjectManager lo = ((org.postgresql.Connection)myconn).getLargeObjectAPI();	 * </pre>	 *	 * <p>where myconn is an open Connection to org.postgresql.	 *	 * @return LargeObject object that implements the API	 * @exception SQLException by LargeObject when initialising for first time	 */	public LargeObjectManager getLargeObjectAPI() throws SQLException	{		if (largeobject == null)			largeobject = new LargeObjectManager(this);		return largeobject;	}	// This holds a reference to the LargeObject API if already open	private LargeObjectManager largeobject = null;	/*	 * This method is used internally to return an object based around	 * org.postgresql's more unique data types.	 *	 * <p>It uses an internal Hashtable to get the handling class. If the	 * type is not supported, then an instance of org.postgresql.util.PGobject	 * is returned.	 *	 * You can use the getValue() or setValue() methods to handle the returned	 * object. Custom objects can have their own methods.	 *	 * @return PGobject for this type, and set to value	 * @exception SQLException if value is not correct for this type	 */	public Object getObject(String type, String value) throws SQLException	{		try		{			Object o = objectTypes.get(type);			// If o is null, then the type is unknown.			// If o is not null, and it is a String, then its a class name that			// extends PGobject.			//			// This is used to implement the org.postgresql unique types (like lseg,			// point, etc).			if (o != null && o instanceof String)			{				// 6.3 style extending PG_Object				PGobject obj = null;				obj = (PGobject)(Class.forName((String)o).newInstance());				obj.setType(type);				obj.setValue(value);				return (Object)obj;			}		}		catch (SQLException sx)		{			// rethrow the exception. Done because we capture any others next			sx.fillInStackTrace();			throw sx;		}		catch (Exception ex)		{			throw new PSQLException("postgresql.con.creobj", PSQLState.CONNECTION_FAILURE, type, ex);		}		// should never be reached		return null;	}	/*	 * This allows client code to add a handler for one of org.postgresql's	 * more unique data types.	 *	 * <p><b>NOTE:</b> This is not part of JDBC, but an extension.	 *	 * <p>The best way to use this is as follows:	 *	 * <p><pre>	 * ...	 * ((org.postgresql.Connection)myconn).addDataType("mytype","my.class.name");	 * ...	 * </pre>	 *	 * <p>where myconn is an open Connection to org.postgresql.	 *	 * <p>The handling class must extend org.postgresql.util.PGobject	 *	 * @see org.postgresql.util.PGobject	 */	public void addDataType(String type, String name)	{		objectTypes.put(type, name);	}	// This holds the available types	private Hashtable objectTypes = new Hashtable();	// This array contains the types that are supported as standard.	//	// The first entry is the types name on the database, the second	// the full class name of the handling class.	//	private static final String defaultObjectTypes[][] = {				{"box", "org.postgresql.geometric.PGbox"},				{"circle", "org.postgresql.geometric.PGcircle"},				{"line", "org.postgresql.geometric.PGline"},				{"lseg", "org.postgresql.geometric.PGlseg"},				{"path", "org.postgresql.geometric.PGpath"},				{"point", "org.postgresql.geometric.PGpoint"},				{"polygon", "org.postgresql.geometric.PGpolygon"},				{"money", "org.postgresql.util.PGmoney"}			};	// This initialises the objectTypes hashtable	private void initObjectTypes()	{		for (int i = 0;i < defaultObjectTypes.length;i++)			objectTypes.put(defaultObjectTypes[i][0], defaultObjectTypes[i][1]);	}	/*	 * In some cases, it is desirable to immediately release a Connection's	 * database and JDBC resources instead of waiting for them to be	 * automatically released (cant think why off the top of my head)	 *	 * <B>Note:</B> A Connection is automatically closed when it is	 * garbage collected.  Certain fatal errors also result in a closed	 * connection.	 *	 * @exception SQLException if a database access error occurs	 */	public void close() throws SQLException	{		if (getPGProtocolVersionMajor() == 3) {			closeV3();		} else {			closeV2();		}	}	public void closeV3() throws SQLException	{		if (pgStream != null)		{			try			{				pgStream.SendChar('X');				pgStream.SendInteger(4,4);				pgStream.flush();				pgStream.close();			}			catch (IOException e)			{}			finally			{				pgStream = null;			}		}	}	public void closeV2() throws SQLException	{		if (pgStream != null)		{			try			{				pgStream.SendChar('X');				pgStream.flush();				pgStream.close();			}			catch (IOException e)			{}			finally			{				pgStream = null;			}		}	}	/*	 * A driver may convert the JDBC sql grammar into its system's	 * native SQL grammar prior to sending it; nativeSQL returns the	 * native form of the statement that the driver would have sent.	 *	 * @param sql a SQL statement that may contain one or more '?'	 *	parameter placeholders	 * @return the native form of this statement	 * @exception SQLException if a database access error occurs	 */	public String nativeSQL(String sql) throws SQLException	{		return sql;	}	/*	 * The first warning reported by calls on this Connection is	 * returned.	 *	 * <B>Note:</B> Sebsequent warnings will be changed to this	 * SQLWarning	 *	 * @return the first SQLWarning or null	 * @exception SQLException if a database access error occurs	 */	public SQLWarning getWarnings() throws SQLException	{		return firstWarning;	}	/*	 * After this call, getWarnings returns null until a new warning	 * is reported for this connection.	 *	 * @exception SQLException if a database access error occurs	 */	public void clearWarnings() throws SQLException	{		firstWarning = null;	}	/*	 * You can put a connection in read-only mode as a hunt to enable	 * database optimizations	 *	 * <B>Note:</B> setReadOnly cannot be called while in the middle	 * of a transaction	 *	 * @param readOnly - true enables read-only mode; false disables it	 * @exception SQLException if a database access error occurs	 */	public void setReadOnly(boolean readOnly) throws SQLException	{		this.readOnly = readOnly;	}	/*	 * Tests to see if the connection is in Read Only Mode.  Note that	 * we cannot really put the database in read only mode, but we pretend	 * we can by returning the value of the readOnly flag	 *	 * @return true if the connection is read only	 * @exception SQLException if a database access error occurs	 */	public boolean isReadOnly() throws SQLException	{		return readOnly;	}	/*	 * If a connection is in auto-commit mode, than all its SQL	 * statements will be executed and committed as individual	 * transactions.  Otherwise, its SQL statements are grouped	 * into transactions that are terminated by either commit()	 * or rollback().  By default, new connections are in auto-	 * commit mode.  The commit occurs when the statement completes	 * or the next execute occurs, whichever comes first.  In the	 * case of statements returning a ResultSet, the statement	 * completes when the last row of the ResultSet has been retrieved	 * or the ResultSet has been closed.  In advanced cases, a single	 * statement may return multiple results as well as output parameter	 * values.	Here the commit occurs when all results and output param	 * values have been retrieved.	 *	 * @param autoCommit - true enables auto-commit; false disables it	 * @exception SQLException if a database access error occurs	 */	public void setAutoCommit(boolean autoCommit) throws SQLException	{		if (this.autoCommit == autoCommit)			return ;		if (autoCommit)		{				execSQL("end");						}		else		{			if (haveMinimumServerVersion("7.1"))			{				execSQL("begin;" + getIsolationLevelSQL());			}			else			{				execSQL("begin");				execSQL(getIsolationLevelSQL());			}		}		this.autoCommit = autoCommit;	}	/*	 * gets the current auto-commit state	 *	 * @return Current state of the auto-commit mode	 * @see setAutoCommit	 */	public boolean getAutoCommit()	{		return this.autoCommit;	}	/*	 * The method commit() makes all changes made since the previous	 * commit/rollback permanent and releases any database locks currently	 * held by the Connection.	This method should only be used when	 * auto-commit has been disabled.  (If autoCommit == true, then we	 * just return anyhow)	 *	 * @exception SQLException if a database access error occurs	 * @see setAutoCommit	 */	public void commit() throws SQLException	{		if (autoCommit)			return ;		//TODO: delay starting new transaction until first command		if (haveMinimumServerVersion("7.1"))		{			execSQL("commit;begin;" + getIsolationLevelSQL());		}		else		{			execSQL("commit");			execSQL("begin");			execSQL(getIsolationLevelSQL());		}	}	/*	 * The method rollback() drops all changes made since the previous	 * commit/rollback and releases any database locks currently held by	 * the Connection.	 *	 * @exception SQLException if a database access error occurs	 * @see commit	 */	public void rollback() throws SQLException	{		if (autoCommit)			return ;		//TODO: delay starting transaction until first command		if (haveMinimumServerVersion("7.1"))		{			execSQL("rollback; begin;" + getIsolationLevelSQL());		}		else		{			execSQL("rollback");			execSQL("begin");			execSQL(getIsolationLevelSQL());		}	}	/*	 * Get this Connection's current transaction isolation mode.	 *	 * @return the current TRANSACTION_* mode value	 * @exception SQLException if a database access error occurs	 */	public int getTransactionIsolation() throws SQLException	{		String sql = "show transaction isolation level";		String level = null;		if (haveMinimumServerVersion("7.3")) {			BaseResultSet rs = execSQL(sql);			if (rs.next()) {				level = rs.getString(1);			}			rs.close();		} else {			BaseResultSet l_rs = execSQL(sql);			BaseStatement l_stat = l_rs.getPGStatement();			SQLWarning warning = l_stat.getWarnings();			if (warning != null)			{				level = warning.getMessage();			}			l_rs.close();			l_stat.close();		}		if (level != null) {			level = level.toUpperCase();			if (level.indexOf("READ COMMITTED") != -1)				return Connection.TRANSACTION_READ_COMMITTED;			else if (level.indexOf("READ UNCOMMITTED") != -1)				return Connection.TRANSACTION_READ_UNCOMMITTED;			else if (level.indexOf("REPEATABLE READ") != -1)				return Connection.TRANSACTION_REPEATABLE_READ;			else if (level.indexOf("SERIALIZABLE") != -1)				return Connection.TRANSACTION_SERIALIZABLE;		}		return Connection.TRANSACTION_READ_COMMITTED;	}	/*	 * You can call this method to try to change the transaction	 * isolation level using one of the TRANSACTION_* values.	 *	 * <B>Note:</B> setTransactionIsolation cannot be called while	 * in the middle of a transaction

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?