|
1 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
2 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
4 |
|
5 /** |
|
6 * This file defines an Sqlite object containing js-ctypes bindings for |
|
7 * sqlite3. It should be included from a worker thread using require. |
|
8 * |
|
9 * It serves the following purposes: |
|
10 * - opens libxul; |
|
11 * - defines sqlite3 API functions; |
|
12 * - defines the necessary sqlite3 types. |
|
13 */ |
|
14 |
|
15 "use strict"; |
|
16 |
|
17 importScripts("resource://gre/modules/workers/require.js"); |
|
18 |
|
19 let SharedAll = require( |
|
20 "resource://gre/modules/osfile/osfile_shared_allthreads.jsm"); |
|
21 |
|
22 // Open the sqlite3 library. |
|
23 let path; |
|
24 if (SharedAll.Constants.Sys.Name === "Android") { |
|
25 path = ctypes.libraryName("sqlite3"); |
|
26 } else if (SharedAll.Constants.Win) { |
|
27 path = ctypes.libraryName("nss3"); |
|
28 } else { |
|
29 path = SharedAll.Constants.Path.libxul; |
|
30 } |
|
31 |
|
32 let lib; |
|
33 try { |
|
34 lib = ctypes.open(path); |
|
35 } catch (ex) { |
|
36 throw new Error("Could not open system library: " + ex.message); |
|
37 } |
|
38 |
|
39 let declareLazyFFI = SharedAll.declareLazyFFI; |
|
40 |
|
41 let Type = Object.create(SharedAll.Type); |
|
42 |
|
43 /** |
|
44 * Opaque Structure |sqlite3_ptr|. |
|
45 * |sqlite3_ptr| is equivalent to a void*. |
|
46 */ |
|
47 Type.sqlite3_ptr = Type.voidptr_t.withName("sqlite3_ptr"); |
|
48 |
|
49 /** |
|
50 * |sqlite3_stmt_ptr| an instance of an object representing a single SQL |
|
51 * statement. |
|
52 * |sqlite3_stmt_ptr| is equivalent to a void*. |
|
53 */ |
|
54 Type.sqlite3_stmt_ptr = Type.voidptr_t.withName("sqlite3_stmt_ptr"); |
|
55 |
|
56 /** |
|
57 * |sqlite3_destructor_ptr| a constant defining a special destructor behaviour. |
|
58 * |sqlite3_destructor_ptr| is equivalent to a void*. |
|
59 */ |
|
60 Type.sqlite3_destructor_ptr = Type.voidptr_t.withName( |
|
61 "sqlite3_destructor_ptr"); |
|
62 |
|
63 /** |
|
64 * A C double. |
|
65 */ |
|
66 Type.double = new SharedAll.Type("double", ctypes.double); |
|
67 |
|
68 /** |
|
69 * |sqlite3_int64| typedef for 64-bit integer. |
|
70 */ |
|
71 Type.sqlite3_int64 = Type.int64_t.withName("sqlite3_int64"); |
|
72 |
|
73 /** |
|
74 * Sqlite3 constants. |
|
75 */ |
|
76 let Constants = {}; |
|
77 |
|
78 /** |
|
79 * |SQLITE_STATIC| a special value for the destructor that is passed as an |
|
80 * argument to routines like bind_blob, bind_text and bind_text16. It means that |
|
81 * the content pointer is constant and will never change and does need to be |
|
82 * destroyed. |
|
83 */ |
|
84 Constants.SQLITE_STATIC = Type.sqlite3_destructor_ptr.implementation(0); |
|
85 |
|
86 /** |
|
87 * |SQLITE_TRANSIENT| a special value for the destructor that is passed as an |
|
88 * argument to routines like bind_blob, bind_text and bind_text16. It means that |
|
89 * the content will likely change in the near future and that SQLite should make |
|
90 * its own private copy of the content before returning. |
|
91 */ |
|
92 Constants.SQLITE_TRANSIENT = Type.sqlite3_destructor_ptr.implementation(-1); |
|
93 |
|
94 /** |
|
95 * |SQLITE_OK| |
|
96 * Successful result. |
|
97 */ |
|
98 Constants.SQLITE_OK = 0; |
|
99 |
|
100 /** |
|
101 * |SQLITE_ROW| |
|
102 * sqlite3_step() has another row ready. |
|
103 */ |
|
104 Constants.SQLITE_ROW = 100; |
|
105 |
|
106 /** |
|
107 * |SQLITE_DONE| |
|
108 * sqlite3_step() has finished executing. |
|
109 */ |
|
110 Constants.SQLITE_DONE = 101; |
|
111 |
|
112 let Sqlite3 = { |
|
113 Constants: Constants, |
|
114 Type: Type |
|
115 }; |
|
116 |
|
117 declareLazyFFI(Sqlite3, "open", lib, "sqlite3_open", null, |
|
118 /*return*/ Type.int, |
|
119 /*path*/ Type.char.in_ptr, |
|
120 /*db handle*/ Type.sqlite3_ptr.out_ptr); |
|
121 |
|
122 declareLazyFFI(Sqlite3, "open_v2", lib, "sqlite3_open_v2", null, |
|
123 /*return*/ Type.int, |
|
124 /*path*/ Type.char.in_ptr, |
|
125 /*db handle*/ Type.sqlite3_ptr.out_ptr, |
|
126 /*flags*/ Type.int, |
|
127 /*VFS*/ Type.char.in_ptr); |
|
128 |
|
129 declareLazyFFI(Sqlite3, "close", lib, "sqlite3_close", null, |
|
130 /*return*/ Type.int, |
|
131 /*db handle*/ Type.sqlite3_ptr); |
|
132 |
|
133 declareLazyFFI(Sqlite3, "prepare_v2", lib, "sqlite3_prepare_v2", null, |
|
134 /*return*/ Type.int, |
|
135 /*db handle*/ Type.sqlite3_ptr, |
|
136 /*zSql*/ Type.char.in_ptr, |
|
137 /*nByte*/ Type.int, |
|
138 /*statement*/ Type.sqlite3_stmt_ptr.out_ptr, |
|
139 /*unused*/ Type.cstring.out_ptr); |
|
140 |
|
141 declareLazyFFI(Sqlite3, "step", lib, "sqlite3_step", null, |
|
142 /*return*/ Type.int, |
|
143 /*statement*/ Type.sqlite3_stmt_ptr); |
|
144 |
|
145 declareLazyFFI(Sqlite3, "finalize", lib, "sqlite3_finalize", null, |
|
146 /*return*/ Type.int, |
|
147 /*statement*/ Type.sqlite3_stmt_ptr); |
|
148 |
|
149 declareLazyFFI(Sqlite3, "reset", lib, "sqlite3_reset", null, |
|
150 /*return*/ Type.int, |
|
151 /*statement*/ Type.sqlite3_stmt_ptr); |
|
152 |
|
153 declareLazyFFI(Sqlite3, "column_int", lib, "sqlite3_column_int", null, |
|
154 /*return*/ Type.int, |
|
155 /*statement*/ Type.sqlite3_stmt_ptr, |
|
156 /*col*/ Type.int); |
|
157 |
|
158 declareLazyFFI(Sqlite3, "column_blob", lib, "sqlite3_column_blob", null, |
|
159 /*return*/ Type.voidptr_t, |
|
160 /*statement*/ Type.sqlite3_stmt_ptr, |
|
161 /*col*/ Type.int); |
|
162 |
|
163 declareLazyFFI(Sqlite3, "column_bytes", lib, "sqlite3_column_bytes", null, |
|
164 /*return*/ Type.int, |
|
165 /*statement*/ Type.sqlite3_stmt_ptr, |
|
166 /*col*/ Type.int); |
|
167 |
|
168 declareLazyFFI(Sqlite3, "column_bytes16", lib, "sqlite3_column_bytes16", |
|
169 null, |
|
170 /*return*/ Type.int, |
|
171 /*statement*/ Type.sqlite3_stmt_ptr, |
|
172 /*col*/ Type.int); |
|
173 |
|
174 declareLazyFFI(Sqlite3, "column_double", lib, "sqlite3_column_double", null, |
|
175 /*return*/ Type.double, |
|
176 /*statement*/ Type.sqlite3_stmt_ptr, |
|
177 /*col*/ Type.int); |
|
178 |
|
179 declareLazyFFI(Sqlite3, "column_int64", lib, "sqlite3_column_int64", null, |
|
180 /*return*/ Type.sqlite3_int64, |
|
181 /*statement*/ Type.sqlite3_stmt_ptr, |
|
182 /*col*/ Type.int); |
|
183 |
|
184 declareLazyFFI(Sqlite3, "column_text", lib, "sqlite3_column_text", null, |
|
185 /*return*/ Type.cstring, |
|
186 /*statement*/ Type.sqlite3_stmt_ptr, |
|
187 /*col*/ Type.int); |
|
188 |
|
189 declareLazyFFI(Sqlite3, "column_text16", lib, "sqlite3_column_text16", null, |
|
190 /*return*/ Type.wstring, |
|
191 /*statement*/ Type.sqlite3_stmt_ptr, |
|
192 /*col*/ Type.int); |
|
193 |
|
194 declareLazyFFI(Sqlite3, "bind_int", lib, "sqlite3_bind_int", null, |
|
195 /*return*/ Type.int, |
|
196 /*statement*/ Type.sqlite3_stmt_ptr, |
|
197 /*index*/ Type.int, |
|
198 /*value*/ Type.int); |
|
199 |
|
200 declareLazyFFI(Sqlite3, "bind_int64", lib, "sqlite3_bind_int64", null, |
|
201 /*return*/ Type.int, |
|
202 /*statement*/ Type.sqlite3_stmt_ptr, |
|
203 /*index*/ Type.int, |
|
204 /*value*/ Type.sqlite3_int64); |
|
205 |
|
206 declareLazyFFI(Sqlite3, "bind_double", lib, "sqlite3_bind_double", null, |
|
207 /*return*/ Type.int, |
|
208 /*statement*/ Type.sqlite3_stmt_ptr, |
|
209 /*index*/ Type.int, |
|
210 /*value*/ Type.double); |
|
211 |
|
212 declareLazyFFI(Sqlite3, "bind_null", lib, "sqlite3_bind_null", null, |
|
213 /*return*/ Type.int, |
|
214 /*statement*/ Type.sqlite3_stmt_ptr, |
|
215 /*index*/ Type.int); |
|
216 |
|
217 declareLazyFFI(Sqlite3, "bind_zeroblob", lib, "sqlite3_bind_zeroblob", null, |
|
218 /*return*/ Type.int, |
|
219 /*statement*/ Type.sqlite3_stmt_ptr, |
|
220 /*index*/ Type.int, |
|
221 /*nBytes*/ Type.int); |
|
222 |
|
223 declareLazyFFI(Sqlite3, "bind_text", lib, "sqlite3_bind_text", null, |
|
224 /*return*/ Type.int, |
|
225 /*statement*/ Type.sqlite3_stmt_ptr, |
|
226 /*index*/ Type.int, |
|
227 /*value*/ Type.cstring, |
|
228 /*nBytes*/ Type.int, |
|
229 /*destructor*/ Type.sqlite3_destructor_ptr); |
|
230 |
|
231 declareLazyFFI(Sqlite3, "bind_text16", lib, "sqlite3_bind_text16", null, |
|
232 /*return*/ Type.int, |
|
233 /*statement*/ Type.sqlite3_stmt_ptr, |
|
234 /*index*/ Type.int, |
|
235 /*value*/ Type.wstring, |
|
236 /*nBytes*/ Type.int, |
|
237 /*destructor*/ Type.sqlite3_destructor_ptr); |
|
238 |
|
239 declareLazyFFI(Sqlite3, "bind_blob", lib, "sqlite3_bind_blob", null, |
|
240 /*return*/ Type.int, |
|
241 /*statement*/ Type.sqlite3_stmt_ptr, |
|
242 /*index*/ Type.int, |
|
243 /*value*/ Type.voidptr_t, |
|
244 /*nBytes*/ Type.int, |
|
245 /*destructor*/ Type.sqlite3_destructor_ptr); |
|
246 |
|
247 module.exports = { |
|
248 get Constants() { |
|
249 return Sqlite3.Constants; |
|
250 }, |
|
251 get Type() { |
|
252 return Sqlite3.Type; |
|
253 }, |
|
254 get open() { |
|
255 return Sqlite3.open; |
|
256 }, |
|
257 get open_v2() { |
|
258 return Sqlite3.open_v2; |
|
259 }, |
|
260 get close() { |
|
261 return Sqlite3.close; |
|
262 }, |
|
263 get prepare_v2() { |
|
264 return Sqlite3.prepare_v2; |
|
265 }, |
|
266 get step() { |
|
267 return Sqlite3.step; |
|
268 }, |
|
269 get finalize() { |
|
270 return Sqlite3.finalize; |
|
271 }, |
|
272 get reset() { |
|
273 return Sqlite3.reset; |
|
274 }, |
|
275 get column_int() { |
|
276 return Sqlite3.column_int; |
|
277 }, |
|
278 get column_blob() { |
|
279 return Sqlite3.column_blob; |
|
280 }, |
|
281 get column_bytes() { |
|
282 return Sqlite3.column_bytes; |
|
283 }, |
|
284 get column_bytes16() { |
|
285 return Sqlite3.column_bytes16; |
|
286 }, |
|
287 get column_double() { |
|
288 return Sqlite3.column_double; |
|
289 }, |
|
290 get column_int64() { |
|
291 return Sqlite3.column_int64; |
|
292 }, |
|
293 get column_text() { |
|
294 return Sqlite3.column_text; |
|
295 }, |
|
296 get column_text16() { |
|
297 return Sqlite3.column_text16; |
|
298 }, |
|
299 get bind_int() { |
|
300 return Sqlite3.bind_int; |
|
301 }, |
|
302 get bind_int64() { |
|
303 return Sqlite3.bind_int64; |
|
304 }, |
|
305 get bind_double() { |
|
306 return Sqlite3.bind_double; |
|
307 }, |
|
308 get bind_null() { |
|
309 return Sqlite3.bind_null; |
|
310 }, |
|
311 get bind_zeroblob() { |
|
312 return Sqlite3.bind_zeroblob; |
|
313 }, |
|
314 get bind_text() { |
|
315 return Sqlite3.bind_text; |
|
316 }, |
|
317 get bind_text16() { |
|
318 return Sqlite3.bind_text16; |
|
319 }, |
|
320 get bind_blob() { |
|
321 return Sqlite3.bind_blob; |
|
322 } |
|
323 }; |