commit ddb130a0e64bf7b8c705dccf9dfabac92797ef57 Author: Waozi Date: Tue Sep 29 22:07:56 2026 -0300 Add the SQLite C library as a Ziran package The binding started in Inbe's storage code: SQLite's own functions and return codes, plus variants that take Ziran strings and copy them to the NUL-terminated text SQLite expects. The test opens an in-memory database and checks text with escapes, 64-bit integers, statement reuse, long SQL, errors, and a refused tail pointer. diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..759c9de --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,30 @@ +name: CI + +on: + push: + branches: [master] + pull_request: + workflow_dispatch: + +jobs: + test: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v7 + with: + path: sqlite + + - name: Install SQLite + run: sudo apt-get update && sudo apt-get install -y libsqlite3-dev + + - name: Build the ziran launcher + run: | + git clone --depth 1 https://github.com/ziranlang/ziran.git ziran + make -C ziran + echo "$PWD/ziran/build/bin" >> "$GITHUB_PATH" + + - name: Test + working-directory: sqlite + run: | + ziran fetch --locked + make check diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9365aaf --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +build/ +/ziran.local.toml diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..9eeb030 --- /dev/null +++ b/LICENSE @@ -0,0 +1,28 @@ +BSD 3-Clause License + +Copyright (c) 2026, Waozi + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + +1. Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimer. + +2. Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + +3. Neither the name of the copyright holder nor the names of its contributors + may be used to endorse or promote products derived from this software without + specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND +ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE +FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR +SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, +OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..a381774 --- /dev/null +++ b/Makefile @@ -0,0 +1,24 @@ +.DEFAULT_GOAL := check + +ZIRAN ?= ziran +CC ?= cc +BUILD := build +SQLITE_LIBS ?= $(shell pkg-config --libs sqlite3 2>/dev/null || echo -lsqlite3) + +.PHONY: check test clean + +# Checks the package, then runs the test against the system SQLite. The +# portable bundle cannot call into the SQLite C library, so the test is +# native only. +check: test + +test: + $(ZIRAN) check --project + $(ZIRAN) build --project --target=c --entry sqlite_test:main \ + -o $(BUILD)/test-c tests/sqlite_test.zi + $(CC) -std=c99 -pedantic-errors -I$$($(ZIRAN) pkg path ziran)/include \ + -I$(BUILD)/test-c $(BUILD)/test-c/*.c $(SQLITE_LIBS) -o $(BUILD)/sqlite-test + $(BUILD)/sqlite-test + +clean: + rm -rf $(BUILD) diff --git a/README.md b/README.md new file mode 100644 index 0000000..785c473 --- /dev/null +++ b/README.md @@ -0,0 +1,33 @@ +# SQLite + +The [SQLite](https://sqlite.org) C library for Ziran programs, as a package. + +```sh +ziran add https://github.com/ziranlang/sqlite.git +``` + +```zi +#import "SQLite/SQLite" + +database: *void = null +flags: s32 = SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE +if SqliteOpen(path, *database, flags) == SQLITE_OK { + SqliteExecText(database, "create table if not exists votes (voter text, score integer)") +} +``` + +The functions keep SQLite's names and return codes (`SqlitePrepare`, +`SqliteStep`, `SqliteColumnText`, `SqliteBindInt64`, …). The `*Text` and +`*String` variants take Ziran strings and copy them to the NUL-terminated +text SQLite expects, so a temporary string is safe to pass. + +A program links SQLite itself: the system `libsqlite3`, or the SQLite +amalgamation compiled into its own build (as web and Android builds do). +The package calls into C, so it runs in native builds, not the portable +bundle. + +## Test + +```sh +make check # needs libsqlite3; set SQLITE_LIBS if pkg-config cannot find it +``` diff --git a/src/sqlite.zi b/src/sqlite.zi new file mode 100644 index 0000000..84cf3b5 --- /dev/null +++ b/src/sqlite.zi @@ -0,0 +1,216 @@ +// The SQLite C library for Ziran programs. Databases and statements are +// opaque handles owned by SQLite. The functions follow SQLite's own names and +// return codes; the *Text variants take Ziran strings and copy them to the +// NUL-terminated text SQLite expects. Programs link libsqlite3, or compile +// SQLite's amalgamation into their own build. +SQLITE_OK :: 0; +SQLITE_ROW :: 100; +SQLITE_DONE :: 101; +SQLITE_BUSY :: 5; +SQLITE_TRANSIENT :: -1; +SQLITE_LOCKED :: 6; +SQLITE_NOMEM :: 7; +SQLITE_TOOBIG :: 18; +SQLITE_MISUSE :: 21; +SQLITE_OPEN_READWRITE :: 0x2; +SQLITE_OPEN_CREATE :: 0x4; +SQLITE_OPEN_FULLMUTEX :: 0x10000; + +#scope_file +sqlite :: #system_library "sqlite3"; +c :: #system_library "c"; +malloc :: (size: usize) -> *void #foreign c; +free :: (pointer: *void) #foreign c; + +PrepareRaw :: (db: *void, sql: *u8, length: s32, + statement: **void, tail: **u8) -> s32 #foreign sqlite "sqlite3_prepare_v2"; +StepRaw :: (statement: *void) -> s32 #foreign sqlite "sqlite3_step"; +FinalizeRaw :: (statement: *void) -> s32 #foreign sqlite "sqlite3_finalize"; +ColumnTextRaw :: (statement: *void, column: s32) -> *u8 #foreign sqlite "sqlite3_column_text"; +ColumnInt64Raw :: (statement: *void, column: s32) -> s64 #foreign sqlite "sqlite3_column_int64"; +BindTextRaw :: (statement: *void, index: s32, value: *u8, + length: s32, destructor: *void) -> s32 #foreign sqlite "sqlite3_bind_text"; +BindInt64Raw :: (statement: *void, index: s32, value: s64) -> s32 #foreign sqlite "sqlite3_bind_int64"; +BindIntRaw :: (statement: *void, index: s32, value: s32) -> s32 #foreign sqlite "sqlite3_bind_int"; +ColumnIntRaw :: (statement: *void, column: s32) -> s32 #foreign sqlite "sqlite3_column_int"; +ResetRaw :: (statement: *void) -> s32 #foreign sqlite "sqlite3_reset"; +ClearBindingsRaw :: (statement: *void) -> s32 #foreign sqlite "sqlite3_clear_bindings"; +OpenRaw :: (path: *u8, database: **void, flags: s32, + vfs: *u8) -> s32 #foreign sqlite "sqlite3_open_v2"; +CloseRaw :: (database: *void) -> s32 #foreign sqlite "sqlite3_close"; +BackupInitRaw :: (destination: *void, destination_name: *u8, + source: *void, source_name: *u8) -> *void #foreign sqlite "sqlite3_backup_init"; +BackupStepRaw :: (backup: *void, pages: s32) -> s32 #foreign sqlite "sqlite3_backup_step"; +BackupFinishRaw :: (backup: *void) -> s32 #foreign sqlite "sqlite3_backup_finish"; +ExecRaw :: (database: *void, sql: *u8, callback: *void, + argument: *void, error: **u8) -> s32 #foreign sqlite "sqlite3_exec"; + +CopyStringBytes :: (source: string, destination: *u8) { + index: s32 = 0 + while index < cast(s32)source.count { + destination[index] = source[index] + index += 1 + } + destination[index] = cast(u8)0 +} + +#scope_export +#program_export +SqlitePrepare :: (db: *void, sql: *u8, length: s32, + statement: **void, tail: **u8) -> s32 { + return PrepareRaw(db, sql, length, statement, tail) +} + +#scope_export +#program_export +SqlitePrepareText :: (db: *void, sql: string, length: s32, + statement: **void, tail: **u8) -> s32 { + if tail != null || sql.count > 2147483647 || length < -1 || + (length >= 0 && cast(s64)length > cast(s64)sql.count) { + return SQLITE_MISUSE + } + if sql.count < 512 { + short_sql: [512]u8 + CopyStringBytes(sql, *short_sql[0]) + return PrepareRaw(db, *short_sql[0], length, statement, null) + } + buffer: *u8 = cast(*u8)malloc(cast(usize)sql.count + 1) + if buffer == null { + return SQLITE_NOMEM + } + CopyStringBytes(sql, buffer) + result := PrepareRaw(db, buffer, length, statement, null) + free(cast(*void)buffer) + return result +} + +#scope_export +#program_export +SqliteBindTextString :: (statement: *void, index: s32, value: string, length: s32) -> s32 { + if value.count > 2147483647 || length < -1 || + (length >= 0 && cast(s64)length > cast(s64)value.count) { + return SQLITE_MISUSE + } + if value.count < 256 { + short_value: [256]u8 + CopyStringBytes(value, *short_value[0]) + return BindTextRaw(statement, index, *short_value[0], length, + cast(*void)cast(s64)SQLITE_TRANSIENT) + } + buffer: *u8 = cast(*u8)malloc(cast(usize)value.count + 1) + if buffer == null { + return SQLITE_NOMEM + } + CopyStringBytes(value, buffer) + result := BindTextRaw(statement, index, buffer, length, + cast(*void)cast(s64)SQLITE_TRANSIENT) + free(cast(*void)buffer) + return result +} + +#program_export +SqliteStep :: (statement: *void) -> s32 { + return StepRaw(statement) +} + +#program_export +SqliteFinalize :: (statement: *void) -> s32 { + return FinalizeRaw(statement) +} + +#program_export +SqliteColumnText :: (statement: *void, column: s32) -> *u8 { + return ColumnTextRaw(statement, column) +} + +#program_export +SqliteColumnInt64 :: (statement: *void, column: s32) -> s64 { + return ColumnInt64Raw(statement, column) +} + +#program_export +SqliteBindText :: (statement: *void, index: s32, value: *u8, + length: s32, destructor: *void) -> s32 { + return BindTextRaw(statement, index, value, length, destructor) +} + +#program_export +SqliteBindInt64 :: (statement: *void, index: s32, value: s64) -> s32 { + return BindInt64Raw(statement, index, value) +} + +#program_export +SqliteBindInt :: (statement: *void, index: s32, value: s32) -> s32 { + return BindIntRaw(statement, index, value) +} + +#program_export +SqliteColumnInt :: (statement: *void, column: s32) -> s32 { + return ColumnIntRaw(statement, column) +} + +#program_export +SqliteReset :: (statement: *void) -> s32 { + return ResetRaw(statement) +} + +#program_export +SqliteClearBindings :: (statement: *void) -> s32 { + return ClearBindingsRaw(statement) +} + +#program_export +SqliteOpen :: (path: *u8, database: **void, flags: s32) -> s32 { + return OpenRaw(path, database, flags, null) +} + +#program_export +SqliteClose :: (database: *void) -> s32 { + return CloseRaw(database) +} + +#program_export +SqliteBackupInit :: (destination: *void, source: *void) -> *void { + name: [5]u8 + name[0] = cast(u8)109 + name[1] = cast(u8)97 + name[2] = cast(u8)105 + name[3] = cast(u8)110 + name[4] = cast(u8)0 + return BackupInitRaw(destination, *name[0], source, *name[0]) +} + +#program_export +SqliteBackupStep :: (backup: *void, pages: s32) -> s32 { + return BackupStepRaw(backup, pages) +} + +#program_export +SqliteBackupFinish :: (backup: *void) -> s32 { + return BackupFinishRaw(backup) +} + +#program_export +SqliteExec :: (database: *void, sql: *u8) -> s32 { + return ExecRaw(database, sql, null, null, null) +} + +#program_export +SqliteExecText :: (database: *void, sql: string) -> s32 { + if sql.count > 2147483647 { + return SQLITE_TOOBIG + } + if sql.count < 256 { + short_sql: [256]u8 + CopyStringBytes(sql, *short_sql[0]) + return ExecRaw(database, *short_sql[0], null, null, null) + } + buffer: *u8 = cast(*u8)malloc(cast(usize)sql.count + 1) + if buffer == null { + return SQLITE_NOMEM + } + CopyStringBytes(sql, buffer) + result := ExecRaw(database, buffer, null, null, null) + free(cast(*void)buffer) + return result +} diff --git a/tests/sqlite_test.zi b/tests/sqlite_test.zi new file mode 100644 index 0000000..d0fd8c6 --- /dev/null +++ b/tests/sqlite_test.zi @@ -0,0 +1,82 @@ +#import "sqlite" + +// Reads a NUL-terminated column value as text. +Same :: (value: *u8, expected: string) -> bool { + if value == null { + return false + } + index: s64 = 0 + while index < expected.count { + if value[index] != expected[index] { + return false + } + index += 1 + } + return value[expected.count] == cast(u8)0 +} + +#program_export +main :: () -> s32 { + path: [9]u8 = .{58, 109, 101, 109, 111, 114, 121, 58, 0} + database: *void = null + flags: s32 = SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE + if SqliteOpen(*path[0], *database, flags) != SQLITE_OK || database == null { + return 1 + } + if SqliteExecText(database, "create table votes (voter text not null, score integer not null)") != SQLITE_OK { + return 2 + } + statement: *void = null + if SqlitePrepareText(database, "insert into votes (voter, score) values (?, ?)", -1, *statement, null) != SQLITE_OK { + return 3 + } + // Binding copies the text, so a temporary Ziran string is safe. + if SqliteBindTextString(statement, 1, "Café \"night\"", -1) != SQLITE_OK || + SqliteBindInt(statement, 2, -3) != SQLITE_OK || + SqliteStep(statement) != SQLITE_DONE { + return 4 + } + if SqliteReset(statement) != SQLITE_OK || SqliteClearBindings(statement) != SQLITE_OK { + return 5 + } + if SqliteBindTextString(statement, 1, "bob", -1) != SQLITE_OK || + SqliteBindInt64(statement, 2, 5000000000) != SQLITE_OK || + SqliteStep(statement) != SQLITE_DONE { + return 6 + } + if SqliteFinalize(statement) != SQLITE_OK { + return 7 + } + if SqlitePrepareText(database, "select voter, score from votes order by rowid", -1, *statement, null) != SQLITE_OK { + return 8 + } + if SqliteStep(statement) != SQLITE_ROW || !Same(SqliteColumnText(statement, 0), "Café \"night\"") || + SqliteColumnInt(statement, 1) != -3 { + return 9 + } + if SqliteStep(statement) != SQLITE_ROW || SqliteColumnInt64(statement, 1) != 5000000000 { + return 10 + } + if SqliteStep(statement) != SQLITE_DONE || SqliteFinalize(statement) != SQLITE_OK { + return 11 + } + // A statement longer than the short buffers takes the allocated path. + long_sql := "select 1 " + if SqlitePrepareText(database, long_sql, -1, *statement, null) != SQLITE_OK || + SqliteStep(statement) != SQLITE_ROW || SqliteColumnInt(statement, 0) != 1 || + SqliteFinalize(statement) != SQLITE_OK { + return 12 + } + if SqliteExecText(database, "not sql") == SQLITE_OK { + return 13 + } + // A tail pointer is refused: the copied SQL does not outlive the call. + tail: *u8 = null + if SqlitePrepareText(database, "select 1", -1, *statement, *tail) != SQLITE_MISUSE { + return 14 + } + if SqliteClose(database) != SQLITE_OK { + return 15 + } + return 0 +} diff --git a/ziran.lock b/ziran.lock new file mode 100644 index 0000000..529e47c --- /dev/null +++ b/ziran.lock @@ -0,0 +1,15 @@ +{ + "format": 1, + "packages": [ + + ], + "root": { + "dependencies": {}, + "name": "SQLite" + }, + "toolchain": { + "commit": "d4489a3e09de5769256eca4994382f25208e99d8", + "ref": "master", + "url": "https://github.com/ziranlang/ziran.git" + } +} diff --git a/ziran.toml b/ziran.toml new file mode 100644 index 0000000..e0c449a --- /dev/null +++ b/ziran.toml @@ -0,0 +1,10 @@ +[package] +name = "SQLite" +module_roots = ["src", "tests"] + +[toolchain] +git = "https://github.com/ziranlang/ziran.git" +ref = "master" + +[exports] +SQLite = "src/sqlite.zi"