| 1 |
package SQLite; |
| 2 |
|
| 3 |
/** |
| 4 |
* Main class wrapping an SQLite database. |
| 5 |
*/ |
| 6 |
|
| 7 |
public class Database { |
| 8 |
|
| 9 |
/** |
| 10 |
* Internal handle for the native SQLite API. |
| 11 |
*/ |
| 12 |
|
| 13 |
protected long handle = 0; |
| 14 |
|
| 15 |
/** |
| 16 |
* Internal last error code for exec() methods. |
| 17 |
*/ |
| 18 |
|
| 19 |
protected int error_code = 0; |
| 20 |
|
| 21 |
/** |
| 22 |
* Open an SQLite database file. |
| 23 |
* |
| 24 |
* @param filename the name of the database file |
| 25 |
* @param mode open mode, currently ignored |
| 26 |
*/ |
| 27 |
|
| 28 |
public synchronized void open(String filename, int mode) |
| 29 |
throws SQLite.Exception { |
| 30 |
_open(filename, mode); |
| 31 |
} |
| 32 |
|
| 33 |
private native void _open(String filename, int mode) |
| 34 |
throws SQLite.Exception; |
| 35 |
|
| 36 |
/** |
| 37 |
* Open SQLite auxiliary database file for temporary |
| 38 |
* tables. |
| 39 |
* |
| 40 |
* @param filename the name of the auxiliary file or null |
| 41 |
*/ |
| 42 |
|
| 43 |
public synchronized void open_aux_file(String filename) |
| 44 |
throws SQLite.Exception { |
| 45 |
_open_aux_file(filename); |
| 46 |
} |
| 47 |
|
| 48 |
private native void _open_aux_file(String filename) |
| 49 |
throws SQLite.Exception; |
| 50 |
|
| 51 |
/** |
| 52 |
* Destructor for object. |
| 53 |
*/ |
| 54 |
|
| 55 |
protected synchronized void finalize() { |
| 56 |
_finalize(); |
| 57 |
} |
| 58 |
|
| 59 |
private native void _finalize(); |
| 60 |
|
| 61 |
/** |
| 62 |
* Close the underlying SQLite database file. |
| 63 |
*/ |
| 64 |
|
| 65 |
public synchronized void close() |
| 66 |
throws SQLite.Exception { |
| 67 |
_close(); |
| 68 |
} |
| 69 |
|
| 70 |
private native void _close() |
| 71 |
throws SQLite.Exception; |
| 72 |
|
| 73 |
/** |
| 74 |
* Execute an SQL statement and invoke callback methods |
| 75 |
* for each row of the result set.<P> |
| 76 |
* |
| 77 |
* It the method fails, an SQLite.Exception is thrown and |
| 78 |
* an error code is set, which later can be retrieved by |
| 79 |
* the last_error() method. |
| 80 |
* |
| 81 |
* @param sql the SQL statement to be executed |
| 82 |
* @param cb the object implementing the callback methods |
| 83 |
*/ |
| 84 |
|
| 85 |
public synchronized void exec(String sql, SQLite.Callback cb) |
| 86 |
throws SQLite.Exception { |
| 87 |
_exec(sql, cb); |
| 88 |
} |
| 89 |
|
| 90 |
private native void _exec(String sql, SQLite.Callback cb) |
| 91 |
throws SQLite.Exception; |
| 92 |
|
| 93 |
/** |
| 94 |
* Execute an SQL statement and invoke callback methods |
| 95 |
* for each row of the result set. Each '%q' or %Q in the |
| 96 |
* statement string is substituted by its corresponding |
| 97 |
* element in the argument vector. |
| 98 |
* <BR><BR> |
| 99 |
* Example:<BR> |
| 100 |
* <PRE> |
| 101 |
* String args[] = new String[1]; |
| 102 |
* args[0] = "tab%"; |
| 103 |
* db.exec("select * from sqlite_master where type like '%q'", |
| 104 |
* null, args); |
| 105 |
* </PRE> |
| 106 |
* |
| 107 |
* It the method fails, an SQLite.Exception is thrown and |
| 108 |
* an error code is set, which later can be retrieved by |
| 109 |
* the last_error() method. |
| 110 |
* |
| 111 |
* @param sql the SQL statement to be executed |
| 112 |
* @param cb the object implementing the callback methods |
| 113 |
* @param args arguments for the SQL statement, '%q' substitution |
| 114 |
*/ |
| 115 |
|
| 116 |
public synchronized void exec(String sql, SQLite.Callback cb, |
| 117 |
String args[]) throws SQLite.Exception { |
| 118 |
_exec(sql, cb, args); |
| 119 |
} |
| 120 |
|
| 121 |
private native void _exec(String sql, SQLite.Callback cb, String args[]) |
| 122 |
throws SQLite.Exception; |
| 123 |
|
| 124 |
/** |
| 125 |
* Return the row identifier of the last inserted |
| 126 |
* row. |
| 127 |
*/ |
| 128 |
|
| 129 |
public synchronized long last_insert_rowid() { |
| 130 |
return _last_insert_rowid(); |
| 131 |
} |
| 132 |
|
| 133 |
private native long _last_insert_rowid(); |
| 134 |
|
| 135 |
/** |
| 136 |
* Abort the current SQLite operation. |
| 137 |
*/ |
| 138 |
|
| 139 |
public synchronized void interrupt() { |
| 140 |
_interrupt(); |
| 141 |
} |
| 142 |
|
| 143 |
private native void _interrupt(); |
| 144 |
|
| 145 |
/** |
| 146 |
* Return the number of changed rows for the last statement. |
| 147 |
*/ |
| 148 |
|
| 149 |
public synchronized long changes() { |
| 150 |
return _changes(); |
| 151 |
} |
| 152 |
|
| 153 |
private native long _changes(); |
| 154 |
|
| 155 |
/** |
| 156 |
* Establish a busy callback method which gets called when |
| 157 |
* an SQLite table is locked. |
| 158 |
* |
| 159 |
* @param bh the object implementing the busy callback method |
| 160 |
*/ |
| 161 |
|
| 162 |
public synchronized void busy_handler(SQLite.BusyHandler bh) { |
| 163 |
_busy_handler(bh); |
| 164 |
} |
| 165 |
|
| 166 |
private native void _busy_handler(SQLite.BusyHandler bh); |
| 167 |
|
| 168 |
/** |
| 169 |
* Set the timeout for waiting for an SQLite table to become |
| 170 |
* unlocked. |
| 171 |
* |
| 172 |
* @param ms number of millisecond to wait |
| 173 |
*/ |
| 174 |
|
| 175 |
public synchronized void busy_timeout(int ms) { |
| 176 |
_busy_timeout(ms); |
| 177 |
} |
| 178 |
|
| 179 |
private native void _busy_timeout(int ms); |
| 180 |
|
| 181 |
/** |
| 182 |
* Convenience method to retrieve an entire result |
| 183 |
* set into memory. |
| 184 |
* |
| 185 |
* @param sql the SQL statement to be executed |
| 186 |
* @return result set |
| 187 |
*/ |
| 188 |
|
| 189 |
public TableResult get_table(String sql) throws SQLite.Exception { |
| 190 |
TableResult ret = new TableResult(); |
| 191 |
if (!is3()) { |
| 192 |
exec(sql, ret); |
| 193 |
} else { |
| 194 |
/* only one statement !!! */ |
| 195 |
Vm vm = compile(sql); |
| 196 |
set_last_error(vm.error_code); |
| 197 |
while (vm.step(ret)) { |
| 198 |
set_last_error(vm.error_code); |
| 199 |
} |
| 200 |
vm.finalize(); |
| 201 |
} |
| 202 |
return ret; |
| 203 |
} |
| 204 |
|
| 205 |
/** |
| 206 |
* Convenience method to retrieve an entire result |
| 207 |
* set into memory. |
| 208 |
* |
| 209 |
* @param sql the SQL statement to be executed |
| 210 |
* @param args arguments for the SQL statement, '%q' substitution |
| 211 |
* @return result set |
| 212 |
*/ |
| 213 |
|
| 214 |
public TableResult get_table(String sql, String args[]) |
| 215 |
throws SQLite.Exception { |
| 216 |
TableResult ret = new TableResult(); |
| 217 |
if (!is3()) { |
| 218 |
exec(sql, ret, args); |
| 219 |
} else { |
| 220 |
/* only one statement !!! */ |
| 221 |
Vm vm = compile(sql, args); |
| 222 |
set_last_error(vm.error_code); |
| 223 |
while (vm.step(ret)) { |
| 224 |
set_last_error(vm.error_code); |
| 225 |
} |
| 226 |
vm.finalize(); |
| 227 |
} |
| 228 |
return ret; |
| 229 |
} |
| 230 |
|
| 231 |
/** |
| 232 |
* Convenience method to retrieve an entire result |
| 233 |
* set into memory. |
| 234 |
* |
| 235 |
* @param sql the SQL statement to be executed |
| 236 |
* @param args arguments for the SQL statement, '%q' substitution |
| 237 |
* @param tbl TableResult to receive result set |
| 238 |
* @return result set |
| 239 |
*/ |
| 240 |
|
| 241 |
public void get_table(String sql, String args[], TableResult tbl) |
| 242 |
throws SQLite.Exception { |
| 243 |
tbl.clear(); |
| 244 |
if (!is3()) { |
| 245 |
exec(sql, tbl, args); |
| 246 |
} else { |
| 247 |
/* only one statement !!! */ |
| 248 |
Vm vm = compile(sql, args); |
| 249 |
while (vm.step(tbl)) { |
| 250 |
} |
| 251 |
vm.finalize(); |
| 252 |
} |
| 253 |
} |
| 254 |
|
| 255 |
/** |
| 256 |
* See if an SQL statement is complete. |
| 257 |
* Returns true if the input string comprises |
| 258 |
* one or more complete SQL statements. |
| 259 |
* |
| 260 |
* @param sql the SQL statement to be checked |
| 261 |
*/ |
| 262 |
|
| 263 |
public synchronized static boolean complete(String sql) { |
| 264 |
return _complete(sql); |
| 265 |
} |
| 266 |
|
| 267 |
private native static boolean _complete(String sql); |
| 268 |
|
| 269 |
/** |
| 270 |
* Return SQLite version number as string. |
| 271 |
* Don't rely on this when both SQLite 2 and 3 are compiled |
| 272 |
* into the native part. Use the class method in this case. |
| 273 |
*/ |
| 274 |
|
| 275 |
public native static String version(); |
| 276 |
|
| 277 |
/** |
| 278 |
* Return SQLite version number as string. |
| 279 |
* If the database is not open, <tt>unknown</tt> is returned. |
| 280 |
*/ |
| 281 |
|
| 282 |
public native String dbversion(); |
| 283 |
|
| 284 |
/** |
| 285 |
* Create regular function. |
| 286 |
* |
| 287 |
* @param name the name of the new function |
| 288 |
* @param nargs number of arguments to function |
| 289 |
* @param f interface of function |
| 290 |
*/ |
| 291 |
|
| 292 |
public native void create_function(String name, int nargs, Function f); |
| 293 |
|
| 294 |
/** |
| 295 |
* Create aggregate function. |
| 296 |
* |
| 297 |
* @param name the name of the new function |
| 298 |
* @param nargs number of arguments to function |
| 299 |
* @param f interface of function |
| 300 |
*/ |
| 301 |
|
| 302 |
public native void create_aggregate(String name, int nargs, Function f); |
| 303 |
|
| 304 |
/** |
| 305 |
* Set function return type. Only available in SQLite 2.6.0 and |
| 306 |
* above, otherwise a no-op. |
| 307 |
* |
| 308 |
* @param name the name of the function whose return type is to be set |
| 309 |
* @param type return type code, e.g. SQLite.Constants.SQLITE_NUMERIC |
| 310 |
*/ |
| 311 |
|
| 312 |
public native void function_type(String name, int type); |
| 313 |
|
| 314 |
/** |
| 315 |
* Return the code of the last error occured in |
| 316 |
* any of the exec() methods. The value is valid |
| 317 |
* after an Exception has been reported by one of |
| 318 |
* these methods. See the <A HREF="Constants.html">Constants</A> |
| 319 |
* class for possible values. |
| 320 |
* |
| 321 |
* @return SQLite error code |
| 322 |
*/ |
| 323 |
|
| 324 |
public int last_error() { |
| 325 |
return error_code; |
| 326 |
} |
| 327 |
|
| 328 |
/** |
| 329 |
* Internal: set error code. |
| 330 |
* @param error_code new error code |
| 331 |
*/ |
| 332 |
|
| 333 |
protected void set_last_error(int error_code) { |
| 334 |
this.error_code = error_code; |
| 335 |
} |
| 336 |
|
| 337 |
/** |
| 338 |
* Return error string given SQLite error code. |
| 339 |
* |
| 340 |
* @param error_code the error code |
| 341 |
* @return error string |
| 342 |
*/ |
| 343 |
|
| 344 |
public static native String error_string(int error_code); |
| 345 |
|
| 346 |
/** |
| 347 |
* Set character encoding. |
| 348 |
* @param enc name of encoding |
| 349 |
*/ |
| 350 |
|
| 351 |
public synchronized void set_encoding(String enc) throws SQLite.Exception { |
| 352 |
_set_encoding(enc); |
| 353 |
} |
| 354 |
|
| 355 |
private native void _set_encoding(String enc) |
| 356 |
throws SQLite.Exception; |
| 357 |
|
| 358 |
/** |
| 359 |
* Set authorizer function. Only available in SQLite 2.7.6 and |
| 360 |
* above, otherwise a no-op. |
| 361 |
* |
| 362 |
* @param auth the authorizer function |
| 363 |
*/ |
| 364 |
|
| 365 |
public synchronized void set_authorizer(Authorizer auth) { |
| 366 |
_set_authorizer(auth); |
| 367 |
} |
| 368 |
|
| 369 |
private native void _set_authorizer(Authorizer auth); |
| 370 |
|
| 371 |
/** |
| 372 |
* Set trace function. Only available in SQLite 2.7.6 and above, |
| 373 |
* otherwise a no-op. |
| 374 |
* |
| 375 |
* @param tr the trace function |
| 376 |
*/ |
| 377 |
|
| 378 |
public synchronized void trace(Trace tr) { |
| 379 |
_trace(tr); |
| 380 |
} |
| 381 |
|
| 382 |
private native void _trace(Trace tr); |
| 383 |
|
| 384 |
/** |
| 385 |
* Compile and return SQLite VM for SQL statement. Only available |
| 386 |
* in SQLite 2.8.0 and above, otherwise a no-op. |
| 387 |
* |
| 388 |
* @param sql SQL statement to be compiled |
| 389 |
* @return a Vm object |
| 390 |
*/ |
| 391 |
|
| 392 |
public synchronized Vm compile(String sql) throws SQLite.Exception { |
| 393 |
Vm vm = new Vm(); |
| 394 |
vm_compile(sql, vm); |
| 395 |
return vm; |
| 396 |
} |
| 397 |
|
| 398 |
/** |
| 399 |
* Compile and return SQLite VM for SQL statement. Only available |
| 400 |
* in SQLite 3.0 and above, otherwise a no-op. |
| 401 |
* |
| 402 |
* @param sql SQL statement to be compiled |
| 403 |
* @param args arguments for the SQL statement, '%q' substitution |
| 404 |
* @return a Vm object |
| 405 |
*/ |
| 406 |
|
| 407 |
public synchronized Vm compile(String sql, String args[]) |
| 408 |
throws SQLite.Exception { |
| 409 |
Vm vm = new Vm(); |
| 410 |
vm_compile_args(sql, vm, args); |
| 411 |
return vm; |
| 412 |
} |
| 413 |
|
| 414 |
/** |
| 415 |
* Check type of open database. |
| 416 |
* @return true if SQLite3 database |
| 417 |
*/ |
| 418 |
|
| 419 |
public native boolean is3(); |
| 420 |
|
| 421 |
/** |
| 422 |
* Internal compile method. |
| 423 |
* @param sql SQL statement |
| 424 |
* @param vm Vm object |
| 425 |
*/ |
| 426 |
|
| 427 |
private native void vm_compile(String sql, Vm vm) |
| 428 |
throws SQLite.Exception; |
| 429 |
|
| 430 |
/** |
| 431 |
* Internal compile method, SQLite 3.0 only. |
| 432 |
* @param sql SQL statement |
| 433 |
* @param args arguments for the SQL statement, '%q' substitution |
| 434 |
* @param vm Vm object |
| 435 |
*/ |
| 436 |
|
| 437 |
private native void vm_compile_args(String sql, Vm vm, String args[]) |
| 438 |
throws SQLite.Exception; |
| 439 |
|
| 440 |
/** |
| 441 |
* Establish a progress callback method which gets called after |
| 442 |
* N SQLite VM opcodes. |
| 443 |
* |
| 444 |
* @param n number of SQLite VM opcodes until callback is invoked |
| 445 |
* @param p the object implementing the progress callback method |
| 446 |
*/ |
| 447 |
|
| 448 |
public synchronized void progress_handler(int n, |
| 449 |
SQLite.ProgressHandler p) { |
| 450 |
_progress_handler(n, p); |
| 451 |
} |
| 452 |
|
| 453 |
private native void _progress_handler(int n, SQLite.ProgressHandler p); |
| 454 |
|
| 455 |
/** |
| 456 |
* Internal native initializer. |
| 457 |
*/ |
| 458 |
|
| 459 |
private static native void internal_init(); |
| 460 |
|
| 461 |
/** |
| 462 |
* Static initializer to load the native part. |
| 463 |
*/ |
| 464 |
|
| 465 |
static { |
| 466 |
try { |
| 467 |
System.loadLibrary("sqlite_jni"); |
| 468 |
internal_init(); |
| 469 |
} catch (Throwable t) { |
| 470 |
System.err.println("Unable to load sqlite: " + t); |
| 471 |
} |
| 472 |
} |
| 473 |
} |