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( &self, query: &str, database: Option<&str>, ) -> Result<u64, SqlError> { ... } async fn fetch_json( &self, query: &str, database: Option<&str>, ) -> Result<Vec<Value>, SqlError> { ... } async fn fetch_optional<T>( &self, query: &str, database: Option<&str>, ) -> Result<Option<T>, SqlError> where T: for<'r> Decode<'r, Self::DB> + Type<Self::DB> + Send + Unpin { ... } async fn fetch_scalar<T>( &self, query: &str, database: Option<&str>, ) -> Result<Vec<T>, SqlError> where T: for<'r> Decode<'r, Self::DB> + Type<Self::DB> + 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.

§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( &self, query: &str, database: Option<&str>, ) -> Result<u64, SqlError>

Runs a statement that returns no meaningful rows.

§Errors

See trait-level documentation.

Source

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

Runs a statement and collects every result row as JSON.

§Errors

See trait-level documentation.

Source

async fn fetch_optional<T>( &self, query: &str, database: Option<&str>, ) -> Result<Option<T>, SqlError>
where 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<T>( &self, query: &str, database: Option<&str>, ) -> Result<Vec<T>, SqlError>
where 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.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§