-
Notifications
You must be signed in to change notification settings - Fork 0
/
connection.h
119 lines (99 loc) · 4.19 KB
/
connection.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
#ifndef _JIMCORE_CONNECTION_H_
#define _JIMCORE_CONNECTION_H_
#include "sys_linker_set.h"
extern char *host;
extern char *user;
extern char *passwd;
extern char *db;
extern char *unix_socket;
/* errno for possibly transient connection / database issues */
#define ETRANSIENT EAGAIN
struct stmt_def
{
int id;
char *query;
counter_t calls;
counter_t runtime_ns;
counter_t errors;
};
SET_DECLARE(stmts, struct stmt_def);
typedef struct statement_t statement_t;
struct statement_t
{
struct stmt_def *def;
MYSQL_STMT *s;
};
typedef struct connection_t connection_t;
struct connection_t
{
MYSQL *c;
connection_t *next; /* next in pool */
bool errored;
count_t statement_count;
statement_t stmts[0];
};
/* Get a full connection. */
connection_t *connection_open(void);
/* A connection, without any prepared statements. */
connection_t *connection_open_raw(void);
/* Close a connection; must be called for every connection_open */
void connection_close(connection_t *c);
int _connection_execute(connection_t *c, int id, MYSQL_BIND *params,
statement_t **s_out);
#define connection_execute(__c, __q, __p, __s_out) \
({ \
static struct stmt_def _stmt = { .id = -1, .query = __q }; \
SET_ENTRY(stmts, _stmt); \
_connection_execute((__c), _stmt.id, (__p), (__s_out)); \
})
void connection_error(connection_t *c);
int connection_begin(connection_t *c);
int connection_commit(connection_t *c);
int connection_rollback(connection_t *c);
int connection_query(connection_t *c, char *query);
bool statement_bind_result(statement_t *s, MYSQL_BIND *row);
int statement_fetch(statement_t *s);
void statement_release(statement_t *s);
int connection_commit_or_rollback(connection_t *c, int res);
#define run_with_trx(func, __c, args...) \
({ \
int _r = connection_begin(__c); \
if (_r == 0) { \
_r = func(__c, ##args); \
_r = connection_commit_or_rollback(c, _r); \
} \
_r; \
})
#define run_with_statement(__c, __q, __p, func, args...) \
({ \
statement_t *_s; \
int _r = connection_execute((__c), __q, (__p), &_s); \
if (_r == 0) { \
_r = func(_s, ##args); \
statement_release(_s); \
} \
_r; \
})
int check_update(statement_t *s);
static inline int
_noop_statement(statement_t *s)
{
return 0;
}
#define run_with_connection(func, args...) \
({ \
int _r; \
connection_t *_c = connection_acquire(); \
if (_c) { \
_r = func(_c, ##args); \
if (_r < 0) \
connection_error(_c); \
connection_release(_c); \
} else { \
_r = -ETRANSIENT; \
} \
_r; \
})
#define run_statement(__c, __q, __p) \
run_with_statement(__c, __q, __p, _noop_statement)
#endif