Skip to main content

rust_d1_orm/
set.rs

1use worker::wasm_bindgen::JsValue;
2
3pub struct Set {
4    fields: Vec<(String, JsValue)>,
5}
6
7impl Set {
8    pub fn new() -> Self {
9        Self { fields: vec![] }
10    }
11
12    pub fn field(mut self, col: &str, val: impl Into<JsValue>) -> Self {
13        self.fields.push((col.to_string(), val.into()));
14        self
15    }
16
17    pub fn nullable_field(mut self, col: &str, val: Option<impl Into<JsValue>>) -> Self {
18        let js = val.map(Into::into).unwrap_or(JsValue::NULL);
19        self.fields.push((col.to_string(), js));
20        self
21    }
22
23    pub fn is_empty(&self) -> bool {
24        self.fields.is_empty()
25    }
26
27    pub(crate) fn build(&self, param_start: usize) -> (String, Vec<JsValue>, usize) {
28        let mut parts: Vec<String> = vec![];
29        let mut values: Vec<JsValue> = vec![];
30        let mut n = param_start;
31        for (col, val) in &self.fields {
32            parts.push(format!("{} = ?{}", col, n));
33            values.push(val.clone());
34            n += 1;
35        }
36        (parts.join(", "), values, n)
37    }
38}
39
40impl Default for Set {
41    fn default() -> Self { Self::new() }
42}