Skip to main content

Pool

Enum Pool 

Source
pub enum Pool {
    Postgres(PgPool),
    Mysql(MySqlPool),
    Sqlite(SqlitePool),
}
Expand description

A wrapper around any sqlx pool rustango supports. Cloning is cheap: sqlx already keeps the pool behind an Arc.

Variants§

§

Postgres(PgPool)

§

Mysql(MySqlPool)

§

Sqlite(SqlitePool)

SQLite, either file-backed or :memory:.

Implementations§

Source§

impl Pool

Source

pub async fn connect(url: &str) -> Result<Self, PoolError>

Connect from a URL. The scheme picks the backend and must be postgres://, postgresql://, mysql:// or sqlite:, and its Cargo feature must be on.

§Errors
Source

pub async fn connect_with_timeout( url: &str, timeout: Duration, ) -> Result<Self, PoolError>

Self::connect with its own acquire timeout. That already has a bounded default, see [ACQUIRE_TIMEOUT_ENV], so use this only when one pool must differ from the rest.

§Errors

Same set as Self::connect, plus a Connect error if sqlx times out before the database accepts the connection.

Source

pub async fn connect_diagnosed( url: &str, timeout: Duration, ) -> Result<Self, ConnectDiagnosis>

Self::connect_with_timeout, but a failure comes back as a ConnectDiagnosis instead of a string.

Use this when the caller needs to branch on the kind of fault, as a tenant pre-flight check or an operator console showing per-fault advice does.

§Errors

A ConnectDiagnosis naming the fault, the endpoint tried (with the password removed) and the driver’s own message.

Source

pub async fn connect_from_env() -> Result<Self, PoolError>

Read connection details from environment variables and connect. See crate::env::database_url_from_env for the var resolution order (DATABASE_URL takes precedence; otherwise assembled from DB_DRIVER / DB_HOST / DB_PORT / DB_USER / DB_PASSWORD / DB_NAME / DB_PARAMS).

§Errors

PoolError::Env when the env-var pass fails (missing required vars or unsupported driver), plus the same set as Self::connect.

Source

pub fn connect_lazy(url: &str) -> Result<Self, PoolError>

Self::connect without dialling: the pool connects on the first query. manage::Cli uses it for commands that may never touch the database, so printing help opens no socket.

§Errors

As Self::connect.

Source

pub async fn close(&self)

Close the pool, waiting for its connections to be released.

Dropping a pool schedules the close but does not wait, so a short-lived pool such as a connection probe can outlive the code that made it and keep a socket open. Call this when you are done with the pool and the release must have happened before you return.

Source

pub fn dialect(&self) -> &'static dyn Dialect

The Dialect for this pool’s backend. Use it to ask about identifier quoting, placeholder shape and the rest without caring which backend is underneath.

Source

pub fn backend_name(&self) -> &'static str

The backend’s short name, for logs and manage output. The same as pool.dialect().name().

Source

pub fn scope_key(&self) -> u64

A stable hash of which database this pool talks to. Use it to partition any process-global cache whose rows are facts about one database.

ContentType is the case that needs it: its id comes from that database’s own sequence, so the same natural key is a different number in every tenant. A cache keyed on the natural key alone would hand one tenant another’s id.

On Postgres the hash includes the connect options, and that part is load-bearing. Schema-mode tenants share one host, port and database, and differ only in their search_path. Hashing the connection details alone would collide across all of them.

Two pools to the same database hash the same, which is what a cache wants. Cheap enough for a hot path.

Source

pub fn as_postgres(&self) -> Option<&PgPool>

Borrow as a PgPool for code that needs Postgres itself. None on any other backend.

Source

pub fn as_mysql(&self) -> Option<&MySqlPool>

Borrow as a MySqlPool. Self::as_postgres for MySQL.

Source

pub fn as_sqlite(&self) -> Option<&SqlitePool>

Borrow as a SqlitePool. Self::as_postgres for SQLite.

Source

pub async fn connect_postgres(url: &str) -> Result<PgPool, PoolError>

Connect to Postgres and return the typed pool.

Prefer Self::connect unless you need sqlx::PgPool itself. Use this over PgPool::connect, which applies none of the framework’s pool options.

§Errors

As Self::connect.

Source

pub fn connect_postgres_lazy(url: &str) -> Result<PgPool, PoolError>

Self::connect_postgres without dialling: the pool connects on first use. For commands that may never touch the database.

§Errors

As Self::connect.

Source

pub async fn connect_mysql(url: &str) -> Result<MySqlPool, PoolError>

Connect to MySQL. Self::connect_postgres for MySQL.

§Errors

As Self::connect.

Source

pub fn connect_mysql_lazy(url: &str) -> Result<MySqlPool, PoolError>

Self::connect_mysql without dialling.

§Errors

As Self::connect.

Source

pub async fn connect_sqlite(url: &str) -> Result<SqlitePool, PoolError>

Connect to SQLite. This also applies the framework’s pragmas through [sqlite_connect_options]: WAL for a file-backed database, and ?mode=rwc so a missing file is created.

§Errors

As Self::connect.

Source

pub fn connect_sqlite_lazy(url: &str) -> Result<SqlitePool, PoolError>

Self::connect_sqlite without dialling.

§Errors

As Self::connect.

Trait Implementations§

Source§

impl Clone for Pool

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Pool

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl From<Pool<MySql>> for Pool

Available on crate feature mysql only.
Source§

fn from(p: MySqlPool) -> Self

Converts to this type from the input type.
Source§

impl From<Pool<Postgres>> for Pool

Available on crate feature postgres only.
Source§

fn from(p: PgPool) -> Self

Converts to this type from the input type.
Source§

impl From<Pool<Sqlite>> for Pool

Available on crate feature sqlite only.
Source§

fn from(p: SqlitePool) -> Self

Converts to this type from the input type.

Auto Trait Implementations§

§

impl !RefUnwindSafe for Pool

§

impl !UnwindSafe for Pool

§

impl Freeze for Pool

§

impl Send for Pool

§

impl Sync for Pool

§

impl Unpin for Pool

§

impl UnsafeUnpin for Pool

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> FromRef<T> for T
where T: Clone,

Source§

fn from_ref(input: &T) -> T

Converts to this type from a reference to the input type.
Source§

impl<A, B, T> HttpServerConnExec<A, B> for T
where B: Body,

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more