lib

Core libraries for Radroots
git clone https://radroots.dev/git/lib.git
Log | Files | Refs | README | LICENSE

lib.rs (1584B)


      1 #![cfg_attr(not(feature = "std"), no_std)]
      2 
      3 extern crate alloc;
      4 
      5 pub mod error;
      6 pub mod migrations;
      7 
      8 #[cfg(all(feature = "native", feature = "std"))]
      9 mod executor_sqlite;
     10 #[cfg(all(feature = "native", feature = "std"))]
     11 pub use executor_sqlite::SqliteExecutor;
     12 #[cfg(all(feature = "native", feature = "std"))]
     13 pub mod sqlite_util;
     14 
     15 #[cfg(feature = "embedded")]
     16 mod executor_embedded;
     17 #[cfg(feature = "embedded")]
     18 pub use executor_embedded::EmbeddedSqlExecutor;
     19 
     20 #[cfg(feature = "std")]
     21 pub mod utils;
     22 
     23 pub use error::SqlError;
     24 
     25 use alloc::string::String;
     26 
     27 #[derive(Clone, Copy, Debug)]
     28 pub struct ExecOutcome {
     29     pub changes: i64,
     30     pub last_insert_id: i64,
     31 }
     32 
     33 pub trait SqlExecutor: Send + Sync {
     34     fn exec(&self, sql: &str, params_json: &str) -> Result<ExecOutcome, SqlError>;
     35     fn query_raw(&self, sql: &str, params_json: &str) -> Result<String, SqlError>;
     36     fn begin(&self) -> Result<(), SqlError>;
     37     fn commit(&self) -> Result<(), SqlError>;
     38     fn rollback(&self) -> Result<(), SqlError>;
     39 }
     40 
     41 impl<T> SqlExecutor for &T
     42 where
     43     T: SqlExecutor + ?Sized,
     44 {
     45     fn exec(&self, sql: &str, params_json: &str) -> Result<ExecOutcome, SqlError> {
     46         (**self).exec(sql, params_json)
     47     }
     48 
     49     fn query_raw(&self, sql: &str, params_json: &str) -> Result<String, SqlError> {
     50         (**self).query_raw(sql, params_json)
     51     }
     52 
     53     fn begin(&self) -> Result<(), SqlError> {
     54         (**self).begin()
     55     }
     56 
     57     fn commit(&self) -> Result<(), SqlError> {
     58         (**self).commit()
     59     }
     60 
     61     fn rollback(&self) -> Result<(), SqlError> {
     62         (**self).rollback()
     63     }
     64 }