1use num_enum::TryFromPrimitive;
4use std::str::FromStr;
5
6use crate::*;
7
8pub trait FirebirdClient
10where
11 Self: FirebirdClientDbOps,
12 Self: FirebirdClientSqlOps<DbHandle = <Self as FirebirdClientDbOps>::DbHandle>,
13{
14}
15
16impl<Hdl, A: FirebirdClientDbOps<DbHandle = Hdl> + FirebirdClientSqlOps<DbHandle = Hdl>>
17 FirebirdClient for A
18where
19 Hdl: Send,
20{
21}
22
23pub trait FirebirdClientDbOps: Send {
25 type DbHandle: Send;
27
28 type AttachmentConfig: Send + Clone;
33
34 fn attach_database(
37 &mut self,
38 config: &Self::AttachmentConfig,
39 dialect: Dialect,
40 no_db_triggers: bool,
41 ) -> Result<Self::DbHandle, FbError>;
42
43 fn detach_database(&mut self, db_handle: &mut Self::DbHandle) -> Result<(), FbError>;
45
46 fn drop_database(&mut self, db_handle: &mut Self::DbHandle) -> Result<(), FbError>;
48
49 fn create_database(
52 &mut self,
53 config: &Self::AttachmentConfig,
54 page_size: Option<u32>,
55 dialect: Dialect,
56 ) -> Result<Self::DbHandle, FbError>;
57}
58
59pub trait FirebirdClientSqlOps {
61 type DbHandle: Send;
63 type TrHandle: Send;
65 type StmtHandle: Send;
67
68 fn begin_transaction(
70 &mut self,
71 db_handle: &mut Self::DbHandle,
72 confs: TransactionConfiguration,
73 ) -> Result<Self::TrHandle, FbError>;
74
75 fn transaction_operation(
77 &mut self,
78 tr_handle: &mut Self::TrHandle,
79 op: TrOp,
80 ) -> Result<(), FbError>;
81
82 fn exec_immediate(
84 &mut self,
85 db_handle: &mut Self::DbHandle,
86 tr_handle: &mut Self::TrHandle,
87 dialect: Dialect,
88 sql: &str,
89 ) -> Result<(), FbError>;
90
91 fn prepare_statement(
94 &mut self,
95 db_handle: &mut Self::DbHandle,
96 tr_handle: &mut Self::TrHandle,
97 dialect: Dialect,
98 sql: &str,
99 ) -> Result<(StmtType, Self::StmtHandle), FbError>;
100
101 fn free_statement(
103 &mut self,
104 stmt_handle: &mut Self::StmtHandle,
105 op: FreeStmtOp,
106 ) -> Result<(), FbError>;
107
108 fn execute(
111 &mut self,
112 db_handle: &mut Self::DbHandle,
113 tr_handle: &mut Self::TrHandle,
114 stmt_handle: &mut Self::StmtHandle,
115 params: Vec<SqlType>,
116 ) -> Result<usize, FbError>;
117
118 fn execute2(
124 &mut self,
125 db_handle: &mut Self::DbHandle,
126 tr_handle: &mut Self::TrHandle,
127 stmt_handle: &mut Self::StmtHandle,
128 params: Vec<SqlType>,
129 ) -> Result<Vec<Column>, FbError>;
130
131 fn fetch(
134 &mut self,
135 db_handle: &mut Self::DbHandle,
136 tr_handle: &mut Self::TrHandle,
137 stmt_handle: &mut Self::StmtHandle,
138 ) -> Result<Option<Vec<Column>>, FbError>;
139}
140
141pub trait FirebirdClientDbEvents: FirebirdClientDbOps {
143 fn wait_for_event(
145 &mut self,
146 db_handle: &mut Self::DbHandle,
147 name: String,
148 ) -> Result<(), FbError>;
149}
150
151#[derive(Debug, Eq, PartialEq, Copy, Clone, Default)]
152#[repr(u8)]
153pub enum Dialect {
155 D1 = 1,
156 D2 = 2,
157 #[default]
158 D3 = 3,
159}
160
161impl FromStr for Dialect {
162 type Err = FbError;
163
164 fn from_str(s: &str) -> Result<Self, Self::Err> {
165 match s {
166 "1" => Ok(Dialect::D1),
167 "2" => Ok(Dialect::D2),
168 "3" => Ok(Dialect::D3),
169 _ => Err(FbError::from(format!(
170 "'{}' doesn't represent any dialect",
171 s
172 ))),
173 }
174 }
175}
176
177#[repr(u8)]
178#[derive(Debug, Eq, PartialEq, Copy, Clone)]
179pub enum FreeStmtOp {
181 Close = ibase::DSQL_close as u8,
182 Drop = ibase::DSQL_drop as u8,
183}
184
185#[repr(u8)]
186#[derive(Debug, Eq, PartialEq, Copy, Clone, TryFromPrimitive)]
187pub enum StmtType {
189 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, }