use std::future::Future;
use fastauth_core::schema::{ColumnType, IdStorage, TableDef};
use fastauth_core::value::Value;
use sqlx::error::DatabaseError;
use sqlx::{Database, SqlStr};
#[derive(Debug)]
pub struct Param {
pub value: Value,
pub ty: ColumnType,
pub lenient: bool,
}
impl Param {
pub fn write(value: Value, ty: ColumnType) -> Self {
Self {
value,
ty,
lenient: false,
}
}
pub fn filter(value: Value, ty: ColumnType) -> Self {
Self {
value,
ty,
lenient: true,
}
}
}
#[derive(Debug, Default)]
pub struct Catalog {
pub columns: Vec<(String, String)>,
pub indexes: Vec<(String, String)>,
pub id_storage: Vec<(String, String, IdStorage)>,
}
pub fn stored_type(table: &TableDef, ty: ColumnType) -> ColumnType {
match (ty, table.id_storage()) {
(ColumnType::Id, IdStorage::Text) => ColumnType::Text,
_ => ty,
}
}
pub trait Driver: Database {
const TEXT_MATCH: (&'static str, &'static str);
const WILDCARD: char;
const UNIQUE_ON_ADD_COLUMN: bool;
fn param(n: usize, out: &mut String);
fn escape_pattern(text: &str) -> String;
fn ident(name: String) -> String;
fn sql_type(ty: ColumnType) -> &'static str;
fn bool_literal(value: bool) -> &'static str;
fn now_default(adding: bool) -> String;
fn fetch_all(
conn: &mut Self::Connection,
sql: SqlStr,
params: Vec<Param>,
) -> impl Future<Output = sqlx::Result<Vec<Self::Row>>> + Send;
fn execute(
conn: &mut Self::Connection,
sql: SqlStr,
params: Vec<Param>,
) -> impl Future<Output = sqlx::Result<u64>> + Send;
fn execute_script(
conn: &mut Self::Connection,
script: String,
) -> impl Future<Output = sqlx::Result<()>> + Send;
fn introspect(
conn: &mut Self::Connection,
schema: Option<String>,
) -> impl Future<Output = sqlx::Result<Catalog>> + Send;
fn decode(row: &Self::Row, index: usize, ty: ColumnType) -> Result<Value, String>;
fn violated_column(err: &dyn DatabaseError, physical: &str, table: &TableDef)
-> Option<String>;
}