Skip to main content

Db

Struct Db 

Source
pub struct Db { /* private fields */ }
Expand description

Raw-SQL SQLite database wrapper tuned for concurrent WAL workloads.

Holds two pools on file-backed databases:

  • A single-connection writer pool (max_connections(1)) used by Db::writer, Db::pool, and Db::begin (BEGIN IMMEDIATE).
  • A 16-connection read-only pool used by Db::reader for parallel SELECT queries.

In-memory databases (sqlite::memory: or mode=memory) share the single writer pool for both reads and writes.

Implementations§

Source§

impl Db

Source

pub async fn connect(database_url: &str) -> SlabResult<Self>

Connects to a SQLite database with tuned WAL pragmas and split read/write pools.

Creates the database file when missing and enables foreign_keys = ON, synchronous = NORMAL, cache_size = -64000, and temp_store = MEMORY. File-backed databases also enable journal_mode = WAL, mmap_size = 268435456, and a separate 16-connection read-only pool. Returns SlabError::Internal if database_url cannot be parsed.

Source

pub fn pool(&self) -> &SqlitePool

Returns the default pool, which aliases the single-connection writer pool.

Prefer Db::reader for read-only SELECT queries so reads run concurrently on file-backed databases.

Source

pub fn reader(&self) -> &SqlitePool

Returns the concurrent read-only WAL pool (up to 16 connections on file-backed databases).

Use this pool for SELECT queries. Executing write statements on a file-backed reader pool fails.

Source

pub fn writer(&self) -> &SqlitePool

Returns the dedicated single-connection writer pool (max_connections(1)).

Serializes writes in-process with a 10-second busy_timeout to prevent SQLITE_BUSY thrashing.

Source

pub async fn begin(&self) -> SlabResult<Transaction<'_, Sqlite>>

Opens an explicit BEGIN IMMEDIATE transaction on the single writer connection.

Acquires the write lock at transaction start, avoiding deferred lock-upgrade SQLITE_BUSY_SNAPSHOT errors.

Source

pub async fn init_system_tables(&self) -> SlabResult<()>

Executes SYSTEM_SCHEMA_SQL against the writer pool to create all 11 system tables idempotently.

Source

pub async fn migrate(&self, migrator: &Migrator) -> SlabResult<()>

Creates system tables if missing and runs all pending migrations from migrator on the writer pool.

Wraps migration failures in SlabError::Internal.

Source

pub async fn migrate_dir(&self, dir: &Path) -> SlabResult<()>

Creates system tables if missing and runs all SQL migrations located in dir on the writer pool.

Wraps directory load and migration failures in SlabError::Internal.

Source

pub async fn dump_schema_sql(&self) -> SlabResult<String>

Exports the current SQLite schema DDL from sqlite_master on the read pool.

Excludes internal sqlite_% and _sqlx_% objects and orders statements by table, index, trigger, then view.

Source

pub async fn execute_sql_file(&self, path: &Path) -> SlabResult<()>

Reads a .sql file from disk and executes its statements against the writer pool.

Intended for seed scripts such as slabs db seed. Empty or whitespace-only files are a no-op.

Source

pub async fn ping(&self) -> SlabResult<()>

Executes SELECT 1 against the read pool to verify database connectivity.

Trait Implementations§

Source§

impl Clone for Db

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 Db

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl !RefUnwindSafe for Db

§

impl !UnwindSafe for Db

§

impl Forget for Db

§

impl Freeze for Db
where *const ArcInner<PoolInner<Sqlite>> is !null: Freeze, PhantomData<ArcInner<PoolInner<Sqlite>>>: Freeze,

§

impl Move for Db

§

impl Send for Db
where Arc<PoolInner<Sqlite>>: Send,

§

impl Sync for Db
where Arc<PoolInner<Sqlite>>: Sync,

§

impl Unpin for Db
where Arc<PoolInner<Sqlite>>: Unpin,

§

impl UnsafeUnpin for Db
where *const ArcInner<PoolInner<Sqlite>> is !null: UnsafeUnpin, PhantomData<ArcInner<PoolInner<Sqlite>>>: UnsafeUnpin,

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> 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