1
package org.postgresql.jdbc2;
3
// IMPORTANT NOTE: This file implements the JDBC 2 version of the driver.
4
// If you make any modifications to this file, you must make sure that the
5
// changes are also made (if relevent) to the related JDBC 1 class in the
6
// org.postgresql.jdbc1 package.
10
import java.lang.reflect.*;
14
import org.postgresql.Field;
15
import org.postgresql.fastpath.*;
16
import org.postgresql.largeobject.*;
17
import org.postgresql.util.*;
20
* $Id: Connection.java,v 1.17 2002/01/15 06:55:13 barry Exp $
22
* A Connection represents a session with a specific database. Within the
23
* context of a Connection, SQL statements are executed and results are
26
* <P>A Connection's database is able to provide information describing
27
* its tables, its supported SQL grammar, its stored procedures, the
28
* capabilities of this connection, etc. This information is obtained
29
* with the getMetaData method.
31
* <p><B>Note:</B> By default, the Connection automatically commits changes
32
* after executing each statement. If auto-commit has been disabled, an
33
* explicit commit must be done or database changes will not be saved.
35
* @see java.sql.Connection
37
public class Connection extends org.postgresql.Connection implements java.sql.Connection
39
// This is a cache of the DatabaseMetaData instance for this connection
40
protected DatabaseMetaData metadata;
43
* The current type mappings
45
protected java.util.Map typemap;
48
* SQL statements without parameters are normally executed using
49
* Statement objects. If the same SQL statement is executed many
50
* times, it is more efficient to use a PreparedStatement
52
* @return a new Statement object
53
* @exception SQLException passed through from the constructor
55
public java.sql.Statement createStatement() throws SQLException
57
// The spec says default of TYPE_FORWARD_ONLY but everyone is used to
58
// using TYPE_SCROLL_INSENSITIVE
59
return createStatement(java.sql.ResultSet.TYPE_SCROLL_INSENSITIVE, java.sql.ResultSet.CONCUR_READ_ONLY);
63
* SQL statements without parameters are normally executed using
64
* Statement objects. If the same SQL statement is executed many
65
* times, it is more efficient to use a PreparedStatement
67
* @param resultSetType to use
68
* @param resultSetCuncurrency to use
69
* @return a new Statement object
70
* @exception SQLException passed through from the constructor
72
public java.sql.Statement createStatement(int resultSetType, int resultSetConcurrency) throws SQLException
74
Statement s = new Statement(this);
75
s.setResultSetType(resultSetType);
76
s.setResultSetConcurrency(resultSetConcurrency);
82
* A SQL statement with or without IN parameters can be pre-compiled
83
* and stored in a PreparedStatement object. This object can then
84
* be used to efficiently execute this statement multiple times.
86
* <B>Note:</B> This method is optimized for handling parametric
87
* SQL statements that benefit from precompilation if the drivers
88
* supports precompilation. PostgreSQL does not support precompilation.
89
* In this case, the statement is not sent to the database until the
90
* PreparedStatement is executed. This has no direct effect on users;
91
* however it does affect which method throws certain SQLExceptions
93
* @param sql a SQL statement that may contain one or more '?' IN
94
* parameter placeholders
95
* @return a new PreparedStatement object containing the pre-compiled
97
* @exception SQLException if a database access error occurs.
99
public java.sql.PreparedStatement prepareStatement(String sql) throws SQLException
101
return prepareStatement(sql, java.sql.ResultSet.TYPE_SCROLL_INSENSITIVE, java.sql.ResultSet.CONCUR_READ_ONLY);
104
public java.sql.PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency) throws SQLException
106
PreparedStatement s = new PreparedStatement(this, sql);
107
s.setResultSetType(resultSetType);
108
s.setResultSetConcurrency(resultSetConcurrency);
113
* A SQL stored procedure call statement is handled by creating a
114
* CallableStatement for it. The CallableStatement provides methods
115
* for setting up its IN and OUT parameters and methods for executing
118
* <B>Note:</B> This method is optimised for handling stored procedure
119
* call statements. Some drivers may send the call statement to the
120
* database when the prepareCall is done; others may wait until the
121
* CallableStatement is executed. This has no direct effect on users;
122
* however, it does affect which method throws certain SQLExceptions
124
* @param sql a SQL statement that may contain one or more '?' parameter
125
* placeholders. Typically this statement is a JDBC function call
127
* @return a new CallableStatement object containing the pre-compiled
129
* @exception SQLException if a database access error occurs
131
public java.sql.CallableStatement prepareCall(String sql) throws SQLException
133
return prepareCall(sql, java.sql.ResultSet.TYPE_SCROLL_INSENSITIVE, java.sql.ResultSet.CONCUR_READ_ONLY);
136
public java.sql.CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency) throws SQLException
138
throw new PSQLException("postgresql.con.call");
139
//CallableStatement s = new CallableStatement(this,sql);
140
//s.setResultSetType(resultSetType);
141
//s.setResultSetConcurrency(resultSetConcurrency);
146
* Tests to see if a Connection is closed.
148
* Peter Feb 7 2000: Now I've discovered that this doesn't actually obey the
149
* specifications. Under JDBC2.1, this should only be valid _after_ close()
150
* has been called. It's result is not guraranteed to be valid before, and
151
* client code should not use it to see if a connection is open. The spec says
152
* that the client should monitor the SQLExceptions thrown when their queries
153
* fail because the connection is dead.
155
* I don't like this definition. As it doesn't hurt breaking it here, our
156
* isClosed() implementation does test the connection, so for PostgreSQL, you
157
* can rely on isClosed() returning a valid result.
159
* @return the status of the connection
160
* @exception SQLException (why?)
162
public boolean isClosed() throws SQLException
164
// If the stream is gone, then close() was called
165
if (pg_stream == null)
168
// ok, test the connection
171
// by sending an empty query. If we are dead, then an SQLException should
173
java.sql.ResultSet rs = ExecSQL(" ");
177
// By now, we must be alive
180
catch (SQLException se)
182
// Why throw an SQLException as this may fail without throwing one,
183
// ie isClosed() is called incase the connection has died, and we don't
184
// want to find out by an Exception, so instead we return true, as its
185
// most likely why it was thrown in the first place.
191
* A connection's database is able to provide information describing
192
* its tables, its supported SQL grammar, its stored procedures, the
193
* capabilities of this connection, etc. This information is made
194
* available through a DatabaseMetaData object.
196
* @return a DatabaseMetaData object for this connection
197
* @exception SQLException if a database access error occurs
199
public java.sql.DatabaseMetaData getMetaData() throws SQLException
201
if (metadata == null)
202
metadata = new DatabaseMetaData(this);
207
* This overides the method in org.postgresql.Connection and returns a
210
public java.sql.ResultSet getResultSet(org.postgresql.Connection conn, java.sql.Statement stat, Field[] fields, Vector tuples, String status, int updateCount, long insertOID, boolean binaryCursor) throws SQLException
212
// In 7.1 we now test concurrency to see which class to return. If we are not working with a
213
// Statement then default to a normal ResultSet object.
216
if (stat.getResultSetConcurrency() == java.sql.ResultSet.CONCUR_UPDATABLE)
217
return new org.postgresql.jdbc2.UpdateableResultSet((org.postgresql.jdbc2.Connection)conn, fields, tuples, status, updateCount, insertOID, binaryCursor);
220
return new org.postgresql.jdbc2.ResultSet((org.postgresql.jdbc2.Connection)conn, fields, tuples, status, updateCount, insertOID, binaryCursor);
227
public java.util.Map getTypeMap() throws SQLException
234
public void setTypeMap(java.util.Map map) throws SQLException
241
* This overides the standard internal getObject method so that we can
242
* check the jdbc2 type map first
244
* @return PGobject for this type, and set to value
245
* @exception SQLException if value is not correct for this type
246
* @see org.postgresql.util.Serialize
248
public Object getObject(String type, String value) throws SQLException
252
SQLData d = (SQLData) typemap.get(type);
255
// Handle the type (requires SQLInput & SQLOutput classes to be implemented)
256
throw org.postgresql.Driver.notImplemented();
260
// Default to the original method
261
return super.getObject(type, value);
264
/* An implementation of the abstract method in the parent class.
265
* This implemetation uses the jdbc2Types array to support the jdbc2
266
* datatypes. Basically jdbc1 and jdbc2 are the same, except that
267
* jdbc2 adds the Array types.
269
public int getSQLType(String pgTypeName)
271
int sqlType = Types.OTHER; // default value
272
for (int i = 0;i < jdbc2Types.length;i++)
274
if (pgTypeName.equals(jdbc2Types[i]))
276
sqlType = jdbc2Typei[i];
284
* This table holds the org.postgresql names for the types supported.
285
* Any types that map to Types.OTHER (eg POINT) don't go into this table.
286
* They default automatically to Types.OTHER
288
* Note: This must be in the same order as below.
290
* Tip: keep these grouped together by the Types. value
292
private static final String jdbc2Types[] = {
300
"bpchar", "char", "char2", "char4", "char8", "char16",
301
"varchar", "text", "name", "filename",
306
"abstime", "timestamp", "timestamptz",
307
"_bool", "_char", "_int2", "_int4", "_text",
308
"_oid", "_varchar", "_int8", "_float4", "_float8",
309
"_abstime", "_date", "_time", "_timestamp", "_numeric",
314
* This table holds the JDBC type for each entry above.
316
* Note: This must be in the same order as above
318
* Tip: keep these grouped together by the Types. value
320
private static final int jdbc2Typei[] = {
322
Types.INTEGER, Types.INTEGER,
324
Types.DOUBLE, Types.DOUBLE,
328
Types.CHAR, Types.CHAR, Types.CHAR, Types.CHAR, Types.CHAR, Types.CHAR,
329
Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.VARCHAR,
334
Types.TIMESTAMP, Types.TIMESTAMP, Types.TIMESTAMP,
335
Types.ARRAY, Types.ARRAY, Types.ARRAY, Types.ARRAY, Types.ARRAY,
336
Types.ARRAY, Types.ARRAY, Types.ARRAY, Types.ARRAY, Types.ARRAY,
337
Types.ARRAY, Types.ARRAY, Types.ARRAY, Types.ARRAY, Types.ARRAY,
344
// ***********************************************************************