SqliteBusyHandler and SqliteProgressHandler register connection-local C callbacks, and SqliteColumnName returns a prepared query's column label. SQLITE_INTERRUPT names the code an interrupted statement reports. The callback test covers both handlers and the query test covers aliased column names.
157 lines
7.0 KiB
Ziran
157 lines
7.0 KiB
Ziran
#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
|
|
}
|
|
|
|
CancelAfter :: (data: *void) -> s32 {
|
|
remaining := cast(*s32)data
|
|
<<remaining -= 1
|
|
return ifx <<remaining <= 0 then 1 else 0
|
|
}
|
|
|
|
CheckProgress :: (database: *void, other: *void) -> bool {
|
|
query := "WITH RECURSIVE n(x) AS (VALUES(1) UNION ALL SELECT x+1 FROM n WHERE x<100) SELECT sum(x) FROM n"
|
|
remaining: s32 = 3
|
|
SqliteProgressHandler(database, 1, CancelAfter, cast(*void)*remaining)
|
|
isolated := SqliteExecText(other, query) == SQLITE_OK && remaining == 3
|
|
interrupted := SqliteExecText(database, query) == SQLITE_INTERRUPT && remaining == 0
|
|
SqliteProgressHandler(database, 0, CancelAfter, null)
|
|
return isolated && interrupted && SqliteExecText(database, query) == SQLITE_OK && remaining == 0
|
|
}
|
|
|
|
#import "std/vec"
|
|
|
|
libc :: #system_library "c";
|
|
TemporaryFile :: (path: *u8) -> s32 #foreign libc "mkstemp";
|
|
CloseDescriptor :: (file: s32) -> s32 #foreign libc "close";
|
|
RemoveFile :: (path: *u8) -> s32 #foreign libc "remove";
|
|
|
|
BusyState :: struct {
|
|
calls: s32
|
|
cancelled: bool
|
|
}
|
|
|
|
RetryBusy :: (data: *void, retries: s32) -> s32 {
|
|
state := cast(*BusyState)data
|
|
if state.calls != retries { return 0 }
|
|
state.calls += 1
|
|
return ifx !state.cancelled && retries < 2 then 1 else 0
|
|
}
|
|
|
|
CheckBusy :: () -> bool {
|
|
pattern := "build/sqlite-busy-XXXXXX"
|
|
path: [25]u8
|
|
for index: cast(s64)0..pattern.count - 1 { path[index] = pattern[index] }
|
|
file := TemporaryFile(*path[0])
|
|
if file < 0 { return false }
|
|
defer { unused RemoveFile(*path[0]) }
|
|
if CloseDescriptor(file) != 0 { return false }
|
|
name := TextView(path[:pattern.count])
|
|
first: *void = null
|
|
second: *void = null
|
|
flags: s32 = SQLITE_OPEN_READWRITE
|
|
defer {
|
|
if second != null { unused SqliteClose(second) }
|
|
if first != null { unused SqliteClose(first) }
|
|
}
|
|
if SqliteOpenText(name, *first, flags) != SQLITE_OK ||
|
|
SqliteOpenText(name, *second, flags) != SQLITE_OK { return false }
|
|
state: BusyState
|
|
if SqliteExecText(first, "BEGIN IMMEDIATE") != SQLITE_OK ||
|
|
SqliteBusyHandler(second, RetryBusy, cast(*void)*state) != SQLITE_OK ||
|
|
SqliteExecText(second, "BEGIN IMMEDIATE") != SQLITE_BUSY || state.calls != 3 { return false }
|
|
state.calls = 0
|
|
state.cancelled = true
|
|
if SqliteExecText(second, "BEGIN IMMEDIATE") != SQLITE_BUSY || state.calls != 1 { return false }
|
|
no_callback := cast(SqliteBusy)cast(*void)null
|
|
if SqliteBusyHandler(second, no_callback, null) != SQLITE_OK ||
|
|
SqliteExecText(second, "BEGIN IMMEDIATE") != SQLITE_BUSY || state.calls != 1 { return false }
|
|
return SqliteExecText(first, "ROLLBACK") == SQLITE_OK &&
|
|
SqliteExecText(second, "BEGIN IMMEDIATE; ROLLBACK") == SQLITE_OK
|
|
}
|
|
|
|
#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 !CheckProgress(database, other) { return 10 }
|
|
if !CheckBusy() { return 11 }
|
|
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
|
|
}
|