pub struct Client<E>where
E: Executor,{ /* private fields */ }Expand description
A Client holding both a Dialect (for SQL rendering) and an Executor (for query execution).
The Client uses Arc internally, making it cheap to clone and thread-safe.
This allows the same client to be shared across multiple parts of your application.
The Client is generic over the Executor type to work around limitations with trait objects and Generic Associated Types (GATs).
§Example
let client = Client::postgres("postgres://user:pass@localhost/db").await?;
// client can now be cloned and passed around cheaply
let clone = client.clone();Implementations§
Source§impl<E> Client<E>where
E: Executor,
impl<E> Client<E>where
E: Executor,
Sourcepub fn new<D>(dialect: D, executor: E) -> Self
pub fn new<D>(dialect: D, executor: E) -> Self
Creates a new Client from a dialect and an executor.
This is the generic constructor that works with any Dialect and Executor implementation.
§Example
let executor = PgExecutor::new("postgres://localhost/mydb").await?;
let dialect = PostgresDialect;
let client = Client::new(dialect, executor);Source§impl Client<PgExecutor>
Convenience constructors for specific database backends.
impl Client<PgExecutor>
Convenience constructors for specific database backends.
Sourcepub async fn postgres(url: &str) -> Result<Self>
pub async fn postgres(url: &str) -> Result<Self>
Creates a new PostgreSQL client.
This is a convenience constructor that creates both a PostgresDialect and a PgExecutor, then wraps them in a Client.
§Arguments
url- PostgreSQL connection string (e.g., “postgres://user:pass@localhost/db”)
§Example
let client = Client::postgres("postgres://localhost/mydb").await?;Sourcepub async fn postgres_with_options(
url: &str,
pool_options: ConnectorPoolOptions,
) -> Result<Self>
pub async fn postgres_with_options( url: &str, pool_options: ConnectorPoolOptions, ) -> Result<Self>
Creates a new PostgreSQL client with explicit pool overrides.
Sourcepub async fn transaction<F, Fut, T>(
&self,
opts: TransactionOptions,
f: F,
) -> Result<T>
pub async fn transaction<F, Fut, T>( &self, opts: TransactionOptions, f: F, ) -> Result<T>
Execute an async closure inside a database transaction.
The closure receives a Client<TransactionExecutor> whose queries all
run on the same underlying connection. If the closure returns Ok,
the transaction is committed; if it returns Err (or panics), the
transaction is rolled back.
§Example
let client = Client::postgres("postgres://localhost/mydb").await?;
let result = client.transaction(Default::default(), |tx| Box::pin(async move {
// tx.executor() runs queries inside the transaction
Ok(42)
})).await?;Source§impl Client<MysqlExecutor>
Convenience constructor for MySQL.
impl Client<MysqlExecutor>
Convenience constructor for MySQL.
Sourcepub async fn mysql(url: &str) -> Result<Self>
pub async fn mysql(url: &str) -> Result<Self>
Creates a new MySQL client.
This is a convenience constructor that creates both a MysqlDialect and a MysqlExecutor, then wraps them in a Client.
§Arguments
url- MySQL connection string (e.g., “mysql://user:pass@localhost/db”)
§Example
let client = Client::mysql("mysql://user:pass@localhost/mydb").await?;Sourcepub async fn mysql_with_options(
url: &str,
pool_options: ConnectorPoolOptions,
) -> Result<Self>
pub async fn mysql_with_options( url: &str, pool_options: ConnectorPoolOptions, ) -> Result<Self>
Creates a new MySQL client with explicit pool overrides.
Sourcepub async fn transaction<F, Fut, T>(
&self,
opts: TransactionOptions,
f: F,
) -> Result<T>
pub async fn transaction<F, Fut, T>( &self, opts: TransactionOptions, f: F, ) -> Result<T>
Execute an async closure inside a MySQL transaction.
See Client::<PgExecutor>::transaction for full documentation.
Source§impl Client<SqliteExecutor>
Convenience constructor for SQLite.
impl Client<SqliteExecutor>
Convenience constructor for SQLite.
Sourcepub async fn sqlite_with_options(
url: &str,
pool_options: ConnectorPoolOptions,
) -> Result<Self>
pub async fn sqlite_with_options( url: &str, pool_options: ConnectorPoolOptions, ) -> Result<Self>
Creates a new SQLite client with explicit pool overrides.
Sourcepub async fn transaction<F, Fut, T>(
&self,
opts: TransactionOptions,
f: F,
) -> Result<T>
pub async fn transaction<F, Fut, T>( &self, opts: TransactionOptions, f: F, ) -> Result<T>
Execute an async closure inside a SQLite transaction.
See Client::<PgExecutor>::transaction for full documentation.
SQLite ignores TransactionOptions::isolation_level because it does not
support SET TRANSACTION ISOLATION LEVEL.
Trait Implementations§
Auto Trait Implementations§
impl<E> !RefUnwindSafe for Client<E>
impl<E> !UnwindSafe for Client<E>
impl<E> Freeze for Client<E>
impl<E> Send for Client<E>
impl<E> Sync for Client<E>
impl<E> Unpin for Client<E>
impl<E> UnsafeUnpin for Client<E>
Blanket Implementations§
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> 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 more