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 byDb::writer,Db::pool, andDb::begin(BEGIN IMMEDIATE). - A 16-connection read-only pool used by
Db::readerfor parallelSELECTqueries.
In-memory databases (sqlite::memory: or mode=memory) share the single writer pool for both reads and writes.
Implementations§
Source§impl Db
impl Db
Sourcepub async fn connect(database_url: &str) -> SlabResult<Self>
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.
Sourcepub fn pool(&self) -> &SqlitePool
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.
Sourcepub fn reader(&self) -> &SqlitePool
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.
Sourcepub fn writer(&self) -> &SqlitePool
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.
Sourcepub async fn begin(&self) -> SlabResult<Transaction<'_, Sqlite>>
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.
Sourcepub async fn init_system_tables(&self) -> SlabResult<()>
pub async fn init_system_tables(&self) -> SlabResult<()>
Executes SYSTEM_SCHEMA_SQL against the writer pool to create all 11 system tables idempotently.
Sourcepub async fn migrate(&self, migrator: &Migrator) -> SlabResult<()>
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.
Sourcepub async fn migrate_dir(&self, dir: &Path) -> SlabResult<()>
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.
Sourcepub async fn dump_schema_sql(&self) -> SlabResult<String>
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.
Sourcepub async fn execute_sql_file(&self, path: &Path) -> SlabResult<()>
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.
Sourcepub async fn ping(&self) -> SlabResult<()>
pub async fn ping(&self) -> SlabResult<()>
Executes SELECT 1 against the read pool to verify database connectivity.
Trait Implementations§
Auto Trait Implementations§
impl !RefUnwindSafe for Db
impl !UnwindSafe for Db
impl Forget for Db
impl Freeze for Db
impl Move for Db
impl Send for Db
impl Sync for Db
impl Unpin for Db
impl UnsafeUnpin for Dbwhere
*const ArcInner<PoolInner<Sqlite>> is !null: UnsafeUnpin,
PhantomData<ArcInner<PoolInner<Sqlite>>>: UnsafeUnpin,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
impl<A, B, T> HttpServerConnExec<A, B> for Twhere
B: Body,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
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