sea_orm/database/
connection.rs1use crate::{
2 DatabaseTransaction, DbBackend, DbErr, ExecResult, QueryResult, Statement, StatementBuilder,
3 TransactionError,
4};
5use futures_util::Stream;
6use std::{future::Future, pin::Pin};
7
8#[async_trait::async_trait]
11pub trait ConnectionTrait: Sync {
12 fn get_database_backend(&self) -> DbBackend;
14
15 async fn execute_raw(&self, stmt: Statement) -> Result<ExecResult, DbErr>;
17
18 async fn execute<S: StatementBuilder>(&self, stmt: &S) -> Result<ExecResult, DbErr> {
20 let db_backend = self.get_database_backend();
21 let stmt = db_backend.build(stmt);
22 self.execute_raw(stmt).await
23 }
24
25 async fn execute_unprepared(&self, sql: &str) -> Result<ExecResult, DbErr>;
27
28 async fn query_one_raw(&self, stmt: Statement) -> Result<Option<QueryResult>, DbErr>;
30
31 async fn query_one<S: StatementBuilder>(&self, stmt: &S) -> Result<Option<QueryResult>, DbErr> {
33 let db_backend = self.get_database_backend();
34 let stmt = db_backend.build(stmt);
35 self.query_one_raw(stmt).await
36 }
37
38 async fn query_all_raw(&self, stmt: Statement) -> Result<Vec<QueryResult>, DbErr>;
40
41 async fn query_all<S: StatementBuilder>(&self, stmt: &S) -> Result<Vec<QueryResult>, DbErr> {
43 let db_backend = self.get_database_backend();
44 let stmt = db_backend.build(stmt);
45 self.query_all_raw(stmt).await
46 }
47
48 fn support_returning(&self) -> bool {
50 let db_backend = self.get_database_backend();
51 db_backend.support_returning()
52 }
53
54 fn is_mock_connection(&self) -> bool {
56 false
57 }
58}
59
60pub trait StreamTrait: Send + Sync {
62 type Stream<'a>: Stream<Item = Result<QueryResult, DbErr>> + Send
64 where
65 Self: 'a;
66
67 fn get_database_backend(&self) -> DbBackend;
69
70 fn stream_raw<'a>(
72 &'a self,
73 stmt: Statement,
74 ) -> Pin<Box<dyn Future<Output = Result<Self::Stream<'a>, DbErr>> + 'a + Send>>;
75
76 fn stream<'a, S: StatementBuilder + Sync>(
78 &'a self,
79 stmt: &S,
80 ) -> Pin<Box<dyn Future<Output = Result<Self::Stream<'a>, DbErr>> + 'a + Send>> {
81 let db_backend = self.get_database_backend();
82 let stmt = db_backend.build(stmt);
83 self.stream_raw(stmt)
84 }
85}
86
87#[derive(Copy, Clone, Debug, PartialEq, Eq)]
88pub enum IsolationLevel {
90 RepeatableRead,
92 ReadCommitted,
94 ReadUncommitted,
96 Serializable,
98}
99
100impl std::fmt::Display for IsolationLevel {
101 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
102 match self {
103 IsolationLevel::RepeatableRead => write!(f, "REPEATABLE READ"),
104 IsolationLevel::ReadCommitted => write!(f, "READ COMMITTED"),
105 IsolationLevel::ReadUncommitted => write!(f, "READ UNCOMMITTED"),
106 IsolationLevel::Serializable => write!(f, "SERIALIZABLE"),
107 }
108 }
109}
110
111#[derive(Copy, Clone, Debug, PartialEq, Eq)]
112pub enum AccessMode {
114 ReadOnly,
116 ReadWrite,
118}
119
120impl std::fmt::Display for AccessMode {
121 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
122 match self {
123 AccessMode::ReadOnly => write!(f, "READ ONLY"),
124 AccessMode::ReadWrite => write!(f, "READ WRITE"),
125 }
126 }
127}
128
129#[async_trait::async_trait]
131pub trait TransactionTrait {
132 async fn begin(&self) -> Result<DatabaseTransaction, DbErr>;
135
136 async fn begin_with_config(
139 &self,
140 isolation_level: Option<IsolationLevel>,
141 access_mode: Option<AccessMode>,
142 ) -> Result<DatabaseTransaction, DbErr>;
143
144 async fn transaction<F, T, E>(&self, callback: F) -> Result<T, TransactionError<E>>
147 where
148 F: for<'c> FnOnce(
149 &'c DatabaseTransaction,
150 ) -> Pin<Box<dyn Future<Output = Result<T, E>> + Send + 'c>>
151 + Send,
152 T: Send,
153 E: std::fmt::Display + std::fmt::Debug + Send;
154
155 async fn transaction_with_config<F, T, E>(
158 &self,
159 callback: F,
160 isolation_level: Option<IsolationLevel>,
161 access_mode: Option<AccessMode>,
162 ) -> Result<T, TransactionError<E>>
163 where
164 F: for<'c> FnOnce(
165 &'c DatabaseTransaction,
166 ) -> Pin<Box<dyn Future<Output = Result<T, E>> + Send + 'c>>
167 + Send,
168 T: Send,
169 E: std::fmt::Display + std::fmt::Debug + Send;
170}