Skip to content

Commit 2ae8782

Browse files
committed
implemented Statement::Run
1 parent e02ab18 commit 2ae8782

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

src/node_sqlite.cc

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1078,6 +1078,7 @@ void DatabaseSync::Prepare(const FunctionCallbackInfo<Value>& args) {
10781078
int r = sqlite3_prepare_v2(db->connection_, *sql, -1, &s, 0);
10791079
CHECK_ERROR_OR_THROW(env->isolate(), db, r, SQLITE_OK, void());
10801080

1081+
// remove this if condition once all the statement operations are implemented in the Statement class
10811082
if (async) {
10821083
BaseObjectPtr<Statement> stmt =
10831084
Statement::Create(env, BaseObjectPtr<DatabaseSync>(db), s, async);
@@ -2365,8 +2366,18 @@ void Statement::Run(const FunctionCallbackInfo<Value>& args) {
23652366
return;
23662367
}
23672368

2368-
auto after = [env, stmt](int sqlite_status, Local<Promise::Resolver> resolver) {
2369-
// TODO: handle error if sqlite_status != SQLITE_OK
2369+
auto after = [env, stmt](int sqlite_status,
2370+
Local<Promise::Resolver> resolver) {
2371+
if (sqlite_status != SQLITE_OK) {
2372+
Isolate* isolate = env->isolate();
2373+
Local<Object> e;
2374+
if (!CreateSQLiteError(isolate, sqlite_status).ToLocal(&e)) {
2375+
return;
2376+
}
2377+
resolver->Reject(env->context(), e);
2378+
return;
2379+
}
2380+
23702381
Local<Object> result = Object::New(env->isolate());
23712382
sqlite3_int64 last_insert_rowid =
23722383
sqlite3_last_insert_rowid(stmt->db_->Connection());
@@ -2393,7 +2404,7 @@ void Statement::Run(const FunctionCallbackInfo<Value>& args) {
23932404
}
23942405

23952406
resolver->Resolve(env->context(), result);
2396-
};
2407+
};
23972408

23982409
Local<Promise::Resolver> resolver;
23992410
if (!Promise::Resolver::New(env->context()).ToLocal(&resolver)) {

test/parallel/test-sqlite-statement-async.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ const db = new DatabaseSync(':memory:');
1717
db.exec(`CREATE TABLE test (id INTEGER PRIMARY KEY, name TEXT);`);
1818

1919
db.prepare('INSERT INTO test (name) VALUES (?);').run('Sync stuff');
20+
// below an example to test failure: constraint violation
21+
// const p = db.prepare(`INSERT INTO test (id, name) VALUES (1, ?);`, true).run('Async stuff');
2022
const p = db.prepare(`INSERT INTO test (name) VALUES (?);`, true).run('Async stuff');
2123

2224
p.then((result) => {

0 commit comments

Comments
 (0)