use std::any::Any;
use std::future::Future;
use std::pin::Pin;
use async_trait::async_trait;
use crate::types::{SqlRow, SqlStatement, SqlValue, StorageResult};
pub type BoxFuture<'a, T> = Pin<Box<dyn Future<Output = T> + Send + 'a>>;
pub type AtomicUnitOp = Box<
dyn for<'w> FnOnce(&'w mut dyn SqlWriter) -> BoxFuture<'w, StorageResult<Box<dyn Any + Send>>>
+ Send,
>;
#[async_trait]
pub trait SqlReader: Send + 'static {
async fn query_row(&mut self, statement: SqlStatement) -> StorageResult<Option<SqlRow>>;
async fn query_all(&mut self, statement: SqlStatement) -> StorageResult<Vec<SqlRow>>;
async fn query_scalar(&mut self, statement: SqlStatement) -> StorageResult<Option<SqlValue>>;
async fn explain(&mut self, statement: SqlStatement) -> StorageResult<Vec<SqlRow>>;
}
#[async_trait]
pub trait SqlWriter: SqlReader + Send + 'static {
async fn execute(&mut self, statement: SqlStatement) -> StorageResult<u64>;
async fn execute_batch(&mut self, statements: Vec<SqlStatement>) -> StorageResult<u64>;
async fn execute_script(&mut self, script: String) -> StorageResult<()>;
async fn execute_script_top_level(&mut self, script: String) -> StorageResult<()> {
self.execute_script(script).await
}
}
#[async_trait]
pub trait SqlAccess: Send + Sync + 'static {
async fn reader(&self) -> StorageResult<Box<dyn SqlReader>>;
async fn writer(&self) -> StorageResult<Box<dyn SqlWriter>>;
async fn atomic_unit(&self, op: AtomicUnitOp) -> StorageResult<Box<dyn Any + Send>>;
}