1/* sqlite3.vala 2 * 3 * Copyright (C) 2007 Jürg Billeter 4 * 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. 9 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. 14 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 18 * 19 * Author: 20 * Jürg Billeter <j@bitron.ch> 21 */ 22 23[CCode (lower_case_cprefix = "sqlite3_", cheader_filename = "sqlite3.h")] 24namespace Sqlite { 25 /* Database Connection Handle */ 26 [Compact] 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; 39 } else { 40 errmsg = null; 41 } 42 Sqlite.Memory.free ((void*) sqlite_errmsg); 43 return ec; 44 } 45 public int extended_result_codes (int onoff); 46 public int get_autocommit (); 47 public void interrupt (); 48 public int64 last_insert_rowid (); 49 public int limit (Sqlite.Limit id, int new_val); 50 public int total_changes (); 51 public static int complete (string sql); 52 [CCode (cname = "sqlite3_get_table")] 53 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); 54 private static void free_table ([CCode (array_length = false)] string[] result); 55 [CCode (cname = "_sqlite3_get_table")] 56 public int get_table (string sql, out string[] resultp, out int nrow, out int ncolumn, out string? errmsg = null) { 57 unowned string? sqlite_errmsg; 58 unowned string[] sqlite_resultp; 59 60 var ec = this._get_table (sql, out sqlite_resultp, out nrow, out ncolumn, out sqlite_errmsg); 61 62 resultp = new string[(nrow + 1) * ncolumn]; 63 for (var entry = 0 ; entry < resultp.length ; entry++ ) { 64 resultp[entry] = sqlite_resultp[entry]; 65 } 66 Sqlite.Database.free_table (sqlite_resultp); 67 68 if (&errmsg != null) { 69 errmsg = sqlite_errmsg; 70 } else { 71 errmsg = null; 72 } 73 Sqlite.Memory.free ((void*) sqlite_errmsg); 74 return ec; 75 } 76 public static int open (string filename, out Database db); 77 public static int open_v2 (string filename, out Database db, int flags = OPEN_READWRITE | OPEN_CREATE, string? zVfs = null); 78 public int errcode (); 79 public unowned string errmsg (); 80 public unowned Sqlite.Statement next_stmt (Sqlite.Statement? current); 81 public int prepare (string sql, int n_bytes, out Statement stmt, out unowned string tail = null); 82 public int prepare_v2 (string sql, int n_bytes, out Statement stmt, out unowned string tail = null); 83 public int set_authorizer (AuthorizeCallback? auth); 84 [CCode (cname = "sqlite3_db_status")] 85 public int status (Sqlite.DatabaseStatus op, out int pCurrent, out int pHighwater, int resetFlag = 0); 86 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); 87 public void trace (TraceCallback? xtrace); 88 public void profile (ProfileCallback? xprofile); 89 public void progress_handler (int n_opcodes, Sqlite.ProgressCallback? progress_handler); 90 public void commit_hook (CommitCallback? commit_hook); 91 public void rollback_hook (RollbackCallback? rollback_hook); 92 public void update_hook (UpdateCallback? update_hook); 93 public int create_function (string zFunctionName, int nArg, int eTextRep, void * user_data, UserFuncCallback? xFunc, UserFuncCallback? xStep, UserFuncFinishCallback? xFinal); 94 public int create_function_v2 (string zFunctionName, int nArg, int eTextRep, void * user_data, UserFuncCallback? xFunc, UserFuncCallback? xStep, UserFuncFinishCallback? xFinal, GLib.DestroyNotify? destroy = null); 95 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); 96 97 public int wal_autocheckpoint (int N); 98 public int wal_checkpoint (string zDb); 99 public void* wal_hook (WALHookCallback cb); 100 } 101 102 [CCode (has_typedef = false, instance_pos = 0.9)] 103 public delegate int AuthorizeCallback (Sqlite.Action action, string? p1, string? p2, string db_name, string? responsible); 104 [CCode (has_typedef = false, instance_pos = 0.9)] 105 public delegate void TraceCallback (string message); 106 [CCode (has_typedef = false, instance_pos = 0.9)] 107 public delegate void ProfileCallback (string sql, uint64 time); 108 [CCode (has_typedef = false)] 109 public delegate int ProgressCallback (); 110 [CCode (has_typedef = false)] 111 public delegate int CommitCallback (); 112 [CCode (has_typedef = false)] 113 public delegate void RollbackCallback (); 114 [CCode (has_typedef = false, has_target = false)] 115 public delegate void UserFuncCallback (Sqlite.Context context, [CCode (array_length_pos = 1.1)] Sqlite.Value[] values); 116 [CCode (has_typedef = false, has_target = false)] 117 public delegate void UserFuncFinishCallback (Sqlite.Context context); 118 [CCode (has_typedef = false, instance_pos = 0.9)] 119 public delegate void UpdateCallback (Sqlite.Action action, string dbname, string table, int64 rowid); 120 [CCode (has_typedef = false, instance_pos = 0.9)] 121 public delegate int CompareCallback (int alen, void* a, int blen, void* b); 122 [CCode (has_typedef = false, instance_pos = 0.9)] 123 public delegate int WALHookCallback (Sqlite.Database db, string dbname, int pages); 124 125 public unowned string? compileoption_get (int n); 126 public int compileoption_used (string option_name); 127 public static int complete (string sql); 128 [CCode (sentinel = "")] 129 public static int config (Sqlite.Config op, ...); 130 public unowned string libversion (); 131 public int libversion_number (); 132 [PrintfFormat] 133 public void log (int error_code, string format, ...); 134 public unowned string sourceid (); 135 public static int status (Sqlite.Status op, out int pCurrent, out int pHighwater, int resetFlag = 0); 136 public static int threadsafe (); 137 138 [CCode (cname = "SQLITE_VERSION")] 139 public const string VERSION; 140 [CCode (cname = "SQLITE_VERSION_NUMBER")] 141 public const int VERSION_NUMBER; 142 [CCode (cname = "SQLITE_SOURCE_ID")] 143 public const string SOURCE_ID; 144 145 /* Dynamically Typed Value Object */ 146 [Compact] 147 [CCode (cname = "sqlite3_value")] 148 public class Value { 149 [CCode (cname = "sqlite3_value_blob")] 150 public void* to_blob (); 151 [CCode (cname = "sqlite3_value_bytes")] 152 public int to_bytes (); 153 [CCode (cname = "sqlite3_value_double")] 154 public double to_double (); 155 [CCode (cname = "sqlite3_value_int")] 156 public int to_int (); 157 [CCode (cname = "sqlite3_value_int64")] 158 public int64 to_int64 (); 159 [CCode (cname = "sqlite3_value_text", type = "const unsigned char*")] 160 public unowned string to_text (); 161 [CCode (cname = "sqlite3_value_type")] 162 public int to_type (); 163 [CCode (cname = "sqlite3_value_numeric_type")] 164 public int to_numeric_type (); 165 } 166 167 [CCode (cname = "sqlite3_callback", instance_pos = 0)] 168 public delegate int Callback (int n_columns, [CCode (array_length = false)] string[] values, [CCode (array_length = false)] string[] column_names); 169 170 [CCode (cname = "SQLITE_OK")] 171 public const int OK; 172 [CCode (cname = "SQLITE_ERROR")] 173 public const int ERROR; 174 [CCode (cname = "SQLITE_INTERNAL")] 175 public const int INTERNAL; 176 [CCode (cname = "SQLITE_PERM")] 177 public const int PERM; 178 [CCode (cname = "SQLITE_ABORT")] 179 public const int ABORT; 180 [CCode (cname = "SQLITE_BUSY")] 181 public const int BUSY; 182 [CCode (cname = "SQLITE_LOCKED")] 183 public const int LOCKED; 184 [CCode (cname = "SQLITE_NOMEM")] 185 public const int NOMEM; 186 [CCode (cname = "SQLITE_READONLY")] 187 public const int READONLY; 188 [CCode (cname = "SQLITE_INTERRUPT")] 189 public const int INTERRUPT; 190 [CCode (cname = "SQLITE_IOERR")] 191 public const int IOERR; 192 [CCode (cname = "SQLITE_CORRUPT")] 193 public const int CORRUPT; 194 [CCode (cname = "SQLITE_NOTFOUND")] 195 public const int NOTFOUND; 196 [CCode (cname = "SQLITE_FULL")] 197 public const int FULL; 198 [CCode (cname = "SQLITE_CANTOPEN")] 199 public const int CANTOPEN; 200 [CCode (cname = "SQLITE_PROTOCOL")] 201 public const int PROTOCOL; 202 [CCode (cname = "SQLITE_EMPTY")] 203 public const int EMPTY; 204 [CCode (cname = "SQLITE_SCHEMA")] 205 public const int SCHEMA; 206 [CCode (cname = "SQLITE_TOOBIG")] 207 public const int TOOBIG; 208 [CCode (cname = "SQLITE_CONSTRAINT")] 209 public const int CONSTRAINT; 210 [CCode (cname = "SQLITE_MISMATCH")] 211 public const int MISMATCH; 212 [CCode (cname = "SQLITE_MISUSE")] 213 public const int MISUSE; 214 [CCode (cname = "SQLITE_NOLFS")] 215 public const int NOLFS; 216 [CCode (cname = "SQLITE_AUTH")] 217 public const int AUTH; 218 [CCode (cname = "SQLITE_FORMAT")] 219 public const int FORMAT; 220 [CCode (cname = "SQLITE_RANGE")] 221 public const int RANGE; 222 [CCode (cname = "SQLITE_NOTADB")] 223 public const int NOTADB; 224 [CCode (cname = "SQLITE_ROW")] 225 public const int ROW; 226 [CCode (cname = "SQLITE_DONE")] 227 public const int DONE; 228 [CCode (cname = "SQLITE_OPEN_READONLY")] 229 public const int OPEN_READONLY; 230 [CCode (cname = "SQLITE_OPEN_READWRITE")] 231 public const int OPEN_READWRITE; 232 [CCode (cname = "SQLITE_OPEN_CREATE")] 233 public const int OPEN_CREATE; 234 [CCode (cname = "SQLITE_OPEN_URI")] 235 public const int OPEN_URI; 236 [CCode (cname = "SQLITE_OPEN_MEMORY")] 237 public const int OPEN_MEMORY; 238 [CCode (cname = "SQLITE_OPEN_NOMUTEX")] 239 public const int OPEN_NOMUTEX; 240 [CCode (cname = "SQLITE_OPEN_FULLMUTEX")] 241 public const int OPEN_FULLMUTEX; 242 [CCode (cname = "SQLITE_OPEN_SHAREDCACHE")] 243 public const int OPEN_SHAREDCACHE; 244 [CCode (cname = "SQLITE_OPEN_PRIVATECACHE")] 245 public const int OPEN_PRIVATECACHE; 246 [CCode (cname = "SQLITE_INTEGER")] 247 public const int INTEGER; 248 [CCode (cname = "SQLITE_FLOAT")] 249 public const int FLOAT; 250 [CCode (cname = "SQLITE_BLOB")] 251 public const int BLOB; 252 [CCode (cname = "SQLITE_NULL")] 253 public const int NULL; 254 [CCode (cname = "SQLITE3_TEXT")] 255 public const int TEXT; 256 [CCode (cname = "SQLITE_MUTEX_FAST")] 257 public const int MUTEX_FAST; 258 [CCode (cname = "SQLITE_MUTEX_RECURSIVE")] 259 public const int MUTEX_RECURSIVE; 260 [CCode (cname = "SQLITE_UTF8")] 261 public const int UTF8; 262 [CCode (cname = "SQLITE_UTF16LE")] 263 public const int UTF16LE; 264 [CCode (cname = "SQLITE_UTF16BE")] 265 public const int UTF16BE; 266 [CCode (cname = "SQLITE_UTF16")] 267 public const int UTF16; 268 [CCode (cname = "SQLITE_ANY")] 269 public const int ANY; 270 [CCode (cname = "SQLITE_UTF16_ALIGNED")] 271 public const int UTF16_ALIGNED; 272 273 [CCode (cname = "int", cprefix = "SQLITE_", has_type_id = false)] 274 public enum Action { 275 CREATE_INDEX, 276 CREATE_TABLE, 277 CREATE_TEMP_INDEX, 278 CREATE_TEMP_TABLE, 279 CREATE_TEMP_TRIGGER, 280 CREATE_TEMP_VIEW, 281 CREATE_TRIGGER, 282 CREATE_VIEW, 283 DELETE, 284 DROP_INDEX, 285 DROP_TABLE, 286 DROP_TEMP_INDEX, 287 DROP_TEMP_TABLE, 288 DROP_TEMP_TRIGGER, 289 DROP_TEMP_VIEW, 290 DROP_TRIGGER, 291 DROP_VIEW, 292 INSERT, 293 PRAGMA, 294 READ, 295 SELECT, 296 TRANSACTION, 297 UPDATE, 298 ATTACH, 299 DETACH, 300 ALTER_TABLE, 301 REINDEX, 302 ANALYZE, 303 CREATE_VTABLE, 304 DROP_VTABLE, 305 FUNCTION, 306 SAVEPOINT, 307 COPY 308 } 309 310 [CCode (cname = "int", cprefix = "SQLITE_CONFIG_", has_type_id = false)] 311 public enum Config { 312 SINGLETHREAD, 313 MULTITHREAD, 314 SERIALIZED, 315 MALLOC, 316 GETMALLOC, 317 SCRATCH, 318 PAGECACHE, 319 HEAP, 320 MEMSTATUS, 321 MUTEX, 322 GETMUTEX, 323 LOOKASIDE, 324 PCACHE, 325 GETPCACHE, 326 LOG, 327 } 328 329 [CCode (cname = "int", cprefix = "SQLITE_DBSTATUS_", has_type_id = false)] 330 public enum DatabaseStatus { 331 LOOKASIDE_USED 332 } 333 334 [CCode (cname = "int", cprefix = "SQLITE_LIMIT_", has_type_id = false)] 335 public enum Limit { 336 LENGTH, 337 SQL_LENGTH, 338 COLUMN, 339 EXPR_DEPTH, 340 COMPOUND_SELECT, 341 VDBE_OP, 342 FUNCTION_ARG, 343 ATTACHED, 344 LIKE_PATTERN_LENGTH, 345 VARIABLE_NUMBER, 346 TRIGGER_DEPTH 347 } 348 349 [CCode (cname = "int", cprefix = "SQLITE_STMTSTATUS_", has_type_id = false)] 350 public enum StatementStatus { 351 FULLSCAN_STEP, 352 SORT 353 } 354 355 [CCode (cname = "int", cprefix = "SQLITE_STATUS_", has_type_id = false)] 356 public enum Status { 357 MEMORY_USED, 358 PAGECACHE_USED, 359 PAGECACHE_OVERFLOW, 360 SCRATCH_USED, 361 SCRATCH_OVERFLOW, 362 MALLOC_SIZE, 363 PARSER_STACK, 364 PAGECACHE_SIZE, 365 SCRATCH_SIZE 366 } 367 368 /* SQL Statement Object */ 369 [Compact] 370 [CCode (free_function = "sqlite3_finalize", cname = "sqlite3_stmt", cprefix = "sqlite3_")] 371 public class Statement { 372 public int bind_parameter_count (); 373 public int bind_parameter_index (string name); 374 public unowned string bind_parameter_name (int index); 375 public int clear_bindings (); 376 public int column_count (); 377 public int data_count (); 378 public unowned Database db_handle (); 379 public int reset (); 380 [CCode (cname = "sqlite3_stmt_status")] 381 public int status (Sqlite.StatementStatus op, int resetFlg = 0); 382 public int step (); 383 public int bind_blob (int index, void* value, int n, GLib.DestroyNotify? destroy_notify = null); 384 public int bind_double (int index, double value); 385 public int bind_int (int index, int value); 386 public int bind_int64 (int index, int64 value); 387 public int bind_null (int index); 388 [CCode (cname = "sqlite3_bind_text")] 389 public int _bind_text (int index, string value, int n = -1, GLib.DestroyNotify? destroy_notify = null); 390 public int bind_text (int index, owned string value, int n = -1, GLib.DestroyNotify destroy_notify = GLib.g_free); 391 public int bind_value (int index, Value value); 392 public int bind_zeroblob (int index, int n); 393 public void* column_blob (int col); 394 public int column_bytes (int col); 395 public double column_double (int col); 396 public int column_int (int col); 397 public int64 column_int64 (int col); 398 [CCode (type = "const unsigned char*")] 399 public unowned string? column_text (int col); 400 public int column_type (int col); 401 public unowned Value column_value (int col); 402 public unowned string column_name (int index); 403 public unowned string column_database_name (int col); 404 public unowned string column_table_name (int col); 405 public unowned string column_origin_name (int col); 406 public unowned string sql (); 407 [CCode (cname = "vala_sqlite3_expanded_sql")] 408 public string? expanded_sql () { 409 string* sqlite = this._expanded_sql (); 410 string? sql = sqlite; 411 Sqlite.Memory.free ((void*) sqlite); 412 return sql; 413 } 414 [CCode (cname = "sqlite3_expanded_sql")] 415 private string? _expanded_sql (); 416 public unowned string normalized_sql (); 417 } 418 419 namespace Memory { 420 [CCode (cname = "sqlite3_malloc")] 421 public static void* malloc (int n_bytes); 422 [CCode (cname = "sqlite3_realloc")] 423 public static void* realloc (void* mem, int n_bytes); 424 [CCode (cname = "sqlite3_free")] 425 public static void free (void* mem); 426 [CCode (cname = "sqlite3_release_memory")] 427 public static int release (int bytes); 428 [CCode (cname = "sqlite3_memory_used")] 429 public static int64 used (); 430 [CCode (cname = "sqlite3_memory_highwater")] 431 public static int64 highwater (int reset = 0); 432 [Version (deprecated_since = "3.7.2", replacement = "Sqlite.Memory.soft_heap_limit64")] 433 [CCode (cname = "sqlite3_soft_heap_limit")] 434 public static void soft_heap_limit (int limit); 435 [CCode (cname = "sqlite3_soft_heap_limit64")] 436 public static int64 soft_heap_limit64 (int64 limit = -1); 437 } 438 439 [Compact] 440 [CCode (cname = "sqlite3_mutex", free_function = "sqlite3_mutex_free")] 441 public class Mutex { 442 [CCode (cname = "sqlite3_mutex_alloc")] 443 public Mutex (int mutex_type = MUTEX_RECURSIVE); 444 public void enter (); 445 public int held (); 446 public int notheld (); 447 public int @try (); 448 public void leave (); 449 } 450 451 [Compact, CCode (cname = "sqlite3_context", cprefix = "sqlite3_")] 452 public class Context { 453 public void result_blob (owned uint8[] data, GLib.DestroyNotify? destroy_notify = GLib.g_free); 454 public void result_double (double value); 455 public void result_error (string value, int error_code); 456 public void result_error_toobig (); 457 public void result_error_nomem (); 458 public void result_error_code (int error_code); 459 public void result_int (int value); 460 public void result_int64 (int64 value); 461 public void result_null (); 462 public void result_text (owned string value, int length = -1, GLib.DestroyNotify? destroy_notify = GLib.g_free); 463 public void result_value (Sqlite.Value value); 464 public void result_zeroblob (int n); 465 466 [CCode (simple_generics = true)] 467 public unowned T user_data<T> (); 468 [CCode (simple_generics = true)] 469 public void set_auxdata<T> (int N, owned T data); 470 [CCode (simple_generics = true)] 471 public unowned T get_auxdata<T> (int N); 472 [CCode (cname = "sqlite3_context_db_handle")] 473 public unowned Database db_handle (); 474 [CCode (cname = "sqlite3_aggregate_context")] 475 public void * aggregate (int n_bytes); 476 } 477 478 [Compact, CCode (cname = "sqlite3_backup", free_function = "sqlite3_backup_finish", cprefix = "sqlite3_backup_")] 479 public class Backup { 480 [CCode (cname = "sqlite3_backup_init")] 481 public Backup (Database dest, string dest_name, Database source, string source_name); 482 public int step (int nPage); 483 public int remaining (); 484 public int pagecount (); 485 } 486} 487 488