• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
open(String filename, int mode)28     public void open(String filename, int mode) throws SQLite.Exception {
29     synchronized(this) {
30         _open(filename, mode);
31     }
32     }
33 
_open(String filename, int mode)34     private native void _open(String filename, int mode)
35     throws SQLite.Exception;
36 
37     /**
38      * Open SQLite auxiliary database file for temporary
39      * tables.
40      *
41      * @param filename the name of the auxiliary file or null
42      */
43 
open_aux_file(String filename)44     public void open_aux_file(String filename) throws SQLite.Exception {
45     synchronized(this) {
46         _open_aux_file(filename);
47     }
48     }
49 
_open_aux_file(String filename)50     private native void _open_aux_file(String filename)
51     throws SQLite.Exception;
52 
53     /**
54      * Destructor for object.
55      */
56 
finalize()57     protected void finalize() {
58     synchronized(this) {
59         _finalize();
60     }
61     }
62 
_finalize()63     private native void _finalize();
64 
65     /**
66      * Close the underlying SQLite database file.
67      */
68 
close()69     public void close()    throws SQLite.Exception {
70     synchronized(this) {
71         _close();
72     }
73     }
74 
_close()75     private native void _close()
76     throws SQLite.Exception;
77 
78     /**
79      * Execute an SQL statement and invoke callback methods
80      * for each row of the result set.<P>
81      *
82      * It the method fails, an SQLite.Exception is thrown and
83      * an error code is set, which later can be retrieved by
84      * the last_error() method.
85      *
86      * @param sql the SQL statement to be executed
87      * @param cb the object implementing the callback methods
88      */
89 
exec(String sql, SQLite.Callback cb)90     public void exec(String sql, SQLite.Callback cb) throws SQLite.Exception {
91     synchronized(this) {
92         _exec(sql, cb);
93     }
94     }
95 
_exec(String sql, SQLite.Callback cb)96     private native void _exec(String sql, SQLite.Callback cb)
97     throws SQLite.Exception;
98 
99     /**
100      * Execute an SQL statement and invoke callback methods
101      * for each row of the result set. Each '%q' or %Q in the
102      * statement string is substituted by its corresponding
103      * element in the argument vector.
104      * <BR><BR>
105      * Example:<BR>
106      * <PRE>
107      *   String args[] = new String[1];
108      *   args[0] = "tab%";
109      *   db.exec("select * from sqlite_master where type like '%q'",
110      *           null, args);
111      * </PRE>
112      *
113      * It the method fails, an SQLite.Exception is thrown and
114      * an error code is set, which later can be retrieved by
115      * the last_error() method.
116      *
117      * @param sql the SQL statement to be executed
118      * @param cb the object implementing the callback methods
119      * @param args arguments for the SQL statement, '%q' substitution
120      */
121 
exec(String sql, SQLite.Callback cb, String args[])122     public void exec(String sql, SQLite.Callback cb,
123              String args[]) throws SQLite.Exception {
124     synchronized(this) {
125         _exec(sql, cb, args);
126     }
127     }
128 
_exec(String sql, SQLite.Callback cb, String args[])129     private native void _exec(String sql, SQLite.Callback cb, String args[])
130     throws SQLite.Exception;
131 
132     /**
133      * Return the row identifier of the last inserted
134      * row.
135      */
136 
last_insert_rowid()137     public long last_insert_rowid() {
138     synchronized(this) {
139         return _last_insert_rowid();
140     }
141     }
142 
_last_insert_rowid()143     private native long _last_insert_rowid();
144 
145     /**
146      * Abort the current SQLite operation.
147      */
148 
interrupt()149     public void interrupt() {
150     synchronized(this) {
151         _interrupt();
152     }
153     }
154 
_interrupt()155     private native void _interrupt();
156 
157     /**
158      * Return the number of changed rows for the last statement.
159      */
160 
changes()161     public long changes() {
162     synchronized(this) {
163         return _changes();
164     }
165     }
166 
_changes()167     private native long _changes();
168 
169     /**
170      * Establish a busy callback method which gets called when
171      * an SQLite table is locked.
172      *
173      * @param bh the object implementing the busy callback method
174      */
175 
busy_handler(SQLite.BusyHandler bh)176     public void busy_handler(SQLite.BusyHandler bh) {
177     synchronized(this) {
178         _busy_handler(bh);
179     }
180     }
181 
_busy_handler(SQLite.BusyHandler bh)182     private native void _busy_handler(SQLite.BusyHandler bh);
183 
184     /**
185      * Set the timeout for waiting for an SQLite table to become
186      * unlocked.
187      *
188      * @param ms number of millisecond to wait
189      */
190 
busy_timeout(int ms)191     public void busy_timeout(int ms) {
192     synchronized(this) {
193         _busy_timeout(ms);
194     }
195     }
196 
_busy_timeout(int ms)197     private native void _busy_timeout(int ms);
198 
199     /**
200      * Convenience method to retrieve an entire result
201      * set into memory.
202      *
203      * @param sql the SQL statement to be executed
204      * @return result set
205      */
206 
get_table(String sql)207     public TableResult get_table(String sql) throws SQLite.Exception {
208     TableResult ret = new TableResult();
209     if (!is3()) {
210         exec(sql, ret);
211     } else {
212         synchronized(this) {
213         /* only one statement !!! */
214         Vm vm = compile(sql);
215         set_last_error(vm.error_code);
216         while (vm.step(ret)) {
217             set_last_error(vm.error_code);
218         }
219         vm.finalize();
220         }
221     }
222     return ret;
223     }
224 
225     /**
226      * Convenience method to retrieve an entire result
227      * set into memory.
228      *
229      * @param sql the SQL statement to be executed
230      * @param args arguments for the SQL statement, '%q' substitution
231      * @return result set
232      */
233 
get_table(String sql, String args[])234     public TableResult get_table(String sql, String args[])
235     throws SQLite.Exception {
236     TableResult ret = new TableResult();
237     if (!is3()) {
238         exec(sql, ret, args);
239     } else {
240         synchronized(this) {
241         /* only one statement !!! */
242         Vm vm = compile(sql, args);
243         set_last_error(vm.error_code);
244         while (vm.step(ret)) {
245             set_last_error(vm.error_code);
246         }
247         vm.finalize();
248         }
249     }
250     return ret;
251     }
252 
253     /**
254      * Convenience method to retrieve an entire result
255      * set into memory.
256      *
257      * @param sql the SQL statement to be executed
258      * @param args arguments for the SQL statement, '%q' substitution
259      * @param tbl TableResult to receive result set
260      * @return result set
261      */
262 
get_table(String sql, String args[], TableResult tbl)263     public void get_table(String sql, String args[], TableResult tbl)
264     throws SQLite.Exception {
265     tbl.clear();
266     if (!is3()) {
267         exec(sql, tbl, args);
268     } else {
269         synchronized(this) {
270         /* only one statement !!! */
271         Vm vm = compile(sql, args);
272         while (vm.step(tbl)) {
273         }
274         vm.finalize();
275         }
276     }
277     }
278 
279     /**
280      * See if an SQL statement is complete.
281      * Returns true if the input string comprises
282      * one or more complete SQL statements.
283      *
284      * @param sql the SQL statement to be checked
285      */
286 
complete(String sql)287     public synchronized static boolean complete(String sql) {
288     return _complete(sql);
289     }
290 
_complete(String sql)291     private native static boolean _complete(String sql);
292 
293     /**
294      * Return SQLite version number as string.
295      * Don't rely on this when both SQLite 2 and 3 are compiled
296      * into the native part. Use the class method in this case.
297      */
298 
version()299     public native static String version();
300 
301     /**
302      * Return SQLite version number as string.
303      * If the database is not open, <tt>unknown</tt> is returned.
304      */
305 
dbversion()306     public native String dbversion();
307 
308     /**
309      * Create regular function.
310      *
311      * @param name the name of the new function
312      * @param nargs number of arguments to function
313      * @param f interface of function
314      */
315 
create_function(String name, int nargs, Function f)316     public void create_function(String name, int nargs, Function f) {
317     synchronized(this) {
318         _create_function(name, nargs, f);
319     }
320     }
321 
_create_function(String name, int nargs, Function f)322     private native void _create_function(String name, int nargs, Function f);
323 
324     /**
325      * Create aggregate function.
326      *
327      * @param name the name of the new function
328      * @param nargs number of arguments to function
329      * @param f interface of function
330      */
331 
create_aggregate(String name, int nargs, Function f)332     public void create_aggregate(String name, int nargs, Function f) {
333     synchronized(this) {
334         _create_aggregate(name, nargs, f);
335     }
336     }
337 
_create_aggregate(String name, int nargs, Function f)338     private native void _create_aggregate(String name, int nargs, Function f);
339 
340     /**
341      * Set function return type. Only available in SQLite 2.6.0 and
342      * above, otherwise a no-op.
343      *
344      * @param name the name of the function whose return type is to be set
345      * @param type return type code, e.g. SQLite.Constants.SQLITE_NUMERIC
346      */
347 
function_type(String name, int type)348     public void function_type(String name, int type) {
349     synchronized(this) {
350         _function_type(name, type);
351     }
352     }
353 
_function_type(String name, int type)354     private native void _function_type(String name, int type);
355 
356     /**
357      * Return the code of the last error occured in
358      * any of the exec() methods. The value is valid
359      * after an Exception has been reported by one of
360      * these methods. See the <A HREF="Constants.html">Constants</A>
361      * class for possible values.
362      *
363      * @return SQLite error code
364      */
365 
last_error()366     public int last_error() {
367     return error_code;
368     }
369 
370     /**
371      * Internal: set error code.
372      * @param error_code new error code
373      */
374 
set_last_error(int error_code)375     protected void set_last_error(int error_code) {
376     this.error_code = error_code;
377     }
378 
379     /**
380      * Return last error message of SQLite3 engine.
381      *
382      * @return error string or null
383      */
384 
error_message()385     public String error_message() {
386     synchronized(this) {
387         return _errmsg();
388     }
389     }
390 
_errmsg()391     private native String _errmsg();
392 
393     /**
394      * Return error string given SQLite error code (SQLite2).
395      *
396      * @param error_code the error code
397      * @return error string
398      */
399 
error_string(int error_code)400     public static native String error_string(int error_code);
401 
402     /**
403      * Set character encoding.
404      * @param enc name of encoding
405      */
406 
set_encoding(String enc)407     public void set_encoding(String enc) throws SQLite.Exception {
408     synchronized(this) {
409         _set_encoding(enc);
410     }
411     }
412 
_set_encoding(String enc)413     private native void _set_encoding(String enc)
414     throws SQLite.Exception;
415 
416     /**
417      * Set authorizer function. Only available in SQLite 2.7.6 and
418      * above, otherwise a no-op.
419      *
420      * @param auth the authorizer function
421      */
422 
set_authorizer(Authorizer auth)423     public void set_authorizer(Authorizer auth) {
424     synchronized(this) {
425         _set_authorizer(auth);
426     }
427     }
428 
_set_authorizer(Authorizer auth)429     private native void _set_authorizer(Authorizer auth);
430 
431     /**
432      * Set trace function. Only available in SQLite 2.7.6 and above,
433      * otherwise a no-op.
434      *
435      * @param tr the trace function
436      */
437 
trace(Trace tr)438     public void trace(Trace tr) {
439     synchronized(this) {
440         _trace(tr);
441     }
442     }
443 
_trace(Trace tr)444     private native void _trace(Trace tr);
445 
446     /**
447      * Compile and return SQLite VM for SQL statement. Only available
448      * in SQLite 2.8.0 and above, otherwise a no-op.
449      *
450      * @param sql SQL statement to be compiled
451      * @return a Vm object
452      */
453 
compile(String sql)454     public Vm compile(String sql) throws SQLite.Exception {
455     synchronized(this) {
456         Vm vm = new Vm();
457         vm_compile(sql, vm);
458         return vm;
459     }
460     }
461 
462     /**
463      * Compile and return SQLite VM for SQL statement. Only available
464      * in SQLite 3.0 and above, otherwise a no-op.
465      *
466      * @param sql SQL statement to be compiled
467      * @param args arguments for the SQL statement, '%q' substitution
468      * @return a Vm object
469      */
470 
compile(String sql, String args[])471     public Vm compile(String sql, String args[]) throws SQLite.Exception {
472     synchronized(this) {
473         Vm vm = new Vm();
474         vm_compile_args(sql, vm, args);
475         return vm;
476     }
477     }
478 
479     /**
480      * Prepare and return SQLite3 statement for SQL. Only available
481      * in SQLite 3.0 and above, otherwise a no-op.
482      *
483      * @param sql SQL statement to be prepared
484      * @return a Stmt object
485      */
486 
prepare(String sql)487     public Stmt prepare(String sql) throws SQLite.Exception {
488     synchronized(this) {
489         Stmt stmt = new Stmt();
490         stmt_prepare(sql, stmt);
491         return stmt;
492     }
493     }
494 
495     /**
496      * Open an SQLite3 blob. Only available in SQLite 3.4.0 and above.
497      * @param db database name
498      * @param table table name
499      * @param column column name
500      * @param row row identifier
501      * @param rw if true, open for read-write, else read-only
502      * @return a Blob object
503      */
504 
open_blob(String db, String table, String column, long row, boolean rw)505     public Blob open_blob(String db, String table, String column,
506               long row, boolean rw) throws SQLite.Exception {
507     synchronized(this) {
508         Blob blob = new Blob();
509         _open_blob(db, table, column, row, rw, blob);
510         return blob;
511     }
512     }
513 
514     /**
515      * Check type of open database.
516      * @return true if SQLite3 database
517      */
518 
is3()519     public native boolean is3();
520 
521     /**
522      * Internal compile method.
523      * @param sql SQL statement
524      * @param vm Vm object
525      */
526 
vm_compile(String sql, Vm vm)527     private native void vm_compile(String sql, Vm vm)
528     throws SQLite.Exception;
529 
530     /**
531      * Internal compile method, SQLite 3.0 only.
532      * @param sql SQL statement
533      * @param args arguments for the SQL statement, '%q' substitution
534      * @param vm Vm object
535      */
536 
vm_compile_args(String sql, Vm vm, String args[])537     private native void vm_compile_args(String sql, Vm vm, String args[])
538     throws SQLite.Exception;
539 
540     /**
541      * Internal SQLite3 prepare method.
542      * @param sql SQL statement
543      * @param stmt Stmt object
544      */
545 
stmt_prepare(String sql, Stmt stmt)546     private native void stmt_prepare(String sql, Stmt stmt)
547     throws SQLite.Exception;
548 
549     /**
550      * Internal SQLite open blob method.
551      * @param db database name
552      * @param table table name
553      * @param column column name
554      * @param row row identifier
555      * @param rw if true, open for read-write, else read-only
556      * @param blob Blob object
557      */
558 
_open_blob(String db, String table, String column, long row, boolean rw, Blob blob)559     private native void _open_blob(String db, String table, String column,
560                    long row, boolean rw, Blob blob)
561     throws SQLite.Exception;
562 
563     /**
564      * Establish a progress callback method which gets called after
565      * N SQLite VM opcodes.
566      *
567      * @param n number of SQLite VM opcodes until callback is invoked
568      * @param p the object implementing the progress callback method
569      */
570 
progress_handler(int n, SQLite.ProgressHandler p)571     public void progress_handler(int n, SQLite.ProgressHandler p) {
572     synchronized(this) {
573         _progress_handler(n, p);
574     }
575     }
576 
_progress_handler(int n, SQLite.ProgressHandler p)577     private native void _progress_handler(int n, SQLite.ProgressHandler p);
578 
579     /**
580      * Internal native initializer.
581      */
582 
internal_init()583     private static native void internal_init();
584 
585     /**
586      * Static initializer to load the native part.
587      */
588 
589     static {
590     try {
591 //        String path = System.getProperty("SQLite.library.path");
592 //        if (path == null || path.length() == 0){
593 //        System.loadLibrary("sqlite_jni");
594 //        } else {
595 //        try {
596 //            java.lang.reflect.Method mapLibraryName;
597 //            Class param[] = new Class[1];
598 //            param[0] = String.class;
599 //            mapLibraryName = System.class.getMethod("mapLibraryName",
600 //                                param);
601 //            Object args[] = new Object[1];
602 //            args[0] = "sqlite_jni";
603 //            String mapped = (String) mapLibraryName.invoke(null, args);
604 //            System.load(path + java.io.File.separator + mapped);
605 //        } catch (Throwable t) {
606 //            System.loadLibrary("sqlite_jni");
607 //        }
608 //        }
internal_init()609         internal_init();
610     } catch (Throwable t) {
611         System.err.println("Unable to load sqlite: " + t);
612     }
613     }
614 }
615 
616