syndesizer/sqlite_actor: move out compile conditional
This commit is contained in:
parent
29b19c711c
commit
119d89ff1c
|
@ -7,19 +7,22 @@ import syndicate, syndicate/relays, syndicate/actors/timers
|
||||||
|
|
||||||
const
|
const
|
||||||
withPostgre* {.booldefine.}: bool = true
|
withPostgre* {.booldefine.}: bool = true
|
||||||
|
withSqlite* {.booldefine.}: bool = true
|
||||||
|
|
||||||
import ./syndesizer/[
|
import ./syndesizer/[
|
||||||
cache_actor,
|
cache_actor,
|
||||||
file_system_usage,
|
file_system_usage,
|
||||||
json_socket_translator,
|
json_socket_translator,
|
||||||
json_translator,
|
json_translator,
|
||||||
sqlite_actor,
|
|
||||||
webhooks,
|
webhooks,
|
||||||
websockets]
|
websockets]
|
||||||
|
|
||||||
when withPostgre:
|
when withPostgre:
|
||||||
import ./syndesizer/postgre_actor
|
import ./syndesizer/postgre_actor
|
||||||
|
|
||||||
|
when withSqlite:
|
||||||
|
import ./syndesizer/sqlite_actor
|
||||||
|
|
||||||
runActor("syndesizer") do (turn: var Turn; root: Cap):
|
runActor("syndesizer") do (turn: var Turn; root: Cap):
|
||||||
connectStdio(turn, root)
|
connectStdio(turn, root)
|
||||||
discard spawnTimers(turn, root)
|
discard spawnTimers(turn, root)
|
||||||
|
|
|
@ -1,114 +1,111 @@
|
||||||
# SPDX-FileCopyrightText: ☭ Emery Hemingway
|
# SPDX-FileCopyrightText: ☭ Emery Hemingway
|
||||||
# SPDX-License-Identifier: Unlicense
|
# SPDX-License-Identifier: Unlicense
|
||||||
|
|
||||||
const withSqlite* {.booldefine.}: bool = true
|
import preserves, syndicate, syndicate/relays
|
||||||
|
import ../schema/[config, sql]
|
||||||
|
|
||||||
when withSqlite:
|
# Avoid Sqlite3 from the standard library because it is
|
||||||
import preserves, syndicate, syndicate/relays
|
# only held together by wishful thinking and dlload.
|
||||||
import ../schema/[config, sql]
|
|
||||||
|
|
||||||
# Avoid Sqlite3 from the standard library because it is
|
{.passC: staticExec("pkg-config --cflags sqlite3").}
|
||||||
# only held together by wishful thinking and dlload.
|
{.passL: staticExec("pkg-config --libs sqlite3").}
|
||||||
|
|
||||||
{.passC: staticExec("pkg-config --cflags sqlite3").}
|
{.pragma: sqlite3h, header: "sqlite3.h".}
|
||||||
{.passL: staticExec("pkg-config --libs sqlite3").}
|
|
||||||
|
|
||||||
{.pragma: sqlite3h, header: "sqlite3.h".}
|
var
|
||||||
|
SQLITE_VERSION_NUMBER {.importc, sqlite3h.}: cint
|
||||||
|
SQLITE_OK {.importc, sqlite3h.}: cint
|
||||||
|
SQLITE_ROW {.importc, sqlite3h.}: cint
|
||||||
|
SQLITE_DONE {.importc, sqlite3h.}: cint
|
||||||
|
SQLITE_OPEN_READONLY {.importc, sqlite3h.}: cint
|
||||||
|
|
||||||
var
|
const
|
||||||
SQLITE_VERSION_NUMBER {.importc, sqlite3h.}: cint
|
SQLITE_INTEGER = 1
|
||||||
SQLITE_OK {.importc, sqlite3h.}: cint
|
SQLITE_FLOAT = 2
|
||||||
SQLITE_ROW {.importc, sqlite3h.}: cint
|
SQLITE_TEXT = 3
|
||||||
SQLITE_DONE {.importc, sqlite3h.}: cint
|
SQLITE_BLOB = 4
|
||||||
SQLITE_OPEN_READONLY {.importc, sqlite3h.}: cint
|
# SQLITE_NULL = 5
|
||||||
|
|
||||||
const
|
type
|
||||||
SQLITE_INTEGER = 1
|
Sqlite3 {.importc: "sqlite3", sqlite3h.} = distinct pointer
|
||||||
SQLITE_FLOAT = 2
|
Stmt {.importc: "sqlite3_stmt", sqlite3h.} = distinct pointer
|
||||||
SQLITE_TEXT = 3
|
|
||||||
SQLITE_BLOB = 4
|
|
||||||
# SQLITE_NULL = 5
|
|
||||||
|
|
||||||
type
|
{.pragma: importSqlite3, importc: "sqlite3_$1", sqlite3h.}
|
||||||
Sqlite3 {.importc: "sqlite3", sqlite3h.} = distinct pointer
|
|
||||||
Stmt {.importc: "sqlite3_stmt", sqlite3h.} = distinct pointer
|
|
||||||
|
|
||||||
{.pragma: importSqlite3, importc: "sqlite3_$1", sqlite3h.}
|
proc libversion_number: cint {.importSqlite3.}
|
||||||
|
|
||||||
proc libversion_number: cint {.importSqlite3.}
|
proc open_v2(filename: cstring; ppDb: ptr Sqlite3; flags: cint; zVfs: cstring): cint {.importSqlite3.}
|
||||||
|
proc close(ds: Sqlite3): int32 {.discardable, importSqlite3.}
|
||||||
|
|
||||||
proc open_v2(filename: cstring; ppDb: ptr Sqlite3; flags: cint; zVfs: cstring): cint {.importSqlite3.}
|
proc errmsg(db: Sqlite3): cstring {.importSqlite3.}
|
||||||
proc close(ds: Sqlite3): int32 {.discardable, importSqlite3.}
|
|
||||||
|
|
||||||
proc errmsg(db: Sqlite3): cstring {.importSqlite3.}
|
proc prepare_v2(db: Sqlite3; zSql: cstring, nByte: cint; ppStmt: ptr Stmt; pzTail: ptr cstring): cint {.importSqlite3.}
|
||||||
|
|
||||||
proc prepare_v2(db: Sqlite3; zSql: cstring, nByte: cint; ppStmt: ptr Stmt; pzTail: ptr cstring): cint {.importSqlite3.}
|
proc step(para1: Stmt): cint {.importSqlite3.}
|
||||||
|
|
||||||
proc step(para1: Stmt): cint {.importSqlite3.}
|
proc column_count(stmt: Stmt): int32 {.importSqlite3.}
|
||||||
|
proc column_blob(stmt: Stmt; col: cint): pointer {.importSqlite3.}
|
||||||
|
proc column_bytes(stmt: Stmt; col: cint): cint {.importSqlite3.}
|
||||||
|
proc column_double(stmt: Stmt; col: cint): float64 {.importSqlite3.}
|
||||||
|
proc column_int64(stmt: Stmt; col: cint): int64 {.importSqlite3.}
|
||||||
|
proc column_text(stmt: Stmt; col: cint): cstring {.importSqlite3.}
|
||||||
|
proc column_type(stmt: Stmt; col: cint): cint {.importSqlite3.}
|
||||||
|
proc finalize(stmt: Stmt): cint {.importSqlite3.}
|
||||||
|
|
||||||
proc column_count(stmt: Stmt): int32 {.importSqlite3.}
|
doAssert libversion_number() == SQLITE_VERSION_NUMBER
|
||||||
proc column_blob(stmt: Stmt; col: cint): pointer {.importSqlite3.}
|
|
||||||
proc column_bytes(stmt: Stmt; col: cint): cint {.importSqlite3.}
|
|
||||||
proc column_double(stmt: Stmt; col: cint): float64 {.importSqlite3.}
|
|
||||||
proc column_int64(stmt: Stmt; col: cint): int64 {.importSqlite3.}
|
|
||||||
proc column_text(stmt: Stmt; col: cint): cstring {.importSqlite3.}
|
|
||||||
proc column_type(stmt: Stmt; col: cint): cint {.importSqlite3.}
|
|
||||||
proc finalize(stmt: Stmt): cint {.importSqlite3.}
|
|
||||||
|
|
||||||
doAssert libversion_number() == SQLITE_VERSION_NUMBER
|
proc logError(db: Sqlite3; context: string) =
|
||||||
|
writeLine(stderr, errmsg(db), ": ", context)
|
||||||
|
|
||||||
proc logError(db: Sqlite3; context: string) =
|
proc extractValue(stmt: Stmt; col: cint): Value =
|
||||||
writeLine(stderr, errmsg(db), ": ", context)
|
case column_type(stmt, col)
|
||||||
|
of SQLITE_INTEGER:
|
||||||
|
result = toPreserves(column_int64(stmt, col))
|
||||||
|
of SQLITE_FLOAT:
|
||||||
|
result = toPreserves(column_double(stmt, col))
|
||||||
|
of SQLITE_TEXT:
|
||||||
|
result = Value(kind: pkString, string: newString(column_bytes(stmt, col)))
|
||||||
|
if result.string.len > 0:
|
||||||
|
copyMem(addr result.string[0], column_text(stmt, col), result.string.len)
|
||||||
|
of SQLITE_BLOB:
|
||||||
|
result = Value(kind: pkByteString, bytes: newSeq[byte](column_bytes(stmt, col)))
|
||||||
|
if result.bytes.len > 0:
|
||||||
|
copyMem(addr result.bytes[0], column_blob(stmt, col), result.bytes.len)
|
||||||
|
else:
|
||||||
|
result = initRecord("null")
|
||||||
|
|
||||||
proc extractValue(stmt: Stmt; col: cint): Value =
|
proc extractRecord(stmt: Stmt; label: Value, arity: cint): Value =
|
||||||
case column_type(stmt, col)
|
result = initRecord(label, arity)
|
||||||
of SQLITE_INTEGER:
|
for col in 0..<arity: result.record[col] = extractValue(stmt, col)
|
||||||
result = toPreserve(column_int64(stmt, col))
|
|
||||||
of SQLITE_FLOAT:
|
|
||||||
result = toPreserve(column_double(stmt, col))
|
|
||||||
of SQLITE_TEXT:
|
|
||||||
result = Value(kind: pkString, string: newString(column_bytes(stmt, col)))
|
|
||||||
if result.string.len > 0:
|
|
||||||
copyMem(addr result.string[0], column_text(stmt, col), result.string.len)
|
|
||||||
of SQLITE_BLOB:
|
|
||||||
result = Value(kind: pkByteString, bytes: newSeq[byte](column_bytes(stmt, col)))
|
|
||||||
if result.bytes.len > 0:
|
|
||||||
copyMem(addr result.bytes[0], column_blob(stmt, col), result.bytes.len)
|
|
||||||
else:
|
|
||||||
result = initRecord("null")
|
|
||||||
|
|
||||||
proc extractRecord(stmt: Stmt; label: Value, arity: cint): Value =
|
proc spawnSqliteActor*(turn: var Turn; root: Cap): Actor {.discardable.} =
|
||||||
result = initRecord(label, arity)
|
spawn("sqlite-actor", turn) do (turn: var Turn):
|
||||||
for col in 0..<arity: result.record[col] = extractValue(stmt, col)
|
during(turn, root, ?:SqliteArguments) do (path: string, ds: Cap):
|
||||||
|
var db: Sqlite3
|
||||||
|
if open_v2(path, addr db, SQLITE_OPEN_READONLY, nil) != SQLITE_OK:
|
||||||
|
logError(db, path)
|
||||||
|
else:
|
||||||
|
during(turn, ds, ?:Query) do (label: Value, statement: string):
|
||||||
|
var stmt: Stmt
|
||||||
|
if prepare_v2(db, statement, statement.len.cint, addr stmt, nil) != SQLITE_OK:
|
||||||
|
logError(db, statement)
|
||||||
|
else:
|
||||||
|
try:
|
||||||
|
let arity = column_count(stmt)
|
||||||
|
var res = step(stmt)
|
||||||
|
while res == SQLITE_ROW:
|
||||||
|
var rec = extractRecord(stmt, label, arity)
|
||||||
|
discard publish(turn, ds, rec)
|
||||||
|
res = step(stmt)
|
||||||
|
assert res != 100
|
||||||
|
if res != SQLITE_DONE:
|
||||||
|
logError(db, statement)
|
||||||
|
finally:
|
||||||
|
if finalize(stmt) != SQLITE_OK: logError(db, statement)
|
||||||
|
do:
|
||||||
|
close(db)
|
||||||
|
|
||||||
proc spawnSqliteActor*(turn: var Turn; root: Cap): Actor {.discardable.} =
|
when isMainModule:
|
||||||
spawn("sqlite-actor", turn) do (turn: var Turn):
|
runActor("main") do (turn: var Turn; root: Cap):
|
||||||
during(turn, root, ?:SqliteArguments) do (path: string, ds: Cap):
|
connectStdio(turn, root)
|
||||||
var db: Sqlite3
|
spawnSqliteActor(turn, root)
|
||||||
if open_v2(path, addr db, SQLITE_OPEN_READONLY, nil) != SQLITE_OK:
|
|
||||||
logError(db, path)
|
|
||||||
else:
|
|
||||||
during(turn, ds, ?:Query) do (label: Value, statement: string):
|
|
||||||
var stmt: Stmt
|
|
||||||
if prepare_v2(db, statement, statement.len.cint, addr stmt, nil) != SQLITE_OK:
|
|
||||||
logError(db, statement)
|
|
||||||
else:
|
|
||||||
try:
|
|
||||||
let arity = column_count(stmt)
|
|
||||||
var res = step(stmt)
|
|
||||||
while res == SQLITE_ROW:
|
|
||||||
var rec = extractRecord(stmt, label, arity)
|
|
||||||
discard publish(turn, ds, rec)
|
|
||||||
res = step(stmt)
|
|
||||||
assert res != 100
|
|
||||||
if res != SQLITE_DONE:
|
|
||||||
logError(db, statement)
|
|
||||||
finally:
|
|
||||||
if finalize(stmt) != SQLITE_OK: logError(db, statement)
|
|
||||||
do:
|
|
||||||
close(db)
|
|
||||||
|
|
||||||
when isMainModule:
|
|
||||||
runActor("main") do (turn: var Turn; root: Cap):
|
|
||||||
connectStdio(turn, root)
|
|
||||||
spawnSqliteActor(turn, root)
|
|
||||||
|
|
Loading…
Reference in New Issue