~ubuntu-branches/ubuntu/gutsy/libpgjava/gutsy

« back to all changes in this revision

Viewing changes to src/interfaces/jdbc/org/postgresql/jdbc2/Connection.java

  • Committer: Bazaar Package Importer
  • Author(s): Arnaud Vandyck
  • Date: 2005-04-21 14:25:11 UTC
  • mfrom: (1.2.1 upstream) (2.1.1 warty)
  • Revision ID: james.westby@ubuntu.com-20050421142511-wibh5vc31fkrorx7
Tags: 7.4.7-3
Built with sources...

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
package org.postgresql.jdbc2;
2
 
 
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.
7
 
 
8
 
import java.io.*;
9
 
import java.lang.*;
10
 
import java.lang.reflect.*;
11
 
import java.net.*;
12
 
import java.util.*;
13
 
import java.sql.*;
14
 
import org.postgresql.Field;
15
 
import org.postgresql.fastpath.*;
16
 
import org.postgresql.largeobject.*;
17
 
import org.postgresql.util.*;
18
 
 
19
 
/*
20
 
 * $Id: Connection.java,v 1.17 2002/01/15 06:55:13 barry Exp $
21
 
 *
22
 
 * A Connection represents a session with a specific database.  Within the
23
 
 * context of a Connection, SQL statements are executed and results are
24
 
 * returned.
25
 
 *
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.
30
 
 *
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.
34
 
 *
35
 
 * @see java.sql.Connection
36
 
 */
37
 
public class Connection extends org.postgresql.Connection implements java.sql.Connection
38
 
{
39
 
        // This is a cache of the DatabaseMetaData instance for this connection
40
 
        protected DatabaseMetaData metadata;
41
 
 
42
 
        /*
43
 
         * The current type mappings
44
 
         */
45
 
        protected java.util.Map typemap;
46
 
 
47
 
        /*
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
51
 
         *
52
 
         * @return a new Statement object
53
 
         * @exception SQLException passed through from the constructor
54
 
         */
55
 
        public java.sql.Statement createStatement() throws SQLException
56
 
        {
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);
60
 
        }
61
 
 
62
 
        /*
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
66
 
         *
67
 
         * @param resultSetType to use
68
 
         * @param resultSetCuncurrency to use
69
 
         * @return a new Statement object
70
 
         * @exception SQLException passed through from the constructor
71
 
         */
72
 
        public java.sql.Statement createStatement(int resultSetType, int resultSetConcurrency) throws SQLException
73
 
        {
74
 
                Statement s = new Statement(this);
75
 
                s.setResultSetType(resultSetType);
76
 
                s.setResultSetConcurrency(resultSetConcurrency);
77
 
                return s;
78
 
        }
79
 
 
80
 
 
81
 
        /*
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.
85
 
         *
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
92
 
         *
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
96
 
         *      statement.
97
 
         * @exception SQLException if a database access error occurs.
98
 
         */
99
 
        public java.sql.PreparedStatement prepareStatement(String sql) throws SQLException
100
 
        {
101
 
                return prepareStatement(sql, java.sql.ResultSet.TYPE_SCROLL_INSENSITIVE, java.sql.ResultSet.CONCUR_READ_ONLY);
102
 
        }
103
 
 
104
 
        public java.sql.PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency) throws SQLException
105
 
        {
106
 
                PreparedStatement s = new PreparedStatement(this, sql);
107
 
                s.setResultSetType(resultSetType);
108
 
                s.setResultSetConcurrency(resultSetConcurrency);
109
 
                return s;
110
 
        }
111
 
 
112
 
        /*
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
116
 
         * it.
117
 
         *
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
123
 
         *
124
 
         * @param sql a SQL statement that may contain one or more '?' parameter
125
 
         *      placeholders.  Typically this statement is a JDBC function call
126
 
         *      escape string.
127
 
         * @return a new CallableStatement object containing the pre-compiled
128
 
         *      SQL statement
129
 
         * @exception SQLException if a database access error occurs
130
 
         */
131
 
        public java.sql.CallableStatement prepareCall(String sql) throws SQLException
132
 
        {
133
 
                return prepareCall(sql, java.sql.ResultSet.TYPE_SCROLL_INSENSITIVE, java.sql.ResultSet.CONCUR_READ_ONLY);
134
 
        }
135
 
 
136
 
        public java.sql.CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency) throws SQLException
137
 
        {
138
 
                throw new PSQLException("postgresql.con.call");
139
 
                //CallableStatement s = new CallableStatement(this,sql);
140
 
                //s.setResultSetType(resultSetType);
141
 
                //s.setResultSetConcurrency(resultSetConcurrency);
142
 
                //return s;
143
 
        }
144
 
 
145
 
        /*
146
 
         * Tests to see if a Connection is closed.
147
 
         *
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.
154
 
         *
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.
158
 
         *
159
 
         * @return the status of the connection
160
 
         * @exception SQLException (why?)
161
 
         */
162
 
        public boolean isClosed() throws SQLException
163
 
        {
164
 
                // If the stream is gone, then close() was called
165
 
                if (pg_stream == null)
166
 
                        return true;
167
 
 
168
 
                // ok, test the connection
169
 
                try
170
 
                {
171
 
                        // by sending an empty query. If we are dead, then an SQLException should
172
 
                        // be thrown
173
 
                        java.sql.ResultSet rs = ExecSQL(" ");
174
 
                        if (rs != null)
175
 
                                rs.close();
176
 
 
177
 
                        // By now, we must be alive
178
 
                        return false;
179
 
                }
180
 
                catch (SQLException se)
181
 
                {
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.
186
 
                        return true;
187
 
                }
188
 
        }
