1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382
|
package SQLite.JDBC2z1;
import java.sql.BatchUpdateException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
import java.sql.SQLWarning;
import java.sql.Statement;
import java.util.ArrayList;
public class JDBCStatement implements java.sql.Statement {
protected JDBCConnection conn;
protected JDBCResultSet rs;
protected int updcnt;
protected int maxrows = 0;
private ArrayList<String> batch;
public JDBCStatement(JDBCConnection conn) {
this.conn = conn;
this.updcnt = 0;
this.rs = null;
this.batch = null;
}
@Override
public void setFetchSize(int fetchSize) throws SQLException {
if (fetchSize != 1) {
throw new SQLException("fetch size not 1");
}
}
@Override
public int getFetchSize() throws SQLException {
return 1;
}
@Override
public int getMaxRows() throws SQLException {
return maxrows;
}
@Override
public void setMaxRows(int max) throws SQLException {
if (max < 0) {
throw new SQLException("max must be >= 0 (was " + max + ")");
}
maxrows = max;
}
@Override
public void setFetchDirection(int fetchDirection) throws SQLException {
throw new SQLException("not supported");
}
@Override
public int getFetchDirection() throws SQLException {
return ResultSet.FETCH_UNKNOWN;
}
@Override
public int getResultSetConcurrency() throws SQLException {
return ResultSet.CONCUR_READ_ONLY;
}
@Override
public int getResultSetType() throws SQLException {
return ResultSet.TYPE_SCROLL_INSENSITIVE;
}
@Override
public void setQueryTimeout(int seconds) throws SQLException {
if (isClosed()) {
throw new SQLException("can't set query timeout on " +
"a closed statement");
} else if (seconds < 0) {
throw new SQLException("can't set a query timeout of " +
"less than 0 seconds");
} else if (seconds == 0) {
conn.timeout = 5000;
} else {
conn.timeout = seconds * 1000;
}
}
@Override
public int getQueryTimeout() throws SQLException {
return conn.timeout / 1000;
}
@Override
public ResultSet getResultSet() throws SQLException {
return rs;
}
ResultSet executeQuery(String sql, String args[], boolean updonly)
throws SQLException {
SQLite.TableResult tr = null;
if (rs != null) {
rs.close();
rs = null;
}
updcnt = -1;
if (conn == null || conn.db == null) {
throw new SQLException("stale connection");
}
int busy = 0;
boolean starttrans = !conn.autocommit && !conn.intrans;
while (true) {
try {
if (starttrans) {
conn.db.exec("BEGIN TRANSACTION", null);
conn.intrans = true;
}
if (args == null) {
if (updonly) {
conn.db.exec(sql, null);
} else {
tr = conn.db.get_table(sql, maxrows);
}
} else {
if (updonly) {
conn.db.exec(sql, null, args);
} else {
tr = conn.db.get_table(sql, maxrows, args);
}
}
updcnt = (int) conn.db.changes();
} catch (SQLite.Exception e) {
if (conn.db.is3() &&
conn.db.last_error() == SQLite.Constants.SQLITE_BUSY &&
conn.busy3(conn.db, ++busy)) {
try {
if (starttrans && conn.intrans) {
conn.db.exec("ROLLBACK", null);
conn.intrans = false;
}
} catch (SQLite.Exception ee) {
}
try {
int ms = 20 + busy * 10;
if (ms > 1000) {
ms = 1000;
}
synchronized (this) {
this.wait(ms);
}
} catch (java.lang.Exception eee) {
}
continue;
}
throw new SQLException(e);
}
break;
}
if (!updonly && tr == null) {
throw new SQLException("no result set produced");
}
if (!updonly && tr != null) {
rs = new JDBCResultSet(new TableResultX(tr), this);
}
return rs;
}
@Override
public ResultSet executeQuery(String sql) throws SQLException {
return executeQuery(sql, null, false);
}
@Override
public boolean execute(String sql) throws SQLException {
return executeQuery(sql) != null;
}
@Override
public void cancel() throws SQLException {
if (conn == null || conn.db == null) {
throw new SQLException("stale connection");
}
conn.db.interrupt();
}
@Override
public void clearWarnings() throws SQLException {
}
@Override
public Connection getConnection() throws SQLException {
return conn;
}
@Override
public void addBatch(String sql) throws SQLException {
if (batch == null) {
batch = new ArrayList<String>(1);
}
batch.add(sql);
}
@Override
public int[] executeBatch() throws SQLException {
if (batch == null) {
return new int[0];
}
int[] ret = new int[batch.size()];
for (int i = 0; i < ret.length; i++) {
ret[i] = EXECUTE_FAILED;
}
int errs = 0;
Exception cause = null;
for (int i = 0; i < ret.length; i++) {
try {
execute(batch.get(i));
ret[i] = updcnt;
} catch (SQLException e) {
++errs;
if (cause == null) {
cause = e;
}
}
}
if (errs > 0) {
throw new BatchUpdateException("batch failed", ret, cause);
}
return ret;
}
@Override
public void clearBatch() throws SQLException {
if (batch != null) {
batch.clear();
batch = null;
}
}
@Override
public void close() throws SQLException {
clearBatch();
conn = null;
}
@Override
public int executeUpdate(String sql) throws SQLException {
executeQuery(sql, null, true);
return updcnt;
}
@Override
public int getMaxFieldSize() throws SQLException {
return 0;
}
@Override
public boolean getMoreResults() throws SQLException {
if (rs != null) {
rs.close();
rs = null;
}
return false;
}
@Override
public int getUpdateCount() throws SQLException {
return updcnt;
}
@Override
public SQLWarning getWarnings() throws SQLException {
return null;
}
@Override
public void setCursorName(String name) throws SQLException {
throw new SQLFeatureNotSupportedException();
}
@Override
public void setEscapeProcessing(boolean enable) throws SQLException {
throw new SQLException("not supported");
}
@Override
public void setMaxFieldSize(int max) throws SQLException {
throw new SQLException("not supported");
}
@Override
public boolean getMoreResults(int x) throws SQLException {
throw new SQLFeatureNotSupportedException();
}
@Override
public ResultSet getGeneratedKeys() throws SQLException {
throw new SQLFeatureNotSupportedException();
}
@Override
public int executeUpdate(String sql, int autokeys)
throws SQLException {
if (autokeys != Statement.NO_GENERATED_KEYS) {
throw new SQLFeatureNotSupportedException("generated keys not supported");
}
return executeUpdate(sql);
}
@Override
public int executeUpdate(String sql, int colIndexes[])
throws SQLException {
throw new SQLFeatureNotSupportedException();
}
@Override
public int executeUpdate(String sql, String colIndexes[])
throws SQLException {
throw new SQLFeatureNotSupportedException();
}
@Override
public boolean execute(String sql, int autokeys)
throws SQLException {
if (autokeys != Statement.NO_GENERATED_KEYS) {
throw new SQLFeatureNotSupportedException("autogenerated keys not supported");
}
return execute(sql);
}
@Override
public boolean execute(String sql, int colIndexes[])
throws SQLException {
throw new SQLFeatureNotSupportedException();
}
@Override
public boolean execute(String sql, String colIndexes[])
throws SQLException {
throw new SQLFeatureNotSupportedException();
}
@Override
public int getResultSetHoldability() throws SQLException {
return ResultSet.HOLD_CURSORS_OVER_COMMIT;
}
@Override
public boolean isClosed() throws SQLException {
return conn == null;
}
@Override
public void setPoolable(boolean yes) throws SQLException {
if (yes) {
throw new SQLException("poolable statements not supported");
}
}
@Override
public boolean isPoolable() throws SQLException {
return false;
}
@Override
public <T> T unwrap(java.lang.Class<T> iface) throws SQLException {
throw new SQLException("unsupported");
}
@Override
public boolean isWrapperFor(java.lang.Class iface) throws SQLException {
return false;
}
@Override
public void closeOnCompletion() throws SQLException {
throw new SQLFeatureNotSupportedException();
}
@Override
public boolean isCloseOnCompletion() throws SQLException {
throw new SQLFeatureNotSupportedException();
}
}
|