kore

a fork of the worlds most advanced web framework
Log | Files | Refs | README | LICENSE

commit 2e321f14de71814f5251d1365058882da13432b0
parent 1447f6573f8272adf950f6a44f8deb036e3a19aa
Author: Joris Vink <joris@coders.se>
Date:   Wed, 18 Jul 2018 11:38:17 +0200

Add KORE_PGSQL_STATE_NOTIFY.

Issue a LISTEN channel on a kore_pgsql, bind a callback to it and you
will get called with pgsql->state being KORE_PGSQL_STATE_NOTIFY.

Diffstat:
Minclude/kore/pgsql.h | 6++++++
Msrc/pgsql.c | 21+++++++++++++++++++++
2 files changed, 27 insertions(+), 0 deletions(-)

diff --git a/include/kore/pgsql.h b/include/kore/pgsql.h @@ -56,6 +56,11 @@ struct kore_pgsql { PGresult *result; struct pgsql_conn *conn; + struct { + char *channel; + char *extra; + } notify; + struct http_request *req; void *arg; void (*cb)(struct kore_pgsql *, void *); @@ -99,5 +104,6 @@ int kore_pgsql_getlength(struct kore_pgsql *, int, int); #define KORE_PGSQL_STATE_ERROR 4 #define KORE_PGSQL_STATE_DONE 5 #define KORE_PGSQL_STATE_COMPLETE 6 +#define KORE_PGSQL_STATE_NOTIFY 7 #endif diff --git a/src/pgsql.c b/src/pgsql.c @@ -361,6 +361,7 @@ kore_pgsql_continue(struct kore_pgsql *pgsql) break; case KORE_PGSQL_STATE_ERROR: case KORE_PGSQL_STATE_RESULT: + case KORE_PGSQL_STATE_NOTIFY: kore_pgsql_handle(pgsql->conn, 0); break; default: @@ -383,6 +384,9 @@ kore_pgsql_cleanup(struct kore_pgsql *pgsql) if (pgsql->conn != NULL) pgsql_conn_release(pgsql); + kore_free(pgsql->notify.extra); + kore_free(pgsql->notify.channel); + pgsql->result = NULL; pgsql->error = NULL; pgsql->conn = NULL; @@ -700,11 +704,28 @@ pgsql_conn_cleanup(struct pgsql_conn *conn) static void pgsql_read_result(struct kore_pgsql *pgsql) { + PGnotify *notify; + if (PQisBusy(pgsql->conn->db)) { pgsql->state = KORE_PGSQL_STATE_WAIT; return; } + while ((notify = PQnotifies(pgsql->conn->db)) != NULL) { + kore_free(pgsql->notify.extra); + kore_free(pgsql->notify.channel); + pgsql->state = KORE_PGSQL_STATE_NOTIFY; + pgsql->notify.channel = kore_strdup(notify->relname); + + if (notify->extra != NULL) + pgsql->notify.extra = kore_strdup(notify->extra); + else + pgsql->notify.extra = NULL; + + PQfreemem(notify); + return; + } + pgsql->result = PQgetResult(pgsql->conn->db); if (pgsql->result == NULL) { pgsql->state = KORE_PGSQL_STATE_DONE;