Skip to main content

nextsql_backend_rust_runtime/
lib.rs

1use std::future::Future;
2
3/// Trait for values that can be bound as SQL query parameters.
4pub trait ToSqlParam: Send + Sync {
5    /// Convert this value into a format the database driver can accept.
6    /// The returned Any should be downcastable to the backend's native param type.
7    fn as_any(&self) -> &(dyn std::any::Any + Send + Sync);
8}
9
10/// Trait for reading typed values from a database result row.
11pub trait Row: Send {
12    fn get_i16(&self, idx: usize) -> i16;
13    fn get_i32(&self, idx: usize) -> i32;
14    fn get_i64(&self, idx: usize) -> i64;
15    fn get_f32(&self, idx: usize) -> f32;
16    fn get_f64(&self, idx: usize) -> f64;
17    fn get_string(&self, idx: usize) -> String;
18    fn get_bool(&self, idx: usize) -> bool;
19    fn get_uuid(&self, idx: usize) -> uuid::Uuid;
20    fn get_timestamp(&self, idx: usize) -> chrono::NaiveDateTime;
21    fn get_timestamptz(&self, idx: usize) -> chrono::DateTime<chrono::Utc>;
22    fn get_date(&self, idx: usize) -> chrono::NaiveDate;
23
24    // Optional variants for nullable columns
25    fn get_opt_i16(&self, idx: usize) -> Option<i16>;
26    fn get_opt_i32(&self, idx: usize) -> Option<i32>;
27    fn get_opt_i64(&self, idx: usize) -> Option<i64>;
28    fn get_opt_f32(&self, idx: usize) -> Option<f32>;
29    fn get_opt_f64(&self, idx: usize) -> Option<f64>;
30    fn get_opt_string(&self, idx: usize) -> Option<String>;
31    fn get_opt_bool(&self, idx: usize) -> Option<bool>;
32    fn get_opt_uuid(&self, idx: usize) -> Option<uuid::Uuid>;
33    fn get_opt_timestamp(&self, idx: usize) -> Option<chrono::NaiveDateTime>;
34    fn get_opt_timestamptz(&self, idx: usize) -> Option<chrono::DateTime<chrono::Utc>>;
35    fn get_opt_date(&self, idx: usize) -> Option<chrono::NaiveDate>;
36
37    // Array variants
38    fn get_vec_i16(&self, idx: usize) -> Vec<i16>;
39    fn get_vec_i32(&self, idx: usize) -> Vec<i32>;
40    fn get_vec_i64(&self, idx: usize) -> Vec<i64>;
41    fn get_vec_f32(&self, idx: usize) -> Vec<f32>;
42    fn get_vec_f64(&self, idx: usize) -> Vec<f64>;
43    fn get_vec_string(&self, idx: usize) -> Vec<String>;
44    fn get_vec_bool(&self, idx: usize) -> Vec<bool>;
45    fn get_vec_uuid(&self, idx: usize) -> Vec<uuid::Uuid>;
46}
47
48/// Trait for executing SQL queries against a database.
49/// Uses `impl Future` instead of async_trait to avoid proc macro compile overhead.
50pub trait Client: Send + Sync {
51    type Error: std::error::Error + Send + Sync + 'static;
52    type Row: Row;
53
54    /// Execute a query that returns rows.
55    fn query(
56        &self,
57        sql: &str,
58        params: &[&dyn ToSqlParam],
59    ) -> impl Future<Output = Result<Vec<Self::Row>, Self::Error>> + Send;
60
61    /// Execute a statement that returns the number of affected rows.
62    fn execute(
63        &self,
64        sql: &str,
65        params: &[&dyn ToSqlParam],
66    ) -> impl Future<Output = Result<u64, Self::Error>> + Send;
67}
68
69#[cfg(feature = "backend-tokio-postgres")]
70pub mod tokio_postgres_impl;