Assert each row as a record

This commit is contained in:
Emery Hemingway 2023-05-26 23:59:01 +01:00
parent 43e0a069d3
commit fb2ad6903a
5 changed files with 45 additions and 15 deletions

28
README.md Normal file
View File

@ -0,0 +1,28 @@
# sqlite_actor
Syndicate actor for accessing SQLite databases.
## Example configuration
```
? <example-dataspace ?ds> [
$ds <query example-row "SELECT id, name FROM stuff">
$ds ? <example-row ?id ?name> [
$log ! <log "-" { row: <example-row $id $name> }>
]
<require-service <daemon sqlite_actor>>
? <service-object <daemon sqlite_actor> ?cap> [
$cap {
dataspace: $ds
database: "/var/db/example.db"
}
]
<daemon sqlite_actor {
argv: [ "/usr/local/bin/sqlite_actor" ]
protocol: application/syndicate
}>
]
```

View File

@ -1,4 +1,6 @@
version 1 .
Query = <sql @statement string @rows [Row ...]> .
Row = [string ...] .
; When asserted the actor reponds with
; rows as records of the given label and
; row columns as record fields.
Query = <query @label any @statement string> .

View File

@ -1,4 +1,4 @@
version = "20230526"
version = "20230527"
author = "Emery Hemingway"
description = "Syndicate Actor for accessing SQLite databases"
license = "Unlicense"

View File

@ -3,13 +3,12 @@ import
preserves
type
Query* {.preservesRecord: "sql".} = object
Query* {.preservesRecord: "query".} = object
`label`*: Preserve[void]
`statement`*: string
`rows`*: seq[Row]
Row* = seq[string]
proc `$`*(x: Query | Row): string =
proc `$`*(x: Query): string =
`$`(toPreserve(x))
proc encode*(x: Query | Row): seq[byte] =
proc encode*(x: Query): seq[byte] =
encode(toPreserve(x))

View File

@ -1,26 +1,27 @@
# SPDX-FileCopyrightText: ☭ Emery Hemingway
# SPDX-License-Identifier: Unlicense
# {.passC: staticExec("pkg-config --cflags sqlcipher").}
# {.passL: staticExec("pkg-config --libs sqlcipher").}
{.passC: staticExec("pkg-config --cflags sqlcipher").}
{.passL: staticExec("pkg-config --libs sqlcipher").}
import std/db_sqlite
import preserves, syndicate
import ./sqlite
type Value = Preserve[void]
type Args {.preservesDictionary.}= object
database: string
dataspace: Ref
runActor("main") do (root: Ref; turn: var Turn):
connectStdio(root, turn)
let queryPat = ?Observe(pattern: !Query) ?? {0: grabLit()}
during(turn, root, ?Args) do (path: string, ds: Ref):
let db = open(path, "", "", "")
during(turn, ds, queryPat) do (statement: string):
var query = Query(statement: statement)
during(turn, ds, ?Query) do (label: Value, statement: string):
for row in rows(db, sql statement):
add(query.rows, row)
discard publish(turn, ds, query)
var rec = initRecord(label, len(row))
for col, val in row: rec[col] = val.toPreserve
discard publish(turn, ds, rec)
do:
close(db)