sqlike

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

    Download zip
    Author
    Version
    0.1.0
    License
    MIT
    Last updated
    3 hours ago
    Downloads
    7

    #mizchi/sqlike

    現在は 0.1.0。index・trigger、SQLトランザクション・CAST・JSON・日時・window・再帰CTE・移行を実装済み。全文検索は任意の mizchi/sqlike/fts に分離し、通常バンドルには含めない。一般的なアプリの利用シナリオと永続化・同期の必須プロファイルを検証した。対応計画・完了条件を参照。

    機能選択プロファイルでRIGHT/FULL JOIN、STRICT、主キー索引、trigger、FKなどを選択し、JS/Wasmを生成できる。AOTはパーサを含まない。binaryの副作用適用は別entryの任意wrapperで有効にする。

    MoonBit 製の SQLite サブセット。値・SQL parser・評価・制約・行ストア、SQLite ファイルの読取・出力を持つ。mizchi/litestream に依存せず利用できる。開発方針に従い、速度と既存の互換性を維持しながら SQLite の対応範囲を広げる。全機能の互換性は未達。

    パッケージ用途
    mizchi/sqlikeConnection / Statement / Bind / Column / Value、事前コンパイルした Program、DB イメージと SQLite ページ
    mizchi/sqlike/compilerビルド時の compile_program。SQL 文字列を解析して固定スキーマとクエリ AST を生成
    mizchi/sqlike/fts任意の転置index・phrase / prefix / boolean検索・BM25。通常のSQL runtimeから独立

    利用する moon.pkg から "mizchi/sqlike" を import し、@sqlike.Connection::open(":memory:") または Connection::from_program を使う。bind は1始まり、column は0始まり。型付き API は sqlite3.mbt を参考にしている。

    リポジトリの moon.work ではローカルソースから解決する。just sqlike-check で native SQLite との直接差分、just sqlike-bench で速度を検査する。ルートで just test-sqlike を実行すると JS / Wasm GC のモジュールテストを行う。外部ワークスペースからの利用も consumer test で確認する。

    ブラウザの parser を除外した実行モジュールは mizchi/litestream-example/runtime が構成する。SQL の検証範囲、AOT の利用手順を参照。

    moon.modに依存を追加する。

    import {
    "mizchi/sqlike@0.1.0",
    }

    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

    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::export_sqlite

    fn Connection::export_sqlite(connection : Connection) -> Bytes raise SqliteError

    Connection::from_program

    fn Connection::from_program(program : Program) -> Connection

    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::prepare_compiled

    fn Connection::prepare_compiled(self : Connection, query : Query) -> Statement raise SqliteError

    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

    Program::load

    fn Program::load(input : String) -> Program raise SqliteError

    Load an application build artifact once. It is not an untrusted SQL endpoint.

    Program::load_binary

    fn Program::load_binary(schema : Bytes, statements : Bytes) -> Program raise SqliteError

    Load fixed schema and named prepared queries without parsing SQL or JSON.

    Program::query

    fn Program::query(self : Program, name : String) -> Query raise SqliteError

    Query

    type Query

    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

    aot_ack_delta

    fn aot_ack_delta(db : AotDatabase, serial : Int) -> String

    aot_apply_delta

    fn aot_apply_delta(db : AotDatabase, text : String) -> String

    aot_clone

    fn aot_clone(db : AotDatabase) -> AotDatabase

    aot_copy_connection_state

    fn aot_copy_connection_state(target : AotDatabase, source : AotDatabase) -> String

    aot_delta

    fn aot_delta(db : AotDatabase, force_snapshot : Bool) -> String

    aot_dump

    fn aot_dump(db : AotDatabase) -> String

    aot_execute

    fn aot_execute(db : AotDatabase, name : String, bindings : String) -> String

    aot_export_sqlite

    fn aot_export_sqlite(db : AotDatabase) -> SqliteResult

    aot_info

    fn aot_info(db : AotDatabase) -> String

    aot_open

    fn aot_open(artifact : String, image : String) -> AotDatabase

    aot_query_info

    fn aot_query_info(db : AotDatabase, name : String) -> String

    aot_restore

    fn aot_restore(db : AotDatabase, image : String) -> String

    aot_set_clock

    fn aot_set_clock(db : AotDatabase, milliseconds : String) -> String

    binary_catalog_info

    fn binary_catalog_info(catalog : BinaryCatalog) -> String

    binary_catalog_schema

    fn binary_catalog_schema(catalog : BinaryCatalog) -> Bytes

    binary_catalog_statements

    fn binary_catalog_statements(catalog : BinaryCatalog) -> Bytes

    binary_catalog_storage

    fn binary_catalog_storage(catalog : BinaryCatalog) -> Bytes

    binary_data

    fn binary_data(result : BinaryResult) -> Bytes

    binary_data_base64

    fn binary_data_base64(result : BinaryResult) -> String

    binary_info

    fn binary_info(result : BinaryResult) -> String

    compile_program

    fn compile_program(request : String) -> String

    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_changeset

    fn database_changeset(db : Database, before : Database) -> String

    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_import_sqlite

    fn database_import_sqlite(db : Database, image : Bytes) -> String

    database_import_sqlite_base64

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

    Portable string ABI for Wasm GC. The JavaScript backend uses bytes directly.

    database_is_readonly

    fn database_is_readonly(sql : String) -> Bool

    database_open

    fn database_open() -> Database

    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

    defined_ack_delta

    fn defined_ack_delta(db : DefinedDatabase, serial : Int) -> String

    defined_apply_delta

    fn defined_apply_delta(db : DefinedDatabase, bytes : Bytes) -> String

    defined_apply_delta_base64

    fn defined_apply_delta_base64(db : DefinedDatabase, text : String) -> String

    defined_clone

    fn defined_clone(db : DefinedDatabase) -> DefinedDatabase

    defined_copy_connection_state

    fn defined_copy_connection_state(target : DefinedDatabase, source : DefinedDatabase) -> String

    defined_delta

    fn defined_delta(db : DefinedDatabase, force_snapshot : Bool) -> BinaryResult

    defined_execute

    fn defined_execute(db : DefinedDatabase, name : String, bindings : String) -> String

    defined_export_sqlite

    fn defined_export_sqlite(schema : Bytes, statements : Bytes, layout : Bytes, image : Bytes) -> SqliteResult

    defined_info

    fn defined_info(db : DefinedDatabase) -> String

    defined_open

    fn defined_open(schema : Bytes, statements : Bytes, image : Bytes) -> DefinedDatabase

    defined_open_base64

    fn defined_open_base64(schema : String, statements : String, image : String) -> DefinedDatabase

    defined_query_info

    fn defined_query_info(db : DefinedDatabase, name : String) -> String

    defined_restore

    fn defined_restore(db : DefinedDatabase, bytes : Bytes) -> String

    defined_restore_base64

    fn defined_restore_base64(db : DefinedDatabase, text : String) -> String

    defined_set_clock

    fn defined_set_clock(db : DefinedDatabase, millis : String) -> String

    defined_snapshot

    fn defined_snapshot(db : DefinedDatabase) -> BinaryResult

    pack_program_binary

    fn pack_program_binary(artifact : String) -> BinaryCatalog

    sqlite_data

    fn sqlite_data(result : SqliteResult) -> Bytes

    sqlite_data_base64

    fn sqlite_data_base64(result : SqliteResult) -> String

    sqlite_info

    fn sqlite_info(result : SqliteResult) -> String