Skip to main content

Crate wasm_dbms_sql

Crate wasm_dbms_sql 

Source
Expand description

§wasm-dbms-sql

SQL front-end for the wasm-dbms DBMS engine.

The crate turns SQL text into operations on a wasm-dbms database. The schema is still defined with #[derive(Table)]; SQL only reads and writes data.

§Supported SQL

  • SELECT with JOIN, WHERE, DISTINCT, aggregate functions, GROUP BY, HAVING, ORDER BY, LIMIT, and OFFSET
  • INSERT, UPDATE, and DELETE; the last two require a WHERE clause
  • BEGIN, COMMIT, and ROLLBACK, addressed by the transaction id passed to execute
  • positional ? parameters

The full dialect is described in the SQL reference.

§Usage

SqlEngine runs statements. It takes the database schema once and the DbmsContext on every call:

ⓘ
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 result = engine.execute(
    &ctx,
    None,
    "SELECT name FROM users WHERE id = 1",
    &[],
)?;

parse exposes the parser on its own. It returns the syntax tree of a statement, as defined in ast, without touching a database.

§Layout

ModuleRole
lexerSplits SQL text into tokens with their line and column
parserBuilds the ast from the tokens
plannerResolves names, converts values, and builds Query and Filter
engineRuns the plan on the database, outside or inside a transaction

The result and error types, SqlResult and SqlError, are defined in wasm-dbms-api behind its sql feature, which this crate enables.

Modules§

ast
Abstract syntax tree of the supported SQL dialect.

Structs§

ParsedStatement
A parsed statement and the number of ? placeholders it contains.
SqlEngine
Runs SQL statements against a wasm-dbms database.

Functions§

parse
Parses one SQL statement.