mirror of
https://github.com/postgres/postgres.git
synced 2025-07-12 21:01:52 +03:00
Another attempt at 7.0
This commit is contained in:
308
src/interfaces/jdbc/org/postgresql/jdbc1/CallableStatement.java
Normal file
308
src/interfaces/jdbc/org/postgresql/jdbc1/CallableStatement.java
Normal file
@ -0,0 +1,308 @@
|
||||
package org.postgresql.jdbc1;
|
||||
|
||||
// IMPORTANT NOTE: This file implements the JDBC 1 version of the driver.
|
||||
// If you make any modifications to this file, you must make sure that the
|
||||
// changes are also made (if relevent) to the related JDBC 2 class in the
|
||||
// org.postgresql.jdbc2 package.
|
||||
|
||||
import java.sql.*;
|
||||
import java.math.*;
|
||||
|
||||
/**
|
||||
* CallableStatement is used to execute SQL stored procedures.
|
||||
*
|
||||
* <p>JDBC provides a stored procedure SQL escape that allows stored
|
||||
* procedures to be called in a standard way for all RDBMS's. This escape
|
||||
* syntax has one form that includes a result parameter and one that does
|
||||
* not. If used, the result parameter must be registered as an OUT
|
||||
* parameter. The other parameters may be used for input, output or both.
|
||||
* Parameters are refered to sequentially, by number. The first parameter
|
||||
* is 1.
|
||||
*
|
||||
* {?= call <procedure-name>[<arg1>,<arg2>, ...]}
|
||||
* {call <procedure-name>[<arg1>,<arg2>, ...]}
|
||||
*
|
||||
*
|
||||
* <p>IN parameter values are set using the set methods inherited from
|
||||
* PreparedStatement. The type of all OUT parameters must be registered
|
||||
* prior to executing the stored procedure; their values are retrieved
|
||||
* after execution via the get methods provided here.
|
||||
*
|
||||
* <p>A Callable statement may return a ResultSet or multiple ResultSets.
|
||||
* Multiple ResultSets are handled using operations inherited from
|
||||
* Statement.
|
||||
*
|
||||
* <p>For maximum portability, a call's ResultSets and update counts should
|
||||
* be processed prior to getting the values of output parameters.
|
||||
*
|
||||
* @see Connection#prepareCall
|
||||
* @see ResultSet
|
||||
*/
|
||||
|
||||
public class CallableStatement extends PreparedStatement implements java.sql.CallableStatement
|
||||
{
|
||||
/**
|
||||
* @exception SQLException on failure
|
||||
*/
|
||||
CallableStatement(Connection c,String q) throws SQLException
|
||||
{
|
||||
super(c,q);
|
||||
}
|
||||
|
||||
/**
|
||||
* Before executing a stored procedure call you must explicitly
|
||||
* call registerOutParameter to register the java.sql.Type of each
|
||||
* out parameter.
|
||||
*
|
||||
* <p>Note: When reading the value of an out parameter, you must use
|
||||
* the getXXX method whose Java type XXX corresponds to the
|
||||
* parameter's registered SQL type.
|
||||
*
|
||||
* @param parameterIndex the first parameter is 1, the second is 2,...
|
||||
* @param sqlType SQL type code defined by java.sql.Types; for
|
||||
* parameters of type Numeric or Decimal use the version of
|
||||
* registerOutParameter that accepts a scale value
|
||||
* @exception SQLException if a database-access error occurs.
|
||||
*/
|
||||
public void registerOutParameter(int parameterIndex, int sqlType) throws SQLException {
|
||||
}
|
||||
|
||||
/**
|
||||
* You must also specify the scale for numeric/decimal types:
|
||||
*
|
||||
* <p>Note: When reading the value of an out parameter, you must use
|
||||
* the getXXX method whose Java type XXX corresponds to the
|
||||
* parameter's registered SQL type.
|
||||
*
|
||||
* @param parameterIndex the first parameter is 1, the second is 2,...
|
||||
* @param sqlType use either java.sql.Type.NUMERIC or java.sql.Type.DECIMAL
|
||||
* @param scale a value greater than or equal to zero representing the
|
||||
* desired number of digits to the right of the decimal point
|
||||
* @exception SQLException if a database-access error occurs.
|
||||
*/
|
||||
public void registerOutParameter(int parameterIndex, int sqlType,
|
||||
int scale) throws SQLException
|
||||
{
|
||||
}
|
||||
|
||||
// Old api?
|
||||
//public boolean isNull(int parameterIndex) throws SQLException {
|
||||
//return true;
|
||||
//}
|
||||
|
||||
/**
|
||||
* An OUT parameter may have the value of SQL NULL; wasNull
|
||||
* reports whether the last value read has this special value.
|
||||
*
|
||||
* <p>Note: You must first call getXXX on a parameter to read its
|
||||
* value and then call wasNull() to see if the value was SQL NULL.
|
||||
* @return true if the last parameter read was SQL NULL
|
||||
* @exception SQLException if a database-access error occurs.
|
||||
*/
|
||||
public boolean wasNull() throws SQLException {
|
||||
// check to see if the last access threw an exception
|
||||
return false; // fake it for now
|
||||
}
|
||||
|
||||
// Old api?
|
||||
//public String getChar(int parameterIndex) throws SQLException {
|
||||
//return null;
|
||||
//}
|
||||
|
||||
/**
|
||||
* Get the value of a CHAR, VARCHAR, or LONGVARCHAR parameter as a
|
||||
* Java String.
|
||||
*
|
||||
* @param parameterIndex the first parameter is 1, the second is 2,...
|
||||
* @return the parameter value; if the value is SQL NULL, the result is null
|
||||
* @exception SQLException if a database-access error occurs.
|
||||
*/
|
||||
public String getString(int parameterIndex) throws SQLException {
|
||||
return null;
|
||||
}
|
||||
//public String getVarChar(int parameterIndex) throws SQLException {
|
||||
// return null;
|
||||
//}
|
||||
|
||||
//public String getLongVarChar(int parameterIndex) throws SQLException {
|
||||
//return null;
|
||||
//}
|
||||
|
||||
/**
|
||||
* Get the value of a BIT parameter as a Java boolean.
|
||||
*
|
||||
* @param parameterIndex the first parameter is 1, the second is 2,...
|
||||
* @return the parameter value; if the value is SQL NULL, the result is false
|
||||
* @exception SQLException if a database-access error occurs.
|
||||
*/
|
||||
public boolean getBoolean(int parameterIndex) throws SQLException {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the value of a TINYINT parameter as a Java byte.
|
||||
*
|
||||
* @param parameterIndex the first parameter is 1, the second is 2,...
|
||||
* @return the parameter value; if the value is SQL NULL, the result is 0
|
||||
* @exception SQLException if a database-access error occurs.
|
||||
*/
|
||||
public byte getByte(int parameterIndex) throws SQLException {
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the value of a SMALLINT parameter as a Java short.
|
||||
*
|
||||
* @param parameterIndex the first parameter is 1, the second is 2,...
|
||||
* @return the parameter value; if the value is SQL NULL, the result is 0
|
||||
* @exception SQLException if a database-access error occurs.
|
||||
*/
|
||||
public short getShort(int parameterIndex) throws SQLException {
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the value of an INTEGER parameter as a Java int.
|
||||
*
|
||||
* @param parameterIndex the first parameter is 1, the second is 2,...
|
||||
* @return the parameter value; if the value is SQL NULL, the result is 0
|
||||
* @exception SQLException if a database-access error occurs.
|
||||
*/
|
||||
public int getInt(int parameterIndex) throws SQLException {
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the value of a BIGINT parameter as a Java long.
|
||||
*
|
||||
* @param parameterIndex the first parameter is 1, the second is 2,...
|
||||
* @return the parameter value; if the value is SQL NULL, the result is 0
|
||||
* @exception SQLException if a database-access error occurs.
|
||||
*/
|
||||
public long getLong(int parameterIndex) throws SQLException {
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the value of a FLOAT parameter as a Java float.
|
||||
*
|
||||
* @param parameterIndex the first parameter is 1, the second is 2,...
|
||||
* @return the parameter value; if the value is SQL NULL, the result is 0
|
||||
* @exception SQLException if a database-access error occurs.
|
||||
*/
|
||||
public float getFloat(int parameterIndex) throws SQLException {
|
||||
return (float) 0.0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the value of a DOUBLE parameter as a Java double.
|
||||
*
|
||||
* @param parameterIndex the first parameter is 1, the second is 2,...
|
||||
* @return the parameter value; if the value is SQL NULL, the result is 0
|
||||
* @exception SQLException if a database-access error occurs.
|
||||
*/
|
||||
public double getDouble(int parameterIndex) throws SQLException {
|
||||
return 0.0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the value of a NUMERIC parameter as a java.math.BigDecimal
|
||||
* object.
|
||||
*
|
||||
* @param parameterIndex the first parameter is 1, the second is 2,...
|
||||
* @param scale a value greater than or equal to zero representing the
|
||||
* desired number of digits to the right of the decimal point
|
||||
* @return the parameter value; if the value is SQL NULL, the result is null
|
||||
* @exception SQLException if a database-access error occurs.
|
||||
*/
|
||||
public BigDecimal getBigDecimal(int parameterIndex, int scale)
|
||||
throws SQLException {
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the value of a SQL BINARY or VARBINARY parameter as a Java
|
||||
* byte[]
|
||||
*
|
||||
* @param parameterIndex the first parameter is 1, the second is 2,...
|
||||
* @return the parameter value; if the value is SQL NULL, the result is null
|
||||
* @exception SQLException if a database-access error occurs.
|
||||
*/
|
||||
public byte[] getBytes(int parameterIndex) throws SQLException {
|
||||
return null;
|
||||
}
|
||||
|
||||
// New API (JPM) (getLongVarBinary)
|
||||
//public byte[] getBinaryStream(int parameterIndex) throws SQLException {
|
||||
//return null;
|
||||
//}
|
||||
|
||||
/**
|
||||
* Get the value of a SQL DATE parameter as a java.sql.Date object
|
||||
*
|
||||
* @param parameterIndex the first parameter is 1, the second is 2,...
|
||||
* @return the parameter value; if the value is SQL NULL, the result is null
|
||||
* @exception SQLException if a database-access error occurs.
|
||||
*/
|
||||
public java.sql.Date getDate(int parameterIndex) throws SQLException {
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the value of a SQL TIME parameter as a java.sql.Time object.
|
||||
*
|
||||
* @param parameterIndex the first parameter is 1, the second is 2,...
|
||||
* @return the parameter value; if the value is SQL NULL, the result is null
|
||||
* @exception SQLException if a database-access error occurs.
|
||||
*/
|
||||
public java.sql.Time getTime(int parameterIndex) throws SQLException {
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the value of a SQL TIMESTAMP parameter as a java.sql.Timestamp object.
|
||||
*
|
||||
* @param parameterIndex the first parameter is 1, the second is 2,...
|
||||
* @return the parameter value; if the value is SQL NULL, the result is null
|
||||
* @exception SQLException if a database-access error occurs.
|
||||
*/
|
||||
public java.sql.Timestamp getTimestamp(int parameterIndex)
|
||||
throws SQLException {
|
||||
return null;
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------------
|
||||
// Advanced features:
|
||||
|
||||
// You can obtain a ParameterMetaData object to get information
|
||||
// about the parameters to this CallableStatement.
|
||||
//public DatabaseMetaData getMetaData() {
|
||||
//return null;
|
||||
//}
|
||||
|
||||
// getObject returns a Java object for the parameter.
|
||||
// See the JDBC spec's "Dynamic Programming" chapter for details.
|
||||
/**
|
||||
* Get the value of a parameter as a Java object.
|
||||
*
|
||||
* <p>This method returns a Java object whose type coresponds to the
|
||||
* SQL type that was registered for this parameter using
|
||||
* registerOutParameter.
|
||||
*
|
||||
* <P>Note that this method may be used to read datatabase-specific,
|
||||
* abstract data types. This is done by specifying a targetSqlType
|
||||
* of java.sql.types.OTHER, which allows the driver to return a
|
||||
* database-specific Java type.
|
||||
*
|
||||
* <p>See the JDBC spec's "Dynamic Programming" chapter for details.
|
||||
*
|
||||
* @param parameterIndex the first parameter is 1, the second is 2,...
|
||||
* @return A java.lang.Object holding the OUT parameter value.
|
||||
* @exception SQLException if a database-access error occurs.
|
||||
*/
|
||||
public Object getObject(int parameterIndex)
|
||||
throws SQLException {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
389
src/interfaces/jdbc/org/postgresql/jdbc1/Connection.java
Normal file
389
src/interfaces/jdbc/org/postgresql/jdbc1/Connection.java
Normal file
@ -0,0 +1,389 @@
|
||||
package org.postgresql.jdbc1;
|
||||
|
||||
// IMPORTANT NOTE: This file implements the JDBC 1 version of the driver.
|
||||
// If you make any modifications to this file, you must make sure that the
|
||||
// changes are also made (if relevent) to the related JDBC 2 class in the
|
||||
// org.postgresql.jdbc2 package.
|
||||
|
||||
import java.io.*;
|
||||
import java.lang.*;
|
||||
import java.lang.reflect.*;
|
||||
import java.net.*;
|
||||
import java.util.*;
|
||||
import java.sql.*;
|
||||
import org.postgresql.Field;
|
||||
import org.postgresql.fastpath.*;
|
||||
import org.postgresql.largeobject.*;
|
||||
import org.postgresql.util.*;
|
||||
|
||||
/**
|
||||
* $Id: Connection.java,v 1.1 2000/04/17 20:07:48 peter Exp $
|
||||
*
|
||||
* A Connection represents a session with a specific database. Within the
|
||||
* context of a Connection, SQL statements are executed and results are
|
||||
* returned.
|
||||
*
|
||||
* <P>A Connection's database is able to provide information describing
|
||||
* its tables, its supported SQL grammar, its stored procedures, the
|
||||
* capabilities of this connection, etc. This information is obtained
|
||||
* with the getMetaData method.
|
||||
*
|
||||
* <p><B>Note:</B> By default, the Connection automatically commits changes
|
||||
* after executing each statement. If auto-commit has been disabled, an
|
||||
* explicit commit must be done or database changes will not be saved.
|
||||
*
|
||||
* @see java.sql.Connection
|
||||
*/
|
||||
public class Connection extends org.postgresql.Connection implements java.sql.Connection
|
||||
{
|
||||
// This is a cache of the DatabaseMetaData instance for this connection
|
||||
protected DatabaseMetaData metadata;
|
||||
|
||||
/**
|
||||
* SQL statements without parameters are normally executed using
|
||||
* Statement objects. If the same SQL statement is executed many
|
||||
* times, it is more efficient to use a PreparedStatement
|
||||
*
|
||||
* @return a new Statement object
|
||||
* @exception SQLException passed through from the constructor
|
||||
*/
|
||||
public java.sql.Statement createStatement() throws SQLException
|
||||
{
|
||||
return new Statement(this);
|
||||
}
|
||||
|
||||
/**
|
||||
* A SQL statement with or without IN parameters can be pre-compiled
|
||||
* and stored in a PreparedStatement object. This object can then
|
||||
* be used to efficiently execute this statement multiple times.
|
||||
*
|
||||
* <B>Note:</B> This method is optimized for handling parametric
|
||||
* SQL statements that benefit from precompilation if the drivers
|
||||
* supports precompilation. PostgreSQL does not support precompilation.
|
||||
* In this case, the statement is not sent to the database until the
|
||||
* PreparedStatement is executed. This has no direct effect on users;
|
||||
* however it does affect which method throws certain SQLExceptions
|
||||
*
|
||||
* @param sql a SQL statement that may contain one or more '?' IN
|
||||
* parameter placeholders
|
||||
* @return a new PreparedStatement object containing the pre-compiled
|
||||
* statement.
|
||||
* @exception SQLException if a database access error occurs.
|
||||
*/
|
||||
public java.sql.PreparedStatement prepareStatement(String sql) throws SQLException
|
||||
{
|
||||
return new PreparedStatement(this, sql);
|
||||
}
|
||||
|
||||
/**
|
||||
* A SQL stored procedure call statement is handled by creating a
|
||||
* CallableStatement for it. The CallableStatement provides methods
|
||||
* for setting up its IN and OUT parameters and methods for executing
|
||||
* it.
|
||||
*
|
||||
* <B>Note:</B> This method is optimised for handling stored procedure
|
||||
* call statements. Some drivers may send the call statement to the
|
||||
* database when the prepareCall is done; others may wait until the
|
||||
* CallableStatement is executed. This has no direct effect on users;
|
||||
* however, it does affect which method throws certain SQLExceptions
|
||||
*
|
||||
* @param sql a SQL statement that may contain one or more '?' parameter
|
||||
* placeholders. Typically this statement is a JDBC function call
|
||||
* escape string.
|
||||
* @return a new CallableStatement object containing the pre-compiled
|
||||
* SQL statement
|
||||
* @exception SQLException if a database access error occurs
|
||||
*/
|
||||
public java.sql.CallableStatement prepareCall(String sql) throws SQLException
|
||||
{
|
||||
throw new PSQLException("postgresql.con.call");
|
||||
// return new CallableStatement(this, sql);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
ExecSQL("begin");
|
||||
this.autoCommit = autoCommit;
|
||||
}
|
||||
|
||||
/**
|
||||
* gets the current auto-commit state
|
||||
*
|
||||
* @return Current state of the auto-commit mode
|
||||
* @exception SQLException (why?)
|
||||
* @see setAutoCommit
|
||||
*/
|
||||
public boolean getAutoCommit() throws SQLException
|
||||
{
|
||||
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;
|
||||
ExecSQL("commit");
|
||||
autoCommit = true;
|
||||
ExecSQL("begin");
|
||||
autoCommit = false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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;
|
||||
ExecSQL("rollback");
|
||||
autoCommit = true;
|
||||
ExecSQL("begin");
|
||||
autoCommit = false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 (pg_stream != null)
|
||||
{
|
||||
try
|
||||
{
|
||||
pg_stream.close();
|
||||
} catch (IOException e) {}
|
||||
pg_stream = null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests to see if a Connection is closed
|
||||
*
|
||||
* @return the status of the connection
|
||||
* @exception SQLException (why?)
|
||||
*/
|
||||
public boolean isClosed() throws SQLException
|
||||
{
|
||||
return (pg_stream == null);
|
||||
}
|
||||
|
||||
/**
|
||||
* A connection's database is able to provide information describing
|
||||
* its tables, its supported SQL grammar, its stored procedures, the
|
||||
* capabilities of this connection, etc. This information is made
|
||||
* available through a DatabaseMetaData object.
|
||||
*
|
||||
* @return a DatabaseMetaData object for this connection
|
||||
* @exception SQLException if a database access error occurs
|
||||
*/
|
||||
public java.sql.DatabaseMetaData getMetaData() throws SQLException
|
||||
{
|
||||
if(metadata==null)
|
||||
metadata = new DatabaseMetaData(this);
|
||||
return metadata;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
|
||||
/**
|
||||
* A sub-space of this Connection's database may be selected by
|
||||
* setting a catalog name. If the driver does not support catalogs,
|
||||
* it will silently ignore this request
|
||||
*
|
||||
* @exception SQLException if a database access error occurs
|
||||
*/
|
||||
public void setCatalog(String catalog) throws SQLException
|
||||
{
|
||||
// No-op
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the connections current catalog name, or null if no
|
||||
* catalog name is set, or we dont support catalogs.
|
||||
*
|
||||
* @return the current catalog name or null
|
||||
* @exception SQLException if a database access error occurs
|
||||
*/
|
||||
public String getCatalog() throws SQLException
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
*
|
||||
* @param level one of the TRANSACTION_* isolation values with
|
||||
* the exception of TRANSACTION_NONE; some databases may
|
||||
* not support other values
|
||||
* @exception SQLException if a database access error occurs
|
||||
* @see java.sql.DatabaseMetaData#supportsTransactionIsolationLevel
|
||||
*/
|
||||
public void setTransactionIsolation(int level) throws SQLException
|
||||
{
|
||||
String q = "SET TRANSACTION ISOLATION LEVEL";
|
||||
|
||||
switch(level) {
|
||||
|
||||
case java.sql.Connection.TRANSACTION_READ_COMMITTED:
|
||||
ExecSQL(q + " READ COMMITTED");
|
||||
return;
|
||||
|
||||
case java.sql.Connection.TRANSACTION_SERIALIZABLE:
|
||||
ExecSQL(q + " SERIALIZABLE");
|
||||
return;
|
||||
|
||||
default:
|
||||
throw new PSQLException("postgresql.con.isolevel",new Integer(level));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
{
|
||||
ExecSQL("show xactisolevel");
|
||||
|
||||
SQLWarning w = getWarnings();
|
||||
if (w != null) {
|
||||
if (w.getMessage().indexOf("READ COMMITTED") != -1) return java.sql.Connection.TRANSACTION_READ_COMMITTED; else
|
||||
if (w.getMessage().indexOf("READ UNCOMMITTED") != -1) return java.sql.Connection.TRANSACTION_READ_UNCOMMITTED; else
|
||||
if (w.getMessage().indexOf("REPEATABLE READ") != -1) return java.sql.Connection.TRANSACTION_REPEATABLE_READ; else
|
||||
if (w.getMessage().indexOf("SERIALIZABLE") != -1) return java.sql.Connection.TRANSACTION_SERIALIZABLE;
|
||||
}
|
||||
return java.sql.Connection.TRANSACTION_READ_COMMITTED;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
|
||||
/**
|
||||
* This overides the method in org.postgresql.Connection and returns a
|
||||
* ResultSet.
|
||||
*/
|
||||
protected java.sql.ResultSet getResultSet(org.postgresql.Connection conn, Field[] fields, Vector tuples, String status, int updateCount) throws SQLException
|
||||
{
|
||||
return new org.postgresql.jdbc1.ResultSet((org.postgresql.jdbc1.Connection)conn,fields,tuples,status,updateCount);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// ***********************************************************************
|
||||
|
2526
src/interfaces/jdbc/org/postgresql/jdbc1/DatabaseMetaData.java
Normal file
2526
src/interfaces/jdbc/org/postgresql/jdbc1/DatabaseMetaData.java
Normal file
File diff suppressed because it is too large
Load Diff
600
src/interfaces/jdbc/org/postgresql/jdbc1/PreparedStatement.java
Normal file
600
src/interfaces/jdbc/org/postgresql/jdbc1/PreparedStatement.java
Normal file
@ -0,0 +1,600 @@
|
||||
package org.postgresql.jdbc1;
|
||||
|
||||
// IMPORTANT NOTE: This file implements the JDBC 1 version of the driver.
|
||||
// If you make any modifications to this file, you must make sure that the
|
||||
// changes are also made (if relevent) to the related JDBC 2 class in the
|
||||
// org.postgresql.jdbc2 package.
|
||||
|
||||
import java.io.*;
|
||||
import java.math.*;
|
||||
import java.sql.*;
|
||||
import java.text.*;
|
||||
import java.util.*;
|
||||
import org.postgresql.largeobject.*;
|
||||
import org.postgresql.util.*;
|
||||
|
||||
/**
|
||||
* A SQL Statement is pre-compiled and stored in a PreparedStatement object.
|
||||
* This object can then be used to efficiently execute this statement multiple
|
||||
* times.
|
||||
*
|
||||
* <p><B>Note:</B> The setXXX methods for setting IN parameter values must
|
||||
* specify types that are compatible with the defined SQL type of the input
|
||||
* parameter. For instance, if the IN parameter has SQL type Integer, then
|
||||
* setInt should be used.
|
||||
*
|
||||
* <p>If arbitrary parameter type conversions are required, then the setObject
|
||||
* method should be used with a target SQL type.
|
||||
*
|
||||
* @see ResultSet
|
||||
* @see java.sql.PreparedStatement
|
||||
*/
|
||||
public class PreparedStatement extends Statement implements java.sql.PreparedStatement
|
||||
{
|
||||
String sql;
|
||||
String[] templateStrings;
|
||||
String[] inStrings;
|
||||
Connection connection;
|
||||
|
||||
/**
|
||||
* Constructor for the PreparedStatement class.
|
||||
* Split the SQL statement into segments - separated by the arguments.
|
||||
* When we rebuild the thing with the arguments, we can substitute the
|
||||
* args and join the whole thing together.
|
||||
*
|
||||
* @param conn the instanatiating connection
|
||||
* @param sql the SQL statement with ? for IN markers
|
||||
* @exception SQLException if something bad occurs
|
||||
*/
|
||||
public PreparedStatement(Connection connection, String sql) throws SQLException
|
||||
{
|
||||
super(connection);
|
||||
|
||||
Vector v = new Vector();
|
||||
boolean inQuotes = false;
|
||||
int lastParmEnd = 0, i;
|
||||
|
||||
this.sql = sql;
|
||||
this.connection = connection;
|
||||
for (i = 0; i < sql.length(); ++i)
|
||||
{
|
||||
int c = sql.charAt(i);
|
||||
|
||||
if (c == '\'')
|
||||
inQuotes = !inQuotes;
|
||||
if (c == '?' && !inQuotes)
|
||||
{
|
||||
v.addElement(sql.substring (lastParmEnd, i));
|
||||
lastParmEnd = i + 1;
|
||||
}
|
||||
}
|
||||
v.addElement(sql.substring (lastParmEnd, sql.length()));
|
||||
|
||||
templateStrings = new String[v.size()];
|
||||
inStrings = new String[v.size() - 1];
|
||||
clearParameters();
|
||||
|
||||
for (i = 0 ; i < templateStrings.length; ++i)
|
||||
templateStrings[i] = (String)v.elementAt(i);
|
||||
}
|
||||
|
||||
/**
|
||||
* A Prepared SQL query is executed and its ResultSet is returned
|
||||
*
|
||||
* @return a ResultSet that contains the data produced by the
|
||||
* query - never null
|
||||
* @exception SQLException if a database access error occurs
|
||||
*/
|
||||
public java.sql.ResultSet executeQuery() throws SQLException
|
||||
{
|
||||
StringBuffer s = new StringBuffer();
|
||||
int i;
|
||||
|
||||
for (i = 0 ; i < inStrings.length ; ++i)
|
||||
{
|
||||
if (inStrings[i] == null)
|
||||
throw new PSQLException("postgresql.prep.param",new Integer(i + 1));
|
||||
s.append (templateStrings[i]);
|
||||
s.append (inStrings[i]);
|
||||
}
|
||||
s.append(templateStrings[inStrings.length]);
|
||||
return super.executeQuery(s.toString()); // in Statement class
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute a SQL INSERT, UPDATE or DELETE statement. In addition,
|
||||
* SQL statements that return nothing such as SQL DDL statements can
|
||||
* be executed.
|
||||
*
|
||||
* @return either the row count for INSERT, UPDATE or DELETE; or
|
||||
* 0 for SQL statements that return nothing.
|
||||
* @exception SQLException if a database access error occurs
|
||||
*/
|
||||
public int executeUpdate() throws SQLException
|
||||
{
|
||||
StringBuffer s = new StringBuffer();
|
||||
int i;
|
||||
|
||||
for (i = 0 ; i < inStrings.length ; ++i)
|
||||
{
|
||||
if (inStrings[i] == null)
|
||||
throw new PSQLException("postgresql.prep.param",new Integer(i + 1));
|
||||
s.append (templateStrings[i]);
|
||||
s.append (inStrings[i]);
|
||||
}
|
||||
s.append(templateStrings[inStrings.length]);
|
||||
return super.executeUpdate(s.toString()); // in Statement class
|
||||
}
|
||||
|
||||
/**
|
||||
* Set a parameter to SQL NULL
|
||||
*
|
||||
* <p><B>Note:</B> You must specify the parameters SQL type (although
|
||||
* PostgreSQL ignores it)
|
||||
*
|
||||
* @param parameterIndex the first parameter is 1, etc...
|
||||
* @param sqlType the SQL type code defined in java.sql.Types
|
||||
* @exception SQLException if a database access error occurs
|
||||
*/
|
||||
public void setNull(int parameterIndex, int sqlType) throws SQLException
|
||||
{
|
||||
set(parameterIndex, "null");
|
||||
}
|
||||
|
||||
/**
|
||||
* Set a parameter to a Java boolean value. The driver converts this
|
||||
* to a SQL BIT value when it sends it to the database.
|
||||
*
|
||||
* @param parameterIndex the first parameter is 1...
|
||||
* @param x the parameter value
|
||||
* @exception SQLException if a database access error occurs
|
||||
*/
|
||||
public void setBoolean(int parameterIndex, boolean x) throws SQLException
|
||||
{
|
||||
set(parameterIndex, x ? "'t'" : "'f'");
|
||||
}
|
||||
|
||||
/**
|
||||
* Set a parameter to a Java byte value. The driver converts this to
|
||||
* a SQL TINYINT value when it sends it to the database.
|
||||
*
|
||||
* @param parameterIndex the first parameter is 1...
|
||||
* @param x the parameter value
|
||||
* @exception SQLException if a database access error occurs
|
||||
*/
|
||||
public void setByte(int parameterIndex, byte x) throws SQLException
|
||||
{
|
||||
set(parameterIndex, (new Integer(x)).toString());
|
||||
}
|
||||
|
||||
/**
|
||||
* Set a parameter to a Java short value. The driver converts this
|
||||
* to a SQL SMALLINT value when it sends it to the database.
|
||||
*
|
||||
* @param parameterIndex the first parameter is 1...
|
||||
* @param x the parameter value
|
||||
* @exception SQLException if a database access error occurs
|
||||
*/
|
||||
public void setShort(int parameterIndex, short x) throws SQLException
|
||||
{
|
||||
set(parameterIndex, (new Integer(x)).toString());
|
||||
}
|
||||
|
||||
/**
|
||||
* Set a parameter to a Java int value. The driver converts this to
|
||||
* a SQL INTEGER value when it sends it to the database.
|
||||
*
|
||||
* @param parameterIndex the first parameter is 1...
|
||||
* @param x the parameter value
|
||||
* @exception SQLException if a database access error occurs
|
||||
*/
|
||||
public void setInt(int parameterIndex, int x) throws SQLException
|
||||
{
|
||||
set(parameterIndex, (new Integer(x)).toString());
|
||||
}
|
||||
|
||||
/**
|
||||
* Set a parameter to a Java long value. The driver converts this to
|
||||
* a SQL BIGINT value when it sends it to the database.
|
||||
*
|
||||
* @param parameterIndex the first parameter is 1...
|
||||
* @param x the parameter value
|
||||
* @exception SQLException if a database access error occurs
|
||||
*/
|
||||
public void setLong(int parameterIndex, long x) throws SQLException
|
||||
{
|
||||
set(parameterIndex, (new Long(x)).toString());
|
||||
}
|
||||
|
||||
/**
|
||||
* Set a parameter to a Java float value. The driver converts this
|
||||
* to a SQL FLOAT value when it sends it to the database.
|
||||
*
|
||||
* @param parameterIndex the first parameter is 1...
|
||||
* @param x the parameter value
|
||||
* @exception SQLException if a database access error occurs
|
||||
*/
|
||||
public void setFloat(int parameterIndex, float x) throws SQLException
|
||||
{
|
||||
set(parameterIndex, (new Float(x)).toString());
|
||||
}
|
||||
|
||||
/**
|
||||
* Set a parameter to a Java double value. The driver converts this
|
||||
* to a SQL DOUBLE value when it sends it to the database
|
||||
*
|
||||
* @param parameterIndex the first parameter is 1...
|
||||
* @param x the parameter value
|
||||
* @exception SQLException if a database access error occurs
|
||||
*/
|
||||
public void setDouble(int parameterIndex, double x) throws SQLException
|
||||
{
|
||||
set(parameterIndex, (new Double(x)).toString());
|
||||
}
|
||||
|
||||
/**
|
||||
* Set a parameter to a java.lang.BigDecimal value. The driver
|
||||
* converts this to a SQL NUMERIC value when it sends it to the
|
||||
* database.
|
||||
*
|
||||
* @param parameterIndex the first parameter is 1...
|
||||
* @param x the parameter value
|
||||
* @exception SQLException if a database access error occurs
|
||||
*/
|
||||
public void setBigDecimal(int parameterIndex, BigDecimal x) throws SQLException
|
||||
{
|
||||
set(parameterIndex, x.toString());
|
||||
}
|
||||
|
||||
/**
|
||||
* Set a parameter to a Java String value. The driver converts this
|
||||
* to a SQL VARCHAR or LONGVARCHAR value (depending on the arguments
|
||||
* size relative to the driver's limits on VARCHARs) when it sends it
|
||||
* to the database.
|
||||
*
|
||||
* @param parameterIndex the first parameter is 1...
|
||||
* @param x the parameter value
|
||||
* @exception SQLException if a database access error occurs
|
||||
*/
|
||||
public void setString(int parameterIndex, String x) throws SQLException
|
||||
{
|
||||
// if the passed string is null, then set this column to null
|
||||
if(x==null)
|
||||
set(parameterIndex,"null");
|
||||
else {
|
||||
StringBuffer b = new StringBuffer();
|
||||
int i;
|
||||
|
||||
b.append('\'');
|
||||
for (i = 0 ; i < x.length() ; ++i)
|
||||
{
|
||||
char c = x.charAt(i);
|
||||
if (c == '\\' || c == '\'')
|
||||
b.append((char)'\\');
|
||||
b.append(c);
|
||||
}
|
||||
b.append('\'');
|
||||
set(parameterIndex, b.toString());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Set a parameter to a Java array of bytes. The driver converts this
|
||||
* to a SQL VARBINARY or LONGVARBINARY (depending on the argument's
|
||||
* size relative to the driver's limits on VARBINARYs) when it sends
|
||||
* it to the database.
|
||||
*
|
||||
* <p>Implementation note:
|
||||
* <br>With org.postgresql, this creates a large object, and stores the
|
||||
* objects oid in this column.
|
||||
*
|
||||
* @param parameterIndex the first parameter is 1...
|
||||
* @param x the parameter value
|
||||
* @exception SQLException if a database access error occurs
|
||||
*/
|
||||
public void setBytes(int parameterIndex, byte x[]) throws SQLException
|
||||
{
|
||||
LargeObjectManager lom = connection.getLargeObjectAPI();
|
||||
int oid = lom.create();
|
||||
LargeObject lob = lom.open(oid);
|
||||
lob.write(x);
|
||||
lob.close();
|
||||
setInt(parameterIndex,oid);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set a parameter to a java.sql.Date value. The driver converts this
|
||||
* to a SQL DATE value when it sends it to the database.
|
||||
*
|
||||
* @param parameterIndex the first parameter is 1...
|
||||
* @param x the parameter value
|
||||
* @exception SQLException if a database access error occurs
|
||||
*/
|
||||
public void setDate(int parameterIndex, java.sql.Date x) throws SQLException
|
||||
{
|
||||
SimpleDateFormat df = new SimpleDateFormat("''yyyy-MM-dd''");
|
||||
|
||||
set(parameterIndex, df.format(x));
|
||||
|
||||
// The above is how the date should be handled.
|
||||
//
|
||||
// However, in JDK's prior to 1.1.6 (confirmed with the
|
||||
// Linux jdk1.1.3 and the Win95 JRE1.1.5), SimpleDateFormat seems
|
||||
// to format a date to the previous day. So the fix is to add a day
|
||||
// before formatting.
|
||||
//
|
||||
// PS: 86400000 is one day
|
||||
//
|
||||
//set(parameterIndex, df.format(new java.util.Date(x.getTime()+86400000)));
|
||||
}
|
||||
|
||||
/**
|
||||
* Set a parameter to a java.sql.Time value. The driver converts
|
||||
* this to a SQL TIME value when it sends it to the database.
|
||||
*
|
||||
* @param parameterIndex the first parameter is 1...));
|
||||
* @param x the parameter value
|
||||
* @exception SQLException if a database access error occurs
|
||||
*/
|
||||
public void setTime(int parameterIndex, Time x) throws SQLException
|
||||
{
|
||||
set(parameterIndex, "'" + x.toString() + "'");
|
||||
}
|
||||
|
||||
/**
|
||||
* Set a parameter to a java.sql.Timestamp value. The driver converts
|
||||
* this to a SQL TIMESTAMP value when it sends it to the database.
|
||||
*
|
||||
* @param parameterIndex the first parameter is 1...
|
||||
* @param x the parameter value
|
||||
* @exception SQLException if a database access error occurs
|
||||
*/
|
||||
public void setTimestamp(int parameterIndex, Timestamp x) throws SQLException
|
||||
{
|
||||
set(parameterIndex, "'" + x.toString() + "'");
|
||||
}
|
||||
|
||||
/**
|
||||
* When a very large ASCII value is input to a LONGVARCHAR parameter,
|
||||
* it may be more practical to send it via a java.io.InputStream.
|
||||
* JDBC will read the data from the stream as needed, until it reaches
|
||||
* end-of-file. The JDBC driver will do any necessary conversion from
|
||||
* ASCII to the database char format.
|
||||
*
|
||||
* <P><B>Note:</B> This stream object can either be a standard Java
|
||||
* stream object or your own subclass that implements the standard
|
||||
* interface.
|
||||
*
|
||||
* @param parameterIndex the first parameter is 1...
|
||||
* @param x the parameter value
|
||||
* @param length the number of bytes in the stream
|
||||
* @exception SQLException if a database access error occurs
|
||||
*/
|
||||
public void setAsciiStream(int parameterIndex, InputStream x, int length) throws SQLException
|
||||
{
|
||||
setBinaryStream(parameterIndex, x, length);
|
||||
}
|
||||
|
||||
/**
|
||||
* When a very large Unicode value is input to a LONGVARCHAR parameter,
|
||||
* it may be more practical to send it via a java.io.InputStream.
|
||||
* JDBC will read the data from the stream as needed, until it reaches
|
||||
* end-of-file. The JDBC driver will do any necessary conversion from
|
||||
* UNICODE to the database char format.
|
||||
*
|
||||
* <P><B>Note:</B> This stream object can either be a standard Java
|
||||
* stream object or your own subclass that implements the standard
|
||||
* interface.
|
||||
*
|
||||
* @param parameterIndex the first parameter is 1...
|
||||
* @param x the parameter value
|
||||
* @exception SQLException if a database access error occurs
|
||||
*/
|
||||
public void setUnicodeStream(int parameterIndex, InputStream x, int length) throws SQLException
|
||||
{
|
||||
setBinaryStream(parameterIndex, x, length);
|
||||
}
|
||||
|
||||
/**
|
||||
* When a very large binary value is input to a LONGVARBINARY parameter,
|
||||
* it may be more practical to send it via a java.io.InputStream.
|
||||
* JDBC will read the data from the stream as needed, until it reaches
|
||||
* end-of-file.
|
||||
*
|
||||
* <P><B>Note:</B> This stream object can either be a standard Java
|
||||
* stream object or your own subclass that implements the standard
|
||||
* interface.
|
||||
*
|
||||
* @param parameterIndex the first parameter is 1...
|
||||
* @param x the parameter value
|
||||
* @exception SQLException if a database access error occurs
|
||||
*/
|
||||
public void setBinaryStream(int parameterIndex, InputStream x, int length) throws SQLException
|
||||
{
|
||||
throw org.postgresql.Driver.notImplemented();
|
||||
}
|
||||
|
||||
/**
|
||||
* In general, parameter values remain in force for repeated used of a
|
||||
* Statement. Setting a parameter value automatically clears its
|
||||
* previous value. However, in coms cases, it is useful to immediately
|
||||
* release the resources used by the current parameter values; this
|
||||
* can be done by calling clearParameters
|
||||
*
|
||||
* @exception SQLException if a database access error occurs
|
||||
*/
|
||||
public void clearParameters() throws SQLException
|
||||
{
|
||||
int i;
|
||||
|
||||
for (i = 0 ; i < inStrings.length ; i++)
|
||||
inStrings[i] = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the value of a parameter using an object; use the java.lang
|
||||
* equivalent objects for integral values.
|
||||
*
|
||||
* <P>The given Java object will be converted to the targetSqlType before
|
||||
* being sent to the database.
|
||||
*
|
||||
* <P>note that this method may be used to pass database-specific
|
||||
* abstract data types. This is done by using a Driver-specific
|
||||
* Java type and using a targetSqlType of java.sql.Types.OTHER
|
||||
*
|
||||
* @param parameterIndex the first parameter is 1...
|
||||
* @param x the object containing the input parameter value
|
||||
* @param targetSqlType The SQL type to be send to the database
|
||||
* @param scale For java.sql.Types.DECIMAL or java.sql.Types.NUMERIC
|
||||
* types this is the number of digits after the decimal. For
|
||||
* all other types this value will be ignored.
|
||||
* @exception SQLException if a database access error occurs
|
||||
*/
|
||||
public void setObject(int parameterIndex, Object x, int targetSqlType, int scale) throws SQLException
|
||||
{
|
||||
switch (targetSqlType)
|
||||
{
|
||||
case Types.TINYINT:
|
||||
case Types.SMALLINT:
|
||||
case Types.INTEGER:
|
||||
case Types.BIGINT:
|
||||
case Types.REAL:
|
||||
case Types.FLOAT:
|
||||
case Types.DOUBLE:
|
||||
case Types.DECIMAL:
|
||||
case Types.NUMERIC:
|
||||
if (x instanceof Boolean)
|
||||
set(parameterIndex, ((Boolean)x).booleanValue() ? "1" : "0");
|
||||
else
|
||||
set(parameterIndex, x.toString());
|
||||
break;
|
||||
case Types.CHAR:
|
||||
case Types.VARCHAR:
|
||||
case Types.LONGVARCHAR:
|
||||
setString(parameterIndex, x.toString());
|
||||
break;
|
||||
case Types.DATE:
|
||||
setDate(parameterIndex, (java.sql.Date)x);
|
||||
break;
|
||||
case Types.TIME:
|
||||
setTime(parameterIndex, (Time)x);
|
||||
break;
|
||||
case Types.TIMESTAMP:
|
||||
setTimestamp(parameterIndex, (Timestamp)x);
|
||||
break;
|
||||
case Types.OTHER:
|
||||
setString(parameterIndex, ((PGobject)x).getValue());
|
||||
break;
|
||||
default:
|
||||
throw new PSQLException("postgresql.prep.type");
|
||||
}
|
||||
}
|
||||
|
||||
public void setObject(int parameterIndex, Object x, int targetSqlType) throws SQLException
|
||||
{
|
||||
setObject(parameterIndex, x, targetSqlType, 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* This stores an Object into a parameter.
|
||||
* <p>New for 6.4, if the object is not recognised, but it is
|
||||
* Serializable, then the object is serialised using the
|
||||
* org.postgresql.util.Serialize class.
|
||||
*/
|
||||
public void setObject(int parameterIndex, Object x) throws SQLException
|
||||
{
|
||||
if (x instanceof String)
|
||||
setString(parameterIndex, (String)x);
|
||||
else if (x instanceof BigDecimal)
|
||||
setBigDecimal(parameterIndex, (BigDecimal)x);
|
||||
else if (x instanceof Short)
|
||||
setShort(parameterIndex, ((Short)x).shortValue());
|
||||
else if (x instanceof Integer)
|
||||
setInt(parameterIndex, ((Integer)x).intValue());
|
||||
else if (x instanceof Long)
|
||||
setLong(parameterIndex, ((Long)x).longValue());
|
||||
else if (x instanceof Float)
|
||||
setFloat(parameterIndex, ((Float)x).floatValue());
|
||||
else if (x instanceof Double)
|
||||
setDouble(parameterIndex, ((Double)x).doubleValue());
|
||||
else if (x instanceof byte[])
|
||||
setBytes(parameterIndex, (byte[])x);
|
||||
else if (x instanceof java.sql.Date)
|
||||
setDate(parameterIndex, (java.sql.Date)x);
|
||||
else if (x instanceof Time)
|
||||
setTime(parameterIndex, (Time)x);
|
||||
else if (x instanceof Timestamp)
|
||||
setTimestamp(parameterIndex, (Timestamp)x);
|
||||
else if (x instanceof Boolean)
|
||||
setBoolean(parameterIndex, ((Boolean)x).booleanValue());
|
||||
else if (x instanceof PGobject)
|
||||
setString(parameterIndex, ((PGobject)x).getValue());
|
||||
else
|
||||
setLong(parameterIndex, connection.putObject(x));
|
||||
}
|
||||
|
||||
/**
|
||||
* Some prepared statements return multiple results; the execute method
|
||||
* handles these complex statements as well as the simpler form of
|
||||
* statements handled by executeQuery and executeUpdate
|
||||
*
|
||||
* @return true if the next result is a ResultSet; false if it is an
|
||||
* update count or there are no more results
|
||||
* @exception SQLException if a database access error occurs
|
||||
*/
|
||||
public boolean execute() throws SQLException
|
||||
{
|
||||
StringBuffer s = new StringBuffer();
|
||||
int i;
|
||||
|
||||
for (i = 0 ; i < inStrings.length ; ++i)
|
||||
{
|
||||
if (inStrings[i] == null)
|
||||
throw new PSQLException("postgresql.prep.param",new Integer(i + 1));
|
||||
s.append (templateStrings[i]);
|
||||
s.append (inStrings[i]);
|
||||
}
|
||||
s.append(templateStrings[inStrings.length]);
|
||||
return super.execute(s.toString()); // in Statement class
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the SQL statement with the current template values
|
||||
* substituted.
|
||||
*/
|
||||
public String toString() {
|
||||
StringBuffer s = new StringBuffer();
|
||||
int i;
|
||||
|
||||
for (i = 0 ; i < inStrings.length ; ++i)
|
||||
{
|
||||
if (inStrings[i] == null)
|
||||
s.append( '?' );
|
||||
else
|
||||
s.append (templateStrings[i]);
|
||||
s.append (inStrings[i]);
|
||||
}
|
||||
s.append(templateStrings[inStrings.length]);
|
||||
return s.toString();
|
||||
}
|
||||
|
||||
// **************************************************************
|
||||
// END OF PUBLIC INTERFACE
|
||||
// **************************************************************
|
||||
|
||||
/**
|
||||
* There are a lot of setXXX classes which all basically do
|
||||
* the same thing. We need a method which actually does the
|
||||
* set for us.
|
||||
*
|
||||
* @param paramIndex the index into the inString
|
||||
* @param s a string to be stored
|
||||
* @exception SQLException if something goes wrong
|
||||
*/
|
||||
private void set(int paramIndex, String s) throws SQLException
|
||||
{
|
||||
if (paramIndex < 1 || paramIndex > inStrings.length)
|
||||
throw new PSQLException("postgresql.prep.range");
|
||||
inStrings[paramIndex - 1] = s;
|
||||
}
|
||||
}
|
768
src/interfaces/jdbc/org/postgresql/jdbc1/ResultSet.java
Normal file
768
src/interfaces/jdbc/org/postgresql/jdbc1/ResultSet.java
Normal file
@ -0,0 +1,768 @@
|
||||
package org.postgresql.jdbc1;
|
||||
|
||||
// IMPORTANT NOTE: This file implements the JDBC 1 version of the driver.
|
||||
// If you make any modifications to this file, you must make sure that the
|
||||
// changes are also made (if relevent) to the related JDBC 2 class in the
|
||||
// org.postgresql.jdbc2 package.
|
||||
|
||||
import java.lang.*;
|
||||
import java.io.*;
|
||||
import java.math.*;
|
||||
import java.text.*;
|
||||
import java.util.*;
|
||||
import java.sql.*;
|
||||
import org.postgresql.Field;
|
||||
import org.postgresql.largeobject.*;
|
||||
import org.postgresql.util.*;
|
||||
|
||||
/**
|
||||
* A ResultSet provides access to a table of data generated by executing a
|
||||
* Statement. The table rows are retrieved in sequence. Within a row its
|
||||
* column values can be accessed in any order.
|
||||
*
|
||||
* <P>A ResultSet maintains a cursor pointing to its current row of data.
|
||||
* Initially the cursor is positioned before the first row. The 'next'
|
||||
* method moves the cursor to the next row.
|
||||
*
|
||||
* <P>The getXXX methods retrieve column values for the current row. You can
|
||||
* retrieve values either using the index number of the column, or by using
|
||||
* the name of the column. In general using the column index will be more
|
||||
* efficient. Columns are numbered from 1.
|
||||
*
|
||||
* <P>For maximum portability, ResultSet columns within each row should be read
|
||||
* in left-to-right order and each column should be read only once.
|
||||
*
|
||||
*<P> For the getXXX methods, the JDBC driver attempts to convert the
|
||||
* underlying data to the specified Java type and returns a suitable Java
|
||||
* value. See the JDBC specification for allowable mappings from SQL types
|
||||
* to Java types with the ResultSet getXXX methods.
|
||||
*
|
||||
* <P>Column names used as input to getXXX methods are case insenstive. When
|
||||
* performing a getXXX using a column name, if several columns have the same
|
||||
* name, then the value of the first matching column will be returned. The
|
||||
* column name option is designed to be used when column names are used in the
|
||||
* SQL Query. For columns that are NOT explicitly named in the query, it is
|
||||
* best to use column numbers. If column names were used there is no way for
|
||||
* the programmer to guarentee that they actually refer to the intended
|
||||
* columns.
|
||||
*
|
||||
* <P>A ResultSet is automatically closed by the Statement that generated it
|
||||
* when that Statement is closed, re-executed, or is used to retrieve the
|
||||
* next result from a sequence of multiple results.
|
||||
*
|
||||
* <P>The number, types and properties of a ResultSet's columns are provided by
|
||||
* the ResultSetMetaData object returned by the getMetaData method.
|
||||
*
|
||||
* @see ResultSetMetaData
|
||||
* @see java.sql.ResultSet
|
||||
*/
|
||||
public class ResultSet extends org.postgresql.ResultSet implements java.sql.ResultSet
|
||||
{
|
||||
/**
|
||||
* Create a new ResultSet - Note that we create ResultSets to
|
||||
* represent the results of everything.
|
||||
*
|
||||
* @param fields an array of Field objects (basically, the
|
||||
* ResultSet MetaData)
|
||||
* @param tuples Vector of the actual data
|
||||
* @param status the status string returned from the back end
|
||||
* @param updateCount the number of rows affected by the operation
|
||||
* @param cursor the positioned update/delete cursor name
|
||||
*/
|
||||
public ResultSet(Connection conn, Field[] fields, Vector tuples, String status, int updateCount)
|
||||
{
|
||||
super(conn,fields,tuples,status,updateCount);
|
||||
}
|
||||
|
||||
/**
|
||||
* A ResultSet is initially positioned before its first row,
|
||||
* the first call to next makes the first row the current row;
|
||||
* the second call makes the second row the current row, etc.
|
||||
*
|
||||
* <p>If an input stream from the previous row is open, it is
|
||||
* implicitly closed. The ResultSet's warning chain is cleared
|
||||
* when a new row is read
|
||||
*
|
||||
* @return true if the new current is valid; false if there are no
|
||||
* more rows
|
||||
* @exception SQLException if a database access error occurs
|
||||
*/
|
||||
public boolean next() throws SQLException
|
||||
{
|
||||
if (++current_row >= rows.size())
|
||||
return false;
|
||||
this_row = (byte [][])rows.elementAt(current_row);
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* In some cases, it is desirable to immediately release a ResultSet
|
||||
* database and JDBC resources instead of waiting for this to happen
|
||||
* when it is automatically closed. The close method provides this
|
||||
* immediate release.
|
||||
*
|
||||
* <p><B>Note:</B> A ResultSet is automatically closed by the Statement
|
||||
* the Statement that generated it when that Statement is closed,
|
||||
* re-executed, or is used to retrieve the next result from a sequence
|
||||
* of multiple results. A ResultSet is also automatically closed
|
||||
* when it is garbage collected.
|
||||
*
|
||||
* @exception SQLException if a database access error occurs
|
||||
*/
|
||||
public void close() throws SQLException
|
||||
{
|
||||
// No-op
|
||||
}
|
||||
|
||||
/**
|
||||
* A column may have the value of SQL NULL; wasNull() reports whether
|
||||
* the last column read had this special value. Note that you must
|
||||
* first call getXXX on a column to try to read its value and then
|
||||
* call wasNull() to find if the value was SQL NULL
|
||||
*
|
||||
* @return true if the last column read was SQL NULL
|
||||
* @exception SQLException if a database access error occurred
|
||||
*/
|
||||
public boolean wasNull() throws SQLException
|
||||
{
|
||||
return wasNullFlag;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the value of a column in the current row as a Java String
|
||||
*
|
||||
* @param columnIndex the first column is 1, the second is 2...
|
||||
* @return the column value, null for SQL NULL
|
||||
* @exception SQLException if a database access error occurs
|
||||
*/
|
||||
public String getString(int columnIndex) throws SQLException
|
||||
{
|
||||
//byte[] bytes = getBytes(columnIndex);
|
||||
//
|
||||
//if (bytes == null)
|
||||
//return null;
|
||||
//return new String(bytes);
|
||||
if (columnIndex < 1 || columnIndex > fields.length)
|
||||
throw new PSQLException("postgresql.res.colrange");
|
||||
wasNullFlag = (this_row[columnIndex - 1] == null);
|
||||
if(wasNullFlag)
|
||||
return null;
|
||||
return new String(this_row[columnIndex - 1]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the value of a column in the current row as a Java boolean
|
||||
*
|
||||
* @param columnIndex the first column is 1, the second is 2...
|
||||
* @return the column value, false for SQL NULL
|
||||
* @exception SQLException if a database access error occurs
|
||||
*/
|
||||
public boolean getBoolean(int columnIndex) throws SQLException
|
||||
{
|
||||
String s = getString(columnIndex);
|
||||
|
||||
if (s != null)
|
||||
{
|
||||
int c = s.charAt(0);
|
||||
return ((c == 't') || (c == 'T'));
|
||||
}
|
||||
return false; // SQL NULL
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the value of a column in the current row as a Java byte.
|
||||
*
|
||||
* @param columnIndex the first column is 1, the second is 2,...
|
||||
* @return the column value; 0 if SQL NULL
|
||||
* @exception SQLException if a database access error occurs
|
||||
*/
|
||||
public byte getByte(int columnIndex) throws SQLException
|
||||
{
|
||||
String s = getString(columnIndex);
|
||||
|
||||
if (s != null)
|
||||
{
|
||||
try
|
||||
{
|
||||
return Byte.parseByte(s);
|
||||
} catch (NumberFormatException e) {
|
||||
throw new PSQLException("postgresql.res.badbyte",s);
|
||||
}
|
||||
}
|
||||
return 0; // SQL NULL
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the value of a column in the current row as a Java short.
|
||||
*
|
||||
* @param columnIndex the first column is 1, the second is 2,...
|
||||
* @return the column value; 0 if SQL NULL
|
||||
* @exception SQLException if a database access error occurs
|
||||
*/
|
||||
public short getShort(int columnIndex) throws SQLException
|
||||
{
|
||||
String s = getString(columnIndex);
|
||||
|
||||
if (s != null)
|
||||
{
|
||||
try
|
||||
{
|
||||
return Short.parseShort(s);
|
||||
} catch (NumberFormatException e) {
|
||||
throw new PSQLException("postgresql.res.badshort",s);
|
||||
}
|
||||
}
|
||||
return 0; // SQL NULL
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the value of a column in the current row as a Java int.
|
||||
*
|
||||
* @param columnIndex the first column is 1, the second is 2,...
|
||||
* @return the column value; 0 if SQL NULL
|
||||
* @exception SQLException if a database access error occurs
|
||||
*/
|
||||
public int getInt(int columnIndex) throws SQLException
|
||||
{
|
||||
String s = getString(columnIndex);
|
||||
|
||||
if (s != null)
|
||||
{
|
||||
try
|
||||
{
|
||||
return Integer.parseInt(s);
|
||||
} catch (NumberFormatException e) {
|
||||
throw new PSQLException ("postgresql.badint",s);
|
||||
}
|
||||
}
|
||||
return 0; // SQL NULL
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the value of a column in the current row as a Java long.
|
||||
*
|
||||
* @param columnIndex the first column is 1, the second is 2,...
|
||||
* @return the column value; 0 if SQL NULL
|
||||
* @exception SQLException if a database access error occurs
|
||||
*/
|
||||
public long getLong(int columnIndex) throws SQLException
|
||||
{
|
||||
String s = getString(columnIndex);
|
||||
|
||||
if (s != null)
|
||||
{
|
||||
try
|
||||
{
|
||||
return Long.parseLong(s);
|
||||
} catch (NumberFormatException e) {
|
||||
throw new PSQLException ("postgresql.res.badlong",s);
|
||||
}
|
||||
}
|
||||
return 0; // SQL NULL
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the value of a column in the current row as a Java float.
|
||||
*
|
||||
* @param columnIndex the first column is 1, the second is 2,...
|
||||
* @return the column value; 0 if SQL NULL
|
||||
* @exception SQLException if a database access error occurs
|
||||
*/
|
||||
public float getFloat(int columnIndex) throws SQLException
|
||||
{
|
||||
String s = getString(columnIndex);
|
||||
|
||||
if (s != null)
|
||||
{
|
||||
try
|
||||
{
|
||||
return Float.valueOf(s).floatValue();
|
||||
} catch (NumberFormatException e) {
|
||||
throw new PSQLException ("postgresql.res.badfloat",s);
|
||||
}
|
||||
}
|
||||
return 0; // SQL NULL
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the value of a column in the current row as a Java double.
|
||||
*
|
||||
* @param columnIndex the first column is 1, the second is 2,...
|
||||
* @return the column value; 0 if SQL NULL
|
||||
* @exception SQLException if a database access error occurs
|
||||
*/
|
||||
public double getDouble(int columnIndex) throws SQLException
|
||||
{
|
||||
String s = getString(columnIndex);
|
||||
|
||||
if (s != null)
|
||||
{
|
||||
try
|
||||
{
|
||||
return Double.valueOf(s).doubleValue();
|
||||
} catch (NumberFormatException e) {
|
||||
throw new PSQLException ("postgresql.res.baddouble",s);
|
||||
}
|
||||
}
|
||||
return 0; // SQL NULL
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the value of a column in the current row as a
|
||||
* java.math.BigDecimal object
|
||||
*
|
||||
* @param columnIndex the first column is 1, the second is 2...
|
||||
* @param scale the number of digits to the right of the decimal
|
||||
* @return the column value; if the value is SQL NULL, null
|
||||
* @exception SQLException if a database access error occurs
|
||||
*/
|
||||
public BigDecimal getBigDecimal(int columnIndex, int scale) throws SQLException
|
||||
{
|
||||
String s = getString(columnIndex);
|
||||
BigDecimal val;
|
||||
|
||||
if (s != null)
|
||||
{
|
||||
try
|
||||
{
|
||||
val = new BigDecimal(s);
|
||||
} catch (NumberFormatException e) {
|
||||
throw new PSQLException ("postgresql.res.badbigdec",s);
|
||||
}
|
||||
try
|
||||
{
|
||||
return val.setScale(scale);
|
||||
} catch (ArithmeticException e) {
|
||||
throw new PSQLException ("postgresql.res.badbigdec",s);
|
||||
}
|
||||
}
|
||||
return null; // SQL NULL
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the value of a column in the current row as a Java byte array.
|
||||
*
|
||||
* <p>In normal use, the bytes represent the raw values returned by the
|
||||
* backend. However, if the column is an OID, then it is assumed to
|
||||
* refer to a Large Object, and that object is returned as a byte array.
|
||||
*
|
||||
* <p><b>Be warned</b> If the large object is huge, then you may run out
|
||||
* of memory.
|
||||
*
|
||||
* @param columnIndex the first column is 1, the second is 2, ...
|
||||
* @return the column value; if the value is SQL NULL, the result
|
||||
* is null
|
||||
* @exception SQLException if a database access error occurs
|
||||
*/
|
||||
public byte[] getBytes(int columnIndex) throws SQLException
|
||||
{
|
||||
if (columnIndex < 1 || columnIndex > fields.length)
|
||||
throw new PSQLException("postgresql.res.colrange");
|
||||
wasNullFlag = (this_row[columnIndex - 1] == null);
|
||||
|
||||
// Handle OID's as BLOBS
|
||||
if(!wasNullFlag)
|
||||
if( fields[columnIndex - 1].getOID() == 26) {
|
||||
LargeObjectManager lom = connection.getLargeObjectAPI();
|
||||
LargeObject lob = lom.open(getInt(columnIndex));
|
||||
byte buf[] = lob.read(lob.size());
|
||||
lob.close();
|
||||
return buf;
|
||||
}
|
||||
|
||||
return this_row[columnIndex - 1];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the value of a column in the current row as a java.sql.Date
|
||||
* object
|
||||
*
|
||||
* @param columnIndex the first column is 1, the second is 2...
|
||||
* @return the column value; null if SQL NULL
|
||||
* @exception SQLException if a database access error occurs
|
||||
*/
|
||||
public java.sql.Date getDate(int columnIndex) throws SQLException
|
||||
{
|
||||
String s = getString(columnIndex);
|
||||
if(s==null)
|
||||
return null;
|
||||
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
|
||||
try {
|
||||
return new java.sql.Date(df.parse(s).getTime());
|
||||
} catch (ParseException e) {
|
||||
throw new PSQLException("postgresql.res.baddate",new Integer(e.getErrorOffset()),s);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the value of a column in the current row as a java.sql.Time
|
||||
* object
|
||||
*
|
||||
* @param columnIndex the first column is 1, the second is 2...
|
||||
* @return the column value; null if SQL NULL
|
||||
* @exception SQLException if a database access error occurs
|
||||
*/
|
||||
public Time getTime(int columnIndex) throws SQLException
|
||||
{
|
||||
String s = getString(columnIndex);
|
||||
|
||||
if (s != null)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (s.length() != 5 && s.length() != 8)
|
||||
throw new NumberFormatException("Wrong Length!");
|
||||
int hr = Integer.parseInt(s.substring(0,2));
|
||||
int min = Integer.parseInt(s.substring(3,5));
|
||||
int sec = (s.length() == 5) ? 0 : Integer.parseInt(s.substring(6));
|
||||
return new Time(hr, min, sec);
|
||||
} catch (NumberFormatException e) {
|
||||
throw new PSQLException ("postgresql.res.badtime",s);
|
||||
}
|
||||
}
|
||||
return null; // SQL NULL
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the value of a column in the current row as a
|
||||
* java.sql.Timestamp object
|
||||
*
|
||||
* @param columnIndex the first column is 1, the second is 2...
|
||||
* @return the column value; null if SQL NULL
|
||||
* @exception SQLException if a database access error occurs
|
||||
*/
|
||||
public Timestamp getTimestamp(int columnIndex) throws SQLException
|
||||
{
|
||||
String s = getString(columnIndex);
|
||||
if(s==null)
|
||||
return null;
|
||||
|
||||
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:sszzz");
|
||||
|
||||
try {
|
||||
return new Timestamp(df.parse(s).getTime());
|
||||
} catch(ParseException e) {
|
||||
throw new PSQLException("postgresql.res.badtimestamp",new Integer(e.getErrorOffset()),s);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A column value can be retrieved as a stream of ASCII characters
|
||||
* and then read in chunks from the stream. This method is
|
||||
* particular suitable for retrieving large LONGVARCHAR values.
|
||||
* The JDBC driver will do any necessary conversion from the
|
||||
* database format into ASCII.
|
||||
*
|
||||
* <p><B>Note:</B> All the data in the returned stream must be read
|
||||
* prior to getting the value of any other column. The next call
|
||||
* to a get method implicitly closes the stream. Also, a stream
|
||||
* may return 0 for available() whether there is data available
|
||||
* or not.
|
||||
*
|
||||
*<p> We implement an ASCII stream as a Binary stream - we should really
|
||||
* do the data conversion, but I cannot be bothered to implement this
|
||||
* right now.
|
||||
*
|
||||
* @param columnIndex the first column is 1, the second is 2, ...
|
||||
* @return a Java InputStream that delivers the database column
|
||||
* value as a stream of one byte ASCII characters. If the
|
||||
* value is SQL NULL then the result is null
|
||||
* @exception SQLException if a database access error occurs
|
||||
* @see getBinaryStream
|
||||
*/
|
||||
public InputStream getAsciiStream(int columnIndex) throws SQLException
|
||||
{
|
||||
return getBinaryStream(columnIndex);
|
||||
}
|
||||
|
||||
/**
|
||||
* A column value can also be retrieved as a stream of Unicode
|
||||
* characters. We implement this as a binary stream.
|
||||
*
|
||||
* @param columnIndex the first column is 1, the second is 2...
|
||||
* @return a Java InputStream that delivers the database column value
|
||||
* as a stream of two byte Unicode characters. If the value is
|
||||
* SQL NULL, then the result is null
|
||||
* @exception SQLException if a database access error occurs
|
||||
* @see getAsciiStream
|
||||
* @see getBinaryStream
|
||||
*/
|
||||
public InputStream getUnicodeStream(int columnIndex) throws SQLException
|
||||
{
|
||||
return getBinaryStream(columnIndex);
|
||||
}
|
||||
|
||||
/**
|
||||
* A column value can also be retrieved as a binary strea. This
|
||||
* method is suitable for retrieving LONGVARBINARY values.
|
||||
*
|
||||
* @param columnIndex the first column is 1, the second is 2...
|
||||
* @return a Java InputStream that delivers the database column value
|
||||
* as a stream of bytes. If the value is SQL NULL, then the result
|
||||
* is null
|
||||
* @exception SQLException if a database access error occurs
|
||||
* @see getAsciiStream
|
||||
* @see getUnicodeStream
|
||||
*/
|
||||
public InputStream getBinaryStream(int columnIndex) throws SQLException
|
||||
{
|
||||
byte b[] = getBytes(columnIndex);
|
||||
|
||||
if (b != null)
|
||||
return new ByteArrayInputStream(b);
|
||||
return null; // SQL NULL
|
||||
}
|
||||
|
||||
/**
|
||||
* The following routines simply convert the columnName into
|
||||
* a columnIndex and then call the appropriate routine above.
|
||||
*
|
||||
* @param columnName is the SQL name of the column
|
||||
* @return the column value
|
||||
* @exception SQLException if a database access error occurs
|
||||
*/
|
||||
public String getString(String columnName) throws SQLException
|
||||
{
|
||||
return getString(findColumn(columnName));
|
||||
}
|
||||
|
||||
public boolean getBoolean(String columnName) throws SQLException
|
||||
{
|
||||
return getBoolean(findColumn(columnName));
|
||||
}
|
||||
|
||||
public byte getByte(String columnName) throws SQLException
|
||||
{
|
||||
|
||||
return getByte(findColumn(columnName));
|
||||
}
|
||||
|
||||
public short getShort(String columnName) throws SQLException
|
||||
{
|
||||
return getShort(findColumn(columnName));
|
||||
}
|
||||
|
||||
public int getInt(String columnName) throws SQLException
|
||||
{
|
||||
return getInt(findColumn(columnName));
|
||||
}
|
||||
|
||||
public long getLong(String columnName) throws SQLException
|
||||
{
|
||||
return getLong(findColumn(columnName));
|
||||
}
|
||||
|
||||
public float getFloat(String columnName) throws SQLException
|
||||
{
|
||||
return getFloat(findColumn(columnName));
|
||||
}
|
||||
|
||||
public double getDouble(String columnName) throws SQLException
|
||||
{
|
||||
return getDouble(findColumn(columnName));
|
||||
}
|
||||
|
||||
public BigDecimal getBigDecimal(String columnName, int scale) throws SQLException
|
||||
{
|
||||
return getBigDecimal(findColumn(columnName), scale);
|
||||
}
|
||||
|
||||
public byte[] getBytes(String columnName) throws SQLException
|
||||
{
|
||||
return getBytes(findColumn(columnName));
|
||||
}
|
||||
|
||||
public java.sql.Date getDate(String columnName) throws SQLException
|
||||
{
|
||||
return getDate(findColumn(columnName));
|
||||
}
|
||||
|
||||
public Time getTime(String columnName) throws SQLException
|
||||
{
|
||||
return getTime(findColumn(columnName));
|
||||
}
|
||||
|
||||
public Timestamp getTimestamp(String columnName) throws SQLException
|
||||
{
|
||||
return getTimestamp(findColumn(columnName));
|
||||
}
|
||||
|
||||
public InputStream getAsciiStream(String columnName) throws SQLException
|
||||
{
|
||||
return getAsciiStream(findColumn(columnName));
|
||||
}
|
||||
|
||||
public InputStream getUnicodeStream(String columnName) throws SQLException
|
||||
{
|
||||
return getUnicodeStream(findColumn(columnName));
|
||||
}
|
||||
|
||||
public InputStream getBinaryStream(String columnName) throws SQLException
|
||||
{
|
||||
return getBinaryStream(findColumn(columnName));
|
||||
}
|
||||
|
||||
/**
|
||||
* The first warning reported by calls on this ResultSet is
|
||||
* returned. Subsequent ResultSet warnings will be chained
|
||||
* to this SQLWarning.
|
||||
*
|
||||
* <p>The warning chain is automatically cleared each time a new
|
||||
* row is read.
|
||||
*
|
||||
* <p><B>Note:</B> This warning chain only covers warnings caused by
|
||||
* ResultSet methods. Any warnings caused by statement methods
|
||||
* (such as reading OUT parameters) will be chained on the
|
||||
* Statement object.
|
||||
*
|
||||
* @return the first SQLWarning or null;
|
||||
* @exception SQLException if a database access error occurs.
|
||||
*/
|
||||
public SQLWarning getWarnings() throws SQLException
|
||||
{
|
||||
return warnings;
|
||||
}
|
||||
|
||||
/**
|
||||
* After this call, getWarnings returns null until a new warning
|
||||
* is reported for this ResultSet
|
||||
*
|
||||
* @exception SQLException if a database access error occurs
|
||||
*/
|
||||
public void clearWarnings() throws SQLException
|
||||
{
|
||||
warnings = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the name of the SQL cursor used by this ResultSet
|
||||
*
|
||||
* <p>In SQL, a result table is retrieved though a cursor that is
|
||||
* named. The current row of a result can be updated or deleted
|
||||
* using a positioned update/delete statement that references
|
||||
* the cursor name.
|
||||
*
|
||||
* <p>JDBC supports this SQL feature by providing the name of the
|
||||
* SQL cursor used by a ResultSet. The current row of a ResulSet
|
||||
* is also the current row of this SQL cursor.
|
||||
*
|
||||
* <p><B>Note:</B> If positioned update is not supported, a SQLException
|
||||
* is thrown.
|
||||
*
|
||||
* @return the ResultSet's SQL cursor name.
|
||||
* @exception SQLException if a database access error occurs
|
||||
*/
|
||||
public String getCursorName() throws SQLException
|
||||
{
|
||||
return connection.getCursorName();
|
||||
}
|
||||
|
||||
/**
|
||||
* The numbers, types and properties of a ResultSet's columns are
|
||||
* provided by the getMetaData method
|
||||
*
|
||||
* @return a description of the ResultSet's columns
|
||||
* @exception SQLException if a database access error occurs
|
||||
*/
|
||||
public java.sql.ResultSetMetaData getMetaData() throws SQLException
|
||||
{
|
||||
return new ResultSetMetaData(rows, fields);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the value of a column in the current row as a Java object
|
||||
*
|
||||
* <p>This method will return the value of the given column as a
|
||||
* Java object. The type of the Java object will be the default
|
||||
* Java Object type corresponding to the column's SQL type, following
|
||||
* the mapping specified in the JDBC specification.
|
||||
*
|
||||
* <p>This method may also be used to read database specific abstract
|
||||
* data types.
|
||||
*
|
||||
* @param columnIndex the first column is 1, the second is 2...
|
||||
* @return a Object holding the column value
|
||||
* @exception SQLException if a database access error occurs
|
||||
*/
|
||||
public Object getObject(int columnIndex) throws SQLException
|
||||
{
|
||||
Field field;
|
||||
|
||||
if (columnIndex < 1 || columnIndex > fields.length)
|
||||
throw new PSQLException("postgresql.res.colrange");
|
||||
field = fields[columnIndex - 1];
|
||||
|
||||
// some fields can be null, mainly from those returned by MetaData methods
|
||||
if(field==null) {
|
||||
wasNullFlag=true;
|
||||
return null;
|
||||
}
|
||||
|
||||
switch (field.getSQLType())
|
||||
{
|
||||
case Types.BIT:
|
||||
return new Boolean(getBoolean(columnIndex));
|
||||
case Types.SMALLINT:
|
||||
return new Integer(getInt(columnIndex));
|
||||
case Types.INTEGER:
|
||||
return new Integer(getInt(columnIndex));
|
||||
case Types.BIGINT:
|
||||
return new Long(getLong(columnIndex));
|
||||
case Types.NUMERIC:
|
||||
return getBigDecimal(columnIndex, ((field.mod-4) & 0xffff));
|
||||
case Types.REAL:
|
||||
return new Float(getFloat(columnIndex));
|
||||
case Types.DOUBLE:
|
||||
return new Double(getDouble(columnIndex));
|
||||
case Types.CHAR:
|
||||
case Types.VARCHAR:
|
||||
return getString(columnIndex);
|
||||
case Types.DATE:
|
||||
return getDate(columnIndex);
|
||||
case Types.TIME:
|
||||
return getTime(columnIndex);
|
||||
case Types.TIMESTAMP:
|
||||
return getTimestamp(columnIndex);
|
||||
default:
|
||||
return connection.getObject(field.getTypeName(), getString(columnIndex));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the value of a column in the current row as a Java object
|
||||
*
|
||||
*<p> This method will return the value of the given column as a
|
||||
* Java object. The type of the Java object will be the default
|
||||
* Java Object type corresponding to the column's SQL type, following
|
||||
* the mapping specified in the JDBC specification.
|
||||
*
|
||||
* <p>This method may also be used to read database specific abstract
|
||||
* data types.
|
||||
*
|
||||
* @param columnName is the SQL name of the column
|
||||
* @return a Object holding the column value
|
||||
* @exception SQLException if a database access error occurs
|
||||
*/
|
||||
public Object getObject(String columnName) throws SQLException
|
||||
{
|
||||
return getObject(findColumn(columnName));
|
||||
}
|
||||
|
||||
/**
|
||||
* Map a ResultSet column name to a ResultSet column index
|
||||
*
|
||||
* @param columnName the name of the column
|
||||
* @return the column index
|
||||
* @exception SQLException if a database access error occurs
|
||||
*/
|
||||
public int findColumn(String columnName) throws SQLException
|
||||
{
|
||||
int i;
|
||||
|
||||
for (i = 0 ; i < fields.length; ++i)
|
||||
if (fields[i].name.equalsIgnoreCase(columnName))
|
||||
return (i+1);
|
||||
throw new PSQLException ("postgresql.res.colname",columnName);
|
||||
}
|
||||
}
|
||||
|
451
src/interfaces/jdbc/org/postgresql/jdbc1/ResultSetMetaData.java
Normal file
451
src/interfaces/jdbc/org/postgresql/jdbc1/ResultSetMetaData.java
Normal file
@ -0,0 +1,451 @@
|
||||
package org.postgresql.jdbc1;
|
||||
|
||||
// IMPORTANT NOTE: This file implements the JDBC 1 version of the driver.
|
||||
// If you make any modifications to this file, you must make sure that the
|
||||
// changes are also made (if relevent) to the related JDBC 2 class in the
|
||||
// org.postgresql.jdbc2 package.
|
||||
|
||||
import java.lang.*;
|
||||
import java.util.*;
|
||||
import org.postgresql.*;
|
||||
import org.postgresql.util.*;
|
||||
|
||||
// We explicitly import classes here as the original line:
|
||||
//import java.sql.*;
|
||||
// causes javac to get confused.
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Types;
|
||||
|
||||
/**
|
||||
* A ResultSetMetaData object can be used to find out about the types and
|
||||
* properties of the columns in a ResultSet
|
||||
*
|
||||
* @see java.sql.ResultSetMetaData
|
||||
*/
|
||||
public class ResultSetMetaData implements java.sql.ResultSetMetaData
|
||||
{
|
||||
Vector rows;
|
||||
Field[] fields;
|
||||
|
||||
/**
|
||||
* Initialise for a result with a tuple set and
|
||||
* a field descriptor set
|
||||
*
|
||||
* @param rows the Vector of rows returned by the ResultSet
|
||||
* @param fields the array of field descriptors
|
||||
*/
|
||||
public ResultSetMetaData(Vector rows, Field[] fields)
|
||||
{
|
||||
this.rows = rows;
|
||||
this.fields = fields;
|
||||
}
|
||||
|
||||
/**
|
||||
* Whats the number of columns in the ResultSet?
|
||||
*
|
||||
* @return the number
|
||||
* @exception SQLException if a database access error occurs
|
||||
*/
|
||||
public int getColumnCount() throws SQLException
|
||||
{
|
||||
return fields.length;
|
||||
}
|
||||
|
||||
/**
|
||||
* Is the column automatically numbered (and thus read-only)
|
||||
* I believe that PostgreSQL does not support this feature.
|
||||
*
|
||||
* @param column the first column is 1, the second is 2...
|
||||
* @return true if so
|
||||
* @exception SQLException if a database access error occurs
|
||||
*/
|
||||
public boolean isAutoIncrement(int column) throws SQLException
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Does a column's case matter? ASSUMPTION: Any field that is
|
||||
* not obviously case insensitive is assumed to be case sensitive
|
||||
*
|
||||
* @param column the first column is 1, the second is 2...
|
||||
* @return true if so
|
||||
* @exception SQLException if a database access error occurs
|
||||
*/
|
||||
public boolean isCaseSensitive(int column) throws SQLException
|
||||
{
|
||||
int sql_type = getField(column).getSQLType();
|
||||
|
||||
switch (sql_type)
|
||||
{
|
||||
case Types.SMALLINT:
|
||||
case Types.INTEGER:
|
||||
case Types.FLOAT:
|
||||
case Types.REAL:
|
||||
case Types.DOUBLE:
|
||||
case Types.DATE:
|
||||
case Types.TIME:
|
||||
case Types.TIMESTAMP:
|
||||
return false;
|
||||
default:
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Can the column be used in a WHERE clause? Basically for
|
||||
* this, I split the functions into two types: recognised
|
||||
* types (which are always useable), and OTHER types (which
|
||||
* may or may not be useable). The OTHER types, for now, I
|
||||
* will assume they are useable. We should really query the
|
||||
* catalog to see if they are useable.
|
||||
*
|
||||
* @param column the first column is 1, the second is 2...
|
||||
* @return true if they can be used in a WHERE clause
|
||||
* @exception SQLException if a database access error occurs
|
||||
*/
|
||||
public boolean isSearchable(int column) throws SQLException
|
||||
{
|
||||
int sql_type = getField(column).getSQLType();
|
||||
|
||||
// This switch is pointless, I know - but it is a set-up
|
||||
// for further expansion.
|
||||
switch (sql_type)
|
||||
{
|
||||
case Types.OTHER:
|
||||
return true;
|
||||
default:
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Is the column a cash value? 6.1 introduced the cash/money
|
||||
* type, which haven't been incorporated as of 970414, so I
|
||||
* just check the type name for both 'cash' and 'money'
|
||||
*
|
||||
* @param column the first column is 1, the second is 2...
|
||||
* @return true if its a cash column
|
||||
* @exception SQLException if a database access error occurs
|
||||
*/
|
||||
public boolean isCurrency(int column) throws SQLException
|
||||
{
|
||||
String type_name = getField(column).getTypeName();
|
||||
|
||||
return type_name.equals("cash") || type_name.equals("money");
|
||||
}
|
||||
|
||||
/**
|
||||
* Can you put a NULL in this column? I think this is always
|
||||
* true in 6.1's case. It would only be false if the field had
|
||||
* been defined NOT NULL (system catalogs could be queried?)
|
||||
*
|
||||
* @param column the first column is 1, the second is 2...
|
||||
* @return one of the columnNullable values
|
||||
* @exception SQLException if a database access error occurs
|
||||
*/
|
||||
public int isNullable(int column) throws SQLException
|
||||
{
|
||||
return columnNullable; // We can always put NULL in
|
||||
}
|
||||
|
||||
/**
|
||||
* Is the column a signed number? In PostgreSQL, all numbers
|
||||
* are signed, so this is trivial. However, strings are not
|
||||
* signed (duh!)
|
||||
*
|
||||
* @param column the first column is 1, the second is 2...
|
||||
* @return true if so
|
||||
* @exception SQLException if a database access error occurs
|
||||
*/
|
||||
public boolean isSigned(int column) throws SQLException
|
||||
{
|
||||
int sql_type = getField(column).getSQLType();
|
||||
|
||||
switch (sql_type)
|
||||
{
|
||||
case Types.SMALLINT:
|
||||
case Types.INTEGER:
|
||||
case Types.FLOAT:
|
||||
case Types.REAL:
|
||||
case Types.DOUBLE:
|
||||
return true;
|
||||
case Types.DATE:
|
||||
case Types.TIME:
|
||||
case Types.TIMESTAMP:
|
||||
return false; // I don't know about these?
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* What is the column's normal maximum width in characters?
|
||||
*
|
||||
* @param column the first column is 1, the second is 2, etc.
|
||||
* @return the maximum width
|
||||
* @exception SQLException if a database access error occurs
|
||||
*/
|
||||
public int getColumnDisplaySize(int column) throws SQLException
|
||||
{
|
||||
Field f = getField(column);
|
||||
String type_name = f.getTypeName();
|
||||
int sql_type = f.getSQLType();
|
||||
int typmod = f.mod;
|
||||
|
||||
// I looked at other JDBC implementations and couldn't find a consistent
|
||||
// interpretation of the "display size" for numeric values, so this is our's
|
||||
// FIXME: currently, only types with a SQL92 or SQL3 pendant are implemented - jens@jens.de
|
||||
|
||||
// fixed length data types
|
||||
if (type_name.equals( "int2" )) return 6; // -32768 to +32768 (5 digits and a sign)
|
||||
if (type_name.equals( "int4" )
|
||||
|| type_name.equals( "oid" )) return 11; // -2147483648 to +2147483647
|
||||
if (type_name.equals( "int8" )) return 20; // -9223372036854775808 to +9223372036854775807
|
||||
if (type_name.equals( "money" )) return 12; // MONEY = DECIMAL(9,2)
|
||||
if (type_name.equals( "float4" )) return 11; // i checked it out ans wasn't able to produce more than 11 digits
|
||||
if (type_name.equals( "float8" )) return 20; // dito, 20
|
||||
if (type_name.equals( "char" )) return 1;
|
||||
if (type_name.equals( "bool" )) return 1;
|
||||
if (type_name.equals( "date" )) return 14; // "01/01/4713 BC" - "31/12/32767 AD"
|
||||
if (type_name.equals( "time" )) return 8; // 00:00:00-23:59:59
|
||||
if (type_name.equals( "timestamp" )) return 22; // hhmmm ... the output looks like this: 1999-08-03 22:22:08+02
|
||||
|
||||
// variable length fields
|
||||
typmod -= 4;
|
||||
if (type_name.equals( "bpchar" )
|
||||
|| type_name.equals( "varchar" )) return typmod; // VARHDRSZ=sizeof(int32)=4
|
||||
if (type_name.equals( "numeric" )) return ( (typmod >>16) & 0xffff )
|
||||
+ 1 + ( typmod & 0xffff ); // DECIMAL(p,s) = (p digits).(s digits)
|
||||
|
||||
// if we don't know better
|
||||
return f.length;
|
||||
}
|
||||
|
||||
/**
|
||||
* What is the suggested column title for use in printouts and
|
||||
* displays? We suggest the ColumnName!
|
||||
*
|
||||
* @param column the first column is 1, the second is 2, etc.
|
||||
* @return the column label
|
||||
* @exception SQLException if a database access error occurs
|
||||
*/
|
||||
public String getColumnLabel(int column) throws SQLException
|
||||
{
|
||||
return getColumnName(column);
|
||||
}
|
||||
|
||||
/**
|
||||
* What's a column's name?
|
||||
*
|
||||
* @param column the first column is 1, the second is 2, etc.
|
||||
* @return the column name
|
||||
* @exception SQLException if a database access error occurs
|
||||
*/
|
||||
public String getColumnName(int column) throws SQLException
|
||||
{
|
||||
Field f = getField(column);
|
||||
if(f!=null)
|
||||
return f.name;
|
||||
return "field"+column;
|
||||
}
|
||||
|
||||
/**
|
||||
* What is a column's table's schema? This relies on us knowing
|
||||
* the table name....which I don't know how to do as yet. The
|
||||
* JDBC specification allows us to return "" if this is not
|
||||
* applicable.
|
||||
*
|
||||
* @param column the first column is 1, the second is 2...
|
||||
* @return the Schema
|
||||
* @exception SQLException if a database access error occurs
|
||||
*/
|
||||
public String getSchemaName(int column) throws SQLException
|
||||
{
|
||||
return "";
|
||||
}
|
||||
|
||||
/**
|
||||
* What is a column's number of decimal digits.
|
||||
*
|
||||
* @param column the first column is 1, the second is 2...
|
||||
* @return the precision
|
||||
* @exception SQLException if a database access error occurs
|
||||
*/
|
||||
public int getPrecision(int column) throws SQLException
|
||||
{
|
||||
int sql_type = getField(column).getSQLType();
|
||||
|
||||
switch (sql_type)
|
||||
{
|
||||
case Types.SMALLINT:
|
||||
return 5;
|
||||
case Types.INTEGER:
|
||||
return 10;
|
||||
case Types.REAL:
|
||||
return 8;
|
||||
case Types.FLOAT:
|
||||
return 16;
|
||||
case Types.DOUBLE:
|
||||
return 16;
|
||||
case Types.VARCHAR:
|
||||
return 0;
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* What is a column's number of digits to the right of the
|
||||
* decimal point?
|
||||
*
|
||||
* @param column the first column is 1, the second is 2...
|
||||
* @return the scale
|
||||
* @exception SQLException if a database access error occurs
|
||||
*/
|
||||
public int getScale(int column) throws SQLException
|
||||
{
|
||||
int sql_type = getField(column).getSQLType();
|
||||
|
||||
switch (sql_type)
|
||||
{
|
||||
case Types.SMALLINT:
|
||||
return 0;
|
||||
case Types.INTEGER:
|
||||
return 0;
|
||||
case Types.REAL:
|
||||
return 8;
|
||||
case Types.FLOAT:
|
||||
return 16;
|
||||
case Types.DOUBLE:
|
||||
return 16;
|
||||
case Types.VARCHAR:
|
||||
return 0;
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Whats a column's table's name? How do I find this out? Both
|
||||
* getSchemaName() and getCatalogName() rely on knowing the table
|
||||
* Name, so we need this before we can work on them.
|
||||
*
|
||||
* @param column the first column is 1, the second is 2...
|
||||
* @return column name, or "" if not applicable
|
||||
* @exception SQLException if a database access error occurs
|
||||
*/
|
||||
public String getTableName(int column) throws SQLException
|
||||
{
|
||||
return "";
|
||||
}
|
||||
|
||||
/**
|
||||
* What's a column's table's catalog name? As with getSchemaName(),
|
||||
* we can say that if getTableName() returns n/a, then we can too -
|
||||
* otherwise, we need to work on it.
|
||||
*
|
||||
* @param column the first column is 1, the second is 2...
|
||||
* @return catalog name, or "" if not applicable
|
||||
* @exception SQLException if a database access error occurs
|
||||
*/
|
||||
public String getCatalogName(int column) throws SQLException
|
||||
{
|
||||
return "";
|
||||
}
|
||||
|
||||
/**
|
||||
* What is a column's SQL Type? (java.sql.Type int)
|
||||
*
|
||||
* @param column the first column is 1, the second is 2, etc.
|
||||
* @return the java.sql.Type value
|
||||
* @exception SQLException if a database access error occurs
|
||||
* @see org.postgresql.Field#getSQLType
|
||||
* @see java.sql.Types
|
||||
*/
|
||||
public int getColumnType(int column) throws SQLException
|
||||
{
|
||||
return getField(column).getSQLType();
|
||||
}
|
||||
|
||||
/**
|
||||
* Whats is the column's data source specific type name?
|
||||
*
|
||||
* @param column the first column is 1, the second is 2, etc.
|
||||
* @return the type name
|
||||
* @exception SQLException if a database access error occurs
|
||||
*/
|
||||
public String getColumnTypeName(int column) throws SQLException
|
||||
{
|
||||
return getField(column).getTypeName();
|
||||
}
|
||||
|
||||
/**
|
||||
* Is the column definitely not writable? In reality, we would
|
||||
* have to check the GRANT/REVOKE stuff for this to be effective,
|
||||
* and I haven't really looked into that yet, so this will get
|
||||
* re-visited.
|
||||
*
|
||||
* @param column the first column is 1, the second is 2, etc.
|
||||
* @return true if so
|
||||
* @exception SQLException if a database access error occurs
|
||||
*/
|
||||
public boolean isReadOnly(int column) throws SQLException
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Is it possible for a write on the column to succeed? Again, we
|
||||
* would in reality have to check the GRANT/REVOKE stuff, which
|
||||
* I haven't worked with as yet. However, if it isn't ReadOnly, then
|
||||
* it is obviously writable.
|
||||
*
|
||||
* @param column the first column is 1, the second is 2, etc.
|
||||
* @return true if so
|
||||
* @exception SQLException if a database access error occurs
|
||||
*/
|
||||
public boolean isWritable(int column) throws SQLException
|
||||
{
|
||||
if (isReadOnly(column))
|
||||
return true;
|
||||
else
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Will a write on this column definately succeed? Hmmm...this
|
||||
* is a bad one, since the two preceding functions have not been
|
||||
* really defined. I cannot tell is the short answer. I thus
|
||||
* return isWritable() just to give us an idea.
|
||||
*
|
||||
* @param column the first column is 1, the second is 2, etc..
|
||||
* @return true if so
|
||||
* @exception SQLException if a database access error occurs
|
||||
*/
|
||||
public boolean isDefinitelyWritable(int column) throws SQLException
|
||||
{
|
||||
return isWritable(column);
|
||||
}
|
||||
|
||||
// ********************************************************
|
||||
// END OF PUBLIC INTERFACE
|
||||
// ********************************************************
|
||||
|
||||
/**
|
||||
* For several routines in this package, we need to convert
|
||||
* a columnIndex into a Field[] descriptor. Rather than do
|
||||
* the same code several times, here it is.
|
||||
*
|
||||
* @param columnIndex the first column is 1, the second is 2...
|
||||
* @return the Field description
|
||||
* @exception SQLException if a database access error occurs
|
||||
*/
|
||||
private Field getField(int columnIndex) throws SQLException
|
||||
{
|
||||
if (columnIndex < 1 || columnIndex > fields.length)
|
||||
throw new PSQLException("postgresql.res.colrange");
|
||||
return fields[columnIndex - 1];
|
||||
}
|
||||
}
|
||||
|
325
src/interfaces/jdbc/org/postgresql/jdbc1/Statement.java
Normal file
325
src/interfaces/jdbc/org/postgresql/jdbc1/Statement.java
Normal file
@ -0,0 +1,325 @@
|
||||
package org.postgresql.jdbc1;
|
||||
|
||||
// IMPORTANT NOTE: This file implements the JDBC 1 version of the driver.
|
||||
// If you make any modifications to this file, you must make sure that the
|
||||
// changes are also made (if relevent) to the related JDBC 2 class in the
|
||||
// org.postgresql.jdbc2 package.
|
||||
|
||||
import java.sql.*;
|
||||
|
||||
import org.postgresql.util.PSQLException;
|
||||
|
||||
/**
|
||||
* A Statement object is used for executing a static SQL statement and
|
||||
* obtaining the results produced by it.
|
||||
*
|
||||
* <p>Only one ResultSet per Statement can be open at any point in time.
|
||||
* Therefore, if the reading of one ResultSet is interleaved with the
|
||||
* reading of another, each must have been generated by different
|
||||
* Statements. All statement execute methods implicitly close a
|
||||
* statement's current ResultSet if an open one exists.
|
||||
*
|
||||
* @see java.sql.Statement
|
||||
* @see ResultSet
|
||||
*/
|
||||
public class Statement implements java.sql.Statement
|
||||
{
|
||||
Connection connection; // The connection who created us
|
||||
java.sql.ResultSet result = null; // The current results
|
||||
SQLWarning warnings = null; // The warnings chain.
|
||||
int timeout = 0; // The timeout for a query (not used)
|
||||
boolean escapeProcessing = true;// escape processing flag
|
||||
|
||||
/**
|
||||
* Constructor for a Statement. It simply sets the connection
|
||||
* that created us.
|
||||
*
|
||||
* @param c the Connection instantation that creates us
|
||||
*/
|
||||
public Statement (Connection c)
|
||||
{
|
||||
connection = c;
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute a SQL statement that retruns a single ResultSet
|
||||
*
|
||||
* @param sql typically a static SQL SELECT statement
|
||||
* @return a ResulSet that contains the data produced by the query
|
||||
* @exception SQLException if a database access error occurs
|
||||
*/
|
||||
public java.sql.ResultSet executeQuery(String sql) throws SQLException
|
||||
{
|
||||
this.execute(sql);
|
||||
while (result != null && !((org.postgresql.ResultSet)result).reallyResultSet())
|
||||
result = ((org.postgresql.ResultSet)result).getNext();
|
||||
if (result == null)
|
||||
throw new PSQLException("postgresql.stat.noresult");
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute a SQL INSERT, UPDATE or DELETE statement. In addition
|
||||
* SQL statements that return nothing such as SQL DDL statements
|
||||
* can be executed
|
||||
*
|
||||
* @param sql a SQL statement
|
||||
* @return either a row count, or 0 for SQL commands
|
||||
* @exception SQLException if a database access error occurs
|
||||
*/
|
||||
public int executeUpdate(String sql) throws SQLException
|
||||
{
|
||||
this.execute(sql);
|
||||
if (((org.postgresql.ResultSet)result).reallyResultSet())
|
||||
throw new PSQLException("postgresql.stat.result");
|
||||
return this.getUpdateCount();
|
||||
}
|
||||
|
||||
/**
|
||||
* In many cases, it is desirable to immediately release a
|
||||
* Statement's database and JDBC resources instead of waiting
|
||||
* for this to happen when it is automatically closed. The
|
||||
* close method provides this immediate release.
|
||||
*
|
||||
* <p><B>Note:</B> A Statement is automatically closed when it is
|
||||
* garbage collected. When a Statement is closed, its current
|
||||
* ResultSet, if one exists, is also closed.
|
||||
*
|
||||
* @exception SQLException if a database access error occurs (why?)
|
||||
*/
|
||||
public void close() throws SQLException
|
||||
{
|
||||
result = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* The maxFieldSize limit (in bytes) is the maximum amount of
|
||||
* data returned for any column value; it only applies to
|
||||
* BINARY, VARBINARY, LONGVARBINARY, CHAR, VARCHAR and LONGVARCHAR
|
||||
* columns. If the limit is exceeded, the excess data is silently
|
||||
* discarded.
|
||||
*
|
||||
* @return the current max column size limit; zero means unlimited
|
||||
* @exception SQLException if a database access error occurs
|
||||
*/
|
||||
public int getMaxFieldSize() throws SQLException
|
||||
{
|
||||
return 8192; // We cannot change this
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the maxFieldSize - NOT! - We throw an SQLException just
|
||||
* to inform them to stop doing this.
|
||||
*
|
||||
* @param max the new max column size limit; zero means unlimited
|
||||
* @exception SQLException if a database access error occurs
|
||||
*/
|
||||
public void setMaxFieldSize(int max) throws SQLException
|
||||
{
|
||||
throw new PSQLException("postgresql.stat.maxfieldsize");
|
||||
}
|
||||
|
||||
/**
|
||||
* The maxRows limit is set to limit the number of rows that
|
||||
* any ResultSet can contain. If the limit is exceeded, the
|
||||
* excess rows are silently dropped.
|
||||
*
|
||||
* @return the current maximum row limit; zero means unlimited
|
||||
* @exception SQLException if a database access error occurs
|
||||
*/
|
||||
public int getMaxRows() throws SQLException
|
||||
{
|
||||
return connection.maxrows;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the maximum number of rows
|
||||
*
|
||||
* @param max the new max rows limit; zero means unlimited
|
||||
* @exception SQLException if a database access error occurs
|
||||
* @see getMaxRows
|
||||
*/
|
||||
public void setMaxRows(int max) throws SQLException
|
||||
{
|
||||
connection.maxrows = max;
|
||||
}
|
||||
|
||||
/**
|
||||
* If escape scanning is on (the default), the driver will do escape
|
||||
* substitution before sending the SQL to the database.
|
||||
*
|
||||
* @param enable true to enable; false to disable
|
||||
* @exception SQLException if a database access error occurs
|
||||
*/
|
||||
public void setEscapeProcessing(boolean enable) throws SQLException
|
||||
{
|
||||
escapeProcessing = enable;
|
||||
}
|
||||
|
||||
/**
|
||||
* The queryTimeout limit is the number of seconds the driver
|
||||
* will wait for a Statement to execute. If the limit is
|
||||
* exceeded, a SQLException is thrown.
|
||||
*
|
||||
* @return the current query timeout limit in seconds; 0 = unlimited
|
||||
* @exception SQLException if a database access error occurs
|
||||
*/
|
||||
public int getQueryTimeout() throws SQLException
|
||||
{
|
||||
return timeout;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the queryTimeout limit
|
||||
*
|
||||
* @param seconds - the new query timeout limit in seconds
|
||||
* @exception SQLException if a database access error occurs
|
||||
*/
|
||||
public void setQueryTimeout(int seconds) throws SQLException
|
||||
{
|
||||
timeout = seconds;
|
||||
}
|
||||
|
||||
/**
|
||||
* Cancel can be used by one thread to cancel a statement that
|
||||
* is being executed by another thread. However, PostgreSQL is
|
||||
* a sync. sort of thing, so this really has no meaning - we
|
||||
* define it as a no-op (i.e. you can't cancel, but there is no
|
||||
* error if you try.)
|
||||
*
|
||||
* 6.4 introduced a cancel operation, but we have not implemented it
|
||||
* yet. Sometime before 6.5, this method will be implemented.
|
||||
*
|
||||
* @exception SQLException only because thats the spec.
|
||||
*/
|
||||
public void cancel() throws SQLException
|
||||
{
|
||||
// No-op
|
||||
}
|
||||
|
||||
/**
|
||||
* The first warning reported by calls on this Statement is
|
||||
* returned. A Statement's execute methods clear its SQLWarning
|
||||
* chain. Subsequent Statement warnings will be chained to this
|
||||
* SQLWarning.
|
||||
*
|
||||
* <p>The Warning chain is automatically cleared each time a statement
|
||||
* is (re)executed.
|
||||
*
|
||||
* <p><B>Note:</B> If you are processing a ResultSet then any warnings
|
||||
* associated with ResultSet reads will be chained on the ResultSet
|
||||
* object.
|
||||
*
|
||||
* @return the first SQLWarning on null
|
||||
* @exception SQLException if a database access error occurs
|
||||
*/
|
||||
public SQLWarning getWarnings() throws SQLException
|
||||
{
|
||||
return warnings;
|
||||
}
|
||||
|
||||
/**
|
||||
* After this call, getWarnings returns null until a new warning
|
||||
* is reported for this Statement.
|
||||
*
|
||||
* @exception SQLException if a database access error occurs (why?)
|
||||
*/
|
||||
public void clearWarnings() throws SQLException
|
||||
{
|
||||
warnings = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* setCursorName defines the SQL cursor name that will be used by
|
||||
* subsequent execute methods. This name can then be used in SQL
|
||||
* positioned update/delete statements to identify the current row
|
||||
* in the ResultSet generated by this statement. If a database
|
||||
* doesn't support positioned update/delete, this method is a
|
||||
* no-op.
|
||||
*
|
||||
* <p><B>Note:</B> By definition, positioned update/delete execution
|
||||
* must be done by a different Statement than the one which
|
||||
* generated the ResultSet being used for positioning. Also, cursor
|
||||
* names must be unique within a Connection.
|
||||
*
|
||||
* <p>We throw an additional constriction. There can only be one
|
||||
* cursor active at any one time.
|
||||
*
|
||||
* @param name the new cursor name
|
||||
* @exception SQLException if a database access error occurs
|
||||
*/
|
||||
public void setCursorName(String name) throws SQLException
|
||||
{
|
||||
connection.setCursorName(name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute a SQL statement that may return multiple results. We
|
||||
* don't have to worry about this since we do not support multiple
|
||||
* ResultSets. You can use getResultSet or getUpdateCount to
|
||||
* retrieve the result.
|
||||
*
|
||||
* @param sql any SQL statement
|
||||
* @return true if the next result is a ResulSet, false if it is
|
||||
* an update count or there are no more results
|
||||
* @exception SQLException if a database access error occurs
|
||||
*/
|
||||
public boolean execute(String sql) throws SQLException
|
||||
{
|
||||
result = connection.ExecSQL(sql);
|
||||
return (result != null && ((org.postgresql.ResultSet)result).reallyResultSet());
|
||||
}
|
||||
|
||||
/**
|
||||
* getResultSet returns the current result as a ResultSet. It
|
||||
* should only be called once per result.
|
||||
*
|
||||
* @return the current result set; null if there are no more
|
||||
* @exception SQLException if a database access error occurs (why?)
|
||||
*/
|
||||
public java.sql.ResultSet getResultSet() throws SQLException
|
||||
{
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* getUpdateCount returns the current result as an update count,
|
||||
* if the result is a ResultSet or there are no more results, -1
|
||||
* is returned. It should only be called once per result.
|
||||
*
|
||||
* @return the current result as an update count.
|
||||
* @exception SQLException if a database access error occurs
|
||||
*/
|
||||
public int getUpdateCount() throws SQLException
|
||||
{
|
||||
if (result == null) return -1;
|
||||
if (((org.postgresql.ResultSet)result).reallyResultSet()) return -1;
|
||||
return ((org.postgresql.ResultSet)result).getResultCount();
|
||||
}
|
||||
|
||||
/**
|
||||
* getMoreResults moves to a Statement's next result. If it returns
|
||||
* true, this result is a ResulSet.
|
||||
*
|
||||
* @return true if the next ResultSet is valid
|
||||
* @exception SQLException if a database access error occurs
|
||||
*/
|
||||
public boolean getMoreResults() throws SQLException
|
||||
{
|
||||
result = ((org.postgresql.ResultSet)result).getNext();
|
||||
return (result != null && ((org.postgresql.ResultSet)result).reallyResultSet());
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the status message from the current Result.<p>
|
||||
* This is used internally by the driver.
|
||||
*
|
||||
* @return status message from backend
|
||||
*/
|
||||
public String getResultStatusString()
|
||||
{
|
||||
if(result == null)
|
||||
return null;
|
||||
return ((org.postgresql.ResultSet)result).getStatusString();
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user