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, E>(
&self,
query: E,
database: Option<&str>,
) -> Result<u64, SqlError>
where E: 'q + Execute<'q, Self::DB> { ... }
async fn fetch_json<'q, E>(
&self,
query: E,
database: Option<&str>,
) -> Result<Vec<Value>, SqlError>
where E: 'q + Execute<'q, Self::DB> { ... }
async fn fetch_optional<'q, E, T>(
&self,
query: E,
database: Option<&str>,
) -> Result<Option<T>, SqlError>
where E: 'q + Execute<'q, Self::DB>,
T: for<'r> Decode<'r, Self::DB> + Type<Self::DB> + Send + Unpin { ... }
async fn fetch_scalar<'q, E, T>(
&self,
query: E,
database: Option<&str>,
) -> Result<Vec<T>, SqlError>
where E: 'q + Execute<'q, Self::DB>,
T: for<'r> Decode<'r, Self::DB> + Type<Self::DB> + Send + Unpin { ... }
async fn fetch<'q, E, T>(
&self,
query: E,
database: Option<&str>,
) -> Result<Vec<T>, SqlError>
where E: 'q + Execute<'q, 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 sqlx::Execute value: a plain &str for
bindless statements (which routes through sqlx’s unprepared text
protocol, required for statements like MySQL USE), or an
sqlx::query(sql).bind(...) value for parameterized statements.
§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 fetch_json<'q, E>(
&self,
query: E,
database: Option<&str>,
) -> Result<Vec<Value>, SqlError>
async fn fetch_json<'q, E>( &self, query: E, database: Option<&str>, ) -> Result<Vec<Value>, SqlError>
Sourceasync fn fetch_optional<'q, E, T>(
&self,
query: E,
database: Option<&str>,
) -> Result<Option<T>, SqlError>
async fn fetch_optional<'q, E, T>( &self, query: E, 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, E, T>(
&self,
query: E,
database: Option<&str>,
) -> Result<Vec<T>, SqlError>
async fn fetch_scalar<'q, E, T>( &self, query: E, database: Option<&str>, ) -> Result<Vec<T>, SqlError>
Sourceasync fn fetch<'q, E, T>(
&self,
query: E,
database: Option<&str>,
) -> Result<Vec<T>, SqlError>
async fn fetch<'q, E, T>( &self, query: E, 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", so this trait is not object safe.