Skip to content

Commit

Permalink
Implement Statement.columns()
Browse files Browse the repository at this point in the history
  • Loading branch information
penberg committed Aug 17, 2023
1 parent 8cee98a commit 38a2636
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 6 deletions.
6 changes: 3 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ exclude = ["index.node"]
crate-type = ["cdylib"]

[dependencies]
libsql = { git = "https://github.com/libsql/libsql/", rev = "3d46e4b328c907b3b4fb229a76bdc9dd335c3334" }
libsql = { git = "https://github.com/libsql/libsql/", rev = "fa76af21b90f1b40d542e67158289588500d2b00" }
tokio = { version = "1.29.1", features = [ "rt-multi-thread" ] }

[dependencies.neon]
Expand Down
2 changes: 1 addition & 1 deletion docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ This function enables or disables raw mode. Prepared statements return objects b

### columns() ⇒ array of objects

This function is currently not supported.
Returns the columns in the result set returned by this prepared statement.

### bind([...bindParameters]) ⇒ this

Expand Down
22 changes: 21 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,20 @@ const { load, currentTarget } = require('@neon-rs/load');
// Static requires for bundlers.
if (0) { require('./.targets'); }

const { databaseOpen, databaseOpenWithRpcSync, databaseClose, databaseSync, databaseExec, databasePrepare, statementRaw, statementGet, statementRun, statementRows, rowsNext } = load(__dirname) || require(`@libsql/experimental-${currentTarget()}`);
const {
databaseOpen,
databaseOpenWithRpcSync,
databaseClose,
databaseSync,
databaseExec,
databasePrepare,
statementRaw,
statementGet,
statementRun,
statementRows,
statementColumns,
rowsNext,
} = load(__dirname) || require(`@libsql/experimental-${currentTarget()}`);

/**
* Database represents a connection that can prepare and execute SQL statements.
Expand Down Expand Up @@ -202,6 +215,13 @@ class Statement {
}
return result;
}

/**
* Returns the columns in the result set returned by this prepared statement.
*/
columns() {
return statementColumns.call(this.stmt);
}
}

module.exports = Database;
29 changes: 29 additions & 0 deletions integration-tests/tests/basic.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,35 @@ test("Statement.all() [raw]", async (t) => {
t.deepEqual(stmt.raw().all(), expected);
});

test("Statement.columns()", async (t) => {
const db = t.context.db;

const stmt = db.prepare("SELECT * FROM users WHERE id = ?");
t.deepEqual(stmt.columns(), [
{
column: 'id',
database: 'main',
name: 'id',
table: 'users',
type: 'INTEGER',
},
{
column: 'name',
database: 'main',
name: 'name',
table: 'users',
type: 'TEXT',
},
{
column: 'email',
database: 'main',
name: 'email',
table: 'users',
type: 'TEXT',
},
]);
});

test("errors", async (t) => {
const db = t.context.db;

Expand Down
23 changes: 23 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,28 @@ impl Statement {
};
Ok(cx.boxed(rows).upcast())
}

fn js_columns(mut cx: FunctionContext) -> JsResult<JsValue> {
let stmt = cx
.this()
.downcast_or_throw::<JsBox<Statement>, _>(&mut cx)?;
let result = cx.empty_array();
for (i, col) in stmt.stmt.columns().iter().enumerate() {
let column = cx.empty_object();
let column_name = cx.string(col.name());
column.set(&mut cx, "column", column_name)?;
let column_origin_name = cx.string(col.origin_name().unwrap());
column.set(&mut cx, "name", column_origin_name)?;
let column_table_name = cx.string(col.table_name().unwrap());
column.set(&mut cx, "table", column_table_name)?;
let column_database_name = cx.string(col.database_name().unwrap());
column.set(&mut cx, "database", column_database_name)?;
let column_decl_type = cx.string(col.decl_type().unwrap());
column.set(&mut cx, "type", column_decl_type)?;
result.set(&mut cx, i as u32, column)?;
}
Ok(result.upcast())
}
}

struct Rows {
Expand Down Expand Up @@ -347,6 +369,7 @@ fn main(mut cx: ModuleContext) -> NeonResult<()> {
cx.export_function("statementRun", Statement::js_run)?;
cx.export_function("statementGet", Statement::js_get)?;
cx.export_function("statementRows", Statement::js_rows)?;
cx.export_function("statementColumns", Statement::js_columns)?;
cx.export_function("rowsNext", Rows::js_next)?;
Ok(())
}

0 comments on commit 38a2636

Please sign in to comment.