SQLx decouples query construction from query execution through two core traits:Documentation Index
Fetch the complete documentation index at: https://mintlify.com/launchbadge/sqlx/llms.txt
Use this file to discover all available pages before exploring further.
Executor and Execute. This lets you write functions that accept a pool, a single connection, or a transaction without knowing which one they receive — a pattern that makes code more testable and reusable.
These traits are re-exported in
sqlx::prelude. Add use sqlx::prelude::*; to bring them into scope alongside the other commonly used SQLx traits.Executor trait
Executor is the trait that knows how to run a query. Its methods correspond directly to the finalizer methods on query types; when you call .fetch_one(&pool), the pool is the executor.
Implementations
| Type | Guarantees same physical connection? |
|---|---|
&Pool<DB> | No — each call may use a different connection from the pool. |
&mut DB::Connection | Yes — all calls use the same connection. |
&mut PoolConnection<DB> | Yes — dereference to &mut *conn first. |
&mut Transaction<'_, DB> | Yes — dereference to &mut *tx first. |
Writing generic functions
Acceptimpl Executor<'_, Database = DB> to write a function that works with any of the above handle types.
Execute trait
Execute is implemented by the types that are a query — the other side of the pairing. You rarely implement or name this trait directly; it is the bound that executor methods use to accept any query-like value.
Implementations
Query / QueryAs / QueryScalar
Returned by
sqlx::query(), sqlx::query_as(), and sqlx::query_scalar(). Carries a SQL string and a set of bound arguments; executes as a prepared statement.RawSql
Returned by
sqlx::raw_sql(). Carries a SQL string with no arguments; executes as an unprepared statement. Use for DDL and multi-statement batches.&str / String
String types that implement
SqlSafeStr also implement Execute. Passing a bare &str to an executor executes it as a simple (unprepared) query with no parameters.Map
Returned by
Query::try_map() and the query! macros. Wraps a Query with a row-mapping closure; shares the same execution methods.Acquire trait
Acquire abstracts over types that can produce a connection — either by borrowing one from a pool or by wrapping a connection that already exists. Use it when you want a function to accept either a pool or a connection, and you need to call methods that require an owned or mutable connection (such as starting a transaction).
Implementations
| Type | Behavior |
|---|---|
&Pool<DB> | Calls Pool::acquire() to borrow a connection from the pool. |
PoolConnection<DB> | Returns itself. |
&mut PoolConnection<DB> | Reborrowed; connection is returned to the pool when the outer guard drops. |
&mut DB::Connection | Reborrowed; does not close the connection when done. |