-
Notifications
You must be signed in to change notification settings - Fork 1
/
db_api.c
274 lines (237 loc) · 6.27 KB
/
db_api.c
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
/*
DB API
*/
#include "csync2.h"
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <time.h>
#include "db_api.h"
#include "db_mysql.h"
#include "db_postgres.h"
#include "db_sqlite.h"
#include "db_sqlite2.h"
#define DEADLOCK_MESSAGE \
"Database backend is exceedingly busy => Terminating (requesting retry).\n"
int db_detect_type(const char **db_str, int type)
{
/* *INDENT-OFF* */
const char *db_types[] = { "mysql://", "sqlite://", "sqlite3://", "sqlite2://", "pgsql://", 0 };
const int types[] = { DB_MYSQL, DB_SQLITE3, DB_SQLITE3, DB_SQLITE2, DB_PGSQL, DB_UNKNOWN_SCHEME };
/* *INDENT-ON* */
int index;
if (*db_str[0] == '/')
return DB_SQLITE3;
for (index = 0; 1; index++) {
if (db_types[index] == 0)
break;
if (!strncmp(*db_str, db_types[index], strlen(db_types[index]))) {
*db_str += strlen(db_types[index]);
return types[index];
}
}
if (strstr(*db_str, "://"))
return DB_UNKNOWN_SCHEME;
return type;
}
/* expects the "scheme://" prefix to be removed already!
* eg. if full url had been
* mysql://user:password@host:port/db
* input url to this function:
* user:password@host:port/db
* Note:
* if no database is specified, it will default to
* csync2_<thishostname>_<configname>
* or csync2_<thishostname>, if no explicit config name is used.
*
* Note that the output arguments *host, *user, *pass and *database will be initialized.
* *port should be initialized to the default port before calling this function.
*
* TODO:
* add support for unix domain sockets?
**/
void csync_parse_url(char *url, char **host, char **user, char **pass, char **database, unsigned int *port)
{
char *pos = strchr(url, '@');
if (pos) {
/* Optional user/passwd */
*pos = '\0';
*user = url;
url = pos + 1;
/* password */
pos = strchr(*user, ':');
if (pos) {
*pos = '\0';
*pass = pos + 1;
} else
*pass = NULL;
} else {
/* No user:pass@ */
*user = NULL;
*pass = NULL;
}
*host = url;
pos = strchr(*host, '/');
if (pos) {
// Database
*pos = '\0';
*database = pos + 1;
} else
*database = NULL;
pos = strchr(*host, ':');
if (pos) {
*pos = '\0';
*port = atoi(pos + 1);
}
if (!*database || !*database[0])
ASPRINTF(database, "csync2_%s%s%s", myhostname, cfgname[0] ? "_" : "", cfgname);
/* I just don't want to know about special characters,
* or differences between db engines. */
for (pos = *database; *pos; pos++) {
switch (*pos) {
case 'a' ... 'z':
case 'A' ... 'Z':
case '0' ... '9':
case '_':
break;
default:
*pos = '_';
}
}
}
int db_open(const char *db_str, int type, db_conn_p * db)
{
int rc = DB_ERROR;
type = db_detect_type(&db_str, type);
if (type == DB_SQLITE2 || type == DB_SQLITE3) {
struct stat sbuf;
if (stat(db_str, &sbuf) == 0 && S_ISDIR(sbuf.st_mode)) {
/* trim trailing slashes; don't trim "/". */
size_t len = strlen(db_str);
char *tmp = strdup(db_str);
while (len > 1 && tmp[--len] == '/')
tmp[len] = '\0';
ASPRINTF((char**)&db_str, "%s/%s%s%s.db%s",
tmp, myhostname, cfgname[0] ? "_" : "", cfgname, (type == DB_SQLITE3) ? "3" : "");
free(tmp);
}
}
/* Switch between implementation */
switch (type) {
case DB_UNKNOWN_SCHEME:
csync_fatal("Unknown database scheme: %s\n", db_str);
break;
case DB_SQLITE2:
rc = db_sqlite2_open(db_str, db);
if (rc != DB_OK && db_str[0] != '/')
fprintf(csync_debug_out,
"Cannot open database file: %s, maybe you need three slashes (like sqlite:///var/lib/csync2/csync2.db)\n",
db_str);
break;
case DB_SQLITE3:
rc = db_sqlite_open(db_str, db);
if (rc != DB_OK && db_str[0] != '/')
fprintf(csync_debug_out,
"Cannot open database file: %s, maybe you need three slashes (like sqlite:///var/lib/csync2/csync2.db)\n",
db_str);
break;
case DB_MYSQL:
#ifdef HAVE_MYSQL
rc = db_mysql_open(db_str, db);
#else
csync_fatal("No Mysql support configured. Please reconfigure with --enable-mysql (database is %s).\n", db_str);
#endif
break;
case DB_PGSQL:
#ifdef HAVE_POSTGRES
rc = db_postgres_open(db_str, db);
#else
csync_fatal("No Postgres SQL support configured. Please reconfigure with --enable-postgres (database is %s).\n",
db_str);
#endif
break;
default:
csync_fatal("Database type not found. Can't open database %s\n", db_str);
rc = DB_ERROR;
}
if (*db)
(*db)->logger = 0;
return rc;
}
void db_set_logger(db_conn_p conn, void (*logger) (int lv, const char *fmt, ...))
{
if (conn == NULL)
csync_fatal("No connection in set_logger.\n");
conn->logger = logger;
}
void db_close(db_conn_p conn)
{
if (!conn || !conn->close)
return;
conn->close(conn);
}
const char *db_errmsg(db_conn_p conn)
{
if (conn && conn->errmsg)
return conn->errmsg(conn);
return "(no error message function available)";
}
int db_exec(db_conn_p conn, const char *sql)
{
if (conn && conn->exec)
return conn->exec(conn, sql);
csync_debug(0, "No exec function in db_exec.\n");
return DB_ERROR;
}
int db_prepare_stmt(db_conn_p conn, const char *sql, db_stmt_p * stmt, char **pptail)
{
if (conn && conn->prepare)
return conn->prepare(conn, sql, stmt, pptail);
csync_debug(0, "No prepare function in db_prepare_stmt.\n");
return DB_ERROR;
}
const char *db_stmt_get_column_text(db_stmt_p stmt, int column)
{
if (stmt && stmt->get_column_text)
return stmt->get_column_text(stmt, column);
csync_debug(0, "No stmt in db_stmt_get_column_text / no function.\n");
return NULL;
}
int db_stmt_get_column_int(db_stmt_p stmt, int column)
{
if (stmt && stmt->get_column_int)
return stmt->get_column_int(stmt, column);
csync_debug(0, "No stmt in db_stmt_get_column_int / no function.\n");
return 0;
}
int db_stmt_next(db_stmt_p stmt)
{
if (stmt && stmt->next)
return stmt->next(stmt);
csync_debug(0, "No stmt in db_stmt_next / no function.\n");
return DB_ERROR;
}
int db_stmt_close(db_stmt_p stmt)
{
if (stmt && stmt->close)
return stmt->close(stmt);
csync_debug(0, "No stmt in db_stmt_close / no function.\n");
return DB_ERROR;
}
int db_schema_version(db_conn_p db)
{
int version = -1;
SQL_BEGIN(NULL, /* ignore errors */
"SELECT count(*) from file") {
version = 0;
} SQL_END;
return version;
}
int db_upgrade_to_schema(db_conn_p db, int version)
{
if (db && db->upgrade_to_schema)
return db->upgrade_to_schema(version);
return DB_ERROR;
}