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.
This commit is contained in:
Joris Vink 2018-07-18 11:38:17 +02:00
parent 1447f6573f
commit 2e321f14de
2 changed files with 27 additions and 0 deletions

View File

@ -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

View File

@ -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;