pub struct DatabaseConnection { /* private fields */ }database and native only.Expand description
Database connection wrapper
Implementations§
Source§impl DatabaseConnection
impl DatabaseConnection
Sourcepub fn new(
backend: DatabaseBackend,
inner: DatabaseConnection,
) -> DatabaseConnection
pub fn new( backend: DatabaseBackend, inner: DatabaseConnection, ) -> DatabaseConnection
Creates a new instance.
Sourcepub async fn connect(url: &str) -> Result<DatabaseConnection, Error>
pub async fn connect(url: &str) -> Result<DatabaseConnection, Error>
Connect to a database from a connection URL
Automatically detects the database type from the URL scheme:
postgres://orpostgresql://→ PostgreSQLmysql://→ MySQLsqlite://orsqlite:→ SQLite
§Examples
use reinhardt_db::orm::connection::DatabaseConnection;
let conn = DatabaseConnection::connect("postgres://localhost/mydb").await.unwrap();Sourcepub async fn connect_postgres(url: &str) -> Result<DatabaseConnection, Error>
Available on crate feature postgres only.
pub async fn connect_postgres(url: &str) -> Result<DatabaseConnection, Error>
postgres only.Connect to a PostgreSQL database
§Examples
use reinhardt_db::orm::connection::DatabaseConnection;
let conn = DatabaseConnection::connect_postgres("postgres://localhost/mydb").await.unwrap();Sourcepub async fn connect_mysql(url: &str) -> Result<DatabaseConnection, Error>
Available on crate feature mysql only.
pub async fn connect_mysql(url: &str) -> Result<DatabaseConnection, Error>
mysql only.Connect to a MySQL database
§Examples
use reinhardt_db::orm::connection::DatabaseConnection;
let conn = DatabaseConnection::connect_mysql("mysql://localhost/mydb").await.unwrap();Sourcepub async fn connect_sqlite(url: &str) -> Result<DatabaseConnection, Error>
Available on crate feature sqlite only.
pub async fn connect_sqlite(url: &str) -> Result<DatabaseConnection, Error>
sqlite only.Connect to a SQLite database
§Examples
use reinhardt_db::orm::connection::DatabaseConnection;
let conn = DatabaseConnection::connect_sqlite("sqlite::memory:").await.unwrap();Sourcepub async fn connect_with_pool_size(
url: &str,
pool_size: Option<u32>,
) -> Result<DatabaseConnection, Error>
pub async fn connect_with_pool_size( url: &str, pool_size: Option<u32>, ) -> Result<DatabaseConnection, Error>
Connect to a database with a specific connection pool size
§Arguments
url- Database connection URLpool_size- Maximum number of connections in the pool (None = use default)
§Examples
use reinhardt_db::orm::connection::DatabaseConnection;
// Use larger pool for high-concurrency scenarios
let conn = DatabaseConnection::connect_with_pool_size(
"postgres://localhost/mydb",
Some(50)
).await.unwrap();Sourcepub fn backend(&self) -> DatabaseBackend
pub fn backend(&self) -> DatabaseBackend
Performs the backend operation.
Sourcepub fn inner(&self) -> &DatabaseConnection
pub fn inner(&self) -> &DatabaseConnection
Get a reference to the inner backends connection
This provides access to the low-level connection for operations that require direct database access.
Sourcepub fn into_inner(self) -> DatabaseConnection
pub fn into_inner(self) -> DatabaseConnection
Consume self and return the inner backends connection
This is useful when you need to pass ownership of the connection
to functions that expect a BackendsConnection.
Sourcepub async fn query_one(
&self,
sql: &str,
params: Vec<QueryValue>,
) -> Result<QueryRow, Error>
pub async fn query_one( &self, sql: &str, params: Vec<QueryValue>, ) -> Result<QueryRow, Error>
Execute a SQL query and return a single row
Sourcepub async fn query_optional(
&self,
sql: &str,
params: Vec<QueryValue>,
) -> Result<Option<QueryRow>, Error>
pub async fn query_optional( &self, sql: &str, params: Vec<QueryValue>, ) -> Result<Option<QueryRow>, Error>
Execute a SQL query and return an optional row
Sourcepub async fn execute(
&self,
sql: &str,
params: Vec<QueryValue>,
) -> Result<u64, Error>
pub async fn execute( &self, sql: &str, params: Vec<QueryValue>, ) -> Result<u64, Error>
Execute a SQL statement (INSERT, UPDATE, DELETE, etc.)
Sourcepub async fn query(
&self,
sql: &str,
params: Vec<QueryValue>,
) -> Result<Vec<QueryRow>, Error>
pub async fn query( &self, sql: &str, params: Vec<QueryValue>, ) -> Result<Vec<QueryRow>, Error>
Execute a SQL query and return all rows
Sourcepub async fn begin_transaction(&self) -> Result<(), Error>
pub async fn begin_transaction(&self) -> Result<(), Error>
Begin a database transaction
§Examples
use reinhardt_db::orm::connection::DatabaseConnection;
let conn = DatabaseConnection::connect("sqlite::memory:").await.unwrap();
let result = conn.begin_transaction().await;
assert!(result.is_ok());Sourcepub async fn begin_transaction_with_isolation(
&self,
level: IsolationLevel,
) -> Result<(), Error>
pub async fn begin_transaction_with_isolation( &self, level: IsolationLevel, ) -> Result<(), Error>
Begin a transaction with a specific isolation level
§Examples
use reinhardt_db::orm::connection::DatabaseConnection;
use reinhardt_db::orm::transaction::IsolationLevel;
let conn = DatabaseConnection::connect("sqlite::memory:").await.unwrap();
let result = conn.begin_transaction_with_isolation(IsolationLevel::Serializable).await;
assert!(result.is_ok());Sourcepub async fn commit_transaction(&self) -> Result<(), Error>
pub async fn commit_transaction(&self) -> Result<(), Error>
Commit the current transaction
§Examples
use reinhardt_db::orm::connection::DatabaseConnection;
let conn = DatabaseConnection::connect("sqlite::memory:").await.unwrap();
conn.begin_transaction().await.unwrap();
// ... perform operations ...
let result = conn.commit_transaction().await;
assert!(result.is_ok());Sourcepub async fn rollback_transaction(&self) -> Result<(), Error>
pub async fn rollback_transaction(&self) -> Result<(), Error>
Rollback the current transaction
§Examples
use reinhardt_db::orm::connection::DatabaseConnection;
let conn = DatabaseConnection::connect("sqlite::memory:").await.unwrap();
conn.begin_transaction().await.unwrap();
// ... error occurs ...
let result = conn.rollback_transaction().await;
assert!(result.is_ok());Sourcepub async fn savepoint(&self, name: &str) -> Result<(), Error>
pub async fn savepoint(&self, name: &str) -> Result<(), Error>
Create a savepoint for nested transactions
§Examples
use reinhardt_db::orm::connection::DatabaseConnection;
let conn = DatabaseConnection::connect("sqlite::memory:").await.unwrap();
conn.begin_transaction().await.unwrap();
let result = conn.savepoint("sp1").await;
assert!(result.is_ok());
// ... nested operations ...
conn.release_savepoint("sp1").await.unwrap();Sourcepub async fn rollback_to_savepoint(&self, name: &str) -> Result<(), Error>
pub async fn rollback_to_savepoint(&self, name: &str) -> Result<(), Error>
Rollback to a savepoint
Sourcepub async fn begin(&self) -> Result<Box<dyn TransactionExecutor>, Error>
pub async fn begin(&self) -> Result<Box<dyn TransactionExecutor>, Error>
Begin a database transaction and return a dedicated executor
This method acquires a dedicated database connection and begins a
transaction on it. All queries executed through the returned
TransactionExecutor are guaranteed to run on the same physical
connection, ensuring proper transaction isolation.
§Returns
A boxed TransactionExecutor that holds the dedicated connection
and provides methods for executing queries within the transaction.
§Example
use reinhardt_db::orm::connection::DatabaseConnection;
let conn = DatabaseConnection::connect("postgres://localhost/mydb").await?;
let mut tx = conn.begin().await?;
tx.execute("INSERT INTO users (name) VALUES ($1)", vec!["Alice".into()]).await?;
tx.commit().await?;Sourcepub async fn begin_with_isolation(
&self,
level: IsolationLevel,
) -> Result<Box<dyn TransactionExecutor>, Error>
pub async fn begin_with_isolation( &self, level: IsolationLevel, ) -> Result<Box<dyn TransactionExecutor>, Error>
Begin a transaction with a specific isolation level using TransactionExecutor
This method returns a TransactionExecutor that provides dedicated connection
semantics with the specified isolation level. All queries executed through
the returned executor are guaranteed to run on the same physical connection.
§Arguments
level- The desired isolation level for the transaction
§Examples
use reinhardt_db::orm::connection::{DatabaseConnection, IsolationLevel};
let conn = DatabaseConnection::connect("postgres://localhost/mydb").await?;
let mut tx = conn.begin_with_isolation(IsolationLevel::Serializable).await?;
tx.execute("INSERT INTO users (name) VALUES ($1)", vec!["Alice".into()]).await?;
tx.commit().await?;Trait Implementations§
Source§impl Clone for DatabaseConnection
impl Clone for DatabaseConnection
Source§fn clone(&self) -> DatabaseConnection
fn clone(&self) -> DatabaseConnection
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl DatabaseExecutor for DatabaseConnection
impl DatabaseExecutor for DatabaseConnection
Source§fn execute<'life0, 'life1, 'async_trait>(
&'life0 self,
sql: &'life1 str,
) -> Pin<Box<dyn Future<Output = Result<u64, Error>> + Send + 'async_trait>>where
'life0: 'async_trait,
'life1: 'async_trait,
DatabaseConnection: 'async_trait,
fn execute<'life0, 'life1, 'async_trait>(
&'life0 self,
sql: &'life1 str,
) -> Pin<Box<dyn Future<Output = Result<u64, Error>> + Send + 'async_trait>>where
'life0: 'async_trait,
'life1: 'async_trait,
DatabaseConnection: 'async_trait,
Source§fn query<'life0, 'life1, 'async_trait>(
&'life0 self,
sql: &'life1 str,
) -> Pin<Box<dyn Future<Output = Result<Vec<QueryRow>, Error>> + Send + 'async_trait>>where
'life0: 'async_trait,
'life1: 'async_trait,
DatabaseConnection: 'async_trait,
fn query<'life0, 'life1, 'async_trait>(
&'life0 self,
sql: &'life1 str,
) -> Pin<Box<dyn Future<Output = Result<Vec<QueryRow>, Error>> + Send + 'async_trait>>where
'life0: 'async_trait,
'life1: 'async_trait,
DatabaseConnection: 'async_trait,
Source§impl Injectable for DatabaseConnection
Injectable implementation for DatabaseConnection
impl Injectable for DatabaseConnection
Injectable implementation for DatabaseConnection
DatabaseConnection must be explicitly registered in the DI context using
InjectionContextBuilder::singleton(). It cannot be auto-injected because
it requires runtime configuration (connection URL, pool settings, etc.).
§Example
use reinhardt_db::orm::DatabaseConnection;
use reinhardt_di::InjectionContext;
// First, establish a database connection
let db = DatabaseConnection::connect("postgres://localhost/mydb").await.unwrap();
// Then register it in the DI context as a singleton
let singleton_scope = reinhardt_di::SingletonScope::new();
let ctx = InjectionContext::builder(singleton_scope)
.singleton(db)
.build();Source§fn inject<'life0, 'async_trait>(
ctx: &'life0 InjectionContext,
) -> Pin<Box<dyn Future<Output = Result<DatabaseConnection, DiError>> + Send + 'async_trait>>where
'life0: 'async_trait,
DatabaseConnection: 'async_trait,
fn inject<'life0, 'async_trait>(
ctx: &'life0 InjectionContext,
) -> Pin<Box<dyn Future<Output = Result<DatabaseConnection, DiError>> + Send + 'async_trait>>where
'life0: 'async_trait,
DatabaseConnection: 'async_trait,
Source§fn inject_uncached<'life0, 'async_trait>(
ctx: &'life0 InjectionContext,
) -> Pin<Box<dyn Future<Output = Result<DatabaseConnection, DiError>> + Send + 'async_trait>>where
'life0: 'async_trait,
DatabaseConnection: 'async_trait,
fn inject_uncached<'life0, 'async_trait>(
ctx: &'life0 InjectionContext,
) -> Pin<Box<dyn Future<Output = Result<DatabaseConnection, DiError>> + Send + 'async_trait>>where
'life0: 'async_trait,
DatabaseConnection: 'async_trait,
cache = false support). Read moreAuto Trait Implementations§
impl Freeze for DatabaseConnection
impl !RefUnwindSafe for DatabaseConnection
impl Send for DatabaseConnection
impl Sync for DatabaseConnection
impl Unpin for DatabaseConnection
impl UnsafeUnpin for DatabaseConnection
impl !UnwindSafe for DatabaseConnection
Blanket Implementations§
Source§impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
Source§impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> FmtForward for T
impl<T> FmtForward for T
Source§fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
self to use its Binary implementation when Debug-formatted.Source§fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
self to use its Display implementation when
Debug-formatted.Source§fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
self to use its LowerExp implementation when
Debug-formatted.Source§fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
self to use its LowerHex implementation when
Debug-formatted.Source§fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
self to use its Octal implementation when Debug-formatted.Source§fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
self to use its Pointer implementation when
Debug-formatted.Source§fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
self to use its UpperExp implementation when
Debug-formatted.Source§fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
self to use its UpperHex implementation when
Debug-formatted.Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
Source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T in a tonic::RequestSource§impl<T> IntoResult<T> for T
impl<T> IntoResult<T> for T
type Err = Infallible
fn into_result(self) -> Result<T, <T as IntoResult<T>>::Err>
Source§impl<T> Pipe for Twhere
T: ?Sized,
impl<T> Pipe for Twhere
T: ?Sized,
Source§fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
Source§fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
self and passes that borrow into the pipe function. Read moreSource§fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
self and passes that borrow into the pipe function. Read moreSource§fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
Source§fn pipe_borrow_mut<'a, B, R>(
&'a mut self,
func: impl FnOnce(&'a mut B) -> R,
) -> R
fn pipe_borrow_mut<'a, B, R>( &'a mut self, func: impl FnOnce(&'a mut B) -> R, ) -> R
Source§fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
self, then passes self.as_ref() into the pipe function.Source§fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
self, then passes self.as_mut() into the pipe
function.Source§fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
self, then passes self.deref() into the pipe function.Source§impl<T> Pointable for T
impl<T> Pointable for T
Source§impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> PolicyExt for Twhere
T: ?Sized,
Source§impl<T> Tap for T
impl<T> Tap for T
Source§fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
Borrow<B> of a value. Read moreSource§fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
BorrowMut<B> of a value. Read moreSource§fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
AsRef<R> view of a value. Read moreSource§fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
AsMut<R> view of a value. Read moreSource§fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
Deref::Target of a value. Read moreSource§fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
Deref::Target of a value. Read moreSource§fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
.tap() only in debug builds, and is erased in release builds.Source§fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
.tap_mut() only in debug builds, and is erased in release
builds.Source§fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
.tap_borrow() only in debug builds, and is erased in release
builds.Source§fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
.tap_borrow_mut() only in debug builds, and is erased in release
builds.Source§fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
.tap_ref() only in debug builds, and is erased in release
builds.Source§fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
.tap_ref_mut() only in debug builds, and is erased in release
builds.Source§fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
.tap_deref() only in debug builds, and is erased in release
builds.