22extern crate neon;
33extern crate rusqlite;
44
5- use rusqlite:: Connection ;
65use neon:: prelude:: * ;
6+ use rusqlite:: { Connection , NO_PARAMS } ;
77
88fn version ( mut cx : FunctionContext ) -> JsResult < JsString > {
99 Ok ( cx. string ( rusqlite:: version:: version ( ) ) )
@@ -12,7 +12,7 @@ fn version(mut cx: FunctionContext) -> JsResult<JsString> {
1212pub struct Sqlite {
1313 pub conn : Option < Connection > ,
1414 pub database : Option < String > ,
15- pub verbose : Option < bool >
15+ pub verbose : Option < bool > ,
1616}
1717
1818declare_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) {
0 commit comments