~ubuntu-branches/ubuntu/oneiric/libpgjava/oneiric

« back to all changes in this revision

Viewing changes to src/interfaces/jdbc/org/postgresql/jdbc2/Statement.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.sql.*;
9
 
import java.util.Vector;
10
 
import org.postgresql.util.*;
11
 
 
12
 
/*
13
 
 * A Statement object is used for executing a static SQL statement and
14
 
 * obtaining the results produced by it.
15
 
 *
16
 
 * <p>Only one ResultSet per Statement can be open at any point in time.
17
 
 * Therefore, if the reading of one ResultSet is interleaved with the
18
 
 * reading of another, each must have been generated by different
19
 
 * Statements.  All statement execute methods implicitly close a
20
 
 * statement's current ResultSet if an open one exists.
21
 
 *
22
 
 * @see java.sql.Statement
23
 
 * @see ResultSet
24
 
 */
25
 
public class Statement extends org.postgresql.Statement implements java.sql.Statement
26
 
{
27
 
        private Connection connection;          // The connection who created us
28
 
        private Vector batch = null;
29
 
        private int resultsettype;                                // the resultset type to return
30
 
        private int concurrency;                 // is it updateable or not?
31
 
 
32
 
        /*
33
 
         * Constructor for a Statement.  It simply sets the connection
34
 
         * that created us.
35
 
         *
36
 
         * @param c the Connection instantation that creates us
37
 
         */
38
 
        public Statement (Connection c)
39
 
        {
40
 
                connection = c;
41
 
                resultsettype = java.sql.ResultSet.TYPE_SCROLL_INSENSITIVE;
42
 
                concurrency = java.sql.ResultSet.CONCUR_READ_ONLY;
43
 
        }
44
 
 
45
 
        /*
46
 
         * Execute a SQL statement that retruns a single ResultSet
47
 
         *
48
 
         * @param sql typically a static SQL SELECT statement
49
 
         * @return a ResulSet that contains the data produced by the query
50
 
         * @exception SQLException if a database access error occurs
51
 
         */
52
 
        public java.sql.ResultSet executeQuery(String sql) throws SQLException
53
 
        {
54
 
                this.execute(sql);
55
 
                while (result != null && !((org.postgresql.ResultSet)result).reallyResultSet())
56
 
                        result = ((org.postgresql.ResultSet)result).getNext();
57
 
                if (result == null)
58
 
                        throw new PSQLException("postgresql.stat.noresult");
59
 
                return result;
60
 
        }
61
 
 
62
 
        /*
63
 
         * Execute a SQL INSERT, UPDATE or DELETE statement.  In addition
64
 
         * SQL statements that return nothing such as SQL DDL statements
65
 
         * can be executed
66
 
         *
67
 
         * @param sql a SQL statement
68
 
         * @return either a row count, or 0 for SQL commands
69
 
         * @exception SQLException if a database access error occurs
70
 
         */
71
 
        public int executeUpdate(String sql) throws SQLException
72
 
        {
73
 
                this.execute(sql);
74
 
                if (((org.postgresql.ResultSet)result).reallyResultSet())
75
 
                        throw new PSQLException("postgresql.stat.result");
76
 
                return this.getUpdateCount();
77
 
        }
78
 
 
79
 
        /*
80
 
         * setCursorName defines the SQL cursor name that will be used by
81
 
         * subsequent execute methods.  This name can then be used in SQL
82
 
         * positioned update/delete statements to identify the current row
83
 
         * in the ResultSet generated by this statement.  If a database
84
 
         * doesn't support positioned update/delete, this method is a
85
 
         * no-op.
86
 
         *
87
 
         * <p><B>Note:</B> By definition, positioned update/delete execution
88
 
         * must be done by a different Statement than the one which
89
 
         * generated the ResultSet being used for positioning.  Also, cursor
90
 
         * names must be unique within a Connection.
91
 
         *
92
 
         * <p>We throw an additional constriction.      There can only be one
93
 
         * cursor active at any one time.
94
 
         *
95
 
         * @param name the new cursor name
96
 
         * @exception SQLException if a database access error occurs
97
 
         */
98
 
        public void setCursorName(String name) throws SQLException
99
 
        {
100
 
                connection.setCursorName(name);
101
 
        }
102
 
 
103
 
        /*
104
 
         * Execute a SQL statement that may return multiple results. We
105
 
         * don't have to worry about this since we do not support multiple
106
 
         * ResultSets.   You can use getResultSet or getUpdateCount to
107
 
         * retrieve the result.
108
 
         *
109
 
         * @param sql any SQL statement
110
 
         * @return true if the next result is a ResulSet, false if it is
111
 
         *      an update count or there are no more results
112
 
         * @exception SQLException if a database access error occurs
113
 
         */
114
 
        public boolean execute(String sql) throws SQLException
115
 
        {
116
 
                if (escapeProcessing)
117
 
                        sql = escapeSQL(sql);
118
 
 
119
 
                // New in 7.1, if we have a previous resultset then force it to close
120
 
                // This brings us nearer to compliance, and helps memory management.
121
 
                // Internal stuff will call ExecSQL directly, bypassing this.
122
 
                if (result != null)
123
 
                {
124
 
                        java.sql.ResultSet rs = getResultSet();
125
 
                        if (rs != null)
126
 
                                rs.close();
127
 
                }
128
 
 
129
 
                // New in 7.1, pass Statement so that ExecSQL can customise to it
130
 
                result = connection.ExecSQL(sql, this);
131
 
 
132
 
                // New in 7.1, required for ResultSet.getStatement() to work
133
 
                ((org.postgresql.jdbc2.ResultSet)result).setStatement(this);
134
 
 
135
 
                return (result != null && ((org.postgresql.ResultSet)result).reallyResultSet());
136
 
        }
137
 
 
138
 
        /*
139
 
         * getUpdateCount returns the current result as an update count,
140
 
         * if the result is a ResultSet or there are no more results, -1
141
 
         * is returned.  It should only be called once per result.
142
 
         *
143
 
         * @return the current result as an update count.
144
 
         * @exception SQLException if a database access error occurs
145
 
         */
146
 
        public int getUpdateCount() throws SQLException
147
 
        {
148
 
                if (result == null)
149
 
                        return -1;
150
 
                if (((org.postgresql.ResultSet)result).reallyResultSet())
151
 
                        return -1;
152
 
                return ((org.postgresql.ResultSet)result).getResultCount();
153
 
        }
154
 
 
155
 
        /*
156
 
         * getMoreResults moves to a Statement's next result.  If it returns
157
 
         * true, this result is a ResulSet.
158
 
         *
159
 
         * @return true if the next ResultSet is valid
160
 
         * @exception SQLException if a database access error occurs
161
 
         */
162
 
        public boolean getMoreResults() throws SQLException
163
 
        {
164
 
                result = ((org.postgresql.ResultSet)result).getNext();
165
 
                return (result != null && ((org.postgresql.ResultSet)result).reallyResultSet());
166
 
        }
167
 
 
168
 
        // ** JDBC 2 Extensions **
169
 
 
170
 
        public void addBatch(String sql) throws SQLException
171
 
        {
172
 
                if (batch == null)
173
 
                        batch = new Vector();
174
 
                batch.addElement(sql);
175
 
        }
176
 
 
177
 
        public void clearBatch() throws SQLException
178
 
        {
179
 
                if (batch != null)
180
 
                        batch.removeAllElements();
181
 
        }
182
 
 
183
 
        public int[] executeBatch() throws SQLException
184
 
        {
185
 
                if (batch == null)
186
 
                        batch = new Vector();
187
 
                int size = batch.size();
188
 
                int[] result = new int[size];
189
 
                int i = 0;
190
 
                try
191
 
                {
192
 
                        for (i = 0;i < size;i++)
193
 
                                result[i] = this.executeUpdate((String)batch.elementAt(i));
194
 
                }
195
 
                catch (SQLException e)
196
 
                {
197
 
                        int[] resultSucceeded = new int[i];
198
 
                        System.arraycopy(result, 0, resultSucceeded, 0, i);
199
 
 
200
 
                        PBatchUpdateException updex =
201
 
                                new PBatchUpdateException("postgresql.stat.batch.error",
202
 
                                                                                  new Integer(i), batch.elementAt(i), resultSucceeded);
203
 
                        updex.setNextException(e);
204
 
 
205
 
                        throw updex;
206
 
                }
207
 
                finally
208
 
                {
209
 
                        batch.removeAllElements();
210
 
                }
211
 
                return result;
212
 
        }
213
 
 
214
 
        public java.sql.Connection getConnection() throws SQLException
215
 
        {
216
 
                return (java.sql.Connection)connection;
217
 
        }
218
 
 
219
 
        public int getFetchDirection() throws SQLException
220
 
        {
221
 
                throw new PSQLException("postgresql.psqlnotimp");
222
 
        }
223
 
 
224
 
        public int getFetchSize() throws SQLException
225
 
        {
226
 
                // This one can only return a valid value when were a cursor?
227
 
                throw org.postgresql.Driver.notImplemented();
228
 
        }
229
 
 
230
 
        public int getResultSetConcurrency() throws SQLException
231
 
        {
232
 
                // new in 7.1
233
 
                return concurrency;
234
 
        }
235
 
 
236
 
        public int getResultSetType() throws SQLException
237
 
        {
238
 
                // new in 7.1
239
 
                return resultsettype;
240
 
        }
241
 
 
242
 
        public void setFetchDirection(int direction) throws SQLException
243
 
        {
244
 
                throw org.postgresql.Driver.notImplemented();
245
 
        }
246
 
 
247
 
        public void setFetchSize(int rows) throws SQLException
248
 
        {
249
 
                throw org.postgresql.Driver.notImplemented();
250
 
        }
251
 
 
252
 
        /*
253
 
         * New in 7.1
254
 
         */
255
 
        public void setResultSetConcurrency(int value) throws SQLException
256
 
        {
257
 
                concurrency = value;
258
 
        }
259
 
 
260
 
        /*
261
 
         * New in 7.1
262
 
         */
263
 
        public void setResultSetType(int value) throws SQLException
264
 
        {
265
 
                resultsettype = value;
266
 
        }
267
 
}