diff --git a/Makefile b/Makefile index e107430..e8d5283 100644 --- a/Makefile +++ b/Makefile @@ -35,5 +35,7 @@ test: native --library-dir $(BUILD)/native '--libraries=-lsqlite3 -lm -ldl -pthread' \ --target $(TARGET) + python3 "$$($(ZIRAN) pkg path ziran)/scripts/test_native_package.py" --ziran "$(ZIRAN)" --source tests/sqlite_callback_test.zi --entry sqlite_callback_test:main --library-dir $(BUILD)/native '--libraries=-lsqlite3 -lm -ldl -pthread' --target c --build-dir $(BUILD)/callback-backends + clean: rm -rf $(BUILD) diff --git a/README.md b/README.md index 1e0c583..210a975 100644 --- a/README.md +++ b/README.md @@ -29,6 +29,33 @@ from a Ziran string; `SqliteBindBlob` copies bytes including embedded NULs, and an empty slice binds an empty BLOB. Column pointers and error messages are borrowed from SQLite; consume or copy them before their owner changes. +`SqliteGetAutocommit(database)` reports whether the live connection has no +active transaction, following SQLite's `sqlite3_get_autocommit`. It returns +false for a null handle. Keep the connection alive while calling it. + +`SqliteCreateFunction` binds `sqlite3_create_function_v2` directly. Its +`SqliteScalar` and `SqliteDestructor` types use `#c_call`; callback programs +require the native C backend. The common connection and statement APIs still +support C and Go. Pass a NUL-terminated function name and SQLite's documented +argument count and encoding flags; use null step/final callbacks for a scalar. +No validation or early return in the binding changes SQLite's destructor rules. + +The data pointer remains the caller's responsibility until SQLite calls its +destructor. SQLite calls that destructor on replacement, deletion, connection +close, and registration failure, including a name longer than 255 UTF-8 bytes. +Do not free the same data again after an error. `SqliteUserData` returns this +pointer inside the registered callback; each connection has its own +registration. See the [function registration contract](https://www.sqlite.org/c3ref/create_function.html) +and [user data contract](https://www.sqlite.org/c3ref/user_data.html). + +`SqliteValueType`, `SqliteValueBytes` and `SqliteValueBlob` inspect callback +arguments; blob bytes are borrowed and may be null for empty values. Inspect +the type before conversions and consume bytes within the callback, following +SQLite's [value lifetime rules](https://www.sqlite.org/c3ref/value_blob.html). +`SqliteResultDouble` sets the callback result. Keep callback context, argument +pointers and returned user data within their documented lifetimes; a callback +must not close its active database or reset/finalize its active statement. + The complete official SQLite source is a Git source dependency pinned to an exact upstream commit in `ziran.lock`. `ziran fetch --locked` retrieves it and `ziran pkg path sqlite3` locates it. A submodule is unnecessary: @@ -47,11 +74,15 @@ call arbitrary native libraries. ## Test ```sh -make check # C, C++, Go, Rust and Python, source and saved IR +make check # available backends, source and saved IR; native C callbacks make check TARGET=go # one backend ``` -The full check needs a C/C++ compiler, Python, Go and Cargo. It tests +The current toolchain check needs a C compiler, Python and Go. Older toolchains +also provide C++, Rust and Python backends and require their build tools. It tests Unicode text, statement reuse, large integers, binary data, empty BLOBs, floating-point values, NULLs, invalid SQL and database backup. This is a binding for the documented API above, rather than every SQLite extension. +A separate C source/saved callback suite exercises value types, binary bytes, +connection-local data, replacement, deletion, close and real failed-registration +destructor calls, without requiring callback support from Go. diff --git a/src/SQLite.zi b/src/SQLite.zi index 8666b9a..09258ce 100644 --- a/src/SQLite.zi +++ b/src/SQLite.zi @@ -21,6 +21,10 @@ SQLITE_TEXT :: 3; SQLITE_BLOB :: 4; SQLITE_NULL :: 5; +// C callbacks. The caller keeps data alive until SQLite invokes destroy. +SqliteScalar :: #type (context: *void, argc: s32, argv: **void) #c_call; +SqliteDestructor :: #type (data: *void) #c_call; + #scope_file sqlite :: #system_library "sqlite3"; c :: #system_library "c"; @@ -55,6 +59,7 @@ ErrorCodeRaw :: (database: *void) -> s32 #foreign sqlite "sqlite3_errcode"; ErrorMessageRaw :: (database: *void) -> *u8 #foreign sqlite "sqlite3_errmsg"; BusyTimeoutRaw :: (database: *void, milliseconds: s32) -> s32 #foreign sqlite "sqlite3_busy_timeout"; ChangesRaw :: (database: *void) -> s32 #foreign sqlite "sqlite3_changes"; +GetAutocommitRaw :: (database: *void) -> s32 #foreign sqlite "sqlite3_get_autocommit"; BindNullRaw :: (statement: *void, index: s32) -> s32 #foreign sqlite "sqlite3_bind_null"; BindDoubleRaw :: (statement: *void, index: s32, value: float64) -> s32 #foreign sqlite "sqlite3_bind_double"; BindBlobRaw :: (statement: *void, index: s32, value: *u8, @@ -66,6 +71,15 @@ ColumnDoubleRaw :: (statement: *void, column: s32) -> float64 #foreign sqlite "s ColumnTypeRaw :: (statement: *void, column: s32) -> s32 #foreign sqlite "sqlite3_column_type"; ColumnCountRaw :: (statement: *void) -> s32 #foreign sqlite "sqlite3_column_count"; +CreateFunctionRaw :: (database: *void, name: *u8, argc: s32, flags: s32, + data: *void, callback: SqliteScalar, step: *void, final: *void, + destroy: SqliteDestructor) -> s32 #foreign sqlite "sqlite3_create_function_v2"; +UserDataRaw :: (context: *void) -> *void #foreign sqlite "sqlite3_user_data"; +ValueTypeRaw :: (value: *void) -> s32 #foreign sqlite "sqlite3_value_type"; +ValueBytesRaw :: (value: *void) -> s32 #foreign sqlite "sqlite3_value_bytes"; +ValueBlobRaw :: (value: *void) -> *void #foreign sqlite "sqlite3_value_blob"; +ResultDoubleRaw :: (context: *void, value: float64) #foreign sqlite "sqlite3_result_double"; + CopyStringBytes :: (source: string, destination: *u8) { index: s32 = 0 while index < cast(s32)source.count { @@ -206,6 +220,14 @@ SqliteClose :: (database: *void) -> s32 { return CloseRaw(database) } +// True when this connection has no active transaction. A null handle is false. +// As with SQLite itself, the caller must keep the connection alive for the call. +#program_export +SqliteGetAutocommit :: (database: *void) -> bool { + if database == null { return false } + return GetAutocommitRaw(database) != 0 +} + #program_export SqliteBackupInit :: (destination: *void, source: *void) -> *void { name: [5]u8 @@ -296,3 +318,22 @@ SqliteColumnDouble :: (statement: *void, column: s32) -> float64 { return Column SqliteColumnType :: (statement: *void, column: s32) -> s32 { return ColumnTypeRaw(statement, column) } #program_export SqliteColumnCount :: (statement: *void) -> s32 { return ColumnCountRaw(statement) } + +// Direct SQLite lifetime: destroy also runs when registration fails. +#program_export +SqliteCreateFunction :: (database: *void, name: *u8, argc: s32, flags: s32, + data: *void, callback: SqliteScalar, step: *void, final: *void, + destroy: SqliteDestructor) -> s32 { + return CreateFunctionRaw(database, name, argc, flags, data, callback, step, final, destroy) +} +#program_export +SqliteUserData :: (context: *void) -> *void { return UserDataRaw(context) } +#program_export +SqliteValueType :: (value: *void) -> s32 { return ValueTypeRaw(value) } +#program_export +SqliteValueBytes :: (value: *void) -> s32 { return ValueBytesRaw(value) } +// Callback value bytes are borrowed; consume them before the callback returns. +#program_export +SqliteValueBlob :: (value: *void) -> *void { return ValueBlobRaw(value) } +#program_export +SqliteResultDouble :: (context: *void, value: float64) { ResultDoubleRaw(context, value) } diff --git a/tests/sqlite_callback_test.zi b/tests/sqlite_callback_test.zi new file mode 100644 index 0000000..5884584 --- /dev/null +++ b/tests/sqlite_callback_test.zi @@ -0,0 +1,87 @@ +#import "SQLite" + +CallbackState :: struct { bias: float64; calls: s32; destroyed: s32; } + +Observe :: (context: *void, argc: s32, argv: **void) { + state := cast(*CallbackState)SqliteUserData(context) + if state == null || argc != 1 || argv == null { + SqliteResultDouble(context, -10000.0) + return + } + state.calls += 1 + kind := SqliteValueType(argv[0]) + value := state.bias - cast(float64)kind + if kind == SQLITE_BLOB { + count := SqliteValueBytes(argv[0]) + bytes := cast(*u8)SqliteValueBlob(argv[0]) + value = state.bias + index: s32 = 0 + while index < count { + if bytes == null { SqliteResultDouble(context, -10001.0); return } + value += cast(float64)bytes[index] + index += 1 + } + } + SqliteResultDouble(context, value) +} + +Destroy :: (data: *void) { + state := cast(*CallbackState)data + if state != null { state.destroyed += 1 } +} + +Query :: (database: *void, expected: float64) -> bool { + statement: *void = null + if SqlitePrepareText(database, "select observe(x'0100ff'),observe(x''),observe(NULL),observe(7),observe('café')", -1, *statement, null) != SQLITE_OK { return false } + defer SqliteFinalize(statement) + return SqliteStep(statement) == SQLITE_ROW && + SqliteColumnDouble(statement, 0) == expected + 256.0 && + SqliteColumnDouble(statement, 1) == expected && + SqliteColumnDouble(statement, 2) == expected - 5.0 && + SqliteColumnDouble(statement, 3) == expected - 1.0 && + SqliteColumnDouble(statement, 4) == expected - 3.0 +} + +#program_export +main :: () -> s32 { + flags: s32 = SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE + database: *void = null + other: *void = null + if SqliteOpenText(":memory:", *database, flags) != SQLITE_OK || + SqliteOpenText(":memory:", *other, flags) != SQLITE_OK { return 1 } + name: [8]u8 = .{111,98,115,101,114,118,101,0} + first: CallbackState = .{bias=10.0} + second: CallbackState = .{bias=100.0} + replacement: CallbackState = .{bias=20.0} + failed: CallbackState = .{bias=30.0} + final_state: CallbackState = .{bias=40.0} + // SQLITE_UTF8 is SQLite's documented text encoding value 1. + if SqliteCreateFunction(database, *name[0], 1, 1, cast(*void)*first, Observe, null, null, Destroy) != SQLITE_OK || + SqliteCreateFunction(other, *name[0], 1, 1, cast(*void)*second, Observe, null, null, Destroy) != SQLITE_OK { return 2 } + if !Query(database, 10.0) || !Query(other, 100.0) || !Query(database, 10.0) || + first.calls != 10 || second.calls != 5 || first.destroyed != 0 || second.destroyed != 0 { return 3 } + // SQLite invokes the replaced registration's destructor synchronously. + if SqliteCreateFunction(database, *name[0], 1, 1, cast(*void)*replacement, Observe, null, null, Destroy) != SQLITE_OK || + first.destroyed != 1 || replacement.destroyed != 0 || !Query(database, 20.0) || + !Query(other, 100.0) || second.destroyed != 0 { return 4 } + // A 256-byte UTF-8 name is documented to fail with SQLITE_MISUSE. + // Unlike an invalid argc, it does not invoke undefined SQLite behavior. + too_long: [257]u8 + index: s32 = 0 + while index < 256 { too_long[index] = cast(u8)120; index += 1 } + too_long[256] = cast(u8)0 + if SqliteCreateFunction(database, *too_long[0], 1, 1, cast(*void)*failed, Observe, null, null, Destroy) != SQLITE_MISUSE || + failed.destroyed != 1 || !Query(database, 20.0) { return 5 } + if SqliteClose(database) != SQLITE_OK || first.destroyed != 1 || + replacement.destroyed != 1 || failed.destroyed != 1 || second.destroyed != 0 { return 6 } + // All three null function callbacks delete the function, releasing data. + empty: *void = null + no_scalar := cast(SqliteScalar)empty + no_destructor := cast(SqliteDestructor)empty + if SqliteCreateFunction(other, *name[0], 1, 1, null, no_scalar, null, null, no_destructor) != SQLITE_OK || second.destroyed != 1 { return 7 } + if SqliteCreateFunction(other, *name[0], 1, 1, cast(*void)*final_state, Observe, null, null, Destroy) != SQLITE_OK || + !Query(other, 40.0) || final_state.destroyed != 0 { return 8 } + if SqliteClose(other) != SQLITE_OK || second.destroyed != 1 || final_state.destroyed != 1 || + first.destroyed != 1 || replacement.destroyed != 1 || failed.destroyed != 1 { return 9 } + return 0 +} diff --git a/tests/sqlite_test.zi b/tests/sqlite_test.zi index 10c85e3..7230c4c 100644 --- a/tests/sqlite_test.zi +++ b/tests/sqlite_test.zi @@ -23,6 +23,11 @@ main :: () -> s32 { if SqliteOpen(*path[0], *database, flags) != SQLITE_OK || database == null { return 1 } + if SqliteGetAutocommit(null) || !SqliteGetAutocommit(database) { return 30 } + if SqliteExecText(database, "BEGIN") != SQLITE_OK || SqliteGetAutocommit(database) { return 31 } + if SqliteExecText(database, "COMMIT") != SQLITE_OK || !SqliteGetAutocommit(database) { return 32 } + if SqliteExecText(database, "BEGIN IMMEDIATE") != SQLITE_OK || SqliteGetAutocommit(database) { return 33 } + if SqliteExecText(database, "ROLLBACK") != SQLITE_OK || !SqliteGetAutocommit(database) { return 34 } if SqliteExecText(database, "create table votes (voter text not null, score integer not null)") != SQLITE_OK { return 2 } diff --git a/ziran.lock b/ziran.lock index b23b5a0..32880a1 100644 --- a/ziran.lock +++ b/ziran.lock @@ -4,7 +4,7 @@ { "commit": "b09c88c14082339b66c7b7158d609a771e64ca69", "dependencies": {}, - "id": "pbeb32cf8d2e03b02", + "id": "p193f493072e2b40b", "name": "sqlite3", "ref": "version-3.53.4", "source": true, @@ -12,11 +12,12 @@ } ], "root": { - "dependencies": {"sqlite3": "pbeb32cf8d2e03b02"}, + "dependencies": {"sqlite3": "p193f493072e2b40b"}, + "dependency_refs": {"sqlite3": "version-3.53.4"}, "name": "SQLite" }, "toolchain": { - "commit": "384026ebf670ee5cda738bbbb3dfa3825310290f", + "commit": "1d616e3a1761dd4e4a1c5a960c165d30ba16da29", "ref": "master", "url": "https://github.com/ziranlang/ziran.git" }