-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdbx.h
173 lines (117 loc) · 4.82 KB
/
dbx.h
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
/* dbx.h : header file of asynchronous PostgreSQL Database Engine
* */
#ifndef _CSTUFF_DBX_H_
#define _CSTUFF_DBX_H_
#include <stdint.h>
#include <stdarg.h>
#include <stdbool.h>
#include <string.h>
#include <time.h>
#include <libpq-fe.h>
#include "retcodes.h"
/* -------------------------------------------------------------------------- */
typedef enum
{
DBX_INT32,
DBX_UINT32,
DBX_INT64,
DBX_UINT64,
DBX_FLOAT,
DBX_TIMESTAMP,
DBX_STRING,
DBX_CONSTANT,
DBX_MD5_HASH,
DBX_STATEMENT,
DBX_BOOLEAN
} dbx_param_t;
/* -------------------------------------------------------------------------- */
#define DBX_FLAG_RECONNECT (1<<0)
#define DBX_FLAG_FREE_SQL (1<<0)
#define DBX_FLAG_TRANSACTION (1<<1)
/* -------------------------------------------------------------------------- */
typedef bool /* true - free result, false - keep result */
(*dbx_on_result_t)( PGresult * result,
int res_i,
void * u_data);
/* -------------------------------------------------------------------------- */
typedef void
(*dbx_on_error_t)( const char * e_message,
int res_i,
void * u_data,
const char * e_sql );
/* -------------------------------------------------------------------------- */
#define dbx_escape(x) PQescapeLiteral(dbxConn, x, strlen(x))
#define dbx_free(x) PQfreemem(x)
/* -------------------------------------------------------------------------- */
cstuff_retcode_t
dbx_init( const char * username,
const char * password,
const char * database,
const char * hostname,
int port,
int connections );
/* -------------------------------------------------------------------------- */
void
dbx_release();
/* -------------------------------------------------------------------------- */
cstuff_retcode_t
dbx_touch();
/* -------------------------------------------------------------------------- */
char *
dbx_sql_format(const char * sql_format, int count, ...);
/* -------------------------------------------------------------------------- */
char *
dbx_sql_vformat(const char * sql_format, int count, va_list args );
/* -------------------------------------------------------------------------- */
uint64_t
dbx_query_format( const char * sql_format,
dbx_on_result_t on_result,
dbx_on_error_t on_error,
void * u_data,
int p_count,
/* dbx_param_t param_type,
* __TYPE__ param, */
... );
/* -------------------------------------------------------------------------- */
uint64_t
dbx_query_const( const char * sql,
dbx_on_result_t on_result,
dbx_on_error_t on_error,
void * u_data );
/* -------------------------------------------------------------------------- */
uint64_t
dbx_query_transaction( const char * sql,
dbx_on_result_t on_result,
dbx_on_error_t on_error,
void * u_data );
/* -------------------------------------------------------------------------- */
cstuff_retcode_t
dbx_cancel(uint64_t id);
/* -------------------------------------------------------------------------- */
const char *
dbx_get_error();
/* -------------------------------------------------------------------------- */
int
dbx_ready_connections_count();
/* -------------------------------------------------------------------------- */
#define dbx_as_string( pg_result, row_num, col_num ) \
PQgetvalue(pg_result, row_num, col_num)
/* -------------------------------------------------------------------------- */
#define dbx_as_bool( pg_result, row_num, col_num ) \
((PQgetvalue(pg_result, row_num, col_num)[0]=='t') ? true : false)
/* -------------------------------------------------------------------------- */
#define dbx_as_integer( pg_result, row_num, col_num ) \
strtoll(PQgetvalue(pg_result, row_num, col_num), NULL, 10)
/* -------------------------------------------------------------------------- */
#define dbx_as_datetime( pg_result, dt_ptr, row_num, col_num ) \
datetime_from_string( \
dt_ptr, PQgetvalue(pg_result, row_num, col_num),DATETIME_NTS,"UTC" \
)
/* -------------------------------------------------------------------------- */
int
dbx_as_timestamp( PGresult * result, int row_num, int col_num, time_t * p_ts );
/* -------------------------------------------------------------------------- */
int
dbx_sleep(int usec);
/* -------------------------------------------------------------------------- */
#endif