-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathdb_api.c
More file actions
181 lines (146 loc) · 4.26 KB
/
db_api.c
File metadata and controls
181 lines (146 loc) · 4.26 KB
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
/*
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"
#define DEADLOCK_MESSAGE \
"Database backend is exceedingly busy => Terminating (requesting retry).\n"
int db_sqlite_open(const char *file, db_conn_p *db);
int db_mysql_open(const char *file, db_conn_p *db);
int db_detect_type(const char **db_str, int type) {
const char *db_types[] = { "mysql://", "sqlite3://", "sqlite2://", "pgsql://", 0 };
int types[] = { DB_MYSQL, DB_SQLITE3, DB_SQLITE2, DB_PGSQL };
int index;
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];
}
}
return type;
}
int db_open(const char *file, int type, db_conn_p *db)
{
int rc = DB_ERROR;
const char *db_str;
db_str = file;
type = db_detect_type(&db_str, type);
/* Switch between implementation */
switch (type) {
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;
#ifdef HAVE_LIBMYSQLCLIENT
case DB_MYSQL:
rc = db_mysql_open(db_str, db);
break;
#else
case DB_MYSQL:
csync_fatal("No Mysql support configured. Please reconfigure with --enable-mysql (database is %s).\n", file);
rc = DB_ERROR;
break;
#endif
#ifdef HAVE_LIBPQ
case DB_PGSQL:
rc = db_pgsql_open(db_str, db);
break;
#else
case DB_PGSQL:
csync_fatal("No Postgres SQL support configured. Please reconfigure with --enable-postgres (database is %s).\n", file);
rc = DB_ERROR;
break;
#endif
default:
csync_fatal("Database type not found. Can't open database %s\n", file);
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;
}