-
Notifications
You must be signed in to change notification settings - Fork 2
/
pq.l
159 lines (134 loc) · 3.71 KB
/
pq.l
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
;; -*- mode: lisp -*-
(define ffi (require 'ffi))
(define motor (require 'motor))
(define-c ffi |
struct pg_conn;
struct pg_result;
typedef struct pg_conn PGconn;
typedef struct pg_result PGresult;
typedef enum
{
CONNECTION_OK,
CONNECTION_BAD,
/* Non-blocking mode only below here */
CONNECTION_STARTED,
CONNECTION_MADE,
CONNECTION_AWAITING_RESPONSE,
CONNECTION_AUTH_OK,
CONNECTION_SETENV,
CONNECTION_SSL_STARTUP,
CONNECTION_NEEDED
} ConnStatusType;
typedef enum
{
PGRES_EMPTY_QUERY = 0,
PGRES_COMMAND_OK,
PGRES_TUPLES_OK,
PGRES_COPY_OUT,
PGRES_COPY_IN,
PGRES_BAD_RESPONSE,
PGRES_NONFATAL_ERROR,
PGRES_FATAL_ERROR,
PGRES_COPY_BOTH,
PGRES_SINGLE_TUPLE
} ExecStatusType;
PGconn *PQconnectdb(const char *conninfo);
ConnStatusType PQstatus(const PGconn *conn);
ExecStatusType PQresultStatus(const PGresult *res);
void PQfinish(PGconn *conn);
void PQreset(PGconn *conn);
int PQsocket(const PGconn *conn);
int PQsendQuery(PGconn *conn, const char *command);
int PQconsumeInput(PGconn *conn);
int PQisBusy(PGconn *conn);
int PQsetnonblocking(PGconn *conn, int arg);
int PQflush(PGconn *conn);
char *PQerrorMessage(const PGconn *conn);
char *PQresultErrorMessage(const PGresult *res);
PGresult *PQgetResult(PGconn *conn);
void PQclear(PGresult *res);
char *PQcmdStatus(PGresult *res);
char *PQcmdTuples(PGresult *res);
int PQntuples(const PGresult *res);
int PQnfields(const PGresult *res);
char *PQfname(const PGresult *res, int column_number);
char *PQgetvalue(const PGresult *res, int row_number, int column_number);
|)
(define pq (ffi.load 'pq))
(define cstr ffi.string)
(define abort (p name)
(let e (cstr (pq.PQerrorMessage p))
(error (cat (or name 'error) ": " e))))
(define connected? (p)
(= (pq.PQstatus p) pq.CONNECTION_OK))
(define finish (p)
(pq.PQfinish p))
(define connect (s t)
(let p (pq.PQconnectdb s)
(when (connected? p)
(let x (pq.PQsetnonblocking p 1)
(unless (= x 0)
(abort p 'connect)))
(when (function? t)
(let f t
(set t (thread (fn () (f p))))))
(let (fd (pq.PQsocket p)
f (fn () (finish p)))
(motor.enter fd t f))
p)))
(define consume (p fd)
(motor.wait fd)
(let x (pq.PQconsumeInput p)
(when (= x 0)
(abort p 'consume))))
(define get-rows (res n m)
(with rs ()
(for i n
(let r ()
(for j m
(let (k (cstr (pq.PQfname res j))
v (cstr (pq.PQgetvalue res i j)))
(set (get r k) v)))
(add rs r)))))
(define result (r)
(let x (pq.PQresultStatus r)
(if (= x pq.PGRES_COMMAND_OK)
(let a (cstr (pq.PQcmdTuples r))
(list command: (cstr (pq.PQcmdStatus r))
size: (when (some? a) (number a))))
(or (= x pq.PGRES_TUPLES_OK)
(= x pq.PGRES_SINGLE_TUPLE))
(let (n (pq.PQntuples r)
m (pq.PQnfields r))
(list command: (cstr (pq.PQcmdStatus r))
size: n
rows: (get-rows r n m)))
(list error: (cstr (pq.PQresultErrorMessage r))))))
(define clear (r)
(pq.PQclear r))
(define send-query (p fd q)
(let x (pq.PQsendQuery p q)
(when (= x 0)
(abort p 'query)))
(let sent false
(while (not sent)
(motor.wait fd 'out)
(let (x (pq.PQflush p))
(if (< x 0) (abort p 'query)
(= x 0) (set sent true))))))
(define get-results (p fd)
(with rs ()
(while true
(if (= (pq.PQisBusy p) 0)
(let r (pq.PQgetResult p)
(if (is? r)
(add rs r)
(break)))
(consume p fd)))))
(define query (p q)
(let fd (pq.PQsocket p)
(send-query p fd q)
(let rs (get-results p fd)
(with xs (map result rs)
(map clear rs)))))
(export connect query)