Skip to main content

khive_storage/
sql.rs

1//! SQL access capability traits.
2
3use async_trait::async_trait;
4
5use crate::types::{SqlRow, SqlStatement, SqlTxOptions, SqlValue, StorageResult};
6
7/// Read-capable SQL connection.
8#[async_trait]
9pub trait SqlReader: Send + 'static {
10    async fn query_row(&mut self, statement: SqlStatement) -> StorageResult<Option<SqlRow>>;
11    async fn query_all(&mut self, statement: SqlStatement) -> StorageResult<Vec<SqlRow>>;
12    async fn query_scalar(&mut self, statement: SqlStatement) -> StorageResult<Option<SqlValue>>;
13    async fn explain(&mut self, statement: SqlStatement) -> StorageResult<Vec<SqlRow>>;
14}
15
16/// Write-capable SQL connection (extends `SqlReader`).
17#[async_trait]
18pub trait SqlWriter: SqlReader + Send + 'static {
19    async fn execute(&mut self, statement: SqlStatement) -> StorageResult<u64>;
20    async fn execute_batch(&mut self, statements: Vec<SqlStatement>) -> StorageResult<u64>;
21    async fn execute_script(&mut self, script: String) -> StorageResult<()>;
22}
23
24/// A SQL transaction (extends `SqlWriter`).
25#[async_trait]
26pub trait SqlTransaction: SqlWriter + Send + 'static {
27    async fn commit(self: Box<Self>) -> StorageResult<()>;
28    async fn rollback(self: Box<Self>) -> StorageResult<()>;
29}
30
31/// Base SQL access capability.
32#[async_trait]
33pub trait SqlAccess: Send + Sync + 'static {
34    async fn reader(&self) -> StorageResult<Box<dyn SqlReader>>;
35    async fn writer(&self) -> StorageResult<Box<dyn SqlWriter>>;
36    async fn begin_tx(&self, options: SqlTxOptions) -> StorageResult<Box<dyn SqlTransaction>>;
37}