use crate::database::types::{from_query_builder_backend, SqlValue};
use crate::error::Result;
use crate::models::query_builder::{DatabaseBackend, QueryBuilder};
use async_trait::async_trait;
use serde_json::Value as JsonValue;
#[derive(Debug)]
pub struct QueryResult {
pub rows_affected: u64,
pub last_insert_id: Option<i64>,
}
#[async_trait]
pub trait DatabaseAdapter: Send + Sync {
fn name(&self) -> &str;
fn backend(&self) -> DatabaseBackend;
async fn execute(&self, sql: &str, params: Vec<SqlValue>) -> Result<QueryResult>;
async fn fetch_all(&self, sql: &str, params: Vec<SqlValue>) -> Result<Vec<JsonValue>>;
async fn fetch_one(&self, sql: &str, params: Vec<SqlValue>) -> Result<Option<JsonValue>>;
async fn ping(&self) -> Result<bool>;
fn query(&self) -> QueryBuilder {
QueryBuilder::new(self.backend())
}
fn type_backend(&self) -> crate::database::types::DatabaseBackend {
from_query_builder_backend(self.backend())
}
async fn begin_transaction(&self) -> Result<()> {
Err(crate::error::Error::database_transaction(
"Transactions are not yet supported by the generic adapter API. Obtain the underlying sqlx::Pool via as_any() and manage transactions there."
))
}
async fn commit(&self) -> Result<()> {
Err(crate::error::Error::database_transaction(
"Transactions are not yet supported by the generic adapter API. Obtain the underlying sqlx::Pool via as_any() and manage transactions there."
))
}
async fn rollback(&self) -> Result<()> {
Err(crate::error::Error::database_transaction(
"Transactions are not yet supported by the generic adapter API. Obtain the underlying sqlx::Pool via as_any() and manage transactions there."
))
}
fn as_any(&self) -> &dyn std::any::Any;
fn clone_box(&self) -> Box<dyn DatabaseAdapter>;
}
impl Clone for Box<dyn DatabaseAdapter> {
fn clone(&self) -> Self {
self.clone_box()
}
}