1417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughespackage SQLite; 2417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes 3417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes/** 4417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes * Main class wrapping an SQLite database. 5417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes */ 6417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes 7417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughespublic class Database { 8417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes 9417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes /** 10417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes * Internal handle for the native SQLite API. 11417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes */ 12417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes 13417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes protected long handle = 0; 14417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes 15417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes /** 16417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes * Internal last error code for exec() methods. 17417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes */ 18417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes 19417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes protected int error_code = 0; 20417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes 21417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes /** 22417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes * Open an SQLite database file. 23417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes * 24417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes * @param filename the name of the database file 257a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes * @param mode open mode (e.g. SQLITE_OPEN_READONLY) 26417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes */ 27417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes 28417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes public void open(String filename, int mode) throws SQLite.Exception { 297a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes if ((mode & 0200) != 0) { 307a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes mode = SQLite.Constants.SQLITE_OPEN_READWRITE | 317a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes SQLite.Constants.SQLITE_OPEN_CREATE; 327a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes } else if ((mode & 0400) != 0) { 337a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes mode = SQLite.Constants.SQLITE_OPEN_READONLY; 347a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes } 357a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes synchronized(this) { 367a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes try { 377a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes _open4(filename, mode, null, false); 387a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes } catch (SQLite.Exception se) { 397a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes throw se; 407a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes } catch (java.lang.OutOfMemoryError me) { 417a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes throw me; 427a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes } catch (Throwable t) { 437a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes _open(filename, mode); 447a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes } 457a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes } 46417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes } 477a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes 487a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes /** 497a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes * Open an SQLite database file. 507a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes * 517a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes * @param filename the name of the database file 527a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes * @param mode open mode (e.g. SQLITE_OPEN_READONLY) 537a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes * @param vfs VFS name (for SQLite >= 3.5) 547a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes */ 557a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes 567a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes public void open(String filename, int mode, String vfs) 577a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes throws SQLite.Exception { 587a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes if ((mode & 0200) != 0) { 597a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes mode = SQLite.Constants.SQLITE_OPEN_READWRITE | 607a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes SQLite.Constants.SQLITE_OPEN_CREATE; 617a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes } else if ((mode & 0400) != 0) { 627a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes mode = SQLite.Constants.SQLITE_OPEN_READONLY; 637a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes } 647a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes synchronized(this) { 657a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes try { 667a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes _open4(filename, mode, vfs, false); 677a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes } catch (SQLite.Exception se) { 687a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes throw se; 697a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes } catch (java.lang.OutOfMemoryError me) { 707a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes throw me; 717a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes } catch (Throwable t) { 727a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes _open(filename, mode); 737a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes } 747a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes } 75417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes } 76417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes 777a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes /** 787a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes * Open an SQLite database file. 797a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes * 807a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes * @param filename the name of the database file 817a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes * @param mode open mode (e.g. SQLITE_OPEN_READONLY) 827a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes * @param vfs VFS name (for SQLite >= 3.5) 837a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes * @param ver2 flag to force version on create (false = SQLite3, true = SQLite2) 847a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes */ 857a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes 867a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes public void open(String filename, int mode, String vfs, boolean ver2) 877a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes throws SQLite.Exception { 887a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes if ((mode & 0200) != 0) { 897a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes mode = SQLite.Constants.SQLITE_OPEN_READWRITE | 907a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes SQLite.Constants.SQLITE_OPEN_CREATE; 917a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes } else if ((mode & 0400) != 0) { 927a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes mode = SQLite.Constants.SQLITE_OPEN_READONLY; 937a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes } 947a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes synchronized(this) { 957a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes try { 967a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes _open4(filename, mode, vfs, ver2); 977a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes } catch (SQLite.Exception se) { 987a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes throw se; 997a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes } catch (java.lang.OutOfMemoryError me) { 1007a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes throw me; 1017a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes } catch (Throwable t) { 1027a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes _open(filename, mode); 1037a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes } 1047a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes } 1057a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes } 1067a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes 1077a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes /* 1087a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes * For backward compatibility to older sqlite.jar, sqlite_jni 1097a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes */ 1107a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes 111417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes private native void _open(String filename, int mode) 1127a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes throws SQLite.Exception; 1137a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes 1147a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes /* 1157a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes * Newer full interface 1167a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes */ 1177a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes 1187a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes private native void _open4(String filename, int mode, String vfs, 1197a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes boolean ver2) 1207a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes throws SQLite.Exception; 121417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes 122417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes /** 123417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes * Open SQLite auxiliary database file for temporary 124417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes * tables. 125417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes * 126417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes * @param filename the name of the auxiliary file or null 127417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes */ 128417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes 129417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes public void open_aux_file(String filename) throws SQLite.Exception { 1307a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes synchronized(this) { 1317a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes _open_aux_file(filename); 1327a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes } 133417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes } 134417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes 135417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes private native void _open_aux_file(String filename) 1367a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes throws SQLite.Exception; 137417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes 138417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes /** 139417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes * Destructor for object. 140417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes */ 141417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes 142417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes protected void finalize() { 1437a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes synchronized(this) { 1447a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes _finalize(); 1457a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes } 146417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes } 147417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes 148417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes private native void _finalize(); 149417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes 150417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes /** 151417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes * Close the underlying SQLite database file. 152417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes */ 153417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes 1547a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes public void close() throws SQLite.Exception { 1557a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes synchronized(this) { 1567a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes _close(); 1577a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes } 158417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes } 159417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes 160417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes private native void _close() 1617a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes throws SQLite.Exception; 162417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes 163417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes /** 164417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes * Execute an SQL statement and invoke callback methods 165417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes * for each row of the result set.<P> 166417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes * 167417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes * It the method fails, an SQLite.Exception is thrown and 168417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes * an error code is set, which later can be retrieved by 169417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes * the last_error() method. 170417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes * 171417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes * @param sql the SQL statement to be executed 172417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes * @param cb the object implementing the callback methods 173417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes */ 174417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes 175417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes public void exec(String sql, SQLite.Callback cb) throws SQLite.Exception { 1767a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes synchronized(this) { 1777a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes _exec(sql, cb); 1787a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes } 179417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes } 180417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes 181417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes private native void _exec(String sql, SQLite.Callback cb) 1827a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes throws SQLite.Exception; 183417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes 184417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes /** 185417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes * Execute an SQL statement and invoke callback methods 186417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes * for each row of the result set. Each '%q' or %Q in the 187417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes * statement string is substituted by its corresponding 188417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes * element in the argument vector. 189417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes * <BR><BR> 190417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes * Example:<BR> 191417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes * <PRE> 192417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes * String args[] = new String[1]; 193417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes * args[0] = "tab%"; 194417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes * db.exec("select * from sqlite_master where type like '%q'", 195417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes * null, args); 196417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes * </PRE> 197417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes * 198417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes * It the method fails, an SQLite.Exception is thrown and 199417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes * an error code is set, which later can be retrieved by 200417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes * the last_error() method. 201417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes * 202417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes * @param sql the SQL statement to be executed 203417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes * @param cb the object implementing the callback methods 204417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes * @param args arguments for the SQL statement, '%q' substitution 205417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes */ 206417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes 207417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes public void exec(String sql, SQLite.Callback cb, 2087a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes String args[]) throws SQLite.Exception { 2097a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes synchronized(this) { 2107a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes _exec(sql, cb, args); 2117a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes } 212417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes } 213417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes 214417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes private native void _exec(String sql, SQLite.Callback cb, String args[]) 2157a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes throws SQLite.Exception; 216417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes 217417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes /** 218417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes * Return the row identifier of the last inserted 219417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes * row. 220417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes */ 221417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes 222417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes public long last_insert_rowid() { 2237a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes synchronized(this) { 2247a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes return _last_insert_rowid(); 2257a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes } 226417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes } 227417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes 228417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes private native long _last_insert_rowid(); 229417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes 230417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes /** 231417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes * Abort the current SQLite operation. 232417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes */ 233417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes 234417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes public void interrupt() { 2357a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes synchronized(this) { 2367a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes _interrupt(); 2377a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes } 238417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes } 239417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes 240417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes private native void _interrupt(); 241417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes 242417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes /** 243417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes * Return the number of changed rows for the last statement. 244417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes */ 245417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes 246417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes public long changes() { 2477a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes synchronized(this) { 2487a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes return _changes(); 2497a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes } 250417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes } 251417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes 252417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes private native long _changes(); 253417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes 254417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes /** 255417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes * Establish a busy callback method which gets called when 256417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes * an SQLite table is locked. 257417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes * 258417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes * @param bh the object implementing the busy callback method 259417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes */ 260417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes 261417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes public void busy_handler(SQLite.BusyHandler bh) { 2627a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes synchronized(this) { 2637a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes _busy_handler(bh); 2647a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes } 265417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes } 266417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes 267417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes private native void _busy_handler(SQLite.BusyHandler bh); 268417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes 269417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes /** 270417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes * Set the timeout for waiting for an SQLite table to become 271417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes * unlocked. 272417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes * 273417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes * @param ms number of millisecond to wait 274417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes */ 275417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes 276417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes public void busy_timeout(int ms) { 2777a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes synchronized(this) { 2787a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes _busy_timeout(ms); 2797a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes } 280417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes } 281417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes 282417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes private native void _busy_timeout(int ms); 283417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes 284417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes /** 285417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes * Convenience method to retrieve an entire result 286417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes * set into memory. 287417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes * 288417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes * @param sql the SQL statement to be executed 2897a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes * @param maxrows the max. number of rows to retrieve 2907a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes * @return result set 2917a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes */ 2927a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes 2937a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes public TableResult get_table(String sql, int maxrows) 2947a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes throws SQLite.Exception { 2957a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes TableResult ret = new TableResult(maxrows); 2967a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes if (!is3()) { 2977a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes try { 2987a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes exec(sql, ret); 2997a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes } catch (SQLite.Exception e) { 3007a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes if (maxrows <= 0 || !ret.atmaxrows) { 3017a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes throw e; 3027a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes } 3037a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes } 3047a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes } else { 3057a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes synchronized(this) { 3067a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes /* only one statement !!! */ 3077a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes Vm vm = compile(sql); 3087a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes set_last_error(vm.error_code); 3097a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes if (ret.maxrows > 0) { 3107a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes while (ret.nrows < ret.maxrows && vm.step(ret)) { 3117a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes set_last_error(vm.error_code); 3127a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes } 3137a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes } else { 3147a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes while (vm.step(ret)) { 3157a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes set_last_error(vm.error_code); 3167a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes } 3177a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes } 3187a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes vm.finalize(); 3197a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes } 3207a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes } 3217a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes return ret; 3227a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes } 3237a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes 3247a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes /** 3257a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes * Convenience method to retrieve an entire result 3267a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes * set into memory. 3277a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes * 3287a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes * @param sql the SQL statement to be executed 329417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes * @return result set 330417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes */ 331417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes 332417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes public TableResult get_table(String sql) throws SQLite.Exception { 3337a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes return get_table(sql, 0); 334417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes } 3357a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes 3367a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes /** 3377a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes * Convenience method to retrieve an entire result 3387a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes * set into memory. 3397a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes * 3407a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes * @param sql the SQL statement to be executed 3417a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes * @param maxrows the max. number of rows to retrieve 3427a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes * @param args arguments for the SQL statement, '%q' substitution 3437a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes * @return result set 3447a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes */ 3457a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes 3467a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes public TableResult get_table(String sql, int maxrows, String args[]) 3477a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes throws SQLite.Exception { 3487a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes TableResult ret = new TableResult(maxrows); 3497a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes if (!is3()) { 3507a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes try { 3517a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes exec(sql, ret, args); 3527a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes } catch (SQLite.Exception e) { 3537a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes if (maxrows <= 0 || !ret.atmaxrows) { 3547a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes throw e; 3557a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes } 3567a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes } 3577a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes } else { 3587a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes synchronized(this) { 3597a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes /* only one statement !!! */ 3607a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes Vm vm = compile(sql, args); 3617a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes set_last_error(vm.error_code); 3627a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes if (ret.maxrows > 0) { 3637a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes while (ret.nrows < ret.maxrows && vm.step(ret)) { 3647a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes set_last_error(vm.error_code); 3657a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes } 3667a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes } else { 3677a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes while (vm.step(ret)) { 3687a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes set_last_error(vm.error_code); 3697a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes } 3707a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes } 3717a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes vm.finalize(); 3727a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes } 3737a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes } 3747a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes return ret; 375417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes } 376417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes 377417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes /** 378417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes * Convenience method to retrieve an entire result 379417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes * set into memory. 380417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes * 381417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes * @param sql the SQL statement to be executed 382417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes * @param args arguments for the SQL statement, '%q' substitution 383417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes * @return result set 384417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes */ 385417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes 386417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes public TableResult get_table(String sql, String args[]) 3877a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes throws SQLite.Exception { 3887a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes return get_table(sql, 0, args); 389417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes } 390417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes 391417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes /** 392417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes * Convenience method to retrieve an entire result 393417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes * set into memory. 394417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes * 395417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes * @param sql the SQL statement to be executed 396417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes * @param args arguments for the SQL statement, '%q' substitution 397417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes * @param tbl TableResult to receive result set 398417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes */ 399417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes 400417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes public void get_table(String sql, String args[], TableResult tbl) 4017a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes throws SQLite.Exception { 4027a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes tbl.clear(); 4037a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes if (!is3()) { 4047a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes try { 4057a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes exec(sql, tbl, args); 4067a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes } catch (SQLite.Exception e) { 4077a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes if (tbl.maxrows <= 0 || !tbl.atmaxrows) { 4087a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes throw e; 4097a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes } 4107a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes } 4117a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes } else { 4127a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes synchronized(this) { 4137a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes /* only one statement !!! */ 4147a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes Vm vm = compile(sql, args); 4157a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes if (tbl.maxrows > 0) { 4167a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes while (tbl.nrows < tbl.maxrows && vm.step(tbl)) { 4177a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes set_last_error(vm.error_code); 4187a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes } 4197a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes } else { 4207a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes while (vm.step(tbl)) { 4217a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes set_last_error(vm.error_code); 4227a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes } 4237a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes } 4247a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes vm.finalize(); 4257a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes } 4267a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes } 427417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes } 428417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes 429417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes /** 430417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes * See if an SQL statement is complete. 431417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes * Returns true if the input string comprises 432417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes * one or more complete SQL statements. 433417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes * 434417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes * @param sql the SQL statement to be checked 435417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes */ 436417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes 437417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes public synchronized static boolean complete(String sql) { 4387a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes return _complete(sql); 439417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes } 440417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes 441417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes private native static boolean _complete(String sql); 442417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes 443417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes /** 444417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes * Return SQLite version number as string. 445417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes * Don't rely on this when both SQLite 2 and 3 are compiled 446417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes * into the native part. Use the class method in this case. 447417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes */ 448417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes 449417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes public native static String version(); 450417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes 451417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes /** 452417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes * Return SQLite version number as string. 453417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes * If the database is not open, <tt>unknown</tt> is returned. 454417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes */ 455417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes 456417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes public native String dbversion(); 457417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes 458417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes /** 459417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes * Create regular function. 460417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes * 461417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes * @param name the name of the new function 462417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes * @param nargs number of arguments to function 463417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes * @param f interface of function 464417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes */ 465417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes 466417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes public void create_function(String name, int nargs, Function f) { 4677a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes synchronized(this) { 4687a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes _create_function(name, nargs, f); 4697a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes } 470417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes } 471417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes 472417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes private native void _create_function(String name, int nargs, Function f); 473417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes 474417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes /** 475417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes * Create aggregate function. 476417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes * 477417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes * @param name the name of the new function 478417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes * @param nargs number of arguments to function 479417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes * @param f interface of function 480417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes */ 481417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes 482417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes public void create_aggregate(String name, int nargs, Function f) { 4837a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes synchronized(this) { 4847a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes _create_aggregate(name, nargs, f); 4857a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes } 486417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes } 487417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes 488417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes private native void _create_aggregate(String name, int nargs, Function f); 489417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes 490417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes /** 491417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes * Set function return type. Only available in SQLite 2.6.0 and 492417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes * above, otherwise a no-op. 493417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes * 494417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes * @param name the name of the function whose return type is to be set 495417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes * @param type return type code, e.g. SQLite.Constants.SQLITE_NUMERIC 496417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes */ 497417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes 498417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes public void function_type(String name, int type) { 4997a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes synchronized(this) { 5007a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes _function_type(name, type); 5017a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes } 502417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes } 503417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes 504417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes private native void _function_type(String name, int type); 505417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes 506417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes /** 507417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes * Return the code of the last error occured in 508417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes * any of the exec() methods. The value is valid 509417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes * after an Exception has been reported by one of 510417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes * these methods. See the <A HREF="Constants.html">Constants</A> 511417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes * class for possible values. 512417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes * 513417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes * @return SQLite error code 514417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes */ 515417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes 516417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes public int last_error() { 5177a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes return error_code; 518417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes } 519417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes 520417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes /** 521417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes * Internal: set error code. 522417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes * @param error_code new error code 523417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes */ 524417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes 525417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes protected void set_last_error(int error_code) { 5267a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes this.error_code = error_code; 527417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes } 528417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes 529417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes /** 530417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes * Return last error message of SQLite3 engine. 531417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes * 532417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes * @return error string or null 533417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes */ 534417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes 535417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes public String error_message() { 5367a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes synchronized(this) { 5377a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes return _errmsg(); 5387a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes } 539417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes } 540417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes 541417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes private native String _errmsg(); 542417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes 543417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes /** 544417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes * Return error string given SQLite error code (SQLite2). 545417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes * 546417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes * @param error_code the error code 547417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes * @return error string 548417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes */ 549417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes 550417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes public static native String error_string(int error_code); 551417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes 552417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes /** 553417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes * Set character encoding. 554417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes * @param enc name of encoding 555417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes */ 556417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes 557417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes public void set_encoding(String enc) throws SQLite.Exception { 5587a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes synchronized(this) { 5597a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes _set_encoding(enc); 5607a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes } 561417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes } 562417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes 563417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes private native void _set_encoding(String enc) 5647a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes throws SQLite.Exception; 565417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes 566417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes /** 567417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes * Set authorizer function. Only available in SQLite 2.7.6 and 568417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes * above, otherwise a no-op. 569417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes * 570417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes * @param auth the authorizer function 571417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes */ 572417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes 573417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes public void set_authorizer(Authorizer auth) { 5747a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes synchronized(this) { 5757a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes _set_authorizer(auth); 5767a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes } 577417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes } 578417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes 579417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes private native void _set_authorizer(Authorizer auth); 580417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes 581417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes /** 582417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes * Set trace function. Only available in SQLite 2.7.6 and above, 583417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes * otherwise a no-op. 584417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes * 585417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes * @param tr the trace function 586417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes */ 587417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes 588417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes public void trace(Trace tr) { 5897a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes synchronized(this) { 5907a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes _trace(tr); 5917a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes } 592417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes } 593417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes 594417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes private native void _trace(Trace tr); 595417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes 596417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes /** 5975cd52ed8fd9244cbbb9c2553e58b511344f75d8bElliott Hughes * Initiate a database backup, SQLite 3.x only. 5985cd52ed8fd9244cbbb9c2553e58b511344f75d8bElliott Hughes * 5995cd52ed8fd9244cbbb9c2553e58b511344f75d8bElliott Hughes * @param dest destination database 6005cd52ed8fd9244cbbb9c2553e58b511344f75d8bElliott Hughes * @param destName schema of destination database to be backed up 6015cd52ed8fd9244cbbb9c2553e58b511344f75d8bElliott Hughes * @param srcName schema of source database 6025cd52ed8fd9244cbbb9c2553e58b511344f75d8bElliott Hughes * @return Backup object to perform the backup operation 6035cd52ed8fd9244cbbb9c2553e58b511344f75d8bElliott Hughes */ 6045cd52ed8fd9244cbbb9c2553e58b511344f75d8bElliott Hughes 6055cd52ed8fd9244cbbb9c2553e58b511344f75d8bElliott Hughes public Backup backup(Database dest, String destName, String srcName) 6065cd52ed8fd9244cbbb9c2553e58b511344f75d8bElliott Hughes throws SQLite.Exception { 6075cd52ed8fd9244cbbb9c2553e58b511344f75d8bElliott Hughes synchronized(this) { 6085cd52ed8fd9244cbbb9c2553e58b511344f75d8bElliott Hughes Backup b = new Backup(); 6095cd52ed8fd9244cbbb9c2553e58b511344f75d8bElliott Hughes _backup(b, dest, destName, this, srcName); 6105cd52ed8fd9244cbbb9c2553e58b511344f75d8bElliott Hughes return b; 6115cd52ed8fd9244cbbb9c2553e58b511344f75d8bElliott Hughes } 6125cd52ed8fd9244cbbb9c2553e58b511344f75d8bElliott Hughes } 6135cd52ed8fd9244cbbb9c2553e58b511344f75d8bElliott Hughes 6145cd52ed8fd9244cbbb9c2553e58b511344f75d8bElliott Hughes private static native void _backup(Backup b, Database dest, 6155cd52ed8fd9244cbbb9c2553e58b511344f75d8bElliott Hughes String destName, Database src, 6165cd52ed8fd9244cbbb9c2553e58b511344f75d8bElliott Hughes String srcName) 6175cd52ed8fd9244cbbb9c2553e58b511344f75d8bElliott Hughes throws SQLite.Exception; 6185cd52ed8fd9244cbbb9c2553e58b511344f75d8bElliott Hughes 6195cd52ed8fd9244cbbb9c2553e58b511344f75d8bElliott Hughes /** 6205cd52ed8fd9244cbbb9c2553e58b511344f75d8bElliott Hughes * Set profile function. Only available in SQLite 3.6 and above, 6215cd52ed8fd9244cbbb9c2553e58b511344f75d8bElliott Hughes * otherwise a no-op. 6225cd52ed8fd9244cbbb9c2553e58b511344f75d8bElliott Hughes * 6235cd52ed8fd9244cbbb9c2553e58b511344f75d8bElliott Hughes * @param pr the trace function 6245cd52ed8fd9244cbbb9c2553e58b511344f75d8bElliott Hughes */ 6255cd52ed8fd9244cbbb9c2553e58b511344f75d8bElliott Hughes 6265cd52ed8fd9244cbbb9c2553e58b511344f75d8bElliott Hughes public void profile(Profile pr) { 6275cd52ed8fd9244cbbb9c2553e58b511344f75d8bElliott Hughes synchronized(this) { 6285cd52ed8fd9244cbbb9c2553e58b511344f75d8bElliott Hughes _profile(pr); 6295cd52ed8fd9244cbbb9c2553e58b511344f75d8bElliott Hughes } 6305cd52ed8fd9244cbbb9c2553e58b511344f75d8bElliott Hughes } 6315cd52ed8fd9244cbbb9c2553e58b511344f75d8bElliott Hughes 6325cd52ed8fd9244cbbb9c2553e58b511344f75d8bElliott Hughes private native void _profile(Profile pr); 6335cd52ed8fd9244cbbb9c2553e58b511344f75d8bElliott Hughes 6345cd52ed8fd9244cbbb9c2553e58b511344f75d8bElliott Hughes /** 6355cd52ed8fd9244cbbb9c2553e58b511344f75d8bElliott Hughes * Return information on SQLite runtime status. 6365cd52ed8fd9244cbbb9c2553e58b511344f75d8bElliott Hughes * Only available in SQLite 3.6 and above, 6375cd52ed8fd9244cbbb9c2553e58b511344f75d8bElliott Hughes * otherwise a no-op. 6385cd52ed8fd9244cbbb9c2553e58b511344f75d8bElliott Hughes * 6395cd52ed8fd9244cbbb9c2553e58b511344f75d8bElliott Hughes * @param op operation code 6405cd52ed8fd9244cbbb9c2553e58b511344f75d8bElliott Hughes * @param info output buffer, must be able to hold two 6415cd52ed8fd9244cbbb9c2553e58b511344f75d8bElliott Hughes * values (current/highwater) 6425cd52ed8fd9244cbbb9c2553e58b511344f75d8bElliott Hughes * @param flag reset flag 6435cd52ed8fd9244cbbb9c2553e58b511344f75d8bElliott Hughes * @return SQLite error code 6445cd52ed8fd9244cbbb9c2553e58b511344f75d8bElliott Hughes */ 6455cd52ed8fd9244cbbb9c2553e58b511344f75d8bElliott Hughes 6465cd52ed8fd9244cbbb9c2553e58b511344f75d8bElliott Hughes public synchronized static int status(int op, int info[], boolean flag) { 6475cd52ed8fd9244cbbb9c2553e58b511344f75d8bElliott Hughes return _status(op, info, flag); 6485cd52ed8fd9244cbbb9c2553e58b511344f75d8bElliott Hughes } 6495cd52ed8fd9244cbbb9c2553e58b511344f75d8bElliott Hughes 6505cd52ed8fd9244cbbb9c2553e58b511344f75d8bElliott Hughes private native static int _status(int op, int info[], boolean flag); 6515cd52ed8fd9244cbbb9c2553e58b511344f75d8bElliott Hughes 6525cd52ed8fd9244cbbb9c2553e58b511344f75d8bElliott Hughes /** 6535cd52ed8fd9244cbbb9c2553e58b511344f75d8bElliott Hughes * Return information on SQLite connection status. 6545cd52ed8fd9244cbbb9c2553e58b511344f75d8bElliott Hughes * Only available in SQLite 3.6 and above, 6555cd52ed8fd9244cbbb9c2553e58b511344f75d8bElliott Hughes * otherwise a no-op. 6565cd52ed8fd9244cbbb9c2553e58b511344f75d8bElliott Hughes * 6575cd52ed8fd9244cbbb9c2553e58b511344f75d8bElliott Hughes * @param op operation code 6585cd52ed8fd9244cbbb9c2553e58b511344f75d8bElliott Hughes * @param info output buffer, must be able to hold two 6595cd52ed8fd9244cbbb9c2553e58b511344f75d8bElliott Hughes * values (current/highwater) 6605cd52ed8fd9244cbbb9c2553e58b511344f75d8bElliott Hughes * @param flag reset flag 6615cd52ed8fd9244cbbb9c2553e58b511344f75d8bElliott Hughes * @return SQLite error code 6625cd52ed8fd9244cbbb9c2553e58b511344f75d8bElliott Hughes */ 6635cd52ed8fd9244cbbb9c2553e58b511344f75d8bElliott Hughes 6645cd52ed8fd9244cbbb9c2553e58b511344f75d8bElliott Hughes public int db_status(int op, int info[], boolean flag) { 6655cd52ed8fd9244cbbb9c2553e58b511344f75d8bElliott Hughes synchronized(this) { 6665cd52ed8fd9244cbbb9c2553e58b511344f75d8bElliott Hughes return _db_status(op, info, flag); 6675cd52ed8fd9244cbbb9c2553e58b511344f75d8bElliott Hughes } 6685cd52ed8fd9244cbbb9c2553e58b511344f75d8bElliott Hughes } 6695cd52ed8fd9244cbbb9c2553e58b511344f75d8bElliott Hughes 6705cd52ed8fd9244cbbb9c2553e58b511344f75d8bElliott Hughes private native int _db_status(int op, int info[], boolean flag); 6715cd52ed8fd9244cbbb9c2553e58b511344f75d8bElliott Hughes 6725cd52ed8fd9244cbbb9c2553e58b511344f75d8bElliott Hughes /** 673417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes * Compile and return SQLite VM for SQL statement. Only available 674417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes * in SQLite 2.8.0 and above, otherwise a no-op. 675417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes * 676417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes * @param sql SQL statement to be compiled 677417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes * @return a Vm object 678417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes */ 679417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes 680417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes public Vm compile(String sql) throws SQLite.Exception { 6817a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes synchronized(this) { 6827a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes Vm vm = new Vm(); 6837a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes vm_compile(sql, vm); 6847a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes return vm; 6857a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes } 686417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes } 687417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes 688417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes /** 689417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes * Compile and return SQLite VM for SQL statement. Only available 690417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes * in SQLite 3.0 and above, otherwise a no-op. 691417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes * 692417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes * @param sql SQL statement to be compiled 693417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes * @param args arguments for the SQL statement, '%q' substitution 694417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes * @return a Vm object 695417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes */ 696417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes 697417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes public Vm compile(String sql, String args[]) throws SQLite.Exception { 6987a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes synchronized(this) { 6997a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes Vm vm = new Vm(); 7007a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes vm_compile_args(sql, vm, args); 7017a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes return vm; 7027a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes } 703417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes } 704417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes 705417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes /** 706417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes * Prepare and return SQLite3 statement for SQL. Only available 707417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes * in SQLite 3.0 and above, otherwise a no-op. 708417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes * 709417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes * @param sql SQL statement to be prepared 710417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes * @return a Stmt object 711417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes */ 712417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes 713417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes public Stmt prepare(String sql) throws SQLite.Exception { 7147a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes synchronized(this) { 7157a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes Stmt stmt = new Stmt(); 7167a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes stmt_prepare(sql, stmt); 7177a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes return stmt; 7187a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes } 719417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes } 720417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes 721417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes /** 722417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes * Open an SQLite3 blob. Only available in SQLite 3.4.0 and above. 723417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes * @param db database name 724417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes * @param table table name 725417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes * @param column column name 726417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes * @param row row identifier 727417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes * @param rw if true, open for read-write, else read-only 728417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes * @return a Blob object 729417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes */ 730417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes 731417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes public Blob open_blob(String db, String table, String column, 7327a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes long row, boolean rw) throws SQLite.Exception { 7337a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes synchronized(this) { 7347a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes Blob blob = new Blob(); 7357a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes _open_blob(db, table, column, row, rw, blob); 7367a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes return blob; 7377a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes } 738417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes } 739417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes 740417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes /** 741417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes * Check type of open database. 742417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes * @return true if SQLite3 database 743417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes */ 744417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes 745417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes public native boolean is3(); 746417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes 747417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes /** 748417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes * Internal compile method. 749417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes * @param sql SQL statement 750417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes * @param vm Vm object 751417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes */ 752417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes 753417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes private native void vm_compile(String sql, Vm vm) 7547a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes throws SQLite.Exception; 755417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes 756417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes /** 757417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes * Internal compile method, SQLite 3.0 only. 758417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes * @param sql SQL statement 759417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes * @param args arguments for the SQL statement, '%q' substitution 760417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes * @param vm Vm object 761417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes */ 762417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes 763417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes private native void vm_compile_args(String sql, Vm vm, String args[]) 7647a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes throws SQLite.Exception; 765417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes 766417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes /** 767417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes * Internal SQLite3 prepare method. 768417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes * @param sql SQL statement 769417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes * @param stmt Stmt object 770417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes */ 771417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes 772417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes private native void stmt_prepare(String sql, Stmt stmt) 7737a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes throws SQLite.Exception; 774417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes 775417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes /** 776417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes * Internal SQLite open blob method. 777417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes * @param db database name 778417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes * @param table table name 779417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes * @param column column name 780417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes * @param row row identifier 781417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes * @param rw if true, open for read-write, else read-only 782417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes * @param blob Blob object 783417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes */ 784417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes 785417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes private native void _open_blob(String db, String table, String column, 7867a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes long row, boolean rw, Blob blob) 7877a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes throws SQLite.Exception; 788417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes 789417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes /** 790417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes * Establish a progress callback method which gets called after 791417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes * N SQLite VM opcodes. 792417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes * 793417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes * @param n number of SQLite VM opcodes until callback is invoked 794417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes * @param p the object implementing the progress callback method 795417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes */ 796417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes 797417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes public void progress_handler(int n, SQLite.ProgressHandler p) { 7987a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes synchronized(this) { 7997a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes _progress_handler(n, p); 8007a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes } 801417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes } 802417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes 803417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes private native void _progress_handler(int n, SQLite.ProgressHandler p); 804417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes 805417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes /** 8067a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes * Specify key for encrypted database. To be called 8077a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes * right after open() on SQLite3 databases. 8087a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes * Not available in public releases of SQLite. 8097a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes * 8107a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes * @param ekey the key as byte array 8117a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes */ 8127a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes 8137a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes public void key(byte[] ekey) throws SQLite.Exception { 8147a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes synchronized(this) { 8157a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes _key(ekey); 8167a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes } 8177a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes } 8187a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes 8197a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes /** 8207a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes * Specify key for encrypted database. To be called 8217a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes * right after open() on SQLite3 databases. 8227a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes * Not available in public releases of SQLite. 8237a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes * 8247a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes * @param skey the key as String 8257a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes */ 8267a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes 8277a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes public void key(String skey) throws SQLite.Exception { 8287a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes synchronized(this) { 8297a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes byte ekey[] = null; 8307a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes if (skey != null && skey.length() > 0) { 8317a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes ekey = new byte[skey.length()]; 8327a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes for (int i = 0; i< skey.length(); i++) { 8337a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes char c = skey.charAt(i); 8347a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes ekey[i] = (byte) ((c & 0xff) ^ (c >> 8)); 8357a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes } 8367a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes } 8377a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes _key(ekey); 8387a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes } 8397a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes } 8407a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes 8417a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes private native void _key(byte[] ekey); 8427a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes 8437a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes /** 8447a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes * Change the key of a encrypted database. The 8457a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes * SQLite3 database must have been open()ed. 8467a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes * Not available in public releases of SQLite. 8477a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes * 8487a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes * @param ekey the key as byte array 8497a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes */ 8507a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes 8517a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes public void rekey(byte[] ekey) throws SQLite.Exception { 8527a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes synchronized(this) { 8537a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes _rekey(ekey); 8547a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes } 8557a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes } 8567a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes 8577a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes /** 8587a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes * Change the key of a encrypted database. The 8597a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes * SQLite3 database must have been open()ed. 8607a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes * Not available in public releases of SQLite. 8617a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes * 8627a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes * @param skey the key as String 8637a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes */ 8647a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes 8657a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes public void rekey(String skey) throws SQLite.Exception { 8667a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes synchronized(this) { 8677a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes byte ekey[] = null; 8687a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes if (skey != null && skey.length() > 0) { 8697a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes ekey = new byte[skey.length()]; 8707a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes for (int i = 0; i< skey.length(); i++) { 8717a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes char c = skey.charAt(i); 8727a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes ekey[i] = (byte) ((c & 0xff) ^ (c >> 8)); 8737a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes } 8747a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes } 8757a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes _rekey(ekey); 8767a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes } 8777a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes } 8787a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes 8797a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes private native void _rekey(byte[] ekey); 8807a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes 8817a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes /** 8827a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes * Enable/disable shared cache mode (SQLite 3.x only). 8837a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes * 8847a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes * @param onoff boolean to enable or disable shared cache 8857a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes * @return boolean when true, function supported/succeeded 8867a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes */ 8877a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes 8887a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes protected static native boolean _enable_shared_cache(boolean onoff); 8897a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes 8907a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes /** 891417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes * Internal native initializer. 892417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes */ 893417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes 894417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes private static native void internal_init(); 895417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes 896417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes /** 8977a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes * Make long value from julian date for java.lang.Date 8987a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes * 8997a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes * @param d double value (julian date in SQLite3 format) 9007a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes * @return long 9017a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes */ 9027a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes 9037a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes public static long long_from_julian(double d) { 9047a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes d -= 2440587.5; 9057a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes d *= 86400000.0; 9067a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes return (long) d; 9077a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes } 9087a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes 9097a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes /** 9107a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes * Make long value from julian date for java.lang.Date 9117a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes * 9127a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes * @param s string (double value) (julian date in SQLite3 format) 9137a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes * @return long 9147a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes */ 9157a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes 9167a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes public static long long_from_julian(String s) throws SQLite.Exception { 9177a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes try { 918885023a4cd04a95372ea38bf92516039666d67d7Elliott Hughes double d = Double.parseDouble(s); // android-changed: performance 9197a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes return long_from_julian(d); 9207a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes } catch (java.lang.Exception ee) { 9217a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes throw new SQLite.Exception("not a julian date"); 9227a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes } 9237a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes } 9247a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes 9257a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes /** 9267a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes * Make julian date value from java.lang.Date 9277a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes * 9287a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes * @param ms millisecond value of java.lang.Date 9297a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes * @return double 9307a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes */ 9317a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes 9327a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes public static double julian_from_long(long ms) { 9337a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes double adj = (ms < 0) ? 0 : 0.5; 9347a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes double d = (ms + adj) / 86400000.0 + 2440587.5; 9357a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes return d; 9367a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes } 9377a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes 9387a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes /** 939417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes * Static initializer to load the native part. 940417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes */ 941417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes 942417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes static { 9437a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes try { 9447a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes String path = System.getProperty("SQLite.library.path"); 9457a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes if (path == null || path.length() == 0) { 9467a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes System.loadLibrary("sqlite_jni"); 9477a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes } else { 9487a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes try { 9497a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes java.lang.reflect.Method mapLibraryName; 9507a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes Class param[] = new Class[1]; 9517a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes param[0] = String.class; 9527a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes mapLibraryName = System.class.getMethod("mapLibraryName", 9537a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes param); 9547a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes Object args[] = new Object[1]; 9557a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes args[0] = "sqlite_jni"; 9567a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes String mapped = (String) mapLibraryName.invoke(null, args); 9577a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes System.load(path + java.io.File.separator + mapped); 9587a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes } catch (Throwable t) { 9597a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes System.err.println("Unable to load sqlite_jni from" + 9607a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes "SQLite.library.path=" + path + 9617a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes ", trying system default: " + t); 9627a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes System.loadLibrary("sqlite_jni"); 9637a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes } 9647a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes } 9657a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes } catch (Throwable t) { 9667a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes System.err.println("Unable to load sqlite_jni: " + t); 9677a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes } 9687a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes /* 9697a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes * Call native initializer functions now, since the 9707a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes * native part could have been linked statically, i.e. 9717a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes * the try/catch above would have failed in that case. 9727a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes */ 9737a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes try { 9747a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes internal_init(); 9757a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes new FunctionContext(); 9767a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes } catch (java.lang.Exception e) { 9777a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes } 978417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes } 979417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes} 98017c83b1a74c906c9a36257a3a99cd1e3730b002eJeremy Sharpe 981