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.
123 lines
6.4 KiB
Ziran
123 lines
6.4 KiB
Ziran
#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 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
|
|
}
|
|
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 SqliteLastInsertRowid(database) != 2 {
|
|
return 16
|
|
}
|
|
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
|
|
}
|
|
if SqliteErrorCode(database) == SQLITE_OK || SqliteErrorMessage(database) == null ||
|
|
SqliteVersionNumber() < 3000000 || SqliteBusyTimeout(database, 100) != SQLITE_OK { return 17 }
|
|
if SqliteExecText(database, "create table binary_values (data blob, value real, absent text)") != SQLITE_OK { return 18 }
|
|
if SqlitePrepareText(database, "insert into binary_values values (?, ?, ?)", -1, *statement, null) != SQLITE_OK { return 19 }
|
|
blob: [3]u8 = .{1, 0, 255}
|
|
if SqliteBindBlob(statement, 1, blob[:]) != SQLITE_OK ||
|
|
SqliteBindDouble(statement, 2, 1.25) != SQLITE_OK ||
|
|
SqliteBindNull(statement, 3) != SQLITE_OK { return 20 }
|
|
// Bind copied the original binary data.
|
|
blob[0] = cast(u8)99
|
|
if SqliteStep(statement) != SQLITE_DONE || SqliteChanges(database) != 1 { return 21 }
|
|
if SqliteReset(statement) != SQLITE_OK ||
|
|
SqliteBindBlob(statement, 1, blob[0:0]) != SQLITE_OK ||
|
|
SqliteStep(statement) != SQLITE_DONE || SqliteFinalize(statement) != SQLITE_OK { return 22 }
|
|
if SqlitePrepareText(database, "select data, value as score, absent from binary_values order by rowid", -1, *statement, null) != SQLITE_OK { return 23 }
|
|
if SqliteStep(statement) != SQLITE_ROW || SqliteColumnCount(statement) != 3 ||
|
|
SqliteColumnType(statement, 0) != SQLITE_BLOB || SqliteColumnBytes(statement, 0) != 3 ||
|
|
SqliteColumnDouble(statement, 1) != 1.25 || SqliteColumnType(statement, 2) != SQLITE_NULL { return 24 }
|
|
if !Same(SqliteColumnName(statement, 0), "data") || !Same(SqliteColumnName(statement, 1), "score") ||
|
|
SqliteColumnName(statement, 3) != null { return 35 }
|
|
bytes := SqliteColumnBlob(statement, 0)
|
|
if bytes == null || bytes[0] != 1 || bytes[1] != 0 || bytes[2] != 255 { return 25 }
|
|
if SqliteStep(statement) != SQLITE_ROW || SqliteColumnType(statement, 0) != SQLITE_BLOB ||
|
|
SqliteColumnBytes(statement, 0) != 0 || SqliteFinalize(statement) != SQLITE_OK { return 26 }
|
|
copy: *void = null
|
|
if SqliteOpenText(":memory:", *copy, flags) != SQLITE_OK { return 27 }
|
|
backup := SqliteBackupInit(copy, database)
|
|
if backup == null || SqliteBackupStep(backup, -1) != SQLITE_DONE ||
|
|
SqliteBackupFinish(backup) != SQLITE_OK { return 28 }
|
|
if SqlitePrepareText(copy, "select count(*) from binary_values", -1, *statement, null) != SQLITE_OK ||
|
|
SqliteStep(statement) != SQLITE_ROW || SqliteColumnInt(statement, 0) != 2 ||
|
|
SqliteFinalize(statement) != SQLITE_OK || SqliteClose(copy) != SQLITE_OK { return 29 }
|
|
// 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
|
|
}
|