sea_orm/database/
connection.rs1use crate::{
2 DatabaseTransaction, DbBackend, DbErr, ExecResult, QueryResult, Statement, TransactionError,
3};
4use futures_util::Stream;
5use std::{future::Future, pin::Pin};
6
7#[async_trait::async_trait]
10pub trait ConnectionTrait: Sync {
11 fn get_database_backend(&self) -> DbBackend;
14
15 async fn execute(&self, stmt: Statement) -> Result<ExecResult, DbErr>;
17
18 async fn execute_unprepared(&self, sql: &str) -> Result<ExecResult, DbErr>;
20
21 async fn query_one(&self, stmt: Statement) -> Result<Option<QueryResult>, DbErr>;
23
24 async fn query_all(&self, stmt: Statement) -> Result<Vec<QueryResult>, DbErr>;
26
27 fn support_returning(&self) -> bool {
29 let db_backend = self.get_database_backend();
30 db_backend.support_returning()
31 }
32
33 fn is_mock_connection(&self) -> bool {
35 false
36 }
37}
38
39pub trait StreamTrait: Send + Sync {
41 type Stream<'a>: Stream<Item = Result<QueryResult, DbErr>> + Send
43 where
44 Self: 'a;
45
46 fn stream<'a>(
48 &'a self,
49 stmt: Statement,
50 ) -> Pin<Box<dyn Future<Output = Result<Self::Stream<'a>, DbErr>> + 'a + Send>>;
51}
52
53#[derive(Copy, Clone, Debug, PartialEq, Eq)]
54pub enum IsolationLevel {
56 RepeatableRead,
58 ReadCommitted,
60 ReadUncommitted,
62 Serializable,
64}
65
66impl std::fmt::Display for IsolationLevel {
67 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
68 match self {
69 IsolationLevel::RepeatableRead => write!(f, "REPEATABLE READ"),
70 IsolationLevel::ReadCommitted => write!(f, "READ COMMITTED"),
71 IsolationLevel::ReadUncommitted => write!(f, "READ UNCOMMITTED"),
72 IsolationLevel::Serializable => write!(f, "SERIALIZABLE"),
73 }
74 }
75}
76
77#[derive(Copy, Clone, Debug, PartialEq, Eq)]
78pub enum AccessMode {
80 ReadOnly,
82 ReadWrite,
84}
85
86impl std::fmt::Display for AccessMode {
87 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
88 match self {
89 AccessMode::ReadOnly => write!(f, "READ ONLY"),
90 AccessMode::ReadWrite => write!(f, "READ WRITE"),
91 }
92 }
93}
94
95#[async_trait::async_trait]
97pub trait TransactionTrait {
98 async fn begin(&self) -> Result<DatabaseTransaction, DbErr>;
101
102 async fn begin_with_config(
105 &self,
106 isolation_level: Option<IsolationLevel>,
107 access_mode: Option<AccessMode>,
108 ) -> Result<DatabaseTransaction, DbErr>;
109
110 async fn transaction<F, T, E>(&self, callback: F) -> Result<T, TransactionError<E>>
113 where
114 F: for<'c> FnOnce(
115 &'c DatabaseTransaction,
116 ) -> Pin<Box<dyn Future<Output = Result<T, E>> + Send + 'c>>
117 + Send,
118 T: Send,
119 E: std::error::Error + Send;
120
121 async fn transaction_with_config<F, T, E>(
124 &self,
125 callback: F,
126 isolation_level: Option<IsolationLevel>,
127 access_mode: Option<AccessMode>,
128 ) -> Result<T, TransactionError<E>>
129 where
130 F: for<'c> FnOnce(
131 &'c DatabaseTransaction,
132 ) -> Pin<Box<dyn Future<Output = Result<T, E>> + Send + 'c>>
133 + Send,
134 T: Send,
135 E: std::error::Error + Send;
136}