sqlike

    A portable SQLite subset with typed statements, an AOT compiler and SQLite page output

    Download zip
    Author
    Version
    0.2.0
    License
    MIT
    Last updated
    2 hours ago
    Downloads
    40

    #mizchi/sqlike

    A SQLite subset for MoonBit, with dynamic SQL, precompiled statements and SQLite image import/export. Supports JS and Wasm GC without a dependency on Litestream.

    Tested with MoonBit 0.10.21+06da1ed44; older core libraries lack the required JSON decoding API.

    moon add mizchi/sqlike@0.2.0

    Import "mizchi/sqlike" from moon.pkg:

    let db = @sqlike.Connection::open(":memory:")
    let statement = db.prepare("SELECT ?")
    statement.bind(index=1, 9007199254740993L)
    if statement.step() {
    let value : Int64 = statement.column(index=0)
    assert_eq(value, 9007199254740993L)
    }
    statement.finalize()
    db.close()

    Bind indices start at 1; column indices start at 0. Public interfaces include Connection, Statement, Bind, Column and Value.

    PackagePurpose
    mizchi/sqlikeBaseline SQL and typed values: CRUD, RETURNING, JOINs, aggregates, indexes, transactions, FK and STRICT
    mizchi/sqlike/fullAll implemented SQL features, including JSON, CTEs, triggers and windows
    mizchi/sqlike/aotPrograms and named prepared statements without the SQL parser; JS/Wasm GC
    mizchi/sqlike/compilerBuild-time schema and SQL compilation
    mizchi/sqlike/ftsOptional full-text search, separate from the SQL runtime

    When upgrading from 0.1.2, imports using JSON, CTEs, triggers or windows must move to mizchi/sqlike/full. Entry types are independent; do not pass a Connection or Statement between entries. AOT connections open compiled program text and prepare queries by their registered name.

    SQLite compatibility is a tested subset. Unsupported SQL is rejected explicitly. Supported SQL · Source generation and build targets.

    JavaScript/TypeScript users can install @mizchi/sqlike. Getting started · Predefined schemas and IndexedDB. MIT.

    Bind

    trait Bind

    Bindings are one-based, result columns zero-based, as in SQLite.
    impl Bind for Int
    impl Bind for Int64
    impl Bind for Double
    impl Bind for String
    impl Bind for Bytes
    impl Bind for BytesView
    impl Bind for StringView

    Column

    trait Column

    impl Column for Int64
    impl Column for Double
    impl Column for String
    impl Column for Bytes

    SqliteError

    pub suberror SqliteError {
    SqliteError(code~ : ErrorCode, extended~ : ExtendedCode?, msg~ : String)
    }

    AotDatabase

    type AotDatabase

    AotPreparedValues

    type AotPreparedValues

    Experimental eager SELECT handle. Owns one Statement, never a result batch. Fixed-schema metadata comes from Program; the action reads the live Store.

    AotSnapshot

    type AotSnapshot

    A frozen row-page root; chunk encoding never builds a database-sized JSON tree.

    AotSnapshotReader

    type AotSnapshotReader

    BinaryCatalog

    type BinaryCatalog

    BinaryResult

    type BinaryResult

    Connection

    type Connection

    Connection::changes

    fn Connection::changes(self : Connection) -> Int64 raise SqliteError

    Connection::close

    fn Connection::close(self : Connection) -> Unit raise SqliteError

    Connection::open

    fn Connection::open(filename : String) -> Connection raise SqliteError

    File/IndexedDB ownership belongs to the host adapter. The portable MoonBit constructor creates a memory connection; AOT adds a program-based constructor.

    Connection::prepare

    fn Connection::prepare(self : Connection, sql : String) -> Statement raise SqliteError

    SQL text entry point for development/build tools. The AOT runtime constructs the same Statement through statement_new without retaining this function.

    Connection::set_clock

    fn Connection::set_clock(self : Connection, milliseconds : Int64) -> Unit raise SqliteError

    Hosts inject one UTC instant before stepping statements that use the clock.

    Database

    type Database

    Opaque connection; only the SQL engine owns and mutates its state.

    DefinedDatabase

    type DefinedDatabase

    ErrorCode

    pub(all) enum ErrorCode {
    Error
    Busy
    ReadOnly
    Corrupt
    Constraint
    Mismatch
    Misuse
    Range
    CantOpen
    Unknown(Int)
    } derive(Eq)

    The typed statement surface follows moonbit-community/sqlite3.mbt. Execution is an injected prepared action so the browser AOT entry cannot pull in lex().

    ExtendedCode

    pub(all) enum ExtendedCode {
    ConstraintCheck
    ConstraintForeignKey
    ConstraintNotNull
    ConstraintPrimaryKey
    ConstraintUnique
    ConstraintTrigger
    ConstraintDatatype
    Unknown(Int)
    } derive(Eq)

    Program

    type Program

    Query

    type Query

    SqlPrepared

    type SqlPrepared

    Opaque SQL handle owned by one Database. Only immutable data-statement ASTs and schema-derived metadata are retained; bindings/rows/results are per call.

    SqlitePageBatch

    type SqlitePageBatch

    SqlitePager

    type SqlitePager

    Explicit, optional export cursor. Clean pages live in the host's bounded cache; this cursor retains only immutable logical row roots and metadata.

    SqliteResult

    type SqliteResult

    Statement

    type Statement

    Statement::bind

    fn[T : Bind] Statement::bind(self : Statement, index~ : Int, value : T) -> Unit raise SqliteError

    Statement::column

    fn[T : Column] Statement::column(self : Statement, index~ : Int) -> T raise SqliteError

    Statement::column_count

    fn Statement::column_count(self : Statement) -> Int raise SqliteError

    Statement::column_name

    fn Statement::column_name(self : Statement, index~ : Int) -> String raise SqliteError

    Statement::finalize

    fn Statement::finalize(self : Statement) -> Unit raise SqliteError

    Statement::reset

    fn Statement::reset(self : Statement, clear_bindings? : Bool) -> Unit raise SqliteError

    Statement::step

    fn Statement::step(self : Statement) -> Bool raise SqliteError

    Value

    pub(all) enum Value {
    Null
    Integer(Int64)
    Real(Double)
    Text(String)
    Blob(Bytes)
    } derive(Eq)

    SQLite storage classes. Int64 values never pass through a JavaScript Number.
    impl Bind for Value
    impl Column for Value

    database_ack_delta

    fn database_ack_delta(db : Database, serial : Int) -> String

    The host calls this only after durable ACK, or after opening a known image.

    database_apply_delta

    fn database_apply_delta(db : Database, text : String) -> String

    database_autocommit

    fn database_autocommit(db : Database) -> Bool

    database_clone

    fn database_clone(db : Database) -> Database

    database_copy_connection_state

    fn database_copy_connection_state(target : Database, source : Database) -> Unit

    Transfer only connection-local counters when a host restores a data clone. No schema, rows, transaction frames, persistence serial or caches are copied.

    database_delta

    fn database_delta(db : Database, force_snapshot : Bool) -> String

    database_dump

    fn database_dump(db : Database) -> String

    A versioned logical image, not a SQLite database file.

    database_exec

    fn database_exec(db : Database, sql : String, bindings : String, read_only : Bool) -> String

    database_exec_values

    fn database_exec_values(db : Database, sql : String, bindings : String) -> String

    Mutable execution with ordinal result columns, including DML RETURNING.

    database_is_readonly

    fn database_is_readonly(sql : String) -> Bool

    database_open

    fn database_open() -> Database

    database_prepare

    fn database_prepare(db : Database, sql : String) -> SqlPrepared

    Parse once; semantic metadata is prepared after validating execution bindings. DDL/transaction/PRAGMA retain the uncached parser/executor, including mutable CREATE TABLE definitions and state-dependent PRAGMA behavior.

    database_prepared_exec

    fn database_prepared_exec(query : SqlPrepared, bindings : String, read_only : Bool, positional : Bool) -> String

    Execute against the handle's owning connection and its current schema/state.

    database_query_values

    fn database_query_values(db : Database, sql : String, bindings : String) -> String

    SQL result columns retain ordinal position even when names are duplicated.

    database_restore

    fn database_restore(db : Database, image : String) -> String

    Decode before replacing the connection; malformed images leave it unchanged.

    database_serial

    fn database_serial(db : Database) -> Int

    database_set_clock

    fn database_set_clock(db : Database, milliseconds : String) -> String

    One clock instant is supplied by the host before executing a statement.

    database_version

    fn database_version(db : Database) -> Int

    Source Files