├── LICENSE └── lsqlite3.lua /LICENSE: -------------------------------------------------------------------------------- 1 | This is free and unencumbered software released into the public domain. 2 | 3 | Anyone is free to copy, modify, publish, use, compile, sell, or 4 | distribute this software, either in source code form or as a compiled 5 | binary, for any purpose, commercial or non-commercial, and by any 6 | means. 7 | 8 | In jurisdictions that recognize copyright laws, the author or authors 9 | of this software dedicate any and all copyright interest in the 10 | software to the public domain. We make this dedication for the benefit 11 | of the public at large and to the detriment of our heirs and 12 | successors. We intend this dedication to be an overt act of 13 | relinquishment in perpetuity of all present and future rights to this 14 | software under copyright law. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 19 | IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR 20 | OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 21 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 22 | OTHER DEALINGS IN THE SOFTWARE. 23 | -------------------------------------------------------------------------------- /lsqlite3.lua: -------------------------------------------------------------------------------- 1 | 2 | local ffi = require "ffi" 3 | local bit = require "bit" 4 | 5 | ffi.cdef[[ 6 | typedef struct sqlite3 sqlite3; 7 | typedef long long int sqlite_int64; 8 | typedef unsigned long long int sqlite_uint64; 9 | typedef sqlite_int64 sqlite3_int64; 10 | typedef sqlite_uint64 sqlite3_uint64; 11 | typedef int (*sqlite3_callback)(void*,int,char**, char**); 12 | typedef struct sqlite3_file sqlite3_file; 13 | typedef struct sqlite3_io_methods sqlite3_io_methods; 14 | typedef struct sqlite3_mutex sqlite3_mutex; 15 | typedef struct sqlite3_vfs sqlite3_vfs; 16 | typedef void (*sqlite3_syscall_ptr)(void); 17 | typedef struct sqlite3_mem_methods sqlite3_mem_methods; 18 | typedef struct sqlite3_stmt sqlite3_stmt; 19 | typedef struct Mem sqlite3_value; 20 | typedef struct sqlite3_context sqlite3_context; 21 | typedef void (*sqlite3_destructor_type)(void*); 22 | typedef struct sqlite3_vtab sqlite3_vtab; 23 | typedef struct sqlite3_index_info sqlite3_index_info; 24 | typedef struct sqlite3_vtab_cursor sqlite3_vtab_cursor; 25 | typedef struct sqlite3_module sqlite3_module; 26 | typedef struct sqlite3_blob sqlite3_blob; 27 | typedef struct sqlite3_mutex_methods sqlite3_mutex_methods; 28 | typedef struct sqlite3_pcache sqlite3_pcache; 29 | typedef struct sqlite3_pcache_methods sqlite3_pcache_methods; 30 | typedef struct sqlite3_backup sqlite3_backup; 31 | typedef struct sqlite3_rtree_geometry sqlite3_rtree_geometry; 32 | 33 | static const int SQLITE_VERSION_NUMBER = 3007009; 34 | static const int SQLITE_OK = 0 /* Successful result */; 35 | static const int SQLITE_ERROR = 1 /* SQL error or missing database */; 36 | static const int SQLITE_INTERNAL = 2 /* Internal logic error in SQLite */; 37 | static const int SQLITE_PERM = 3 /* Access permission denied */; 38 | static const int SQLITE_ABORT = 4 /* Callback routine requested an abort */; 39 | static const int SQLITE_BUSY = 5 /* The database file is locked */; 40 | static const int SQLITE_LOCKED = 6 /* A table in the database is locked */; 41 | static const int SQLITE_NOMEM = 7 /* A malloc() failed */; 42 | static const int SQLITE_READONLY = 8 /* Attempt to write a readonly database */; 43 | static const int SQLITE_INTERRUPT = 9 /* Operation terminated by sqlite3_interrupt()*/; 44 | static const int SQLITE_IOERR = 10 /* Some kind of disk I/O error occurred */; 45 | static const int SQLITE_CORRUPT = 11 /* The database disk image is malformed */; 46 | static const int SQLITE_NOTFOUND = 12 /* Unknown opcode in sqlite3_file_control() */; 47 | static const int SQLITE_FULL = 13 /* Insertion failed because database is full */; 48 | static const int SQLITE_CANTOPEN = 14 /* Unable to open the database file */; 49 | static const int SQLITE_PROTOCOL = 15 /* Database lock protocol error */; 50 | static const int SQLITE_EMPTY = 16 /* Database is empty */; 51 | static const int SQLITE_SCHEMA = 17 /* The database schema changed */; 52 | static const int SQLITE_TOOBIG = 18 /* String or BLOB exceeds size limit */; 53 | static const int SQLITE_CONSTRAINT = 19 /* Abort due to constraint violation */; 54 | static const int SQLITE_MISMATCH = 20 /* Data type mismatch */; 55 | static const int SQLITE_MISUSE = 21 /* Library used incorrectly */; 56 | static const int SQLITE_NOLFS = 22 /* Uses OS features not supported on host */; 57 | static const int SQLITE_AUTH = 23 /* Authorization denied */; 58 | static const int SQLITE_FORMAT = 24 /* Auxiliary database format error */; 59 | static const int SQLITE_RANGE = 25 /* 2nd parameter to sqlite3_bind out of range */; 60 | static const int SQLITE_NOTADB = 26 /* File opened that is not a database file */; 61 | static const int SQLITE_ROW = 100 /* sqlite3_step() has another row ready */; 62 | static const int SQLITE_DONE = 101 /* sqlite3_step() has finished executing */; 63 | static const int SQLITE_IOERR_READ = (SQLITE_IOERR | (1<<8)); 64 | static const int SQLITE_IOERR_SHORT_READ = (SQLITE_IOERR | (2<<8)); 65 | static const int SQLITE_IOERR_WRITE = (SQLITE_IOERR | (3<<8)); 66 | static const int SQLITE_IOERR_FSYNC = (SQLITE_IOERR | (4<<8)); 67 | static const int SQLITE_IOERR_DIR_FSYNC = (SQLITE_IOERR | (5<<8)); 68 | static const int SQLITE_IOERR_TRUNCATE = (SQLITE_IOERR | (6<<8)); 69 | static const int SQLITE_IOERR_FSTAT = (SQLITE_IOERR | (7<<8)); 70 | static const int SQLITE_IOERR_UNLOCK = (SQLITE_IOERR | (8<<8)); 71 | static const int SQLITE_IOERR_RDLOCK = (SQLITE_IOERR | (9<<8)); 72 | static const int SQLITE_IOERR_DELETE = (SQLITE_IOERR | (10<<8)); 73 | static const int SQLITE_IOERR_BLOCKED = (SQLITE_IOERR | (11<<8)); 74 | static const int SQLITE_IOERR_NOMEM = (SQLITE_IOERR | (12<<8)); 75 | static const int SQLITE_IOERR_ACCESS = (SQLITE_IOERR | (13<<8)); 76 | static const int SQLITE_IOERR_CHECKRESERVEDLOCK = (SQLITE_IOERR | (14<<8)); 77 | static const int SQLITE_IOERR_LOCK = (SQLITE_IOERR | (15<<8)); 78 | static const int SQLITE_IOERR_CLOSE = (SQLITE_IOERR | (16<<8)); 79 | static const int SQLITE_IOERR_DIR_CLOSE = (SQLITE_IOERR | (17<<8)); 80 | static const int SQLITE_IOERR_SHMOPEN = (SQLITE_IOERR | (18<<8)); 81 | static const int SQLITE_IOERR_SHMSIZE = (SQLITE_IOERR | (19<<8)); 82 | static const int SQLITE_IOERR_SHMLOCK = (SQLITE_IOERR | (20<<8)); 83 | static const int SQLITE_IOERR_SHMMAP = (SQLITE_IOERR | (21<<8)); 84 | static const int SQLITE_IOERR_SEEK = (SQLITE_IOERR | (22<<8)); 85 | static const int SQLITE_LOCKED_SHAREDCACHE = (SQLITE_LOCKED | (1<<8)); 86 | static const int SQLITE_BUSY_RECOVERY = (SQLITE_BUSY | (1<<8)); 87 | static const int SQLITE_CANTOPEN_NOTEMPDIR = (SQLITE_CANTOPEN | (1<<8)); 88 | static const int SQLITE_CORRUPT_VTAB = (SQLITE_CORRUPT | (1<<8)); 89 | static const int SQLITE_READONLY_RECOVERY = (SQLITE_READONLY | (1<<8)); 90 | static const int SQLITE_READONLY_CANTLOCK = (SQLITE_READONLY | (2<<8)); 91 | static const int SQLITE_OPEN_READONLY = 0x00000001 /* Ok for sqlite3_open_v2() */; 92 | static const int SQLITE_OPEN_READWRITE = 0x00000002 /* Ok for sqlite3_open_v2() */; 93 | static const int SQLITE_OPEN_CREATE = 0x00000004 /* Ok for sqlite3_open_v2() */; 94 | static const int SQLITE_OPEN_DELETEONCLOSE = 0x00000008 /* VFS only */; 95 | static const int SQLITE_OPEN_EXCLUSIVE = 0x00000010 /* VFS only */; 96 | static const int SQLITE_OPEN_AUTOPROXY = 0x00000020 /* VFS only */; 97 | static const int SQLITE_OPEN_URI = 0x00000040 /* Ok for sqlite3_open_v2() */; 98 | static const int SQLITE_OPEN_MAIN_DB = 0x00000100 /* VFS only */; 99 | static const int SQLITE_OPEN_TEMP_DB = 0x00000200 /* VFS only */; 100 | static const int SQLITE_OPEN_TRANSIENT_DB = 0x00000400 /* VFS only */; 101 | static const int SQLITE_OPEN_MAIN_JOURNAL = 0x00000800 /* VFS only */; 102 | static const int SQLITE_OPEN_TEMP_JOURNAL = 0x00001000 /* VFS only */; 103 | static const int SQLITE_OPEN_SUBJOURNAL = 0x00002000 /* VFS only */; 104 | static const int SQLITE_OPEN_MASTER_JOURNAL = 0x00004000 /* VFS only */; 105 | static const int SQLITE_OPEN_NOMUTEX = 0x00008000 /* Ok for sqlite3_open_v2() */; 106 | static const int SQLITE_OPEN_FULLMUTEX = 0x00010000 /* Ok for sqlite3_open_v2() */; 107 | static const int SQLITE_OPEN_SHAREDCACHE = 0x00020000 /* Ok for sqlite3_open_v2() */; 108 | static const int SQLITE_OPEN_PRIVATECACHE = 0x00040000 /* Ok for sqlite3_open_v2() */; 109 | static const int SQLITE_OPEN_WAL = 0x00080000 /* VFS only */; 110 | static const int SQLITE_IOCAP_ATOMIC = 0x00000001; 111 | static const int SQLITE_IOCAP_ATOMIC512 = 0x00000002; 112 | static const int SQLITE_IOCAP_ATOMIC1K = 0x00000004; 113 | static const int SQLITE_IOCAP_ATOMIC2K = 0x00000008; 114 | static const int SQLITE_IOCAP_ATOMIC4K = 0x00000010; 115 | static const int SQLITE_IOCAP_ATOMIC8K = 0x00000020; 116 | static const int SQLITE_IOCAP_ATOMIC16K = 0x00000040; 117 | static const int SQLITE_IOCAP_ATOMIC32K = 0x00000080; 118 | static const int SQLITE_IOCAP_ATOMIC64K = 0x00000100; 119 | static const int SQLITE_IOCAP_SAFE_APPEND = 0x00000200; 120 | static const int SQLITE_IOCAP_SEQUENTIAL = 0x00000400; 121 | static const int SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN = 0x00000800; 122 | static const int SQLITE_LOCK_NONE = 0; 123 | static const int SQLITE_LOCK_SHARED = 1; 124 | static const int SQLITE_LOCK_RESERVED = 2; 125 | static const int SQLITE_LOCK_PENDING = 3; 126 | static const int SQLITE_LOCK_EXCLUSIVE = 4; 127 | static const int SQLITE_SYNC_NORMAL = 0x00002; 128 | static const int SQLITE_SYNC_FULL = 0x00003; 129 | static const int SQLITE_SYNC_DATAONLY = 0x00010; 130 | static const int SQLITE_FCNTL_LOCKSTATE = 1; 131 | static const int SQLITE_GET_LOCKPROXYFILE = 2; 132 | static const int SQLITE_SET_LOCKPROXYFILE = 3; 133 | static const int SQLITE_LAST_ERRNO = 4; 134 | static const int SQLITE_FCNTL_SIZE_HINT = 5; 135 | static const int SQLITE_FCNTL_CHUNK_SIZE = 6; 136 | static const int SQLITE_FCNTL_FILE_POINTER = 7; 137 | static const int SQLITE_FCNTL_SYNC_OMITTED = 8; 138 | static const int SQLITE_FCNTL_WIN32_AV_RETRY = 9; 139 | static const int SQLITE_FCNTL_PERSIST_WAL = 10; 140 | static const int SQLITE_FCNTL_OVERWRITE = 11; 141 | static const int SQLITE_ACCESS_EXISTS = 0; 142 | static const int SQLITE_ACCESS_READWRITE = 1 /* Used by PRAGMA temp_store_directory */; 143 | static const int SQLITE_ACCESS_READ = 2 /* Unused */; 144 | static const int SQLITE_SHM_UNLOCK = 1; 145 | static const int SQLITE_SHM_LOCK = 2; 146 | static const int SQLITE_SHM_SHARED = 4; 147 | static const int SQLITE_SHM_EXCLUSIVE = 8; 148 | static const int SQLITE_SHM_NLOCK = 8; 149 | static const int SQLITE_CONFIG_SINGLETHREAD = 1 /* nil */; 150 | static const int SQLITE_CONFIG_MULTITHREAD = 2 /* nil */; 151 | static const int SQLITE_CONFIG_SERIALIZED = 3 /* nil */; 152 | static const int SQLITE_CONFIG_MALLOC = 4 /* sqlite3_mem_methods* */; 153 | static const int SQLITE_CONFIG_GETMALLOC = 5 /* sqlite3_mem_methods* */; 154 | static const int SQLITE_CONFIG_SCRATCH = 6 /* void*, int sz, int N */; 155 | static const int SQLITE_CONFIG_PAGECACHE = 7 /* void*, int sz, int N */; 156 | static const int SQLITE_CONFIG_HEAP = 8 /* void*, int nByte, int min */; 157 | static const int SQLITE_CONFIG_MEMSTATUS = 9 /* boolean */; 158 | static const int SQLITE_CONFIG_MUTEX = 10 /* sqlite3_mutex_methods* */; 159 | static const int SQLITE_CONFIG_GETMUTEX = 11 /* sqlite3_mutex_methods* */; 160 | static const int SQLITE_CONFIG_LOOKASIDE = 13 /* int int */; 161 | static const int SQLITE_CONFIG_PCACHE = 14 /* sqlite3_pcache_methods* */; 162 | static const int SQLITE_CONFIG_GETPCACHE = 15 /* sqlite3_pcache_methods* */; 163 | static const int SQLITE_CONFIG_LOG = 16 /* xFunc, void* */; 164 | static const int SQLITE_CONFIG_URI = 17 /* int */; 165 | static const int SQLITE_DBCONFIG_LOOKASIDE = 1001 /* void* int int */; 166 | static const int SQLITE_DBCONFIG_ENABLE_FKEY = 1002 /* int int* */; 167 | static const int SQLITE_DBCONFIG_ENABLE_TRIGGER = 1003 /* int int* */; 168 | static const int SQLITE_DENY = 1 /* Abort the SQL statement with an error */; 169 | static const int SQLITE_IGNORE = 2 /* Don't allow access, but don't generate an error */; 170 | static const int SQLITE_CREATE_INDEX = 1 /* Index Name Table Name */; 171 | static const int SQLITE_CREATE_TABLE = 2 /* Table Name NULL */; 172 | static const int SQLITE_CREATE_TEMP_INDEX = 3 /* Index Name Table Name */; 173 | static const int SQLITE_CREATE_TEMP_TABLE = 4 /* Table Name NULL */; 174 | static const int SQLITE_CREATE_TEMP_TRIGGER = 5 /* Trigger Name Table Name */; 175 | static const int SQLITE_CREATE_TEMP_VIEW = 6 /* View Name NULL */; 176 | static const int SQLITE_CREATE_TRIGGER = 7 /* Trigger Name Table Name */; 177 | static const int SQLITE_CREATE_VIEW = 8 /* View Name NULL */; 178 | static const int SQLITE_DELETE = 9 /* Table Name NULL */; 179 | static const int SQLITE_DROP_INDEX = 10 /* Index Name Table Name */; 180 | static const int SQLITE_DROP_TABLE = 11 /* Table Name NULL */; 181 | static const int SQLITE_DROP_TEMP_INDEX = 12 /* Index Name Table Name */; 182 | static const int SQLITE_DROP_TEMP_TABLE = 13 /* Table Name NULL */; 183 | static const int SQLITE_DROP_TEMP_TRIGGER = 14 /* Trigger Name Table Name */; 184 | static const int SQLITE_DROP_TEMP_VIEW = 15 /* View Name NULL */; 185 | static const int SQLITE_DROP_TRIGGER = 16 /* Trigger Name Table Name */; 186 | static const int SQLITE_DROP_VIEW = 17 /* View Name NULL */; 187 | static const int SQLITE_INSERT = 18 /* Table Name NULL */; 188 | static const int SQLITE_PRAGMA = 19 /* Pragma Name 1st arg or NULL */; 189 | static const int SQLITE_READ = 20 /* Table Name Column Name */; 190 | static const int SQLITE_SELECT = 21 /* NULL NULL */; 191 | static const int SQLITE_TRANSACTION = 22 /* Operation NULL */; 192 | static const int SQLITE_UPDATE = 23 /* Table Name Column Name */; 193 | static const int SQLITE_ATTACH = 24 /* Filename NULL */; 194 | static const int SQLITE_DETACH = 25 /* Database Name NULL */; 195 | static const int SQLITE_ALTER_TABLE = 26 /* Database Name Table Name */; 196 | static const int SQLITE_REINDEX = 27 /* Index Name NULL */; 197 | static const int SQLITE_ANALYZE = 28 /* Table Name NULL */; 198 | static const int SQLITE_CREATE_VTABLE = 29 /* Table Name Module Name */; 199 | static const int SQLITE_DROP_VTABLE = 30 /* Table Name Module Name */; 200 | static const int SQLITE_FUNCTION = 31 /* NULL Function Name */; 201 | static const int SQLITE_SAVEPOINT = 32 /* Operation Savepoint Name */; 202 | static const int SQLITE_COPY = 0 /* No longer used */; 203 | static const int SQLITE_LIMIT_LENGTH = 0; 204 | static const int SQLITE_LIMIT_SQL_LENGTH = 1; 205 | static const int SQLITE_LIMIT_COLUMN = 2; 206 | static const int SQLITE_LIMIT_EXPR_DEPTH = 3; 207 | static const int SQLITE_LIMIT_COMPOUND_SELECT = 4; 208 | static const int SQLITE_LIMIT_VDBE_OP = 5; 209 | static const int SQLITE_LIMIT_FUNCTION_ARG = 6; 210 | static const int SQLITE_LIMIT_ATTACHED = 7; 211 | static const int SQLITE_LIMIT_LIKE_PATTERN_LENGTH = 8; 212 | static const int SQLITE_LIMIT_VARIABLE_NUMBER = 9; 213 | static const int SQLITE_LIMIT_TRIGGER_DEPTH = 10; 214 | static const int SQLITE_INTEGER = 1; 215 | static const int SQLITE_FLOAT = 2; 216 | static const int SQLITE_BLOB = 4; 217 | static const int SQLITE_NULL = 5; 218 | static const int SQLITE_TEXT = 3; 219 | static const int SQLITE_UTF8 = 1; 220 | static const int SQLITE_UTF16LE = 2; 221 | static const int SQLITE_UTF16BE = 3; 222 | static const int SQLITE_UTF16 = 4 /* Use native byte order */; 223 | static const int SQLITE_ANY = 5 /* sqlite3_create_function only */; 224 | static const int SQLITE_UTF16_ALIGNED = 8 /* sqlite3_create_collation only */; 225 | static const int SQLITE_INDEX_CONSTRAINT_EQ = 2; 226 | static const int SQLITE_INDEX_CONSTRAINT_GT = 4; 227 | static const int SQLITE_INDEX_CONSTRAINT_LE = 8; 228 | static const int SQLITE_INDEX_CONSTRAINT_LT = 16; 229 | static const int SQLITE_INDEX_CONSTRAINT_GE = 32; 230 | static const int SQLITE_INDEX_CONSTRAINT_MATCH = 64; 231 | static const int SQLITE_MUTEX_FAST = 0; 232 | static const int SQLITE_MUTEX_RECURSIVE = 1; 233 | static const int SQLITE_MUTEX_STATIC_MASTER = 2; 234 | static const int SQLITE_MUTEX_STATIC_MEM = 3 /* sqlite3_malloc() */; 235 | static const int SQLITE_MUTEX_STATIC_MEM2 = 4 /* NOT USED */; 236 | static const int SQLITE_MUTEX_STATIC_OPEN = 4 /* sqlite3BtreeOpen() */; 237 | static const int SQLITE_MUTEX_STATIC_PRNG = 5 /* sqlite3_random() */; 238 | static const int SQLITE_MUTEX_STATIC_LRU = 6 /* lru page list */; 239 | static const int SQLITE_MUTEX_STATIC_LRU2 = 7 /* NOT USED */; 240 | static const int SQLITE_MUTEX_STATIC_PMEM = 7 /* sqlite3PageMalloc() */; 241 | static const int SQLITE_TESTCTRL_FIRST = 5; 242 | static const int SQLITE_TESTCTRL_PRNG_SAVE = 5; 243 | static const int SQLITE_TESTCTRL_PRNG_RESTORE = 6; 244 | static const int SQLITE_TESTCTRL_PRNG_RESET = 7; 245 | static const int SQLITE_TESTCTRL_BITVEC_TEST = 8; 246 | static const int SQLITE_TESTCTRL_FAULT_INSTALL = 9; 247 | static const int SQLITE_TESTCTRL_BENIGN_MALLOC_HOOKS = 10; 248 | static const int SQLITE_TESTCTRL_PENDING_BYTE = 11; 249 | static const int SQLITE_TESTCTRL_ASSERT = 12; 250 | static const int SQLITE_TESTCTRL_ALWAYS = 13; 251 | static const int SQLITE_TESTCTRL_RESERVE = 14; 252 | static const int SQLITE_TESTCTRL_OPTIMIZATIONS = 15; 253 | static const int SQLITE_TESTCTRL_ISKEYWORD = 16; 254 | static const int SQLITE_TESTCTRL_PGHDRSZ = 17; 255 | static const int SQLITE_TESTCTRL_SCRATCHMALLOC = 18; 256 | static const int SQLITE_TESTCTRL_LOCALTIME_FAULT = 19; 257 | static const int SQLITE_TESTCTRL_LAST = 19; 258 | static const int SQLITE_STATUS_MEMORY_USED = 0; 259 | static const int SQLITE_STATUS_PAGECACHE_USED = 1; 260 | static const int SQLITE_STATUS_PAGECACHE_OVERFLOW = 2; 261 | static const int SQLITE_STATUS_SCRATCH_USED = 3; 262 | static const int SQLITE_STATUS_SCRATCH_OVERFLOW = 4; 263 | static const int SQLITE_STATUS_MALLOC_SIZE = 5; 264 | static const int SQLITE_STATUS_PARSER_STACK = 6; 265 | static const int SQLITE_STATUS_PAGECACHE_SIZE = 7; 266 | static const int SQLITE_STATUS_SCRATCH_SIZE = 8; 267 | static const int SQLITE_STATUS_MALLOC_COUNT = 9; 268 | static const int SQLITE_DBSTATUS_LOOKASIDE_USED = 0; 269 | static const int SQLITE_DBSTATUS_CACHE_USED = 1; 270 | static const int SQLITE_DBSTATUS_SCHEMA_USED = 2; 271 | static const int SQLITE_DBSTATUS_STMT_USED = 3; 272 | static const int SQLITE_DBSTATUS_LOOKASIDE_HIT = 4; 273 | static const int SQLITE_DBSTATUS_LOOKASIDE_MISS_SIZE = 5; 274 | static const int SQLITE_DBSTATUS_LOOKASIDE_MISS_FULL = 6; 275 | static const int SQLITE_DBSTATUS_CACHE_HIT = 7; 276 | static const int SQLITE_DBSTATUS_CACHE_MISS = 8; 277 | static const int SQLITE_DBSTATUS_MAX = 8 /* Largest defined DBSTATUS */; 278 | static const int SQLITE_STMTSTATUS_FULLSCAN_STEP = 1; 279 | static const int SQLITE_STMTSTATUS_SORT = 2; 280 | static const int SQLITE_STMTSTATUS_AUTOINDEX = 3; 281 | static const int SQLITE_CHECKPOINT_PASSIVE = 0; 282 | static const int SQLITE_CHECKPOINT_FULL = 1; 283 | static const int SQLITE_CHECKPOINT_RESTART = 2; 284 | static const int SQLITE_VTAB_CONSTRAINT_SUPPORT = 1; 285 | static const int SQLITE_ROLLBACK = 1; 286 | static const int SQLITE_FAIL = 3; 287 | static const int SQLITE_REPLACE = 5; 288 | 289 | extern const char sqlite3_version[]; 290 | const char *sqlite3_libversion(void); 291 | const char *sqlite3_sourceid(void); 292 | int sqlite3_libversion_number(void); 293 | 294 | int sqlite3_compileoption_used(const char *zOptName); 295 | const char *sqlite3_compileoption_get(int N); 296 | 297 | int sqlite3_threadsafe(void); 298 | 299 | int sqlite3_close(sqlite3 *); 300 | 301 | int sqlite3_exec( 302 | sqlite3*, 303 | const char *sql, 304 | int (*callback)(void*,int,char**,char**), 305 | void *, 306 | char **errmsg 307 | ); 308 | 309 | struct sqlite3_file { 310 | const struct sqlite3_io_methods *pMethods; 311 | }; 312 | 313 | struct sqlite3_io_methods { 314 | int iVersion; 315 | int (*xClose)(sqlite3_file*); 316 | int (*xRead)(sqlite3_file*, void*, int iAmt, sqlite3_int64 iOfst); 317 | int (*xWrite)(sqlite3_file*, const void*, int iAmt, sqlite3_int64 iOfst); 318 | int (*xTruncate)(sqlite3_file*, sqlite3_int64 size); 319 | int (*xSync)(sqlite3_file*, int flags); 320 | int (*xFileSize)(sqlite3_file*, sqlite3_int64 *pSize); 321 | int (*xLock)(sqlite3_file*, int); 322 | int (*xUnlock)(sqlite3_file*, int); 323 | int (*xCheckReservedLock)(sqlite3_file*, int *pResOut); 324 | int (*xFileControl)(sqlite3_file*, int op, void *pArg); 325 | int (*xSectorSize)(sqlite3_file*); 326 | int (*xDeviceCharacteristics)(sqlite3_file*); 327 | 328 | int (*xShmMap)(sqlite3_file*, int iPg, int pgsz, int, void volatile**); 329 | int (*xShmLock)(sqlite3_file*, int offset, int n, int flags); 330 | void (*xShmBarrier)(sqlite3_file*); 331 | int (*xShmUnmap)(sqlite3_file*, int deleteFlag); 332 | 333 | 334 | }; 335 | 336 | struct sqlite3_vfs { 337 | int iVersion; 338 | int szOsFile; 339 | int mxPathname; 340 | sqlite3_vfs *pNext; 341 | const char *zName; 342 | void *pAppData; 343 | int (*xOpen)(sqlite3_vfs*, const char *zName, sqlite3_file*, 344 | int flags, int *pOutFlags); 345 | int (*xDelete)(sqlite3_vfs*, const char *zName, int syncDir); 346 | int (*xAccess)(sqlite3_vfs*, const char *zName, int flags, int *pResOut); 347 | int (*xFullPathname)(sqlite3_vfs*, const char *zName, int nOut, char *zOut); 348 | void *(*xDlOpen)(sqlite3_vfs*, const char *zFilename); 349 | void (*xDlError)(sqlite3_vfs*, int nByte, char *zErrMsg); 350 | void (*(*xDlSym)(sqlite3_vfs*,void*, const char *zSymbol))(void); 351 | void (*xDlClose)(sqlite3_vfs*, void*); 352 | int (*xRandomness)(sqlite3_vfs*, int nByte, char *zOut); 353 | int (*xSleep)(sqlite3_vfs*, int microseconds); 354 | int (*xCurrentTime)(sqlite3_vfs*, double*); 355 | int (*xGetLastError)(sqlite3_vfs*, int, char *); 356 | 357 | 358 | 359 | 360 | int (*xCurrentTimeInt64)(sqlite3_vfs*, sqlite3_int64*); 361 | 362 | 363 | 364 | 365 | int (*xSetSystemCall)(sqlite3_vfs*, const char *zName, sqlite3_syscall_ptr); 366 | sqlite3_syscall_ptr (*xGetSystemCall)(sqlite3_vfs*, const char *zName); 367 | const char *(*xNextSystemCall)(sqlite3_vfs*, const char *zName); 368 | 369 | 370 | 371 | 372 | 373 | }; 374 | 375 | int sqlite3_initialize(void); 376 | int sqlite3_shutdown(void); 377 | int sqlite3_os_init(void); 378 | int sqlite3_os_end(void); 379 | 380 | int sqlite3_config(int, ...); 381 | 382 | int sqlite3_db_config(sqlite3*, int op, ...); 383 | 384 | struct sqlite3_mem_methods { 385 | void *(*xMalloc)(int); 386 | void (*xFree)(void*); 387 | void *(*xRealloc)(void*,int); 388 | int (*xSize)(void*); 389 | int (*xRoundup)(int); 390 | int (*xInit)(void*); 391 | void (*xShutdown)(void*); 392 | void *pAppData; 393 | }; 394 | 395 | int sqlite3_extended_result_codes(sqlite3*, int onoff); 396 | 397 | sqlite3_int64 sqlite3_last_insert_rowid(sqlite3*); 398 | 399 | int sqlite3_changes(sqlite3*); 400 | 401 | int sqlite3_total_changes(sqlite3*); 402 | 403 | void sqlite3_interrupt(sqlite3*); 404 | 405 | int sqlite3_complete(const char *sql); 406 | int sqlite3_complete16(const void *sql); 407 | 408 | int sqlite3_busy_handler(sqlite3*, int(*)(void*,int), void*); 409 | 410 | int sqlite3_busy_timeout(sqlite3*, int ms); 411 | 412 | int sqlite3_get_table( 413 | sqlite3 *db, 414 | const char *zSql, 415 | char ***pazResult, 416 | int *pnRow, 417 | int *pnColumn, 418 | char **pzErrmsg 419 | ); 420 | void sqlite3_free_table(char **result); 421 | 422 | char *sqlite3_mprintf(const char*,...); 423 | char *sqlite3_vmprintf(const char*, va_list); 424 | char *sqlite3_snprintf(int,char*,const char*, ...); 425 | char *sqlite3_vsnprintf(int,char*,const char*, va_list); 426 | 427 | void *sqlite3_malloc(int); 428 | void *sqlite3_realloc(void*, int); 429 | void sqlite3_free(void*); 430 | 431 | sqlite3_int64 sqlite3_memory_used(void); 432 | sqlite3_int64 sqlite3_memory_highwater(int resetFlag); 433 | 434 | void sqlite3_randomness(int N, void *P); 435 | 436 | int sqlite3_set_authorizer( 437 | sqlite3*, 438 | int (*xAuth)(void*,int,const char*,const char*,const char*,const char*), 439 | void *pUserData 440 | ); 441 | 442 | void *sqlite3_trace(sqlite3*, void(*xTrace)(void*,const char*), void*); 443 | void *sqlite3_profile(sqlite3*, 444 | void(*xProfile)(void*,const char*,sqlite3_uint64), void*); 445 | 446 | void sqlite3_progress_handler(sqlite3*, int, int(*)(void*), void*); 447 | 448 | int sqlite3_open( 449 | const char *filename, 450 | sqlite3 **ppDb 451 | ); 452 | int sqlite3_open16( 453 | const void *filename, 454 | sqlite3 **ppDb 455 | ); 456 | int sqlite3_open_v2( 457 | const char *filename, 458 | sqlite3 **ppDb, 459 | int flags, 460 | const char *zVfs 461 | ); 462 | 463 | const char *sqlite3_uri_parameter(const char *zFilename, const char *zParam); 464 | 465 | int sqlite3_errcode(sqlite3 *db); 466 | int sqlite3_extended_errcode(sqlite3 *db); 467 | const char *sqlite3_errmsg(sqlite3*); 468 | const void *sqlite3_errmsg16(sqlite3*); 469 | 470 | int sqlite3_limit(sqlite3*, int id, int newVal); 471 | 472 | int sqlite3_prepare( 473 | sqlite3 *db, 474 | const char *zSql, 475 | int nByte, 476 | sqlite3_stmt **ppStmt, 477 | const char **pzTail 478 | ); 479 | int sqlite3_prepare_v2( 480 | sqlite3 *db, 481 | const char *zSql, 482 | int nByte, 483 | sqlite3_stmt **ppStmt, 484 | const char **pzTail 485 | ); 486 | int sqlite3_prepare16( 487 | sqlite3 *db, 488 | const void *zSql, 489 | int nByte, 490 | sqlite3_stmt **ppStmt, 491 | const void **pzTail 492 | ); 493 | int sqlite3_prepare16_v2( 494 | sqlite3 *db, 495 | const void *zSql, 496 | int nByte, 497 | sqlite3_stmt **ppStmt, 498 | const void **pzTail 499 | ); 500 | 501 | const char *sqlite3_sql(sqlite3_stmt *pStmt); 502 | 503 | int sqlite3_stmt_readonly(sqlite3_stmt *pStmt); 504 | 505 | 506 | 507 | int sqlite3_bind_blob(sqlite3_stmt*, int, const void*, int n, void(*)(void*)); 508 | int sqlite3_bind_double(sqlite3_stmt*, int, double); 509 | int sqlite3_bind_int(sqlite3_stmt*, int, int); 510 | int sqlite3_bind_int64(sqlite3_stmt*, int, sqlite3_int64); 511 | int sqlite3_bind_null(sqlite3_stmt*, int); 512 | int sqlite3_bind_text(sqlite3_stmt*, int, const char*, int n, void(*)(void*)); 513 | int sqlite3_bind_text16(sqlite3_stmt*, int, const void*, int, void(*)(void*)); 514 | int sqlite3_bind_value(sqlite3_stmt*, int, const sqlite3_value*); 515 | int sqlite3_bind_zeroblob(sqlite3_stmt*, int, int n); 516 | 517 | int sqlite3_bind_parameter_count(sqlite3_stmt*); 518 | 519 | const char *sqlite3_bind_parameter_name(sqlite3_stmt*, int); 520 | 521 | int sqlite3_bind_parameter_index(sqlite3_stmt*, const char *zName); 522 | 523 | int sqlite3_clear_bindings(sqlite3_stmt*); 524 | 525 | int sqlite3_column_count(sqlite3_stmt *pStmt); 526 | 527 | const char *sqlite3_column_name(sqlite3_stmt*, int N); 528 | const void *sqlite3_column_name16(sqlite3_stmt*, int N); 529 | 530 | const char *sqlite3_column_database_name(sqlite3_stmt*,int); 531 | const void *sqlite3_column_database_name16(sqlite3_stmt*,int); 532 | const char *sqlite3_column_table_name(sqlite3_stmt*,int); 533 | const void *sqlite3_column_table_name16(sqlite3_stmt*,int); 534 | const char *sqlite3_column_origin_name(sqlite3_stmt*,int); 535 | const void *sqlite3_column_origin_name16(sqlite3_stmt*,int); 536 | 537 | const char *sqlite3_column_decltype(sqlite3_stmt*,int); 538 | const void *sqlite3_column_decltype16(sqlite3_stmt*,int); 539 | 540 | int sqlite3_step(sqlite3_stmt*); 541 | 542 | int sqlite3_data_count(sqlite3_stmt *pStmt); 543 | 544 | const void *sqlite3_column_blob(sqlite3_stmt*, int iCol); 545 | int sqlite3_column_bytes(sqlite3_stmt*, int iCol); 546 | int sqlite3_column_bytes16(sqlite3_stmt*, int iCol); 547 | double sqlite3_column_double(sqlite3_stmt*, int iCol); 548 | int sqlite3_column_int(sqlite3_stmt*, int iCol); 549 | sqlite3_int64 sqlite3_column_int64(sqlite3_stmt*, int iCol); 550 | const unsigned char *sqlite3_column_text(sqlite3_stmt*, int iCol); 551 | const void *sqlite3_column_text16(sqlite3_stmt*, int iCol); 552 | int sqlite3_column_type(sqlite3_stmt*, int iCol); 553 | sqlite3_value *sqlite3_column_value(sqlite3_stmt*, int iCol); 554 | 555 | int sqlite3_finalize(sqlite3_stmt *pStmt); 556 | 557 | int sqlite3_reset(sqlite3_stmt *pStmt); 558 | 559 | int sqlite3_create_function( 560 | sqlite3 *db, 561 | const char *zFunctionName, 562 | int nArg, 563 | int eTextRep, 564 | void *pApp, 565 | void (*xFunc)(sqlite3_context*,int,sqlite3_value**), 566 | void (*xStep)(sqlite3_context*,int,sqlite3_value**), 567 | void (*xFinal)(sqlite3_context*) 568 | ); 569 | int sqlite3_create_function16( 570 | sqlite3 *db, 571 | const void *zFunctionName, 572 | int nArg, 573 | int eTextRep, 574 | void *pApp, 575 | void (*xFunc)(sqlite3_context*,int,sqlite3_value**), 576 | void (*xStep)(sqlite3_context*,int,sqlite3_value**), 577 | void (*xFinal)(sqlite3_context*) 578 | ); 579 | int sqlite3_create_function_v2( 580 | sqlite3 *db, 581 | const char *zFunctionName, 582 | int nArg, 583 | int eTextRep, 584 | void *pApp, 585 | void (*xFunc)(sqlite3_context*,int,sqlite3_value**), 586 | void (*xStep)(sqlite3_context*,int,sqlite3_value**), 587 | void (*xFinal)(sqlite3_context*), 588 | void(*xDestroy)(void*) 589 | ); 590 | 591 | int sqlite3_aggregate_count(sqlite3_context*); 592 | int sqlite3_expired(sqlite3_stmt*); 593 | int sqlite3_transfer_bindings(sqlite3_stmt*, sqlite3_stmt*); 594 | int sqlite3_global_recover(void); 595 | void sqlite3_thread_cleanup(void); 596 | int sqlite3_memory_alarm(void(*)(void*,sqlite3_int64,int),void*,sqlite3_int64); 597 | 598 | const void *sqlite3_value_blob(sqlite3_value*); 599 | int sqlite3_value_bytes(sqlite3_value*); 600 | int sqlite3_value_bytes16(sqlite3_value*); 601 | double sqlite3_value_double(sqlite3_value*); 602 | int sqlite3_value_int(sqlite3_value*); 603 | sqlite3_int64 sqlite3_value_int64(sqlite3_value*); 604 | const unsigned char *sqlite3_value_text(sqlite3_value*); 605 | const void *sqlite3_value_text16(sqlite3_value*); 606 | const void *sqlite3_value_text16le(sqlite3_value*); 607 | const void *sqlite3_value_text16be(sqlite3_value*); 608 | int sqlite3_value_type(sqlite3_value*); 609 | int sqlite3_value_numeric_type(sqlite3_value*); 610 | 611 | void *sqlite3_aggregate_context(sqlite3_context*, int nBytes); 612 | 613 | void *sqlite3_user_data(sqlite3_context*); 614 | 615 | sqlite3 *sqlite3_context_db_handle(sqlite3_context*); 616 | 617 | void *sqlite3_get_auxdata(sqlite3_context*, int N); 618 | void sqlite3_set_auxdata(sqlite3_context*, int N, void*, void (*)(void*)); 619 | 620 | void sqlite3_result_blob(sqlite3_context*, const void*, int, void(*)(void*)); 621 | void sqlite3_result_double(sqlite3_context*, double); 622 | void sqlite3_result_error(sqlite3_context*, const char*, int); 623 | void sqlite3_result_error16(sqlite3_context*, const void*, int); 624 | void sqlite3_result_error_toobig(sqlite3_context*); 625 | void sqlite3_result_error_nomem(sqlite3_context*); 626 | void sqlite3_result_error_code(sqlite3_context*, int); 627 | void sqlite3_result_int(sqlite3_context*, int); 628 | void sqlite3_result_int64(sqlite3_context*, sqlite3_int64); 629 | void sqlite3_result_null(sqlite3_context*); 630 | void sqlite3_result_text(sqlite3_context*, const char*, int, void(*)(void*)); 631 | void sqlite3_result_text16(sqlite3_context*, const void*, int, void(*)(void*)); 632 | void sqlite3_result_text16le(sqlite3_context*, const void*, int,void(*)(void*)); 633 | void sqlite3_result_text16be(sqlite3_context*, const void*, int,void(*)(void*)); 634 | void sqlite3_result_value(sqlite3_context*, sqlite3_value*); 635 | void sqlite3_result_zeroblob(sqlite3_context*, int n); 636 | 637 | int sqlite3_create_collation( 638 | sqlite3*, 639 | const char *zName, 640 | int eTextRep, 641 | void *pArg, 642 | int(*xCompare)(void*,int,const void*,int,const void*) 643 | ); 644 | int sqlite3_create_collation_v2( 645 | sqlite3*, 646 | const char *zName, 647 | int eTextRep, 648 | void *pArg, 649 | int(*xCompare)(void*,int,const void*,int,const void*), 650 | void(*xDestroy)(void*) 651 | ); 652 | int sqlite3_create_collation16( 653 | sqlite3*, 654 | const void *zName, 655 | int eTextRep, 656 | void *pArg, 657 | int(*xCompare)(void*,int,const void*,int,const void*) 658 | ); 659 | 660 | int sqlite3_collation_needed( 661 | sqlite3*, 662 | void*, 663 | void(*)(void*,sqlite3*,int eTextRep,const char*) 664 | ); 665 | int sqlite3_collation_needed16( 666 | sqlite3*, 667 | void*, 668 | void(*)(void*,sqlite3*,int eTextRep,const void*) 669 | ); 670 | 671 | int sqlite3_sleep(int); 672 | 673 | extern char *sqlite3_temp_directory; 674 | 675 | int sqlite3_get_autocommit(sqlite3*); 676 | 677 | sqlite3 *sqlite3_db_handle(sqlite3_stmt*); 678 | 679 | sqlite3_stmt *sqlite3_next_stmt(sqlite3 *pDb, sqlite3_stmt *pStmt); 680 | 681 | void *sqlite3_commit_hook(sqlite3*, int(*)(void*), void*); 682 | void *sqlite3_rollback_hook(sqlite3*, void(*)(void *), void*); 683 | 684 | void *sqlite3_update_hook( 685 | sqlite3*, 686 | void(*)(void *,int ,char const *,char const *,sqlite3_int64), 687 | void* 688 | ); 689 | 690 | int sqlite3_enable_shared_cache(int); 691 | 692 | int sqlite3_release_memory(int); 693 | 694 | sqlite3_int64 sqlite3_soft_heap_limit64(sqlite3_int64 N); 695 | 696 | void sqlite3_soft_heap_limit(int N); 697 | 698 | int sqlite3_table_column_metadata( 699 | sqlite3 *db, 700 | const char *zDbName, 701 | const char *zTableName, 702 | const char *zColumnName, 703 | char const **pzDataType, 704 | char const **pzCollSeq, 705 | int *pNotNull, 706 | int *pPrimaryKey, 707 | int *pAutoinc 708 | ); 709 | 710 | int sqlite3_load_extension( 711 | sqlite3 *db, 712 | const char *zFile, 713 | const char *zProc, 714 | char **pzErrMsg 715 | ); 716 | 717 | int sqlite3_enable_load_extension(sqlite3 *db, int onoff); 718 | 719 | int sqlite3_auto_extension(void (*xEntryPoint)(void)); 720 | 721 | 722 | 723 | 724 | 725 | 726 | 727 | void sqlite3_reset_auto_extension(void); 728 | 729 | struct sqlite3_module { 730 | int iVersion; 731 | int (*xCreate)(sqlite3*, void *pAux, 732 | int argc, const char *const*argv, 733 | sqlite3_vtab **ppVTab, char**); 734 | int (*xConnect)(sqlite3*, void *pAux, 735 | int argc, const char *const*argv, 736 | sqlite3_vtab **ppVTab, char**); 737 | int (*xBestIndex)(sqlite3_vtab *pVTab, sqlite3_index_info*); 738 | int (*xDisconnect)(sqlite3_vtab *pVTab); 739 | int (*xDestroy)(sqlite3_vtab *pVTab); 740 | int (*xOpen)(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor); 741 | int (*xClose)(sqlite3_vtab_cursor*); 742 | int (*xFilter)(sqlite3_vtab_cursor*, int idxNum, const char *idxStr, 743 | int argc, sqlite3_value **argv); 744 | int (*xNext)(sqlite3_vtab_cursor*); 745 | int (*xEof)(sqlite3_vtab_cursor*); 746 | int (*xColumn)(sqlite3_vtab_cursor*, sqlite3_context*, int); 747 | int (*xRowid)(sqlite3_vtab_cursor*, sqlite3_int64 *pRowid); 748 | int (*xUpdate)(sqlite3_vtab *, int, sqlite3_value **, sqlite3_int64 *); 749 | int (*xBegin)(sqlite3_vtab *pVTab); 750 | int (*xSync)(sqlite3_vtab *pVTab); 751 | int (*xCommit)(sqlite3_vtab *pVTab); 752 | int (*xRollback)(sqlite3_vtab *pVTab); 753 | int (*xFindFunction)(sqlite3_vtab *pVtab, int nArg, const char *zName, 754 | void (**pxFunc)(sqlite3_context*,int,sqlite3_value**), 755 | void **ppArg); 756 | int (*xRename)(sqlite3_vtab *pVtab, const char *zNew); 757 | 758 | 759 | int (*xSavepoint)(sqlite3_vtab *pVTab, int); 760 | int (*xRelease)(sqlite3_vtab *pVTab, int); 761 | int (*xRollbackTo)(sqlite3_vtab *pVTab, int); 762 | }; 763 | 764 | struct sqlite3_index_info { 765 | 766 | int nConstraint; 767 | struct sqlite3_index_constraint { 768 | int iColumn; 769 | unsigned char op; 770 | unsigned char usable; 771 | int iTermOffset; 772 | } *aConstraint; 773 | int nOrderBy; 774 | struct sqlite3_index_orderby { 775 | int iColumn; 776 | unsigned char desc; 777 | } *aOrderBy; 778 | 779 | struct sqlite3_index_constraint_usage { 780 | int argvIndex; 781 | unsigned char omit; 782 | } *aConstraintUsage; 783 | int idxNum; 784 | char *idxStr; 785 | int needToFreeIdxStr; 786 | int orderByConsumed; 787 | double estimatedCost; 788 | }; 789 | 790 | int sqlite3_create_module( 791 | sqlite3 *db, 792 | const char *zName, 793 | const sqlite3_module *p, 794 | void *pClientData 795 | ); 796 | int sqlite3_create_module_v2( 797 | sqlite3 *db, 798 | const char *zName, 799 | const sqlite3_module *p, 800 | void *pClientData, 801 | void(*xDestroy)(void*) 802 | ); 803 | 804 | struct sqlite3_vtab { 805 | const sqlite3_module *pModule; 806 | int nRef; 807 | char *zErrMsg; 808 | 809 | }; 810 | 811 | struct sqlite3_vtab_cursor { 812 | sqlite3_vtab *pVtab; 813 | 814 | }; 815 | 816 | int sqlite3_declare_vtab(sqlite3*, const char *zSQL); 817 | 818 | int sqlite3_overload_function(sqlite3*, const char *zFuncName, int nArg); 819 | 820 | int sqlite3_blob_open( 821 | sqlite3*, 822 | const char *zDb, 823 | const char *zTable, 824 | const char *zColumn, 825 | sqlite3_int64 iRow, 826 | int flags, 827 | sqlite3_blob **ppBlob 828 | ); 829 | 830 | int sqlite3_blob_reopen(sqlite3_blob *, sqlite3_int64); 831 | 832 | int sqlite3_blob_close(sqlite3_blob *); 833 | 834 | int sqlite3_blob_bytes(sqlite3_blob *); 835 | 836 | int sqlite3_blob_read(sqlite3_blob *, void *Z, int N, int iOffset); 837 | 838 | int sqlite3_blob_write(sqlite3_blob *, const void *z, int n, int iOffset); 839 | 840 | sqlite3_vfs *sqlite3_vfs_find(const char *zVfsName); 841 | int sqlite3_vfs_register(sqlite3_vfs*, int makeDflt); 842 | int sqlite3_vfs_unregister(sqlite3_vfs*); 843 | 844 | sqlite3_mutex *sqlite3_mutex_alloc(int); 845 | void sqlite3_mutex_free(sqlite3_mutex*); 846 | void sqlite3_mutex_enter(sqlite3_mutex*); 847 | int sqlite3_mutex_try(sqlite3_mutex*); 848 | void sqlite3_mutex_leave(sqlite3_mutex*); 849 | 850 | 851 | struct sqlite3_mutex_methods { 852 | int (*xMutexInit)(void); 853 | int (*xMutexEnd)(void); 854 | sqlite3_mutex *(*xMutexAlloc)(int); 855 | void (*xMutexFree)(sqlite3_mutex *); 856 | void (*xMutexEnter)(sqlite3_mutex *); 857 | int (*xMutexTry)(sqlite3_mutex *); 858 | void (*xMutexLeave)(sqlite3_mutex *); 859 | int (*xMutexHeld)(sqlite3_mutex *); 860 | int (*xMutexNotheld)(sqlite3_mutex *); 861 | }; 862 | 863 | int sqlite3_mutex_held(sqlite3_mutex*); 864 | int sqlite3_mutex_notheld(sqlite3_mutex*); 865 | 866 | sqlite3_mutex *sqlite3_db_mutex(sqlite3*); 867 | 868 | int sqlite3_file_control(sqlite3*, const char *zDbName, int op, void*); 869 | 870 | int sqlite3_test_control(int op, ...); 871 | 872 | int sqlite3_status(int op, int *pCurrent, int *pHighwater, int resetFlag); 873 | 874 | int sqlite3_db_status(sqlite3*, int op, int *pCur, int *pHiwtr, int resetFlg); 875 | 876 | int sqlite3_stmt_status(sqlite3_stmt*, int op,int resetFlg); 877 | 878 | 879 | struct sqlite3_pcache_methods { 880 | void *pArg; 881 | int (*xInit)(void*); 882 | void (*xShutdown)(void*); 883 | sqlite3_pcache *(*xCreate)(int szPage, int bPurgeable); 884 | void (*xCachesize)(sqlite3_pcache*, int nCachesize); 885 | int (*xPagecount)(sqlite3_pcache*); 886 | void *(*xFetch)(sqlite3_pcache*, unsigned key, int createFlag); 887 | void (*xUnpin)(sqlite3_pcache*, void*, int discard); 888 | void (*xRekey)(sqlite3_pcache*, void*, unsigned oldKey, unsigned newKey); 889 | void (*xTruncate)(sqlite3_pcache*, unsigned iLimit); 890 | void (*xDestroy)(sqlite3_pcache*); 891 | }; 892 | 893 | sqlite3_backup *sqlite3_backup_init( 894 | sqlite3 *pDest, 895 | const char *zDestName, 896 | sqlite3 *pSource, 897 | const char *zSourceName 898 | ); 899 | int sqlite3_backup_step(sqlite3_backup *p, int nPage); 900 | int sqlite3_backup_finish(sqlite3_backup *p); 901 | int sqlite3_backup_remaining(sqlite3_backup *p); 902 | int sqlite3_backup_pagecount(sqlite3_backup *p); 903 | 904 | int sqlite3_unlock_notify( 905 | sqlite3 *pBlocked, 906 | void (*xNotify)(void **apArg, int nArg), 907 | void *pNotifyArg 908 | ); 909 | 910 | int sqlite3_strnicmp(const char *, const char *, int); 911 | 912 | void sqlite3_log(int iErrCode, const char *zFormat, ...); 913 | 914 | void *sqlite3_wal_hook( 915 | sqlite3*, 916 | int(*)(void *,sqlite3*,const char*,int), 917 | void* 918 | ); 919 | 920 | int sqlite3_wal_autocheckpoint(sqlite3 *db, int N); 921 | 922 | int sqlite3_wal_checkpoint(sqlite3 *db, const char *zDb); 923 | 924 | int sqlite3_wal_checkpoint_v2( 925 | sqlite3 *db, 926 | const char *zDb, 927 | int eMode, 928 | int *pnLog, 929 | int *pnCkpt 930 | ); 931 | 932 | int sqlite3_vtab_config(sqlite3*, int op, ...); 933 | 934 | int sqlite3_vtab_on_conflict(sqlite3 *); 935 | 936 | int sqlite3_rtree_geometry_callback( 937 | sqlite3 *db, 938 | const char *zGeom, 939 | int (*xGeom)(sqlite3_rtree_geometry *, int nCoord, double *aCoord, int *pRes), 940 | void *pContext 941 | ); 942 | 943 | struct sqlite3_rtree_geometry { 944 | void *pContext; 945 | int nParam; 946 | double *aParam; 947 | void *pUser; 948 | void (*xDelUser)(void *); 949 | }; 950 | ]] 951 | 952 | local sqlite3 = ffi.load("sqlite3") 953 | local new_db_ptr = ffi.typeof("sqlite3*[1]") 954 | local new_stmt_ptr = ffi.typeof("sqlite3_stmt*[1]") 955 | local new_exec_ptr = ffi.typeof("int (*)(void*,int,char**,char**)") 956 | local new_blob_ptr = ffi.typeof("sqlite3_blob*[1]") 957 | local new_bytearr = ffi.typeof("uint8_t[?]") 958 | local sqlite3_transient = ffi.cast("void*",-1) 959 | 960 | local value_handlers = { 961 | [sqlite3.SQLITE_INTEGER] = function(stmt, n) return sqlite3.sqlite3_column_int(stmt, n) end, 962 | [sqlite3.SQLITE_FLOAT] = function(stmt, n) return sqlite3.sqlite3_column_double(stmt, n) end, 963 | [sqlite3.SQLITE_TEXT] = function(stmt, n) return ffi.string(sqlite3.sqlite3_column_text(stmt,n), sqlite3.sqlite3_column_bytes(stmt,n)-1) end, 964 | [sqlite3.SQLITE_BLOB] = function(stmt, n) return sqlite3.sqlite3_column_blob(stmt,n), sqlite3.sqlite3_column_bytes(stmt,n) end, 965 | [sqlite3.SQLITE_NULL] = function() return nil end 966 | } 967 | 968 | local lsqlite3 = {} 969 | 970 | local sqlite_db = {} 971 | sqlite_db.__index = sqlite_db 972 | function sqlite_db:__call(...) 973 | return self:exec(...) 974 | end 975 | 976 | local sqlite_stmt = {} 977 | function sqlite_stmt:__index(k) 978 | if type(k) == "number" then 979 | return sqlite_stmt.get_value(self, k) 980 | else 981 | return sqlite_stmt[k] 982 | end 983 | end 984 | function sqlite_stmt:__newindex(...) 985 | sqlite_stmt.bind(self,...) 986 | end 987 | function sqlite_stmt:__call() 988 | return self:step() 989 | end 990 | 991 | local sqlite_blob = {} 992 | sqlite_blob.__index = sqlite_blob 993 | 994 | -- Set to true to enable collecting of stack trace information when a statement or blob is created. 995 | lsqlite3.DEBUG = false 996 | 997 | -- -------------------------- Library Methods -------------------------- -- 998 | 999 | local modes = { 1000 | read = sqlite3.SQLITE_OPEN_READONLY, 1001 | write = sqlite3.SQLITE_OPEN_READWRITE, 1002 | create = bit.bor(sqlite3.SQLITE_OPEN_READWRITE, sqlite3.SQLITE_OPEN_CREATE) 1003 | } 1004 | 1005 | function lsqlite3.open(filename, mode) 1006 | local sdb = new_db_ptr() 1007 | local err = sqlite3.sqlite3_open_v2(filename, sdb, modes[mode or "create"] or error("unknown mode: "..tostring(mode),2), nil) 1008 | local db = sdb[0] 1009 | if err ~= sqlite3.SQLITE_OK then return nil, ffi.string(sqlite3.sqlite3_errmsg(db)), sqlite3.sqlite3_errcode(db) end 1010 | return setmetatable({ 1011 | db = db, 1012 | stmts = {}, 1013 | blobs = {}, 1014 | },sqlite_db) 1015 | end 1016 | function lsqlite3.open_memory() 1017 | return lsqlite3.open(":memory:") 1018 | end 1019 | 1020 | function lsqlite3.complete(str) 1021 | local r = sqlite3.sqlite3_complete(str) 1022 | if r == sqlite3.SQLITE_NOMEM then error("out of memory",2) end 1023 | return r ~= 0 and true or false 1024 | end 1025 | 1026 | function lsqlite3.version() 1027 | return ffi.string(sqlite3.sqlite3_version) 1028 | end 1029 | 1030 | -- TODO: lsqlite3.temp_directory 1031 | 1032 | lsqlite3.OK = sqlite3.SQLITE_OK 1033 | lsqlite3.ERROR = sqlite3.SQLITE_ERROR 1034 | lsqlite3.INTERNAL = sqlite3.SQLITE_INTERNAL 1035 | lsqlite3.PERM = sqlite3.SQLITE_PERM 1036 | lsqlite3.ABORT = sqlite3.SQLITE_ABORT 1037 | lsqlite3.BUSY = sqlite3.SQLITE_BUSY 1038 | lsqlite3.LOCKED = sqlite3.SQLITE_LOCKED 1039 | lsqlite3.NOMEM = sqlite3.SQLITE_NOMEM 1040 | lsqlite3.READONLY = sqlite3.SQLITE_READONLY 1041 | lsqlite3.INTERRUPT = sqlite3.SQLITE_INTERRUPT 1042 | lsqlite3.IOERR = sqlite3.SQLITE_IOERR 1043 | lsqlite3.CORRUPT = sqlite3.SQLITE_CORRUPT 1044 | lsqlite3.NOTFOUND = sqlite3.SQLITE_NOTFOUND 1045 | lsqlite3.FULL = sqlite3.SQLITE_FULL 1046 | lsqlite3.CANTOPEN = sqlite3.SQLITE_CANTOPEN 1047 | lsqlite3.PROTOCOL = sqlite3.SQLITE_PROTOCOL 1048 | lsqlite3.EMPTY = sqlite3.SQLITE_EMPTY 1049 | lsqlite3.SCHEMA = sqlite3.SQLITE_SCHEMA 1050 | lsqlite3.TOOBIG = sqlite3.SQLITE_TOOBIG 1051 | lsqlite3.CONSTRAINT = sqlite3.SQLITE_CONSTRAINT 1052 | lsqlite3.MISMATCH = sqlite3.SQLITE_MISMATCH 1053 | lsqlite3.MISUSE = sqlite3.SQLITE_MISUSE 1054 | lsqlite3.NOLFS = sqlite3.SQLITE_NOLFS 1055 | lsqlite3.FORMAT = sqlite3.SQLITE_FORMAT 1056 | lsqlite3.NOTADB = sqlite3.SQLITE_NOTADB 1057 | lsqlite3.RANGE = sqlite3.SQLITE_RANGE 1058 | lsqlite3.ROW = sqlite3.SQLITE_ROW 1059 | lsqlite3.DONE = sqlite3.SQLITE_DONE 1060 | lsqlite3.INTEGER = sqlite3.SQLITE_INTEGER 1061 | lsqlite3.FLOAT = sqlite3.SQLITE_FLOAT 1062 | lsqlite3.TEXT = sqlite3.SQLITE_TEXT 1063 | lsqlite3.BLOB = sqlite3.SQLITE_BLOB 1064 | lsqlite3.NULL = sqlite3.SQLITE_NULL 1065 | 1066 | -- -------------------------- Database Methods -------------------------- -- 1067 | 1068 | -- TODO: db:busy_handler 1069 | -- TODO: db:busy_timeout 1070 | 1071 | function sqlite_db:changes() 1072 | return sqlite3.sqlite3_changes(self.db) 1073 | end 1074 | 1075 | function sqlite_db:close() 1076 | local r = sqlite3.sqlite3_close(self.db) 1077 | if r == sqlite3.SQLITE_OK then 1078 | self.db = nil 1079 | else 1080 | self:check(r) 1081 | end 1082 | end 1083 | 1084 | -- TODO: db:close_vm 1085 | -- TODO: db:create_aggregate 1086 | -- TODO: db:create_collation 1087 | -- TODO: db:create_function 1088 | 1089 | function sqlite_db:errcode() 1090 | return sqlite3.sqlite3_extended_errcode(self.db) 1091 | end 1092 | sqlite_db.error_code = sqlite_db.errcode 1093 | 1094 | function sqlite_db:errmsg() 1095 | return ffi.string(sqlite3.sqlite3_errmsg(self.db)) 1096 | end 1097 | sqlite_db.error_message = sqlite_db.errmsg 1098 | 1099 | function sqlite_db:exec(sql, func) 1100 | if func then 1101 | local cb = ffi.cast("int (*callback)(void*,int,char**,char**)", func) 1102 | self:check(sqlite3.sqlite3_exec(self.db, sql, cb, nil, nil)) 1103 | cb:free() 1104 | else 1105 | self:check(sqlite3.sqlite3_exec(self.db, sql, nil, nil, nil)) 1106 | end 1107 | end 1108 | 1109 | function sqlite_db:interrupt() 1110 | sqlite3.sqlite3_interrupt(self.db) 1111 | end 1112 | 1113 | function sqlite_db:isopen() return self.db and true or false end 1114 | 1115 | function sqlite_db:last_insert_rowid() 1116 | return tonumber(sqlite3.sqlite3_last_insert_rowid(self.db)) 1117 | end 1118 | 1119 | -- TODO: db:nrows 1120 | 1121 | function sqlite_db:prepare(sql) 1122 | local stmtptr = new_stmt_ptr() 1123 | self:check(sqlite3.sqlite3_prepare_v2(self.db, sql, #sql+1, stmtptr, nil)) 1124 | local stmt = setmetatable( 1125 | { 1126 | stmt=stmtptr[0], 1127 | db=self, 1128 | trace=lsqlite3.DEBUG and debug.traceback() or nil 1129 | },sqlite_stmt) 1130 | self.stmts[stmt] = stmt 1131 | return stmt 1132 | end 1133 | 1134 | -- TODO: db:progress_handler 1135 | -- TODO: db:rows 1136 | 1137 | function sqlite_db:total_changes() 1138 | return sqlite3.sqlite3_total_changes(self.db) 1139 | end 1140 | 1141 | -- TODO: db:trace 1142 | -- TODO: db:urows 1143 | 1144 | function sqlite_db:check(ret) 1145 | if ret ~= sqlite3.SQLITE_OK then 1146 | error(self:errmsg(),0) 1147 | end 1148 | return ret 1149 | end 1150 | 1151 | function sqlite_db:open_blob(db, tbl, column, row, write) 1152 | local blobptr = new_blob_ptr() 1153 | self:check(sqlite3.sqlite3_blob_open(self.db, db or "main", tbl, column, row, write, blobptr)) 1154 | local blob = setmetatable( 1155 | { 1156 | blob = blobptr[0], 1157 | db = self, 1158 | trace = lsqlite3.DEBUG and debug.traceback() or nil 1159 | },sqlite_blob) 1160 | self.blobs[blob] = blob 1161 | return blob 1162 | end 1163 | 1164 | function sqlite_db:get_autocommit() 1165 | return sqlite3.sqlite3_get_autocommit(self.db) ~= 0 1166 | end 1167 | 1168 | function sqlite_db:dump_unfinalized_statements() 1169 | for _,stmt in pairs(self.stmts) do 1170 | print(tostring(stmt)) 1171 | if stmt.trace then 1172 | print("defined at: "..stmt.trace) 1173 | end 1174 | end 1175 | end 1176 | 1177 | function sqlite_db:dump_unclosed_blobs() 1178 | for _,blob in pairs(self.blobs) do 1179 | print(tostring(blob)) 1180 | if blob.trace then 1181 | print("defined at: "..blob.trace) 1182 | end 1183 | end 1184 | end 1185 | 1186 | -- -------------------------- Statement Methods -------------------------- -- 1187 | 1188 | function sqlite_stmt:bind(n, value, bloblen) 1189 | local t = type(value) 1190 | if t == "string" then 1191 | self.db:check(sqlite3.sqlite3_bind_text(self.stmt, n, value, #value+1, sqlite3_transient)) 1192 | elseif t == "number" then 1193 | self.db:check(sqlite3.sqlite3_bind_double(self.stmt, n, value)) 1194 | elseif t == "boolean" then 1195 | self.db:check(sqlite3.sqlite3_bind_int(self.stmt, n, value)) 1196 | elseif t == "nil" then 1197 | self.db:check(sqlite3.sqlite3_bind_null(self.stmt, n)) 1198 | elseif t == "cdata" then 1199 | if ffi.istype("int64_t", value) then 1200 | self.db:check(sqlite3.sqlite3_bind_int64(self.stmt, n, value)) 1201 | else 1202 | self.db:check(sqlite3.sqlite3_bind_blob(self.stmt, n, value, bloblen, sqlite3_transient)) 1203 | end 1204 | else error("invalid bind type: "..t,2) end 1205 | end 1206 | 1207 | function sqlite_stmt:bind_blob(n,value,len) 1208 | if not value then 1209 | self.db:check(sqlite3.sqlite3_bind_zeroblob(self.stmt, n, len or 0)) 1210 | elseif type(value) == "string" then 1211 | self.db:check(sqlite3.sqlite3_bind_blob(self.stmt, n, value, len or #value, sqlite3_transient)) 1212 | elseif type(value) == "cdata" then 1213 | self.db:check(sqlite3.sqlite3_bind_blob(self.stmt, n, value, len, sqlite3_transient)) 1214 | else 1215 | error("invalid bind type: "..type(value),2) 1216 | end 1217 | end 1218 | 1219 | -- TODO: stmt:bind_names 1220 | -- TODO: stmt:bind_parameter_count 1221 | -- TODO: stmt:bind_parameter_name 1222 | 1223 | function sqlite_stmt:bind_values(...) 1224 | local l=select("#",...) 1225 | for i=1,l do 1226 | self:bind(i,select(i,...)) 1227 | end 1228 | end 1229 | 1230 | function sqlite_stmt:columns() 1231 | return sqlite3.sqlite3_column_count(self.stmt) 1232 | end 1233 | 1234 | function sqlite_stmt:finalize() 1235 | local r = sqlite3.sqlite3_finalize(self.stmt) 1236 | if r == sqlite3.SQLITE_OK then 1237 | self.stmt = nil 1238 | self.db.stmts[self] = nil 1239 | else 1240 | self.db:check(r) 1241 | end 1242 | end 1243 | 1244 | function sqlite_stmt:get_name(n) 1245 | return ffi.string(sqlite3.sqlite3_column_name(self.stmt, n)) 1246 | end 1247 | 1248 | function sqlite_stmt:get_named_types() 1249 | local tbl = {} 1250 | for i=0,sqlite3.sqlite3_column_count(self.stmt)-1 do 1251 | tbl[ffi.string(sqlite3.sqlite3_column_name(self.stmt, n))] = ffi.string(sqlite3.sqlite3_column_decltype(self.stmt, n)) 1252 | end 1253 | return tbl 1254 | end 1255 | 1256 | -- TODO: stmt:get_named_values 1257 | -- TODO: stmt:get_names 1258 | -- TODO: stmt:get_unames 1259 | -- TODO: stmt:get_utypes 1260 | -- TODO: stmt:get_uvalues 1261 | 1262 | function sqlite_stmt:get_value(n) 1263 | return value_handlers[sqlite3.sqlite3_column_type(self.stmt,n)](self.stmt,n) 1264 | end 1265 | 1266 | function sqlite_stmt:get_values() 1267 | local tbl = {} 1268 | for i=0,sqlite3.sqlite3_column_count(self.stmt)-1 do 1269 | tbl[i+1] = self:get_value(i) 1270 | end 1271 | return tbl 1272 | end 1273 | 1274 | function sqlite_stmt:get_values_unpacked(n) 1275 | n = n or 0 1276 | if n < sqlite3.sqlite3_column_count(self.stmt) then 1277 | return self:get_value(n), self:get_values_unpacked(n+1) 1278 | end 1279 | end 1280 | 1281 | function sqlite_stmt:isopen() return self.stmt and true or false end 1282 | 1283 | -- TODO: stmt:nrows 1284 | 1285 | function sqlite_stmt:reset() 1286 | self.db:check(sqlite3.sqlite3_reset(self.stmt)) 1287 | end 1288 | 1289 | function sqlite_stmt:rows() 1290 | return function() 1291 | if self:step() then 1292 | return self:get_values() 1293 | else 1294 | return nil 1295 | end 1296 | end 1297 | end 1298 | 1299 | function sqlite_stmt:rows_unpacked() 1300 | return function() 1301 | if self:step() then 1302 | return self:get_values_unpacked() 1303 | else 1304 | return nil 1305 | end 1306 | end 1307 | end 1308 | 1309 | function sqlite_stmt:step() 1310 | local ret = sqlite3.sqlite3_step(self.stmt) 1311 | if ret == sqlite3.SQLITE_ROW then 1312 | return true 1313 | elseif ret == sqlite3.SQLITE_DONE then 1314 | return false 1315 | else 1316 | error(self.db:errmsg(),0) 1317 | end 1318 | end 1319 | 1320 | -- TODO: stmt:urows 1321 | 1322 | function sqlite_stmt:clear_bindings() 1323 | self.db:check(sqlite3.sqlite3_clear_bindings(self.stmt)) 1324 | end 1325 | 1326 | -- -------------------------- Blob Methods -------------------------- -- 1327 | 1328 | function sqlite_blob:read(numbytes, offset, buffer) 1329 | numbytes = numbytes or #self 1330 | buffer = buffer or new_bytearr(numbytes) 1331 | self.db:check(sqlite3.sqlite3_blob_read(self.blob, buffer, numbytes, offset or 0)) 1332 | return buffer 1333 | end 1334 | 1335 | function sqlite_blob:length() 1336 | return sqlite3.sqlite3_blob_bytes(self.blob) 1337 | end 1338 | sqlite_blob.__len = sqlite_blob.length 1339 | 1340 | function sqlite_blob:write(offset, data, datalen) 1341 | if type(data) == "string" then 1342 | datalen = #data 1343 | data = new_bytearr(datalen, data) 1344 | else assert(datalen and type(datalen) == "number") end 1345 | self.db:check(sqlite3.sqlite3_blob_write(self.blob, data, datalen, offset)) 1346 | end 1347 | 1348 | function sqlite_blob:close() 1349 | local r = sqlite3.sqlite3_blob_close(self.blob) 1350 | if r == sqlite3.SQLITE_OK then 1351 | self.blob = nil 1352 | self.db.blobs[self] = nil 1353 | else 1354 | self.db:check(r) 1355 | end 1356 | end 1357 | 1358 | function sqlite_blob:reopen(row) 1359 | self.db:check(sqlite3.sqlite3_blob_reopen(self.blob, row)) 1360 | end 1361 | 1362 | return lsqlite3 1363 | --------------------------------------------------------------------------------