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 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473
|
package SQLite;
/**
* Main class wrapping an SQLite database.
*/
public class Database {
/**
* Internal handle for the native SQLite API.
*/
protected long handle = 0;
/**
* Internal last error code for exec() methods.
*/
protected int error_code = 0;
/**
* Open an SQLite database file.
*
* @param filename the name of the database file
* @param mode open mode, currently ignored
*/
public synchronized void open(String filename, int mode)
throws SQLite.Exception {
_open(filename, mode);
}
private native void _open(String filename, int mode)
throws SQLite.Exception;
/**
* Open SQLite auxiliary database file for temporary
* tables.
*
* @param filename the name of the auxiliary file or null
*/
public synchronized void open_aux_file(String filename)
throws SQLite.Exception {
_open_aux_file(filename);
}
private native void _open_aux_file(String filename)
throws SQLite.Exception;
/**
* Destructor for object.
*/
protected synchronized void finalize() {
_finalize();
}
private native void _finalize();
/**
* Close the underlying SQLite database file.
*/
public synchronized void close()
throws SQLite.Exception {
_close();
}
private native void _close()
throws SQLite.Exception;
/**
* Execute an SQL statement and invoke callback methods
* for each row of the result set.<P>
*
* It the method fails, an SQLite.Exception is thrown and
* an error code is set, which later can be retrieved by
* the last_error() method.
*
* @param sql the SQL statement to be executed
* @param cb the object implementing the callback methods
*/
public synchronized void exec(String sql, SQLite.Callback cb)
throws SQLite.Exception {
_exec(sql, cb);
}
private native void _exec(String sql, SQLite.Callback cb)
throws SQLite.Exception;
/**
* Execute an SQL statement and invoke callback methods
* for each row of the result set. Each '%q' or %Q in the
* statement string is substituted by its corresponding
* element in the argument vector.
* <BR><BR>
* Example:<BR>
* <PRE>
* String args[] = new String[1];
* args[0] = "tab%";
* db.exec("select * from sqlite_master where type like '%q'",
* null, args);
* </PRE>
*
* It the method fails, an SQLite.Exception is thrown and
* an error code is set, which later can be retrieved by
* the last_error() method.
*
* @param sql the SQL statement to be executed
* @param cb the object implementing the callback methods
* @param args arguments for the SQL statement, '%q' substitution
*/
public synchronized void exec(String sql, SQLite.Callback cb,
String args[]) throws SQLite.Exception {
_exec(sql, cb, args);
}
private native void _exec(String sql, SQLite.Callback cb, String args[])
throws SQLite.Exception;
/**
* Return the row identifier of the last inserted
* row.
*/
public synchronized long last_insert_rowid() {
return _last_insert_rowid();
}
private native long _last_insert_rowid();
/**
* Abort the current SQLite operation.
*/
public synchronized void interrupt() {
_interrupt();
}
private native void _interrupt();
/**
* Return the number of changed rows for the last statement.
*/
public synchronized long changes() {
return _changes();
}
private native long _changes();
/**
* Establish a busy callback method which gets called when
* an SQLite table is locked.
*
* @param bh the object implementing the busy callback method
*/
public synchronized void busy_handler(SQLite.BusyHandler bh) {
_busy_handler(bh);
}
private native void _busy_handler(SQLite.BusyHandler bh);
/**
* Set the timeout for waiting for an SQLite table to become
* unlocked.
*
* @param ms number of millisecond to wait
*/
public synchronized void busy_timeout(int ms) {
_busy_timeout(ms);
}
private native void _busy_timeout(int ms);
/**
* Convenience method to retrieve an entire result
* set into memory.
*
* @param sql the SQL statement to be executed
* @return result set
*/
public TableResult get_table(String sql) throws SQLite.Exception {
TableResult ret = new TableResult();
if (!is3()) {
exec(sql, ret);
} else {
/* only one statement !!! */
Vm vm = compile(sql);
set_last_error(vm.error_code);
while (vm.step(ret)) {
set_last_error(vm.error_code);
}
vm.finalize();
}
return ret;
}
/**
* Convenience method to retrieve an entire result
* set into memory.
*
* @param sql the SQL statement to be executed
* @param args arguments for the SQL statement, '%q' substitution
* @return result set
*/
public TableResult get_table(String sql, String args[])
throws SQLite.Exception {
TableResult ret = new TableResult();
if (!is3()) {
exec(sql, ret, args);
} else {
/* only one statement !!! */
Vm vm = compile(sql, args);
set_last_error(vm.error_code);
while (vm.step(ret)) {
set_last_error(vm.error_code);
}
vm.finalize();
}
return ret;
}
/**
* Convenience method to retrieve an entire result
* set into memory.
*
* @param sql the SQL statement to be executed
* @param args arguments for the SQL statement, '%q' substitution
* @param tbl TableResult to receive result set
* @return result set
*/
public void get_table(String sql, String args[], TableResult tbl)
throws SQLite.Exception {
tbl.clear();
if (!is3()) {
exec(sql, tbl, args);
} else {
/* only one statement !!! */
Vm vm = compile(sql, args);
while (vm.step(tbl)) {
}
vm.finalize();
}
}
/**
* See if an SQL statement is complete.
* Returns true if the input string comprises
* one or more complete SQL statements.
*
* @param sql the SQL statement to be checked
*/
public synchronized static boolean complete(String sql) {
return _complete(sql);
}
private native static boolean _complete(String sql);
/**
* Return SQLite version number as string.
* Don't rely on this when both SQLite 2 and 3 are compiled
* into the native part. Use the class method in this case.
*/
public native static String version();
/**
* Return SQLite version number as string.
* If the database is not open, <tt>unknown</tt> is returned.
*/
public native String dbversion();
/**
* Create regular function.
*
* @param name the name of the new function
* @param nargs number of arguments to function
* @param f interface of function
*/
public native void create_function(String name, int nargs, Function f);
/**
* Create aggregate function.
*
* @param name the name of the new function
* @param nargs number of arguments to function
* @param f interface of function
*/
public native void create_aggregate(String name, int nargs, Function f);
/**
* Set function return type. Only available in SQLite 2.6.0 and
* above, otherwise a no-op.
*
* @param name the name of the function whose return type is to be set
* @param type return type code, e.g. SQLite.Constants.SQLITE_NUMERIC
*/
public native void function_type(String name, int type);
/**
* Return the code of the last error occured in
* any of the exec() methods. The value is valid
* after an Exception has been reported by one of
* these methods. See the <A HREF="Constants.html">Constants</A>
* class for possible values.
*
* @return SQLite error code
*/
public int last_error() {
return error_code;
}
/**
* Internal: set error code.
* @param error_code new error code
*/
protected void set_last_error(int error_code) {
this.error_code = error_code;
}
/**
* Return error string given SQLite error code.
*
* @param error_code the error code
* @return error string
*/
public static native String error_string(int error_code);
/**
* Set character encoding.
* @param enc name of encoding
*/
public synchronized void set_encoding(String enc) throws SQLite.Exception {
_set_encoding(enc);
}
private native void _set_encoding(String enc)
throws SQLite.Exception;
/**
* Set authorizer function. Only available in SQLite 2.7.6 and
* above, otherwise a no-op.
*
* @param auth the authorizer function
*/
public synchronized void set_authorizer(Authorizer auth) {
_set_authorizer(auth);
}
private native void _set_authorizer(Authorizer auth);
/**
* Set trace function. Only available in SQLite 2.7.6 and above,
* otherwise a no-op.
*
* @param tr the trace function
*/
public synchronized void trace(Trace tr) {
_trace(tr);
}
private native void _trace(Trace tr);
/**
* Compile and return SQLite VM for SQL statement. Only available
* in SQLite 2.8.0 and above, otherwise a no-op.
*
* @param sql SQL statement to be compiled
* @return a Vm object
*/
public synchronized Vm compile(String sql) throws SQLite.Exception {
Vm vm = new Vm();
vm_compile(sql, vm);
return vm;
}
/**
* Compile and return SQLite VM for SQL statement. Only available
* in SQLite 3.0 and above, otherwise a no-op.
*
* @param sql SQL statement to be compiled
* @param args arguments for the SQL statement, '%q' substitution
* @return a Vm object
*/
public synchronized Vm compile(String sql, String args[])
throws SQLite.Exception {
Vm vm = new Vm();
vm_compile_args(sql, vm, args);
return vm;
}
/**
* Check type of open database.
* @return true if SQLite3 database
*/
public native boolean is3();
/**
* Internal compile method.
* @param sql SQL statement
* @param vm Vm object
*/
private native void vm_compile(String sql, Vm vm)
throws SQLite.Exception;
/**
* Internal compile method, SQLite 3.0 only.
* @param sql SQL statement
* @param args arguments for the SQL statement, '%q' substitution
* @param vm Vm object
*/
private native void vm_compile_args(String sql, Vm vm, String args[])
throws SQLite.Exception;
/**
* Establish a progress callback method which gets called after
* N SQLite VM opcodes.
*
* @param n number of SQLite VM opcodes until callback is invoked
* @param p the object implementing the progress callback method
*/
public synchronized void progress_handler(int n,
SQLite.ProgressHandler p) {
_progress_handler(n, p);
}
private native void _progress_handler(int n, SQLite.ProgressHandler p);
/**
* Internal native initializer.
*/
private static native void internal_init();
/**
* Static initializer to load the native part.
*/
static {
try {
System.loadLibrary("sqlite_jni");
internal_init();
} catch (Throwable t) {
System.err.println("Unable to load sqlite: " + t);
}
}
}
|