-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
137 additions
and
14 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
use uuid::Uuid; | ||
use wasm_bindgen::JsValue; | ||
|
||
// Borrowed from Rusqlite | ||
pub trait ToSql { | ||
fn to_sql(&self) -> JsValue; | ||
} | ||
impl ToSql for u8 { | ||
fn to_sql(&self) -> JsValue { | ||
JsValue::from_f64(*self as f64) | ||
} | ||
} | ||
impl ToSql for String { | ||
fn to_sql(&self) -> JsValue { | ||
JsValue::from_str(self) | ||
} | ||
} | ||
impl ToSql for Uuid { | ||
fn to_sql(&self) -> JsValue { | ||
JsValue::from_str(&self.to_string()) | ||
} | ||
} | ||
|
||
pub trait Params { | ||
fn to_sql(&self) -> JsValue; | ||
} | ||
impl Params for [&(dyn ToSql + Send + Sync); 0] { | ||
fn to_sql(&self) -> JsValue { | ||
JsValue::NULL | ||
} | ||
} | ||
impl Params for &[&dyn ToSql] { | ||
fn to_sql(&self) -> JsValue { | ||
let array = js_sys::Array::new(); | ||
for item in *self { | ||
array.push(&item.to_sql()); | ||
} | ||
array.into() | ||
} | ||
} | ||
impl Params for &[(&str, &dyn ToSql)] { | ||
fn to_sql(&self) -> JsValue { | ||
let object = js_sys::Object::new(); | ||
for (key, value) in *self { | ||
js_sys::Reflect::set(&object, &JsValue::from_str(key), &value.to_sql()).unwrap(); | ||
} | ||
object.into() | ||
} | ||
} | ||
|
||
#[macro_export] | ||
macro_rules! params { | ||
() => { | ||
&[] as &[&dyn $crate::ToSql] | ||
}; | ||
($($param:expr),+ $(,)?) => { | ||
&[$(&$param as &dyn $crate::ToSql),+] as &[&dyn $crate::ToSql] | ||
}; | ||
} | ||
|
||
#[macro_export] | ||
macro_rules! named_params { | ||
() => { | ||
&[] as &[(&str, &dyn $crate::ToSql)] | ||
}; | ||
// Note: It's a lot more work to support this as part of the same macro as | ||
// `params!`, unfortunately. | ||
($($param_name:literal: $param_val:expr),+ $(,)?) => { | ||
&[$(($param_name, &$param_val as &dyn $crate::ToSql)),+] as &[(&str, &dyn $crate::ToSql)] | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters