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? sqlite3_callback = null, [CCode (type = "char**")] out unowned string errmsg = null);
33 [CCode (cname = "_sqlite3_exec")]
34 public int exec (string sql, Callback? sqlite3_callback = null, out string errmsg = null) {
35 unowned string sqlite_errmsg;
36 var ec = this._exec (sql, sqlite3_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_INTEGER")]
228 public const int INTEGER;
229 [CCode (cname = "SQLITE_FLOAT")]
230 public const int FLOAT;
231 [CCode (cname = "SQLITE_BLOB")]
232 public const int BLOB;
233 [CCode (cname = "SQLITE_NULL")]
234 public const int NULL;
235 [CCode (cname = "SQLITE3_TEXT")]
236 public const int TEXT;
237 [CCode (cname = "SQLITE_MUTEX_FAST")]
238 public const int MUTEX_FAST;
239 [CCode (cname = "SQLITE_MUTEX_RECURSIVE")]
240 public const int MUTEX_RECURSIVE;
241 [CCode (cname = "SQLITE_UTF8")]
242 public const int UTF8;
243 [CCode (cname = "SQLITE_UTF16LE")]
244 public const int UTF16LE;
245 [CCode (cname = "SQLITE_UTF16BE")]
246 public const int UTF16BE;
247 [CCode (cname = "SQLITE_UTF16")]
248 public const int UTF16;
249 [CCode (cname = "SQLITE_ANY")]
250 public const int ANY;
251 [CCode (cname = "SQLITE_UTF16_ALIGNED")]
252 public const int UTF16_ALIGNED;
254 [CCode (cname = "int", cprefix = "SQLITE_")]
291 [CCode (cname = "int", cprefix = "SQLITE_CONFIG_")]
310 [CCode (cname = "int", cprefix = "SQLITE_DBSTATUS_")]
311 public enum DatabaseStatus {
315 [CCode (cname = "int", cprefix = "SQLITE_LIMIT_")]
330 [CCode (cname = "int", cprefix = "SQLITE_STMTSTATUS_")]
331 public enum StatementStatus {
336 [CCode (cname = "int", cprefix = "SQLITE_STATUS_")]
349 /* SQL Statement Object */
351 [CCode (free_function = "sqlite3_finalize", cname = "sqlite3_stmt", cprefix = "sqlite3_")]
352 public class Statement {
353 public int bind_parameter_count ();
354 public int bind_parameter_index (string name);
355 public unowned string bind_parameter_name (int index);
356 public int clear_bindings ();
357 public int column_count ();
358 public int data_count ();
359 public unowned Database db_handle ();
361 [CCode (cname = "sqlite3_stmt_status")]
362 public int status (Sqlite.StatementStatus op, int resetFlg = 0);
364 public int bind_blob (int index, void* value, int n, GLib.DestroyNotify? destroy_notify = null);
365 public int bind_double (int index, double value);
366 public int bind_int (int index, int value);
367 public int bind_int64 (int index, int64 value);
368 public int bind_null (int index);
369 [CCode (cname = "sqlite3_bind_text")]
370 public int _bind_text (int index, string value, int n = -1, GLib.DestroyNotify? destroy_notify = null);
371 public int bind_text (int index, owned string value, int n = -1, GLib.DestroyNotify destroy_notify = GLib.g_free);
372 public int bind_value (int index, Value value);
373 public int bind_zeroblob (int index, int n);
374 public void* column_blob (int col);
375 public int column_bytes (int col);
376 public double column_double (int col);
377 public int column_int (int col);
378 public int64 column_int64 (int col);
379 public unowned string column_text (int col);
380 public int column_type (int col);
381 public unowned Value column_value (int col);
382 public unowned string column_name (int index);
383 public unowned string column_database_name (int col);
384 public unowned string column_table_name (int col);
385 public unowned string column_origin_name (int col);
386 public unowned string sql ();
390 [CCode (cname = "sqlite3_malloc")]
391 public static void* malloc (int n_bytes);
392 [CCode (cname = "sqlite3_realloc")]
393 public static void* realloc (void* mem, int n_bytes);
394 [CCode (cname = "sqlite3_free")]
395 public static void free (void* mem);
396 [CCode (cname = "sqlite3_release_memory")]
397 public static int release (int bytes);
398 [CCode (cname = "sqlite3_memory_used")]
399 public static int64 used ();
400 [CCode (cname = "sqlite3_memory_highwater")]
401 public static int64 highwater (int reset = 0);
402 [Deprecated (since = "3.7.2", replacement = "Sqlite.Memory.soft_heap_limit64")]
403 [CCode (cname = "sqlite3_soft_heap_limit")]
404 public static void soft_heap_limit (int limit);
405 [CCode (cname = "sqlite3_soft_heap_limit64")]
406 public static int64 soft_heap_limit64 (int64 limit = -1);
410 [CCode (cname = "sqlite3_mutex")]
412 [CCode (cname = "sqlite3_mutex_alloc")]
413 public Mutex (int mutex_type = MUTEX_RECURSIVE);
414 public void enter ();
416 public int notheld ();
418 public void leave ();
421 [Compact, CCode (cname = "sqlite3_context", cprefix = "sqlite3_")]
422 public class Context {
423 public void result_blob (owned uint8[] data, GLib.DestroyNotify? destroy_notify = GLib.g_free);
424 public void result_double (double value);
425 public void result_error (string value, int error_code);
426 public void result_error_toobig ();
427 public void result_error_nomem ();
428 public void result_error_code (int error_code);
429 public void result_int (int value);
430 public void result_int64 (int64 value);
431 public void result_null ();
432 public void result_text (owned string value, int length = -1, GLib.DestroyNotify? destroy_notify = GLib.g_free);
433 public void result_value (Sqlite.Value value);
434 public void result_zeroblob (int n);
436 [CCode (simple_generics = true)]
437 public unowned T user_data<T> ();
438 [CCode (simple_generics = true)]
439 public void set_auxdata<T> (int N, owned T data);
440 [CCode (simple_generics = true)]
441 public unowned T get_auxdata<T> (int N);
442 [CCode (cname = "sqlite3_context_db_handle")]
443 public unowned Database db_handle ();
444 [CCode (cname = "sqlite3_aggregate_context")]
445 public void * aggregate (int n_bytes);
448 [Compact, CCode (cname = "sqlite3_backup", free_function = "sqlite3_backup_finish", cprefix = "sqlite3_backup_")]
449 public class Backup {
450 [CCode (cname = "sqlite3_backup_init")]
451 public Backup (Database dest, string dest_name, Database source, string source_name);
452 public int step (int nPage);
453 public int remaining ();
454 public int pagecount ();