aboutsummaryrefslogtreecommitdiff
path: root/src/bun.js/bindings/sqlite/JSSQLStatement.cpp
diff options
context:
space:
mode:
authorGravatar Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com> 2023-01-22 19:09:52 -0800
committerGravatar Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com> 2023-01-22 19:09:52 -0800
commit349224869751c9d74540d5de75bf0681c69dd45d (patch)
tree7dc7f48a085a217f187b80a445b351a7e4f332eb /src/bun.js/bindings/sqlite/JSSQLStatement.cpp
parentbc7192dca1c9da8714f72be3848100e9d16a5425 (diff)
downloadbun-349224869751c9d74540d5de75bf0681c69dd45d.tar.gz
bun-349224869751c9d74540d5de75bf0681c69dd45d.tar.zst
bun-349224869751c9d74540d5de75bf0681c69dd45d.zip
Fixes #1366
Diffstat (limited to 'src/bun.js/bindings/sqlite/JSSQLStatement.cpp')
-rw-r--r--src/bun.js/bindings/sqlite/JSSQLStatement.cpp135
1 files changed, 72 insertions, 63 deletions
diff --git a/src/bun.js/bindings/sqlite/JSSQLStatement.cpp b/src/bun.js/bindings/sqlite/JSSQLStatement.cpp
index efcb12355..39f1f42ed 100644
--- a/src/bun.js/bindings/sqlite/JSSQLStatement.cpp
+++ b/src/bun.js/bindings/sqlite/JSSQLStatement.cpp
@@ -727,9 +727,11 @@ JSC_DEFINE_HOST_FUNCTION(jsSQLStatementExecuteFunction, (JSC::JSGlobalObject * l
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) {
+ while (rc == SQLITE_ROW) {
+ rc = sqlite3_step(statement);
+ }
+
+ if (rc != SQLITE_DONE) {
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
@@ -1206,44 +1208,46 @@ JSC_DEFINE_HOST_FUNCTION(jsSQLStatementExecuteStatementFunctionAll, (JSC::JSGlob
}
size_t columnCount = castedThis->columnNames->size();
- int counter = 0;
+ JSValue result = jsUndefined();
if (status == SQLITE_ROW) {
// this is a count from UPDATE or another query like that
if (columnCount == 0) {
- RELEASE_AND_RETURN(scope, JSC::JSValue::encode(jsNumber(sqlite3_changes(castedThis->version_db->db))));
- }
-
- JSC::JSArray* resultArray = JSC::constructEmptyArray(lexicalGlobalObject, nullptr, 0);
- {
- JSC::ObjectInitializationScope initializationScope(vm);
- JSC::GCDeferralContext deferralContext(vm);
+ result = jsNumber(sqlite3_changes(castedThis->version_db->db));
while (status == SQLITE_ROW) {
- JSC::JSValue result = constructResultObject(lexicalGlobalObject, castedThis);
- resultArray->push(lexicalGlobalObject, result);
status = sqlite3_step(stmt);
}
- }
+ } else {
- if (UNLIKELY(status != SQLITE_DONE)) {
- throwException(lexicalGlobalObject, scope, createError(lexicalGlobalObject, WTF::String::fromUTF8(sqlite3_errstr(status))));
- sqlite3_reset(stmt);
- return JSValue::encode(jsUndefined());
- }
+ JSC::JSArray* resultArray = JSC::constructEmptyArray(lexicalGlobalObject, nullptr, 0);
+ {
+ JSC::ObjectInitializationScope initializationScope(vm);
+ JSC::GCDeferralContext deferralContext(vm);
- RELEASE_AND_RETURN(scope, JSC::JSValue::encode(resultArray));
+ while (status == SQLITE_ROW) {
+ JSC::JSValue result = constructResultObject(lexicalGlobalObject, castedThis);
+ resultArray->push(lexicalGlobalObject, result);
+ status = sqlite3_step(stmt);
+ }
+ }
+ result = resultArray;
+ }
} else if (status == SQLITE_DONE) {
if (columnCount == 0) {
- RELEASE_AND_RETURN(scope, JSValue::encode(jsNumber(0)));
+ result = jsNumber(sqlite3_changes(castedThis->version_db->db));
+ } else {
+ result = JSC::constructEmptyArray(lexicalGlobalObject, nullptr, 0);
}
+ }
- RELEASE_AND_RETURN(scope, JSValue::encode(JSC::constructEmptyArray(lexicalGlobalObject, nullptr, 0)));
- } else {
+ if (UNLIKELY(status != SQLITE_DONE)) {
throwException(lexicalGlobalObject, scope, createError(lexicalGlobalObject, WTF::String::fromUTF8(sqlite3_errstr(status))));
sqlite3_reset(stmt);
return JSValue::encode(jsUndefined());
}
+
+ RELEASE_AND_RETURN(scope, JSC::JSValue::encode(result));
}
JSC_DEFINE_HOST_FUNCTION(jsSQLStatementExecuteStatementFunctionGet, (JSC::JSGlobalObject * lexicalGlobalObject, JSC::CallFrame* callFrame))
@@ -1278,13 +1282,16 @@ JSC_DEFINE_HOST_FUNCTION(jsSQLStatementExecuteStatementFunctionGet, (JSC::JSGlob
initializeColumnNames(lexicalGlobalObject, castedThis);
}
- size_t columnCount = castedThis->columnNames->size();
- int counter = 0;
-
+ JSValue result = jsNull();
if (status == SQLITE_ROW) {
- RELEASE_AND_RETURN(scope, JSC::JSValue::encode(constructResultObject(lexicalGlobalObject, castedThis)));
- } else if (status == SQLITE_DONE) {
- RELEASE_AND_RETURN(scope, JSValue::encode(JSC::jsNull()));
+ result = constructResultObject(lexicalGlobalObject, castedThis);
+ while (status == SQLITE_ROW) {
+ status = sqlite3_step(stmt);
+ }
+ }
+
+ if (status == SQLITE_DONE) {
+ RELEASE_AND_RETURN(scope, JSValue::encode(result));
} else {
throwException(lexicalGlobalObject, scope, createError(lexicalGlobalObject, WTF::String::fromUTF8(sqlite3_errstr(status))));
sqlite3_reset(stmt);
@@ -1298,8 +1305,11 @@ static JSC_DECLARE_JIT_OPERATION_WITHOUT_WTF_INTERNAL(jsSQLStatementExecuteState
JSC_DEFINE_JIT_OPERATION(jsSQLStatementExecuteStatementFunctionGetWithoutTypeChecking, EncodedJSValue, (JSC::JSGlobalObject * lexicalGlobalObject, JSSQLStatement* castedThis))
{
-
- JSC::VM& vm = lexicalGlobalObject->vm();
+ VM& vm = JSC::getVM(lexicalGlobalObject);
+ IGNORE_WARNINGS_BEGIN("frame-address")
+ CallFrame* callFrame = DECLARE_CALL_FRAME(vm);
+ IGNORE_WARNINGS_END
+ JSC::JITOperationPrologueCallFrameTracer tracer(vm, callFrame);
auto scope = DECLARE_THROW_SCOPE(vm);
auto* stmt = castedThis->stmt;
@@ -1320,13 +1330,16 @@ JSC_DEFINE_JIT_OPERATION(jsSQLStatementExecuteStatementFunctionGetWithoutTypeChe
initializeColumnNames(lexicalGlobalObject, castedThis);
}
- size_t columnCount = castedThis->columnNames->size();
- int counter = 0;
-
+ JSValue result = jsNull();
if (status == SQLITE_ROW) {
- RELEASE_AND_RETURN(scope, JSC::JSValue::encode(constructResultObject(lexicalGlobalObject, castedThis)));
- } else if (status == SQLITE_DONE) {
- RELEASE_AND_RETURN(scope, JSValue::encode(JSC::jsNull()));
+ result = constructResultObject(lexicalGlobalObject, castedThis);
+ while (status == SQLITE_ROW) {
+ status = sqlite3_step(stmt);
+ }
+ }
+
+ if (status == SQLITE_DONE) {
+ RELEASE_AND_RETURN(scope, JSValue::encode(result));
} else {
throwException(lexicalGlobalObject, scope, createError(lexicalGlobalObject, WTF::String::fromUTF8(sqlite3_errstr(status))));
sqlite3_reset(stmt);
@@ -1378,46 +1391,42 @@ JSC_DEFINE_HOST_FUNCTION(jsSQLStatementExecuteStatementFunctionRows, (JSC::JSGlo
}
size_t columnCount = castedThis->columnNames->size();
- int counter = 0;
-
+ JSValue result = jsNull();
if (status == SQLITE_ROW) {
// this is a count from UPDATE or another query like that
if (columnCount == 0) {
- RELEASE_AND_RETURN(scope, JSC::JSValue::encode(jsNumber(sqlite3_changes(castedThis->version_db->db))));
- }
-
- JSC::ObjectInitializationScope initializationScope(vm);
- JSC::GCDeferralContext deferralContext(vm);
-
- JSC::JSArray* resultArray = JSC::constructEmptyArray(lexicalGlobalObject, nullptr, 0);
- {
-
while (status == SQLITE_ROW) {
- JSC::JSValue result = constructResultRow(lexicalGlobalObject, castedThis, initializationScope, &deferralContext);
- resultArray->push(lexicalGlobalObject, result);
status = sqlite3_step(stmt);
}
- }
- if (UNLIKELY(status != SQLITE_DONE)) {
- throwException(lexicalGlobalObject, scope, createError(lexicalGlobalObject, WTF::String::fromUTF8(sqlite3_errstr(status))));
- sqlite3_reset(stmt);
- return JSValue::encode(jsUndefined());
- }
+ result = jsNumber(sqlite3_column_count(stmt));
- // sqlite3_reset(stmt);
- RELEASE_AND_RETURN(scope, JSC::JSValue::encode(resultArray));
- } else if (status == SQLITE_DONE) {
- if (columnCount == 0) {
- RELEASE_AND_RETURN(scope, JSValue::encode(jsNumber(0)));
+ } else {
+ JSC::ObjectInitializationScope initializationScope(vm);
+ JSC::GCDeferralContext deferralContext(vm);
+
+ JSC::JSArray* resultArray = JSC::constructEmptyArray(lexicalGlobalObject, nullptr, 0);
+ {
+
+ while (status == SQLITE_ROW) {
+ JSC::JSValue row = constructResultRow(lexicalGlobalObject, castedThis, initializationScope, &deferralContext);
+ resultArray->push(lexicalGlobalObject, row);
+ status = sqlite3_step(stmt);
+ }
+ }
+
+ result = resultArray;
}
+ }
- RELEASE_AND_RETURN(scope, JSValue::encode(JSC::constructEmptyArray(lexicalGlobalObject, nullptr, 0)));
- } else {
+ if (UNLIKELY(status != SQLITE_DONE)) {
throwException(lexicalGlobalObject, scope, createError(lexicalGlobalObject, WTF::String::fromUTF8(sqlite3_errstr(status))));
sqlite3_reset(stmt);
return JSValue::encode(jsUndefined());
}
+
+ // sqlite3_reset(stmt);
+ RELEASE_AND_RETURN(scope, JSC::JSValue::encode(result));
}
JSC_DEFINE_HOST_FUNCTION(jsSQLStatementExecuteStatementFunctionRun, (JSC::JSGlobalObject * lexicalGlobalObject, JSC::CallFrame* callFrame))