Skip to main content

Connection

Trait Connection 

Source
pub trait Connection: Send + Sync {
    type DB: Database;

    const IDENTIFIER_QUOTE: char;

    // Required methods
    async fn pool(
        &self,
        target: Option<&str>,
    ) -> Result<Pool<Self::DB>, AppError>;
    fn query_timeout(&self) -> Option<u64>;

    // Provided methods
    async fn execute(
        &self,
        query: &str,
        database: Option<&str>,
    ) -> Result<u64, AppError>
       where for<'c> &'c mut <Self::DB as Database>::Connection: Executor<'c, Database = Self::DB>,
             <Self::DB as Database>::QueryResult: QueryResult { ... }
    async fn fetch(
        &self,
        query: &str,
        database: Option<&str>,
    ) -> Result<Vec<Value>, AppError>
       where for<'c> &'c mut <Self::DB as Database>::Connection: Executor<'c, Database = Self::DB>,
             <Self::DB as Database>::Row: RowExt { ... }
    async fn fetch_optional(
        &self,
        query: &str,
        database: Option<&str>,
    ) -> Result<Option<Value>, AppError>
       where for<'c> &'c mut <Self::DB as Database>::Connection: Executor<'c, Database = Self::DB>,
             <Self::DB as Database>::Row: RowExt { ... }
    fn quote_identifier(&self, name: &str) -> String { ... }
    fn quote_string(&self, value: &str) -> String { ... }
}
Expand description

Unified query and quoting surface every backend tool handler uses.

Backends supply four required items — DB, IDENTIFIER_QUOTE, pool, and query_timeout — and receive default implementations for query execution and SQL quoting.

§Errors

Query methods may return:

Required Associated Constants§

Source

const IDENTIFIER_QUOTE: char

Character used to quote identifiers (` for MySQL, " for PostgreSQL/SQLite).

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

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, AppError>
where for<'c> &'c mut <Self::DB as Database>::Connection: Executor<'c, Database = Self::DB>, <Self::DB as Database>::QueryResult: QueryResult,

Runs a statement that returns no meaningful rows.

§Errors

See trait-level documentation.

Source

async fn fetch( &self, query: &str, database: Option<&str>, ) -> Result<Vec<Value>, AppError>
where for<'c> &'c mut <Self::DB as Database>::Connection: Executor<'c, Database = Self::DB>, <Self::DB as Database>::Row: RowExt,

Runs a statement and collects every result row as JSON.

§Errors

See trait-level documentation.

Source

async fn fetch_optional( &self, query: &str, database: Option<&str>, ) -> Result<Option<Value>, AppError>
where for<'c> &'c mut <Self::DB as Database>::Connection: Executor<'c, Database = Self::DB>, <Self::DB as Database>::Row: RowExt,

Runs a statement and returns at most one result row as JSON.

§Errors

See trait-level documentation.

Source

fn quote_identifier(&self, name: &str) -> String

Wraps name in the backend’s identifier quote character.

Source

fn quote_string(&self, value: &str) -> String

Wraps value in single quotes for use as a SQL string literal.

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§