Skip to main content

Database

Struct Database 

Source
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: String

Name 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 BEGIN is rejected
  • ROLLBACK restores tables from the snapshot

Implementations§

Source§

impl Database

Source

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());
Source

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.

Source

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.

Source

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.

Source

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.

Source

pub fn in_transaction(&self) -> bool

Returns true while a BEGIN … COMMIT/ROLLBACK block is open.

Source

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.

Source

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.

Source

pub fn rollback_transaction(&mut self) -> Result<()>

Restores tables from the transaction snapshot and clears it. Errors if no transaction is open.

Trait Implementations§

Source§

impl Debug for Database

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

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<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.