Expand description
The async SQL toolkit for Rust, built with ❤️ by the LaunchBadge team.
See our README to get started or browse our example projects.
Have a question? Check our FAQ or open a discussion.
Runtime Support
SQLx supports both the Tokio and async-std runtimes.
You choose which runtime SQLx uses by default by enabling one of the following features:
runtime-async-stdruntime-tokio
The runtime-actix feature also exists but is an alias of runtime-tokio.
If more than one runtime feature is enabled, the Tokio runtime is used if a Tokio context exists on the current thread, i.e. tokio::runtime::Handle::try_current() returns Ok; async-std is used otherwise.
Note that while SQLx no longer produces a compile error if zero or multiple runtime features are enabled, which is useful for libraries building on top of it, the use of nearly any async function in the API will panic without at least one runtime feature enabled.
The chief exception is the SQLite driver, which is runtime-agnostic, including its integration with the query macros. However, SqlitePool does require runtime support for timeouts and spawning internal management tasks.
TLS Support
For securely communicating with SQL servers over an untrusted network connection such as the internet, you can enable Transport Layer Security (TLS) by enabling one of the following features:
tls-native-tls: Enables thenative-tlsbackend which uses the OS-native TLS capabilities:- SecureTransport on macOS.
- SChannel on Windows.
- OpenSSL on all other platforms.
tls-rustls: Enables the RusTLS backend, a crossplatform TLS library.- Only supports TLS revisions 1.2 and 1.3.
- If you get
HandshakeFailureerrors when using this feature, it likely means your database server does not support these newer revisions. This might be resolved by enabling or switching to thetls-native-tlsfeature.
If more than one TLS feature is enabled, the tls-native-tls feature takes precedent so that it is only necessary to enable it to see if it resolves the HandshakeFailure error without disabling tls-rustls.
Consult the user manual for your database to find the TLS versions it supports.
If your connection configuration requires a TLS upgrade but TLS support was not enabled, the connection attempt will return an error.
The legacy runtime+TLS combination feature flags are still supported, but for forward-compatibility, use of the separate runtime and TLS feature flags is recommended.
Re-exports
pub use sqlx_mysql as mysql;pub use sqlx_postgres as postgres;pub use sqlx_sqlite as sqlite;
Modules
- SEE DOCUMENTATION BEFORE USE. Runtime-generic database driver.
- Traits to represent a database driver.
- Provides
Decodefor decoding values from the database. - Provides
Encodefor encoding values for the database. - Types for working with errors produced by SQLx.
- Provides the connection pool for asynchronous SQLx connections.
- Convenience re-export of common traits.
- Types and traits for the
queryfamily of functions and macros. - Runtime query-builder API.
- Conversions between Rust and SQL types.
Macros
- Embeds migrations into the binary by expanding to a static instance of [Migrator][crate::migrate::Migrator].
- query
macrosStatically checked SQL query withprintln!()style syntax. - query_as
macrosA variant of [query!] which takes a path to an explicitly defined struct as the output type. - query_as_unchecked
macrosA variant of [query_as!] which does not check the input or output types. This still does parse the query to ensure it’s syntactically and semantically valid for the current database. - query_file
macrosA variant of [query!] where the SQL query is stored in a separate file. - query_file_as
macrosCombines the syntaxes of [query_as!] and [query_file!]. - query_file_as_unchecked
macrosA variant of [query_file_as!] which does not check the input or output types. This still does parse the query to ensure it’s syntactically and semantically valid for the current database. - query_file_scalar
macrosA variant of query_scalar! which takes a file path like [query_file!]. - A variant of query_file_scalar! which does not typecheck bind parameters and leaves the output type to inference. The query itself is still checked that it is syntactically and semantically valid for the database, that it only produces one column and that the number of bind parameters is correct.
- query_file_unchecked
macrosA variant of [query_file!] which does not check the input or output types. This still does parse the query to ensure it’s syntactically and semantically valid for the current database. - query_scalar
macrosA variant of [query!] which expects a single column from the query and evaluates to an instance of QueryScalar. - query_scalar_unchecked
macrosA variant of query_scalar! which does not typecheck bind parameters and leaves the output type to inference. The query itself is still checked that it is syntactically and semantically valid for the database, that it only produces one column and that the number of bind parameters is correct. - query_unchecked
macrosA variant of [query!] which does not check the input or output types. This still does parse the query to ensure it’s syntactically and semantically valid for the current database.
Structs
- Any
anyOpaque database driver. Capable of being used in place of any SQLx database driver. The actual driver used will be selected at runtime, from the connection url. - SEE DOCUMENTATION BEFORE USE. Runtime-generic database connection.
- MySql
mysqlMySQL database driver. - MySqlConnection
mysqlA connection to a MySQL database. - PgConnection
postgresA connection to a PostgreSQL database. - An asynchronous pool of SQLx database connections.
- Postgres
postgresPostgreSQL database driver. - A builder type for constructing queries at runtime.
- Sqlite
sqliteSqlite database driver. - SqliteConnection
sqliteA connection to an open Sqlite database. - An in-progress database transaction or savepoint.
Enums
- The enum
Eitherwith variantsLeftandRightis a general purpose sum type with two cases. - Represents all the ways a method can fail within SQLx.
Traits
- Acquire connections or transactions from a database in a generic way.
- AnyExecutor
anyAn alias forExecutor<'_, Database = Any>. - A tuple of arguments to be sent to the database.
- Represents a single database connection.
- A database driver.
- A type that can be decoded from the database.
- Encode a single value to be sent to the database.
- A type that may be executed against a database connection.
- A type that contains or can provide a database connection to use for executing queries against the database.
- A record that can be built from a row returned by the database.
- MySqlExecutor
mysqlAn alias forExecutor<'_, Database = MySql>. - PgExecutor
postgresAn alias forExecutor<'_, Database = Postgres>. - Represents a single row from the database.
- SqliteExecutor
sqliteAn alias forExecutor<'_, Database = Sqlite>. - An explicitly prepared statement.
- Indicates that a SQL type is supported for a database.
- Provides information about a SQL type for the database driver.
- An owned value from the database.
- A reference to a single value from the database.
Functions
- Make a SQL query.
- Make a SQL query that is mapped to a concrete type using
FromRow. - Make a SQL query, with the given arguments, that is mapped to a concrete type using
FromRow. - Make a SQL query that is mapped to a single concrete type using
FromRow. - Make a SQL query, with the given arguments, that is mapped to a single concrete type using
FromRow. - Make a SQL query, with the given arguments.
Type Aliases
- SEE DOCUMENTATION BEFORE USE. Type alias for
Pool<Any>. - MySqlPool
mysqlAn alias forPool, specialized for MySQL. - PgPool
postgresAn alias forPool, specialized for Postgres. - A specialized
Resulttype for SQLx. - SqlitePool
sqliteAn alias forPool, specialized for SQLite.
Attribute Macros
- Mark an
async fnas a test with SQLx support.