rxqlite_common/
lib.rs

1#![deny(unused_extern_crates)]
2#![deny(warnings)]
3
4use chrono::{DateTime, Utc};
5use serde::{Deserialize, Serialize};
6pub use either::Either;
7
8#[derive(Serialize, Deserialize, Debug, Clone)]
9pub enum Value {
10    Null,
11    Bool(bool),
12    Int(i64),
13    F32(f32),
14    F64(f64),
15    String(String),
16    DateTime(DateTime<Utc>),
17    Blob(Vec<u8>),
18}
19impl From<i64> for Value {
20    fn from(i: i64) -> Self {
21        Self::Int(i)
22    }
23}
24impl From<bool> for Value {
25    fn from(b: bool) -> Self {
26        Self::Bool(b)
27    }
28}
29impl From<f32> for Value {
30    fn from(f: f32) -> Self {
31        Self::F32(f)
32    }
33}
34impl From<f64> for Value {
35    fn from(f: f64) -> Self {
36        Self::F64(f)
37    }
38}
39impl From<String> for Value {
40    fn from(s: String) -> Self {
41        Self::String(s)
42    }
43}
44
45impl From<&str> for Value {
46    fn from(s: &str) -> Self {
47        Self::String(s.into())
48    }
49}
50
51impl From<DateTime<Utc>> for Value {
52    fn from(dt: DateTime<Utc>) -> Self {
53        Self::DateTime(dt)
54    }
55}
56
57pub trait FromValueRef {
58    fn from_value_ref(value: &Value) -> Self;
59}
60
61impl FromValueRef for i64 {
62    fn from_value_ref(v: &Value) -> Self {
63        match v {
64            Value::Null => 0,
65            Value::Bool(b) => {
66                if *b {
67                    1
68                } else {
69                    0
70                }
71            }
72            Value::Int(i) => *i,
73            Value::F32(f) => *f as _,
74            Value::F64(f) => *f as _,
75            Value::String(s) => i64::from_str_radix(&s, 10).unwrap_or(0),
76            Value::DateTime(dt) => dt.timestamp() as _,
77            Value::Blob(_) => 0,
78        }
79    }
80}
81impl FromValueRef for i32 {
82    fn from_value_ref(v: &Value) -> Self {
83        match v {
84            Value::Null => 0,
85            Value::Bool(b) => {
86                if *b {
87                    1
88                } else {
89                    0
90                }
91            }
92            Value::Int(i) => *i as _,
93            Value::F32(f) => *f as _,
94            Value::F64(f) => *f as _,
95            Value::String(s) => i32::from_str_radix(&s, 10).unwrap_or(0),
96            Value::DateTime(dt) => dt.timestamp() as _,
97            Value::Blob(_) => 0,
98        }
99    }
100}
101impl FromValueRef for bool {
102    fn from_value_ref(v: &Value) -> Self {
103        match v {
104            Value::Null => false,
105            Value::Bool(b) => *b,
106            Value::Int(i) => *i != 0,
107            Value::F32(f) => *f != 0.,
108            Value::F64(f) => *f != 0.,
109            Value::String(s) => s == "true" || s == "True" || s == "1",
110            Value::DateTime(dt) => dt.timestamp() != 0,
111            Value::Blob(b) => b.len() != 0,
112        }
113    }
114}
115
116impl FromValueRef for f32 {
117    fn from_value_ref(v: &Value) -> Self {
118        match v {
119            Value::Null => 0.,
120            Value::Bool(b) => {
121                if *b {
122                    1.
123                } else {
124                    0.
125                }
126            }
127            Value::Int(i) => *i as _,
128            Value::F32(f) => *f,
129            Value::F64(f) => *f as _,
130            Value::String(s) => s.parse().unwrap_or(0.),
131            Value::DateTime(dt) => dt.timestamp() as _,
132            Value::Blob(_) => 0.,
133        }
134    }
135}
136
137impl FromValueRef for f64 {
138    fn from_value_ref(v: &Value) -> Self {
139        match v {
140            Value::Null => 0.,
141            Value::Bool(b) => {
142                if *b {
143                    1.
144                } else {
145                    0.
146                }
147            }
148            Value::Int(i) => *i as _,
149            Value::F32(f) => *f as _,
150            Value::F64(f) => *f,
151            Value::String(s) => s.parse().unwrap_or(0.),
152            Value::DateTime(dt) => dt.timestamp() as _,
153            Value::Blob(_) => 0.,
154        }
155    }
156}
157
158impl FromValueRef for String {
159    fn from_value_ref(v: &Value) -> Self {
160        match v {
161            Value::Null => Default::default(),
162            Value::Bool(b) => {
163                if *b {
164                    "true".into()
165                } else {
166                    "false".into()
167                }
168            }
169            Value::Int(i) => i.to_string(),
170            Value::F32(f) => f.to_string(),
171            Value::F64(f) => f.to_string(),
172            Value::String(s) => s.clone(),
173            Value::DateTime(dt) => dt.to_rfc3339(),
174            Value::Blob(_) => Default::default(),
175        }
176    }
177}
178
179impl FromValueRef for DateTime<Utc> {
180    fn from_value_ref(v: &Value) -> Self {
181        match v {
182            Value::DateTime(dt) => dt.clone(),
183            _ => Default::default(),
184        }
185    }
186}
187
188#[derive(Serialize, Deserialize, Debug)]
189pub struct Col {
190  pub value: Value,
191  pub ordinal: u64,
192}
193
194impl Col {
195  pub fn new(value: Value, ordinal: u64) -> Self {
196    Self {
197      value,
198      ordinal,
199    }
200  }
201}
202
203impl From<(Value,u64)> for Col {
204  fn from((value,ordinal): (Value,u64))->Self {
205    Self {
206      value,
207      ordinal,
208    }
209  }
210}
211
212impl std::ops::Deref for Col {
213    type Target = Value;
214    fn deref(&self) -> &Self::Target {
215        &self.value
216    }
217}
218
219impl std::ops::DerefMut for Col {
220    fn deref_mut(&mut self) -> &mut Self::Target {
221        &mut self.value
222    }
223}
224
225#[derive(Serialize, Deserialize, Debug, Default)]
226pub struct Row {
227    pub columns: Vec<Column>,
228    pub inner: Vec<Col>,
229}
230
231impl std::ops::Deref for Row {
232    type Target = Vec<Col>;
233    fn deref(&self) -> &Self::Target {
234        &self.inner
235    }
236}
237
238impl std::ops::DerefMut for Row {
239    fn deref_mut(&mut self) -> &mut Self::Target {
240        &mut self.inner
241    }
242}
243
244impl Row {
245    pub fn get<T: FromValueRef>(&self, idx: usize) -> T {
246        T::from_value_ref(&self[idx])
247    }
248}
249
250impl From<(Vec<Column>,Vec<Col>)> for Row {
251    fn from((columns,inner): (Vec<Column>,Vec<Col>)) -> Self {
252        Self { columns, inner }
253    }
254}
255
256
257pub type Rows = Vec<Row>;
258
259#[derive(Serialize, Deserialize, Debug, Clone)]
260pub enum TypeInfo {
261    Null,
262    Int,
263    Float,
264    Text,
265    Blob,
266    Numeric,
267    Bool,
268    Int64,
269    Date,
270    Time,
271    DateTime,
272}
273/*
274#[derive(Serialize, Deserialize, Debug, Clone)]
275pub struct TypeInfo {
276  pub is_null: bool,
277  pub name: String,
278}
279*/
280impl TypeInfo {
281  pub fn is_null(&self) -> bool {
282    match self {
283      TypeInfo::Null=>true,
284      _=>false,
285    }
286  }
287  pub fn name(&self) -> &'static str {
288    match self {
289      Self::Null => "NULL",
290      Self::Int => "INT",
291      Self::Float => "FLOAT",
292      Self::Text => "FLOAT",
293      Self::Blob => "BLOB",
294      Self::Numeric => "NUMERIC",
295      Self::Bool => "BOOL",
296      Self::Int64 => "INTEGER",
297      Self::Date => "DATE",
298      Self::Time => "TIME",
299      Self::DateTime => "DATETIME",
300    }
301  }
302}
303
304#[derive(Serialize, Deserialize, Debug, Clone)]
305pub struct Column {
306  pub ordinal: usize,
307  pub name: String,
308  pub type_info: TypeInfo,
309}
310/*
311#[derive(Serialize, Deserialize, Debug, Clone)]
312pub struct Rows {
313  pub description: ColumnsDescription,
314  pub inner: Vec<Row>,
315}
316
317impl std::ops::Deref for Rows {
318  type Target = Vec<Row>;
319  fn deref(&self) -> &Self::Target {
320    &self.inner
321  }
322}
323
324impl std::ops::DerefMut for Rows {
325  fn deref_mut(&mut self) -> &mut Self::Target {
326    &mut self.inner
327  }
328}
329*/
330#[derive(Serialize, Deserialize, Debug, Default)]
331pub struct QueryResult {
332  pub last_insert_rowid: i64,
333  pub changes: u64,
334}
335
336
337#[derive(Serialize, Deserialize, Debug, Clone)]
338pub enum Message {
339    Execute(String, Vec<Value>),
340    Fetch(String, Vec<Value>),
341    FetchOne(String, Vec<Value>),
342    FetchOptional(String, Vec<Value>),
343    FetchMany(String, Vec<Value>),
344}
345
346impl Message {
347    pub fn sql(&self) -> &str {
348        match self {
349            Self::Execute(s, _) => s.as_str(),
350            Self::Fetch(s, _) => s.as_str(),
351            Self::FetchOne(s, _) => s.as_str(),
352            Self::FetchOptional(s, _) => s.as_str(),
353            Self::FetchMany(s, _) => s.as_str(),
354        }
355    }
356}
357
358#[derive(Serialize, Deserialize, Debug)]
359pub enum MessageResponse {
360    Rows(Rows),
361    QueryResult(QueryResult),
362    QueryResultsAndRows(Vec<Result<Either<QueryResult,Row>,String>>),
363    Error(String),
364}
365
366
367
368#[derive(Debug, Default, Clone, Serialize, Deserialize, PartialEq, Eq)]
369pub struct RSQliteClientTlsConfig {
370    pub cert_paths: Vec<String>,
371    pub accept_invalid_certificates: bool,
372}
373
374impl RSQliteClientTlsConfig {
375    pub fn accept_invalid_certificates(mut self, accept_invalid_certificates: bool) -> Self {
376        self.accept_invalid_certificates = accept_invalid_certificates;
377        self
378    }
379    pub fn add_cert_path(mut self, cert_path: String) -> Self {
380        self.cert_paths.push(cert_path);
381        self
382    }
383}
384/*
385#[derive(Debug, Default, Clone, Serialize, Deserialize)]
386pub struct RSQliteNodeConfig {
387    pub tls_config: Option<RSQliteNodeTlsConfig>,
388}
389*/
390
391/*
392pub use std::sync::atomic::{AtomicBool,Ordering};
393pub const IN_TEST: AtomicBool = AtomicBool::new(false);
394*/