pgsql.h (3405B)
1 /*
2 * Copyright (c) 2014-2022 Joris Vink <joris@coders.se>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
16
17 #ifndef _H_KORE_PGSQL
18 #define _H_KORE_PGSQL
19
20 #include <libpq-fe.h>
21
22 #define KORE_PGSQL_FORMAT_TEXT 0
23 #define KORE_PGSQL_FORMAT_BINARY 1
24
25 #define KORE_PGSQL_SYNC 0x0001
26 #define KORE_PGSQL_ASYNC 0x0002
27 #define KORE_PGSQL_SCHEDULED 0x0004
28
29 #define KORE_PGSQL_PARAM_BINARY(v, l) v, l, 1
30 #define KORE_PGSQL_PARAM_TEXT_LEN(v, l) v, l, 0
31 #define KORE_PGSQL_PARAM_TEXT(v) v, strlen(v), 0
32
33 #if defined(__cplusplus)
34 extern "C" {
35 #endif
36
37 struct pgsql_conn {
38 struct kore_event evt;
39 u_int8_t flags;
40 char *name;
41
42 PGconn *db;
43 struct pgsql_job *job;
44 TAILQ_ENTRY(pgsql_conn) list;
45 };
46
47 struct pgsql_db {
48 char *name;
49 char *conn_string;
50 u_int16_t conn_max;
51 u_int16_t conn_count;
52
53 LIST_ENTRY(pgsql_db) rlist;
54 };
55
56 struct kore_pgsql {
57 u_int8_t state;
58 int flags;
59 char *error;
60 PGresult *result;
61 struct pgsql_conn *conn;
62
63 struct {
64 char *channel;
65 char *extra;
66 } notify;
67
68 struct http_request *req;
69 void *arg;
70 void (*cb)(struct kore_pgsql *, void *);
71
72 LIST_ENTRY(kore_pgsql) rlist;
73 };
74
75 extern u_int16_t pgsql_conn_max;
76 extern u_int32_t pgsql_queue_limit;
77
78 void kore_pgsql_sys_init(void);
79 void kore_pgsql_sys_cleanup(void);
80 void kore_pgsql_init(struct kore_pgsql *);
81 void kore_pgsql_bind_request(struct kore_pgsql *, struct http_request *);
82 void kore_pgsql_bind_callback(struct kore_pgsql *,
83 void (*cb)(struct kore_pgsql *, void *), void *);
84 int kore_pgsql_setup(struct kore_pgsql *, const char *, int);
85 void kore_pgsql_handle(void *, int);
86 void kore_pgsql_cleanup(struct kore_pgsql *);
87 void kore_pgsql_continue(struct kore_pgsql *);
88 int kore_pgsql_query(struct kore_pgsql *, const void *);
89 int kore_pgsql_query_params(struct kore_pgsql *,
90 const void *, int, int, ...);
91 int kore_pgsql_v_query_params(struct kore_pgsql *,
92 const void *, int, int, va_list);
93 int kore_pgsql_query_param_fields(struct kore_pgsql *, const void *,
94 int, int, const char **, int *, int *);
95 int kore_pgsql_register(const char *, const char *);
96 int kore_pgsql_ntuples(struct kore_pgsql *);
97 int kore_pgsql_nfields(struct kore_pgsql *);
98 void kore_pgsql_logerror(struct kore_pgsql *);
99 char *kore_pgsql_fieldname(struct kore_pgsql *, int);
100 char *kore_pgsql_getvalue(struct kore_pgsql *, int, int);
101 int kore_pgsql_getlength(struct kore_pgsql *, int, int);
102 int kore_pgsql_column_binary(struct kore_pgsql *, int);
103
104 #if defined(__cplusplus)
105 }
106 #endif
107
108 #define KORE_PGSQL_STATE_INIT 1
109 #define KORE_PGSQL_STATE_WAIT 2
110 #define KORE_PGSQL_STATE_RESULT 3
111 #define KORE_PGSQL_STATE_ERROR 4
112 #define KORE_PGSQL_STATE_DONE 5
113 #define KORE_PGSQL_STATE_COMPLETE 6
114 #define KORE_PGSQL_STATE_NOTIFY 7
115
116 #endif