-
Notifications
You must be signed in to change notification settings - Fork 9
/
listen.go
68 lines (58 loc) · 1.25 KB
/
listen.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package libpq
/*
#include <stdlib.h>
#include <sys/select.h>
#include <libpq-fe.h>
static PGnotify *waitForNotify(PGconn *conn) {
int sock;
fd_set input_mask;
PGnotify *note;
sock = PQsocket(conn);
if (sock < 0) {
return NULL;
}
while (1) {
FD_ZERO(&input_mask);
FD_SET(sock, &input_mask);
// block waiting for input
if (select(sock+1, &input_mask, NULL, NULL, NULL) < 0) {
return NULL;
}
// check for notifications
PQconsumeInput(conn);
if ((note = PQnotifies(conn)) != NULL) {
return note;
}
}
}
*/
import "C"
import (
"database/sql/driver"
"unsafe"
)
type libpqListenRows struct {
c *libpqConn
}
func (r *libpqListenRows) Close() error {
// we're the exclusive owners of this libpqConn, so it's safe to unlisten *
_, err := r.c.exec("UNLISTEN *", false)
return err
}
func (r *libpqListenRows) Columns() []string {
return []string{"NOTIFICATION"}
}
func (r *libpqListenRows) Next(dest []driver.Value) error {
// see if we already have pending notifications
note := C.PQnotifies(r.c.db)
if note == nil {
// none pending - block waiting for one
note = C.waitForNotify(r.c.db)
if note == nil {
return driver.ErrBadConn
}
}
defer C.PQfreemem(unsafe.Pointer(note))
dest[0] = C.GoString(note.extra)
return nil
}