@rocicorp/zero-sqlite3 1.0.8 → 1.0.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,120 @@
1
+ Backup::Backup(
2
+ Database* db,
3
+ sqlite3* dest_handle,
4
+ sqlite3_backup* backup_handle,
5
+ sqlite3_uint64 id,
6
+ bool unlink
7
+ ) :
8
+ node::ObjectWrap(),
9
+ db(db),
10
+ dest_handle(dest_handle),
11
+ backup_handle(backup_handle),
12
+ id(id),
13
+ alive(true),
14
+ unlink(unlink) {
15
+ assert(db != NULL);
16
+ assert(dest_handle != NULL);
17
+ assert(backup_handle != NULL);
18
+ db->AddBackup(this);
19
+ }
20
+
21
+ Backup::~Backup() {
22
+ if (alive) db->RemoveBackup(this);
23
+ CloseHandles();
24
+ }
25
+
26
+ // Whenever this is used, db->RemoveBackup must be invoked beforehand.
27
+ void Backup::CloseHandles() {
28
+ if (alive) {
29
+ alive = false;
30
+ std::string filename(sqlite3_db_filename(dest_handle, "main"));
31
+ sqlite3_backup_finish(backup_handle);
32
+ int status = sqlite3_close(dest_handle);
33
+ assert(status == SQLITE_OK); ((void)status);
34
+ if (unlink) remove(filename.c_str());
35
+ }
36
+ }
37
+
38
+ INIT(Backup::Init) {
39
+ v8::Local<v8::FunctionTemplate> t = NewConstructorTemplate(isolate, data, JS_new, "Backup");
40
+ SetPrototypeMethod(isolate, data, t, "transfer", JS_transfer);
41
+ SetPrototypeMethod(isolate, data, t, "close", JS_close);
42
+ return t->GetFunction(OnlyContext).ToLocalChecked();
43
+ }
44
+
45
+ NODE_METHOD(Backup::JS_new) {
46
+ UseAddon;
47
+ if (!addon->privileged_info) return ThrowTypeError("Disabled constructor");
48
+ assert(info.IsConstructCall());
49
+ Database* db = Unwrap<Database>(addon->privileged_info->This());
50
+ REQUIRE_DATABASE_OPEN(db->GetState());
51
+ REQUIRE_DATABASE_NOT_BUSY(db->GetState());
52
+
53
+ v8::Local<v8::Object> database = (*addon->privileged_info)[0].As<v8::Object>();
54
+ v8::Local<v8::String> attachedName = (*addon->privileged_info)[1].As<v8::String>();
55
+ v8::Local<v8::String> destFile = (*addon->privileged_info)[2].As<v8::String>();
56
+ bool unlink = (*addon->privileged_info)[3].As<v8::Boolean>()->Value();
57
+
58
+ UseIsolate;
59
+ sqlite3* dest_handle;
60
+ v8::String::Utf8Value dest_file(isolate, destFile);
61
+ v8::String::Utf8Value attached_name(isolate, attachedName);
62
+ int mask = (SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE);
63
+
64
+ if (sqlite3_open_v2(*dest_file, &dest_handle, mask, NULL) != SQLITE_OK) {
65
+ Database::ThrowSqliteError(addon, dest_handle);
66
+ int status = sqlite3_close(dest_handle);
67
+ assert(status == SQLITE_OK); ((void)status);
68
+ return;
69
+ }
70
+
71
+ sqlite3_extended_result_codes(dest_handle, 1);
72
+ sqlite3_limit(dest_handle, SQLITE_LIMIT_LENGTH, INT_MAX);
73
+ sqlite3_backup* backup_handle = sqlite3_backup_init(dest_handle, "main", db->GetHandle(), *attached_name);
74
+ if (backup_handle == NULL) {
75
+ Database::ThrowSqliteError(addon, dest_handle);
76
+ int status = sqlite3_close(dest_handle);
77
+ assert(status == SQLITE_OK); ((void)status);
78
+ return;
79
+ }
80
+
81
+ Backup* backup = new Backup(db, dest_handle, backup_handle, addon->NextId(), unlink);
82
+ backup->Wrap(info.This());
83
+ SetFrozen(isolate, OnlyContext, info.This(), addon->cs.database, database);
84
+
85
+ info.GetReturnValue().Set(info.This());
86
+ }
87
+
88
+ NODE_METHOD(Backup::JS_transfer) {
89
+ Backup* backup = Unwrap<Backup>(info.This());
90
+ REQUIRE_ARGUMENT_INT32(first, int pages);
91
+ REQUIRE_DATABASE_OPEN(backup->db->GetState());
92
+ assert(backup->db->GetState()->busy == false);
93
+ assert(backup->alive == true);
94
+
95
+ sqlite3_backup* backup_handle = backup->backup_handle;
96
+ int status = sqlite3_backup_step(backup_handle, pages) & 0xff;
97
+
98
+ Addon* addon = backup->db->GetAddon();
99
+ if (status == SQLITE_OK || status == SQLITE_DONE || status == SQLITE_BUSY) {
100
+ int total_pages = sqlite3_backup_pagecount(backup_handle);
101
+ int remaining_pages = sqlite3_backup_remaining(backup_handle);
102
+ UseIsolate;
103
+ UseContext;
104
+ v8::Local<v8::Object> result = v8::Object::New(isolate);
105
+ result->Set(ctx, addon->cs.totalPages.Get(isolate), v8::Int32::New(isolate, total_pages)).FromJust();
106
+ result->Set(ctx, addon->cs.remainingPages.Get(isolate), v8::Int32::New(isolate, remaining_pages)).FromJust();
107
+ info.GetReturnValue().Set(result);
108
+ if (status == SQLITE_DONE) backup->unlink = false;
109
+ } else {
110
+ Database::ThrowSqliteError(addon, sqlite3_errstr(status), status);
111
+ }
112
+ }
113
+
114
+ NODE_METHOD(Backup::JS_close) {
115
+ Backup* backup = Unwrap<Backup>(info.This());
116
+ assert(backup->db->GetState()->busy == false);
117
+ if (backup->alive) backup->db->RemoveBackup(backup);
118
+ backup->CloseHandles();
119
+ info.GetReturnValue().Set(info.This());
120
+ }
@@ -0,0 +1,36 @@
1
+ class Backup : public node::ObjectWrap {
2
+ public:
3
+
4
+ ~Backup();
5
+
6
+ // Whenever this is used, db->RemoveBackup must be invoked beforehand.
7
+ void CloseHandles();
8
+
9
+ // Used to support ordered containers.
10
+ static inline bool Compare(Backup const * const a, Backup const * const b) {
11
+ return a->id < b->id;
12
+ }
13
+
14
+ static INIT(Init);
15
+
16
+ private:
17
+
18
+ explicit Backup(
19
+ Database* db,
20
+ sqlite3* dest_handle,
21
+ sqlite3_backup* backup_handle,
22
+ sqlite3_uint64 id,
23
+ bool unlink
24
+ );
25
+
26
+ static NODE_METHOD(JS_new);
27
+ static NODE_METHOD(JS_transfer);
28
+ static NODE_METHOD(JS_close);
29
+
30
+ Database* const db;
31
+ sqlite3* const dest_handle;
32
+ sqlite3_backup* const backup_handle;
33
+ const sqlite3_uint64 id;
34
+ bool alive;
35
+ bool unlink;
36
+ };
@@ -0,0 +1,392 @@
1
+ const int Database::MAX_BUFFER_SIZE = (
2
+ node::Buffer::kMaxLength > INT_MAX
3
+ ? INT_MAX
4
+ : static_cast<int>(node::Buffer::kMaxLength)
5
+ );
6
+
7
+ const int Database::MAX_STRING_SIZE = (
8
+ v8::String::kMaxLength > INT_MAX
9
+ ? INT_MAX
10
+ : static_cast<int>(v8::String::kMaxLength)
11
+ );
12
+
13
+ Database::Database(
14
+ v8::Isolate* isolate,
15
+ Addon* addon,
16
+ sqlite3* db_handle,
17
+ v8::Local<v8::Value> logger
18
+ ) :
19
+ node::ObjectWrap(),
20
+ db_handle(db_handle),
21
+ open(true),
22
+ busy(false),
23
+ safe_ints(false),
24
+ unsafe_mode(false),
25
+ was_js_error(false),
26
+ has_logger(logger->IsFunction()),
27
+ iterators(0),
28
+ addon(addon),
29
+ logger(isolate, logger),
30
+ stmts(),
31
+ backups() {
32
+ assert(db_handle != NULL);
33
+ addon->dbs.insert(this);
34
+ }
35
+
36
+ Database::~Database() {
37
+ if (open) addon->dbs.erase(this);
38
+ CloseHandles();
39
+ }
40
+
41
+ // Whenever this is used, addon->dbs.erase() must be invoked beforehand.
42
+ void Database::CloseHandles() {
43
+ if (open) {
44
+ open = false;
45
+ for (Statement* stmt : stmts) stmt->CloseHandles();
46
+ for (Backup* backup : backups) backup->CloseHandles();
47
+ stmts.clear();
48
+ backups.clear();
49
+ int status = sqlite3_close(db_handle);
50
+ assert(status == SQLITE_OK); ((void)status);
51
+ }
52
+ }
53
+
54
+ void Database::ThrowDatabaseError() {
55
+ if (was_js_error) was_js_error = false;
56
+ else ThrowSqliteError(addon, db_handle);
57
+ }
58
+
59
+ void Database::ThrowSqliteError(Addon* addon, sqlite3* db_handle) {
60
+ assert(db_handle != NULL);
61
+ ThrowSqliteError(addon, sqlite3_errmsg(db_handle), sqlite3_extended_errcode(db_handle));
62
+ }
63
+
64
+ void Database::ThrowSqliteError(Addon* addon, const char* message, int code) {
65
+ assert(message != NULL);
66
+ assert((code & 0xff) != SQLITE_OK);
67
+ assert((code & 0xff) != SQLITE_ROW);
68
+ assert((code & 0xff) != SQLITE_DONE);
69
+ EasyIsolate;
70
+ v8::Local<v8::Value> args[2] = {
71
+ StringFromUtf8(isolate, message, -1),
72
+ addon->cs.Code(isolate, code)
73
+ };
74
+ isolate->ThrowException(addon->SqliteError.Get(isolate)
75
+ ->NewInstance(OnlyContext, 2, args)
76
+ .ToLocalChecked());
77
+ }
78
+
79
+ // Allows Statements to log their executed SQL.
80
+ bool Database::Log(v8::Isolate* isolate, sqlite3_stmt* handle) {
81
+ assert(was_js_error == false);
82
+ if (!has_logger) return false;
83
+ char* expanded = sqlite3_expanded_sql(handle);
84
+ v8::Local<v8::Value> arg = StringFromUtf8(isolate, expanded ? expanded : sqlite3_sql(handle), -1);
85
+ was_js_error = logger.Get(isolate).As<v8::Function>()
86
+ ->Call(OnlyContext, v8::Undefined(isolate), 1, &arg)
87
+ .IsEmpty();
88
+ if (expanded) sqlite3_free(expanded);
89
+ return was_js_error;
90
+ }
91
+
92
+ bool Database::Deserialize(
93
+ v8::Local<v8::Object> buffer,
94
+ Addon* addon,
95
+ sqlite3* db_handle,
96
+ bool readonly
97
+ ) {
98
+ size_t length = node::Buffer::Length(buffer);
99
+ unsigned char* data = (unsigned char*)sqlite3_malloc64(length);
100
+ unsigned int flags = SQLITE_DESERIALIZE_FREEONCLOSE | SQLITE_DESERIALIZE_RESIZEABLE;
101
+
102
+ if (readonly) {
103
+ flags |= SQLITE_DESERIALIZE_READONLY;
104
+ }
105
+ if (length) {
106
+ if (!data) {
107
+ ThrowError("Out of memory");
108
+ return false;
109
+ }
110
+ memcpy(data, node::Buffer::Data(buffer), length);
111
+ }
112
+
113
+ int status = sqlite3_deserialize(db_handle, "main", data, length, length, flags);
114
+ if (status != SQLITE_OK) {
115
+ ThrowSqliteError(addon, status == SQLITE_ERROR ? "unable to deserialize database" : sqlite3_errstr(status), status);
116
+ return false;
117
+ }
118
+
119
+ return true;
120
+ }
121
+
122
+ void Database::FreeSerialization(char* data, void* _) {
123
+ sqlite3_free(data);
124
+ }
125
+
126
+ INIT(Database::Init) {
127
+ v8::Local<v8::FunctionTemplate> t = NewConstructorTemplate(isolate, data, JS_new, "Database");
128
+ SetPrototypeMethod(isolate, data, t, "prepare", JS_prepare);
129
+ SetPrototypeMethod(isolate, data, t, "exec", JS_exec);
130
+ SetPrototypeMethod(isolate, data, t, "backup", JS_backup);
131
+ SetPrototypeMethod(isolate, data, t, "serialize", JS_serialize);
132
+ SetPrototypeMethod(isolate, data, t, "function", JS_function);
133
+ SetPrototypeMethod(isolate, data, t, "aggregate", JS_aggregate);
134
+ SetPrototypeMethod(isolate, data, t, "table", JS_table);
135
+ SetPrototypeMethod(isolate, data, t, "close", JS_close);
136
+ SetPrototypeMethod(isolate, data, t, "defaultSafeIntegers", JS_defaultSafeIntegers);
137
+ SetPrototypeMethod(isolate, data, t, "unsafeMode", JS_unsafeMode);
138
+ SetPrototypeGetter(isolate, data, t, "open", JS_open);
139
+ SetPrototypeGetter(isolate, data, t, "inTransaction", JS_inTransaction);
140
+ return t->GetFunction(OnlyContext).ToLocalChecked();
141
+ }
142
+
143
+ NODE_METHOD(Database::JS_new) {
144
+ assert(info.IsConstructCall());
145
+ REQUIRE_ARGUMENT_STRING(first, v8::Local<v8::String> filename);
146
+ REQUIRE_ARGUMENT_STRING(second, v8::Local<v8::String> filenameGiven);
147
+ REQUIRE_ARGUMENT_BOOLEAN(third, bool in_memory);
148
+ REQUIRE_ARGUMENT_BOOLEAN(fourth, bool readonly);
149
+ REQUIRE_ARGUMENT_BOOLEAN(fifth, bool must_exist);
150
+ REQUIRE_ARGUMENT_INT32(sixth, int timeout);
151
+ REQUIRE_ARGUMENT_ANY(seventh, v8::Local<v8::Value> logger);
152
+ REQUIRE_ARGUMENT_ANY(eighth, v8::Local<v8::Value> buffer);
153
+
154
+ UseAddon;
155
+ UseIsolate;
156
+ sqlite3* db_handle;
157
+ v8::String::Utf8Value utf8(isolate, filename);
158
+ int mask = readonly ? SQLITE_OPEN_READONLY
159
+ : must_exist ? SQLITE_OPEN_READWRITE
160
+ : (SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE);
161
+
162
+ if (sqlite3_open_v2(*utf8, &db_handle, mask, NULL) != SQLITE_OK) {
163
+ ThrowSqliteError(addon, db_handle);
164
+ int status = sqlite3_close(db_handle);
165
+ assert(status == SQLITE_OK); ((void)status);
166
+ return;
167
+ }
168
+
169
+ assert(sqlite3_db_mutex(db_handle) == NULL);
170
+ sqlite3_extended_result_codes(db_handle, 1);
171
+ sqlite3_busy_timeout(db_handle, timeout);
172
+ sqlite3_limit(db_handle, SQLITE_LIMIT_LENGTH, MAX_BUFFER_SIZE < MAX_STRING_SIZE ? MAX_BUFFER_SIZE : MAX_STRING_SIZE);
173
+ sqlite3_limit(db_handle, SQLITE_LIMIT_SQL_LENGTH, MAX_STRING_SIZE);
174
+ int status = sqlite3_db_config(db_handle, SQLITE_DBCONFIG_DEFENSIVE, 1, NULL);
175
+ assert(status == SQLITE_OK); ((void)status);
176
+
177
+ if (node::Buffer::HasInstance(buffer) && !Deserialize(buffer.As<v8::Object>(), addon, db_handle, readonly)) {
178
+ int status = sqlite3_close(db_handle);
179
+ assert(status == SQLITE_OK); ((void)status);
180
+ return;
181
+ }
182
+
183
+ UseContext;
184
+ Database* db = new Database(isolate, addon, db_handle, logger);
185
+ db->Wrap(info.This());
186
+ SetFrozen(isolate, ctx, info.This(), addon->cs.memory, v8::Boolean::New(isolate, in_memory));
187
+ SetFrozen(isolate, ctx, info.This(), addon->cs.readonly, v8::Boolean::New(isolate, readonly));
188
+ SetFrozen(isolate, ctx, info.This(), addon->cs.name, filenameGiven);
189
+
190
+ info.GetReturnValue().Set(info.This());
191
+ }
192
+
193
+ NODE_METHOD(Database::JS_prepare) {
194
+ REQUIRE_ARGUMENT_STRING(first, v8::Local<v8::String> source);
195
+ REQUIRE_ARGUMENT_OBJECT(second, v8::Local<v8::Object> database);
196
+ REQUIRE_ARGUMENT_BOOLEAN(third, bool pragmaMode);
197
+ (void)source;
198
+ (void)database;
199
+ (void)pragmaMode;
200
+ UseAddon;
201
+ UseIsolate;
202
+ v8::Local<v8::Function> c = addon->Statement.Get(isolate);
203
+ addon->privileged_info = &info;
204
+ v8::MaybeLocal<v8::Object> maybeStatement = c->NewInstance(OnlyContext, 0, NULL);
205
+ addon->privileged_info = NULL;
206
+ if (!maybeStatement.IsEmpty()) info.GetReturnValue().Set(maybeStatement.ToLocalChecked());
207
+ }
208
+
209
+ NODE_METHOD(Database::JS_exec) {
210
+ Database* db = Unwrap<Database>(info.This());
211
+ REQUIRE_ARGUMENT_STRING(first, v8::Local<v8::String> source);
212
+ REQUIRE_DATABASE_OPEN(db);
213
+ REQUIRE_DATABASE_NOT_BUSY(db);
214
+ REQUIRE_DATABASE_NO_ITERATORS_UNLESS_UNSAFE(db);
215
+ db->busy = true;
216
+
217
+ UseIsolate;
218
+ v8::String::Utf8Value utf8(isolate, source);
219
+ const char* sql = *utf8;
220
+ const char* tail;
221
+
222
+ int status;
223
+ const bool has_logger = db->has_logger;
224
+ sqlite3* const db_handle = db->db_handle;
225
+ sqlite3_stmt* handle;
226
+
227
+ for (;;) {
228
+ while (IS_SKIPPED(*sql)) ++sql;
229
+ status = sqlite3_prepare_v2(db_handle, sql, -1, &handle, &tail);
230
+ sql = tail;
231
+ if (!handle) break;
232
+ if (has_logger && db->Log(isolate, handle)) {
233
+ sqlite3_finalize(handle);
234
+ status = -1;
235
+ break;
236
+ }
237
+ do status = sqlite3_step(handle);
238
+ while (status == SQLITE_ROW);
239
+ status = sqlite3_finalize(handle);
240
+ if (status != SQLITE_OK) break;
241
+ }
242
+
243
+ db->busy = false;
244
+ if (status != SQLITE_OK) {
245
+ db->ThrowDatabaseError();
246
+ }
247
+ }
248
+
249
+ NODE_METHOD(Database::JS_backup) {
250
+ REQUIRE_ARGUMENT_OBJECT(first, v8::Local<v8::Object> database);
251
+ REQUIRE_ARGUMENT_STRING(second, v8::Local<v8::String> attachedName);
252
+ REQUIRE_ARGUMENT_STRING(third, v8::Local<v8::String> destFile);
253
+ REQUIRE_ARGUMENT_BOOLEAN(fourth, bool unlink);
254
+ (void)database;
255
+ (void)attachedName;
256
+ (void)destFile;
257
+ (void)unlink;
258
+ UseAddon;
259
+ UseIsolate;
260
+ v8::Local<v8::Function> c = addon->Backup.Get(isolate);
261
+ addon->privileged_info = &info;
262
+ v8::MaybeLocal<v8::Object> maybeBackup = c->NewInstance(OnlyContext, 0, NULL);
263
+ addon->privileged_info = NULL;
264
+ if (!maybeBackup.IsEmpty()) info.GetReturnValue().Set(maybeBackup.ToLocalChecked());
265
+ }
266
+
267
+ NODE_METHOD(Database::JS_serialize) {
268
+ Database* db = Unwrap<Database>(info.This());
269
+ REQUIRE_ARGUMENT_STRING(first, v8::Local<v8::String> attachedName);
270
+ REQUIRE_DATABASE_OPEN(db);
271
+ REQUIRE_DATABASE_NOT_BUSY(db);
272
+ REQUIRE_DATABASE_NO_ITERATORS(db);
273
+
274
+ UseIsolate;
275
+ v8::String::Utf8Value attached_name(isolate, attachedName);
276
+ sqlite3_int64 length = -1;
277
+ unsigned char* data = sqlite3_serialize(db->db_handle, *attached_name, &length, 0);
278
+
279
+ if (!data && length) {
280
+ ThrowError("Out of memory");
281
+ return;
282
+ }
283
+
284
+ info.GetReturnValue().Set(
285
+ SAFE_NEW_BUFFER(isolate, reinterpret_cast<char*>(data), length, FreeSerialization, NULL).ToLocalChecked()
286
+ );
287
+ }
288
+
289
+ NODE_METHOD(Database::JS_function) {
290
+ Database* db = Unwrap<Database>(info.This());
291
+ REQUIRE_ARGUMENT_FUNCTION(first, v8::Local<v8::Function> fn);
292
+ REQUIRE_ARGUMENT_STRING(second, v8::Local<v8::String> nameString);
293
+ REQUIRE_ARGUMENT_INT32(third, int argc);
294
+ REQUIRE_ARGUMENT_INT32(fourth, int safe_ints);
295
+ REQUIRE_ARGUMENT_BOOLEAN(fifth, bool deterministic);
296
+ REQUIRE_ARGUMENT_BOOLEAN(sixth, bool direct_only);
297
+ REQUIRE_DATABASE_OPEN(db);
298
+ REQUIRE_DATABASE_NOT_BUSY(db);
299
+ REQUIRE_DATABASE_NO_ITERATORS(db);
300
+
301
+ UseIsolate;
302
+ v8::String::Utf8Value name(isolate, nameString);
303
+ int mask = SQLITE_UTF8;
304
+ if (deterministic) mask |= SQLITE_DETERMINISTIC;
305
+ if (direct_only) mask |= SQLITE_DIRECTONLY;
306
+ safe_ints = safe_ints < 2 ? safe_ints : static_cast<int>(db->safe_ints);
307
+
308
+ if (sqlite3_create_function_v2(db->db_handle, *name, argc, mask, new CustomFunction(isolate, db, *name, fn, safe_ints), CustomFunction::xFunc, NULL, NULL, CustomFunction::xDestroy) != SQLITE_OK) {
309
+ db->ThrowDatabaseError();
310
+ }
311
+ }
312
+
313
+ NODE_METHOD(Database::JS_aggregate) {
314
+ Database* db = Unwrap<Database>(info.This());
315
+ REQUIRE_ARGUMENT_ANY(first, v8::Local<v8::Value> start);
316
+ REQUIRE_ARGUMENT_FUNCTION(second, v8::Local<v8::Function> step);
317
+ REQUIRE_ARGUMENT_ANY(third, v8::Local<v8::Value> inverse);
318
+ REQUIRE_ARGUMENT_ANY(fourth, v8::Local<v8::Value> result);
319
+ REQUIRE_ARGUMENT_STRING(fifth, v8::Local<v8::String> nameString);
320
+ REQUIRE_ARGUMENT_INT32(sixth, int argc);
321
+ REQUIRE_ARGUMENT_INT32(seventh, int safe_ints);
322
+ REQUIRE_ARGUMENT_BOOLEAN(eighth, bool deterministic);
323
+ REQUIRE_ARGUMENT_BOOLEAN(ninth, bool direct_only);
324
+ REQUIRE_DATABASE_OPEN(db);
325
+ REQUIRE_DATABASE_NOT_BUSY(db);
326
+ REQUIRE_DATABASE_NO_ITERATORS(db);
327
+
328
+ UseIsolate;
329
+ v8::String::Utf8Value name(isolate, nameString);
330
+ auto xInverse = inverse->IsFunction() ? CustomAggregate::xInverse : NULL;
331
+ auto xValue = xInverse ? CustomAggregate::xValue : NULL;
332
+ int mask = SQLITE_UTF8;
333
+ if (deterministic) mask |= SQLITE_DETERMINISTIC;
334
+ if (direct_only) mask |= SQLITE_DIRECTONLY;
335
+ safe_ints = safe_ints < 2 ? safe_ints : static_cast<int>(db->safe_ints);
336
+
337
+ if (sqlite3_create_window_function(db->db_handle, *name, argc, mask, new CustomAggregate(isolate, db, *name, start, step, inverse, result, safe_ints), CustomAggregate::xStep, CustomAggregate::xFinal, xValue, xInverse, CustomAggregate::xDestroy) != SQLITE_OK) {
338
+ db->ThrowDatabaseError();
339
+ }
340
+ }
341
+
342
+ NODE_METHOD(Database::JS_table) {
343
+ Database* db = Unwrap<Database>(info.This());
344
+ REQUIRE_ARGUMENT_FUNCTION(first, v8::Local<v8::Function> factory);
345
+ REQUIRE_ARGUMENT_STRING(second, v8::Local<v8::String> nameString);
346
+ REQUIRE_ARGUMENT_BOOLEAN(third, bool eponymous);
347
+ REQUIRE_DATABASE_OPEN(db);
348
+ REQUIRE_DATABASE_NOT_BUSY(db);
349
+ REQUIRE_DATABASE_NO_ITERATORS(db);
350
+
351
+ UseIsolate;
352
+ v8::String::Utf8Value name(isolate, nameString);
353
+ sqlite3_module* module = eponymous ? &CustomTable::EPONYMOUS_MODULE : &CustomTable::MODULE;
354
+
355
+ db->busy = true;
356
+ if (sqlite3_create_module_v2(db->db_handle, *name, module, new CustomTable(isolate, db, *name, factory), CustomTable::Destructor) != SQLITE_OK) {
357
+ db->ThrowDatabaseError();
358
+ }
359
+ db->busy = false;
360
+ }
361
+
362
+ NODE_METHOD(Database::JS_close) {
363
+ Database* db = Unwrap<Database>(info.This());
364
+ if (db->open) {
365
+ REQUIRE_DATABASE_NOT_BUSY(db);
366
+ REQUIRE_DATABASE_NO_ITERATORS(db);
367
+ db->addon->dbs.erase(db);
368
+ db->CloseHandles();
369
+ }
370
+ }
371
+
372
+ NODE_METHOD(Database::JS_defaultSafeIntegers) {
373
+ Database* db = Unwrap<Database>(info.This());
374
+ if (info.Length() == 0) db->safe_ints = true;
375
+ else { REQUIRE_ARGUMENT_BOOLEAN(first, db->safe_ints); }
376
+ }
377
+
378
+ NODE_METHOD(Database::JS_unsafeMode) {
379
+ Database* db = Unwrap<Database>(info.This());
380
+ if (info.Length() == 0) db->unsafe_mode = true;
381
+ else { REQUIRE_ARGUMENT_BOOLEAN(first, db->unsafe_mode); }
382
+ sqlite3_db_config(db->db_handle, SQLITE_DBCONFIG_DEFENSIVE, static_cast<int>(!db->unsafe_mode), NULL);
383
+ }
384
+
385
+ NODE_GETTER(Database::JS_open) {
386
+ info.GetReturnValue().Set(Unwrap<Database>(info.This())->open);
387
+ }
388
+
389
+ NODE_GETTER(Database::JS_inTransaction) {
390
+ Database* db = Unwrap<Database>(info.This());
391
+ info.GetReturnValue().Set(db->open && !static_cast<bool>(sqlite3_get_autocommit(db->db_handle)));
392
+ }
@@ -0,0 +1,102 @@
1
+ class Database : public node::ObjectWrap {
2
+ public:
3
+
4
+ ~Database();
5
+
6
+ // Whenever this is used, addon->dbs.erase() must be invoked beforehand.
7
+ void CloseHandles();
8
+
9
+ // Used to support ordered containers.
10
+ class CompareDatabase { public:
11
+ inline bool operator() (Database const * const a, Database const * const b) const {
12
+ return a < b;
13
+ }
14
+ };
15
+ class CompareStatement { public:
16
+ inline bool operator() (Statement const * const a, Statement const * const b) const {
17
+ return Statement::Compare(a, b);
18
+ }
19
+ };
20
+ class CompareBackup { public:
21
+ inline bool operator() (Backup const * const a, Backup const * const b) const {
22
+ return Backup::Compare(a, b);
23
+ }
24
+ };
25
+
26
+ // Proper error handling logic for when an sqlite3 operation fails.
27
+ void ThrowDatabaseError();
28
+ static void ThrowSqliteError(Addon* addon, sqlite3* db_handle);
29
+ static void ThrowSqliteError(Addon* addon, const char* message, int code);
30
+
31
+ // Allows Statements to log their executed SQL.
32
+ bool Log(v8::Isolate* isolate, sqlite3_stmt* handle);
33
+
34
+ // Allow Statements to manage themselves when created and garbage collected.
35
+ inline void AddStatement(Statement* stmt) { stmts.insert(stmts.end(), stmt); }
36
+ inline void RemoveStatement(Statement* stmt) { stmts.erase(stmt); }
37
+
38
+ // Allow Backups to manage themselves when created and garbage collected.
39
+ inline void AddBackup(Backup* backup) { backups.insert(backups.end(), backup); }
40
+ inline void RemoveBackup(Backup* backup) { backups.erase(backup); }
41
+
42
+ // A view for Statements to see and modify Database state.
43
+ // The order of these fields must exactly match their actual order.
44
+ struct State {
45
+ const bool open;
46
+ bool busy;
47
+ const bool safe_ints;
48
+ const bool unsafe_mode;
49
+ bool was_js_error;
50
+ const bool has_logger;
51
+ unsigned short iterators;
52
+ Addon* const addon;
53
+ };
54
+
55
+ inline State* GetState() { return reinterpret_cast<State*>(&open); }
56
+ inline sqlite3* GetHandle() { return db_handle; }
57
+ inline Addon* GetAddon() { return addon; }
58
+
59
+ static INIT(Init);
60
+
61
+ private:
62
+
63
+ explicit Database(
64
+ v8::Isolate* isolate,
65
+ Addon* addon,
66
+ sqlite3* db_handle,
67
+ v8::Local<v8::Value> logger
68
+ );
69
+
70
+ static NODE_METHOD(JS_new);
71
+ static NODE_METHOD(JS_prepare);
72
+ static NODE_METHOD(JS_exec);
73
+ static NODE_METHOD(JS_backup);
74
+ static NODE_METHOD(JS_serialize);
75
+ static NODE_METHOD(JS_function);
76
+ static NODE_METHOD(JS_aggregate);
77
+ static NODE_METHOD(JS_table);
78
+ static NODE_METHOD(JS_close);
79
+ static NODE_METHOD(JS_defaultSafeIntegers);
80
+ static NODE_METHOD(JS_unsafeMode);
81
+ static NODE_GETTER(JS_open);
82
+ static NODE_GETTER(JS_inTransaction);
83
+
84
+ static bool Deserialize(v8::Local<v8::Object> buffer, Addon* addon, sqlite3* db_handle, bool readonly);
85
+ static void FreeSerialization(char* data, void* _);
86
+
87
+ static const int MAX_BUFFER_SIZE;
88
+ static const int MAX_STRING_SIZE;
89
+
90
+ sqlite3* const db_handle;
91
+ bool open;
92
+ bool busy;
93
+ bool safe_ints;
94
+ bool unsafe_mode;
95
+ bool was_js_error;
96
+ const bool has_logger;
97
+ unsigned short iterators;
98
+ Addon* const addon;
99
+ const v8::Global<v8::Value> logger;
100
+ std::set<Statement*, CompareStatement> stmts;
101
+ std::set<Backup*, CompareBackup> backups;
102
+ };