Created
March 6, 2017 11:11
-
-
Save NoTimeForHero/af9ca2bc9741348f66e1e63a5558d001 to your computer and use it in GitHub Desktop.
Harbour PostgreSQL LISTEN
This file contains hidden or 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
HB_FUNC ( PQ_GET_NOTIFY ) // VAA 07.03.2017 | |
{ | |
const char *clisten = "LISTEN "; | |
PGconn * conn = hb_PGconn_par( 1 ); | |
const char *source = hb_parcx( 2 ); | |
HB_SIZE size = strlen(source); | |
char *channel; | |
char *output; | |
int outlen; | |
PGresult * res; | |
int sock; | |
fd_set input_mask; | |
PGnotify *notify; | |
if( !conn ) | |
hb_errRT_BASE( EG_ARG, 2020, NULL, HB_ERR_FUNCNAME, HB_ERR_ARGS_BASEPARAMS ); | |
channel = PQescapeIdentifier( conn, source, ( size_t ) size ); | |
outlen = strlen(clisten) + strlen(channel); | |
// #OUTPUT_COMMAND = "LISTEN " + #CHANNEL_NAME | |
output = (char *) hb_xgrab(outlen * 2 + 1); | |
memset(output, 0, outlen * 2 + 1); | |
strncpy(output, clisten, strlen(clisten)); | |
strncat(output, channel, strlen(channel)); | |
PQfreemem(channel); | |
res = PQexec(conn, output); | |
hb_xfree(output); | |
if (PQresultStatus(res) != PGRES_COMMAND_OK) | |
hb_ret(); | |
PQclear(res); | |
sock = PQsocket(conn); | |
if (sock < 0) | |
hb_ret(); | |
FD_ZERO(&input_mask); | |
FD_SET(sock, &input_mask); | |
if (select(sock + 1, &input_mask, NULL, NULL, NULL) < 0) | |
hb_ret(); | |
PQconsumeInput(conn); | |
while ((notify = PQnotifies(conn)) != NULL) { | |
PHB_ITEM aNotify = hb_itemArrayNew( 3 ); | |
hb_arraySetNInt(aNotify, 2, notify->be_pid); | |
hb_arraySetC(aNotify, 1, notify->relname); | |
hb_arraySetC(aNotify, 3, notify->extra); | |
hb_itemReturnRelease( aNotify ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment