-
Notifications
You must be signed in to change notification settings - Fork 0
/
dbfunc.h
68 lines (44 loc) · 1.5 KB
/
dbfunc.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
#ifndef DBFUNC_H
#define DBFUNC_H
#include <libpq-fe.h>
#include "xtypes.h"
#define PREPARE_CHECK_SUCCESS(name, retval) \
if (PQresultStatus(res) != PGRES_COMMAND_OK) \
{ \
fprintf(stderr, "[" #name "]: Query preparation failed\xa"); \
PQclear(res); \
FINISH_CONN_IFSTARTED \
return retval; \
}
#define EXEC_PREPARED_CHECK_SUCCESS(name, retval, type) \
if (PQresultStatus(res) != PGRES_ ## type ## _OK) \
{ \
fprintf(stderr, "[" #name "]: Query failed: %s\xa", \
ExecStatusType_to_cstr(PQresultStatus(res))); \
PQclear(res); \
FINISH_CONN_IFSTARTED \
return retval; \
}
#define START_CONN_IFNOT(retval) \
int __new_conn_created = 0; \
if (!conn) \
{ \
__new_conn_created = 1; \
conn = new_conn(); \
if (!conn) \
return retval; \
}
#define FINISH_CONN_IFSTARTED \
if (__new_conn_created) \
PQfinish(conn);
PGconn *
new_conn();
char const *
ConnStatusType_to_cstr(
ConnStatusType status
);
char const *
ExecStatusType_to_cstr(
ExecStatusType status
);
#endif /* DBFUNC_H */