pub trait Connection: Send + Syncwhere
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:
SqlError::InvalidIdentifier—databasefailed identifier validation.- [
SqlError::Connection] — the underlying driver failed. SqlError::QueryTimeout— the query exceeded the configured timeout.
Required Associated Types§
Required Methods§
Sourceasync fn pool(&self, target: Option<&str>) -> Result<Pool<Self::DB>, SqlError>
async fn pool(&self, target: Option<&str>) -> Result<Pool<Self::DB>, SqlError>
Resolves the connection pool for the given target database.
§Errors
SqlError::InvalidIdentifier—targetfailed validation.
Sourcefn query_timeout(&self) -> Option<u64>
fn query_timeout(&self) -> Option<u64>
Returns the configured query timeout in seconds, if any.
Provided Methods§
Sourceasync fn execute<Q>(
&self,
query: Q,
database: Option<&str>,
) -> Result<u64, SqlError>where
Q: IntoSafeQuery<Self::DB>,
async fn execute<Q>(
&self,
query: Q,
database: Option<&str>,
) -> Result<u64, SqlError>where
Q: IntoSafeQuery<Self::DB>,
Sourceasync fn fetch_json<Q>(
&self,
query: Q,
database: Option<&str>,
) -> Result<Vec<Value>, 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>,
Sourceasync fn fetch_optional<Q, T>(
&self,
query: Q,
database: Option<&str>,
) -> Result<Option<T>, SqlError>
async fn fetch_optional<Q, T>( &self, query: Q, database: Option<&str>, ) -> Result<Option<T>, SqlError>
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.
Sourceasync fn fetch_scalar<Q, T>(
&self,
query: Q,
database: Option<&str>,
) -> Result<Vec<T>, SqlError>
async fn fetch_scalar<Q, T>( &self, query: Q, database: Option<&str>, ) -> Result<Vec<T>, SqlError>
Sourceasync fn fetch<Q, T>(
&self,
query: Q,
database: Option<&str>,
) -> Result<Vec<T>, SqlError>
async fn fetch<Q, T>( &self, query: Q, database: Option<&str>, ) -> Result<Vec<T>, SqlError>
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".