Skip to content

Commit 2ac5142

Browse files
authored
Merge pull request #124 from tursodatabase/fix-statement-run
Fix Statement.run() for SELECT statements
2 parents c9c1cb3 + 2df0e5e commit 2ac5142

File tree

4 files changed

+33
-14
lines changed

4 files changed

+33
-14
lines changed

Cargo.lock

Lines changed: 8 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ crate-type = ["cdylib"]
1212

1313
[dependencies]
1414
tracing-subscriber = { version = "0.3", features = ["env-filter"] }
15-
libsql = { git = "https://github.com/tursodatabase/libsql/", rev = "8521f874f67365fc07f0469681f3351d65f3c761", features = ["encryption"] }
15+
libsql = { git = "https://github.com/tursodatabase/libsql/", rev = "dd548a902b56cc5b9daa3cec7a43ca887ea81136", features = ["encryption"] }
1616
tracing = "0.1"
1717
once_cell = "1.18.0"
1818
tokio = { version = "1.29.1", features = [ "rt-multi-thread" ] }

integration-tests/tests/sync.test.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,14 @@ test.serial("Statement.prepare() error", async (t) => {
4141
});
4242
});
4343

44+
test.serial("Statement.run() returning rows", async (t) => {
45+
const db = t.context.db;
46+
47+
const stmt = db.prepare("SELECT 1");
48+
const info = stmt.run();
49+
t.is(info.changes, 0);
50+
});
51+
4452
test.serial("Statement.run() [positional]", async (t) => {
4553
const db = t.context.db;
4654

src/statement.rs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,16 +76,27 @@ impl Statement {
7676

7777
pub fn js_run(mut cx: FunctionContext) -> JsResult<JsValue> {
7878
let stmt: Handle<'_, JsBox<Statement>> = cx.this()?;
79+
let raw_conn = stmt.conn.clone();
80+
let total_changes_before = raw_conn.blocking_lock().total_changes();
7981
let params = cx.argument::<JsValue>(0)?;
8082
let params = convert_params(&mut cx, &stmt, params)?;
8183
let mut raw_stmt = stmt.stmt.blocking_lock();
8284
raw_stmt.reset();
83-
let fut = raw_stmt.execute(params);
85+
let fut = raw_stmt.run(params);
8486
let rt = runtime(&mut cx)?;
85-
let result = rt.block_on(fut);
86-
let changes = result.or_else(|err| throw_libsql_error(&mut cx, err))?;
87-
let raw_conn = stmt.conn.clone();
88-
let last_insert_rowid = raw_conn.blocking_lock().last_insert_rowid();
87+
rt.block_on(fut)
88+
.or_else(|err| throw_libsql_error(&mut cx, err))?;
89+
let (changes, last_insert_rowid) = {
90+
let raw_conn = stmt.conn.clone();
91+
let raw_conn = raw_conn.blocking_lock();
92+
let changes = if raw_conn.total_changes() == total_changes_before {
93+
0
94+
} else {
95+
raw_conn.changes()
96+
};
97+
let last_insert_rowid = raw_conn.last_insert_rowid();
98+
(changes, last_insert_rowid)
99+
};
89100
let info = cx.empty_object();
90101
let changes = cx.number(changes as f64);
91102
info.set(&mut cx, "changes", changes)?;

0 commit comments

Comments
 (0)