fix
[libpgclient.git] / include / libpgcli / pgconn.h
blob51db8e610ca6c6d6167f4648f4a1dd41e2562e2f
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
8 information.
9 */
10 #ifndef __PGCONN_H__
11 #define __PGCONN_H__
13 #include <unistd.h>
14 #include <sys/types.h>
15 #include <sys/socket.h>
16 #include <netdb.h>
17 #include <netinet/in.h>
18 #include <arpa/inet.h>
19 #include <limits.h>
20 #include <assert.h>
21 #include <libex/net.h>
22 #include <libex/endian.h>
23 #include "pgerrcodes.h"
24 #include "pgprov3.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))
40 typedef struct {
41 char *code;
42 char *text;
43 char *msg;
44 char *detail;
45 } pgerror_t;
47 typedef struct pgpool pgpool_t;
48 typedef struct pgconn pgconn_t;
50 struct pgconn {
51 pgconn_t *prev;
52 pgconn_t *next;
53 int fd;
54 pgmsg_list_t msgs;
55 pgmsg_datarow_list_t row_list;
56 int nflds;
57 uint32_t intr_error;
58 pgmsg_datarow_t **rows;
59 pgmsg_error_t *error;
60 pgmsg_ready_t *ready;
61 pgmsg_rowdesc_t *rowdesc;
62 pgmsg_cmd_complete_t *complete;
63 pgmsg_t *suspended;
64 pgmsg_t *emptyquery;
65 char *date_style;
66 char *client_encoding;
67 char *server_encoding;
68 char *session_auth;
69 char *timezone;
70 int is_int_datetimes;
71 int is_superuser;
72 int major_ver;
73 int minor_ver;
74 int authok;
75 char *dbname;
76 int cols;
77 int fmt;
78 cstr_t *nonce;
79 time_t tm_released;
80 pgpool_t *pool;
83 #ifndef __GNUC__
84 void pg_init ();
85 #endif
86 void pgconn_init (const char *pg_srv_addr, const char *pg_srv_service);
87 pgconn_t *pg_connect (const char *url);
88 #ifdef __GNUC__
89 const int pg_error (pgconn_t *conn, pgerror_t *e);
90 #else
91 int pg_error (pgconn_t *conn, pgerror_t *e);
92 #endif
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)
146 #ifdef __GNUC__
147 const strptr_t pg_get (pgconn_t *conn, int row, int col);
148 #else
149 strptr_t pg_get (pgconn_t *conn, int row, int col);
150 #endif
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);
184 #ifdef HAVE_GMP
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);
189 #endif
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);
195 #endif