pub struct Database {
pub db_name: String,
pub tables: HashMap<String, Table>,
pub source_path: Option<PathBuf>,
pub pager: Option<Pager>,
pub txn: Option<TxnSnapshot>,
}Expand description
The database is represented by this structure.assert_eq!
Fields§
§db_name: StringName of this database. (schema name, not filename)
tables: HashMap<String, Table>HashMap of tables in this database
source_path: Option<PathBuf>If Some, every committing SQL statement auto-flushes the DB to
this path. None → transient in-memory mode (the default; the
REPL only enters persistent mode after .open FILE).
pager: Option<Pager>Long-lived pager attached when the database is file-backed. Keeps
an in-memory snapshot of every page so auto-saves can diff
against the last-committed state and skip rewriting unchanged
pages. None means “in-memory only” or “not yet opened”.
txn: Option<TxnSnapshot>Active transaction state (Phase 4f). Some between BEGIN and
the matching COMMIT / ROLLBACK. While set:
- auto-save is suppressed (mutations stay in-memory)
- nested
BEGINis rejected ROLLBACKrestorestablesfrom the snapshot
Implementations§
Source§impl Database
impl Database
Sourcepub fn new(db_name: String) -> Self
pub fn new(db_name: String) -> Self
Creates an empty in-memory Database.
§Examples
use sqlrite::Database;
let mut db = Database::new("my_db".to_string());Sourcepub fn contains_table(&self, table_name: String) -> bool
pub fn contains_table(&self, table_name: String) -> bool
Returns true if the database contains a table with the specified key as a table name.
Sourcepub fn get_table(&self, table_name: String) -> Result<&Table>
pub fn get_table(&self, table_name: String) -> Result<&Table>
Returns an immutable reference of sql::db::table::Table if the database contains a
table with the specified key as a table name.
Sourcepub fn get_table_mut(&mut self, table_name: String) -> Result<&mut Table>
pub fn get_table_mut(&mut self, table_name: String) -> Result<&mut Table>
Returns an mutable reference of sql::db::table::Table if the database contains a
table with the specified key as a table name.
Sourcepub fn is_read_only(&self) -> bool
pub fn is_read_only(&self) -> bool
Returns true if this database is attached to a file and that
file was opened in AccessMode::ReadOnly. In-memory databases
(no pager) and read-write file-backed databases both return
false. Callers use this to reject mutating SQL at the
dispatcher level so the in-memory tables don’t drift away from
disk on a would-be INSERT / UPDATE / DELETE.
Sourcepub fn in_transaction(&self) -> bool
pub fn in_transaction(&self) -> bool
Returns true while a BEGIN … COMMIT/ROLLBACK block is open.
Sourcepub fn begin_transaction(&mut self) -> Result<()>
pub fn begin_transaction(&mut self) -> Result<()>
Starts a transaction: snapshots every table deep-cloned so that
a later rollback_transaction can restore the pre-BEGIN state.
Nested transactions are rejected — explicit savepoints are not
on this phase’s roadmap. Errors on a read-only database.
Sourcepub fn commit_transaction(&mut self) -> Result<()>
pub fn commit_transaction(&mut self) -> Result<()>
Drops the transaction snapshot and returns it for the caller to
discard. The in-memory tables state is the new committed state;
the caller is responsible for flushing to disk via the pager.
Errors if no transaction is open.
Sourcepub fn rollback_transaction(&mut self) -> Result<()>
pub fn rollback_transaction(&mut self) -> Result<()>
Restores tables from the transaction snapshot and clears it.
Errors if no transaction is open.