bind/js: Apply pluck's logic to all methods

This commit is contained in:
Diego Reis 2025-05-26 17:14:33 -03:00
parent b60fd81995
commit b012d07aa3
2 changed files with 29 additions and 2 deletions

View file

@ -65,13 +65,13 @@ test("Empty prepared statement should throw", async (t) => {
);
});
test("Test pragma", async (t) => {
test("Test pragma()", async (t) => {
const [db] = await connect(":memory:");
t.true(typeof db.pragma("cache_size")[0].cache_size === "number");
t.true(typeof db.pragma("cache_size", { simple: true }) === "number");
});
test("Test bind()", async (t) => {
test("Statement binded with bind() shouldn't be binded again", async (t) => {
const [db] = await connect(":memory:");
db.prepare("CREATE TABLE users (name TEXT, age INTEGER)").run();
db.prepare("INSERT INTO users (name, age) VALUES (?, ?)").run("Alice", 42);
@ -90,6 +90,19 @@ test("Test bind()", async (t) => {
);
});
test("Test pluck(): Rows should only have the values of the first column", async (t) => {
const [db] = await connect(":memory:");
db.prepare("CREATE TABLE users (name TEXT, age INTEGER)").run();
db.prepare("INSERT INTO users (name, age) VALUES (?, ?)").run("Alice", 42);
db.prepare("INSERT INTO users (name, age) VALUES (?, ?)").run("Bob", 24);
let stmt = db.prepare("SELECT * FROM users").pluck();
for (const row of stmt.iterate()) {
t.truthy(row.name);
t.true(typeof row.age === "undefined");
}
});
const connect = async (path) => {
const db = new Database(path);
return [db];

View file

@ -214,6 +214,10 @@ impl Statement {
let key = stmt.get_column_name(idx);
let js_value = to_js_value(&env, value);
obj.set_named_property(&key, js_value)?;
if self.pluck {
return Ok(obj.into_unknown());
}
}
Ok(obj.into_unknown())
}
@ -245,6 +249,7 @@ impl Statement {
stmt: Rc::clone(&self.inner),
database: self.database.clone(),
env,
plucked: self.pluck,
})
}
@ -271,6 +276,10 @@ impl Statement {
let key = stmt.get_column_name(idx);
let js_value = to_js_value(&env, value);
obj.set_named_property(&key, js_value)?;
if self.pluck {
break;
}
}
results.set_element(index, obj)?;
index += 1;
@ -356,6 +365,7 @@ pub struct IteratorStatement {
stmt: Rc<RefCell<limbo_core::Statement>>,
database: Database,
env: Env,
plucked: bool,
}
impl Generator for IteratorStatement {
@ -377,6 +387,10 @@ impl Generator for IteratorStatement {
let key = stmt.get_column_name(idx);
let js_value = to_js_value(&self.env, value);
js_row.set_named_property(&key, js_value).ok()?;
if self.plucked {
break;
}
}
Some(js_row)