189
 
 
190
 
        /*
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.
195
 
         *
196
 
         * @return a DatabaseMetaData object for this connection
197
 
         * @exception SQLException if a database access error occurs
198
 
         */
199
 
        public java.sql.DatabaseMetaData getMetaData() throws SQLException
200
 
        {
201
 
                if (metadata == null)
202
 
                        metadata = new DatabaseMetaData(this);
203
 
                return metadata;
204
 
        }
205
 
 
206
 
        /*
207
 
         * This overides the method in org.postgresql.Connection and returns a
208
 
         * ResultSet.
209
 
         */
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
211
 
        {
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.
214
 
                if (stat != null)
215
 
                {
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);
218
 
                }
219
 
 
220
 
                return new org.postgresql.jdbc2.ResultSet((org.postgresql.jdbc2.Connection)conn, fields, tuples, status, updateCount, insertOID, binaryCursor);
221
 
        }
222
 
 
223
 
        // *****************
224
 
        // JDBC 2 extensions
225
 
        // *****************
226
 
 
227
 
        public java.util.Map getTypeMap() throws SQLException
228
 
        {
229
 
                // new in 7.1
230
 
                return typemap;
231
 
        }
232
 
 
233
 
 
234
 
        public void setTypeMap(java.util.Map map) throws SQLException
235
 
        {
236
 
                // new in 7.1
237
 
                typemap = map;
238
 
        }
239
 
 
240
 
        /*
241
 
         * This overides the standard internal getObject method so that we can
242
 
         * check the jdbc2 type map first
243
 
         *
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
247
 
         */
248
 
        public Object getObject(String type, String value) throws SQLException
249
 
        {
250
 
                if (typemap != null)
251
 
                {
252
 
                        SQLData d = (SQLData) typemap.get(type);
253
 
                        if (d != null)
254
 
                        {
255
 
                                // Handle the type (requires SQLInput & SQLOutput classes to be implemented)
256
 
                                throw org.postgresql.Driver.notImplemented();
257
 
                        }
258
 
                }
259
 
 
260
 
                // Default to the original method
261
 
                return super.getObject(type, value);
262
 
        }
263
 
 
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.
268
 
         */
269
 
        public int getSQLType(String pgTypeName)
270
 
        {
271
 
                int sqlType = Types.OTHER; // default value
272
 
                for (int i = 0;i < jdbc2Types.length;i++)
273
 
                {
274
 
                        if (pgTypeName.equals(jdbc2Types[i]))
275
 
                        {
276
 
                                sqlType = jdbc2Typei[i];
277
 
                                break;
278
 
                        }
279
 
                }
280
 
                return sqlType;
281
 
        }
282
 
 
283
 
        /*
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
287
 
         *
288
 
         * Note: This must be in the same order as below.
289
 
         *
290
 
         * Tip: keep these grouped together by the Types. value
291
 
         */
292
 
        private static final String jdbc2Types[] = {
293
 
                                "int2",
294
 
                                "int4", "oid",
295
 
                                "int8",
296
 
                                "cash", "money",
297
 
                                "numeric",
298
 
                                "float4",
299
 
                                "float8",
300
 
                                "bpchar", "char", "char2", "char4", "char8", "char16",
301
 
                                "varchar", "text", "name", "filename",
302
 
                                "bytea",
303
 
                                "bool",
304
 
                                "date",
305
 
                                "time",
306
 
                                "abstime", "timestamp", "timestamptz",
307
 
                                "_bool", "_char", "_int2", "_int4", "_text",
308
 
                                "_oid", "_varchar", "_int8", "_float4", "_float8",
309
 
                                "_abstime", "_date", "_time", "_timestamp", "_numeric",
310
 
                                "_bytea"
311
 
                        };
312
 
 
313
 
        /*
314
 
         * This table holds the JDBC type for each entry above.
315
 
         *
316
 
         * Note: This must be in the same order as above
317
 
         *
318
 
         * Tip: keep these grouped together by the Types. value
319
 
         */
320
 
        private static final int jdbc2Typei[] = {
321
 
                                                                                                Types.SMALLINT,
322
 
                                                                                                Types.INTEGER, Types.INTEGER,
323
 
                                                                                                Types.BIGINT,
324
 
                                                                                                Types.DOUBLE, Types.DOUBLE,
325
 
                                                                                                Types.NUMERIC,
326
 
                                                                                                Types.REAL,
327
 
                                                                                                Types.DOUBLE,
328
 
                                                                                                Types.CHAR, Types.CHAR, Types.CHAR, Types.CHAR, Types.CHAR, Types.CHAR,
329
 
                                                                                                Types.VARCHAR, Types.VARCHAR, Types.VARCHAR, Types.VARCHAR,
330
 
                                                                                                Types.BINARY,
331
 
                                                                                                Types.BIT,
332
 
                                                                                                Types.DATE,
333
 
                                                                                                Types.TIME,
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,
338
 
                                                                                                Types.ARRAY
339
 
                                                                                        };
340
 
 
341
 
 
342
 
}
343
 
 
344
 
// ***********************************************************************
345