use num_enum::TryFromPrimitive;
use std::str::FromStr;
use crate::*;
pub trait FirebirdClient
where
Self: FirebirdClientDbOps,
Self: FirebirdClientSqlOps<DbHandle = <Self as FirebirdClientDbOps>::DbHandle>,
{
}
impl<Hdl, A: FirebirdClientDbOps<DbHandle = Hdl> + FirebirdClientSqlOps<DbHandle = Hdl>>
FirebirdClient for A
where
Hdl: Send,
{
}
pub trait FirebirdClientDbOps: Send {
type DbHandle: Send;
type AttachmentConfig: Send + Clone;
fn attach_database(
&mut self,
config: &Self::AttachmentConfig,
dialect: Dialect,
no_db_triggers: bool,
) -> Result<Self::DbHandle, FbError>;
fn detach_database(&mut self, db_handle: &mut Self::DbHandle) -> Result<(), FbError>;
fn drop_database(&mut self, db_handle: &mut Self::DbHandle) -> Result<(), FbError>;
fn create_database(
&mut self,
config: &Self::AttachmentConfig,
page_size: Option<u32>,
dialect: Dialect,
) -> Result<Self::DbHandle, FbError>;
}
pub trait FirebirdClientSqlOps {
type DbHandle: Send;
type TrHandle: Send;
type StmtHandle: Send;
fn begin_transaction(
&mut self,
db_handle: &mut Self::DbHandle,
confs: TransactionConfiguration,
) -> Result<Self::TrHandle, FbError>;
fn transaction_operation(
&mut self,
tr_handle: &mut Self::TrHandle,
op: TrOp,
) -> Result<(), FbError>;
fn exec_immediate(
&mut self,
db_handle: &mut Self::DbHandle,
tr_handle: &mut Self::TrHandle,
dialect: Dialect,
sql: &str,
) -> Result<(), FbError>;
fn prepare_statement(
&mut self,
db_handle: &mut Self::DbHandle,
tr_handle: &mut Self::TrHandle,
dialect: Dialect,
sql: &str,
) -> Result<(StmtType, Self::StmtHandle), FbError>;
fn free_statement(
&mut self,
stmt_handle: &mut Self::StmtHandle,
op: FreeStmtOp,
) -> Result<(), FbError>;
fn execute(
&mut self,
db_handle: &mut Self::DbHandle,
tr_handle: &mut Self::TrHandle,
stmt_handle: &mut Self::StmtHandle,
params: Vec<SqlType>,
) -> Result<usize, FbError>;
fn execute2(
&mut self,
db_handle: &mut Self::DbHandle,
tr_handle: &mut Self::TrHandle,
stmt_handle: &mut Self::StmtHandle,
params: Vec<SqlType>,
) -> Result<Vec<Column>, FbError>;
fn fetch(
&mut self,
db_handle: &mut Self::DbHandle,
tr_handle: &mut Self::TrHandle,
stmt_handle: &mut Self::StmtHandle,
) -> Result<Option<Vec<Column>>, FbError>;
}
pub trait FirebirdClientDbEvents: FirebirdClientDbOps {
fn wait_for_event(
&mut self,
db_handle: &mut Self::DbHandle,
name: String,
) -> Result<(), FbError>;
}
#[derive(Debug, Eq, PartialEq, Copy, Clone, Default)]
#[repr(u8)]
pub enum Dialect {
D1 = 1,
D2 = 2,
#[default]
D3 = 3,
}
impl FromStr for Dialect {
type Err = FbError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"1" => Ok(Dialect::D1),
"2" => Ok(Dialect::D2),
"3" => Ok(Dialect::D3),
_ => Err(FbError::from(format!(
"'{}' doesn't represent any dialect",
s
))),
}
}
}
#[repr(u8)]
#[derive(Debug, Eq, PartialEq, Copy, Clone)]
pub enum FreeStmtOp {
Close = ibase::DSQL_close as u8,
Drop = ibase::DSQL_drop as u8,
}
#[repr(u8)]
#[derive(Debug, Eq, PartialEq, Copy, Clone, TryFromPrimitive)]
pub enum StmtType {
Select = 1, Insert = 2, Update = 3, Delete = 4, Ddl = 5, GetSegment = 6, PutSegment = 7, ExecProcedure = 8, StartTrans = 9, Commit = 10, Rollback = 11, SelectForUpd = 12, SetGenerator = 13, Savepoint = 14, }