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.
9
import java.util.Vector;
10
import org.postgresql.util.*;
13
* A Statement object is used for executing a static SQL statement and
14
* obtaining the results produced by it.
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.
22
* @see java.sql.Statement
25
public class Statement extends org.postgresql.Statement implements java.sql.Statement
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?
33
* Constructor for a Statement. It simply sets the connection
36
* @param c the Connection instantation that creates us
38
public Statement (Connection c)
41
resultsettype = java.sql.ResultSet.TYPE_SCROLL_INSENSITIVE;
42
concurrency = java.sql.ResultSet.CONCUR_READ_ONLY;
46
* Execute a SQL statement that retruns a single ResultSet
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
52
public java.sql.ResultSet executeQuery(String sql) throws SQLException
55
while (result != null && !((org.postgresql.ResultSet)result).reallyResultSet())
56
result = ((org.postgresql.ResultSet)result).getNext();
58
throw new PSQLException("postgresql.stat.noresult");
63
* Execute a SQL INSERT, UPDATE or DELETE statement. In addition
64
* SQL statements that return nothing such as SQL DDL statements
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
71
public int executeUpdate(String sql) throws SQLException
74
if (((org.postgresql.ResultSet)result).reallyResultSet())
75
throw new PSQLException("postgresql.stat.result");
76
return this.getUpdateCount();
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
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.
92
* <p>We throw an additional constriction. There can only be one
93
* cursor active at any one time.
95
* @param name the new cursor name
96
* @exception SQLException if a database access error occurs
98
public void setCursorName(String name) throws SQLException
100
connection.setCursorName(name);
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.
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
114
public boolean execute(String sql) throws SQLException
116
if (escapeProcessing)
117
sql = escapeSQL(sql);
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.
124
java.sql.ResultSet rs = getResultSet();
129
// New in 7.1, pass Statement so that ExecSQL can customise to it
130
result = connection.ExecSQL(sql, this);
132
// New in 7.1, required for ResultSet.getStatement() to work
133
((org.postgresql.jdbc2.ResultSet)result).setStatement(this);
135
return (result != null && ((org.postgresql.ResultSet)result).reallyResultSet());
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.
143
* @return the current result as an update count.
144
* @exception SQLException if a database access error occurs
146
public int getUpdateCount() throws SQLException
150
if (((org.postgresql.ResultSet)result).reallyResultSet())
152
return ((org.postgresql.ResultSet)result).getResultCount();
156
* getMoreResults moves to a Statement's next result. If it returns
157
* true, this result is a ResulSet.
159
* @return true if the next ResultSet is valid
160
* @exception SQLException if a database access error occurs
162
public boolean getMoreResults() throws SQLException
164
result = ((org.postgresql.ResultSet)result).getNext();
165
return (result != null && ((org.postgresql.ResultSet)result).reallyResultSet());
168
// ** JDBC 2 Extensions **
170
public void addBatch(String sql) throws SQLException
173
batch = new Vector();
174
batch.addElement(sql);
177
public void clearBatch() throws SQLException
180
batch.removeAllElements();
183
public int[] executeBatch() throws SQLException
186
batch = new Vector();
187
int size = batch.size();
188
int[] result = new int[size];
192
for (i = 0;i < size;i++)
193
result[i] = this.executeUpdate((String)batch.elementAt(i));
195
catch (SQLException e)
197
int[] resultSucceeded = new int[i];
198
System.arraycopy(result, 0, resultSucceeded, 0, i);
200
PBatchUpdateException updex =
201
new PBatchUpdateException("postgresql.stat.batch.error",
202
new Integer(i), batch.elementAt(i), resultSucceeded);
203
updex.setNextException(e);
209
batch.removeAllElements();
214
public java.sql.Connection getConnection() throws SQLException
216
return (java.sql.Connection)connection;
219
public int getFetchDirection() throws SQLException
221
throw new PSQLException("postgresql.psqlnotimp");
224
public int getFetchSize() throws SQLException
226
// This one can only return a valid value when were a cursor?
227
throw org.postgresql.Driver.notImplemented();
230
public int getResultSetConcurrency() throws SQLException
236
public int getResultSetType() throws SQLException
239
return resultsettype;
242
public void setFetchDirection(int direction) throws SQLException
244
throw org.postgresql.Driver.notImplemented();
247
public void setFetchSize(int rows) throws SQLException
249
throw org.postgresql.Driver.notImplemented();
255
public void setResultSetConcurrency(int value) throws SQLException
263
public void setResultSetType(int value) throws SQLException
265
resultsettype = value;