| 1 |
package SQLite.JDBC2x; |
| 2 |
|
| 3 |
import java.sql.*; |
| 4 |
import java.util.*; |
| 5 |
|
| 6 |
public class JDBCStatement implements java.sql.Statement { |
| 7 |
|
| 8 |
protected JDBCConnection conn; |
| 9 |
protected JDBCResultSet rs; |
| 10 |
protected int updcnt; |
| 11 |
protected int maxrows = 0; |
| 12 |
private ArrayList batch; |
| 13 |
|
| 14 |
public JDBCStatement(JDBCConnection conn) { |
| 15 |
this.conn = conn; |
| 16 |
this.updcnt = 0; |
| 17 |
this.rs = null; |
| 18 |
this.batch = null; |
| 19 |
} |
| 20 |
|
| 21 |
public void setFetchSize(int fetchSize) throws SQLException { |
| 22 |
if (fetchSize != 1) { |
| 23 |
throw new SQLException("fetch size not 1"); |
| 24 |
} |
| 25 |
} |
| 26 |
|
| 27 |
public int getFetchSize() throws SQLException { |
| 28 |
return 1; |
| 29 |
} |
| 30 |
|
| 31 |
public int getMaxRows() throws SQLException { |
| 32 |
return maxrows; |
| 33 |
} |
| 34 |
|
| 35 |
public void setMaxRows(int max) throws SQLException { |
| 36 |
if (max < 0) { |
| 37 |
throw new SQLException("max must be >= 0 (was " + max + ")"); |
| 38 |
} |
| 39 |
maxrows = max; |
| 40 |
} |
| 41 |
|
| 42 |
public void setFetchDirection(int fetchDirection) throws SQLException { |
| 43 |
throw new SQLException("not supported"); |
| 44 |
} |
| 45 |
|
| 46 |
public int getFetchDirection() throws SQLException { |
| 47 |
return ResultSet.FETCH_UNKNOWN; |
| 48 |
} |
| 49 |
|
| 50 |
public int getResultSetConcurrency() throws SQLException { |
| 51 |
return ResultSet.CONCUR_READ_ONLY; |
| 52 |
} |
| 53 |
|
| 54 |
public int getResultSetType() throws SQLException { |
| 55 |
return ResultSet.TYPE_SCROLL_INSENSITIVE; |
| 56 |
} |
| 57 |
|
| 58 |
public void setQueryTimeout(int seconds) throws SQLException { |
| 59 |
conn.timeout = seconds * 1000; |
| 60 |
if (conn.timeout < 0) { |
| 61 |
conn.timeout = 120000; |
| 62 |
} else if (conn.timeout < 1000) { |
| 63 |
conn.timeout = 5000; |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
public int getQueryTimeout() throws SQLException { |
| 68 |
return conn.timeout / 1000; |
| 69 |
} |
| 70 |
|
| 71 |
public ResultSet getResultSet() throws SQLException { |
| 72 |
return rs; |
| 73 |
} |
| 74 |
|
| 75 |
ResultSet executeQuery(String sql, String args[], boolean updonly) |
| 76 |
throws SQLException { |
| 77 |
SQLite.TableResult tr = null; |
| 78 |
if (rs != null) { |
| 79 |
rs.close(); |
| 80 |
rs = null; |
| 81 |
} |
| 82 |
updcnt = -1; |
| 83 |
if (conn == null || conn.db == null) { |
| 84 |
throw new SQLException("stale connection"); |
| 85 |
} |
| 86 |
int busy = 0; |
| 87 |
boolean starttrans = !conn.autocommit && !conn.intrans; |
| 88 |
while (true) { |
| 89 |
try { |
| 90 |
if (starttrans) { |
| 91 |
conn.db.exec("BEGIN TRANSACTION", null); |
| 92 |
conn.intrans = true; |
| 93 |
} |
| 94 |
if (args == null) { |
| 95 |
if (updonly) { |
| 96 |
conn.db.exec(sql, null); |
| 97 |
} else { |
| 98 |
tr = conn.db.get_table(sql, maxrows); |
| 99 |
} |
| 100 |
} else { |
| 101 |
if (updonly) { |
| 102 |
conn.db.exec(sql, null, args); |
| 103 |
} else { |
| 104 |
tr = conn.db.get_table(sql, maxrows, args); |
| 105 |
} |
| 106 |
} |
| 107 |
updcnt = (int) conn.db.changes(); |
| 108 |
} catch (SQLite.Exception e) { |
| 109 |
if (conn.db.is3() && |
| 110 |
conn.db.last_error() == SQLite.Constants.SQLITE_BUSY && |
| 111 |
conn.busy3(conn.db, ++busy)) { |
| 112 |
try { |
| 113 |
if (starttrans && conn.intrans) { |
| 114 |
conn.db.exec("ROLLBACK", null); |
| 115 |
conn.intrans = false; |
| 116 |
} |
| 117 |
} catch (SQLite.Exception ee) { |
| 118 |
} |
| 119 |
try { |
| 120 |
int ms = 20 + busy * 10; |
| 121 |
if (ms > 1000) { |
| 122 |
ms = 1000; |
| 123 |
} |
| 124 |
synchronized (this) { |
| 125 |
this.wait(ms); |
| 126 |
} |
| 127 |
} catch (java.lang.Exception eee) { |
| 128 |
} |
| 129 |
continue; |
| 130 |
} |
| 131 |
throw new SQLException(e.toString()); |
| 132 |
} |
| 133 |
break; |
| 134 |
} |
| 135 |
if (!updonly && tr == null) { |
| 136 |
throw new SQLException("no result set produced"); |
| 137 |
} |
| 138 |
if (!updonly && tr != null) { |
| 139 |
rs = new JDBCResultSet(new TableResultX(tr), this); |
| 140 |
} |
| 141 |
return rs; |
| 142 |
} |
| 143 |
|
| 144 |
public ResultSet executeQuery(String sql) throws SQLException { |
| 145 |
return executeQuery(sql, null, false); |
| 146 |
} |
| 147 |
|
| 148 |
public boolean execute(String sql) throws SQLException { |
| 149 |
return executeQuery(sql) != null; |
| 150 |
} |
| 151 |
|
| 152 |
public void cancel() throws SQLException { |
| 153 |
if (conn == null || conn.db == null) { |
| 154 |
throw new SQLException("stale connection"); |
| 155 |
} |
| 156 |
conn.db.interrupt(); |
| 157 |
} |
| 158 |
|
| 159 |
public void clearWarnings() throws SQLException { |
| 160 |
} |
| 161 |
|
| 162 |
public Connection getConnection() throws SQLException { |
| 163 |
return conn; |
| 164 |
} |
| 165 |
|
| 166 |
public void addBatch(String sql) throws SQLException { |
| 167 |
if (batch == null) { |
| 168 |
batch = new ArrayList(1); |
| 169 |
} |
| 170 |
batch.add(sql); |
| 171 |
} |
| 172 |
|
| 173 |
public int[] executeBatch() throws SQLException { |
| 174 |
if (batch == null) { |
| 175 |
return new int[0]; |
| 176 |
} |
| 177 |
int[] ret = new int[batch.size()]; |
| 178 |
for (int i = 0; i < ret.length; i++) { |
| 179 |
ret[i] = EXECUTE_FAILED; |
| 180 |
} |
| 181 |
int errs = 0; |
| 182 |
for (int i = 0; i < ret.length; i++) { |
| 183 |
try { |
| 184 |
execute((String) batch.get(i)); |
| 185 |
ret[i] = updcnt; |
| 186 |
} catch (SQLException e) { |
| 187 |
++errs; |
| 188 |
} |
| 189 |
} |
| 190 |
if (errs > 0) { |
| 191 |
throw new BatchUpdateException("batch failed", ret); |
| 192 |
} |
| 193 |
return ret; |
| 194 |
} |
| 195 |
|
| 196 |
public void clearBatch() throws SQLException { |
| 197 |
if (batch != null) { |
| 198 |
batch.clear(); |
| 199 |
batch = null; |
| 200 |
} |
| 201 |
} |
| 202 |
|
| 203 |
public void close() throws SQLException { |
| 204 |
clearBatch(); |
| 205 |
conn = null; |
| 206 |
} |
| 207 |
|
| 208 |
public int executeUpdate(String sql) throws SQLException { |
| 209 |
executeQuery(sql, null, true); |
| 210 |
return updcnt; |
| 211 |
} |
| 212 |
|
| 213 |
public int getMaxFieldSize() throws SQLException { |
| 214 |
return 0; |
| 215 |
} |
| 216 |
|
| 217 |
public boolean getMoreResults() throws SQLException { |
| 218 |
if (rs != null) { |
| 219 |
rs.close(); |
| 220 |
rs = null; |
| 221 |
} |
| 222 |
return false; |
| 223 |
} |
| 224 |
|
| 225 |
public int getUpdateCount() throws SQLException { |
| 226 |
return updcnt; |
| 227 |
} |
| 228 |
|
| 229 |
public SQLWarning getWarnings() throws SQLException { |
| 230 |
return null; |
| 231 |
} |
| 232 |
|
| 233 |
public void setCursorName(String name) throws SQLException { |
| 234 |
throw new SQLException("not supported"); |
| 235 |
} |
| 236 |
|
| 237 |
public void setEscapeProcessing(boolean enable) throws SQLException { |
| 238 |
throw new SQLException("not supported"); |
| 239 |
} |
| 240 |
|
| 241 |
public void setMaxFieldSize(int max) throws SQLException { |
| 242 |
throw new SQLException("not supported"); |
| 243 |
} |
| 244 |
|
| 245 |
public boolean getMoreResults(int x) throws SQLException { |
| 246 |
throw new SQLException("not supported"); |
| 247 |
} |
| 248 |
|
| 249 |
public ResultSet getGeneratedKeys() throws SQLException { |
| 250 |
throw new SQLException("not supported"); |
| 251 |
} |
| 252 |
|
| 253 |
public int executeUpdate(String sql, int autokeys) |
| 254 |
throws SQLException { |
| 255 |
if (autokeys != Statement.NO_GENERATED_KEYS) { |
| 256 |
throw new SQLException("not supported"); |
| 257 |
} |
| 258 |
return executeUpdate(sql); |
| 259 |
} |
| 260 |
|
| 261 |
public int executeUpdate(String sql, int colIndexes[]) |
| 262 |
throws SQLException { |
| 263 |
throw new SQLException("not supported"); |
| 264 |
} |
| 265 |
|
| 266 |
public int executeUpdate(String sql, String colIndexes[]) |
| 267 |
throws SQLException { |
| 268 |
throw new SQLException("not supported"); |
| 269 |
} |
| 270 |
|
| 271 |
public boolean execute(String sql, int autokeys) |
| 272 |
throws SQLException { |
| 273 |
if (autokeys != Statement.NO_GENERATED_KEYS) { |
| 274 |
throw new SQLException("not supported"); |
| 275 |
} |
| 276 |
return execute(sql); |
| 277 |
} |
| 278 |
|
| 279 |
public boolean execute(String sql, int colIndexes[]) |
| 280 |
throws SQLException { |
| 281 |
throw new SQLException("not supported"); |
| 282 |
} |
| 283 |
|
| 284 |
public boolean execute(String sql, String colIndexes[]) |
| 285 |
throws SQLException { |
| 286 |
throw new SQLException("not supported"); |
| 287 |
} |
| 288 |
|
| 289 |
public int getResultSetHoldability() throws SQLException { |
| 290 |
return ResultSet.HOLD_CURSORS_OVER_COMMIT; |
| 291 |
} |
| 292 |
|
| 293 |
} |