Expand description
Async SQL access for mariadb, mysql, postgres, and sqlite.
quex is a small facade over quex_driver. It gives you one API for:
- opening one connection or a pool
- running SQL directly
- binding positional parameters
- decoding rows into your own types
It stays close to SQL. This crate does not try to hide queries behind an orm or query builder.
The main entry points are:
Poolfor many short-lived checked-out connectionsConnectionfor one connectionqueryfor one-off SQLpreparefor a reusable prepared statementHookswhen a pool needs connection setup or validationExecutorwhen you want generic code that works with any supported executor typeEncodefor binding your own parameter typesFromRow,FromRowRef, andDecodefor mapping rows into your own types
query and prepare use ? placeholders for every driver. When the driver
is postgres, quex rewrites them to $1, $2, and so on before sending
the SQL to libpq.
A small sqlite example:
use quex::{FromRow, Pool, Row, SqliteConnectOptions};
struct User {
id: i64,
name: String,
}
impl FromRow for User {
fn from_row(row: &Row) -> quex::Result<Self> {
Ok(Self {
id: row.get("id")?,
name: row.get("name")?,
})
}
}
let pool = Pool::connect(SqliteConnectOptions::new().in_memory())?
.max_size(4)
.build()
.await?;
let mut db = pool.acquire().await?;
quex::query("create table users(id integer primary key, name text not null)")
.execute(&mut db)
.await?;
quex::query("insert into users(name) values(?)")
.bind("Ada")
.execute(&mut db)
.await?;
let users = quex::query("select id, name from users order by id")
.all::<User>(&mut db)
.await?;When you already have a Pool, the high-level query helpers can run
directly against it:
let pool = Pool::connect(SqliteConnectOptions::new().in_memory())?
.max_size(4)
.build()
.await?;
let ids: Vec<i64> = quex::query("select id from users order by id")
.all(&pool)
.await?;A few things matter when using the crate:
- Borrowed rows only live until the stream advances. If you need to keep data longer, decode into owned types or collect owned rows.
Query::oneandQuery::optionalignore extra rows.- A
PoolTransactionholds one checked-out connection until commit, rollback, or drop. Pool::with_hookscan run setup on fresh connections and validate a connection before it is handed out.Poolis the better default when many tasks need database access.Connectionis the simpler single-connection option.- Postgres
LISTEN/NOTIFYworks onConnectionandPooledConnection. Keep one checked-out connection dedicated to waiting for notifications.
Optional features add support for common value types:
mysqlenables mysql and mariadb supportpostgresenables postgres supportsqliteenables sqlite supportchronofor chrono date and time typestimefortimecrate typesuuidforuuid::Uuidjsonforserde_json::Value
Setup depends on the driver you use. quex_driver talks to libpq,
libmariadb, and sqlite3 through ffi, so those client libraries need to be
available for your target environment. No database backend is enabled by
default, so pick the ones you need in your Cargo.toml.
Structs§
- Bound
Statement - A prepared statement plus parameters collected through
.bind(...). - Column
- Metadata for one result column.
- Connect
Options - Connection settings parsed from a URL or built manually.
- Connection
- A simple handle for one database connection.
- Date
- A calendar date.
- Date
Time - A date and time without a timezone offset.
- Date
Time Tz - A date and time with a fixed offset.
- Decoder
- Reads one SQL column value for
Decode. - Encoder
- Encodes one application value into a SQL parameter sink.
- Exec
Result - The outcome of an
insert,update,delete, or other statement that does not return rows. - Hooks
- Optional pool hooks.
- Mysql
Connect Options - mysql or mariadb connection settings.
- Params
- An owned list of positional parameters.
- Pool
- A cloneable pool of database connections.
- Pool
Builder - Builder for
Pool. - Pool
Statement - A statement that belongs to a pool.
- Pool
Transaction - A transaction started from a
Pool. - Pooled
Connection - A connection checked out from a
Pool. - Pooled
Statement - A statement prepared on a checked-out connection.
- Pooled
Transaction - A transaction borrowed from a
PooledConnection. - Postgres
Connect Options - postgres connection settings.
- Prepare
- A request to prepare SQL against an executor.
- Query
- A query with optional bound parameters.
- Row
- A row whose column metadata and values are owned by Rust.
- RowRef
- A borrowed row from a result stream.
- Rows
- A forward-only stream of rows returned by a query.
- Sqlite
Connect Options - sqlite connection settings.
- Time
- A time of day with microsecond precision.
- Transaction
- A transaction on a
Connectionconnection.
Enums§
- Acquire
Decision - Decides what the pool should do with a connection after
before_acquire. - Column
Type - Driver-specific column type metadata.
- Driver
- The database driver selected for a connection or pool.
- Error
- Errors returned by the facade.
- Param
Ref - A borrowed SQL parameter value.
- Param
Value - A value borrowed or owned long enough to bind to a prepared statement.
- Statement
- A driver-specific prepared statement behind the facade.
- Value
- An owned database value.
Traits§
- Column
Index - A column lookup accepted by row accessors.
- Decode
- Decodes a single SQL value into an application type.
- Encode
- Converts application values into SQL parameters.
- Encode
Target - Executor
- Something that can run SQL.
- From
Column Ref - Decodes one column directly from a borrowed row.
- FromRow
- Decodes an owned row into an application type.
- From
RowRef - Decodes a borrowed row without first collecting it into an owned row.
- Into
Connect Options - Converts a value into generic connection options.
- Param
Source - A borrowed source of positional SQL parameters.
- Prepared
Statement - A prepared statement that can be run with borrowed parameters.
- RowStream
- A forward-only stream of result rows.
Functions§
Type Aliases§
- Result
- Convenient result type used by
quex.