1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/toolkit/components/sqlite/sqlite_internal.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,323 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 + 1.8 +/** 1.9 + * This file defines an Sqlite object containing js-ctypes bindings for 1.10 + * sqlite3. It should be included from a worker thread using require. 1.11 + * 1.12 + * It serves the following purposes: 1.13 + * - opens libxul; 1.14 + * - defines sqlite3 API functions; 1.15 + * - defines the necessary sqlite3 types. 1.16 + */ 1.17 + 1.18 +"use strict"; 1.19 + 1.20 +importScripts("resource://gre/modules/workers/require.js"); 1.21 + 1.22 +let SharedAll = require( 1.23 + "resource://gre/modules/osfile/osfile_shared_allthreads.jsm"); 1.24 + 1.25 +// Open the sqlite3 library. 1.26 +let path; 1.27 +if (SharedAll.Constants.Sys.Name === "Android") { 1.28 + path = ctypes.libraryName("sqlite3"); 1.29 +} else if (SharedAll.Constants.Win) { 1.30 + path = ctypes.libraryName("nss3"); 1.31 +} else { 1.32 + path = SharedAll.Constants.Path.libxul; 1.33 +} 1.34 + 1.35 +let lib; 1.36 +try { 1.37 + lib = ctypes.open(path); 1.38 +} catch (ex) { 1.39 + throw new Error("Could not open system library: " + ex.message); 1.40 +} 1.41 + 1.42 +let declareLazyFFI = SharedAll.declareLazyFFI; 1.43 + 1.44 +let Type = Object.create(SharedAll.Type); 1.45 + 1.46 +/** 1.47 + * Opaque Structure |sqlite3_ptr|. 1.48 + * |sqlite3_ptr| is equivalent to a void*. 1.49 + */ 1.50 +Type.sqlite3_ptr = Type.voidptr_t.withName("sqlite3_ptr"); 1.51 + 1.52 +/** 1.53 + * |sqlite3_stmt_ptr| an instance of an object representing a single SQL 1.54 + * statement. 1.55 + * |sqlite3_stmt_ptr| is equivalent to a void*. 1.56 + */ 1.57 +Type.sqlite3_stmt_ptr = Type.voidptr_t.withName("sqlite3_stmt_ptr"); 1.58 + 1.59 +/** 1.60 + * |sqlite3_destructor_ptr| a constant defining a special destructor behaviour. 1.61 + * |sqlite3_destructor_ptr| is equivalent to a void*. 1.62 + */ 1.63 +Type.sqlite3_destructor_ptr = Type.voidptr_t.withName( 1.64 + "sqlite3_destructor_ptr"); 1.65 + 1.66 +/** 1.67 + * A C double. 1.68 + */ 1.69 +Type.double = new SharedAll.Type("double", ctypes.double); 1.70 + 1.71 +/** 1.72 + * |sqlite3_int64| typedef for 64-bit integer. 1.73 + */ 1.74 +Type.sqlite3_int64 = Type.int64_t.withName("sqlite3_int64"); 1.75 + 1.76 +/** 1.77 + * Sqlite3 constants. 1.78 + */ 1.79 +let Constants = {}; 1.80 + 1.81 +/** 1.82 + * |SQLITE_STATIC| a special value for the destructor that is passed as an 1.83 + * argument to routines like bind_blob, bind_text and bind_text16. It means that 1.84 + * the content pointer is constant and will never change and does need to be 1.85 + * destroyed. 1.86 + */ 1.87 +Constants.SQLITE_STATIC = Type.sqlite3_destructor_ptr.implementation(0); 1.88 + 1.89 +/** 1.90 + * |SQLITE_TRANSIENT| a special value for the destructor that is passed as an 1.91 + * argument to routines like bind_blob, bind_text and bind_text16. It means that 1.92 + * the content will likely change in the near future and that SQLite should make 1.93 + * its own private copy of the content before returning. 1.94 + */ 1.95 +Constants.SQLITE_TRANSIENT = Type.sqlite3_destructor_ptr.implementation(-1); 1.96 + 1.97 +/** 1.98 + * |SQLITE_OK| 1.99 + * Successful result. 1.100 + */ 1.101 +Constants.SQLITE_OK = 0; 1.102 + 1.103 +/** 1.104 + * |SQLITE_ROW| 1.105 + * sqlite3_step() has another row ready. 1.106 + */ 1.107 +Constants.SQLITE_ROW = 100; 1.108 + 1.109 +/** 1.110 + * |SQLITE_DONE| 1.111 + * sqlite3_step() has finished executing. 1.112 + */ 1.113 +Constants.SQLITE_DONE = 101; 1.114 + 1.115 +let Sqlite3 = { 1.116 + Constants: Constants, 1.117 + Type: Type 1.118 +}; 1.119 + 1.120 +declareLazyFFI(Sqlite3, "open", lib, "sqlite3_open", null, 1.121 + /*return*/ Type.int, 1.122 + /*path*/ Type.char.in_ptr, 1.123 + /*db handle*/ Type.sqlite3_ptr.out_ptr); 1.124 + 1.125 +declareLazyFFI(Sqlite3, "open_v2", lib, "sqlite3_open_v2", null, 1.126 + /*return*/ Type.int, 1.127 + /*path*/ Type.char.in_ptr, 1.128 + /*db handle*/ Type.sqlite3_ptr.out_ptr, 1.129 + /*flags*/ Type.int, 1.130 + /*VFS*/ Type.char.in_ptr); 1.131 + 1.132 +declareLazyFFI(Sqlite3, "close", lib, "sqlite3_close", null, 1.133 + /*return*/ Type.int, 1.134 + /*db handle*/ Type.sqlite3_ptr); 1.135 + 1.136 +declareLazyFFI(Sqlite3, "prepare_v2", lib, "sqlite3_prepare_v2", null, 1.137 + /*return*/ Type.int, 1.138 + /*db handle*/ Type.sqlite3_ptr, 1.139 + /*zSql*/ Type.char.in_ptr, 1.140 + /*nByte*/ Type.int, 1.141 + /*statement*/ Type.sqlite3_stmt_ptr.out_ptr, 1.142 + /*unused*/ Type.cstring.out_ptr); 1.143 + 1.144 +declareLazyFFI(Sqlite3, "step", lib, "sqlite3_step", null, 1.145 + /*return*/ Type.int, 1.146 + /*statement*/ Type.sqlite3_stmt_ptr); 1.147 + 1.148 +declareLazyFFI(Sqlite3, "finalize", lib, "sqlite3_finalize", null, 1.149 + /*return*/ Type.int, 1.150 + /*statement*/ Type.sqlite3_stmt_ptr); 1.151 + 1.152 +declareLazyFFI(Sqlite3, "reset", lib, "sqlite3_reset", null, 1.153 + /*return*/ Type.int, 1.154 + /*statement*/ Type.sqlite3_stmt_ptr); 1.155 + 1.156 +declareLazyFFI(Sqlite3, "column_int", lib, "sqlite3_column_int", null, 1.157 + /*return*/ Type.int, 1.158 + /*statement*/ Type.sqlite3_stmt_ptr, 1.159 + /*col*/ Type.int); 1.160 + 1.161 +declareLazyFFI(Sqlite3, "column_blob", lib, "sqlite3_column_blob", null, 1.162 + /*return*/ Type.voidptr_t, 1.163 + /*statement*/ Type.sqlite3_stmt_ptr, 1.164 + /*col*/ Type.int); 1.165 + 1.166 +declareLazyFFI(Sqlite3, "column_bytes", lib, "sqlite3_column_bytes", null, 1.167 + /*return*/ Type.int, 1.168 + /*statement*/ Type.sqlite3_stmt_ptr, 1.169 + /*col*/ Type.int); 1.170 + 1.171 +declareLazyFFI(Sqlite3, "column_bytes16", lib, "sqlite3_column_bytes16", 1.172 + null, 1.173 + /*return*/ Type.int, 1.174 + /*statement*/ Type.sqlite3_stmt_ptr, 1.175 + /*col*/ Type.int); 1.176 + 1.177 +declareLazyFFI(Sqlite3, "column_double", lib, "sqlite3_column_double", null, 1.178 + /*return*/ Type.double, 1.179 + /*statement*/ Type.sqlite3_stmt_ptr, 1.180 + /*col*/ Type.int); 1.181 + 1.182 +declareLazyFFI(Sqlite3, "column_int64", lib, "sqlite3_column_int64", null, 1.183 + /*return*/ Type.sqlite3_int64, 1.184 + /*statement*/ Type.sqlite3_stmt_ptr, 1.185 + /*col*/ Type.int); 1.186 + 1.187 +declareLazyFFI(Sqlite3, "column_text", lib, "sqlite3_column_text", null, 1.188 + /*return*/ Type.cstring, 1.189 + /*statement*/ Type.sqlite3_stmt_ptr, 1.190 + /*col*/ Type.int); 1.191 + 1.192 +declareLazyFFI(Sqlite3, "column_text16", lib, "sqlite3_column_text16", null, 1.193 + /*return*/ Type.wstring, 1.194 + /*statement*/ Type.sqlite3_stmt_ptr, 1.195 + /*col*/ Type.int); 1.196 + 1.197 +declareLazyFFI(Sqlite3, "bind_int", lib, "sqlite3_bind_int", null, 1.198 + /*return*/ Type.int, 1.199 + /*statement*/ Type.sqlite3_stmt_ptr, 1.200 + /*index*/ Type.int, 1.201 + /*value*/ Type.int); 1.202 + 1.203 +declareLazyFFI(Sqlite3, "bind_int64", lib, "sqlite3_bind_int64", null, 1.204 + /*return*/ Type.int, 1.205 + /*statement*/ Type.sqlite3_stmt_ptr, 1.206 + /*index*/ Type.int, 1.207 + /*value*/ Type.sqlite3_int64); 1.208 + 1.209 +declareLazyFFI(Sqlite3, "bind_double", lib, "sqlite3_bind_double", null, 1.210 + /*return*/ Type.int, 1.211 + /*statement*/ Type.sqlite3_stmt_ptr, 1.212 + /*index*/ Type.int, 1.213 + /*value*/ Type.double); 1.214 + 1.215 +declareLazyFFI(Sqlite3, "bind_null", lib, "sqlite3_bind_null", null, 1.216 + /*return*/ Type.int, 1.217 + /*statement*/ Type.sqlite3_stmt_ptr, 1.218 + /*index*/ Type.int); 1.219 + 1.220 +declareLazyFFI(Sqlite3, "bind_zeroblob", lib, "sqlite3_bind_zeroblob", null, 1.221 + /*return*/ Type.int, 1.222 + /*statement*/ Type.sqlite3_stmt_ptr, 1.223 + /*index*/ Type.int, 1.224 + /*nBytes*/ Type.int); 1.225 + 1.226 +declareLazyFFI(Sqlite3, "bind_text", lib, "sqlite3_bind_text", null, 1.227 + /*return*/ Type.int, 1.228 + /*statement*/ Type.sqlite3_stmt_ptr, 1.229 + /*index*/ Type.int, 1.230 + /*value*/ Type.cstring, 1.231 + /*nBytes*/ Type.int, 1.232 + /*destructor*/ Type.sqlite3_destructor_ptr); 1.233 + 1.234 +declareLazyFFI(Sqlite3, "bind_text16", lib, "sqlite3_bind_text16", null, 1.235 + /*return*/ Type.int, 1.236 + /*statement*/ Type.sqlite3_stmt_ptr, 1.237 + /*index*/ Type.int, 1.238 + /*value*/ Type.wstring, 1.239 + /*nBytes*/ Type.int, 1.240 + /*destructor*/ Type.sqlite3_destructor_ptr); 1.241 + 1.242 +declareLazyFFI(Sqlite3, "bind_blob", lib, "sqlite3_bind_blob", null, 1.243 + /*return*/ Type.int, 1.244 + /*statement*/ Type.sqlite3_stmt_ptr, 1.245 + /*index*/ Type.int, 1.246 + /*value*/ Type.voidptr_t, 1.247 + /*nBytes*/ Type.int, 1.248 + /*destructor*/ Type.sqlite3_destructor_ptr); 1.249 + 1.250 +module.exports = { 1.251 + get Constants() { 1.252 + return Sqlite3.Constants; 1.253 + }, 1.254 + get Type() { 1.255 + return Sqlite3.Type; 1.256 + }, 1.257 + get open() { 1.258 + return Sqlite3.open; 1.259 + }, 1.260 + get open_v2() { 1.261 + return Sqlite3.open_v2; 1.262 + }, 1.263 + get close() { 1.264 + return Sqlite3.close; 1.265 + }, 1.266 + get prepare_v2() { 1.267 + return Sqlite3.prepare_v2; 1.268 + }, 1.269 + get step() { 1.270 + return Sqlite3.step; 1.271 + }, 1.272 + get finalize() { 1.273 + return Sqlite3.finalize; 1.274 + }, 1.275 + get reset() { 1.276 + return Sqlite3.reset; 1.277 + }, 1.278 + get column_int() { 1.279 + return Sqlite3.column_int; 1.280 + }, 1.281 + get column_blob() { 1.282 + return Sqlite3.column_blob; 1.283 + }, 1.284 + get column_bytes() { 1.285 + return Sqlite3.column_bytes; 1.286 + }, 1.287 + get column_bytes16() { 1.288 + return Sqlite3.column_bytes16; 1.289 + }, 1.290 + get column_double() { 1.291 + return Sqlite3.column_double; 1.292 + }, 1.293 + get column_int64() { 1.294 + return Sqlite3.column_int64; 1.295 + }, 1.296 + get column_text() { 1.297 + return Sqlite3.column_text; 1.298 + }, 1.299 + get column_text16() { 1.300 + return Sqlite3.column_text16; 1.301 + }, 1.302 + get bind_int() { 1.303 + return Sqlite3.bind_int; 1.304 + }, 1.305 + get bind_int64() { 1.306 + return Sqlite3.bind_int64; 1.307 + }, 1.308 + get bind_double() { 1.309 + return Sqlite3.bind_double; 1.310 + }, 1.311 + get bind_null() { 1.312 + return Sqlite3.bind_null; 1.313 + }, 1.314 + get bind_zeroblob() { 1.315 + return Sqlite3.bind_zeroblob; 1.316 + }, 1.317 + get bind_text() { 1.318 + return Sqlite3.bind_text; 1.319 + }, 1.320 + get bind_text16() { 1.321 + return Sqlite3.bind_text16; 1.322 + }, 1.323 + get bind_blob() { 1.324 + return Sqlite3.bind_blob; 1.325 + } 1.326 +};