3 * Copyright (C) 2007 Jürg Billeter
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 * Jürg Billeter <j@bitron.ch>
23 [CCode (lower_case_cprefix = "sqlite3_", cheader_filename = "sqlite3.h")]
25 /* Database Connection Handle */
27 [CCode (free_function = "sqlite3_close", cname = "sqlite3", cprefix = "sqlite3_")]
28 public class Database {
29 public int busy_timeout (int ms);
30 public int changes ();
31 [CCode (cname = "sqlite3_exec")]
32 public int _exec (string sql, Callback? callback = null, [CCode (type = "char**")] out unowned string? errmsg = null);
33 [CCode (cname = "_sqlite3_exec")]
34 public int exec (string sql, Callback? callback = null, out string? errmsg = null) {
35 unowned string? sqlite_errmsg;
36 var ec = this._exec (sql, callback, out sqlite_errmsg);
37 if (&errmsg != null) {
38 errmsg = sqlite_errmsg;
40 Sqlite.Memory.free ((void*) sqlite_errmsg);
43 public int extended_result_codes (int onoff);
44 public int get_autocommit ();
45 public void interrupt ();
46 public int64 last_insert_rowid ();
47 public int limit (Sqlite.Limit id, int new_val);
48 public int total_changes ();
49 public int complete (string sql);
50 [CCode (cname = "sqlite3_get_table")]
51 public int _get_table (string sql, [CCode (array_length = false)] out unowned string[] resultp, out int nrow, out int ncolumn, [CCode (type = "char**")] out unowned string? errmsg = null);
52 private static void free_table ([CCode (array_length = false)] string[] result);
53 [CCode (cname = "_sqlite3_get_table")]
54 public int get_table (string sql, out string[] resultp, out int nrow, out int ncolumn, out string? errmsg = null) {
55 unowned string? sqlite_errmsg;
56 unowned string[] sqlite_resultp;
58 var ec = this._get_table (sql, out sqlite_resultp, out nrow, out ncolumn, out sqlite_errmsg);
60 resultp = new string[(nrow + 1) * ncolumn];
61 for (var entry = 0 ; entry < resultp.length ; entry++ ) {
62 resultp[entry] = sqlite_resultp[entry];
64 Sqlite.Database.free_table (sqlite_resultp);
66 if (&errmsg != null) {
67 errmsg = sqlite_errmsg;
69 Sqlite.Memory.free ((void*) sqlite_errmsg);
72 public static int open (string filename, out Database db);
73 public static int open_v2 (string filename, out Database db, int flags = OPEN_READWRITE | OPEN_CREATE, string? zVfs = null);
74 public int errcode ();
75 public unowned string errmsg ();
76 public unowned Sqlite.Statement next_stmt (Sqlite.Statement? current);
77 public int prepare (string sql, int n_bytes, out Statement stmt, out unowned string tail = null);
78 public int prepare_v2 (string sql, int n_bytes, out Statement stmt, out unowned string tail = null);
79 public int set_authorizer (AuthorizeCallback? auth);
80 [CCode (cname = "sqlite3_db_status")]
81 public int status (Sqlite.DatabaseStatus op, out int pCurrent, out int pHighwater, int resetFlag = 0);
82 public int table_column_metadata (string db_name, string table_name, string column_name, out string? data_type, out string? collation_sequence, out int? not_null, out int? primary_key, out int? auto_increment);
83 public void trace (TraceCallback? xtrace);
84 public void profile (ProfileCallback? xprofile);
85 public void progress_handler (int n_opcodes, Sqlite.ProgressCallback? progress_handler);
86 public void commit_hook (CommitCallback? commit_hook);
87 public void rollback_hook (RollbackCallback? rollback_hook);
88 public void update_hook (UpdateCallback? update_hook);
89 public int create_function (string zFunctionName, int nArg, int eTextRep, void * user_data, UserFuncCallback? xFunc, UserFuncCallback? xStep, UserFuncFinishCallback? xFinal);
90 public int create_function_v2 (string zFunctionName, int nArg, int eTextRep, void * user_data, UserFuncCallback? xFunc, UserFuncCallback? xStep, UserFuncFinishCallback? xFinal, GLib.DestroyNotify? destroy = null);
91 public int create_collation (string zName, int eTextRep, [CCode (delegate_target_pos = 2.9, type = "int (*)(void *, int, const void *, int, const void *)")] CompareCallback xCompare);
93 public int wal_autocheckpoint (int N);
94 public int wal_checkpoint (string zDb);
95 public void* wal_hook (WALHookCallback cb, string db_name, int page_count);
98 [CCode (instance_pos = 0)]
99 public delegate int AuthorizeCallback (Sqlite.Action action, string? p1, string? p2, string db_name, string? responsible);
100 [CCode (instance_pos = 0)]
101 public delegate void TraceCallback (string message);
102 [CCode (instance_pos = 0)]
103 public delegate void ProfileCallback (string sql, uint64 time);
104 public delegate int ProgressCallback ();
105 public delegate int CommitCallback ();
106 public delegate void RollbackCallback ();
107 [CCode (has_target = false)]
108 public delegate void UserFuncCallback (Sqlite.Context context, [CCode (array_length_pos = 1.1)] Sqlite.Value[] values);
109 [CCode (has_target = false)]
110 public delegate void UserFuncFinishCallback (Sqlite.Context context);
111 [CCode (instance_pos = 0)]
112 public delegate void UpdateCallback (Sqlite.Action action, string dbname, string table, int64 rowid);
113 [CCode (instance_pos = 0)]
114 public delegate int CompareCallback (int alen, void* a, int blen, void* b);
115 [CCode (instance_pos = 0)]
116 public delegate int WALHookCallback (Sqlite.Database db, string dbname, int pages);
118 public unowned string? compileoption_get (int n);
119 public int compileoption_used (string option_name);
120 public static int complete (string sql);
121 [CCode (sentinel = "")]
122 public static int config (Sqlite.Config op, ...);
123 public unowned string libversion ();
124 public int libversion_number ();
126 public void log (int error_code, string format, ...);
127 public unowned string sourceid ();
128 public static int status (Sqlite.Status op, out int pCurrent, out int pHighwater, int resetFlag = 0);
129 public static int threadsafe ();
131 [CCode (cname = "SQLITE_VERSION")]
132 public const string VERSION;
133 [CCode (cname = "SQLITE_VERSION_NUMBER")]
134 public const int VERSION_NUMBER;
135 [CCode (cname = "SQLITE_SOURCE_ID")]
136 public const string SOURCE_ID;
138 /* Dynamically Typed Value Object */
140 [CCode (cname = "sqlite3_value")]
142 [CCode (cname = "sqlite3_value_blob")]
143 public void* to_blob ();
144 [CCode (cname = "sqlite3_value_bytes")]
145 public int to_bytes ();
146 [CCode (cname = "sqlite3_value_double")]
147 public double to_double ();
148 [CCode (cname = "sqlite3_value_int")]
149 public int to_int ();
150 [CCode (cname = "sqlite3_value_int64")]
151 public int64 to_int64 ();
152 [CCode (cname = "sqlite3_value_text")]
153 public unowned string to_text ();
154 [CCode (cname = "sqlite3_value_type")]
155 public int to_type ();
156 [CCode (cname = "sqlite3_value_numeric_type")]
157 public int to_numeric_type ();
160 [CCode (cname = "sqlite3_callback", instance_pos = 0)]
161 public delegate int Callback (int n_columns, [CCode (array_length = false)] string[] values, [CCode (array_length = false)] string[] column_names);
163 [CCode (cname = "SQLITE_OK")]
165 [CCode (cname = "SQLITE_ERROR")]
166 public const int ERROR;
167 [CCode (cname = "SQLITE_INTERNAL")]
168 public const int INTERNAL;
169 [CCode (cname = "SQLITE_PERM")]
170 public const int PERM;
171 [CCode (cname = "SQLITE_ABORT")]
172 public const int ABORT;
173 [CCode (cname = "SQLITE_BUSY")]
174 public const int BUSY;
175 [CCode (cname = "SQLITE_LOCKED")]
176 public const int LOCKED;
177 [CCode (cname = "SQLITE_NOMEM")]
178 public const int NOMEM;
179 [CCode (cname = "SQLITE_READONLY")]
180 public const int READONLY;
181 [CCode (cname = "SQLITE_INTERRUPT")]
182 public const int INTERRUPT;
183 [CCode (cname = "SQLITE_IOERR")]
184 public const int IOERR;
185 [CCode (cname = "SQLITE_CORRUPT")]
186 public const int CORRUPT;
187 [CCode (cname = "SQLITE_NOTFOUND")]
188 public const int NOTFOUND;
189 [CCode (cname = "SQLITE_FULL")]
190 public const int FULL;
191 [CCode (cname = "SQLITE_CANTOPEN")]
192 public const int CANTOPEN;
193 [CCode (cname = "SQLITE_PROTOCOL")]
194 public const int PROTOCOL;
195 [CCode (cname = "SQLITE_EMPTY")]
196 public const int EMPTY;
197 [CCode (cname = "SQLITE_SCHEMA")]
198 public const int SCHEMA;
199 [CCode (cname = "SQLITE_TOOBIG")]
200 public const int TOOBIG;
201 [CCode (cname = "SQLITE_CONSTRAINT")]
202 public const int CONSTRAINT;
203 [CCode (cname = "SQLITE_MISMATCH")]
204 public const int MISMATCH;
205 [CCode (cname = "SQLITE_MISUSE")]
206 public const int MISUSE;
207 [CCode (cname = "SQLITE_NOLFS")]
208 public const int NOLFS;
209 [CCode (cname = "SQLITE_AUTH")]
210 public const int AUTH;
211 [CCode (cname = "SQLITE_FORMAT")]
212 public const int FORMAT;
213 [CCode (cname = "SQLITE_RANGE")]
214 public const int RANGE;
215 [CCode (cname = "SQLITE_NOTADB")]
216 public const int NOTADB;
217 [CCode (cname = "SQLITE_ROW")]
218 public const int ROW;
219 [CCode (cname = "SQLITE_DONE")]
220 public const int DONE;
221 [CCode (cname = "SQLITE_OPEN_READONLY")]
222 public const int OPEN_READONLY;
223 [CCode (cname = "SQLITE_OPEN_READWRITE")]
224 public const int OPEN_READWRITE;
225 [CCode (cname = "SQLITE_OPEN_CREATE")]
226 public const int OPEN_CREATE;
227 [CCode (cname = "SQLITE_OPEN_URI")]
228 public const int OPEN_URI;
229 [CCode (cname = "SQLITE_OPEN_MEMORY")]
230 public const int OPEN_MEMORY;
231 [CCode (cname = "SQLITE_OPEN_NOMUTEX")]
232 public const int OPEN_NOMUTEX;
233 [CCode (cname = "SQLITE_OPEN_FULLMUTEX")]
234 public const int OPEN_FULLMUTEX;
235 [CCode (cname = "SQLITE_OPEN_SHAREDCACHE")]
236 public const int OPEN_SHAREDCACHE;
237 [CCode (cname = "SQLITE_OPEN_PRIVATECACHE")]
238 public const int OPEN_PRIVATECACHE;
239 [CCode (cname = "SQLITE_INTEGER")]
240 public const int INTEGER;
241 [CCode (cname = "SQLITE_FLOAT")]
242 public const int FLOAT;
243 [CCode (cname = "SQLITE_BLOB")]
244 public const int BLOB;
245 [CCode (cname = "SQLITE_NULL")]
246 public const int NULL;
247 [CCode (cname = "SQLITE3_TEXT")]
248 public const int TEXT;
249 [CCode (cname = "SQLITE_MUTEX_FAST")]
250 public const int MUTEX_FAST;
251 [CCode (cname = "SQLITE_MUTEX_RECURSIVE")]
252 public const int MUTEX_RECURSIVE;
253 [CCode (cname = "SQLITE_UTF8")]
254 public const int UTF8;
255 [CCode (cname = "SQLITE_UTF16LE")]
256 public const int UTF16LE;
257 [CCode (cname = "SQLITE_UTF16BE")]
258 public const int UTF16BE;
259 [CCode (cname = "SQLITE_UTF16")]
260 public const int UTF16;
261 [CCode (cname = "SQLITE_ANY")]
262 public const int ANY;
263 [CCode (cname = "SQLITE_UTF16_ALIGNED")]
264 public const int UTF16_ALIGNED;
266 [CCode (cname = "int", cprefix = "SQLITE_", has_type_id = false)]
303 [CCode (cname = "int", cprefix = "SQLITE_CONFIG_", has_type_id = false)]
322 [CCode (cname = "int", cprefix = "SQLITE_DBSTATUS_", has_type_id = false)]
323 public enum DatabaseStatus {
327 [CCode (cname = "int", cprefix = "SQLITE_LIMIT_", has_type_id = false)]
342 [CCode (cname = "int", cprefix = "SQLITE_STMTSTATUS_", has_type_id = false)]
343 public enum StatementStatus {
348 [CCode (cname = "int", cprefix = "SQLITE_STATUS_", has_type_id = false)]
361 /* SQL Statement Object */
363 [CCode (free_function = "sqlite3_finalize", cname = "sqlite3_stmt", cprefix = "sqlite3_")]
364 public class Statement {
365 public int bind_parameter_count ();
366 public int bind_parameter_index (string name);
367 public unowned string bind_parameter_name (int index);
368 public int clear_bindings ();
369 public int column_count ();
370 public int data_count ();
371 public unowned Database db_handle ();
373 [CCode (cname = "sqlite3_stmt_status")]
374 public int status (Sqlite.StatementStatus op, int resetFlg = 0);
376 public int bind_blob (int index, void* value, int n, GLib.DestroyNotify? destroy_notify = null);
377 public int bind_double (int index, double value);
378 public int bind_int (int index, int value);
379 public int bind_int64 (int index, int64 value);
380 public int bind_null (int index);
381 [CCode (cname = "sqlite3_bind_text")]
382 public int _bind_text (int index, string value, int n = -1, GLib.DestroyNotify? destroy_notify = null);
383 public int bind_text (int index, owned string value, int n = -1, GLib.DestroyNotify destroy_notify = GLib.g_free);
384 public int bind_value (int index, Value value);
385 public int bind_zeroblob (int index, int n);
386 public void* column_blob (int col);
387 public int column_bytes (int col);
388 public double column_double (int col);
389 public int column_int (int col);
390 public int64 column_int64 (int col);
391 public unowned string? column_text (int col);
392 public int column_type (int col);
393 public unowned Value column_value (int col);
394 public unowned string column_name (int index);
395 public unowned string column_database_name (int col);
396 public unowned string column_table_name (int col);
397 public unowned string column_origin_name (int col);
398 public unowned string sql ();
402 [CCode (cname = "sqlite3_malloc")]
403 public static void* malloc (int n_bytes);
404 [CCode (cname = "sqlite3_realloc")]
405 public static void* realloc (void* mem, int n_bytes);
406 [CCode (cname = "sqlite3_free")]
407 public static void free (void* mem);
408 [CCode (cname = "sqlite3_release_memory")]
409 public static int release (int bytes);
410 [CCode (cname = "sqlite3_memory_used")]
411 public static int64 used ();
412 [CCode (cname = "sqlite3_memory_highwater")]
413 public static int64 highwater (int reset = 0);
414 [Version (deprecated_since = "3.7.2", replacement = "Sqlite.Memory.soft_heap_limit64")]
415 [CCode (cname = "sqlite3_soft_heap_limit")]
416 public static void soft_heap_limit (int limit);
417 [CCode (cname = "sqlite3_soft_heap_limit64")]
418 public static int64 soft_heap_limit64 (int64 limit = -1);
422 [CCode (cname = "sqlite3_mutex")]
424 [CCode (cname = "sqlite3_mutex_alloc")]
425 public Mutex (int mutex_type = MUTEX_RECURSIVE);
426 public void enter ();
428 public int notheld ();
430 public void leave ();
433 [Compact, CCode (cname = "sqlite3_context", cprefix = "sqlite3_")]
434 public class Context {
435 public void result_blob (owned uint8[] data, GLib.DestroyNotify? destroy_notify = GLib.g_free);
436 public void result_double (double value);
437 public void result_error (string value, int error_code);
438 public void result_error_toobig ();
439 public void result_error_nomem ();
440 public void result_error_code (int error_code);
441 public void result_int (int value);
442 public void result_int64 (int64 value);
443 public void result_null ();
444 public void result_text (owned string value, int length = -1, GLib.DestroyNotify? destroy_notify = GLib.g_free);
445 public void result_value (Sqlite.Value value);
446 public void result_zeroblob (int n);
448 [CCode (simple_generics = true)]
449 public unowned T user_data<T> ();
450 [CCode (simple_generics = true)]
451 public void set_auxdata<T> (int N, owned T data);
452 [CCode (simple_generics = true)]
453 public unowned T get_auxdata<T> (int N);
454 [CCode (cname = "sqlite3_context_db_handle")]
455 public unowned Database db_handle ();
456 [CCode (cname = "sqlite3_aggregate_context")]
457 public void * aggregate (int n_bytes);
460 [Compact, CCode (cname = "sqlite3_backup", free_function = "sqlite3_backup_finish", cprefix = "sqlite3_backup_")]
461 public class Backup {
462 [CCode (cname = "sqlite3_backup_init")]
463 public Backup (Database dest, string dest_name, Database source, string source_name);
464 public int step (int nPage);
465 public int remaining ();
466 public int pagecount ();