|
2 | 2 | extern crate neon;
|
3 | 3 | extern crate rusqlite;
|
4 | 4 |
|
5 |
| -use rusqlite::version::{version as sqlite_version}; |
| 5 | +use rusqlite::Connection; |
6 | 6 | use neon::prelude::*;
|
7 | 7 |
|
8 | 8 | fn version(mut cx: FunctionContext) -> JsResult<JsString> {
|
9 |
| - Ok(cx.string(sqlite_version())) |
| 9 | + Ok(cx.string(rusqlite::version::version())) |
| 10 | +} |
| 11 | + |
| 12 | +pub struct Sqlite { |
| 13 | + pub conn: Option<Connection>, |
| 14 | + pub database: Option<String>, |
| 15 | + pub verbose: Option<bool> |
| 16 | +} |
| 17 | + |
| 18 | +declare_types! { |
| 19 | + pub class JsSqlite for Sqlite { |
| 20 | + init(_cx) { |
| 21 | + Ok(Sqlite { |
| 22 | + conn: None, |
| 23 | + database: None, |
| 24 | + verbose: None |
| 25 | + }) |
| 26 | + } |
| 27 | + |
| 28 | + method create(mut cx) { |
| 29 | + let database_value: String; |
| 30 | + let verbose_value: bool; |
| 31 | + |
| 32 | + match cx.argument_opt(0) { |
| 33 | + Some(arg) => { |
| 34 | + let obj = arg.downcast::<JsObject>().or_throw(&mut cx)?; |
| 35 | + // Handle verbose property defaults |
| 36 | + if obj.get(&mut cx, "verbose")?.is_a::<JsUndefined>() { |
| 37 | + verbose_value = true; |
| 38 | + } else { |
| 39 | + verbose_value = obj.get(&mut cx, "verbose")?.downcast::<JsBoolean>().or_throw(&mut cx)?.value(); |
| 40 | + } |
| 41 | + // Handle database property defaults |
| 42 | + if obj.get(&mut cx, "database")?.is_a::<JsUndefined>() { |
| 43 | + database_value = ":memory:".to_string(); |
| 44 | + } else { |
| 45 | + database_value = obj.get(&mut cx, "database")?.downcast::<JsString>().or_throw(&mut cx)?.value(); |
| 46 | + } |
| 47 | + }, |
| 48 | + None => { |
| 49 | + database_value = ":memory:".to_string(); |
| 50 | + verbose_value = true; |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + let conn = if database_value == ":memory:".to_string() { |
| 55 | + Connection::open_in_memory().unwrap(); |
| 56 | + } else { |
| 57 | + Connection::open(&database_value).unwrap(); |
| 58 | + }; |
| 59 | + |
| 60 | + let this = cx.this(); |
| 61 | + let js_database_value = cx.string(database_value); |
| 62 | + let js_verbose_value = cx.boolean(verbose_value); |
| 63 | + this.set(&mut cx, "database", js_database_value)?; |
| 64 | + this.set(&mut cx, "verbose", js_verbose_value)?; |
| 65 | + |
| 66 | + Ok(this.upcast()) |
| 67 | + } |
| 68 | + |
| 69 | + method execute() { |
| 70 | + |
| 71 | + } |
| 72 | + |
| 73 | + method statement(mut cx) { |
| 74 | + Ok(cx.undefined().upcast()) |
| 75 | + } |
| 76 | + } |
10 | 77 | }
|
11 | 78 |
|
12 | 79 | register_module!(mut m, {
|
13 |
| - m.export_function("version", version) |
| 80 | + m.export_class::<JsSqlite>("Sqlite")?; |
| 81 | + m.export_function("version", version)?; |
| 82 | + Ok(()) |
14 | 83 | });
|
0 commit comments