WiP! Postgre actor
This commit is contained in:
parent
651efc1e6b
commit
7f481f9ff6
|
@ -22,6 +22,12 @@ JsonSocketTranslatorArguments = <json-socket-translator {
|
|||
socket: string
|
||||
}>.
|
||||
|
||||
PostgreArguments = <postgre {
|
||||
connection: [PostgreConnectionParameter ...]
|
||||
dataspace: #!any
|
||||
}>.
|
||||
PostgreConnectionParameter = [@key string @val string].
|
||||
|
||||
SqliteArguments = <sqlite {
|
||||
database: string
|
||||
dataspace: #!any
|
||||
|
|
|
@ -3,5 +3,5 @@
|
|||
pkgs.buildNimPackage {
|
||||
name = "dummy";
|
||||
propagatedNativeBuildInputs = [ pkgs.pkg-config ];
|
||||
propagatedBuildInputs = [ pkgs.sqlite ];
|
||||
propagatedBuildInputs = [ pkgs.postgresql pkgs.sqlite ];
|
||||
}
|
||||
|
|
|
@ -54,6 +54,17 @@ type
|
|||
CacheArguments* {.preservesRecord: "cache".} = object
|
||||
`field0`*: CacheArgumentsField0
|
||||
|
||||
PostgreConnectionParameter* {.preservesTuple.} = object
|
||||
`key`*: string
|
||||
`val`*: string
|
||||
|
||||
PostgreArgumentsField0* {.preservesDictionary.} = object
|
||||
`connection`*: seq[PostgreConnectionParameter]
|
||||
`dataspace`* {.preservesEmbedded.}: EmbeddedRef
|
||||
|
||||
PostgreArguments* {.preservesRecord: "postgre".} = object
|
||||
`field0`*: PostgreArgumentsField0
|
||||
|
||||
Tcp* {.preservesRecord: "tcp".} = object
|
||||
`host`*: string
|
||||
`port`*: BiggestInt
|
||||
|
@ -65,6 +76,8 @@ proc `$`*(x: WebsocketArguments | JsonTranslatorArguments |
|
|||
FileSystemUsageArguments |
|
||||
SqliteArguments |
|
||||
CacheArguments |
|
||||
PostgreConnectionParameter |
|
||||
PostgreArguments |
|
||||
Tcp): string =
|
||||
`$`(toPreserves(x))
|
||||
|
||||
|
@ -75,5 +88,7 @@ proc encode*(x: WebsocketArguments | JsonTranslatorArguments |
|
|||
FileSystemUsageArguments |
|
||||
SqliteArguments |
|
||||
CacheArguments |
|
||||
PostgreConnectionParameter |
|
||||
PostgreArguments |
|
||||
Tcp): seq[byte] =
|
||||
encode(toPreserves(x))
|
||||
|
|
|
@ -5,6 +5,9 @@
|
|||
|
||||
import syndicate, syndicate/relays, syndicate/actors/timers
|
||||
|
||||
const
|
||||
withPostgre* {.booldefine.}: bool = true
|
||||
|
||||
import ./syndesizer/[
|
||||
cache_actor,
|
||||
file_system_usage,
|
||||
|
@ -14,6 +17,9 @@ import ./syndesizer/[
|
|||
webhooks,
|
||||
websockets]
|
||||
|
||||
when withPostgre:
|
||||
import ./syndesizer/postgre_actor
|
||||
|
||||
runActor("syndesizer") do (turn: var Turn; root: Cap):
|
||||
connectStdio(turn, root)
|
||||
discard spawnTimers(turn, root)
|
||||
|
@ -23,5 +29,7 @@ runActor("syndesizer") do (turn: var Turn; root: Cap):
|
|||
discard spawnJsonStdioTranslator(turn, root)
|
||||
discard spawnWebhookActor(turn, root)
|
||||
discard spawnWebsocketActor(turn, root)
|
||||
when withPostgre:
|
||||
discard spawnPostgreActor(turn, root)
|
||||
when withSqlite:
|
||||
discard spawnSqliteActor(turn, root)
|
||||
|
|
|
@ -0,0 +1,83 @@
|
|||
# SPDX-FileCopyrightText: ☭ Emery Hemingway
|
||||
# SPDX-License-Identifier: Unlicense
|
||||
|
||||
|
||||
import preserves, syndicate, syndicate/relays
|
||||
import ../schema/[config, sql]
|
||||
|
||||
{.passL: "-lpq".}
|
||||
|
||||
{.pragma: libpq, header: "libpq-fe.h", importc.}
|
||||
|
||||
type
|
||||
PGconn* {.libpq.} = ptr object
|
||||
ConnStatusType* {.libpq.} = enum
|
||||
CONNECTION_OK, CONNECTION_BAD, ## Non-blocking mode only below here
|
||||
##
|
||||
## The existence of these should never be relied upon - they should only
|
||||
## be used for user feedback or similar purposes.
|
||||
##
|
||||
CONNECTION_STARTED, ## Waiting for connection to be made.
|
||||
CONNECTION_MADE, ## Connection OK; waiting to send.
|
||||
CONNECTION_AWAITING_RESPONSE, ## Waiting for a response from the
|
||||
## postmaster.
|
||||
CONNECTION_AUTH_OK, ## Received authentication; waiting for
|
||||
## backend startup.
|
||||
CONNECTION_SETENV, ## This state is no longer used.
|
||||
CONNECTION_SSL_STARTUP, ## Negotiating SSL.
|
||||
CONNECTION_NEEDED, ## Internal state: connect() needed
|
||||
CONNECTION_CHECK_WRITABLE, ## Checking if session is read-write.
|
||||
CONNECTION_CONSUME, ## Consuming any extra messages.
|
||||
CONNECTION_GSS_STARTUP, ## Negotiating GSSAPI.
|
||||
CONNECTION_CHECK_TARGET, ## Checking target server properties.
|
||||
CONNECTION_CHECK_STANDBY ## Checking if server is in standby mode.
|
||||
|
||||
|
||||
proc PQconnectdbParams(
|
||||
keywords: cstringArray; values: cstringArray; expand_dbname: cint): PGconn {.libpq.}
|
||||
|
||||
proc PQerrorMessage(conn: PGconn): cstring {.libpq.}
|
||||
proc PQfinish(conn: PGconn) {.libpq.}
|
||||
proc PQstatus(conn: PGconn): ConnStatusType {.libpq.}
|
||||
|
||||
# proc PQsocket(conn: PGconn): cint
|
||||
# proc PQconnectStartParams(
|
||||
# keywords: cstringArray; values: cstringArray; expand_dbname: cint): PGconn
|
||||
# TODO: async
|
||||
|
||||
|
||||
type StringPairs = seq[tuple[key: string, val: string]]
|
||||
|
||||
proc splitParams(params: StringPairs): (cstringArray, cstringArray) =
|
||||
var strings = newSeq[string](params.len)
|
||||
for i, _ in params: strings[i] = params[i][0]
|
||||
result[0] = allocCStringArray(strings)
|
||||
for i, _ in params: strings[i] = params[i][1]
|
||||
result[1] = allocCStringArray(strings)
|
||||
|
||||
proc spawnPostgreActor*(turn: var Turn; root: Cap): Actor {.discardable.} =
|
||||
spawn("postgre", turn) do (turn: var Turn):
|
||||
during(turn, root, ?:PostgreArguments) do (params: StringPairs, ds: Cap):
|
||||
var
|
||||
conn: PGconn
|
||||
statusHandle: Handle
|
||||
block:
|
||||
var (keys, vals) = splitParams(params)
|
||||
conn = PQconnectdbParams(keys, vals, 0)
|
||||
deallocCStringArray(keys)
|
||||
deallocCStringArray(vals)
|
||||
if conn.isNil: raise newException(OutOfMemDefect, "failed to alloc PQ connection")
|
||||
let
|
||||
status = PQstatus(conn)
|
||||
msg = $PQerrorMessage(conn)
|
||||
statusHandle = publish(turn, ds,
|
||||
initRecord("status", toSymbol($status), msg.toPreserves))
|
||||
if status == CONNECTION_OK:
|
||||
discard
|
||||
do:
|
||||
PQfinish(conn)
|
||||
|
||||
when isMainModule:
|
||||
runActor("main") do (turn: var Turn; root: Cap):
|
||||
connectStdio(turn, root)
|
||||
spawnPostgreActor(turn, root)
|
Loading…
Reference in New Issue