Skip to main content

SqlEngine

Struct SqlEngine 

Source
pub struct SqlEngine<S> { /* private fields */ }
Expand description

Runs SQL statements against a wasm-dbms database.

The engine holds the database schema and nothing else. It does not borrow the DbmsContext: the context is passed to every execute call, so one engine can serve several contexts and can be stored next to its context, for example in a thread_local!.

§Transactions

Transactions are addressed by their TransactionId, the same ids that DbmsContext::begin_transaction returns. BEGIN opens a transaction and returns its id in SqlResult::TxBegin; passing that id to later calls runs them inside the transaction, where they see its uncommitted writes. COMMIT and ROLLBACK close the transaction whose id they receive. A statement that fails leaves the transaction open. Calls without an id are applied immediately and do not see open transactions.

The engine does not check who holds an id. An application layer that serves several identities keeps its own ledger from id to identity and checks it before calling execute with that id.

§Examples

ⓘ
use wasm_dbms::prelude::*;
use wasm_dbms_api::prelude::*;
use wasm_dbms_memory::prelude::HeapMemoryProvider;
use wasm_dbms_sql::SqlEngine;

#[derive(Clone, DatabaseSchema)]
#[tables(User = "users")]
pub struct MySchema;

let ctx = DbmsContext::new(HeapMemoryProvider::default());
MySchema::register_tables(&ctx)?;
let engine = SqlEngine::new(MySchema);

engine.execute(
    &ctx,
    None,
    "INSERT INTO users (id, name) VALUES (?, ?)",
    &[Value::from(1u32), Value::from("Alice")],
)?;

let SqlResult::TxBegin(tx) = engine.execute(&ctx, None, "BEGIN", &[])? else {
    unreachable!("BEGIN returns TxBegin");
};
engine.execute(&ctx, Some(tx), "UPDATE users SET name = 'Alicia' WHERE id = 1", &[])?;
engine.execute(&ctx, Some(tx), "COMMIT", &[])?;

Implementations§

Source§

impl<S> SqlEngine<S>

Source

pub fn new(schema: S) -> Self

Creates an engine for the tables of schema.

Source

pub fn execute<'ctx, M>( &self, ctx: &'ctx DbmsContext<M>, tx: Option<TransactionId>, sql: &str, params: &[Value], ) -> Result<SqlResult, SqlError>
where M: MemoryProvider, S: DatabaseSchema<M> + Clone + 'ctx,

Parses and runs one SQL statement, outside or inside a transaction.

tx selects where the statement runs: None applies it immediately through WasmDbmsDatabase::oneshot, Some(id) runs it through WasmDbmsDatabase::from_transaction. BEGIN requires None and returns the new id in SqlResult::TxBegin; COMMIT and ROLLBACK require Some(id). params supplies one value per ? placeholder, in order.

§Errors

Trait Implementations§

Source§

impl<S> Debug for SqlEngine<S>

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<S> Freeze for SqlEngine<S>
where S: Freeze,

§

impl<S> RefUnwindSafe for SqlEngine<S>
where S: RefUnwindSafe,

§

impl<S> Send for SqlEngine<S>
where S: Send,

§

impl<S> Sync for SqlEngine<S>
where S: Sync,

§

impl<S> Unpin for SqlEngine<S>
where S: Unpin,

§

impl<S> UnsafeUnpin for SqlEngine<S>
where S: UnsafeUnpin,

§

impl<S> UnwindSafe for SqlEngine<S>
where S: UnwindSafe,

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

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.