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
15 #include <sys/types.h>
16 #include <sys/socket.h>
18 #include <netinet/in.h>
19 #include <arpa/inet.h>
22 #include <libex/list.h>
23 #include <libex/net.h>
24 #include "pgerrcodes.h"
27 #define EPG_PORT 0x80000000
28 #define EPG_ERRNO 0x40000000
29 #define EPG_PROTO 0x20000000
30 #define EPG_HOST 0x0fffffff
32 #define PG_NRECS(C) C->row_list->len
33 #define PG_NFLDS(C) C->rowdesc->nflds
34 #define PG_FLDLEN(C,R,F) C->rows[R]->fields[F].len
35 #define PG_DATA(C,R,F) C->rows[R]->fields[F].data
36 #define PG_ISNULL(C,R,F) C->rows[R]->fields[F].len >= 0 ? 0 : 1
38 #define CHECK_BOUNDS(C,R,F) (R >= 0 && R < PG_NRECS(C) && F >= 0 && F < PG_NFLDS(C))
46 typedef struct pgpool pgpool_t
;
54 pgmsg_datarow_t
**rows
;
57 pgmsg_rowdesc_t
*rowdesc
;
58 pgmsg_cmd_complete_t
*complete
;
62 char *client_encoding
;
63 char *server_encoding
;
78 void pgconn_init (const char *pg_srv_addr
, const char *pg_srv_service
);
79 pgconn_t
*pg_connect (const char *url
);
80 const int pg_error (pgconn_t
*conn
, pgerror_t
*e
);
81 static inline char pg_ready (pgconn_t
*conn
) {
82 return conn
->ready
? conn
->ready
->tr
: 0;
84 int pg_execsql (pgconn_t
*conn
, const char *sql
, size_t sql_len
);
85 static inline int pg_start (pgconn_t
* conn
) {
86 return pg_execsql(conn
, CONST_STR_LEN("start transaction"));
88 static inline int pg_commit (pgconn_t
*conn
) {
89 return pg_execsql(conn
, CONST_STR_LEN("commit"));
91 static inline int pg_rollback (pgconn_t
*conn
) {
92 return pg_execsql(conn
, CONST_STR_LEN("rollback"));
94 int pg_prepareln (pgconn_t
*conn
, const char *name
, size_t name_len
,
95 const char *sql
, size_t sql_len
, int fld_len
, pgfld_t
**flds
);
96 static inline int pg_preparel (pgconn_t
*conn
, const char *sql
, size_t sql_len
, int fld_len
, pgfld_t
**flds
) {
97 return pg_prepareln(conn
, CONST_STR_NULL
, sql
, sql_len
, fld_len
, flds
);
99 int pg_preparen (pgconn_t
*conn
, const char *name
, size_t name_len
,
100 const char *sql
, size_t sql_len
, pgfld_t
*fld
, ...);
101 int pg_prepare (pgconn_t
*conn
, const char *sql
, size_t sql_len
, pgfld_t
*fld
, ...);
102 int pg_execln (pgconn_t
*conn
, const char *portal
, size_t portal_len
,
103 const char *stmt
, size_t stmt_len
,
104 int fld_len
, pgfld_t
**flds
, int res_fmt_len
, int *res_fmt
, int max_data
);
105 static inline int pg_execl (pgconn_t
*conn
, int fld_len
, pgfld_t
**flds
, int res_fmt_len
, int *res_fmt
, int max_data
) {
106 return pg_execln(conn
, CONST_STR_NULL
, CONST_STR_NULL
, fld_len
, flds
, res_fmt_len
, res_fmt
, max_data
);
108 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
);
109 static inline int pg_execv (pgconn_t
*conn
, int max_data
, int out_fmt
, pgfld_t
*fld
, va_list ap
) {
110 return pg_execvn(conn
, CONST_STR_NULL
, CONST_STR_NULL
, max_data
, out_fmt
, fld
, ap
);
112 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
, ...);
113 int pg_exec (pgconn_t
*conn
, int max_data
, int out_fmt
, pgfld_t
*fld
, ...);
114 int pg_nextn (pgconn_t
*conn
, const char *portal
, size_t portal_len
, int max_data
);
115 static inline int pg_next (pgconn_t
*conn
, int max_data
) {
116 return pg_nextn(conn
, CONST_STR_NULL
, max_data
);
118 static inline char pg_getready (pgconn_t
*conn
) {
119 return conn
->ready
? conn
->ready
->tr
: 0;
121 int pg_release (pgconn_t
*conn
, const char *name
, size_t name_len
);
122 void pg_close (pgconn_t
*conn
);
123 void pg_disconnect(pgconn_t
*conn
);
125 static inline int pg_nrecs (pgconn_t
*conn
) { return PG_NRECS(conn
); };
126 static inline int pg_nflds (pgconn_t
*conn
) { return conn
->rowdesc
? PG_NFLDS(conn
) : 0; };
127 static inline int pg_isnull (pgconn_t
*conn
, int row
, int col
) { return PG_ISNULL(conn
, row
, col
); };
129 #define pg_getdate(C,R,F) pg_geti32(C,R,F)
130 #define pg_gettm(C,R,F) pg_geti64(C,R,F)
131 const strptr_t
pg_get (pgconn_t
*conn
, int row
, int col
);
132 static inline int pg_fldlen (pgconn_t
*conn
, int row
, int col
) {
133 assert(CHECK_BOUNDS(conn
, row
, col
));
134 return PG_FLDLEN(conn
, row
, col
);
136 cstr_t
*pg_getstr (pgconn_t
*conn
, int row
, int col
);
137 static inline int16_t pg_geti16 (pgconn_t
*conn
, int row
, int col
) {
138 assert(CHECK_BOUNDS(conn
, row
, col
));
139 return be16toh(*(int16_t*)PG_DATA(conn
, row
, col
));
142 static inline int32_t pg_geti32 (pgconn_t
*conn
, int row
, int col
) {
143 assert(CHECK_BOUNDS(conn
, row
, col
));
144 return be32toh(*(int32_t*)PG_DATA(conn
, row
, col
));
146 static inline int64_t pg_geti64 (pgconn_t
*conn
, int row
, int col
) {
147 assert(CHECK_BOUNDS(conn
, row
, col
));
148 return be64toh(*(int64_t*)PG_DATA(conn
, row
, col
));
150 static inline double pg_getd (pgconn_t
*conn
, int row
, int col
) {
151 assert(CHECK_BOUNDS(conn
, row
, col
));
152 return pg_conv_double(*(double*)PG_DATA(conn
, row
, col
));
154 static inline float pg_getf (pgconn_t
*conn
, int row
, int col
) {
155 assert(CHECK_BOUNDS(conn
, row
, col
));
156 return pg_conv_float(*(float*)PG_DATA(conn
, row
, col
));
158 static inline int8_t pg_getbool (pgconn_t
*conn
, int row
, int col
) {
159 assert(CHECK_BOUNDS(conn
, row
, col
));
160 return *PG_DATA(conn
, row
, col
);
162 uint32_t pg_getx32 (pgconn_t
*conn
, int row
, int col
);
163 pg_intv_t
pg_getintv (pgconn_t
*conn
, int row
, int col
);
164 int32_t pg_getinet4 (pgconn_t
*conn
, int row
, int col
);
166 static inline void pg_getnum (pgconn_t
*conn
, int row
, int col
, mpq_t res
, uint16_t *dscale
) {
167 assert(CHECK_BOUNDS(conn
, row
, col
));
168 pg_get_numeric(PG_DATA(conn
, row
, col
), res
, dscale
);
171 static inline unsigned char *pg_getuuid (pgconn_t
*conn
, int row
, int col
) {
172 assert(CHECK_BOUNDS(conn
, row
, col
));
173 return PG_DATA(conn
, row
, col
);