1 /*Copyright (c) Brian B.
3 This library is free software; you can redistribute it and/or
4 modify it under the terms of the GNU Lesser General Public
5 License as published by the Free Software Foundation; either
6 version 3 of the License, or (at your option) any later version.
7 See the file LICENSE included with this distribution for more
14 #include <sys/types.h>
15 #include <sys/socket.h>
17 #include <netinet/in.h>
18 #include <arpa/inet.h>
21 #include <libex/net.h>
22 #include <libex/endian.h>
23 #include "pgerrcodes.h"
26 #define EPG_PORT 0x80000000
27 #define EPG_ERRNO 0x40000000
28 #define EPG_PROTO 0x20000000
29 #define EPG_HOST 0x0fffffff
31 #define PG_NRECS(C) C->row_list.len
32 #define PG_NFLDS(C) C->rowdesc->nflds
33 #define PG_FLDLEN(C,R,F) C->rows[R]->fields[F].len
34 #define PG_DATA(C,R,F) C->rows[R]->fields[F].data
35 #define PG_ISNULL(C,R,F) C->rows[R]->fields[F].len >= 0 ? 0 : 1
36 #define PG_TYPE(C,F) C->rowdesc->fields[F].oid_field
38 #define CHECK_BOUNDS(C,R,F) (R >= 0 && R < PG_NRECS(C) && F >= 0 && F < PG_NFLDS(C))
47 typedef struct pgpool pgpool_t
;
48 typedef struct pgconn pgconn_t
;
55 pgmsg_datarow_list_t row_list
;
58 pgmsg_datarow_t
**rows
;
61 pgmsg_rowdesc_t
*rowdesc
;
62 pgmsg_cmd_complete_t
*complete
;
66 char *client_encoding
;
67 char *server_encoding
;
86 void pgconn_init (const char *pg_srv_addr
, const char *pg_srv_service
);
87 pgconn_t
*pg_connect (const char *url
);
89 const int pg_error (pgconn_t
*conn
, pgerror_t
*e
);
91 int pg_error (pgconn_t
*conn
, pgerror_t
*e
);
93 static inline char pg_ready (pgconn_t
*conn
) {
94 return conn
->ready
? conn
->ready
->tr
: 0;
96 int pg_execsql (pgconn_t
*conn
, const char *sql
, size_t sql_len
);
97 int pg_copyin (pgconn_t
*conn
, const char *table_name
);
98 int pg_copyin_sendln (pgconn_t
*conn
, pgfld_t
**flds
);
99 int pg_copyin_end (pgconn_t
*conn
);
100 static inline int pg_start (pgconn_t
* conn
) {
101 return pg_execsql(conn
, CONST_STR_LEN("start transaction"));
103 static inline int pg_commit (pgconn_t
*conn
) {
104 return pg_execsql(conn
, CONST_STR_LEN("commit"));
106 static inline int pg_rollback (pgconn_t
*conn
) {
107 return pg_execsql(conn
, CONST_STR_LEN("rollback"));
109 int pg_prepareln (pgconn_t
*conn
, const char *name
, size_t name_len
,
110 const char *sql
, size_t sql_len
, int fld_len
, pgfld_t
**flds
);
111 static inline int pg_preparel (pgconn_t
*conn
, const char *sql
, size_t sql_len
, int fld_len
, pgfld_t
**flds
) {
112 return pg_prepareln(conn
, CONST_STR_NULL
, sql
, sql_len
, fld_len
, flds
);
114 int pg_preparen (pgconn_t
*conn
, const char *name
, size_t name_len
,
115 const char *sql
, size_t sql_len
, pgfld_t
*fld
, ...);
116 int pg_prepare (pgconn_t
*conn
, const char *sql
, size_t sql_len
, pgfld_t
*fld
, ...);
117 int pg_execln (pgconn_t
*conn
, const char *portal
, size_t portal_len
,
118 const char *stmt
, size_t stmt_len
,
119 int fld_len
, pgfld_t
**flds
, int res_fmt_len
, int *res_fmt
, int max_data
);
120 static inline int pg_execl (pgconn_t
*conn
, int fld_len
, pgfld_t
**flds
, int res_fmt_len
, int *res_fmt
, int max_data
) {
121 return pg_execln(conn
, CONST_STR_NULL
, CONST_STR_NULL
, fld_len
, flds
, res_fmt_len
, res_fmt
, max_data
);
123 int pg_execvn (pgconn_t
*conn
, const char *portal
, size_t portal_len
, const char *stmt
, size_t stmt_len
, int max_data
, int fmt_out
, pgfld_t
*fld
, va_list ap
);
124 static inline int pg_execv (pgconn_t
*conn
, int max_data
, int out_fmt
, pgfld_t
*fld
, va_list ap
) {
125 return pg_execvn(conn
, CONST_STR_NULL
, CONST_STR_NULL
, max_data
, out_fmt
, fld
, ap
);
127 int pg_execn (pgconn_t
*conn
, const char *portal
, size_t portal_len
, const char *stmt
, size_t stmt_len
, int max_data
, int out_fmt
, pgfld_t
*fld
, ...);
128 int pg_exec (pgconn_t
*conn
, int max_data
, int out_fmt
, pgfld_t
*fld
, ...);
129 int pg_nextn (pgconn_t
*conn
, const char *portal
, size_t portal_len
, int max_data
);
130 static inline int pg_next (pgconn_t
*conn
, int max_data
) {
131 return pg_nextn(conn
, CONST_STR_NULL
, max_data
);
133 static inline char pg_getready (pgconn_t
*conn
) {
134 return conn
->ready
? conn
->ready
->tr
: 0;
136 int pg_release (pgconn_t
*conn
, const char *name
, size_t name_len
);
137 void pg_close (pgconn_t
*conn
);
138 void pg_disconnect(pgconn_t
*conn
);
140 static inline int pg_nrecs (pgconn_t
*conn
) { return PG_NRECS(conn
); };
141 static inline int pg_nflds (pgconn_t
*conn
) { return conn
->rowdesc
? PG_NFLDS(conn
) : 0; };
142 static inline int pg_isnull (pgconn_t
*conn
, int row
, int col
) { return PG_ISNULL(conn
, row
, col
); };
144 #define pg_getdate(C,R,F) pg_geti32(C,R,F)
145 #define pg_gettm(C,R,F) pg_geti64(C,R,F)
147 const strptr_t
pg_get (pgconn_t
*conn
, int row
, int col
);
149 strptr_t
pg_get (pgconn_t
*conn
, int row
, int col
);
151 static inline int pg_fldlen (pgconn_t
*conn
, int row
, int col
) {
152 assert(CHECK_BOUNDS(conn
, row
, col
));
153 return PG_FLDLEN(conn
, row
, col
);
155 cstr_t
*pg_getstr (pgconn_t
*conn
, int row
, int col
);
156 static inline int16_t pg_geti16 (pgconn_t
*conn
, int row
, int col
) {
157 assert(CHECK_BOUNDS(conn
, row
, col
));
158 return be16toh(*(int16_t*)PG_DATA(conn
, row
, col
));
161 static inline int32_t pg_geti32 (pgconn_t
*conn
, int row
, int col
) {
162 assert(CHECK_BOUNDS(conn
, row
, col
));
163 return be32toh(*(int32_t*)PG_DATA(conn
, row
, col
));
165 static inline int64_t pg_geti64 (pgconn_t
*conn
, int row
, int col
) {
166 assert(CHECK_BOUNDS(conn
, row
, col
));
167 return be64toh(*(int64_t*)PG_DATA(conn
, row
, col
));
169 static inline double pg_getd (pgconn_t
*conn
, int row
, int col
) {
170 assert(CHECK_BOUNDS(conn
, row
, col
));
171 return pg_conv_double(*(double*)PG_DATA(conn
, row
, col
));
173 static inline float pg_getf (pgconn_t
*conn
, int row
, int col
) {
174 assert(CHECK_BOUNDS(conn
, row
, col
));
175 return pg_conv_float(*(float*)PG_DATA(conn
, row
, col
));
177 static inline int8_t pg_getbool (pgconn_t
*conn
, int row
, int col
) {
178 assert(CHECK_BOUNDS(conn
, row
, col
));
179 return *PG_DATA(conn
, row
, col
);
181 uint32_t pg_getx32 (pgconn_t
*conn
, int row
, int col
);
182 pg_intv_t
pg_getintv (pgconn_t
*conn
, int row
, int col
);
183 int32_t pg_getinet4 (pgconn_t
*conn
, int row
, int col
);
185 static inline void pg_getnum (pgconn_t
*conn
, int row
, int col
, mpq_t res
, uint16_t *dscale
) {
186 assert(CHECK_BOUNDS(conn
, row
, col
));
187 pg_get_numeric(PG_DATA(conn
, row
, col
), res
, dscale
);
190 static inline unsigned char *pg_getuuid (pgconn_t
*conn
, int row
, int col
) {
191 assert(CHECK_BOUNDS(conn
, row
, col
));
192 return PG_DATA(conn
, row
, col
);