-
Notifications
You must be signed in to change notification settings - Fork 4
/
pq.c
675 lines (567 loc) · 20.3 KB
/
pq.c
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
#include <alloca.h>
#include <errno.h>
#include <inttypes.h>
#include <janet.h>
#include <libpq-fe.h>
#if defined(__APPLE_CC__)
#include <math.h>
#include <sys/select.h>
#endif
typedef struct {
PGresult *r;
} JPQresult;
static int result_gc(void *p, size_t s) {
(void)s;
JPQresult *jpqr = (JPQresult *)p;
if (jpqr->r) {
PQclear(jpqr->r);
jpqr->r = NULL;
}
return 0;
}
static void result_to_string(void *p, JanetBuffer *buffer) {
JPQresult *jpqr = (JPQresult *)p;
janet_buffer_push_cstring(buffer, "PGResult: ");
if (!jpqr->r) {
janet_buffer_push_cstring(buffer, "uninit");
return;
}
int status = PQresultStatus(jpqr->r);
if (status != PGRES_TUPLES_OK && status != PGRES_EMPTY_QUERY &&
status != PGRES_COMMAND_OK) {
char *msg = PQresultErrorMessage(jpqr->r);
janet_buffer_push_cstring(buffer, msg ? msg : "error");
} else {
janet_buffer_push_cstring(buffer, "...");
}
}
static const JanetAbstractType pq_result_type = {
"pq/result", result_gc, NULL, NULL, NULL, NULL,
NULL, result_to_string, NULL};
static Janet safe_cstringv(char *s) {
return s ? janet_cstringv(s) : janet_wrap_nil();
}
static Janet safe_ckeywordv(char *s) {
return s ? janet_ckeywordv(s) : janet_wrap_nil();
}
static Janet jpq_result_ntuples(int32_t argc, Janet *argv) {
janet_fixarity(argc, 1);
JPQresult *jpqr = (JPQresult *)janet_getabstract(argv, 0, &pq_result_type);
return janet_wrap_number(PQntuples(jpqr->r));
}
static Janet jpq_result_nfields(int32_t argc, Janet *argv) {
janet_fixarity(argc, 1);
JPQresult *jpqr = (JPQresult *)janet_getabstract(argv, 0, &pq_result_type);
return janet_wrap_number(PQnfields(jpqr->r));
}
static Janet jpq_result_fname(int32_t argc, Janet *argv) {
janet_fixarity(argc, 2);
JPQresult *jpqr = (JPQresult *)janet_getabstract(argv, 0, &pq_result_type);
int col = janet_getinteger(argv, 1);
return safe_cstringv(PQfname(jpqr->r, col));
}
static Janet jpq_result_fnumber(int32_t argc, Janet *argv) {
janet_fixarity(argc, 2);
JPQresult *jpqr = (JPQresult *)janet_getabstract(argv, 0, &pq_result_type);
const char *s = janet_getcstring(argv, 1);
return janet_wrap_number(PQfnumber(jpqr->r, s));
}
static Janet jpq_result_ftype(int32_t argc, Janet *argv) {
janet_fixarity(argc, 2);
JPQresult *jpqr = (JPQresult *)janet_getabstract(argv, 0, &pq_result_type);
int col = janet_getinteger(argv, 1);
return janet_wrap_number(PQftype(jpqr->r, col));
}
static Janet jpq_result_fformat(int32_t argc, Janet *argv) {
janet_fixarity(argc, 2);
JPQresult *jpqr = (JPQresult *)janet_getabstract(argv, 0, &pq_result_type);
int col = janet_getinteger(argv, 1);
return janet_wrap_number(PQfformat(jpqr->r, col));
}
static Janet jpq_result_status(int32_t argc, Janet *argv) {
janet_fixarity(argc, 1);
JPQresult *jpqr = (JPQresult *)janet_getabstract(argv, 0, &pq_result_type);
return janet_wrap_number(PQresultStatus(jpqr->r));
}
static Janet jpq_result_error_message(int32_t argc, Janet *argv) {
janet_fixarity(argc, 1);
JPQresult *jpqr = (JPQresult *)janet_getabstract(argv, 0, &pq_result_type);
return safe_cstringv(PQresultErrorMessage(jpqr->r));
}
static Janet jpq_result_error_field(int32_t argc, Janet *argv) {
janet_fixarity(argc, 2);
JPQresult *jpqr = (JPQresult *)janet_getabstract(argv, 0, &pq_result_type);
int code = janet_getinteger(argv, 1);
return safe_cstringv(PQresultErrorField(jpqr->r, code));
}
static Janet jpq_error_pred(int32_t argc, Janet *argv) {
janet_fixarity(argc, 1);
if (!janet_checkabstract(argv[0], &pq_result_type))
return janet_wrap_boolean(0);
JPQresult *jpqr = (JPQresult *)janet_getabstract(argv, 0, &pq_result_type);
int status = PQresultStatus(jpqr->r);
return janet_wrap_boolean(status != PGRES_TUPLES_OK &&
status != PGRES_EMPTY_QUERY &&
status != PGRES_COMMAND_OK);
}
static Janet jpq_result_unpack(int32_t argc, Janet *argv) {
janet_fixarity(argc, 2);
JPQresult *jpqr = (JPQresult *)janet_getabstract(argv, 0, &pq_result_type);
JanetTable *decode_tab = janet_gettable(argv, 1);
int status = PQresultStatus(jpqr->r);
if (status == PGRES_EMPTY_QUERY || status == PGRES_COMMAND_OK)
return janet_wrap_nil();
if (status != PGRES_TUPLES_OK)
janet_panicv(argv[0]);
int n = PQntuples(jpqr->r);
int ncols = PQnfields(jpqr->r);
JanetArray *a = janet_array(n);
for (int i = 0; i < n; i++) {
JanetTable *t = janet_table(ncols);
for (int j = 0; j < ncols; j++) {
Janet jv;
if (PQgetisnull(jpqr->r, i, j)) {
jv = janet_wrap_nil();
} else {
Oid t = PQftype(jpqr->r, j);
int format = PQfformat(jpqr->r, j);
if (format != 0)
janet_panic("BUG: currently only text decoding is supported");
char *v = PQgetvalue(jpqr->r, i, j);
int l = PQgetlength(jpqr->r, i, j);
Janet decoder = janet_table_get(decode_tab, janet_wrap_integer(t));
if (janet_checktype(decoder, JANET_NIL))
janet_panicf("no decoder entry for returned row item with oid - %d",
t);
switch (janet_type(decoder)) {
case JANET_FUNCTION: {
Janet args[1];
args[0] = janet_stringv((uint8_t *)v, l);
JanetFunction *f = janet_unwrap_function(decoder);
/* XXX should we reenable GC? */
jv = janet_call(f, 1, args);
break;
}
case JANET_CFUNCTION: {
Janet args[1];
args[0] = janet_stringv((uint8_t *)v, l);
JanetCFunction f = janet_unwrap_cfunction(decoder);
jv = f(1, args);
break;
}
default:
/* XXX we could have an abstract type for more efficient c functions
*/
janet_panic("decoder entry is not a callable function");
}
}
Janet k = safe_ckeywordv(PQfname(jpqr->r, j));
janet_table_put(t, k, jv);
}
janet_array_push(a, janet_wrap_table(t));
}
return janet_wrap_array(a);
}
typedef struct {
PGconn *conn;
} Context;
static void context_close(Context *ctx) {
if (ctx->conn) {
PQfinish(ctx->conn);
ctx->conn = NULL;
}
}
static int context_gc(void *p, size_t s) {
(void)s;
Context *ctx = (Context *)p;
context_close(ctx);
return 0;
}
static Janet jpq_close(int32_t argc, Janet *argv);
static JanetMethod context_methods[] = {
{"close", jpq_close}, /* So contexts can be used with 'with' */
{NULL, NULL}};
static int context_get(void *ptr, Janet key, Janet *out) {
Context *p = (Context *)ptr;
return janet_getmethod(janet_unwrap_keyword(key), context_methods, out);
}
static const JanetAbstractType pq_context_type = {
"pq/context", context_gc, NULL, context_get, NULL,
NULL, NULL, NULL, NULL, NULL};
static Janet jpq_connect(int32_t argc, Janet *argv) {
janet_fixarity(argc, 1);
const char *u = janet_getcstring(argv, 0);
Context *ctx = (Context *)janet_abstract(&pq_context_type, sizeof(Context));
ctx->conn = PQconnectdb(u);
if (!ctx->conn)
janet_panic("unable to create connection");
if (PQstatus(ctx->conn) != CONNECTION_OK) {
/* GC cleans this up as we assigned to the type. */
janet_panicf("connection failed: %s", PQerrorMessage(ctx->conn));
}
return janet_wrap_abstract(ctx);
}
static void __ensure_ctx_ok(Context *ctx) {
if (ctx->conn == NULL)
janet_panic("pq context is disconnected");
}
static void *zsmalloc(size_t n) {
void *p = janet_smalloc(n);
memset(p, 0, n);
return p;
}
static Janet jpq_exec(int32_t argc, Janet *argv) {
if (argc < 2)
janet_panic("expected at least a pq context and a query string");
if (argc > 10000000)
janet_panic("too many arguments");
Context *ctx = (Context *)janet_getabstract(argv, 0, &pq_context_type);
__ensure_ctx_ok(ctx);
JPQresult *jpqr =
(JPQresult *)janet_abstract(&pq_result_type, sizeof(JPQresult));
jpqr->r = NULL;
const char *q = janet_getcstring(argv, 1);
argc -= 2;
argv += 2;
Oid *poids;
int *plengths;
int *pformats;
char **pvals;
#define NFAST_PATH 4
if (argc <= NFAST_PATH) {
int n;
#define ZALLOCA(P, N) \
do { \
n = N; \
P = alloca(n); \
memset(P, 0, n); \
} while (0)
ZALLOCA(poids, sizeof(Oid) * argc);
ZALLOCA(plengths, sizeof(int) * argc);
ZALLOCA(pformats, sizeof(int) * argc);
ZALLOCA(pvals, sizeof(char *) * argc);
#undef ZALLOCA
} else {
poids = zsmalloc(sizeof(Oid) * argc);
plengths = zsmalloc(sizeof(int) * argc);
pformats = zsmalloc(sizeof(int) * argc);
pvals = zsmalloc(sizeof(char *) * argc);
}
for (int i = 0; i < argc; i++) {
Janet j = argv[i];
Oid oid = 0;
try_again:
switch (janet_type(j)) {
case JANET_NIL:
break;
case JANET_BOOLEAN:
plengths[i] = 2;
pvals[i] = janet_smalloc(2);
pvals[i][0] = janet_unwrap_boolean(j) ? 't' : 'f';
pvals[i][1] = 0;
break;
case JANET_BUFFER: {
JanetBuffer *b = janet_unwrap_buffer(j);
pvals[i] = janet_smalloc(b->count);
memcpy(pvals[i], b->data, b->count);
plengths[i] = b->count;
pformats[i] = 1;
break;
}
case JANET_KEYWORD:
case JANET_STRING: {
const char *s = (char *)janet_unwrap_string(j);
size_t l = janet_string_length(s);
pvals[i] = janet_smalloc(l);
memcpy(pvals[i], s, l);
plengths[i] = l;
pformats[i] = 1;
break;
}
case JANET_NUMBER: {
double d = janet_unwrap_number(j);
/* The exact range could be increased to ~52 bits afaik, for now 32 bits
* only. */
const char *fmt = (d == floor(d) && d <= ((double)INT32_MAX) &&
d >= ((double)INT32_MIN))
? "%.0f"
: "%g";
size_t l = snprintf(NULL, 0, fmt, d);
pvals[i] = janet_smalloc(l + 1);
snprintf(pvals[i], l + 1, fmt, d);
plengths[i] = l;
break;
}
case JANET_ARRAY:
case JANET_TUPLE:
raw_encode : {
JanetView view = janet_getindexed(&j, 0);
if (view.len != 3)
janet_panic(
"arrays and tuples must be an [oid is-binary string] triple");
if (!janet_checktype(view.items[0], JANET_NUMBER))
janet_panic("oid must be a number");
poids[i] = (Oid)janet_unwrap_number(view.items[0]);
if (!janet_checktype(view.items[1], JANET_BOOLEAN))
janet_panic("is-binary must be a boolean");
pformats[i] = (Oid)janet_unwrap_boolean(view.items[1]);
if (!janet_checktype(view.items[2], JANET_STRING) &&
!janet_checktype(view.items[2], JANET_BUFFER))
janet_panic("value in oid pair must be a string or buffer");
JanetByteView bytes = janet_getbytes(&view.items[2], 0);
pvals[i] = janet_smalloc(bytes.len + 1);
memcpy(pvals[i], bytes.bytes, bytes.len);
pvals[i][bytes.len] = 0;
plengths[i] = bytes.len;
break;
}
case JANET_ABSTRACT: {
JanetIntType intt = janet_is_int(j);
if (intt == JANET_INT_S64) {
int64_t v = janet_unwrap_s64(j);
size_t l = snprintf(NULL, 0, "%" PRId64, v);
pvals[i] = janet_smalloc(l + 1);
snprintf(pvals[i], l + 1, "%" PRId64, v);
plengths[i] = l;
break;
} else if (intt == JANET_INT_U64) {
uint64_t v = janet_unwrap_u64(j);
size_t l = snprintf(NULL, 0, "%" PRIu64, v);
pvals[i] = janet_smalloc(l + 1);
snprintf(pvals[i], l + 1, "%" PRIu64, v);
plengths[i] = l;
break;
} else {
/* fall through to default */
}
}
default: {
/* XXX: renable janet GC for this call? how do we do that? what do we
need to root these values? */
j = janet_mcall("pq/encode", 1, &j);
if (!janet_checktype(j, JANET_ARRAY) && !janet_checktype(j, JANET_TUPLE))
janet_panic("method :pq/encode did not return an array or tuple");
goto raw_encode;
}
}
}
jpqr->r = PQexecParams(ctx->conn, q, argc, NULL, (const char *const *)pvals,
plengths, pformats, 0);
if (!jpqr->r)
janet_panic("query failed");
/* Free in reverse order for sfree's sake */
for (int i = argc - 1; i > 0; i--) {
janet_sfree(pvals[i]);
}
if (argc > NFAST_PATH) {
janet_sfree(pvals);
janet_sfree(pformats);
janet_sfree(plengths);
janet_sfree(poids);
}
#undef NFAST_PATH
if (PQresultStatus(jpqr->r) == PGRES_TUPLES_OK) {
size_t pressure = 0;
int n = PQntuples(jpqr->r);
int ncols = PQnfields(jpqr->r);
for (int i = 0; i < n; i++) {
for (int j = 0; j < ncols; j++) {
if (!PQgetisnull(jpqr->r, i, j)) {
pressure += PQgetlength(jpqr->r, i, j);
}
}
}
janet_gcpressure(pressure);
}
return janet_wrap_abstract(jpqr);
}
static Janet jpq_status(int32_t argc, Janet *argv) {
janet_fixarity(argc, 1);
Context *ctx = (Context *)janet_getabstract(argv, 0, &pq_context_type);
__ensure_ctx_ok(ctx);
return janet_wrap_integer(PQstatus(ctx->conn));
}
static Janet jpq_transaction_status(int32_t argc, Janet *argv) {
janet_fixarity(argc, 1);
Context *ctx = (Context *)janet_getabstract(argv, 0, &pq_context_type);
__ensure_ctx_ok(ctx);
return janet_wrap_integer(PQtransactionStatus(ctx->conn));
}
static Janet jpq_wait_for_pending_data(int32_t argc, Janet *argv) {
janet_fixarity(argc, 2);
Context *ctx = (Context *)janet_getabstract(argv, 0, &pq_context_type);
double delay = janet_getnumber(argv, 1);
__ensure_ctx_ok(ctx);
int sockfd = PQsocket(ctx->conn);
if (sockfd < 0)
janet_panic("no connected socket");
int select_rc;
do {
fd_set set;
struct timeval timeout;
FD_ZERO(&set);
FD_SET(sockfd, &set);
timeout.tv_sec = (time_t)delay;
timeout.tv_usec = (delay <= UINT32_MAX)
? (long)((delay - ((uint32_t)delay)) * 1000000)
: 0;
select_rc = select(FD_SETSIZE, &set, NULL, NULL, &timeout);
} while (select_rc < 0 && errno == EINTR);
if (select_rc == -1)
janet_panicf("io error on pq socket: %s", strerror(errno));
return janet_wrap_boolean(select_rc != 0);
}
static Janet jpq_consume_input(int32_t argc, Janet *argv) {
janet_fixarity(argc, 1);
Context *ctx = (Context *)janet_getabstract(argv, 0, &pq_context_type);
__ensure_ctx_ok(ctx);
if (!PQconsumeInput(ctx->conn))
janet_panicv(safe_cstringv(PQerrorMessage(ctx->conn)));
return janet_wrap_nil();
}
static Janet jpq_notifies(int32_t argc, Janet *argv) {
janet_fixarity(argc, 1);
Context *ctx = (Context *)janet_getabstract(argv, 0, &pq_context_type);
__ensure_ctx_ok(ctx);
PGnotify *nf = PQnotifies(ctx->conn);
if (!nf)
return janet_wrap_nil();
JanetKV *st = janet_struct_begin(3);
janet_struct_put(st, janet_ckeywordv("name"), janet_cstringv(nf->relname));
janet_struct_put(st, janet_ckeywordv("pid"), janet_wrap_number(nf->be_pid));
if (nf->extra)
janet_struct_put(st, janet_ckeywordv("extra"), janet_cstringv(nf->extra));
PQfreemem(nf);
return janet_wrap_struct(janet_struct_end(st));
}
static Janet jpq_close(int32_t argc, Janet *argv) {
janet_fixarity(argc, 1);
Context *ctx = (Context *)janet_getabstract(argv, 0, &pq_context_type);
context_close(ctx);
return janet_wrap_nil();
}
static Janet jpq_escape_literal(int32_t argc, Janet *argv) {
janet_fixarity(argc, 2);
Context *ctx = (Context *)janet_getabstract(argv, 0, &pq_context_type);
char *input = (char *)janet_getstring(argv, 1);
char *output = PQescapeLiteral(ctx->conn, input, janet_string_length(input));
if (!output)
janet_panicv(safe_cstringv(PQerrorMessage(ctx->conn)));
const uint8_t *result = janet_string((uint8_t *)output, strlen(output));
PQfreemem(output);
return janet_wrap_string(result);
}
static Janet jpq_escape_identifier(int32_t argc, Janet *argv) {
janet_fixarity(argc, 2);
Context *ctx = (Context *)janet_getabstract(argv, 0, &pq_context_type);
char *input = (char *)janet_getstring(argv, 1);
char *output =
PQescapeIdentifier(ctx->conn, input, janet_string_length(input));
if (!output)
janet_panicv(safe_cstringv(PQerrorMessage(ctx->conn)));
const uint8_t *result = janet_string((uint8_t *)output, strlen(output));
PQfreemem(output);
return janet_wrap_string(result);
}
static int decode_nibble(uint8_t b) {
if (b >= '0' && b <= '9')
return b - '0';
if (b >= 'a' && b <= 'f')
return 10 + b - 'a';
if (b >= 'A' && b <= 'F')
return 10 + b - 'A';
return 0;
}
static Janet jpq_decode_bytea(int32_t argc, Janet *argv) {
const uint8_t *in;
uint8_t *out;
janet_fixarity(argc, 1);
JanetByteView b = janet_getbytes(argv, 0);
size_t nbytes = (size_t)b.len;
if (nbytes < 2 || b.bytes[0] != '\\' || b.bytes[1] != 'x')
janet_panic("bytea encoded data should begin with '\\x'");
nbytes -= 2;
in = b.bytes + 2;
out = janet_smalloc(nbytes / 2);
size_t nout = 0;
for (size_t i = 0; i < nbytes; i += 2) {
uint8_t c1 = decode_nibble(in[i]);
uint8_t c2 = decode_nibble((i + 1 < nbytes) ? in[i + 1] : 0);
out[nout++] = (c1 << 4) | c2;
}
Janet s = janet_stringv(out, nout);
janet_sfree(out);
return s;
}
#define upstream_doc "See libpq documentation at https://www.postgresql.org."
static const JanetReg cfuns[] = {
{"connect", jpq_connect,
"(pq/connect url)\n\n"
"Connect to a postgres server or raise an error."},
{"consume-input", jpq_consume_input, upstream_doc},
{"wait-for-pending-data", jpq_wait_for_pending_data,
"(pq/wait-for-pending-data ctx timeout)\n\n"
"Perform a select on the underlying connection waiting for data."},
{"exec", jpq_exec, "See pq/exec"},
{"close", jpq_close,
"(pq/close ctx)\n\n"
"Close a pq context."},
{"error?", jpq_error_pred,
"(pq/error? result)\n\n"
"Check if an object is a pq.result containing an error."},
{"transaction-status", jpq_transaction_status, upstream_doc},
{"notifies", jpq_notifies, upstream_doc},
{"status", jpq_status, upstream_doc},
{"escape-literal", jpq_escape_literal, upstream_doc},
{"escape-identifier", jpq_escape_identifier, upstream_doc},
{"result-ntuples", jpq_result_ntuples, upstream_doc},
{"result-nfields", jpq_result_nfields, upstream_doc},
{"result-fname", jpq_result_fname, upstream_doc},
{"result-fnumber", jpq_result_fnumber, upstream_doc},
{"result-ftype", jpq_result_ftype, upstream_doc},
{"result-fformat", jpq_result_fformat, upstream_doc},
{"result-status", jpq_result_status, upstream_doc},
{"result-error-message", jpq_result_error_message, upstream_doc},
{"result-error-field", jpq_result_error_field, upstream_doc},
{"result-unpack", jpq_result_unpack, upstream_doc},
{"decode-bytea", jpq_decode_bytea, "(pq/decode-bytea buf)"},
{NULL, NULL, NULL}};
JANET_MODULE_ENTRY(JanetTable *env) {
janet_cfuns(env, "pq", cfuns);
janet_register_abstract_type(&pq_context_type);
janet_register_abstract_type(&pq_result_type);
#define DEF_CONSTANT_INT(X) janet_def(env, #X, janet_wrap_integer(X), NULL)
/* PQStatus */
DEF_CONSTANT_INT(CONNECTION_OK);
DEF_CONSTANT_INT(CONNECTION_BAD);
/* PQransactionStatus */
DEF_CONSTANT_INT(PQTRANS_IDLE);
DEF_CONSTANT_INT(PQTRANS_ACTIVE);
DEF_CONSTANT_INT(PQTRANS_INERROR);
DEF_CONSTANT_INT(PQTRANS_INTRANS);
/* PQresultStatus */
DEF_CONSTANT_INT(PGRES_EMPTY_QUERY);
DEF_CONSTANT_INT(PGRES_COMMAND_OK);
DEF_CONSTANT_INT(PGRES_TUPLES_OK);
DEF_CONSTANT_INT(PGRES_BAD_RESPONSE);
DEF_CONSTANT_INT(PGRES_FATAL_ERROR);
/* PQresultErrorField */
DEF_CONSTANT_INT(PG_DIAG_SEVERITY);
DEF_CONSTANT_INT(PG_DIAG_SQLSTATE);
DEF_CONSTANT_INT(PG_DIAG_MESSAGE_PRIMARY);
DEF_CONSTANT_INT(PG_DIAG_MESSAGE_DETAIL);
DEF_CONSTANT_INT(PG_DIAG_MESSAGE_HINT);
DEF_CONSTANT_INT(PG_DIAG_STATEMENT_POSITION);
DEF_CONSTANT_INT(PG_DIAG_INTERNAL_POSITION);
DEF_CONSTANT_INT(PG_DIAG_INTERNAL_QUERY);
DEF_CONSTANT_INT(PG_DIAG_CONTEXT);
DEF_CONSTANT_INT(PG_DIAG_SCHEMA_NAME);
DEF_CONSTANT_INT(PG_DIAG_TABLE_NAME);
DEF_CONSTANT_INT(PG_DIAG_COLUMN_NAME);
DEF_CONSTANT_INT(PG_DIAG_DATATYPE_NAME);
DEF_CONSTANT_INT(PG_DIAG_CONSTRAINT_NAME);
DEF_CONSTANT_INT(PG_DIAG_SOURCE_FILE);
DEF_CONSTANT_INT(PG_DIAG_SOURCE_LINE);
DEF_CONSTANT_INT(PG_DIAG_SOURCE_FUNCTION);
#undef DEF_CONSTANT_INT
}