aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Derrick Farris <mr.dcfarris@gmail.com> 2023-02-27 20:29:00 -0600
committerGravatar GitHub <noreply@github.com> 2023-02-27 18:29:00 -0800
commitae35f17a99c96372bcdd519e8c5202e08a419379 (patch)
treee12ef96ed39554e9a5dabf877f839c38b670887d
parent062b5565a7a00aa7f85b404eb45b6c8be6d7b52b (diff)
downloadbun-ae35f17a99c96372bcdd519e8c5202e08a419379.tar.gz
bun-ae35f17a99c96372bcdd519e8c5202e08a419379.tar.zst
bun-ae35f17a99c96372bcdd519e8c5202e08a419379.zip
fix(bun:sqlite): fix `sqliteDb.run(' ')` throwing `not an error` (#2226)
-rw-r--r--src/bun.js/bindings/sqlite/JSSQLStatement.cpp16
-rw-r--r--test/bun.js/sqlite.test.js23
2 files changed, 31 insertions, 8 deletions
diff --git a/src/bun.js/bindings/sqlite/JSSQLStatement.cpp b/src/bun.js/bindings/sqlite/JSSQLStatement.cpp
index f375233dc..46d8a2442 100644
--- a/src/bun.js/bindings/sqlite/JSSQLStatement.cpp
+++ b/src/bun.js/bindings/sqlite/JSSQLStatement.cpp
@@ -701,8 +701,8 @@ JSC_DEFINE_HOST_FUNCTION(jsSQLStatementExecuteFunction, (JSC::JSGlobalObject * l
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))));
+ if (UNLIKELY(rc != SQLITE_OK || statement == nullptr)) {
+ throwException(lexicalGlobalObject, scope, createError(lexicalGlobalObject, rc == SQLITE_OK ? "Query contained no valid SQL statement; likely empty query."_s : WTF::String::fromUTF8(sqlite3_errmsg(db))));
// sqlite3 handles when the pointer is null
sqlite3_finalize(statement);
return JSValue::encode(JSC::jsUndefined());
@@ -731,7 +731,7 @@ JSC_DEFINE_HOST_FUNCTION(jsSQLStatementExecuteFunction, (JSC::JSGlobalObject * l
rc = sqlite3_step(statement);
}
- if (rc != SQLITE_DONE) {
+ if (UNLIKELY(rc != SQLITE_DONE && rc != SQLITE_OK)) {
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
@@ -1241,7 +1241,7 @@ JSC_DEFINE_HOST_FUNCTION(jsSQLStatementExecuteStatementFunctionAll, (JSC::JSGlob
}
}
- if (UNLIKELY(status != SQLITE_DONE)) {
+ if (UNLIKELY(status != SQLITE_DONE && status != SQLITE_OK)) {
throwException(lexicalGlobalObject, scope, createError(lexicalGlobalObject, WTF::String::fromUTF8(sqlite3_errstr(status))));
sqlite3_reset(stmt);
return JSValue::encode(jsUndefined());
@@ -1290,7 +1290,7 @@ JSC_DEFINE_HOST_FUNCTION(jsSQLStatementExecuteStatementFunctionGet, (JSC::JSGlob
}
}
- if (status == SQLITE_DONE) {
+ if (status == SQLITE_DONE || status == SQLITE_OK) {
RELEASE_AND_RETURN(scope, JSValue::encode(result));
} else {
throwException(lexicalGlobalObject, scope, createError(lexicalGlobalObject, WTF::String::fromUTF8(sqlite3_errstr(status))));
@@ -1338,7 +1338,7 @@ JSC_DEFINE_JIT_OPERATION(jsSQLStatementExecuteStatementFunctionGetWithoutTypeChe
}
}
- if (status == SQLITE_DONE) {
+ if (status == SQLITE_DONE || status == SQLITE_OK) {
RELEASE_AND_RETURN(scope, JSValue::encode(result));
} else {
throwException(lexicalGlobalObject, scope, createError(lexicalGlobalObject, WTF::String::fromUTF8(sqlite3_errstr(status))));
@@ -1419,7 +1419,7 @@ JSC_DEFINE_HOST_FUNCTION(jsSQLStatementExecuteStatementFunctionRows, (JSC::JSGlo
}
}
- if (UNLIKELY(status != SQLITE_DONE)) {
+ if (UNLIKELY(status != SQLITE_DONE && status != SQLITE_OK)) {
throwException(lexicalGlobalObject, scope, createError(lexicalGlobalObject, WTF::String::fromUTF8(sqlite3_errstr(status))));
sqlite3_reset(stmt);
return JSValue::encode(jsUndefined());
@@ -1465,7 +1465,7 @@ JSC_DEFINE_HOST_FUNCTION(jsSQLStatementExecuteStatementFunctionRun, (JSC::JSGlob
status = sqlite3_step(stmt);
}
- if (UNLIKELY(status != SQLITE_DONE)) {
+ if (UNLIKELY(status != SQLITE_DONE && status != SQLITE_OK)) {
sqlite3_reset(stmt);
throwException(lexicalGlobalObject, scope, createError(lexicalGlobalObject, WTF::String::fromUTF8(sqlite3_errstr(status))));
return JSValue::encode(jsUndefined());
diff --git a/test/bun.js/sqlite.test.js b/test/bun.js/sqlite.test.js
index 3120283f4..408fa5a26 100644
--- a/test/bun.js/sqlite.test.js
+++ b/test/bun.js/sqlite.test.js
@@ -504,3 +504,26 @@ it("latin1 supplement chars", () => {
},
]);
});
+
+describe("Database.run", () => {
+ it("should not throw error `not an error` when provided query containing only whitespace", () => {
+ const db = Database.open(":memory:");
+ db.exec("CREATE TABLE test (id INTEGER PRIMARY KEY, name TEXT)");
+
+ expect(db[Symbol.for("Bun.Database.cache.count")]).toBe(0);
+
+ var q = db.query("SELECT * FROM test WHERE name = ?");
+ expect(q.get("Hello") === null).toBe(true);
+
+ db.exec('INSERT INTO test (name) VALUES ("Hello")');
+ db.exec('INSERT INTO test (name) VALUES ("World")');
+
+ try {
+ db.run(" ");
+ expect(true).toBeFalsy();
+ } catch (e) {
+ expect(e.message).not.toBe("not an error");
+ expect(e.message).toBe("Query contained no valid SQL statement; likely empty query.");
+ }
+ });
+});