-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
JSSQLStatement.cpp
1500 lines (1244 loc) · 63.9 KB
/
JSSQLStatement.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "root.h"
#include "JSSQLStatement.h"
#include "JavaScriptCore/JSObjectInlines.h"
#include "wtf/text/ExternalStringImpl.h"
#include "JavaScriptCore/FunctionPrototype.h"
#include "JavaScriptCore/HeapAnalyzer.h"
#include "JavaScriptCore/JSDestructibleObjectHeapCellType.h"
#include "JavaScriptCore/SlotVisitorMacros.h"
#include "JavaScriptCore/ObjectConstructor.h"
#include "JavaScriptCore/SubspaceInlines.h"
#include "wtf/GetPtr.h"
#include "wtf/PointerPreparations.h"
#include "wtf/URL.h"
#include "JavaScriptCore/TypedArrayInlines.h"
#include "JavaScriptCore/PropertyNameArray.h"
#include "Buffer.h"
#include "GCDefferalContext.h"
#include "Buffer.h"
#include <JavaScriptCore/DOMJITAbstractHeap.h>
#include "DOMJITIDLConvert.h"
#include "DOMJITIDLType.h"
#include "DOMJITIDLTypeFilter.h"
#include "DOMJITHelpers.h"
#include <JavaScriptCore/DFGAbstractHeap.h>
/* ******************************************************************************** */
// Lazy Load SQLite on macOS
// This seemed to be about 3% faster on macOS
// but it might be noise
// it's kind of hard to tell
// it should be strictly better though because
// instead of two pointers, one for DYLD_STUB$$ and one for the actual library
// we only call one pointer for the actual library
// and it means there's less work for DYLD to do on startup
// i.e. it shouldn't have any impact on startup time
#ifdef LAZY_LOAD_SQLITE
#include "lazy_sqlite3.h"
#else
static inline int lazyLoadSQLite()
{
return 0;
}
#endif
/* ******************************************************************************** */
static WTF::String sqliteString(const char* str)
{
auto res = WTF::String::fromUTF8(str);
sqlite3_free((void*)str);
return res;
}
static void sqlite_free_typed_array(void* ctx, void* buf)
{
sqlite3_free((void*)buf);
}
/** This is like a 3x perf improvement for allocating objects **/
#define SQL_USE_PROTOTYPE 1
static int DEFAULT_SQLITE_FLAGS
= SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE;
static unsigned int DEFAULT_SQLITE_PREPARE_FLAGS = SQLITE_PREPARE_PERSISTENT;
static int MAX_SQLITE_PREPARE_FLAG = SQLITE_PREPARE_PERSISTENT | SQLITE_PREPARE_NORMALIZE | SQLITE_PREPARE_NO_VTAB;
static JSC_DECLARE_HOST_FUNCTION(jsSQLStatementPrepareStatementFunction);
static JSC_DECLARE_HOST_FUNCTION(jsSQLStatementExecuteFunction);
static JSC_DECLARE_HOST_FUNCTION(jsSQLStatementOpenStatementFunction);
static JSC_DECLARE_HOST_FUNCTION(jsSQLStatementIsInTransactionFunction);
static JSC_DECLARE_HOST_FUNCTION(jsSQLStatementLoadExtensionFunction);
static JSC_DECLARE_HOST_FUNCTION(jsSQLStatementExecuteStatementFunction);
static JSC_DECLARE_HOST_FUNCTION(jsSQLStatementExecuteStatementFunctionRun);
static JSC_DECLARE_HOST_FUNCTION(jsSQLStatementExecuteStatementFunctionGet);
static JSC_DECLARE_HOST_FUNCTION(jsSQLStatementExecuteStatementFunctionAll);
static JSC_DECLARE_HOST_FUNCTION(jsSQLStatementExecuteStatementFunctionRows);
static JSC_DECLARE_CUSTOM_GETTER(jsSqlStatementGetColumnNames);
static JSC_DECLARE_CUSTOM_GETTER(jsSqlStatementGetColumnCount);
static JSC_DECLARE_HOST_FUNCTION(jsSQLStatementSerialize);
static JSC_DECLARE_HOST_FUNCTION(jsSQLStatementDeserialize);
#define CHECK_THIS \
if (UNLIKELY(!castedThis)) { \
throwException(lexicalGlobalObject, scope, createError(lexicalGlobalObject, "Expected SQLStatement"_s)); \
return JSValue::encode(jsUndefined()); \
}
#define DO_REBIND(param) \
if (param.isObject()) { \
JSC::JSValue reb = castedThis->rebind(lexicalGlobalObject, param, true); \
if (UNLIKELY(!reb.isNumber())) { \
return JSValue::encode(reb); /* this means an error */ \
} \
} else { \
throwException(lexicalGlobalObject, scope, createTypeError(lexicalGlobalObject, "Expected object or array"_s)); \
return JSValue::encode(jsUndefined()); \
}
#define CHECK_PREPARED \
if (UNLIKELY(castedThis->stmt == nullptr || castedThis->version_db == nullptr)) { \
throwException(lexicalGlobalObject, scope, createError(lexicalGlobalObject, "Statement has finalized"_s)); \
return JSValue::encode(jsUndefined()); \
}
namespace WebCore {
using namespace JSC;
class JSSQLStatement : public JSC::JSNonFinalObject {
public:
using Base = JSC::JSNonFinalObject;
static JSSQLStatement* create(JSC::Structure* structure, JSDOMGlobalObject* globalObject, sqlite3_stmt* stmt, VersionSqlite3* version_db)
{
JSSQLStatement* ptr = new (NotNull, JSC::allocateCell<JSSQLStatement>(globalObject->vm())) JSSQLStatement(structure, *globalObject, stmt, version_db);
ptr->finishCreation(globalObject->vm());
return ptr;
}
static void destroy(JSC::JSCell*);
template<typename, SubspaceAccess mode> static JSC::GCClient::IsoSubspace* subspaceFor(JSC::VM& vm)
{
return WebCore::subspaceForImpl<JSSQLStatement, UseCustomHeapCellType::No>(
vm,
[](auto& spaces) { return spaces.m_clientSubspaceForJSSQLStatement.get(); },
[](auto& spaces, auto&& space) { spaces.m_clientSubspaceForJSSQLStatement = WTFMove(space); },
[](auto& spaces) { return spaces.m_subspaceForJSSQLStatement.get(); },
[](auto& spaces, auto&& space) { spaces.m_subspaceForJSSQLStatement = WTFMove(space); });
}
DECLARE_VISIT_CHILDREN;
DECLARE_EXPORT_INFO;
// static void analyzeHeap(JSCell*, JSC::HeapAnalyzer&);
JSC::JSValue rebind(JSGlobalObject* globalObject, JSC::JSValue values, bool clone);
static JSC::Structure* createStructure(JSC::VM& vm, JSC::JSGlobalObject* globalObject, JSC::JSValue prototype)
{
return JSC::Structure::create(vm, globalObject, prototype, JSC::TypeInfo(JSC::ObjectType, StructureFlags), info());
}
bool need_update() { return version_db->version.load() != version; }
void update_version() { version = version_db->version.load(); }
~JSSQLStatement();
sqlite3_stmt* stmt;
VersionSqlite3* version_db;
uint64_t version;
bool hasExecuted = false;
std::unique_ptr<PropertyNameArray> columnNames;
mutable WriteBarrier<JSC::JSObject> _prototype;
protected:
JSSQLStatement(JSC::Structure* structure, JSDOMGlobalObject& globalObject, sqlite3_stmt* stmt, VersionSqlite3* version_db)
: Base(globalObject.vm(), structure)
, stmt(stmt)
, version_db(version_db)
, columnNames(new PropertyNameArray(globalObject.vm(), PropertyNameMode::Strings, PrivateSymbolMode::Exclude))
, _prototype(globalObject.vm(), this, nullptr)
{
}
void finishCreation(JSC::VM&);
};
static void initializeColumnNames(JSC::JSGlobalObject* lexicalGlobalObject, JSSQLStatement* castedThis)
{
if (!castedThis->hasExecuted) {
castedThis->hasExecuted = true;
} else {
// reinitialize column
castedThis->columnNames.reset(new PropertyNameArray(
castedThis->columnNames->vm(),
castedThis->columnNames->propertyNameMode(),
castedThis->columnNames->privateSymbolMode()));
}
castedThis->update_version();
JSC::VM& vm = lexicalGlobalObject->vm();
auto* stmt = castedThis->stmt;
int count = sqlite3_column_count(stmt);
if (count == 0)
return;
JSC::ObjectInitializationScope initializationScope(vm);
// 64 is the maximum we can preallocate here
// see https://github.com/oven-sh/bun/issues/987
JSC::JSObject* object = JSC::constructEmptyObject(lexicalGlobalObject, lexicalGlobalObject->objectPrototype(), std::min(count, 64));
for (int i = 0; i < count; i++) {
const char* name = sqlite3_column_name(stmt, i);
if (name == nullptr)
break;
size_t len = strlen(name);
if (len == 0)
break;
auto wtfString = WTF::String::fromUTF8(name, len);
auto str = JSValue(jsString(vm, wtfString));
auto key = str.toPropertyKey(lexicalGlobalObject);
JSC::JSValue primitive = JSC::jsUndefined();
auto decl = sqlite3_column_decltype(stmt, i);
if (decl != nullptr) {
switch (decl[0]) {
case 'F':
case 'D':
case 'I': {
primitive = jsNumber(0);
break;
}
case 'V':
case 'T': {
primitive = jsEmptyString(vm);
break;
}
}
}
object->putDirect(vm, key, primitive, 0);
castedThis->columnNames->add(key);
}
castedThis->_prototype.set(vm, castedThis, object);
}
void JSSQLStatement::destroy(JSC::JSCell* cell)
{
JSSQLStatement* thisObject = static_cast<JSSQLStatement*>(cell);
sqlite3_finalize(thisObject->stmt);
thisObject->stmt = nullptr;
}
void JSSQLStatementConstructor::destroy(JSC::JSCell* cell)
{
JSSQLStatementConstructor* thisObject = static_cast<JSSQLStatementConstructor*>(cell);
for (auto version_db : thisObject->databases) {
delete version_db;
}
}
static inline bool rebindValue(JSC::JSGlobalObject* lexicalGlobalObject, sqlite3_stmt* stmt, int i, JSC::JSValue value, JSC::ThrowScope& scope, bool clone)
{
#define CHECK_BIND(param) \
int result = param; \
if (UNLIKELY(result != SQLITE_OK)) { \
throwException(lexicalGlobalObject, scope, createError(lexicalGlobalObject, WTF::String::fromUTF8(sqlite3_errstr(result)))); \
return false; \
}
// only clone if necessary
// SQLite has a way to call a destructor
// but there doesn't seem to be a way to pass a pointer?
// we can't use it if there's no pointer to ref/unref
auto transientOrStatic = (void (*)(void*))(clone ? SQLITE_TRANSIENT : SQLITE_STATIC);
if (value.isUndefinedOrNull()) {
CHECK_BIND(sqlite3_bind_null(stmt, i));
} else if (value.isBoolean()) {
CHECK_BIND(sqlite3_bind_int(stmt, i, value.toBoolean(lexicalGlobalObject) ? 1 : 0));
} else if (value.isAnyInt()) {
int64_t val = value.asAnyInt();
if (val < INT_MIN || val > INT_MAX) {
CHECK_BIND(sqlite3_bind_int64(stmt, i, val));
} else {
CHECK_BIND(sqlite3_bind_int(stmt, i, val))
}
} else if (value.isNumber()) {
CHECK_BIND(sqlite3_bind_double(stmt, i, value.asDouble()))
} else if (value.isString()) {
auto* str = value.toStringOrNull(lexicalGlobalObject);
if (UNLIKELY(!str)) {
throwException(lexicalGlobalObject, scope, createTypeError(lexicalGlobalObject, "Expected string"_s));
return false;
}
auto roped = str->tryGetValue(lexicalGlobalObject);
if (UNLIKELY(!roped)) {
throwException(lexicalGlobalObject, scope, createError(lexicalGlobalObject, "Out of memory :("_s));
return false;
}
if (roped.is8Bit()) {
CHECK_BIND(sqlite3_bind_text(stmt, i, reinterpret_cast<const char*>(roped.characters8()), roped.length(), transientOrStatic));
} else {
CHECK_BIND(sqlite3_bind_text16(stmt, i, roped.characters16(), roped.length() * 2, transientOrStatic));
}
} else if (UNLIKELY(value.isHeapBigInt())) {
CHECK_BIND(sqlite3_bind_int64(stmt, i, JSBigInt::toBigInt64(value)));
} else if (JSC::JSArrayBufferView* buffer = JSC::jsDynamicCast<JSC::JSArrayBufferView*>(value)) {
CHECK_BIND(sqlite3_bind_blob(stmt, i, buffer->vector(), buffer->byteLength(), transientOrStatic));
} else {
throwException(lexicalGlobalObject, scope, createTypeError(lexicalGlobalObject, "Binding expected string, TypedArray, boolean, number, bigint or null"_s));
return false;
}
return true;
#undef CHECK_BIND
}
// this function does the equivalent of
// Object.entries(obj)
// except without the intermediate array of arrays
static JSC::JSValue rebindObject(JSC::JSGlobalObject* globalObject, JSC::JSValue targetValue, JSC::ThrowScope& scope, sqlite3_stmt* stmt, bool clone)
{
JSObject* target = targetValue.toObject(globalObject);
RETURN_IF_EXCEPTION(scope, {});
JSC::VM& vm = globalObject->vm();
PropertyNameArray properties(vm, PropertyNameMode::Strings, PrivateSymbolMode::Exclude);
target->methodTable()->getOwnPropertyNames(target, globalObject, properties, DontEnumPropertiesMode::Include);
RETURN_IF_EXCEPTION(scope, {});
int count = 0;
for (const auto& propertyName : properties) {
PropertySlot slot(target, PropertySlot::InternalMethodType::GetOwnProperty);
bool hasProperty = target->methodTable()->getOwnPropertySlot(target, globalObject, propertyName, slot);
RETURN_IF_EXCEPTION(scope, JSValue());
if (!hasProperty)
continue;
if (slot.attributes() & PropertyAttribute::DontEnum)
continue;
JSValue value;
if (LIKELY(!slot.isTaintedByOpaqueObject()))
value = slot.getValue(globalObject, propertyName);
else
value = target->get(globalObject, propertyName);
// Ensure this gets freed on scope clear
auto utf8 = WTF::String(propertyName.string()).utf8();
int index = sqlite3_bind_parameter_index(stmt, utf8.data());
if (index == 0) {
throwException(globalObject, scope, createError(globalObject, "Unknown parameter \"" + propertyName.string() + "\""_s));
return JSValue();
}
if (!rebindValue(globalObject, stmt, index, value, scope, clone))
return JSValue();
RETURN_IF_EXCEPTION(scope, {});
count++;
}
return jsNumber(count);
}
static JSC::JSValue rebindStatement(JSC::JSGlobalObject* lexicalGlobalObject, JSC::JSValue values, JSC::ThrowScope& scope, sqlite3_stmt* stmt, bool clone)
{
sqlite3_clear_bindings(stmt);
JSC::JSArray* array = jsDynamicCast<JSC::JSArray*>(values);
int max = sqlite3_bind_parameter_count(stmt);
if (!array) {
if (JSC::JSObject* object = values.getObject()) {
auto res = rebindObject(lexicalGlobalObject, object, scope, stmt, clone);
RETURN_IF_EXCEPTION(scope, {});
return res;
}
throwException(lexicalGlobalObject, scope, createError(lexicalGlobalObject, "Expected array"_s));
return jsUndefined();
}
int count = array->length();
if (count == 0) {
return jsNumber(0);
}
if (count != max) {
throwException(lexicalGlobalObject, scope, createError(lexicalGlobalObject, "Expected " + String::number(max) + " values, got " + String::number(count)));
return jsUndefined();
}
int i = 0;
for (; i < count; i++) {
JSC::JSValue value = array->getIndexQuickly(i);
rebindValue(lexicalGlobalObject, stmt, i + 1, value, scope, clone);
RETURN_IF_EXCEPTION(scope, {});
}
return jsNumber(i);
}
JSC_DEFINE_HOST_FUNCTION(jsSQLStatementSetCustomSQLite, (JSC::JSGlobalObject * lexicalGlobalObject, JSC::CallFrame* callFrame))
{
JSC::VM& vm = lexicalGlobalObject->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
JSValue thisValue = callFrame->thisValue();
JSSQLStatementConstructor* thisObject = jsDynamicCast<JSSQLStatementConstructor*>(thisValue.getObject());
if (UNLIKELY(!thisObject)) {
throwException(lexicalGlobalObject, scope, createError(lexicalGlobalObject, "Expected SQL"_s));
return JSValue::encode(JSC::jsUndefined());
}
if (callFrame->argumentCount() < 1) {
throwException(lexicalGlobalObject, scope, createError(lexicalGlobalObject, "Expected 1 argument"_s));
return JSValue::encode(JSC::jsUndefined());
}
JSC::JSValue sqliteStrValue = callFrame->argument(0);
if (UNLIKELY(!sqliteStrValue.isString())) {
throwException(lexicalGlobalObject, scope, createError(lexicalGlobalObject, "Expected SQLite path"_s));
return JSValue::encode(JSC::jsUndefined());
}
#ifdef LAZY_LOAD_SQLITE
if (sqlite3_handle) {
throwException(lexicalGlobalObject, scope, createError(lexicalGlobalObject, "SQLite already loaded\nThis function can only be called before SQLite has been loaded and exactly once. SQLite auto-loads when the first time you open a Database."_s));
return JSValue::encode(JSC::jsUndefined());
}
sqlite3_lib_path = sqliteStrValue.toWTFString(lexicalGlobalObject).utf8().data();
if (lazyLoadSQLite() == -1) {
sqlite3_handle = nullptr;
WTF::String msg = WTF::String::fromUTF8(dlerror());
throwException(lexicalGlobalObject, scope, createError(lexicalGlobalObject, msg));
return JSValue::encode(JSC::jsUndefined());
}
#endif
RELEASE_AND_RETURN(scope, JSValue::encode(JSC::jsBoolean(true)));
}
JSC_DEFINE_HOST_FUNCTION(jsSQLStatementDeserialize, (JSC::JSGlobalObject * lexicalGlobalObject, JSC::CallFrame* callFrame))
{
JSC::VM& vm = lexicalGlobalObject->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
JSValue thisValue = callFrame->thisValue();
JSSQLStatementConstructor* thisObject = jsDynamicCast<JSSQLStatementConstructor*>(thisValue.getObject());
JSC::JSArrayBufferView* array = jsDynamicCast<JSC::JSArrayBufferView*>(callFrame->argument(0));
unsigned int flags = SQLITE_DESERIALIZE_FREEONCLOSE | SQLITE_DESERIALIZE_RESIZEABLE;
JSC::EnsureStillAliveScope ensureAliveArray(array);
if (callFrame->argumentCount() > 1 and callFrame->argument(1).toBoolean(lexicalGlobalObject)) {
flags |= SQLITE_DESERIALIZE_READONLY;
}
if (UNLIKELY(!thisObject)) {
throwException(lexicalGlobalObject, scope, createError(lexicalGlobalObject, "Expected SQL"_s));
return JSValue::encode(JSC::jsUndefined());
}
if (callFrame->argumentCount() < 1) {
throwException(lexicalGlobalObject, scope, createError(lexicalGlobalObject, "Expected 1 argument"_s));
return JSValue::encode(JSC::jsUndefined());
}
if (UNLIKELY(!array)) {
throwException(lexicalGlobalObject, scope, createError(lexicalGlobalObject, "Expected Uint8Array or Buffer"_s));
return JSValue::encode(JSC::jsUndefined());
}
if (UNLIKELY(array->isDetached())) {
throwException(lexicalGlobalObject, scope, createError(lexicalGlobalObject, "TypedArray is detached"_s));
return JSValue::encode(JSC::jsUndefined());
}
#if LAZY_LOAD_SQLITE
if (UNLIKELY(lazyLoadSQLite() < 0)) {
WTF::String msg = WTF::String::fromUTF8(dlerror());
throwException(lexicalGlobalObject, scope, createError(lexicalGlobalObject, msg));
return JSValue::encode(JSC::jsUndefined());
}
#endif
size_t byteLength = array->byteLength();
void* ptr = array->vector();
if (UNLIKELY(ptr == nullptr || byteLength == 0)) {
throwException(lexicalGlobalObject, scope, createError(lexicalGlobalObject, "ArrayBuffer must not be empty"_s));
return JSValue::encode(JSC::jsUndefined());
}
void* data = sqlite3_malloc64(byteLength);
if (UNLIKELY(data == nullptr)) {
throwException(lexicalGlobalObject, scope, createError(lexicalGlobalObject, "Failed to allocate memory"_s));
return JSValue::encode(JSC::jsUndefined());
}
if (byteLength) {
memcpy(data, ptr, byteLength);
}
sqlite3* db = nullptr;
if (sqlite3_open_v2(":memory:", &db, DEFAULT_SQLITE_FLAGS, nullptr) != SQLITE_OK) {
throwException(lexicalGlobalObject, scope, createError(lexicalGlobalObject, "Failed to open SQLite"_s));
return JSValue::encode(JSC::jsUndefined());
}
int status = sqlite3_db_config(db, SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION, 1, NULL);
assert(status == SQLITE_OK);
status = sqlite3_db_config(db, SQLITE_DBCONFIG_DEFENSIVE, 1, NULL);
assert(status == SQLITE_OK);
status = sqlite3_deserialize(db, "main", reinterpret_cast<unsigned char*>(data), byteLength, byteLength, flags);
if (status == SQLITE_BUSY) {
sqlite3_free(data);
throwException(lexicalGlobalObject, scope, createError(lexicalGlobalObject, "SQLITE_BUSY"_s));
return JSValue::encode(JSC::jsUndefined());
}
if (status != SQLITE_OK) {
sqlite3_free(data);
throwException(lexicalGlobalObject, scope, createError(lexicalGlobalObject, status == SQLITE_ERROR ? "unable to deserialize database"_s : sqliteString(sqlite3_errstr(status))));
return JSValue::encode(JSC::jsUndefined());
}
auto count = thisObject->databases.size();
thisObject->databases.append(new VersionSqlite3(db));
RELEASE_AND_RETURN(scope, JSValue::encode(jsNumber(count)));
}
JSC_DEFINE_HOST_FUNCTION(jsSQLStatementSerialize, (JSC::JSGlobalObject * lexicalGlobalObject, JSC::CallFrame* callFrame))
{
JSC::VM& vm = lexicalGlobalObject->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
JSValue thisValue = callFrame->thisValue();
JSSQLStatementConstructor* thisObject = jsDynamicCast<JSSQLStatementConstructor*>(thisValue.getObject());
if (UNLIKELY(!thisObject)) {
throwException(lexicalGlobalObject, scope, createError(lexicalGlobalObject, "Expected SQL"_s));
return JSValue::encode(JSC::jsUndefined());
}
int32_t dbIndex = callFrame->argument(0).toInt32(lexicalGlobalObject);
if (UNLIKELY(dbIndex < 0 || dbIndex >= thisObject->databases.size())) {
throwException(lexicalGlobalObject, scope, createError(lexicalGlobalObject, "Invalid database handle"_s));
return JSValue::encode(JSC::jsUndefined());
}
sqlite3* db = thisObject->databases[dbIndex]->db;
if (UNLIKELY(!db)) {
throwException(lexicalGlobalObject, scope, createError(lexicalGlobalObject, "Can't do this on a closed database"_s));
return JSValue::encode(JSC::jsUndefined());
}
WTF::String attachedName = callFrame->argument(1).toWTFString(lexicalGlobalObject);
RETURN_IF_EXCEPTION(scope, JSC::JSValue::encode(JSC::jsUndefined()));
if (attachedName.isEmpty()) {
throwException(lexicalGlobalObject, scope, createError(lexicalGlobalObject, "Expected attached database name"_s));
return JSValue::encode(JSC::jsUndefined());
}
sqlite3_int64 length = -1;
unsigned char* data = sqlite3_serialize(db, attachedName.utf8().data(), &length, 0);
if (UNLIKELY(data == nullptr && length)) {
throwException(lexicalGlobalObject, scope, createError(lexicalGlobalObject, "Out of memory"_s));
return JSValue::encode(JSC::jsUndefined());
}
RELEASE_AND_RETURN(scope, JSBuffer__bufferFromPointerAndLengthAndDeinit(lexicalGlobalObject, reinterpret_cast<char*>(data), static_cast<unsigned int>(length), data, sqlite_free_typed_array));
}
JSC_DEFINE_HOST_FUNCTION(jsSQLStatementLoadExtensionFunction, (JSC::JSGlobalObject * lexicalGlobalObject, JSC::CallFrame* callFrame))
{
JSC::VM& vm = lexicalGlobalObject->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
JSValue thisValue = callFrame->thisValue();
JSSQLStatementConstructor* thisObject = jsDynamicCast<JSSQLStatementConstructor*>(thisValue.getObject());
if (UNLIKELY(!thisObject)) {
throwException(lexicalGlobalObject, scope, createError(lexicalGlobalObject, "Expected SQL"_s));
return JSValue::encode(JSC::jsUndefined());
}
int32_t dbIndex = callFrame->argument(0).toInt32(lexicalGlobalObject);
if (UNLIKELY(dbIndex < 0 || dbIndex >= thisObject->databases.size())) {
throwException(lexicalGlobalObject, scope, createError(lexicalGlobalObject, "Invalid database handle"_s));
return JSValue::encode(JSC::jsUndefined());
}
JSC::JSValue extension = callFrame->argument(1);
if (UNLIKELY(!extension.isString())) {
throwException(lexicalGlobalObject, scope, createTypeError(lexicalGlobalObject, "Expected string"_s));
return JSValue::encode(JSC::jsUndefined());
}
auto extensionString = extension.toWTFString(lexicalGlobalObject);
RETURN_IF_EXCEPTION(scope, {});
sqlite3* db = thisObject->databases[dbIndex]->db;
if (UNLIKELY(!db)) {
throwException(lexicalGlobalObject, scope, createError(lexicalGlobalObject, "Can't do this on a closed database"_s));
return JSValue::encode(JSC::jsUndefined());
}
auto entryPointStr = callFrame->argumentCount() > 2 && callFrame->argument(2).isString() ? callFrame->argument(2).toWTFString(lexicalGlobalObject) : String();
const char* entryPoint = entryPointStr.length() == 0 ? NULL : entryPointStr.utf8().data();
char* error;
int rc = sqlite3_load_extension(db, extensionString.utf8().data(), entryPoint, &error);
// TODO: can we disable loading extensions after this?
if (rc != SQLITE_OK) {
throwException(lexicalGlobalObject, scope, createError(lexicalGlobalObject, sqliteString(error != nullptr ? error : sqlite3_errmsg(db))));
return JSValue::encode(JSC::jsUndefined());
}
RELEASE_AND_RETURN(scope, JSValue::encode(JSC::jsUndefined()));
}
// This runs a query one-off
// without the overhead of a long-lived statement object
// does not return anything
JSC_DEFINE_HOST_FUNCTION(jsSQLStatementExecuteFunction, (JSC::JSGlobalObject * lexicalGlobalObject, JSC::CallFrame* callFrame))
{
JSC::VM& vm = lexicalGlobalObject->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
JSValue thisValue = callFrame->thisValue();
JSSQLStatementConstructor* thisObject = jsDynamicCast<JSSQLStatementConstructor*>(thisValue.getObject());
if (UNLIKELY(!thisObject)) {
throwException(lexicalGlobalObject, scope, createError(lexicalGlobalObject, "Expected SQL"_s));
return JSValue::encode(JSC::jsUndefined());
}
if (callFrame->argumentCount() < 2) {
throwException(lexicalGlobalObject, scope, createError(lexicalGlobalObject, "Expected at least 2 arguments"_s));
return JSValue::encode(JSC::jsUndefined());
}
int32_t handle = callFrame->argument(0).toInt32(lexicalGlobalObject);
if (thisObject->databases.size() < handle) {
throwException(lexicalGlobalObject, scope, createError(lexicalGlobalObject, "Invalid database handle"_s));
return JSValue::encode(JSC::jsUndefined());
}
sqlite3* db = thisObject->databases[handle]->db;
if (UNLIKELY(!db)) {
throwException(lexicalGlobalObject, scope, createError(lexicalGlobalObject, "Database has closed"_s));
return JSValue::encode(JSC::jsUndefined());
}
JSC::JSValue sqlValue = callFrame->argument(1);
if (UNLIKELY(!sqlValue.isString())) {
throwException(lexicalGlobalObject, scope, createError(lexicalGlobalObject, "Expected SQL string"_s));
return JSValue::encode(JSC::jsUndefined());
}
EnsureStillAliveScope bindingsAliveScope = callFrame->argumentCount() > 2 ? callFrame->argument(2) : jsUndefined();
auto sqlString = sqlValue.toWTFString(lexicalGlobalObject);
if (UNLIKELY(sqlString.length() == 0)) {
throwException(lexicalGlobalObject, scope, createError(lexicalGlobalObject, "SQL string mustn't be blank"_s));
return JSValue::encode(JSC::jsUndefined());
}
// TODO: trim whitespace & newlines before sending
// we don't because webkit doesn't expose a function that makes this super
// easy without using unicode whitespace definition the
// StringPrototype.trim() implementation GC allocates a new JSString* and
// potentially re-allocates the string (not 100% sure if reallocates) so we
// can't use that here
sqlite3_stmt* statement = nullptr;
int rc = SQLITE_OK;
if (sqlString.is8Bit()) {
rc = sqlite3_prepare_v3(db, reinterpret_cast<const char*>(sqlString.characters8()), sqlString.length(), 0, &statement, nullptr);
} else {
rc = sqlite3_prepare16_v3(db, sqlString.characters16(), sqlString.length() * 2, 0, &statement, nullptr);
}
if (rc != SQLITE_OK || statement == nullptr) {
throwException(lexicalGlobalObject, scope, createError(lexicalGlobalObject, WTF::String::fromUTF8(sqlite3_errmsg(db))));
// sqlite3 handles when the pointer is null
sqlite3_finalize(statement);
return JSValue::encode(JSC::jsUndefined());
}
if (!bindingsAliveScope.value().isUndefinedOrNull()) {
if (bindingsAliveScope.value().isObject()) {
JSC::JSValue reb = rebindStatement(lexicalGlobalObject, bindingsAliveScope.value(), scope, statement, false);
if (UNLIKELY(!reb.isNumber())) {
sqlite3_finalize(statement);
return JSValue::encode(reb); /* this means an error */
}
} else {
throwException(lexicalGlobalObject, scope, createTypeError(lexicalGlobalObject, "Expected bindings to be an object or array"_s));
sqlite3_finalize(statement);
return JSValue::encode(jsUndefined());
}
}
rc = sqlite3_step(statement);
if (!sqlite3_stmt_readonly(statement)) {
thisObject->databases[handle]->version++;
}
// we don't care about the results, therefore the row-by-row output doesn't matter
// that's why we don't bother to loop through the results
if (rc != SQLITE_DONE and rc != SQLITE_ROW) {
throwException(lexicalGlobalObject, scope, createError(lexicalGlobalObject, WTF::String::fromUTF8(sqlite3_errstr(rc))));
// we finalize after just incase something about error messages in
// sqlite depends on the existence of the most recent statement i don't
// think that's actually how this works - just being cautious
sqlite3_finalize(statement);
return JSValue::encode(JSC::jsUndefined());
}
sqlite3_finalize(statement);
return JSValue::encode(jsUndefined());
}
JSC_DEFINE_HOST_FUNCTION(jsSQLStatementIsInTransactionFunction, (JSC::JSGlobalObject * lexicalGlobalObject, JSC::CallFrame* callFrame))
{
JSC::VM& vm = lexicalGlobalObject->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
JSValue thisValue = callFrame->thisValue();
JSSQLStatementConstructor* thisObject = jsDynamicCast<JSSQLStatementConstructor*>(thisValue.getObject());
if (UNLIKELY(!thisObject)) {
throwException(lexicalGlobalObject, scope, createError(lexicalGlobalObject, "Expected SQLStatement"_s));
return JSValue::encode(JSC::jsUndefined());
}
JSC::JSValue dbNumber = callFrame->argument(0);
if (!dbNumber.isNumber()) {
throwException(lexicalGlobalObject, scope, createError(lexicalGlobalObject, "Invalid database handle"_s));
return JSValue::encode(JSC::jsUndefined());
}
int32_t handle = dbNumber.toInt32(lexicalGlobalObject);
if (handle < 0 || handle > thisObject->databases.size()) {
throwException(lexicalGlobalObject, scope, createRangeError(lexicalGlobalObject, "Invalid database handle"_s));
return JSValue::encode(JSC::jsUndefined());
}
sqlite3* db = thisObject->databases[handle]->db;
if (UNLIKELY(!db)) {
throwException(lexicalGlobalObject, scope, createError(lexicalGlobalObject, "Database has closed"_s));
return JSValue::encode(JSC::jsUndefined());
}
RELEASE_AND_RETURN(scope, JSValue::encode(jsBoolean(!sqlite3_get_autocommit(db))));
}
JSC_DEFINE_HOST_FUNCTION(jsSQLStatementPrepareStatementFunction, (JSC::JSGlobalObject * lexicalGlobalObject, JSC::CallFrame* callFrame))
{
JSC::VM& vm = lexicalGlobalObject->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
JSValue thisValue = callFrame->thisValue();
JSSQLStatementConstructor* thisObject = jsDynamicCast<JSSQLStatementConstructor*>(thisValue.getObject());
if (UNLIKELY(!thisObject)) {
throwException(lexicalGlobalObject, scope, createError(lexicalGlobalObject, "Expected SQLStatement"_s));
return JSValue::encode(JSC::jsUndefined());
}
JSC::JSValue dbNumber = callFrame->argument(0);
JSC::JSValue sqlValue = callFrame->argument(1);
JSC::JSValue bindings = callFrame->argument(2);
JSC::JSValue prepareFlagsValue = callFrame->argument(3);
if (!dbNumber.isNumber() || !sqlValue.isString()) {
throwException(lexicalGlobalObject, scope, createError(lexicalGlobalObject, "SQLStatement requires a number and a string"_s));
return JSValue::encode(JSC::jsUndefined());
}
int32_t handle = dbNumber.toInt32(lexicalGlobalObject);
if (handle < 0 || handle > thisObject->databases.size()) {
throwException(lexicalGlobalObject, scope, createRangeError(lexicalGlobalObject, "Invalid database handle"_s));
return JSValue::encode(JSC::jsUndefined());
}
sqlite3* db = thisObject->databases[handle]->db;
if (!db) {
throwException(lexicalGlobalObject, scope, createRangeError(lexicalGlobalObject, "Cannot use a closed database"_s));
return JSValue::encode(JSC::jsUndefined());
}
auto sqlString = sqlValue.toWTFString(lexicalGlobalObject);
if (!sqlString.length()) {
throwException(lexicalGlobalObject, scope, createRangeError(lexicalGlobalObject, "Invalid SQL statement"_s));
return JSValue::encode(JSC::jsUndefined());
}
unsigned int flags = DEFAULT_SQLITE_PREPARE_FLAGS;
if (prepareFlagsValue.isNumber()) {
int prepareFlags = prepareFlagsValue.toInt32(lexicalGlobalObject);
if (prepareFlags < 0 || prepareFlags > MAX_SQLITE_PREPARE_FLAG) {
throwException(lexicalGlobalObject, scope, createRangeError(lexicalGlobalObject, "Invalid prepare flags"_s));
return JSValue::encode(JSC::jsUndefined());
}
flags = static_cast<unsigned int>(prepareFlags);
}
sqlite3_stmt* statement = nullptr;
int rc = SQLITE_OK;
if (sqlString.is8Bit()) {
rc = sqlite3_prepare_v3(db, reinterpret_cast<const char*>(sqlString.characters8()), sqlString.length(), flags, &statement, nullptr);
} else {
rc = sqlite3_prepare16_v3(db, sqlString.characters16(), sqlString.length() * 2, flags, &statement, nullptr);
}
if (rc != SQLITE_OK) {
throwException(lexicalGlobalObject, scope, createError(lexicalGlobalObject, WTF::String::fromUTF8(sqlite3_errmsg(db))));
return JSValue::encode(JSC::jsUndefined());
}
auto* structure = JSSQLStatement::createStructure(vm, lexicalGlobalObject, lexicalGlobalObject->objectPrototype());
// auto* structure = JSSQLStatement::createStructure(vm, globalObject(), thisObject->getDirect(vm, vm.propertyNames->prototype));
JSSQLStatement* sqlStatement = JSSQLStatement::create(
structure, reinterpret_cast<Zig::GlobalObject*>(lexicalGlobalObject), statement, thisObject->databases[handle]);
if (bindings.isObject()) {
auto* castedThis = sqlStatement;
DO_REBIND(bindings)
}
return JSValue::encode(JSValue(sqlStatement));
}
JSSQLStatementConstructor* JSSQLStatementConstructor::create(JSC::VM& vm, JSC::JSGlobalObject* globalObject, JSC::Structure* structure)
{
NativeExecutable* executable = vm.getHostFunction(jsSQLStatementPrepareStatementFunction, ImplementationVisibility::Public, callHostFunctionAsConstructor, String("SQLStatement"_s));
JSSQLStatementConstructor* ptr = new (NotNull, JSC::allocateCell<JSSQLStatementConstructor>(vm)) JSSQLStatementConstructor(vm, executable, globalObject, structure);
ptr->finishCreation(vm);
return ptr;
}
JSC_DEFINE_HOST_FUNCTION(jsSQLStatementOpenStatementFunction, (JSC::JSGlobalObject * lexicalGlobalObject, JSC::CallFrame* callFrame))
{
JSC::VM& vm = lexicalGlobalObject->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
JSValue thisValue = callFrame->thisValue();
JSSQLStatementConstructor* constructor = jsDynamicCast<JSSQLStatementConstructor*>(thisValue.getObject());
if (!constructor) {
throwException(lexicalGlobalObject, scope, createError(lexicalGlobalObject, "Expected SQLStatement"_s));
return JSValue::encode(jsUndefined());
}
if (callFrame->argumentCount() < 1) {
throwException(lexicalGlobalObject, scope, createError(lexicalGlobalObject, "Expected 1 argument"_s));
return JSValue::encode(jsUndefined());
}
JSValue pathValue = callFrame->argument(0);
if (!pathValue.isString()) {
throwException(lexicalGlobalObject, scope, createError(lexicalGlobalObject, "Expected string"_s));
return JSValue::encode(jsUndefined());
}
#if LAZY_LOAD_SQLITE
if (UNLIKELY(lazyLoadSQLite() < 0)) {
WTF::String msg = WTF::String::fromUTF8(dlerror());
throwException(lexicalGlobalObject, scope, createError(lexicalGlobalObject, msg));
return JSValue::encode(JSC::jsUndefined());
}
#endif
auto catchScope = DECLARE_CATCH_SCOPE(vm);
String path = pathValue.toWTFString(lexicalGlobalObject);
RETURN_IF_EXCEPTION(catchScope, JSValue::encode(jsUndefined()));
catchScope.clearException();
int openFlags = DEFAULT_SQLITE_FLAGS;
if (callFrame->argumentCount() > 1) {
JSValue flags = callFrame->argument(1);
if (!flags.isNumber()) {
throwException(lexicalGlobalObject, scope, createError(lexicalGlobalObject, "Expected number"_s));
return JSValue::encode(jsUndefined());
}
openFlags = flags.toInt32(lexicalGlobalObject);
}
sqlite3* db = nullptr;
int statusCode = sqlite3_open_v2(path.utf8().data(), &db, openFlags, nullptr);
if (statusCode != SQLITE_OK) {
throwException(lexicalGlobalObject, scope, createError(lexicalGlobalObject, WTF::String::fromUTF8(sqlite3_errmsg(db))));
return JSValue::encode(jsUndefined());
}
int status = sqlite3_db_config(db, SQLITE_DBCONFIG_ENABLE_LOAD_EXTENSION, 1, NULL);
assert(status == SQLITE_OK);
status = sqlite3_db_config(db, SQLITE_DBCONFIG_DEFENSIVE, 1, NULL);
assert(status == SQLITE_OK);
auto count = constructor->databases.size();
constructor->databases.append(new VersionSqlite3(db));
RELEASE_AND_RETURN(scope, JSValue::encode(jsNumber(count)));
}
JSC_DEFINE_HOST_FUNCTION(jsSQLStatementCloseStatementFunction, (JSC::JSGlobalObject * lexicalGlobalObject, JSC::CallFrame* callFrame))
{
JSC::VM& vm = lexicalGlobalObject->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
JSValue thisValue = callFrame->thisValue();
JSSQLStatementConstructor* constructor = jsDynamicCast<JSSQLStatementConstructor*>(thisValue.getObject());
if (!constructor) {
throwException(lexicalGlobalObject, scope, createError(lexicalGlobalObject, "Expected SQLStatement"_s));
return JSValue::encode(jsUndefined());
}
if (callFrame->argumentCount() < 1) {
throwException(lexicalGlobalObject, scope, createError(lexicalGlobalObject, "Expected 1 argument"_s));
return JSValue::encode(jsUndefined());
}
JSValue dbNumber = callFrame->argument(0);
if (!dbNumber.isNumber()) {
throwException(lexicalGlobalObject, scope, createError(lexicalGlobalObject, "Expected number"_s));
return JSValue::encode(jsUndefined());
}
int dbIndex = dbNumber.toInt32(lexicalGlobalObject);
if (dbIndex < 0 || dbIndex >= constructor->databases.size()) {
throwException(lexicalGlobalObject, scope, createError(lexicalGlobalObject, "Invalid database handle"_s));
return JSValue::encode(jsUndefined());
}
sqlite3* db = constructor->databases[dbIndex]->db;
// no-op if already closed
if (!db) {
return JSValue::encode(jsUndefined());
}
int statusCode = sqlite3_close_v2(db);
if (statusCode != SQLITE_OK) {
throwException(lexicalGlobalObject, scope, createError(lexicalGlobalObject, WTF::String::fromUTF8(sqlite3_errmsg(db))));
return JSValue::encode(jsUndefined());
}
constructor->databases[dbIndex]->db = nullptr;
return JSValue::encode(jsUndefined());
}
/* Hash table for constructor */
static const HashTableValue JSSQLStatementConstructorTableValues[] = {
{ "open"_s, static_cast<unsigned>(JSC::PropertyAttribute::Function), NoIntrinsic, { HashTableValue::NativeFunctionType, jsSQLStatementOpenStatementFunction, 2 } },
{ "close"_s, static_cast<unsigned>(JSC::PropertyAttribute::Function), NoIntrinsic, { HashTableValue::NativeFunctionType, jsSQLStatementCloseStatementFunction, 1 } },
{ "prepare"_s, static_cast<unsigned>(JSC::PropertyAttribute::Function), NoIntrinsic, { HashTableValue::NativeFunctionType, jsSQLStatementPrepareStatementFunction, 2 } },
{ "run"_s, static_cast<unsigned>(JSC::PropertyAttribute::Function), NoIntrinsic, { HashTableValue::NativeFunctionType, jsSQLStatementExecuteFunction, 3 } },
{ "isInTransaction"_s, static_cast<unsigned>(JSC::PropertyAttribute::Function), NoIntrinsic, { HashTableValue::NativeFunctionType, jsSQLStatementIsInTransactionFunction, 1 } },
{ "loadExtension"_s, static_cast<unsigned>(JSC::PropertyAttribute::Function), NoIntrinsic, { HashTableValue::NativeFunctionType, jsSQLStatementLoadExtensionFunction, 2 } },
{ "setCustomSQLite"_s, static_cast<unsigned>(JSC::PropertyAttribute::Function), NoIntrinsic, { HashTableValue::NativeFunctionType, jsSQLStatementSetCustomSQLite, 1 } },
{ "serialize"_s, static_cast<unsigned>(JSC::PropertyAttribute::Function), NoIntrinsic, { HashTableValue::NativeFunctionType, jsSQLStatementSerialize, 1 } },
{ "deserialize"_s, static_cast<unsigned>(JSC::PropertyAttribute::Function), NoIntrinsic, { HashTableValue::NativeFunctionType, jsSQLStatementDeserialize, 2 } },
};
const ClassInfo JSSQLStatementConstructor::s_info = { "SQLStatement"_s, nullptr, nullptr, nullptr, CREATE_METHOD_TABLE(JSSQLStatementConstructor) };
void JSSQLStatementConstructor::finishCreation(VM& vm)
{
Base::finishCreation(vm);
auto* structure = JSSQLStatement::createStructure(vm, globalObject(), globalObject()->objectPrototype());
auto* proto = JSSQLStatement::create(structure, reinterpret_cast<Zig::GlobalObject*>(globalObject()), nullptr, nullptr);
this->putDirect(vm, vm.propertyNames->prototype, proto, PropertyAttribute::DontEnum | PropertyAttribute::DontDelete | PropertyAttribute::ReadOnly);
reifyStaticProperties(vm, JSSQLStatementConstructor::info(), JSSQLStatementConstructorTableValues, *this);
JSC_TO_STRING_TAG_WITHOUT_TRANSITION();
}
static inline JSC::JSValue constructResultObject(JSC::JSGlobalObject* lexicalGlobalObject, JSSQLStatement* castedThis);
static inline JSC::JSValue constructResultObject(JSC::JSGlobalObject* lexicalGlobalObject, JSSQLStatement* castedThis)
{
auto& columnNames = castedThis->columnNames->data()->propertyNameVector();
int count = columnNames.size();
auto& vm = lexicalGlobalObject->vm();
// 64 is the maximum we can preallocate here
// see https://github.com/oven-sh/bun/issues/987
#if SQL_USE_PROTOTYPE == 1
JSC::JSObject* result;
if (count <= 64) {
result = JSC::JSFinalObject::create(vm, castedThis->_prototype.get()->structure());
} else {
result = JSC::JSFinalObject::create(vm, JSC::JSFinalObject::createStructure(vm, lexicalGlobalObject, lexicalGlobalObject->objectPrototype(), std::min(count, 64)));
}
#else
JSC::JSObject* result = JSC::JSFinalObject::create(vm, JSC::JSFinalObject::createStructure(vm, lexicalGlobalObject, lexicalGlobalObject->objectPrototype(), std::min(count, 64)));
#endif
auto* stmt = castedThis->stmt;
for (int i = 0; i < count; i++) {
auto name = columnNames[i];
switch (sqlite3_column_type(stmt, i)) {
case SQLITE_INTEGER: {
result->putDirect(vm, name, jsNumber(sqlite3_column_int(stmt, i)), 0);
break;
}
case SQLITE_FLOAT: {
result->putDirect(vm, name, jsNumber(sqlite3_column_double(stmt, i)), 0);
break;