better-sqlite3-multiple-ciphers 12.4.1 → 12.5.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.
@@ -1,193 +1,193 @@
1
- class Binder {
2
- public:
3
-
4
- explicit Binder(sqlite3_stmt* _handle) {
5
- handle = _handle;
6
- param_count = sqlite3_bind_parameter_count(_handle);
7
- anon_index = 0;
8
- success = true;
9
- }
10
-
11
- bool Bind(NODE_ARGUMENTS info, int argc, Statement* stmt) {
12
- assert(anon_index == 0);
13
- Result result = BindArgs(info, argc, stmt);
14
- if (success && result.count != param_count) {
15
- if (result.count < param_count) {
16
- if (!result.bound_object && stmt->GetBindMap(OnlyIsolate)->GetSize()) {
17
- Fail(ThrowTypeError, "Missing named parameters");
18
- } else {
19
- Fail(ThrowRangeError, "Too few parameter values were provided");
20
- }
21
- } else {
22
- Fail(ThrowRangeError, "Too many parameter values were provided");
23
- }
24
- }
25
- return success;
26
- }
27
-
28
- private:
29
-
30
- struct Result {
31
- int count;
32
- bool bound_object;
33
- };
34
-
35
- static bool IsPlainObject(v8::Isolate* isolate, v8::Local<v8::Object> obj) {
36
- v8::Local<v8::Value> proto = obj->GetPrototype();
37
- v8::Local<v8::Context> ctx = obj->GetCreationContext().ToLocalChecked();
38
- ctx->Enter();
39
- v8::Local<v8::Value> baseProto = v8::Object::New(isolate)->GetPrototype();
40
- ctx->Exit();
41
- return proto->StrictEquals(baseProto) || proto->StrictEquals(v8::Null(isolate));
42
- }
43
-
44
- void Fail(void (*Throw)(const char* _), const char* message) {
45
- assert(success == true);
46
- assert((Throw == NULL) == (message == NULL));
47
- assert(Throw == ThrowError || Throw == ThrowTypeError || Throw == ThrowRangeError || Throw == NULL);
48
- if (Throw) Throw(message);
49
- success = false;
50
- }
51
-
52
- int NextAnonIndex() {
53
- while (sqlite3_bind_parameter_name(handle, ++anon_index) != NULL) {}
54
- return anon_index;
55
- }
56
-
57
- // Binds the value at the given index or throws an appropriate error.
58
- void BindValue(v8::Isolate* isolate, v8::Local<v8::Value> value, int index) {
59
- int status = Data::BindValueFromJS(isolate, handle, index, value);
60
- if (status != SQLITE_OK) {
61
- switch (status) {
62
- case -1:
63
- return Fail(ThrowTypeError, "SQLite3 can only bind numbers, strings, bigints, buffers, and null");
64
- case SQLITE_TOOBIG:
65
- return Fail(ThrowRangeError, "The bound string, buffer, or bigint is too big");
66
- case SQLITE_RANGE:
67
- return Fail(ThrowRangeError, "Too many parameter values were provided");
68
- case SQLITE_NOMEM:
69
- return Fail(ThrowError, "Out of memory");
70
- default:
71
- return Fail(ThrowError, "An unexpected error occured while trying to bind parameters");
72
- }
73
- assert(false);
74
- }
75
- }
76
-
77
- // Binds each value in the array or throws an appropriate error.
78
- // The number of successfully bound parameters is returned.
79
- int BindArray(v8::Isolate* isolate, v8::Local<v8::Array> arr) {
80
- UseContext;
81
- uint32_t length = arr->Length();
82
- if (length > INT_MAX) {
83
- Fail(ThrowRangeError, "Too many parameter values were provided");
84
- return 0;
85
- }
86
- int len = static_cast<int>(length);
87
- for (int i = 0; i < len; ++i) {
88
- v8::MaybeLocal<v8::Value> maybeValue = arr->Get(ctx, i);
89
- if (maybeValue.IsEmpty()) {
90
- Fail(NULL, NULL);
91
- return i;
92
- }
93
- BindValue(isolate, maybeValue.ToLocalChecked(), NextAnonIndex());
94
- if (!success) {
95
- return i;
96
- }
97
- }
98
- return len;
99
- }
100
-
101
- // Binds all named parameters using the values found in the given object.
102
- // The number of successfully bound parameters is returned.
103
- // If a named parameter is missing from the object, an error is thrown.
104
- // This should only be invoked once per instance.
105
- int BindObject(v8::Isolate* isolate, v8::Local<v8::Object> obj, Statement* stmt) {
106
- UseContext;
107
- BindMap* bind_map = stmt->GetBindMap(isolate);
108
- BindMap::Pair* pairs = bind_map->GetPairs();
109
- int len = bind_map->GetSize();
110
-
111
- for (int i = 0; i < len; ++i) {
112
- v8::Local<v8::String> key = pairs[i].GetName(isolate);
113
-
114
- // Check if the named parameter was provided.
115
- v8::Maybe<bool> has_property = obj->HasOwnProperty(ctx, key);
116
- if (has_property.IsNothing()) {
117
- Fail(NULL, NULL);
118
- return i;
119
- }
120
- if (!has_property.FromJust()) {
121
- v8::String::Utf8Value param_name(isolate, key);
122
- Fail(ThrowRangeError, (std::string("Missing named parameter \"") + *param_name + "\"").c_str());
123
- return i;
124
- }
125
-
126
- // Get the current property value.
127
- v8::MaybeLocal<v8::Value> maybeValue = obj->Get(ctx, key);
128
- if (maybeValue.IsEmpty()) {
129
- Fail(NULL, NULL);
130
- return i;
131
- }
132
-
133
- BindValue(isolate, maybeValue.ToLocalChecked(), pairs[i].GetIndex());
134
- if (!success) {
135
- return i;
136
- }
137
- }
138
-
139
- return len;
140
- }
141
-
142
- // Binds all parameters using the values found in the arguments object.
143
- // Anonymous parameter values can be directly in the arguments object or in an Array.
144
- // Named parameter values can be provided in a plain Object argument.
145
- // Only one plain Object argument may be provided.
146
- // If an error occurs, an appropriate error is thrown.
147
- // The return value is a struct indicating how many parameters were successfully bound
148
- // and whether or not it tried to bind an object.
149
- Result BindArgs(NODE_ARGUMENTS info, int argc, Statement* stmt) {
150
- UseIsolate;
151
- int count = 0;
152
- bool bound_object = false;
153
-
154
- for (int i = 0; i < argc; ++i) {
155
- v8::Local<v8::Value> arg = info[i];
156
-
157
- if (arg->IsArray()) {
158
- count += BindArray(isolate, arg.As<v8::Array>());
159
- if (!success) break;
160
- continue;
161
- }
162
-
163
- if (arg->IsObject() && !node::Buffer::HasInstance(arg)) {
164
- v8::Local<v8::Object> obj = arg.As<v8::Object>();
165
- if (IsPlainObject(isolate, obj)) {
166
- if (bound_object) {
167
- Fail(ThrowTypeError, "You cannot specify named parameters in two different objects");
168
- break;
169
- }
170
- bound_object = true;
171
-
172
- count += BindObject(isolate, obj, stmt);
173
- if (!success) break;
174
- continue;
175
- } else if (stmt->GetBindMap(isolate)->GetSize()) {
176
- Fail(ThrowTypeError, "Named parameters can only be passed within plain objects");
177
- break;
178
- }
179
- }
180
-
181
- BindValue(isolate, arg, NextAnonIndex());
182
- if (!success) break;
183
- count += 1;
184
- }
185
-
186
- return { count, bound_object };
187
- }
188
-
189
- sqlite3_stmt* handle;
190
- int param_count;
191
- int anon_index; // This value should only be used by NextAnonIndex()
192
- bool success; // This value should only be set by Fail()
193
- };
1
+ class Binder {
2
+ public:
3
+
4
+ explicit Binder(sqlite3_stmt* _handle) {
5
+ handle = _handle;
6
+ param_count = sqlite3_bind_parameter_count(_handle);
7
+ anon_index = 0;
8
+ success = true;
9
+ }
10
+
11
+ bool Bind(NODE_ARGUMENTS info, int argc, Statement* stmt) {
12
+ assert(anon_index == 0);
13
+ Result result = BindArgs(info, argc, stmt);
14
+ if (success && result.count != param_count) {
15
+ if (result.count < param_count) {
16
+ if (!result.bound_object && stmt->GetBindMap(OnlyIsolate)->GetSize()) {
17
+ Fail(ThrowTypeError, "Missing named parameters");
18
+ } else {
19
+ Fail(ThrowRangeError, "Too few parameter values were provided");
20
+ }
21
+ } else {
22
+ Fail(ThrowRangeError, "Too many parameter values were provided");
23
+ }
24
+ }
25
+ return success;
26
+ }
27
+
28
+ private:
29
+
30
+ struct Result {
31
+ int count;
32
+ bool bound_object;
33
+ };
34
+
35
+ static bool IsPlainObject(v8::Isolate* isolate, v8::Local<v8::Object> obj) {
36
+ v8::Local<v8::Value> proto = GET_PROTOTYPE(obj);
37
+ v8::Local<v8::Context> ctx = obj->GetCreationContext().ToLocalChecked();
38
+ ctx->Enter();
39
+ v8::Local<v8::Value> baseProto = GET_PROTOTYPE(v8::Object::New(isolate));
40
+ ctx->Exit();
41
+ return proto->StrictEquals(baseProto) || proto->StrictEquals(v8::Null(isolate));
42
+ }
43
+
44
+ void Fail(void (*Throw)(const char* _), const char* message) {
45
+ assert(success == true);
46
+ assert((Throw == NULL) == (message == NULL));
47
+ assert(Throw == ThrowError || Throw == ThrowTypeError || Throw == ThrowRangeError || Throw == NULL);
48
+ if (Throw) Throw(message);
49
+ success = false;
50
+ }
51
+
52
+ int NextAnonIndex() {
53
+ while (sqlite3_bind_parameter_name(handle, ++anon_index) != NULL) {}
54
+ return anon_index;
55
+ }
56
+
57
+ // Binds the value at the given index or throws an appropriate error.
58
+ void BindValue(v8::Isolate* isolate, v8::Local<v8::Value> value, int index) {
59
+ int status = Data::BindValueFromJS(isolate, handle, index, value);
60
+ if (status != SQLITE_OK) {
61
+ switch (status) {
62
+ case -1:
63
+ return Fail(ThrowTypeError, "SQLite3 can only bind numbers, strings, bigints, buffers, and null");
64
+ case SQLITE_TOOBIG:
65
+ return Fail(ThrowRangeError, "The bound string, buffer, or bigint is too big");
66
+ case SQLITE_RANGE:
67
+ return Fail(ThrowRangeError, "Too many parameter values were provided");
68
+ case SQLITE_NOMEM:
69
+ return Fail(ThrowError, "Out of memory");
70
+ default:
71
+ return Fail(ThrowError, "An unexpected error occured while trying to bind parameters");
72
+ }
73
+ assert(false);
74
+ }
75
+ }
76
+
77
+ // Binds each value in the array or throws an appropriate error.
78
+ // The number of successfully bound parameters is returned.
79
+ int BindArray(v8::Isolate* isolate, v8::Local<v8::Array> arr) {
80
+ UseContext;
81
+ uint32_t length = arr->Length();
82
+ if (length > INT_MAX) {
83
+ Fail(ThrowRangeError, "Too many parameter values were provided");
84
+ return 0;
85
+ }
86
+ int len = static_cast<int>(length);
87
+ for (int i = 0; i < len; ++i) {
88
+ v8::MaybeLocal<v8::Value> maybeValue = arr->Get(ctx, i);
89
+ if (maybeValue.IsEmpty()) {
90
+ Fail(NULL, NULL);
91
+ return i;
92
+ }
93
+ BindValue(isolate, maybeValue.ToLocalChecked(), NextAnonIndex());
94
+ if (!success) {
95
+ return i;
96
+ }
97
+ }
98
+ return len;
99
+ }
100
+
101
+ // Binds all named parameters using the values found in the given object.
102
+ // The number of successfully bound parameters is returned.
103
+ // If a named parameter is missing from the object, an error is thrown.
104
+ // This should only be invoked once per instance.
105
+ int BindObject(v8::Isolate* isolate, v8::Local<v8::Object> obj, Statement* stmt) {
106
+ UseContext;
107
+ BindMap* bind_map = stmt->GetBindMap(isolate);
108
+ BindMap::Pair* pairs = bind_map->GetPairs();
109
+ int len = bind_map->GetSize();
110
+
111
+ for (int i = 0; i < len; ++i) {
112
+ v8::Local<v8::String> key = pairs[i].GetName(isolate);
113
+
114
+ // Check if the named parameter was provided.
115
+ v8::Maybe<bool> has_property = obj->HasOwnProperty(ctx, key);
116
+ if (has_property.IsNothing()) {
117
+ Fail(NULL, NULL);
118
+ return i;
119
+ }
120
+ if (!has_property.FromJust()) {
121
+ v8::String::Utf8Value param_name(isolate, key);
122
+ Fail(ThrowRangeError, (std::string("Missing named parameter \"") + *param_name + "\"").c_str());
123
+ return i;
124
+ }
125
+
126
+ // Get the current property value.
127
+ v8::MaybeLocal<v8::Value> maybeValue = obj->Get(ctx, key);
128
+ if (maybeValue.IsEmpty()) {
129
+ Fail(NULL, NULL);
130
+ return i;
131
+ }
132
+
133
+ BindValue(isolate, maybeValue.ToLocalChecked(), pairs[i].GetIndex());
134
+ if (!success) {
135
+ return i;
136
+ }
137
+ }
138
+
139
+ return len;
140
+ }
141
+
142
+ // Binds all parameters using the values found in the arguments object.
143
+ // Anonymous parameter values can be directly in the arguments object or in an Array.
144
+ // Named parameter values can be provided in a plain Object argument.
145
+ // Only one plain Object argument may be provided.
146
+ // If an error occurs, an appropriate error is thrown.
147
+ // The return value is a struct indicating how many parameters were successfully bound
148
+ // and whether or not it tried to bind an object.
149
+ Result BindArgs(NODE_ARGUMENTS info, int argc, Statement* stmt) {
150
+ UseIsolate;
151
+ int count = 0;
152
+ bool bound_object = false;
153
+
154
+ for (int i = 0; i < argc; ++i) {
155
+ v8::Local<v8::Value> arg = info[i];
156
+
157
+ if (arg->IsArray()) {
158
+ count += BindArray(isolate, arg.As<v8::Array>());
159
+ if (!success) break;
160
+ continue;
161
+ }
162
+
163
+ if (arg->IsObject() && !node::Buffer::HasInstance(arg)) {
164
+ v8::Local<v8::Object> obj = arg.As<v8::Object>();
165
+ if (IsPlainObject(isolate, obj)) {
166
+ if (bound_object) {
167
+ Fail(ThrowTypeError, "You cannot specify named parameters in two different objects");
168
+ break;
169
+ }
170
+ bound_object = true;
171
+
172
+ count += BindObject(isolate, obj, stmt);
173
+ if (!success) break;
174
+ continue;
175
+ } else if (stmt->GetBindMap(isolate)->GetSize()) {
176
+ Fail(ThrowTypeError, "Named parameters can only be passed within plain objects");
177
+ break;
178
+ }
179
+ }
180
+
181
+ BindValue(isolate, arg, NextAnonIndex());
182
+ if (!success) break;
183
+ count += 1;
184
+ }
185
+
186
+ return { count, bound_object };
187
+ }
188
+
189
+ sqlite3_stmt* handle;
190
+ int param_count;
191
+ int anon_index; // This value should only be used by NextAnonIndex()
192
+ bool success; // This value should only be set by Fail()
193
+ };