Skip to content

Commit f0f56f1

Browse files
committed
Fix Statement.columns() for SELECT 1-like queries
1 parent 19f937c commit f0f56f1

File tree

3 files changed

+50
-8
lines changed

3 files changed

+50
-8
lines changed

integration-tests/tests/async.test.js

+14-1
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,20 @@ test.serial("Statement.all() [statement safe integers]", async (t) => {
156156
test.serial("Statement.columns()", async (t) => {
157157
const db = t.context.db;
158158

159-
const stmt = await db.prepare("SELECT * FROM users WHERE id = ?");
159+
var stmt = undefined;
160+
161+
stmt = await db.prepare("SELECT 1");
162+
t.deepEqual(stmt.columns(), [
163+
{
164+
column: null,
165+
database: null,
166+
name: '1',
167+
table: null,
168+
type: null,
169+
},
170+
]);
171+
172+
stmt = await db.prepare("SELECT * FROM users WHERE id = ?");
160173
t.deepEqual(stmt.columns(), [
161174
{
162175
column: "id",

integration-tests/tests/sync.test.js

+14-1
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,20 @@ test.serial("Statement.all() [statement safe integers]", async (t) => {
154154
test.serial("Statement.columns()", async (t) => {
155155
const db = t.context.db;
156156

157-
const stmt = db.prepare("SELECT * FROM users WHERE id = ?");
157+
var stmt = undefined;
158+
159+
stmt = db.prepare("SELECT 1");
160+
t.deepEqual(stmt.columns(), [
161+
{
162+
column: null,
163+
database: null,
164+
name: '1',
165+
table: null,
166+
type: null,
167+
},
168+
]);
169+
170+
stmt = db.prepare("SELECT * FROM users WHERE id = ?");
158171
t.deepEqual(stmt.columns(), [
159172
{
160173
column: "id",

src/lib.rs

+22-6
Original file line numberDiff line numberDiff line change
@@ -351,14 +351,30 @@ impl Statement {
351351
for (i, col) in stmt.stmt.columns().iter().enumerate() {
352352
let column = cx.empty_object();
353353
let column_name = cx.string(col.name());
354-
column.set(&mut cx, "column", column_name)?;
355-
let column_origin_name = cx.string(col.origin_name().unwrap());
356-
column.set(&mut cx, "name", column_origin_name)?;
357-
let column_table_name = cx.string(col.table_name().unwrap());
354+
column.set(&mut cx, "name", column_name)?;
355+
let column_origin_name: Handle<'_, JsValue> = if let Some(origin_name) = col.origin_name() {
356+
cx.string(origin_name).upcast()
357+
} else {
358+
cx.null().upcast()
359+
};
360+
column.set(&mut cx, "column", column_origin_name)?;
361+
let column_table_name: Handle<'_, JsValue> = if let Some(table_name) = col.table_name() {
362+
cx.string(table_name).upcast()
363+
} else {
364+
cx.null().upcast()
365+
};
358366
column.set(&mut cx, "table", column_table_name)?;
359-
let column_database_name = cx.string(col.database_name().unwrap());
367+
let column_database_name: Handle<'_, JsValue> = if let Some(database_name) = col.database_name() {
368+
cx.string(database_name).upcast()
369+
} else {
370+
cx.null().upcast()
371+
};
360372
column.set(&mut cx, "database", column_database_name)?;
361-
let column_decl_type = cx.string(col.decl_type().unwrap());
373+
let column_decl_type: Handle<'_, JsValue> = if let Some(decl_type) = col.decl_type() {
374+
cx.string(decl_type).upcast()
375+
} else {
376+
cx.null().upcast()
377+
};
362378
column.set(&mut cx, "type", column_decl_type)?;
363379
result.set(&mut cx, i as u32, column)?;
364380
}

0 commit comments

Comments
 (0)