Expand description
A connection pool to a SQL database.
A pool is created through the builder method, starting from a connection
string that allows some of the parameters be delivered by the user.
A connection string has the following structure:
connector_type://user:password@host/database?parameters
Connector type can be one of the following:
fileopens an SQLite connection.mysqlopens a MySQL connection.postgres/postgresqlopens a PostgreSQL connection.
All parameters should be given in the query string format:
?key1=val1&key2=val2. All parameters are optional.
As a special case, Microsoft SQL Server connections use the JDBC URI format:
jdbc:sqlserver://host\instance:port;key1=val1;key2=val2;
Common parameters
connection_limitdefines the maximum number of connections opened to the database.
SQLite
user/passworddo not do anything and can be emitted.hostshould point to the database file.db_nameparameter should give a name to the database attached for query namespacing.socket_timeoutdefined in seconds. Acts as the busy timeout in SQLite. When set, queries that are waiting for a lock to be released will return theTimeouterror after the defined value.
PostgreSQL
sslmodeeitherdisable,preferorrequire. Read moresslcertshould point to a PEM certificate file.sslidentityshould point to a PKCS12 certificate database.sslpasswordthe password to open the PKCS12 database.sslaccepteitherstrictoraccept_invalid_certs. If strict, the certificate needs to be valid and in the CA certificates.accept_invalid_certsaccepts any certificate from the server and can lead to weakened security. Defaults toaccept_invalid_certs.schemathe default search path.hostadditionally the host can be given as a parameter, typically in cases when connectiong to the database through a unix socket to separate the database name from the database path, such aspostgresql:///dbname?host=/var/run/postgresql.socket_timeoutdefined in seconds. If set, a query will return aTimeouterror if it fails to resolve before given time.connect_timeoutdefined in seconds. Connecting to a database will return aConnectTimeouterror if taking more than the defined value. Defaults to 5 seconds, if set to 0, no timeout.pool_timeoutdefined in seconds. If all connections are in use, the database will return aPoolTimeouterror after waiting for the given time. If set to zero, no timeout.pgbouncereithertrueorfalse. If set, allows usage with the pgBouncer connection pool in transaction mode. Additionally a transaction is required for every query for the mode to work. When starting a new transaction, a deallocation queryDEALLOCATE ALLis executed right afterBEGINto avoid possible collisions with statements created in other sessions.statement_cache_size, number of prepared statements kept cached. Defaults to 500. Ifpgbouncermode is enabled, caching is always off.optionsSpecifies command-line options to send to the server at connection start. Read more
MySQL
sslcertshould point to a PEM certificate file.sslidentityshould point to a PKCS12 certificate database.sslpasswordthe password to open the PKCS12 database.sslaccepteitherstrictoraccept_invalid_certs. If strict, the certificate needs to be valid and in the CA certificates.accept_invalid_certsaccepts any certificate from the server and can lead to weakened security. Defaults tostrict.socketneeded when connecting to MySQL database through a unix socket. When set, the host parameter is dismissed.socket_timeoutdefined in seconds. If set, a query will return aTimeouterror if it fails to resolve before given time.connect_timeoutdefined in seconds. Connecting to a database will return aConnectTimeouterror if taking more than the defined value. Defaults to 5 seconds, if set to 0, no timeout.pool_timeoutdefined in seconds. If all connections are in use, the database will return aPoolTimeouterror after waiting for the given time. If set to zero, no timeout.statement_cache_size, number of prepared statements kept cached. Defaults to 1000. Set to 0 to disable caching.
Microsoft SQL Server
encryptif set totrueencrypts all traffic over TLS. Iffalse, only the login details are encrypted. A special valueDANGER_PLAINTEXTwill disable TLS completely, including sending login credentials as plaintext.usersets the login name.passwordsets the login password.databasesets the database to connect to.trustServerCertificateif set totrue, accepts any kind of certificate from the server.trustServerCertificateCAsets the path to a custom certificate file. Needs to be in pem, crt or der format. Cannot be used together withtrustServerCertificateparameter.socketTimeoutdefined in seconds. If set, a query will return aTimeouterror if it fails to resolve before given time.connectTimeoutdefined in seconds (default: 5). Connecting to a database will return aConnectTimeouterror if taking more than the defined value. Defaults to 5 seconds, disabled if set to zero.poolTimeoutdefined in seconds. If all connections are in use, the database will return aTimeouterror after waiting for the given time. If set to zero, no timeout.connectionLimitdefines the maximum number of connections opened to the database.schemathe name of the lookup schema. Only stored to the connection, must be used in every query to be effective.isolationLevelthe transaction isolation level. Possible values:READ UNCOMMITTED,READ COMMITTED,REPEATABLE READ,SNAPSHOT,SERIALIZABLE.
To create a new Quaint pool connecting to a PostgreSQL database:
use quaint::{prelude::*, pooled::Quaint};
use std::time::Duration;
#[tokio::main]
async fn main() -> Result<(), quaint::error::Error> {
let mut builder = Quaint::builder("postgresql://postgres:password@localhost:5432/postgres")?;
builder.connection_limit(5);
builder.max_idle_lifetime(Duration::from_secs(300));
builder.test_on_check_out(true);
let pool = builder.build();
let conn = pool.check_out().await?;
let result = conn.select(Select::default().value(1)).await?;
assert_eq!(
Some(1),
result.into_iter().nth(0).and_then(|row| row[0].as_i64()),
);
Ok(())
}