Skip to main content

Connection

Trait Connection 

Source
pub trait Connection: Send + Sync
where for<'c> &'c mut <Self::DB as Database>::Connection: Executor<'c, Database = Self::DB>, usize: ColumnIndex<<Self::DB as Database>::Row>, <Self::DB as Database>::Row: RowExt, <Self::DB as Database>::QueryResult: QueryResult,
{ type DB: Database; // Required methods async fn pool( &self, target: Option<&str>, ) -> Result<Pool<Self::DB>, SqlError>; fn query_timeout(&self) -> Option<u64>; // Provided methods async fn execute<Q>( &self, query: Q, database: Option<&str>, ) -> Result<u64, SqlError> where Q: IntoSafeQuery<Self::DB> { ... } async fn fetch_json<Q>( &self, query: Q, database: Option<&str>, ) -> Result<Vec<Value>, SqlError> where Q: IntoSafeQuery<Self::DB> { ... } async fn fetch_optional<Q, T>( &self, query: Q, database: Option<&str>, ) -> Result<Option<T>, SqlError> where Q: IntoSafeQuery<Self::DB>, T: for<'r> Decode<'r, Self::DB> + Type<Self::DB> + Send + Unpin { ... } async fn fetch_scalar<Q, T>( &self, query: Q, database: Option<&str>, ) -> Result<Vec<T>, SqlError> where Q: IntoSafeQuery<Self::DB>, T: for<'r> Decode<'r, Self::DB> + Type<Self::DB> + Send + Unpin { ... } async fn fetch<Q, T>( &self, query: Q, database: Option<&str>, ) -> Result<Vec<T>, SqlError> where Q: IntoSafeQuery<Self::DB>, T: for<'r> FromRow<'r, <Self::DB as Database>::Row> + Send + Unpin { ... } }
Expand description

Unified query surface every backend tool handler uses.

Backends supply three required items — DB, pool, and query_timeout — and receive default implementations for query execution.

Query methods accept any IntoSafeQuery value: a bindless &str (run through the unprepared text protocol, required for statements like MySQL USE) or a parameterized sqlx::query(sql).bind(...) value (run as a prepared statement).

§Errors

Query methods may return:

Required Associated Types§

Source

type DB: Database

The sqlx database driver type (e.g. sqlx::MySql).

Required Methods§

Source

async fn pool(&self, target: Option<&str>) -> Result<Pool<Self::DB>, SqlError>

Resolves the connection pool for the given target database.

§Errors
Source

fn query_timeout(&self) -> Option<u64>

Returns the configured query timeout in seconds, if any.

Provided Methods§

Source

async fn execute<Q>( &self, query: Q, database: Option<&str>, ) -> Result<u64, SqlError>
where Q: IntoSafeQuery<Self::DB>,

Runs a statement that returns no meaningful rows.

§Errors

See trait-level documentation.

Source

async fn fetch_json<Q>( &self, query: Q, database: Option<&str>, ) -> Result<Vec<Value>, SqlError>
where Q: IntoSafeQuery<Self::DB>,

Runs a statement and collects every result row as JSON.

§Errors

See trait-level documentation.

Source

async fn fetch_optional<Q, T>( &self, query: Q, database: Option<&str>, ) -> Result<Option<T>, SqlError>
where Q: IntoSafeQuery<Self::DB>, T: for<'r> Decode<'r, Self::DB> + Type<Self::DB> + Send + Unpin,

Runs a query and extracts column 0 from the first row, if any.

Returns None for both “no row returned” and “row where column 0 is NULL” (decode errors are caught, not propagated).

§Errors

See trait-level documentation.

Source

async fn fetch_scalar<Q, T>( &self, query: Q, database: Option<&str>, ) -> Result<Vec<T>, SqlError>
where Q: IntoSafeQuery<Self::DB>, T: for<'r> Decode<'r, Self::DB> + Type<Self::DB> + Send + Unpin,

Runs a query and extracts the first column of every row.

§Errors

See trait-level documentation.

Source

async fn fetch<Q, T>( &self, query: Q, database: Option<&str>, ) -> Result<Vec<T>, SqlError>
where Q: IntoSafeQuery<Self::DB>, T: for<'r> FromRow<'r, <Self::DB as Database>::Row> + Send + Unpin,

Runs a query and decodes every row into T via sqlx::FromRow.

§Errors

See trait-level documentation. Row decode failures (column type mismatch, malformed JSON inside a sqlx::types::Json column, etc.) surface as SqlError::Query.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§