| 1 |
package SQLite.JDBC2; |
| 2 |
|
| 3 |
import java.sql.Array; |
| 4 |
import java.sql.Blob; |
| 5 |
import java.sql.CallableStatement; |
| 6 |
import java.sql.Clob; |
| 7 |
import java.sql.DatabaseMetaData; |
| 8 |
import java.sql.PreparedStatement; |
| 9 |
import java.sql.ResultSet; |
| 10 |
import java.sql.SQLException; |
| 11 |
import java.sql.SQLWarning; |
| 12 |
import java.sql.Statement; |
| 13 |
import java.sql.Struct; |
| 14 |
import java.util.Properties; |
| 15 |
|
| 16 |
public class JDBCConnection |
| 17 |
implements java.sql.Connection, SQLite.BusyHandler { |
| 18 |
|
| 19 |
/** |
| 20 |
* Open database. |
| 21 |
*/ |
| 22 |
protected DatabaseX db; |
| 23 |
|
| 24 |
/** |
| 25 |
* Database URL. |
| 26 |
*/ |
| 27 |
protected String url; |
| 28 |
|
| 29 |
/** |
| 30 |
* Character encoding. |
| 31 |
*/ |
| 32 |
protected String enc; |
| 33 |
|
| 34 |
/** |
| 35 |
* SQLite 3 VFS to use. |
| 36 |
*/ |
| 37 |
protected String vfs; |
| 38 |
|
| 39 |
/** |
| 40 |
* Autocommit flag, true means autocommit. |
| 41 |
*/ |
| 42 |
protected boolean autocommit = true; |
| 43 |
|
| 44 |
/** |
| 45 |
* In-transaction flag. |
| 46 |
* Can be true only when autocommit false. |
| 47 |
*/ |
| 48 |
protected boolean intrans = false; |
| 49 |
|
| 50 |
/** |
| 51 |
* Timeout for Database.exec() |
| 52 |
*/ |
| 53 |
protected int timeout = 1000000; |
| 54 |
|
| 55 |
/** |
| 56 |
* Use double/julian date representation. |
| 57 |
*/ |
| 58 |
protected boolean useJulian = false; |
| 59 |
|
| 60 |
/** |
| 61 |
* File name of database. |
| 62 |
*/ |
| 63 |
private String dbfile = null; |
| 64 |
|
| 65 |
/** |
| 66 |
* Reference to meta data or null. |
| 67 |
*/ |
| 68 |
private JDBCDatabaseMetaData meta = null; |
| 69 |
|
| 70 |
/** |
| 71 |
* Base time value for timeout handling. |
| 72 |
*/ |
| 73 |
private long t0; |
| 74 |
|
| 75 |
/** |
| 76 |
* Database in readonly mode. |
| 77 |
*/ |
| 78 |
private boolean readonly = false; |
| 79 |
|
| 80 |
/** |
| 81 |
* Transaction isolation mode. |
| 82 |
*/ |
| 83 |
private int trmode = TRANSACTION_SERIALIZABLE; |
| 84 |
|
| 85 |
private boolean busy0(DatabaseX db, int count) { |
| 86 |
if (count <= 1) { |
| 87 |
t0 = System.currentTimeMillis(); |
| 88 |
} |
| 89 |
if (db != null) { |
| 90 |
long t1 = System.currentTimeMillis(); |
| 91 |
if (t1 - t0 > timeout) { |
| 92 |
return false; |
| 93 |
} |
| 94 |
db.wait(100); |
| 95 |
return true; |
| 96 |
} |
| 97 |
return false; |
| 98 |
} |
| 99 |
|
| 100 |
public boolean busy(String table, int count) { |
| 101 |
return busy0(db, count); |
| 102 |
} |
| 103 |
|
| 104 |
protected boolean busy3(DatabaseX db, int count) { |
| 105 |
if (count <= 1) { |
| 106 |
t0 = System.currentTimeMillis(); |
| 107 |
} |
| 108 |
if (db != null) { |
| 109 |
long t1 = System.currentTimeMillis(); |
| 110 |
if (t1 - t0 > timeout) { |
| 111 |
return false; |
| 112 |
} |
| 113 |
return true; |
| 114 |
} |
| 115 |
return false; |
| 116 |
} |
| 117 |
|
| 118 |
private DatabaseX open(boolean readonly) throws SQLException { |
| 119 |
DatabaseX dbx = null; |
| 120 |
try { |
| 121 |
dbx = new DatabaseX(); |
| 122 |
dbx.open(dbfile, readonly ? SQLite.Constants.SQLITE_OPEN_READONLY : |
| 123 |
(SQLite.Constants.SQLITE_OPEN_READWRITE | |
| 124 |
SQLite.Constants.SQLITE_OPEN_CREATE), vfs); |
| 125 |
dbx.set_encoding(enc); |
| 126 |
} catch (SQLite.Exception e) { |
| 127 |
throw new SQLException(e.toString()); |
| 128 |
} |
| 129 |
int loop = 0; |
| 130 |
while (true) { |
| 131 |
try { |
| 132 |
dbx.exec("PRAGMA short_column_names = off;", null); |
| 133 |
dbx.exec("PRAGMA full_column_names = on;", null); |
| 134 |
dbx.exec("PRAGMA empty_result_callbacks = on;", null); |
| 135 |
if (SQLite.Database.version().compareTo("2.6.0") >= 0) { |
| 136 |
dbx.exec("PRAGMA show_datatypes = on;", null); |
| 137 |
} |
| 138 |
} catch (SQLite.Exception e) { |
| 139 |
if (dbx.last_error() != SQLite.Constants.SQLITE_BUSY || |
| 140 |
!busy0(dbx, ++loop)) { |
| 141 |
try { |
| 142 |
dbx.close(); |
| 143 |
} catch (SQLite.Exception ee) { |
| 144 |
} |
| 145 |
throw new SQLException(e.toString()); |
| 146 |
} |
| 147 |
continue; |
| 148 |
} |
| 149 |
break; |
| 150 |
} |
| 151 |
return dbx; |
| 152 |
} |
| 153 |
|
| 154 |
public JDBCConnection(String url, String enc, String pwd, String drep, |
| 155 |
String vfs) |
| 156 |
throws SQLException { |
| 157 |
if (url.startsWith("sqlite:/")) { |
| 158 |
dbfile = url.substring(8); |
| 159 |
} else if (url.startsWith("jdbc:sqlite:/")) { |
| 160 |
dbfile = url.substring(13); |
| 161 |
} else { |
| 162 |
throw new SQLException("unsupported url"); |
| 163 |
} |
| 164 |
this.url = url; |
| 165 |
this.enc = enc; |
| 166 |
this.vfs = vfs; |
| 167 |
try { |
| 168 |
db = open(readonly); |
| 169 |
try { |
| 170 |
if (pwd != null && pwd.length() > 0) { |
| 171 |
db.key(pwd); |
| 172 |
} |
| 173 |
} catch (SQLite.Exception se) { |
| 174 |
throw new |
| 175 |
SQLException("error while setting key: " + se.toString()); |
| 176 |
} |
| 177 |
db.busy_handler(this); |
| 178 |
} catch (SQLException e) { |
| 179 |
if (db != null) { |
| 180 |
try { |
| 181 |
db.close(); |
| 182 |
} catch (SQLite.Exception ee) { |
| 183 |
} |
| 184 |
} |
| 185 |
throw e; |
| 186 |
} |
| 187 |
useJulian = drep != null && |
| 188 |
(drep.startsWith("j") || drep.startsWith("J")); |
| 189 |
} |
| 190 |
|
| 191 |
/* non-standard */ |
| 192 |
public SQLite.Database getSQLiteDatabase() { |
| 193 |
return db; |
| 194 |
} |
| 195 |
|
| 196 |
public Statement createStatement() { |
| 197 |
JDBCStatement s = new JDBCStatement(this); |
| 198 |
return s; |
| 199 |
} |
| 200 |
|
| 201 |
public Statement createStatement(int resultSetType, |
| 202 |
int resultSetConcurrency) |
| 203 |
throws SQLException { |
| 204 |
if (resultSetType != ResultSet.TYPE_FORWARD_ONLY && |
| 205 |
resultSetType != ResultSet.TYPE_SCROLL_INSENSITIVE && |
| 206 |
resultSetType != ResultSet.TYPE_SCROLL_SENSITIVE) { |
| 207 |
throw new SQLException("unsupported result set type"); |
| 208 |
} |
| 209 |
if (resultSetConcurrency != ResultSet.CONCUR_READ_ONLY && |
| 210 |
resultSetConcurrency != ResultSet.CONCUR_UPDATABLE) { |
| 211 |
throw new SQLException("unsupported result set concurrency"); |
| 212 |
} |
| 213 |
JDBCStatement s = new JDBCStatement(this); |
| 214 |
return s; |
| 215 |
} |
| 216 |
|
| 217 |
public DatabaseMetaData getMetaData() throws SQLException { |
| 218 |
if (meta == null) { |
| 219 |
meta = new JDBCDatabaseMetaData(this); |
| 220 |
} |
| 221 |
return meta; |
| 222 |
} |
| 223 |
|
| 224 |
public void close() throws SQLException { |
| 225 |
try { |
| 226 |
rollback(); |
| 227 |
} catch (SQLException e) { |
| 228 |
/* ignored */ |
| 229 |
} |
| 230 |
intrans = false; |
| 231 |
if (db != null) { |
| 232 |
try { |
| 233 |
db.close(); |
| 234 |
db = null; |
| 235 |
} catch (SQLite.Exception e) { |
| 236 |
throw new SQLException(e.toString()); |
| 237 |
} |
| 238 |
} |
| 239 |
} |
| 240 |
|
| 241 |
public boolean isClosed() throws SQLException { |
| 242 |
return db == null; |
| 243 |
} |
| 244 |
|
| 245 |
public boolean isReadOnly() throws SQLException { |
| 246 |
return readonly; |
| 247 |
} |
| 248 |
|
| 249 |
public void clearWarnings() throws SQLException { |
| 250 |
} |
| 251 |
|
| 252 |
public void commit() throws SQLException { |
| 253 |
if (db == null) { |
| 254 |
throw new SQLException("stale connection"); |
| 255 |
} |
| 256 |
if (!intrans) { |
| 257 |
return; |
| 258 |
} |
| 259 |
try { |
| 260 |
db.exec("COMMIT", null); |
| 261 |
intrans = false; |
| 262 |
} catch (SQLite.Exception e) { |
| 263 |
throw new SQLException(e.toString()); |
| 264 |
} |
| 265 |
} |
| 266 |
|
| 267 |
public boolean getAutoCommit() throws SQLException { |
| 268 |
return autocommit; |
| 269 |
} |
| 270 |
|
| 271 |
public String getCatalog() throws SQLException { |
| 272 |
return null; |
| 273 |
} |
| 274 |
|
| 275 |
public int getTransactionIsolation() throws SQLException { |
| 276 |
return trmode; |
| 277 |
} |
| 278 |
|
| 279 |
public SQLWarning getWarnings() throws SQLException { |
| 280 |
return null; |
| 281 |
} |
| 282 |
|
| 283 |
public String nativeSQL(String sql) throws SQLException { |
| 284 |
throw new SQLException("not supported"); |
| 285 |
} |
| 286 |
|
| 287 |
public CallableStatement prepareCall(String sql) throws SQLException { |
| 288 |
throw new SQLException("not supported"); |
| 289 |
} |
| 290 |
|
| 291 |
public CallableStatement prepareCall(String sql, int x, int y) |
| 292 |
throws SQLException { |
| 293 |
throw new SQLException("not supported"); |
| 294 |
} |
| 295 |
|
| 296 |
public PreparedStatement prepareStatement(String sql) throws SQLException { |
| 297 |
JDBCPreparedStatement s = new JDBCPreparedStatement(this, sql); |
| 298 |
return s; |
| 299 |
} |
| 300 |
|
| 301 |
public PreparedStatement prepareStatement(String sql, int resultSetType, |
| 302 |
int resultSetConcurrency) |
| 303 |
throws SQLException { |
| 304 |
if (resultSetType != ResultSet.TYPE_FORWARD_ONLY && |
| 305 |
resultSetType != ResultSet.TYPE_SCROLL_INSENSITIVE && |
| 306 |
resultSetType != ResultSet.TYPE_SCROLL_SENSITIVE) { |
| 307 |
throw new SQLException("unsupported result set type"); |
| 308 |
} |
| 309 |
if (resultSetConcurrency != ResultSet.CONCUR_READ_ONLY && |
| 310 |
resultSetConcurrency != ResultSet.CONCUR_UPDATABLE) { |
| 311 |
throw new SQLException("unsupported result set concurrency"); |
| 312 |
} |
| 313 |
JDBCPreparedStatement s = new JDBCPreparedStatement(this, sql); |
| 314 |
return s; |
| 315 |
} |
| 316 |
|
| 317 |
public void rollback() throws SQLException { |
| 318 |
if (db == null) { |
| 319 |
throw new SQLException("stale connection"); |
| 320 |
} |
| 321 |
if (!intrans) { |
| 322 |
return; |
| 323 |
} |
| 324 |
try { |
| 325 |
db.exec("ROLLBACK", null); |
| 326 |
intrans = false; |
| 327 |
} catch (SQLite.Exception e) { |
| 328 |
throw new SQLException(e.toString()); |
| 329 |
} |
| 330 |
} |
| 331 |
|
| 332 |
public void setAutoCommit(boolean ac) throws SQLException { |
| 333 |
if (ac && intrans && db != null) { |
| 334 |
try { |
| 335 |
db.exec("ROLLBACK", null); |
| 336 |
} catch (SQLite.Exception e) { |
| 337 |
throw new SQLException(e.toString()); |
| 338 |
} finally { |
| 339 |
intrans = false; |
| 340 |
} |
| 341 |
} |
| 342 |
autocommit = ac; |
| 343 |
} |
| 344 |
|
| 345 |
public void setCatalog(String catalog) throws SQLException { |
| 346 |
} |
| 347 |
|
| 348 |
public void setReadOnly(boolean ro) throws SQLException { |
| 349 |
if (intrans) { |
| 350 |
throw new SQLException("incomplete transaction"); |
| 351 |
} |
| 352 |
if (ro != readonly) { |
| 353 |
DatabaseX dbx = null; |
| 354 |
try { |
| 355 |
dbx = open(ro); |
| 356 |
db.close(); |
| 357 |
db = dbx; |
| 358 |
dbx = null; |
| 359 |
readonly = ro; |
| 360 |
} catch (SQLException e) { |
| 361 |
throw e; |
| 362 |
} catch (SQLite.Exception ee) { |
| 363 |
if (dbx != null) { |
| 364 |
try { |
| 365 |
dbx.close(); |
| 366 |
} catch (SQLite.Exception eee) { |
| 367 |
} |
| 368 |
} |
| 369 |
throw new SQLException(ee.toString()); |
| 370 |
} |
| 371 |
} |
| 372 |
} |
| 373 |
|
| 374 |
public void setTransactionIsolation(int level) throws SQLException { |
| 375 |
if (db.is3() && SQLite.JDBC.sharedCache) { |
| 376 |
String flag = null; |
| 377 |
if (level == TRANSACTION_READ_UNCOMMITTED && |
| 378 |
trmode != TRANSACTION_READ_UNCOMMITTED) { |
| 379 |
flag = "on"; |
| 380 |
} else if (level == TRANSACTION_SERIALIZABLE && |
| 381 |
trmode != TRANSACTION_SERIALIZABLE) { |
| 382 |
flag = "off"; |
| 383 |
} |
| 384 |
if (flag != null) { |
| 385 |
try { |
| 386 |
db.exec("PRAGMA read_uncommitted = " + flag + ";", null); |
| 387 |
trmode = level; |
| 388 |
} catch (java.lang.Exception e) { |
| 389 |
} |
| 390 |
} |
| 391 |
} |
| 392 |
if (level != trmode) { |
| 393 |
throw new SQLException("not supported"); |
| 394 |
} |
| 395 |
} |
| 396 |
|
| 397 |
public java.util.Map getTypeMap() throws SQLException { |
| 398 |
throw new SQLException("not supported"); |
| 399 |
} |
| 400 |
|
| 401 |
public void setTypeMap(java.util.Map map) throws SQLException { |
| 402 |
throw new SQLException("not supported"); |
| 403 |
} |
| 404 |
|
| 405 |
} |
| 406 |
|
| 407 |
class DatabaseX extends SQLite.Database { |
| 408 |
|
| 409 |
static Object lock = new Object(); |
| 410 |
|
| 411 |
public DatabaseX() { |
| 412 |
super(); |
| 413 |
} |
| 414 |
|
| 415 |
void wait(int ms) { |
| 416 |
try { |
| 417 |
synchronized (lock) { |
| 418 |
lock.wait(ms); |
| 419 |
} |
| 420 |
} catch (java.lang.Exception e) { |
| 421 |
} |
| 422 |
} |
| 423 |
|
| 424 |
public void exec(String sql, SQLite.Callback cb) |
| 425 |
throws SQLite.Exception { |
| 426 |
super.exec(sql, cb); |
| 427 |
synchronized (lock) { |
| 428 |
lock.notifyAll(); |
| 429 |
} |
| 430 |
} |
| 431 |
|
| 432 |
public void exec(String sql, SQLite.Callback cb, String args[]) |
| 433 |
throws SQLite.Exception { |
| 434 |
super.exec(sql, cb, args); |
| 435 |
synchronized (lock) { |
| 436 |
lock.notifyAll(); |
| 437 |
} |
| 438 |
} |
| 439 |
|
| 440 |
public SQLite.TableResult get_table(String sql, String args[]) |
| 441 |
throws SQLite.Exception { |
| 442 |
SQLite.TableResult ret = super.get_table(sql, args); |
| 443 |
synchronized (lock) { |
| 444 |
lock.notifyAll(); |
| 445 |
} |
| 446 |
return ret; |
| 447 |
} |
| 448 |
|
| 449 |
public void get_table(String sql, String args[], SQLite.TableResult tbl) |
| 450 |
throws SQLite.Exception { |
| 451 |
super.get_table(sql, args, tbl); |
| 452 |
synchronized (lock) { |
| 453 |
lock.notifyAll(); |
| 454 |
} |
| 455 |
} |
| 456 |
|
| 457 |
} |