-
-
Save anonymous/8884fa960a41c0ab72df9b7cfdd184c3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use "lib:pq" | |
use @pony_asio_event_create[AsioEventID](owner: AsioEventNotify, fd: U32, flags: U32, nsec: U64, noisy: Bool) | |
use @pony_asio_event_destroy[None](event: AsioEventID) | |
primitive PGconn | |
primitive PGresult | |
primitive ConnectionOk | |
primitive ConnectionBad | |
type ConnStatusType is (ConnectionOk | ConnectionBad) | |
primitive ResultCommandOk | |
type ExecStatusType is (ResultCommandOk) | |
struct PGnotify | |
var relname: Pointer[U8] = Pointer[U8] | |
var extra: Pointer[U8] = Pointer[U8] | |
var be_pid: I32 = -1 | |
new create() => None | |
class Libpq | |
fun error_message(conn: PGconn): String ref => | |
String.from_cstring(@PQerrorMessage[Pointer[U8]](conn)) | |
fun connectdb(conn_str: String): PGconn => | |
@PQconnectdb[PGconn](conn_str.cstring()) | |
fun finish(conn: PGconn) => | |
@PQfinish[None](conn) | |
fun status(conn: PGconn): (None | ConnStatusType) => | |
match @PQstatus[I32](conn) | |
| 0 => ConnectionOk | |
| 1 => ConnectionBad | |
else | |
None | |
end | |
fun exec(conn: PGconn, query: String): PGresult => | |
@PQexec[PGresult](conn, query.cstring()) | |
fun result_status(result: PGresult): (None | ExecStatusType) => | |
match @PQresultStatus[I32](result) | |
| 1 => ResultCommandOk | |
else | |
None | |
end | |
fun clear(result: PGresult) => | |
@PQclear[None](result) | |
fun socket(conn: PGconn): U32 => | |
@PQsocket[U32](conn) | |
actor Main | |
var _event: AsioEventID = AsioEvent.none() | |
var _env: Env | |
let _libpq: Libpq = Libpq | |
var _conn: PGconn | |
fun disconnect(conn: PGconn) => | |
_libpq.finish(conn) | |
_env.out.print("Disconnected") | |
new create(env: Env) => | |
_env = env | |
env.out.print("Connecting...") | |
let conn = _libpq.connectdb("dbname=postgres") | |
_conn = conn | |
if not(_libpq.status(conn) is ConnectionOk) then | |
env.out.print(_libpq.error_message(conn).string()) | |
disconnect(conn) | |
return | |
end | |
env.out.print("Connected") | |
let result = _libpq.exec(conn, "listen tbl2") | |
if not(_libpq.result_status(result) is ResultCommandOk) then | |
env.out.print(_libpq.error_message(conn).string()) | |
_libpq.clear(result) | |
disconnect(conn) | |
return | |
end | |
_libpq.clear(result) | |
var fd: U32 = _libpq.socket(conn) | |
_event = @pony_asio_event_create(this, fd, AsioEvent.read(), 0, true) | |
//disconnect(conn) | |
be _event_notify(event: AsioEventID, flags: U32, arg: U32) => | |
if AsioEvent.disposable(flags) then | |
@pony_asio_event_destroy(event) | |
else | |
//_env.out.write("Event") | |
@PQconsumeInput[None](_conn) | |
let notify = @PQnotifies[PGnotify](_conn) | |
_env.out.write(String.from_cstring(notify.extra).string()) | |
@PQfreemem[None](notify) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment