00001
00025
package org.objectweb.cjdbc.driver;
00026
00027
import java.sql.BatchUpdateException;
00028
import java.sql.SQLException;
00029
import java.sql.SQLWarning;
00030
import java.util.Vector;
00031
00032
import org.objectweb.cjdbc.common.sql.AbstractWriteRequest;
00033
import org.objectweb.cjdbc.common.sql.AlterRequest;
00034
import org.objectweb.cjdbc.common.sql.CreateRequest;
00035
import org.objectweb.cjdbc.common.sql.DeleteRequest;
00036
import org.objectweb.cjdbc.common.sql.DropRequest;
00037
import org.objectweb.cjdbc.common.sql.InsertRequest;
00038
import org.objectweb.cjdbc.common.sql.NotImplementedException;
00039
import org.objectweb.cjdbc.common.sql.SelectRequest;
00040
import org.objectweb.cjdbc.common.sql.StoredProcedure;
00041
import org.objectweb.cjdbc.common.sql.UpdateRequest;
00042
00060 public class Statement implements java.sql.
Statement
00061 {
00063 protected Connection connection = null;
00064
00066 private Vector
batch = null;
00067
00069 private SQLWarning
warnings = null;
00070
00072 private java.sql.ResultSet
result = null;
00073
00075 private int updateCount = -1;
00076
00078 private int timeout = 0;
00079
00081 private int fetchSize = 0;
00083 private String
cursorName;
00084
00086 private int resultSetType;
00087
00089 private int maxFieldSize = 0;
00090
00092 private int maxRows = 0;
00093
00097 protected boolean escapeProcessing =
true;
00098
00100 protected java.sql.ResultSet
generatedKeys = null;
00101 protected int generatedKeysFlag = java.sql.Statement.NO_GENERATED_KEYS;
00102
00108 public Statement(
Connection c)
00109 {
00110
connection = c;
00111
resultSetType = java.sql.ResultSet.TYPE_FORWARD_ONLY;
00112 }
00113
00120 public void addBatch(String sql)
throws SQLException
00121 {
00122
if (
batch == null)
00123
batch =
new Vector();
00124
batch.addElement(sql.trim());
00125 }
00126
00133 public void cancel() throws SQLException
00134 {
00135
throw new NotImplementedException(
"cancel()");
00136 }
00137
00143 public void clearBatch() throws SQLException
00144 {
00145
if (
batch != null)
00146
batch.removeAllElements();
00147 }
00148
00155 public void clearWarnings() throws SQLException
00156 {
00157
warnings = null;
00158 }
00159
00167 public int[]
executeBatch() throws SQLException
00168 {
00169
if (
batch == null ||
batch.isEmpty())
00170
throw new SQLException(
"Error: batch is empty");
00171
00172
int size =
batch.size();
00173
int[]
result =
new int[size];
00174
int i = 0;
00175
00176
try
00177 {
00178
for (i = 0; i < size; i++)
00179 result[i] =
this.executeUpdate((String)
batch.elementAt(i));
00180
return result;
00181 }
00182
catch (SQLException e)
00183 {
00184 String message =
"Batch failed for request " + i +
": "
00185 +
batch.elementAt(i) +
" (" + e +
")";
00186
00187
int[] updateCounts =
new int[i];
00188 System.arraycopy(result, 0, updateCounts, 0, i);
00189
00190
throw new BatchUpdateException(message, updateCounts);
00191 }
00192 finally
00193 {
00194
batch.removeAllElements();
00195 }
00196 }
00197
00209 public void close() throws SQLException
00210 {
00211
00212
if (
result != null)
00213
result.close();
00214
00215
00216
result = null;
00217
connection = null;
00218 }
00219
00227 public boolean execute(String sql)
throws SQLException
00228 {
00229
if (sql.regionMatches(
true, 0,
"select", 0, 6)
00230 || (sql.regionMatches(
true, 0,
"{call", 0, 5)))
00231 {
00232
result =
executeQuery(sql);
00233
return true;
00234 }
00235
else
00236 {
00237
updateCount =
executeUpdate(sql);
00238
return false;
00239 }
00240 }
00241
00249 public java.sql.ResultSet
executeQuery(String sql)
throws SQLException
00250 {
00251
return executeQuery(null, sql.trim());
00252 }
00253
00263 protected java.sql.ResultSet
executeQuery(String sqlSkeleton, String sqlQuery)
00264
throws SQLException
00265 {
00266
updateCount = -1;
00267
if (
result != null)
00268 {
00269
result.close();
00270
result = null;
00271 }
00272
00273
if (sqlQuery.regionMatches(
true, 0,
"select", 0, 6))
00274 {
00275 SelectRequest request =
new SelectRequest(sqlQuery,
escapeProcessing,
00276
timeout,
connection.
LINE_SEPARATOR);
00277
if (
connection.
needSqlSkeleton)
00278 request.setSqlSkeleton(sqlSkeleton);
00279 request.setMaxRows(
maxRows);
00280 request.setFetchSize(
fetchSize);
00281 request.setCursorName(
cursorName);
00282
result =
connection.
execReadRequest(request);
00283 }
00284
else if (sqlQuery.regionMatches(
true, 0,
"{call", 0, 5))
00285 {
00286 StoredProcedure proc =
new StoredProcedure(sqlQuery,
escapeProcessing,
00287
timeout,
connection.
LINE_SEPARATOR);
00288 proc.setMaxRows(
maxRows);
00289
result =
connection.
execReadStoredProcedure(proc);
00290 }
00291
else
00292
throw new SQLException(
00293
"executeQuery only accepts statements starting with 'select' or '{call' ("
00294 + sqlQuery +
")");
00295
00296
if (
result instanceof
DriverResultSet)
00297 ((
DriverResultSet)
result).setStatement(
this);
00298
return result;
00299 }
00300
00309 public int executeUpdate(String sql)
throws SQLException
00310 {
00311
return executeUpdateWithSkeleton(null, sql.trim());
00312 }
00313
00323 protected int executeUpdateWithSkeleton(String sqlSkeleton, String sqlQuery)
00324
throws SQLException
00325 {
00326
if (
result != null)
00327 {
00328
result.close();
00329
result = null;
00330 }
00331
00332
00333
00334 String lower = sqlQuery.substring(0,
00335 6 < sqlQuery.length() ? 6 : sqlQuery.length()).toLowerCase();
00336 AbstractWriteRequest request;
00337
if (lower.equals(
"insert"))
00338 request =
new InsertRequest(sqlQuery,
escapeProcessing,
timeout,
00339
connection.
LINE_SEPARATOR);
00340
else if (lower.equals(
"update"))
00341 request =
new UpdateRequest(sqlQuery,
escapeProcessing,
timeout,
00342
connection.
LINE_SEPARATOR);
00343
else if (lower.equals(
"delete"))
00344 request =
new DeleteRequest(sqlQuery,
escapeProcessing,
timeout,
00345
connection.
LINE_SEPARATOR);
00346
else if (lower.startsWith(
"create"))
00347 request =
new CreateRequest(sqlQuery,
escapeProcessing,
timeout,
00348
connection.
LINE_SEPARATOR);
00349
else if (lower.startsWith(
"drop"))
00350 request =
new DropRequest(sqlQuery,
escapeProcessing,
timeout,
00351
connection.
LINE_SEPARATOR);
00352
else if (lower.startsWith(
"alter"))
00353 request =
new AlterRequest(sqlQuery,
escapeProcessing,
timeout,
00354
connection.
LINE_SEPARATOR);
00355
else if (lower.startsWith(
"{call"))
00356 {
00357
updateCount =
connection.
execWriteStoredProcedure(
new StoredProcedure(
00358 sqlQuery,
escapeProcessing,
timeout,
connection.
LINE_SEPARATOR));
00359
return updateCount;
00360 }
00361
else
00362
throw new SQLException(
00363
"executeUpdate only accepts statements starting with insert/update/delete/create/drop/{call ("
00364 + sqlQuery +
")");
00365
00366
if (
connection.
needSqlSkeleton)
00367 request.setSqlSkeleton(sqlSkeleton);
00368
00369
if (
generatedKeysFlag ==
Statement.RETURN_GENERATED_KEYS)
00370 {
00371
generatedKeys =
connection.
execWriteRequestWithKeys(request);
00372
00373
00374
00375
00376
00377
00378
00379
return 1;
00380 }
00381
else
00382 {
00383
updateCount =
connection.
execWriteRequest(request);
00384
return updateCount;
00385 }
00386 }
00387
00394 public java.sql.Connection
getConnection() throws SQLException
00395 {
00396
return connection;
00397 }
00398
00405 public int getFetchDirection() throws SQLException
00406 {
00407
throw new NotImplementedException(
"getFetchDirection()");
00408 }
00409
00413 public int getFetchSize() throws SQLException
00414 {
00415
return fetchSize;
00416 }
00417
00430 public int getMaxFieldSize() throws SQLException
00431 {
00432
return maxFieldSize;
00433 }
00434
00443 public int getMaxRows() throws SQLException
00444 {
00445
return maxRows;
00446 }
00447
00455 public boolean getMoreResults() throws SQLException
00456 {
00457
if (
result != null)
00458
result.close();
00459
updateCount = -1;
00460
return false;
00461 }
00462
00471 public int getQueryTimeout() throws SQLException
00472 {
00473
return timeout;
00474 }
00475
00482 public java.sql.ResultSet
getResultSet() throws SQLException
00483 {
00484
return result;
00485 }
00486
00493 public int getResultSetConcurrency() throws SQLException
00494 {
00495
return java.sql.ResultSet.CONCUR_READ_ONLY;
00496 }
00497
00505 public int getResultSetType() throws SQLException
00506 {
00507
return resultSetType;
00508 }
00509
00518 public int getUpdateCount() throws SQLException
00519 {
00520
return updateCount;
00521 }
00522
00538 public SQLWarning
getWarnings() throws SQLException
00539 {
00540
return warnings;
00541 }
00542
00554 public void setCursorName(String name)
throws SQLException
00555 {
00556
cursorName = name;
00557 }
00558
00566 public void setEscapeProcessing(
boolean enable)
throws SQLException
00567 {
00568
escapeProcessing = enable;
00569 }
00570
00578 public void setFetchDirection(
int direction)
throws SQLException
00579 {
00580 }
00581
00588 public void setFetchSize(
int rows)
throws SQLException
00589 {
00590
fetchSize = rows;
00591 }
00592
00599 public void setMaxFieldSize(
int max)
throws SQLException
00600 {
00601
maxFieldSize = max;
00602 }
00603
00610 public void setMaxRows(
int max)
throws SQLException
00611 {
00612
maxRows = max;
00613 }
00614
00621 public void setQueryTimeout(
int seconds)
throws SQLException
00622 {
00623
timeout = seconds;
00624 }
00625
00630 public void setResultSetConcurrency(
int value)
throws SQLException
00631 {
00632
if (value != java.sql.ResultSet.CONCUR_READ_ONLY)
00633
throw new SQLException(
00634
"CONCUR_READ_ONLY is the only supported conccurency mode");
00635 }
00636
00641 public void setResultSetType(
int value)
throws SQLException
00642 {
00643
if (value == java.sql.ResultSet.TYPE_SCROLL_SENSITIVE)
00644
throw new SQLException(
00645
"TYPE_SCROLL_SENSITIVE is not a supported ResultSet type");
00646
resultSetType = value;
00647 }
00648
00649
00650
00687 public boolean getMoreResults(
int current)
throws SQLException
00688 {
00689
throw new NotImplementedException(
"getMoreResults");
00690 }
00691
00704 public java.sql.ResultSet
getGeneratedKeys() throws SQLException
00705 {
00706
return generatedKeys;
00707 }
00708
00729 public int executeUpdate(String sql,
int autoGeneratedKeys)
00730
throws SQLException
00731 {
00732
generatedKeysFlag = autoGeneratedKeys;
00733
return executeUpdate(sql);
00734 }
00735
00754 public int executeUpdate(String sql,
int columnIndexes[])
throws SQLException
00755 {
00756
throw new NotImplementedException(
"executeUpdate");
00757 }
00758
00776 public int executeUpdate(String sql, String columnNames[])
00777
throws SQLException
00778 {
00779
throw new NotImplementedException(
"executeUpdate");
00780 }
00781
00815 public boolean execute(String sql,
int autoGeneratedKeys)
throws SQLException
00816 {
00817
generatedKeysFlag = autoGeneratedKeys;
00818
return execute(sql);
00819 }
00820
00854 public boolean execute(String sql,
int columnIndexes[])
throws SQLException
00855 {
00856
throw new NotImplementedException(
"execute");
00857 }
00858
00892 public boolean execute(String sql, String columnNames[])
throws SQLException
00893 {
00894
throw new NotImplementedException(
"execute");
00895 }
00896
00906 public int getResultSetHoldability() throws SQLException
00907 {
00908
throw new NotImplementedException(
"getResultSetHoldability");
00909 }
00910
00911 }