SQL: don't label results, send tuples to a continuation
This commit is contained in:
parent
ff1f1ac44b
commit
2d3189288f
|
@ -148,9 +148,9 @@ let ?sqlspace = dataspace
|
|||
|
||||
let ?tuplespace = dataspace
|
||||
|
||||
$sqlspace <query example-row "SELECT id, name FROM stuff" $tuplespace>
|
||||
$sqlspace <query "SELECT id, name FROM stuff" $tuplespace>
|
||||
|
||||
$tuplespace ? <example-row ?id ?name> [
|
||||
$tuplespace ? [?id ?name] [
|
||||
$log ! <log "-" { row: <example-row $id $name> }>
|
||||
]
|
||||
```
|
||||
|
@ -197,9 +197,9 @@ let ?sqlspace = dataspace
|
|||
|
||||
let ?tuplespace = dataspace
|
||||
|
||||
$sqlspace <query example-row "SELECT id, name FROM stuff" $tuplespace>
|
||||
$sqlspace <query "SELECT id, name FROM stuff" $tuplespace>
|
||||
|
||||
$tuplespace ? <example-row ?id ?name> [
|
||||
$tuplespace ? [?id ?name] [
|
||||
$log ! <log "-" { row: <example-row $id $name> }>
|
||||
]
|
||||
```
|
||||
|
|
2
sql.prs
2
sql.prs
|
@ -2,4 +2,4 @@ version 1 .
|
|||
|
||||
# When asserted the actor reponds to @target rows as records
|
||||
# of the given label and row columns as record fields.
|
||||
Query = <query @label any @statement string @target #!any> .
|
||||
Query = <query @statement string @target #!any> .
|
||||
|
|
|
@ -4,7 +4,6 @@ import
|
|||
|
||||
type
|
||||
Query* {.preservesRecord: "query".} = object
|
||||
`label`*: Value
|
||||
`statement`*: string
|
||||
`target`* {.preservesEmbedded.}: Value
|
||||
|
||||
|
|
|
@ -103,20 +103,20 @@ proc spawnPostgreActor*(turn: var Turn; root: Cap): Actor {.discardable.} =
|
|||
statusHandle = publish(turn, ds,
|
||||
initRecord("status", toSymbol($status), msg.toPreserves))
|
||||
if status == CONNECTION_OK:
|
||||
during(turn, ds, ?:Query) do (label: Value, statement: string, target: Cap):
|
||||
during(turn, ds, ?:Query) do (statement: string, target: Cap):
|
||||
var res = PQexec(conn, statement)
|
||||
var st = PQresultStatus(res)
|
||||
discard publish(turn, ds, toRecord(
|
||||
label, toSymbol($PQresStatus(st)), $PQresultErrorMessage(res)))
|
||||
"error", statement, toSymbol($PQresStatus(st)), $PQresultErrorMessage(res)))
|
||||
if st == PGRES_TUPLES_OK or st == PGRES_SINGLE_TUPLE:
|
||||
let tuples = PQntuples(res)
|
||||
let fields = PQnfields(res)
|
||||
if tuples > 0 and fields > 0:
|
||||
for r in 0..<tuples:
|
||||
var rec = initRecord(label, fields)
|
||||
var tupl = initSequence(fields)
|
||||
for f in 0..<fields:
|
||||
rec[f] = toPreserves($PQgetvalue(res, r, f))
|
||||
discard publish(turn, target, rec)
|
||||
tupl[f] = toPreserves($PQgetvalue(res, r, f))
|
||||
discard publish(turn, target, tupl)
|
||||
PQclear(res)
|
||||
else:
|
||||
stderr.writeLine "refusing to do anything when status is ", status
|
||||
|
|
|
@ -74,9 +74,9 @@ proc extractValue(stmt: Stmt; col: cint): Value =
|
|||
else:
|
||||
result = initRecord("null")
|
||||
|
||||
proc extractRecord(stmt: Stmt; label: Value, arity: cint): Value =
|
||||
result = initRecord(label, arity)
|
||||
for col in 0..<arity: result.record[col] = extractValue(stmt, col)
|
||||
proc extractTuple(stmt: Stmt; arity: cint): Value =
|
||||
result = initSequence(arity)
|
||||
for col in 0..<arity: result[col] = extractValue(stmt, col)
|
||||
|
||||
proc spawnSqliteActor*(turn: var Turn; root: Cap): Actor {.discardable.} =
|
||||
spawn("sqlite-actor", turn) do (turn: var Turn):
|
||||
|
@ -85,7 +85,7 @@ proc spawnSqliteActor*(turn: var Turn; root: Cap): Actor {.discardable.} =
|
|||
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, target: Cap):
|
||||
during(turn, ds, ?:Query) do (statement: string, target: Cap):
|
||||
var stmt: Stmt
|
||||
if prepare_v2(db, statement, statement.len.cint, addr stmt, nil) != SQLITE_OK:
|
||||
logError(db, statement)
|
||||
|
@ -94,7 +94,7 @@ proc spawnSqliteActor*(turn: var Turn; root: Cap): Actor {.discardable.} =
|
|||
let arity = column_count(stmt)
|
||||
var res = step(stmt)
|
||||
while res == SQLITE_ROW:
|
||||
var rec = extractRecord(stmt, label, arity)
|
||||
var rec = extractTuple(stmt, arity)
|
||||
discard publish(turn, target, rec)
|
||||
res = step(stmt)
|
||||
assert res != 100
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
# Package
|
||||
|
||||
version = "20240116"
|
||||
version = "20240118"
|
||||
author = "Emery Hemingway"
|
||||
description = "Utilites for Syndicated Actors and Synit"
|
||||
license = "unlicense"
|
||||
|
|
Loading…
Reference in New Issue