Skip to content

Commit c241396

Browse files
committed
add initial support for query execution
1 parent 0e13ee2 commit c241396

File tree

5 files changed

+55
-9
lines changed

5 files changed

+55
-9
lines changed

native/Cargo.lock

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

native/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "sqlite"
3-
version = "0.1.0"
3+
version = "0.0.1"
44
authors = ["Amila Welihinda <[email protected]>"]
55
license = "MIT"
66
build = "build.rs"

native/src/lib.rs

+33-6
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
extern crate neon;
33
extern crate rusqlite;
44

5-
use rusqlite::Connection;
65
use neon::prelude::*;
6+
use rusqlite::{Connection, NO_PARAMS};
77

88
fn version(mut cx: FunctionContext) -> JsResult<JsString> {
99
Ok(cx.string(rusqlite::version::version()))
@@ -12,7 +12,7 @@ fn version(mut cx: FunctionContext) -> JsResult<JsString> {
1212
pub struct Sqlite {
1313
pub conn: Option<Connection>,
1414
pub database: Option<String>,
15-
pub verbose: Option<bool>
15+
pub verbose: Option<bool>,
1616
}
1717

1818
declare_types! {
@@ -52,22 +52,49 @@ declare_types! {
5252
}
5353

5454
let conn = if database_value == ":memory:".to_string() {
55-
Connection::open_in_memory().unwrap();
55+
Connection::open_in_memory().unwrap()
5656
} else {
57-
Connection::open(&database_value).unwrap();
57+
Connection::open(&database_value).unwrap()
5858
};
5959

60-
let this = cx.this();
6160
let js_database_value = cx.string(database_value);
6261
let js_verbose_value = cx.boolean(verbose_value);
62+
63+
let mut this = cx.this();
6364
this.set(&mut cx, "database", js_database_value)?;
6465
this.set(&mut cx, "verbose", js_verbose_value)?;
66+
cx.borrow_mut(&mut this, |mut sqlite| {
67+
sqlite.conn = Some(conn);
68+
});
6569

6670
Ok(this.upcast())
6771
}
6872

69-
method execute() {
73+
method execute(mut cx) {
74+
let sql = cx.argument::<JsString>(0)?.value();
75+
let this = cx.this();
76+
77+
let vec = cx.borrow(&this, |sqlite| {
78+
let conn = (&sqlite.conn).as_ref().unwrap();
79+
let mut stmt = conn.prepare(&sql).unwrap();
80+
let res: Vec<String> = stmt
81+
.query_map(NO_PARAMS, |row| row.get(0))
82+
.unwrap()
83+
.into_iter()
84+
.map(|r| r.unwrap())
85+
.collect();
86+
res
87+
});
88+
89+
let js_array = JsArray::new(&mut cx, vec.len() as u32);
90+
vec.iter()
91+
.enumerate()
92+
.for_each(|(i, obj)| {
93+
let js_string = cx.string(obj);
94+
js_array.set(&mut cx, i as u32, js_string);
95+
});
7096

97+
Ok(js_array.upcast())
7198
}
7299

73100
method statement(mut cx) {

test/basic.spec.js

+20-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const { version, Sqlite } = require('..');
2+
const path = require('path');
23

34
describe('basic', () => {
45
it('should get sqlite version', () => {
@@ -7,8 +8,26 @@ describe('basic', () => {
78

89
it('should get ', async () => {
910
const conn = new Sqlite();
11+
1012
await conn.create({
11-
verbose: true
13+
verbose: true,
14+
database: path.join(__dirname, 'test.db')
1215
});
16+
17+
conn.execute('DROP TABLE IF EXISTS contacts;');
18+
19+
conn.execute('CREATE TABLE contacts (t VARCHAR PRIMARY KEY);');
20+
21+
conn.execute(`
22+
INSERT INTO contacts (t)
23+
VALUES ("john");
24+
`);
25+
26+
conn.execute(`
27+
SELECT *
28+
FROM contacts;
29+
`);
30+
31+
conn.execute('DROP TABLE contacts;');
1332
})
1433
})

test/test.db

12 KB
Binary file not shown.

0 commit comments

Comments
 (0)