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 || !
connection.
isDriverProcessed())
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 if (lower.startsWith(
"}call"))
00362 {
00363
00364
updateCount =
connection.
execWriteStoredProcedure(
new StoredProcedure(
"{"
00365 + sqlQuery.substring(1),
escapeProcessing,
timeout,
00366
Connection.LINE_SEPARATOR));
00367
return updateCount;
00368 }
00369
else
00370
throw new SQLException(
00371
"executeUpdate only accepts statements starting with insert/update/delete/create/drop/{call ("
00372 + sqlQuery +
")");
00373
00374
if (
connection.
needSqlSkeleton || !
connection.
isDriverProcessed())
00375 request.
setSqlSkeleton(sqlSkeleton);
00376
00377
if (
generatedKeysFlag ==
Statement.RETURN_GENERATED_KEYS)
00378 {
00379
generatedKeys =
connection.
execWriteRequestWithKeys(request);
00380
00381
00382
00383
00384
00385
00386
00387
return 1;
00388 }
00389
else
00390 {
00391
updateCount =
connection.
execWriteRequest(request);
00392
return updateCount;
00393 }
00394 }
00395
00402 public java.sql.Connection
getConnection() throws SQLException
00403 {
00404
return connection;
00405 }
00406
00413 public int getFetchDirection() throws SQLException
00414 {
00415
throw new NotImplementedException(
"getFetchDirection()");
00416 }
00417
00421 public int getFetchSize() throws SQLException
00422 {
00423
return fetchSize;
00424 }
00425
00438 public int getMaxFieldSize() throws SQLException
00439 {
00440
return maxFieldSize;
00441 }
00442
00451 public int getMaxRows() throws SQLException
00452 {
00453
return maxRows;
00454 }
00455
00463 public boolean getMoreResults() throws SQLException
00464 {
00465
if (
result != null)
00466
result.close();
00467
updateCount = -1;
00468
return false;
00469 }
00470
00479 public int getQueryTimeout() throws SQLException
00480 {
00481
return timeout;
00482 }
00483
00490 public java.sql.ResultSet
getResultSet() throws SQLException
00491 {
00492
return result;
00493 }
00494
00501 public int getResultSetConcurrency() throws SQLException
00502 {
00503
return java.sql.ResultSet.CONCUR_READ_ONLY;
00504 }
00505
00513 public int getResultSetType() throws SQLException
00514 {
00515
return resultSetType;
00516 }
00517
00526 public int getUpdateCount() throws SQLException
00527 {
00528
return updateCount;
00529 }
00530
00546 public SQLWarning
getWarnings() throws SQLException
00547 {
00548
return warnings;
00549 }
00550
00562 public void setCursorName(String name)
throws SQLException
00563 {
00564
cursorName = name;
00565 }
00566
00574 public void setEscapeProcessing(
boolean enable)
throws SQLException
00575 {
00576
escapeProcessing = enable;
00577 }
00578
00586 public void setFetchDirection(
int direction)
throws SQLException
00587 {
00588 }
00589
00596 public void setFetchSize(
int rows)
throws SQLException
00597 {
00598
fetchSize = rows;
00599 }
00600
00607 public void setMaxFieldSize(
int max)
throws SQLException
00608 {
00609
maxFieldSize = max;
00610 }
00611
00618 public void setMaxRows(
int max)
throws SQLException
00619 {
00620
maxRows = max;
00621 }
00622
00629 public void setQueryTimeout(
int seconds)
throws SQLException
00630 {
00631
timeout = seconds;
00632 }
00633
00638 public void setResultSetConcurrency(
int value)
throws SQLException
00639 {
00640
if (value != java.sql.ResultSet.CONCUR_READ_ONLY)
00641
throw new SQLException(
00642
"CONCUR_READ_ONLY is the only supported conccurency mode");
00643 }
00644
00649 public void setResultSetType(
int value)
throws SQLException
00650 {
00651
if (value == java.sql.ResultSet.TYPE_SCROLL_SENSITIVE)
00652
throw new SQLException(
00653
"TYPE_SCROLL_SENSITIVE is not a supported ResultSet type");
00654
resultSetType = value;
00655 }
00656
00657
00658
00695 public boolean getMoreResults(
int current)
throws SQLException
00696 {
00697
throw new NotImplementedException(
"getMoreResults");
00698 }
00699
00712 public java.sql.ResultSet
getGeneratedKeys() throws SQLException
00713 {
00714
return generatedKeys;
00715 }
00716
00737 public int executeUpdate(String sql,
int autoGeneratedKeys)
00738
throws SQLException
00739 {
00740
generatedKeysFlag = autoGeneratedKeys;
00741
return executeUpdate(sql);
00742 }
00743
00762 public int executeUpdate(String sql,
int columnIndexes[])
throws SQLException
00763 {
00764
throw new NotImplementedException(
"executeUpdate");
00765 }
00766
00784 public int executeUpdate(String sql, String columnNames[])
00785
throws SQLException
00786 {
00787
throw new NotImplementedException(
"executeUpdate");
00788 }
00789
00823 public boolean execute(String sql,
int autoGeneratedKeys)
throws SQLException
00824 {
00825
generatedKeysFlag = autoGeneratedKeys;
00826
return execute(sql);
00827 }
00828
00862 public boolean execute(String sql,
int columnIndexes[])
throws SQLException
00863 {
00864
throw new NotImplementedException(
"execute");
00865 }
00866
00900 public boolean execute(String sql, String columnNames[])
throws SQLException
00901 {
00902
throw new NotImplementedException(
"execute");
00903 }
00904
00914 public int getResultSetHoldability() throws SQLException
00915 {
00916
throw new NotImplementedException(
"getResultSetHoldability");
00917 }
00918
00919 }