better-sqlite3-multiple-ciphers 12.2.0 → 12.4.6

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,105 @@
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_key);
73
+ static NODE_METHOD(JS_rekey);
74
+ static NODE_METHOD(JS_exec);
75
+ static NODE_METHOD(JS_backup);
76
+ static NODE_METHOD(JS_serialize);
77
+ static NODE_METHOD(JS_function);
78
+ static NODE_METHOD(JS_aggregate);
79
+ static NODE_METHOD(JS_table);
80
+ static NODE_METHOD(JS_loadExtension);
81
+ static NODE_METHOD(JS_close);
82
+ static NODE_METHOD(JS_defaultSafeIntegers);
83
+ static NODE_METHOD(JS_unsafeMode);
84
+ static NODE_GETTER(JS_open);
85
+ static NODE_GETTER(JS_inTransaction);
86
+
87
+ static bool Deserialize(v8::Local<v8::Object> buffer, Addon* addon, sqlite3* db_handle, bool readonly);
88
+ static void FreeSerialization(char* data, void* _);
89
+
90
+ static const int MAX_BUFFER_SIZE;
91
+ static const int MAX_STRING_SIZE;
92
+
93
+ sqlite3* const db_handle;
94
+ bool open;
95
+ bool busy;
96
+ bool safe_ints;
97
+ bool unsafe_mode;
98
+ bool was_js_error;
99
+ const bool has_logger;
100
+ unsigned short iterators;
101
+ Addon* const addon;
102
+ const v8::Global<v8::Value> logger;
103
+ std::set<Statement*, CompareStatement> stmts;
104
+ std::set<Backup*, CompareBackup> backups;
105
+ };
@@ -0,0 +1,113 @@
1
+ StatementIterator::StatementIterator(Statement* stmt, bool bound) :
2
+ node::ObjectWrap(),
3
+ stmt(stmt),
4
+ handle(stmt->handle),
5
+ db_state(stmt->db->GetState()),
6
+ bound(bound),
7
+ safe_ints(stmt->safe_ints),
8
+ mode(stmt->mode),
9
+ alive(true),
10
+ logged(!db_state->has_logger) {
11
+ assert(stmt != NULL);
12
+ assert(handle != NULL);
13
+ assert(stmt->bound == bound);
14
+ assert(stmt->alive == true);
15
+ assert(stmt->locked == false);
16
+ assert(db_state->iterators < USHRT_MAX);
17
+ stmt->locked = true;
18
+ db_state->iterators += 1;
19
+ }
20
+
21
+ // The ~Statement destructor currently covers any state this object creates.
22
+ // Additionally, we actually DON'T want to revert stmt->locked or db_state
23
+ // ->iterators in this destructor, to ensure deterministic database access.
24
+ StatementIterator::~StatementIterator() {}
25
+
26
+ void StatementIterator::Next(NODE_ARGUMENTS info) {
27
+ assert(alive == true);
28
+ db_state->busy = true;
29
+ if (!logged) {
30
+ logged = true;
31
+ if (stmt->db->Log(OnlyIsolate, handle)) {
32
+ db_state->busy = false;
33
+ Throw();
34
+ return;
35
+ }
36
+ }
37
+ int status = sqlite3_step(handle);
38
+ db_state->busy = false;
39
+ if (status == SQLITE_ROW) {
40
+ UseIsolate;
41
+ UseContext;
42
+ info.GetReturnValue().Set(
43
+ NewRecord(isolate, ctx, Data::GetRowJS(isolate, ctx, handle, safe_ints, mode), db_state->addon, false)
44
+ );
45
+ } else {
46
+ if (status == SQLITE_DONE) Return(info);
47
+ else Throw();
48
+ }
49
+ }
50
+
51
+ void StatementIterator::Return(NODE_ARGUMENTS info) {
52
+ Cleanup();
53
+ STATEMENT_RETURN_LOGIC(DoneRecord(OnlyIsolate, db_state->addon));
54
+ }
55
+
56
+ void StatementIterator::Throw() {
57
+ Cleanup();
58
+ Database* db = stmt->db;
59
+ STATEMENT_THROW_LOGIC();
60
+ }
61
+
62
+ void StatementIterator::Cleanup() {
63
+ assert(alive == true);
64
+ alive = false;
65
+ stmt->locked = false;
66
+ db_state->iterators -= 1;
67
+ sqlite3_reset(handle);
68
+ }
69
+
70
+ INIT(StatementIterator::Init) {
71
+ v8::Local<v8::FunctionTemplate> t = NewConstructorTemplate(isolate, data, JS_new, "StatementIterator");
72
+ SetPrototypeMethod(isolate, data, t, "next", JS_next);
73
+ SetPrototypeMethod(isolate, data, t, "return", JS_return);
74
+ SetPrototypeSymbolMethod(isolate, data, t, v8::Symbol::GetIterator(isolate), JS_symbolIterator);
75
+ return t->GetFunction(OnlyContext).ToLocalChecked();
76
+ }
77
+
78
+ NODE_METHOD(StatementIterator::JS_new) {
79
+ UseAddon;
80
+ if (!addon->privileged_info) return ThrowTypeError("Disabled constructor");
81
+ assert(info.IsConstructCall());
82
+
83
+ StatementIterator* iter;
84
+ {
85
+ NODE_ARGUMENTS info = *addon->privileged_info;
86
+ STATEMENT_START_LOGIC(REQUIRE_STATEMENT_RETURNS_DATA, DOES_ADD_ITERATOR);
87
+ iter = new StatementIterator(stmt, bound);
88
+ }
89
+ UseIsolate;
90
+ UseContext;
91
+ iter->Wrap(info.This());
92
+ SetFrozen(isolate, ctx, info.This(), addon->cs.statement, addon->privileged_info->This());
93
+
94
+ info.GetReturnValue().Set(info.This());
95
+ }
96
+
97
+ NODE_METHOD(StatementIterator::JS_next) {
98
+ StatementIterator* iter = Unwrap<StatementIterator>(info.This());
99
+ REQUIRE_DATABASE_NOT_BUSY(iter->db_state);
100
+ if (iter->alive) iter->Next(info);
101
+ else info.GetReturnValue().Set(DoneRecord(OnlyIsolate, iter->db_state->addon));
102
+ }
103
+
104
+ NODE_METHOD(StatementIterator::JS_return) {
105
+ StatementIterator* iter = Unwrap<StatementIterator>(info.This());
106
+ REQUIRE_DATABASE_NOT_BUSY(iter->db_state);
107
+ if (iter->alive) iter->Return(info);
108
+ else info.GetReturnValue().Set(DoneRecord(OnlyIsolate, iter->db_state->addon));
109
+ }
110
+
111
+ NODE_METHOD(StatementIterator::JS_symbolIterator) {
112
+ info.GetReturnValue().Set(info.This());
113
+ }
@@ -0,0 +1,50 @@
1
+ class StatementIterator : public node::ObjectWrap {
2
+ public:
3
+
4
+ // The ~Statement destructor currently covers any state this object creates.
5
+ // Additionally, we actually DON'T want to revert stmt->locked or db_state
6
+ // ->iterators in this destructor, to ensure deterministic database access.
7
+ ~StatementIterator();
8
+
9
+ static INIT(Init);
10
+
11
+ private:
12
+
13
+ explicit StatementIterator(Statement* stmt, bool bound);
14
+
15
+ void Next(NODE_ARGUMENTS info);
16
+ void Return(NODE_ARGUMENTS info);
17
+ void Throw();
18
+ void Cleanup();
19
+
20
+ static inline v8::Local<v8::Object> NewRecord(
21
+ v8::Isolate* isolate,
22
+ v8::Local<v8::Context> ctx,
23
+ v8::Local<v8::Value> value,
24
+ Addon* addon,
25
+ bool done
26
+ ) {
27
+ v8::Local<v8::Object> record = v8::Object::New(isolate);
28
+ record->Set(ctx, addon->cs.value.Get(isolate), value).FromJust();
29
+ record->Set(ctx, addon->cs.done.Get(isolate), v8::Boolean::New(isolate, done)).FromJust();
30
+ return record;
31
+ }
32
+
33
+ static inline v8::Local<v8::Object> DoneRecord(v8::Isolate* isolate, Addon* addon) {
34
+ return NewRecord(isolate, OnlyContext, v8::Undefined(isolate), addon, true);
35
+ }
36
+
37
+ static NODE_METHOD(JS_new);
38
+ static NODE_METHOD(JS_next);
39
+ static NODE_METHOD(JS_return);
40
+ static NODE_METHOD(JS_symbolIterator);
41
+
42
+ Statement* const stmt;
43
+ sqlite3_stmt* const handle;
44
+ Database::State* const db_state;
45
+ const bool bound;
46
+ const bool safe_ints;
47
+ const char mode;
48
+ bool alive;
49
+ bool logged;
50
+ };
@@ -0,0 +1,383 @@
1
+ Statement::Statement(
2
+ Database* db,
3
+ sqlite3_stmt* handle,
4
+ sqlite3_uint64 id,
5
+ bool returns_data
6
+ ) :
7
+ node::ObjectWrap(),
8
+ db(db),
9
+ handle(handle),
10
+ extras(new Extras(id)),
11
+ alive(true),
12
+ locked(false),
13
+ bound(false),
14
+ has_bind_map(false),
15
+ safe_ints(db->GetState()->safe_ints),
16
+ mode(Data::FLAT),
17
+ returns_data(returns_data) {
18
+ assert(db != NULL);
19
+ assert(handle != NULL);
20
+ assert(db->GetState()->open);
21
+ assert(!db->GetState()->busy);
22
+ db->AddStatement(this);
23
+ }
24
+
25
+ Statement::~Statement() {
26
+ if (alive) db->RemoveStatement(this);
27
+ CloseHandles();
28
+ delete extras;
29
+ }
30
+
31
+ // Whenever this is used, db->RemoveStatement must be invoked beforehand.
32
+ void Statement::CloseHandles() {
33
+ if (alive) {
34
+ alive = false;
35
+ sqlite3_finalize(handle);
36
+ }
37
+ }
38
+
39
+ // Returns the Statement's bind map (creates it upon first execution).
40
+ BindMap* Statement::GetBindMap(v8::Isolate* isolate) {
41
+ if (has_bind_map) return &extras->bind_map;
42
+ BindMap* bind_map = &extras->bind_map;
43
+ int param_count = sqlite3_bind_parameter_count(handle);
44
+ for (int i = 1; i <= param_count; ++i) {
45
+ const char* name = sqlite3_bind_parameter_name(handle, i);
46
+ if (name != NULL) bind_map->Add(isolate, name + 1, i);
47
+ }
48
+ has_bind_map = true;
49
+ return bind_map;
50
+ }
51
+
52
+ Statement::Extras::Extras(sqlite3_uint64 id)
53
+ : bind_map(0), id(id) {}
54
+
55
+ INIT(Statement::Init) {
56
+ v8::Local<v8::FunctionTemplate> t = NewConstructorTemplate(isolate, data, JS_new, "Statement");
57
+ SetPrototypeMethod(isolate, data, t, "run", JS_run);
58
+ SetPrototypeMethod(isolate, data, t, "get", JS_get);
59
+ SetPrototypeMethod(isolate, data, t, "all", JS_all);
60
+ SetPrototypeMethod(isolate, data, t, "iterate", JS_iterate);
61
+ SetPrototypeMethod(isolate, data, t, "bind", JS_bind);
62
+ SetPrototypeMethod(isolate, data, t, "pluck", JS_pluck);
63
+ SetPrototypeMethod(isolate, data, t, "expand", JS_expand);
64
+ SetPrototypeMethod(isolate, data, t, "raw", JS_raw);
65
+ SetPrototypeMethod(isolate, data, t, "safeIntegers", JS_safeIntegers);
66
+ SetPrototypeMethod(isolate, data, t, "columns", JS_columns);
67
+ SetPrototypeGetter(isolate, data, t, "busy", JS_busy);
68
+ return t->GetFunction(OnlyContext).ToLocalChecked();
69
+ }
70
+
71
+ NODE_METHOD(Statement::JS_new) {
72
+ UseAddon;
73
+ if (!addon->privileged_info) {
74
+ return ThrowTypeError("Statements can only be constructed by the db.prepare() method");
75
+ }
76
+ assert(info.IsConstructCall());
77
+ Database* db = Unwrap<Database>(addon->privileged_info->This());
78
+ REQUIRE_DATABASE_OPEN(db->GetState());
79
+ REQUIRE_DATABASE_NOT_BUSY(db->GetState());
80
+
81
+ v8::Local<v8::String> source = (*addon->privileged_info)[0].As<v8::String>();
82
+ v8::Local<v8::Object> database = (*addon->privileged_info)[1].As<v8::Object>();
83
+ bool pragmaMode = (*addon->privileged_info)[2].As<v8::Boolean>()->Value();
84
+ int flags = SQLITE_PREPARE_PERSISTENT;
85
+
86
+ if (pragmaMode) {
87
+ REQUIRE_DATABASE_NO_ITERATORS_UNLESS_UNSAFE(db->GetState());
88
+ flags = 0;
89
+ }
90
+
91
+ UseIsolate;
92
+ v8::String::Utf8Value utf8(isolate, source);
93
+ sqlite3_stmt* handle;
94
+ const char* tail;
95
+
96
+ if (sqlite3_prepare_v3(db->GetHandle(), *utf8, utf8.length() + 1, flags, &handle, &tail) != SQLITE_OK) {
97
+ return db->ThrowDatabaseError();
98
+ }
99
+ if (handle == NULL) {
100
+ return ThrowRangeError("The supplied SQL string contains no statements");
101
+ }
102
+ // https://github.com/WiseLibs/better-sqlite3/issues/975#issuecomment-1520934678
103
+ for (char c; (c = *tail); ) {
104
+ if (IS_SKIPPED(c)) {
105
+ ++tail;
106
+ continue;
107
+ }
108
+ if (c == '/' && tail[1] == '*') {
109
+ tail += 2;
110
+ for (char c; (c = *tail); ++tail) {
111
+ if (c == '*' && tail[1] == '/') {
112
+ tail += 2;
113
+ break;
114
+ }
115
+ }
116
+ } else if (c == '-' && tail[1] == '-') {
117
+ tail += 2;
118
+ for (char c; (c = *tail); ++tail) {
119
+ if (c == '\n') {
120
+ ++tail;
121
+ break;
122
+ }
123
+ }
124
+ } else {
125
+ sqlite3_finalize(handle);
126
+ return ThrowRangeError("The supplied SQL string contains more than one statement");
127
+ }
128
+ }
129
+
130
+ UseContext;
131
+ bool returns_data = sqlite3_column_count(handle) >= 1 || pragmaMode;
132
+ Statement* stmt = new Statement(db, handle, addon->NextId(), returns_data);
133
+ stmt->Wrap(info.This());
134
+ SetFrozen(isolate, ctx, info.This(), addon->cs.reader, v8::Boolean::New(isolate, returns_data));
135
+ SetFrozen(isolate, ctx, info.This(), addon->cs.readonly, v8::Boolean::New(isolate, sqlite3_stmt_readonly(handle) != 0));
136
+ SetFrozen(isolate, ctx, info.This(), addon->cs.source, source);
137
+ SetFrozen(isolate, ctx, info.This(), addon->cs.database, database);
138
+
139
+ info.GetReturnValue().Set(info.This());
140
+ }
141
+
142
+ NODE_METHOD(Statement::JS_run) {
143
+ STATEMENT_START(ALLOW_ANY_STATEMENT, DOES_MUTATE);
144
+ sqlite3* db_handle = db->GetHandle();
145
+ int total_changes_before = sqlite3_total_changes(db_handle);
146
+
147
+ sqlite3_step(handle);
148
+ if (sqlite3_reset(handle) == SQLITE_OK) {
149
+ int changes = sqlite3_total_changes(db_handle) == total_changes_before ? 0 : sqlite3_changes(db_handle);
150
+ sqlite3_int64 id = sqlite3_last_insert_rowid(db_handle);
151
+ Addon* addon = db->GetAddon();
152
+ UseContext;
153
+ v8::Local<v8::Object> result = v8::Object::New(isolate);
154
+ result->Set(ctx, addon->cs.changes.Get(isolate), v8::Int32::New(isolate, changes)).FromJust();
155
+ result->Set(ctx, addon->cs.lastInsertRowid.Get(isolate),
156
+ stmt->safe_ints
157
+ ? v8::BigInt::New(isolate, id).As<v8::Value>()
158
+ : v8::Number::New(isolate, (double)id).As<v8::Value>()
159
+ ).FromJust();
160
+ STATEMENT_RETURN(result);
161
+ }
162
+ STATEMENT_THROW();
163
+ }
164
+
165
+ NODE_METHOD(Statement::JS_get) {
166
+ STATEMENT_START(REQUIRE_STATEMENT_RETURNS_DATA, DOES_NOT_MUTATE);
167
+ int status = sqlite3_step(handle);
168
+ if (status == SQLITE_ROW) {
169
+ v8::Local<v8::Value> result = Data::GetRowJS(isolate, OnlyContext, handle, stmt->safe_ints, stmt->mode);
170
+ sqlite3_reset(handle);
171
+ STATEMENT_RETURN(result);
172
+ } else if (status == SQLITE_DONE) {
173
+ sqlite3_reset(handle);
174
+ STATEMENT_RETURN(v8::Undefined(isolate));
175
+ }
176
+ sqlite3_reset(handle);
177
+ STATEMENT_THROW();
178
+ }
179
+
180
+ NODE_METHOD(Statement::JS_all) {
181
+ STATEMENT_START(REQUIRE_STATEMENT_RETURNS_DATA, DOES_NOT_MUTATE);
182
+ UseContext;
183
+ const bool safe_ints = stmt->safe_ints;
184
+ const char mode = stmt->mode;
185
+
186
+ #if !defined(NODE_MODULE_VERSION) || NODE_MODULE_VERSION < 127
187
+ bool js_error = false;
188
+ uint32_t row_count = 0;
189
+ v8::Local<v8::Array> result = v8::Array::New(isolate, 0);
190
+
191
+ while (sqlite3_step(handle) == SQLITE_ROW) {
192
+ if (row_count == 0xffffffff) { ThrowRangeError("Array overflow (too many rows returned)"); js_error = true; break; }
193
+ result->Set(ctx, row_count++, Data::GetRowJS(isolate, ctx, handle, safe_ints, mode)).FromJust();
194
+ }
195
+
196
+ if (sqlite3_reset(handle) == SQLITE_OK && !js_error) {
197
+ STATEMENT_RETURN(result);
198
+ }
199
+ if (js_error) db->GetState()->was_js_error = true;
200
+ STATEMENT_THROW();
201
+ #else
202
+ v8::LocalVector<v8::Value> rows(isolate);
203
+ rows.reserve(8);
204
+
205
+ if (mode == Data::FLAT) {
206
+ RowBuilder rowBuilder(isolate, handle, safe_ints);
207
+ while (sqlite3_step(handle) == SQLITE_ROW) {
208
+ rows.emplace_back(rowBuilder.GetRowJS());
209
+ }
210
+ } else {
211
+ while (sqlite3_step(handle) == SQLITE_ROW) {
212
+ rows.emplace_back(Data::GetRowJS(isolate, ctx, handle, safe_ints, mode));
213
+ }
214
+ }
215
+
216
+ if (sqlite3_reset(handle) == SQLITE_OK) {
217
+ if (rows.size() > 0xffffffff) {
218
+ ThrowRangeError("Array overflow (too many rows returned)");
219
+ db->GetState()->was_js_error = true;
220
+ } else {
221
+ STATEMENT_RETURN(v8::Array::New(isolate, rows.data(), rows.size()));
222
+ }
223
+ }
224
+ STATEMENT_THROW();
225
+ #endif
226
+ }
227
+
228
+ NODE_METHOD(Statement::JS_iterate) {
229
+ UseAddon;
230
+ UseIsolate;
231
+ v8::Local<v8::Function> c = addon->StatementIterator.Get(isolate);
232
+ addon->privileged_info = &info;
233
+ v8::MaybeLocal<v8::Object> maybeIterator = c->NewInstance(OnlyContext, 0, NULL);
234
+ addon->privileged_info = NULL;
235
+ if (!maybeIterator.IsEmpty()) info.GetReturnValue().Set(maybeIterator.ToLocalChecked());
236
+ }
237
+
238
+ NODE_METHOD(Statement::JS_bind) {
239
+ Statement* stmt = Unwrap<Statement>(info.This());
240
+ if (stmt->bound) return ThrowTypeError("The bind() method can only be invoked once per statement object");
241
+ REQUIRE_DATABASE_OPEN(stmt->db->GetState());
242
+ REQUIRE_DATABASE_NOT_BUSY(stmt->db->GetState());
243
+ REQUIRE_STATEMENT_NOT_LOCKED(stmt);
244
+ STATEMENT_BIND(stmt->handle);
245
+ stmt->bound = true;
246
+ info.GetReturnValue().Set(info.This());
247
+ }
248
+
249
+ NODE_METHOD(Statement::JS_pluck) {
250
+ Statement* stmt = Unwrap<Statement>(info.This());
251
+ if (!stmt->returns_data) return ThrowTypeError("The pluck() method is only for statements that return data");
252
+ REQUIRE_DATABASE_NOT_BUSY(stmt->db->GetState());
253
+ REQUIRE_STATEMENT_NOT_LOCKED(stmt);
254
+ bool use = true;
255
+ if (info.Length() != 0) { REQUIRE_ARGUMENT_BOOLEAN(first, use); }
256
+ stmt->mode = use ? Data::PLUCK : stmt->mode == Data::PLUCK ? Data::FLAT : stmt->mode;
257
+ info.GetReturnValue().Set(info.This());
258
+ }
259
+
260
+ NODE_METHOD(Statement::JS_expand) {
261
+ Statement* stmt = Unwrap<Statement>(info.This());
262
+ if (!stmt->returns_data) return ThrowTypeError("The expand() method is only for statements that return data");
263
+ REQUIRE_DATABASE_NOT_BUSY(stmt->db->GetState());
264
+ REQUIRE_STATEMENT_NOT_LOCKED(stmt);
265
+ bool use = true;
266
+ if (info.Length() != 0) { REQUIRE_ARGUMENT_BOOLEAN(first, use); }
267
+ stmt->mode = use ? Data::EXPAND : stmt->mode == Data::EXPAND ? Data::FLAT : stmt->mode;
268
+ info.GetReturnValue().Set(info.This());
269
+ }
270
+
271
+ NODE_METHOD(Statement::JS_raw) {
272
+ Statement* stmt = Unwrap<Statement>(info.This());
273
+ if (!stmt->returns_data) return ThrowTypeError("The raw() method is only for statements that return data");
274
+ REQUIRE_DATABASE_NOT_BUSY(stmt->db->GetState());
275
+ REQUIRE_STATEMENT_NOT_LOCKED(stmt);
276
+ bool use = true;
277
+ if (info.Length() != 0) { REQUIRE_ARGUMENT_BOOLEAN(first, use); }
278
+ stmt->mode = use ? Data::RAW : stmt->mode == Data::RAW ? Data::FLAT : stmt->mode;
279
+ info.GetReturnValue().Set(info.This());
280
+ }
281
+
282
+ NODE_METHOD(Statement::JS_safeIntegers) {
283
+ Statement* stmt = Unwrap<Statement>(info.This());
284
+ REQUIRE_DATABASE_NOT_BUSY(stmt->db->GetState());
285
+ REQUIRE_STATEMENT_NOT_LOCKED(stmt);
286
+ if (info.Length() == 0) stmt->safe_ints = true;
287
+ else { REQUIRE_ARGUMENT_BOOLEAN(first, stmt->safe_ints); }
288
+ info.GetReturnValue().Set(info.This());
289
+ }
290
+
291
+ NODE_METHOD(Statement::JS_columns) {
292
+ Statement* stmt = Unwrap<Statement>(info.This());
293
+ if (!stmt->returns_data) return ThrowTypeError("The columns() method is only for statements that return data");
294
+ REQUIRE_DATABASE_OPEN(stmt->db->GetState());
295
+ REQUIRE_DATABASE_NOT_BUSY(stmt->db->GetState());
296
+ Addon* addon = stmt->db->GetAddon();
297
+ UseIsolate;
298
+
299
+ #if !defined(NODE_MODULE_VERSION) || NODE_MODULE_VERSION < 127
300
+ UseContext;
301
+ int column_count = sqlite3_column_count(stmt->handle);
302
+ v8::Local<v8::Array> columns = v8::Array::New(isolate);
303
+
304
+ v8::Local<v8::String> name = addon->cs.name.Get(isolate);
305
+ v8::Local<v8::String> columnName = addon->cs.column.Get(isolate);
306
+ v8::Local<v8::String> tableName = addon->cs.table.Get(isolate);
307
+ v8::Local<v8::String> databaseName = addon->cs.database.Get(isolate);
308
+ v8::Local<v8::String> typeName = addon->cs.type.Get(isolate);
309
+
310
+ for (int i = 0; i < column_count; ++i) {
311
+ v8::Local<v8::Object> column = v8::Object::New(isolate);
312
+
313
+ column->Set(ctx, name,
314
+ InternalizedFromUtf8OrNull(isolate, sqlite3_column_name(stmt->handle, i), -1)
315
+ ).FromJust();
316
+ column->Set(ctx, columnName,
317
+ InternalizedFromUtf8OrNull(isolate, sqlite3_column_origin_name(stmt->handle, i), -1)
318
+ ).FromJust();
319
+ column->Set(ctx, tableName,
320
+ InternalizedFromUtf8OrNull(isolate, sqlite3_column_table_name(stmt->handle, i), -1)
321
+ ).FromJust();
322
+ column->Set(ctx, databaseName,
323
+ InternalizedFromUtf8OrNull(isolate, sqlite3_column_database_name(stmt->handle, i), -1)
324
+ ).FromJust();
325
+ column->Set(ctx, typeName,
326
+ InternalizedFromUtf8OrNull(isolate, sqlite3_column_decltype(stmt->handle, i), -1)
327
+ ).FromJust();
328
+
329
+ columns->Set(ctx, i, column).FromJust();
330
+ }
331
+
332
+ info.GetReturnValue().Set(columns);
333
+ #else
334
+ v8::LocalVector<v8::Name> keys(isolate);
335
+ keys.reserve(5);
336
+ keys.emplace_back(addon->cs.name.Get(isolate).As<v8::Name>());
337
+ keys.emplace_back(addon->cs.column.Get(isolate).As<v8::Name>());
338
+ keys.emplace_back(addon->cs.table.Get(isolate).As<v8::Name>());
339
+ keys.emplace_back(addon->cs.database.Get(isolate).As<v8::Name>());
340
+ keys.emplace_back(addon->cs.type.Get(isolate).As<v8::Name>());
341
+
342
+ int column_count = sqlite3_column_count(stmt->handle);
343
+ v8::LocalVector<v8::Value> columns(isolate);
344
+ columns.reserve(column_count);
345
+
346
+ for (int i = 0; i < column_count; ++i) {
347
+ v8::LocalVector<v8::Value> values(isolate);
348
+ keys.reserve(5);
349
+ values.emplace_back(
350
+ InternalizedFromUtf8OrNull(isolate, sqlite3_column_name(stmt->handle, i), -1)
351
+ );
352
+ values.emplace_back(
353
+ InternalizedFromUtf8OrNull(isolate, sqlite3_column_origin_name(stmt->handle, i), -1)
354
+ );
355
+ values.emplace_back(
356
+ InternalizedFromUtf8OrNull(isolate, sqlite3_column_table_name(stmt->handle, i), -1)
357
+ );
358
+ values.emplace_back(
359
+ InternalizedFromUtf8OrNull(isolate, sqlite3_column_database_name(stmt->handle, i), -1)
360
+ );
361
+ values.emplace_back(
362
+ InternalizedFromUtf8OrNull(isolate, sqlite3_column_decltype(stmt->handle, i), -1)
363
+ );
364
+ columns.emplace_back(
365
+ v8::Object::New(isolate,
366
+ GET_PROTOTYPE(v8::Object::New(isolate)),
367
+ keys.data(),
368
+ values.data(),
369
+ keys.size()
370
+ )
371
+ );
372
+ }
373
+
374
+ info.GetReturnValue().Set(
375
+ v8::Array::New(isolate, columns.data(), columns.size())
376
+ );
377
+ #endif
378
+ }
379
+
380
+ NODE_GETTER(Statement::JS_busy) {
381
+ Statement* stmt = Unwrap<Statement>(info.This());
382
+ info.GetReturnValue().Set(stmt->alive && stmt->locked);
383
+ }
@@ -0,0 +1,58 @@
1
+ class Statement : public node::ObjectWrap { friend class StatementIterator;
2
+ public:
3
+
4
+ ~Statement();
5
+
6
+ // Whenever this is used, db->RemoveStatement must be invoked beforehand.
7
+ void CloseHandles();
8
+
9
+ // Used to support ordered containers.
10
+ static inline bool Compare(Statement const * const a, Statement const * const b) {
11
+ return a->extras->id < b->extras->id;
12
+ }
13
+
14
+ // Returns the Statement's bind map (creates it upon first execution).
15
+ BindMap* GetBindMap(v8::Isolate* isolate);
16
+
17
+ static INIT(Init);
18
+
19
+ private:
20
+
21
+ // A class for holding values that are less often used.
22
+ class Extras { friend class Statement;
23
+ explicit Extras(sqlite3_uint64 id);
24
+ BindMap bind_map;
25
+ const sqlite3_uint64 id;
26
+ };
27
+
28
+ explicit Statement(
29
+ Database* db,
30
+ sqlite3_stmt* handle,
31
+ sqlite3_uint64 id,
32
+ bool returns_data
33
+ );
34
+
35
+ static NODE_METHOD(JS_new);
36
+ static NODE_METHOD(JS_run);
37
+ static NODE_METHOD(JS_get);
38
+ static NODE_METHOD(JS_all);
39
+ static NODE_METHOD(JS_iterate);
40
+ static NODE_METHOD(JS_bind);
41
+ static NODE_METHOD(JS_pluck);
42
+ static NODE_METHOD(JS_expand);
43
+ static NODE_METHOD(JS_raw);
44
+ static NODE_METHOD(JS_safeIntegers);
45
+ static NODE_METHOD(JS_columns);
46
+ static NODE_GETTER(JS_busy);
47
+
48
+ Database* const db;
49
+ sqlite3_stmt* const handle;
50
+ Extras* const extras;
51
+ bool alive;
52
+ bool locked;
53
+ bool bound;
54
+ bool has_bind_map;
55
+ bool safe_ints;
56
+ char mode;
57
+ const bool returns_data;
58
+ };