use async_trait::async_trait;
use fraiseql_error::Result;
use super::{ChangeLogWrite, DatabaseAdapter, MutationRowGate};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum WriteMode {
Commit,
DryRun,
}
#[derive(Debug, Clone, Copy)]
#[non_exhaustive]
pub struct WriteRequest<'a> {
pub function: &'a str,
pub args: &'a [serde_json::Value],
pub session_vars: &'a [(&'a str, &'a str)],
pub changelog: Option<&'a ChangeLogWrite<'a>>,
pub mode: WriteMode,
}
impl<'a> WriteRequest<'a> {
#[must_use]
pub const fn new(function: &'a str, args: &'a [serde_json::Value]) -> Self {
Self {
function,
args,
session_vars: &[],
changelog: None,
mode: WriteMode::Commit,
}
}
#[must_use]
pub const fn with_session_vars(mut self, session_vars: &'a [(&'a str, &'a str)]) -> Self {
self.session_vars = session_vars;
self
}
#[must_use]
pub const fn with_changelog(mut self, changelog: Option<&'a ChangeLogWrite<'a>>) -> Self {
self.changelog = changelog;
self
}
#[must_use]
pub const fn with_mode(mut self, mode: WriteMode) -> Self {
self.mode = mode;
self
}
}
#[async_trait]
pub trait Writer: DatabaseAdapter {
async fn execute_write(
&self,
request: &WriteRequest<'_>,
gate: MutationRowGate<'_>,
) -> Result<Vec<std::collections::HashMap<String, serde_json::Value>>>;
async fn bump_fact_table_versions(&self, _tables: &[String]) -> Result<()> {
Ok(())
}
}