Skip to main content

Client

Struct Client 

Source
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,

Source

pub fn new<D>(dialect: D, executor: E) -> Self
where D: Dialect + Send + Sync + 'static,

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

pub fn dialect(&self) -> &(dyn Dialect + Send + Sync)

Returns a reference to the underlying Dialect.

Use this to render queries into SQL.

Source

pub fn executor(&self) -> &E

Returns a reference to the underlying Executor.

Use this to execute rendered SQL queries against the database.

Source§

impl Client<PgExecutor>

Convenience constructors for specific database backends.

Source

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?;
Source

pub async fn postgres_with_options( url: &str, pool_options: ConnectorPoolOptions, ) -> Result<Self>

Creates a new PostgreSQL client with explicit pool overrides.

Source

pub async fn transaction<F, Fut, T>( &self, opts: TransactionOptions, f: F, ) -> Result<T>
where F: FnOnce(Client<TransactionExecutor>) -> Fut, Fut: Future<Output = Result<T>> + Send, T: Send + 'static,

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.

Source

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?;
Source

pub async fn mysql_with_options( url: &str, pool_options: ConnectorPoolOptions, ) -> Result<Self>

Creates a new MySQL client with explicit pool overrides.

Source

pub async fn transaction<F, Fut, T>( &self, opts: TransactionOptions, f: F, ) -> Result<T>
where F: FnOnce(Client<TransactionExecutor>) -> Fut, Fut: Future<Output = Result<T>> + Send, T: Send + 'static,

Execute an async closure inside a MySQL transaction.

See Client::<PgExecutor>::transaction for full documentation.

Source§

impl Client<SqliteExecutor>

Convenience constructor for SQLite.

Source

pub async fn sqlite(url: &str) -> Result<Self>

Creates a new SQLite client.

This is a convenience constructor that creates both a SqliteDialect and a SqliteExecutor, then wraps them in a Client.

§Arguments
  • url - SQLite connection URL (e.g., sqlite:mydb.db or sqlite::memory:)
§Example
let client = Client::sqlite("sqlite::memory:").await?;
Source

pub async fn sqlite_with_options( url: &str, pool_options: ConnectorPoolOptions, ) -> Result<Self>

Creates a new SQLite client with explicit pool overrides.

Source

pub async fn transaction<F, Fut, T>( &self, opts: TransactionOptions, f: F, ) -> Result<T>
where F: FnOnce(Client<TransactionExecutor>) -> Fut, Fut: Future<Output = Result<T>> + Send, T: Send + 'static,

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§

Source§

impl<E> Clone for Client<E>
where E: Executor,

Source§

fn clone(&self) -> Self

Cloning a Client is cheap - it only clones the Arc pointers, not the underlying data.

1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more