@homeofthings/sqlite3 0.0.1-alpha0 → 6.1.0
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.
- package/LICENSE +25 -0
- package/README.md +209 -14
- package/binding.gyp +58 -0
- package/deps/common-sqlite.gypi +60 -0
- package/deps/extract.js +19 -0
- package/deps/sqlite-autoconf-3510300.tar.gz +0 -0
- package/deps/sqlite3.gyp +121 -0
- package/lib/sqlite3-binding.js +1 -0
- package/lib/sqlite3.d.ts +205 -0
- package/lib/sqlite3.js +207 -0
- package/lib/trace.js +38 -0
- package/package.json +79 -3
- package/src/async.h +76 -0
- package/src/backup.cc +418 -0
- package/src/backup.h +209 -0
- package/src/database.cc +751 -0
- package/src/database.h +188 -0
- package/src/gcc-preinclude.h +31 -0
- package/src/macros.h +207 -0
- package/src/node_sqlite3.cc +128 -0
- package/src/statement.cc +939 -0
- package/src/statement.h +244 -0
- package/src/threading.h +10 -0
- package/index.js +0 -1
package/src/backup.cc
ADDED
|
@@ -0,0 +1,418 @@
|
|
|
1
|
+
#include <cstring>
|
|
2
|
+
#include <napi.h>
|
|
3
|
+
#include "macros.h"
|
|
4
|
+
#include "database.h"
|
|
5
|
+
#include "backup.h"
|
|
6
|
+
|
|
7
|
+
using namespace node_sqlite3;
|
|
8
|
+
|
|
9
|
+
Napi::Object Backup::Init(Napi::Env env, Napi::Object exports) {
|
|
10
|
+
Napi::HandleScope scope(env);
|
|
11
|
+
|
|
12
|
+
// declare napi_default_method here as it is only available in Node v14.12.0+
|
|
13
|
+
auto napi_default_method = static_cast<napi_property_attributes>(napi_writable | napi_configurable);
|
|
14
|
+
|
|
15
|
+
auto t = DefineClass(env, "Backup", {
|
|
16
|
+
InstanceMethod("step", &Backup::Step, napi_default_method),
|
|
17
|
+
InstanceMethod("finish", &Backup::Finish, napi_default_method),
|
|
18
|
+
InstanceAccessor("idle", &Backup::IdleGetter, nullptr),
|
|
19
|
+
InstanceAccessor("completed", &Backup::CompletedGetter, nullptr),
|
|
20
|
+
InstanceAccessor("failed", &Backup::FailedGetter, nullptr),
|
|
21
|
+
InstanceAccessor("remaining", &Backup::RemainingGetter, nullptr),
|
|
22
|
+
InstanceAccessor("pageCount", &Backup::PageCountGetter, nullptr),
|
|
23
|
+
InstanceAccessor("retryErrors", &Backup::RetryErrorGetter, &Backup::RetryErrorSetter),
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
exports.Set("Backup", t);
|
|
27
|
+
return exports;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
void Backup::Process() {
|
|
31
|
+
if (finished && !queue.empty()) {
|
|
32
|
+
return CleanQueue();
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
while (inited && !locked && !queue.empty()) {
|
|
36
|
+
auto call = std::move(queue.front());
|
|
37
|
+
queue.pop();
|
|
38
|
+
|
|
39
|
+
call->callback(call->baton);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
void Backup::Schedule(Work_Callback callback, Baton* baton) {
|
|
44
|
+
if (finished) {
|
|
45
|
+
queue.emplace(new Call(callback, baton));
|
|
46
|
+
CleanQueue();
|
|
47
|
+
}
|
|
48
|
+
else if (!inited || locked || !queue.empty()) {
|
|
49
|
+
queue.emplace(new Call(callback, baton));
|
|
50
|
+
}
|
|
51
|
+
else {
|
|
52
|
+
callback(baton);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
template <class T> void Backup::Error(T* baton) {
|
|
57
|
+
auto env = baton->backup->Env();
|
|
58
|
+
Napi::HandleScope scope(env);
|
|
59
|
+
|
|
60
|
+
Backup* backup = baton->backup;
|
|
61
|
+
// Fail hard on logic errors.
|
|
62
|
+
assert(backup->status != 0);
|
|
63
|
+
EXCEPTION(Napi::String::New(env, backup->message), backup->status, exception);
|
|
64
|
+
|
|
65
|
+
Napi::Function cb = baton->callback.Value();
|
|
66
|
+
|
|
67
|
+
if (!cb.IsEmpty() && cb.IsFunction()) {
|
|
68
|
+
Napi::Value argv[] = { exception };
|
|
69
|
+
TRY_CATCH_CALL(backup->Value(), cb, 1, argv);
|
|
70
|
+
}
|
|
71
|
+
else {
|
|
72
|
+
Napi::Value argv[] = { Napi::String::New(env, "error"), exception };
|
|
73
|
+
EMIT_EVENT(backup->Value(), 2, argv);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
void Backup::CleanQueue() {
|
|
78
|
+
auto env = this->Env();
|
|
79
|
+
Napi::HandleScope scope(env);
|
|
80
|
+
|
|
81
|
+
if (inited && !queue.empty()) {
|
|
82
|
+
// This backup has already been initialized and is now finished.
|
|
83
|
+
// Fire error for all remaining items in the queue.
|
|
84
|
+
EXCEPTION(Napi::String::New(env, "Backup is already finished"), SQLITE_MISUSE, exception);
|
|
85
|
+
Napi::Value argv[] = { exception };
|
|
86
|
+
bool called = false;
|
|
87
|
+
|
|
88
|
+
// Clear out the queue so that this object can get GC'ed.
|
|
89
|
+
while (!queue.empty()) {
|
|
90
|
+
auto call = std::move(queue.front());
|
|
91
|
+
queue.pop();
|
|
92
|
+
|
|
93
|
+
std::unique_ptr<Baton> baton(call->baton);
|
|
94
|
+
Napi::Function cb = baton->callback.Value();
|
|
95
|
+
|
|
96
|
+
if (inited && !cb.IsEmpty() &&
|
|
97
|
+
cb.IsFunction()) {
|
|
98
|
+
TRY_CATCH_CALL(Value(), cb, 1, argv);
|
|
99
|
+
called = true;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
// When we couldn't call a callback function, emit an error on the
|
|
104
|
+
// Backup object.
|
|
105
|
+
if (!called) {
|
|
106
|
+
Napi::Value info[] = { Napi::String::New(env, "error"), exception };
|
|
107
|
+
EMIT_EVENT(Value(), 2, info);
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
else while (!queue.empty()) {
|
|
111
|
+
// Just delete all items in the queue; we already fired an event when
|
|
112
|
+
// initializing the backup failed.
|
|
113
|
+
auto call = std::move(queue.front());
|
|
114
|
+
queue.pop();
|
|
115
|
+
|
|
116
|
+
// We don't call the actual callback, so we have to make sure that
|
|
117
|
+
// the baton gets destroyed.
|
|
118
|
+
delete call->baton;
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
Backup::Backup(const Napi::CallbackInfo& info) : Napi::ObjectWrap<Backup>(info) {
|
|
123
|
+
auto env = info.Env();
|
|
124
|
+
if (!info.IsConstructCall()) {
|
|
125
|
+
Napi::TypeError::New(env, "Use the new operator to create new Backup objects").ThrowAsJavaScriptException();
|
|
126
|
+
return;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
auto length = info.Length();
|
|
130
|
+
|
|
131
|
+
if (length <= 0 || !Database::HasInstance(info[0])) {
|
|
132
|
+
Napi::TypeError::New(env, "Database object expected").ThrowAsJavaScriptException();
|
|
133
|
+
return;
|
|
134
|
+
}
|
|
135
|
+
else if (length <= 1 || !info[1].IsString()) {
|
|
136
|
+
Napi::TypeError::New(env, "Filename expected").ThrowAsJavaScriptException();
|
|
137
|
+
return;
|
|
138
|
+
}
|
|
139
|
+
else if (length <= 2 || !info[2].IsString()) {
|
|
140
|
+
Napi::TypeError::New(env, "Source database name expected").ThrowAsJavaScriptException();
|
|
141
|
+
return;
|
|
142
|
+
}
|
|
143
|
+
else if (length <= 3 || !info[3].IsString()) {
|
|
144
|
+
Napi::TypeError::New(env, "Destination database name expected").ThrowAsJavaScriptException();
|
|
145
|
+
return;
|
|
146
|
+
}
|
|
147
|
+
else if (length <= 4 || !info[4].IsBoolean()) {
|
|
148
|
+
Napi::TypeError::New(env, "Direction flag expected").ThrowAsJavaScriptException();
|
|
149
|
+
return;
|
|
150
|
+
}
|
|
151
|
+
else if (length > 5 && !info[5].IsUndefined() && !info[5].IsFunction()) {
|
|
152
|
+
Napi::TypeError::New(env, "Callback expected").ThrowAsJavaScriptException();
|
|
153
|
+
return;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
this->db = Napi::ObjectWrap<Database>::Unwrap(info[0].As<Napi::Object>());
|
|
157
|
+
this->db->Ref();
|
|
158
|
+
|
|
159
|
+
auto filename = info[1].As<Napi::String>();
|
|
160
|
+
auto sourceName = info[2].As<Napi::String>();
|
|
161
|
+
auto destName = info[3].As<Napi::String>();
|
|
162
|
+
auto filenameIsDest = info[4].As<Napi::Boolean>();
|
|
163
|
+
|
|
164
|
+
info.This().As<Napi::Object>().DefineProperty(Napi::PropertyDescriptor::Value("filename", filename));
|
|
165
|
+
info.This().As<Napi::Object>().DefineProperty(Napi::PropertyDescriptor::Value("sourceName", sourceName));
|
|
166
|
+
info.This().As<Napi::Object>().DefineProperty(Napi::PropertyDescriptor::Value("destName", destName));
|
|
167
|
+
info.This().As<Napi::Object>().DefineProperty(Napi::PropertyDescriptor::Value("filenameIsDest", filenameIsDest));
|
|
168
|
+
|
|
169
|
+
auto* baton = new InitializeBaton(this->db, info[5].As<Napi::Function>(), this);
|
|
170
|
+
baton->filename = filename.Utf8Value();
|
|
171
|
+
baton->sourceName = sourceName.Utf8Value();
|
|
172
|
+
baton->destName = destName.Utf8Value();
|
|
173
|
+
baton->filenameIsDest = filenameIsDest.Value();
|
|
174
|
+
|
|
175
|
+
this->db->Schedule(Work_BeginInitialize, baton);
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
void Backup::Work_BeginInitialize(Database::Baton* baton) {
|
|
179
|
+
assert(baton->db->open);
|
|
180
|
+
baton->db->pending++;
|
|
181
|
+
auto env = baton->db->Env();
|
|
182
|
+
CREATE_WORK("sqlite3.Backup.Initialize", Work_Initialize, Work_AfterInitialize);
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
void Backup::Work_Initialize(napi_env e, void* data) {
|
|
186
|
+
BACKUP_INIT(InitializeBaton);
|
|
187
|
+
|
|
188
|
+
// In case stepping fails, we use a mutex to make sure we get the associated
|
|
189
|
+
// error message.
|
|
190
|
+
auto* mtx = sqlite3_db_mutex(baton->db->_handle);
|
|
191
|
+
sqlite3_mutex_enter(mtx);
|
|
192
|
+
|
|
193
|
+
backup->status = sqlite3_open(baton->filename.c_str(), &backup->_otherDb);
|
|
194
|
+
|
|
195
|
+
if (backup->status == SQLITE_OK) {
|
|
196
|
+
backup->_handle = sqlite3_backup_init(
|
|
197
|
+
baton->filenameIsDest ? backup->_otherDb : backup->db->_handle,
|
|
198
|
+
baton->destName.c_str(),
|
|
199
|
+
baton->filenameIsDest ? backup->db->_handle : backup->_otherDb,
|
|
200
|
+
baton->sourceName.c_str());
|
|
201
|
+
}
|
|
202
|
+
backup->_destDb = baton->filenameIsDest ? backup->_otherDb : backup->db->_handle;
|
|
203
|
+
|
|
204
|
+
if (backup->status != SQLITE_OK) {
|
|
205
|
+
backup->message = std::string(sqlite3_errmsg(backup->_destDb));
|
|
206
|
+
sqlite3_close(backup->_otherDb);
|
|
207
|
+
backup->_otherDb = NULL;
|
|
208
|
+
backup->_destDb = NULL;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
sqlite3_mutex_leave(mtx);
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
void Backup::Work_AfterInitialize(napi_env e, napi_status status, void* data) {
|
|
215
|
+
std::unique_ptr<InitializeBaton> baton(static_cast<InitializeBaton*>(data));
|
|
216
|
+
auto* backup = baton->backup;
|
|
217
|
+
|
|
218
|
+
auto env = backup->Env();
|
|
219
|
+
Napi::HandleScope scope(env);
|
|
220
|
+
|
|
221
|
+
if (backup->status != SQLITE_OK) {
|
|
222
|
+
Error(baton.get());
|
|
223
|
+
backup->FinishAll();
|
|
224
|
+
}
|
|
225
|
+
else {
|
|
226
|
+
backup->inited = true;
|
|
227
|
+
Napi::Function cb = baton->callback.Value();
|
|
228
|
+
if (!cb.IsEmpty() && cb.IsFunction()) {
|
|
229
|
+
Napi::Value argv[] = { env.Null() };
|
|
230
|
+
TRY_CATCH_CALL(backup->Value(), cb, 1, argv);
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
BACKUP_END();
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
Napi::Value Backup::Step(const Napi::CallbackInfo& info) {
|
|
237
|
+
auto* backup = this;
|
|
238
|
+
auto env = backup->Env();
|
|
239
|
+
|
|
240
|
+
REQUIRE_ARGUMENT_INTEGER(0, pages);
|
|
241
|
+
OPTIONAL_ARGUMENT_FUNCTION(1, callback);
|
|
242
|
+
|
|
243
|
+
auto* baton = new StepBaton(backup, callback, pages);
|
|
244
|
+
backup->GetRetryErrors(baton->retryErrorsSet);
|
|
245
|
+
backup->Schedule(Work_BeginStep, baton);
|
|
246
|
+
return info.This();
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
void Backup::Work_BeginStep(Baton* baton) {
|
|
250
|
+
BACKUP_BEGIN(Step);
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
void Backup::Work_Step(napi_env e, void* data) {
|
|
254
|
+
BACKUP_INIT(StepBaton);
|
|
255
|
+
if (backup->_handle) {
|
|
256
|
+
backup->status = sqlite3_backup_step(backup->_handle, baton->pages);
|
|
257
|
+
backup->remaining = sqlite3_backup_remaining(backup->_handle);
|
|
258
|
+
backup->pageCount = sqlite3_backup_pagecount(backup->_handle);
|
|
259
|
+
}
|
|
260
|
+
if (backup->status != SQLITE_OK) {
|
|
261
|
+
// Text of message is a little awkward to get, since the error is not associated
|
|
262
|
+
// with a db connection.
|
|
263
|
+
#if SQLITE_VERSION_NUMBER >= 3007015
|
|
264
|
+
// sqlite3_errstr is a relatively new method
|
|
265
|
+
backup->message = std::string(sqlite3_errstr(backup->status));
|
|
266
|
+
#else
|
|
267
|
+
backup->message = "Sqlite error";
|
|
268
|
+
#endif
|
|
269
|
+
if (baton->retryErrorsSet.size() > 0) {
|
|
270
|
+
if (baton->retryErrorsSet.find(backup->status) == baton->retryErrorsSet.end()) {
|
|
271
|
+
backup->FinishSqlite();
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
void Backup::Work_AfterStep(napi_env e, napi_status status, void* data) {
|
|
278
|
+
std::unique_ptr<StepBaton> baton(static_cast<StepBaton*>(data));
|
|
279
|
+
auto* backup = baton->backup;
|
|
280
|
+
|
|
281
|
+
auto env = backup->Env();
|
|
282
|
+
Napi::HandleScope scope(env);
|
|
283
|
+
|
|
284
|
+
if (backup->status == SQLITE_DONE) {
|
|
285
|
+
backup->completed = true;
|
|
286
|
+
} else if (!backup->_handle) {
|
|
287
|
+
backup->failed = true;
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
if (backup->status != SQLITE_OK && backup->status != SQLITE_DONE) {
|
|
291
|
+
Error(baton.get());
|
|
292
|
+
}
|
|
293
|
+
else {
|
|
294
|
+
// Fire callbacks.
|
|
295
|
+
Napi::Function cb = baton->callback.Value();
|
|
296
|
+
if (!cb.IsEmpty() && cb.IsFunction()) {
|
|
297
|
+
Napi::Value argv[] = { env.Null(), Napi::Boolean::New(env, backup->status == SQLITE_DONE) };
|
|
298
|
+
TRY_CATCH_CALL(backup->Value(), cb, 2, argv);
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
BACKUP_END();
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
Napi::Value Backup::Finish(const Napi::CallbackInfo& info) {
|
|
306
|
+
auto* backup = this;
|
|
307
|
+
auto env = backup->Env();
|
|
308
|
+
|
|
309
|
+
OPTIONAL_ARGUMENT_FUNCTION(0, callback);
|
|
310
|
+
|
|
311
|
+
auto* baton = new Baton(backup, callback);
|
|
312
|
+
backup->Schedule(Work_BeginFinish, baton);
|
|
313
|
+
return info.This();
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
void Backup::Work_BeginFinish(Baton* baton) {
|
|
317
|
+
BACKUP_BEGIN(Finish);
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
void Backup::Work_Finish(napi_env e, void* data) {
|
|
321
|
+
BACKUP_INIT(Baton);
|
|
322
|
+
backup->FinishSqlite();
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
void Backup::Work_AfterFinish(napi_env e, napi_status status, void* data) {
|
|
326
|
+
std::unique_ptr<Baton> baton(static_cast<Baton*>(data));
|
|
327
|
+
auto* backup = baton->backup;
|
|
328
|
+
|
|
329
|
+
auto env = backup->Env();
|
|
330
|
+
Napi::HandleScope scope(env);
|
|
331
|
+
|
|
332
|
+
backup->FinishAll();
|
|
333
|
+
|
|
334
|
+
// Fire callback in case there was one.
|
|
335
|
+
Napi::Function cb = baton->callback.Value();
|
|
336
|
+
if (!cb.IsEmpty() && cb.IsFunction()) {
|
|
337
|
+
TRY_CATCH_CALL(backup->Value(), cb, 0, NULL);
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
BACKUP_END();
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
void Backup::FinishAll() {
|
|
344
|
+
assert(!finished);
|
|
345
|
+
if (!completed && !failed) {
|
|
346
|
+
failed = true;
|
|
347
|
+
}
|
|
348
|
+
finished = true;
|
|
349
|
+
CleanQueue();
|
|
350
|
+
FinishSqlite();
|
|
351
|
+
db->Unref();
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
void Backup::FinishSqlite() {
|
|
355
|
+
if (_handle) {
|
|
356
|
+
sqlite3_backup_finish(_handle);
|
|
357
|
+
_handle = NULL;
|
|
358
|
+
}
|
|
359
|
+
if (_otherDb) {
|
|
360
|
+
sqlite3_close(_otherDb);
|
|
361
|
+
_otherDb = NULL;
|
|
362
|
+
}
|
|
363
|
+
_destDb = NULL;
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
Napi::Value Backup::IdleGetter(const Napi::CallbackInfo& info) {
|
|
367
|
+
auto* backup = this;
|
|
368
|
+
bool idle = backup->inited && !backup->locked && backup->queue.empty();
|
|
369
|
+
return Napi::Boolean::New(this->Env(), idle);
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
Napi::Value Backup::CompletedGetter(const Napi::CallbackInfo& info) {
|
|
373
|
+
auto* backup = this;
|
|
374
|
+
return Napi::Boolean::New(this->Env(), backup->completed);
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
Napi::Value Backup::FailedGetter(const Napi::CallbackInfo& info) {
|
|
378
|
+
auto* backup = this;
|
|
379
|
+
return Napi::Boolean::New(this->Env(), backup->failed);
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
Napi::Value Backup::RemainingGetter(const Napi::CallbackInfo& info) {
|
|
383
|
+
auto* backup = this;
|
|
384
|
+
return Napi::Number::New(this->Env(), backup->remaining);
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
Napi::Value Backup::PageCountGetter(const Napi::CallbackInfo& info) {
|
|
388
|
+
auto* backup = this;
|
|
389
|
+
return Napi::Number::New(this->Env(), backup->pageCount);
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
Napi::Value Backup::RetryErrorGetter(const Napi::CallbackInfo& info) {
|
|
393
|
+
auto* backup = this;
|
|
394
|
+
return backup->retryErrors.Value();
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
void Backup::RetryErrorSetter(const Napi::CallbackInfo& info, const Napi::Value& value) {
|
|
398
|
+
auto* backup = this;
|
|
399
|
+
auto env = backup->Env();
|
|
400
|
+
if (!value.IsArray()) {
|
|
401
|
+
Napi::Error::New(env, "retryErrors must be an array").ThrowAsJavaScriptException();
|
|
402
|
+
return;
|
|
403
|
+
}
|
|
404
|
+
Napi::Array array = value.As<Napi::Array>();
|
|
405
|
+
backup->retryErrors.Reset(array, 1);
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
void Backup::GetRetryErrors(std::set<int>& retryErrorsSet) {
|
|
409
|
+
retryErrorsSet.clear();
|
|
410
|
+
Napi::Array array = retryErrors.Value();
|
|
411
|
+
auto length = array.Length();
|
|
412
|
+
for (size_t i = 0; i < length; i++) {
|
|
413
|
+
Napi::Value code = (array).Get(static_cast<uint32_t>(i));
|
|
414
|
+
if (code.IsNumber()) {
|
|
415
|
+
retryErrorsSet.insert(code.As<Napi::Number>().Int32Value());
|
|
416
|
+
}
|
|
417
|
+
}
|
|
418
|
+
}
|
package/src/backup.h
ADDED
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
#ifndef NODE_SQLITE3_SRC_BACKUP_H
|
|
2
|
+
#define NODE_SQLITE3_SRC_BACKUP_H
|
|
3
|
+
|
|
4
|
+
#include "database.h"
|
|
5
|
+
|
|
6
|
+
#include <string>
|
|
7
|
+
#include <queue>
|
|
8
|
+
#include <set>
|
|
9
|
+
|
|
10
|
+
#include <sqlite3.h>
|
|
11
|
+
#include <napi.h>
|
|
12
|
+
|
|
13
|
+
using namespace Napi;
|
|
14
|
+
|
|
15
|
+
namespace node_sqlite3 {
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
*
|
|
19
|
+
* A class for managing an sqlite3_backup object. For consistency
|
|
20
|
+
* with other node-sqlite3 classes, it maintains an internal queue
|
|
21
|
+
* of calls.
|
|
22
|
+
*
|
|
23
|
+
* Intended usage from node:
|
|
24
|
+
*
|
|
25
|
+
* var db = new sqlite3.Database('live.db');
|
|
26
|
+
* var backup = db.backup('backup.db');
|
|
27
|
+
* ...
|
|
28
|
+
* // in event loop, move backup forward when we have time.
|
|
29
|
+
* if (backup.idle) { backup.step(NPAGES); }
|
|
30
|
+
* if (backup.completed) { ... success ... }
|
|
31
|
+
* if (backup.failed) { ... sadness ... }
|
|
32
|
+
* // do other work in event loop - fine to modify live.db
|
|
33
|
+
* ...
|
|
34
|
+
*
|
|
35
|
+
* Here is how sqlite's backup api is exposed:
|
|
36
|
+
*
|
|
37
|
+
* - `sqlite3_backup_init`: This is implemented as
|
|
38
|
+
* `db.backup(filename, [callback])` or
|
|
39
|
+
* `db.backup(filename, destDbName, sourceDbName, filenameIsDest, [callback])`.
|
|
40
|
+
* - `sqlite3_backup_step`: `backup.step(pages, [callback])`.
|
|
41
|
+
* - `sqlite3_backup_finish`: `backup.finish([callback])`.
|
|
42
|
+
* - `sqlite3_backup_remaining`: `backup.remaining`.
|
|
43
|
+
* - `sqlite3_backup_pagecount`: `backup.pageCount`.
|
|
44
|
+
*
|
|
45
|
+
* There are the following read-only properties:
|
|
46
|
+
*
|
|
47
|
+
* - `backup.completed` is set to `true` when the backup
|
|
48
|
+
* succeeeds.
|
|
49
|
+
* - `backup.failed` is set to `true` when the backup
|
|
50
|
+
* has a fatal error.
|
|
51
|
+
* - `backup.idle` is set to `true` when no operation
|
|
52
|
+
* is currently in progress or queued for the backup.
|
|
53
|
+
* - `backup.remaining` is an integer with the remaining
|
|
54
|
+
* number of pages after the last call to `backup.step`
|
|
55
|
+
* (-1 if `step` not yet called).
|
|
56
|
+
* - `backup.pageCount` is an integer with the total number
|
|
57
|
+
* of pages measured during the last call to `backup.step`
|
|
58
|
+
* (-1 if `step` not yet called).
|
|
59
|
+
*
|
|
60
|
+
* There is the following writable property:
|
|
61
|
+
*
|
|
62
|
+
* - `backup.retryErrors`: an array of sqlite3 error codes
|
|
63
|
+
* that are treated as non-fatal - meaning, if they occur,
|
|
64
|
+
* backup.failed is not set, and the backup may continue.
|
|
65
|
+
* By default, this is `[sqlite3.BUSY, sqlite3.LOCKED]`.
|
|
66
|
+
*
|
|
67
|
+
* The `db.backup(filename, [callback])` shorthand is sufficient
|
|
68
|
+
* for making a backup of a database opened by node-sqlite3. If
|
|
69
|
+
* using attached or temporary databases, or moving data in the
|
|
70
|
+
* opposite direction, the more complete (but daunting)
|
|
71
|
+
* `db.backup(filename, destDbName, sourceDbName, filenameIsDest, [callback])`
|
|
72
|
+
* signature is provided.
|
|
73
|
+
*
|
|
74
|
+
* A backup will finish automatically when it succeeds or a fatal
|
|
75
|
+
* error occurs, meaning it is not necessary to call `db.finish()`.
|
|
76
|
+
* By default, SQLITE_LOCKED and SQLITE_BUSY errors are not
|
|
77
|
+
* treated as failures, and the backup will continue if they
|
|
78
|
+
* occur. The set of errors that are tolerated can be controlled
|
|
79
|
+
* by setting `backup.retryErrors`. To disable automatic
|
|
80
|
+
* finishing and stick strictly to sqlite's raw api, set
|
|
81
|
+
* `backup.retryErrors` to `[]`. In that case, it is necessary
|
|
82
|
+
* to call `backup.finish()`.
|
|
83
|
+
*
|
|
84
|
+
* In the same way as node-sqlite3 databases and statements,
|
|
85
|
+
* backup methods can be called safely without callbacks, due
|
|
86
|
+
* to an internal call queue. So for example this naive code
|
|
87
|
+
* will correctly back up a db, if there are no errors:
|
|
88
|
+
*
|
|
89
|
+
* var backup = db.backup('backup.db');
|
|
90
|
+
* backup.step(-1);
|
|
91
|
+
* backup.finish();
|
|
92
|
+
*
|
|
93
|
+
*/
|
|
94
|
+
class Backup : public Napi::ObjectWrap<Backup> {
|
|
95
|
+
public:
|
|
96
|
+
static Napi::Object Init(Napi::Env env, Napi::Object exports);
|
|
97
|
+
|
|
98
|
+
struct Baton {
|
|
99
|
+
napi_async_work request = NULL;
|
|
100
|
+
Backup* backup;
|
|
101
|
+
Napi::FunctionReference callback;
|
|
102
|
+
|
|
103
|
+
Baton(Backup* backup_, Napi::Function cb_) : backup(backup_) {
|
|
104
|
+
backup->Ref();
|
|
105
|
+
callback.Reset(cb_, 1);
|
|
106
|
+
}
|
|
107
|
+
virtual ~Baton() {
|
|
108
|
+
if (request) napi_delete_async_work(backup->Env(), request);
|
|
109
|
+
backup->Unref();
|
|
110
|
+
callback.Reset();
|
|
111
|
+
}
|
|
112
|
+
};
|
|
113
|
+
|
|
114
|
+
struct InitializeBaton : Database::Baton {
|
|
115
|
+
Backup* backup;
|
|
116
|
+
std::string filename;
|
|
117
|
+
std::string sourceName;
|
|
118
|
+
std::string destName;
|
|
119
|
+
bool filenameIsDest;
|
|
120
|
+
InitializeBaton(Database* db_, Napi::Function cb_, Backup* backup_) :
|
|
121
|
+
Baton(db_, cb_), backup(backup_), filenameIsDest(true) {
|
|
122
|
+
backup->Ref();
|
|
123
|
+
}
|
|
124
|
+
virtual ~InitializeBaton() override {
|
|
125
|
+
backup->Unref();
|
|
126
|
+
if (!db->IsOpen() && db->IsLocked()) {
|
|
127
|
+
// The database handle was closed before the backup could be opened.
|
|
128
|
+
backup->FinishAll();
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
};
|
|
132
|
+
|
|
133
|
+
struct StepBaton : Baton {
|
|
134
|
+
int pages;
|
|
135
|
+
std::set<int> retryErrorsSet;
|
|
136
|
+
StepBaton(Backup* backup_, Napi::Function cb_, int pages_) :
|
|
137
|
+
Baton(backup_, cb_), pages(pages_) {}
|
|
138
|
+
virtual ~StepBaton() override = default;
|
|
139
|
+
};
|
|
140
|
+
|
|
141
|
+
typedef void (*Work_Callback)(Baton* baton);
|
|
142
|
+
|
|
143
|
+
struct Call {
|
|
144
|
+
Call(Work_Callback cb_, Baton* baton_) : callback(cb_), baton(baton_) {};
|
|
145
|
+
Work_Callback callback;
|
|
146
|
+
Baton* baton;
|
|
147
|
+
};
|
|
148
|
+
|
|
149
|
+
Backup(const Napi::CallbackInfo& info);
|
|
150
|
+
|
|
151
|
+
~Backup() {
|
|
152
|
+
if (!finished) {
|
|
153
|
+
FinishAll();
|
|
154
|
+
}
|
|
155
|
+
retryErrors.Reset();
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
WORK_DEFINITION(Step)
|
|
159
|
+
WORK_DEFINITION(Finish)
|
|
160
|
+
|
|
161
|
+
Napi::Value IdleGetter(const Napi::CallbackInfo& info);
|
|
162
|
+
Napi::Value CompletedGetter(const Napi::CallbackInfo& info);
|
|
163
|
+
Napi::Value FailedGetter(const Napi::CallbackInfo& info);
|
|
164
|
+
Napi::Value PageCountGetter(const Napi::CallbackInfo& info);
|
|
165
|
+
Napi::Value RemainingGetter(const Napi::CallbackInfo& info);
|
|
166
|
+
Napi::Value FatalErrorGetter(const Napi::CallbackInfo& info);
|
|
167
|
+
Napi::Value RetryErrorGetter(const Napi::CallbackInfo& info);
|
|
168
|
+
|
|
169
|
+
void FatalErrorSetter(const Napi::CallbackInfo& info, const Napi::Value& value);
|
|
170
|
+
void RetryErrorSetter(const Napi::CallbackInfo& info, const Napi::Value& value);
|
|
171
|
+
|
|
172
|
+
protected:
|
|
173
|
+
static void Work_BeginInitialize(Database::Baton* baton);
|
|
174
|
+
static void Work_Initialize(napi_env env, void* data);
|
|
175
|
+
static void Work_AfterInitialize(napi_env env, napi_status status, void* data);
|
|
176
|
+
|
|
177
|
+
void Schedule(Work_Callback callback, Baton* baton);
|
|
178
|
+
void Process();
|
|
179
|
+
void CleanQueue();
|
|
180
|
+
template <class T> static void Error(T* baton);
|
|
181
|
+
|
|
182
|
+
void FinishAll();
|
|
183
|
+
void FinishSqlite();
|
|
184
|
+
void GetRetryErrors(std::set<int>& retryErrorsSet);
|
|
185
|
+
|
|
186
|
+
Database* db;
|
|
187
|
+
|
|
188
|
+
sqlite3_backup* _handle = NULL;
|
|
189
|
+
sqlite3* _otherDb = NULL;
|
|
190
|
+
sqlite3* _destDb = NULL;
|
|
191
|
+
|
|
192
|
+
bool inited = false;
|
|
193
|
+
bool locked = true;
|
|
194
|
+
bool completed = false;
|
|
195
|
+
bool failed = false;
|
|
196
|
+
int remaining = -1;
|
|
197
|
+
int pageCount = -1;
|
|
198
|
+
bool finished = false;
|
|
199
|
+
|
|
200
|
+
int status;
|
|
201
|
+
std::string message;
|
|
202
|
+
std::queue<std::unique_ptr<Call>> queue;
|
|
203
|
+
|
|
204
|
+
Napi::Reference<Array> retryErrors;
|
|
205
|
+
};
|
|
206
|
+
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
#endif
|