-
Notifications
You must be signed in to change notification settings - Fork 4
/
db.c
159 lines (123 loc) · 3.7 KB
/
db.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
#include "db.h"
#include <gio/gio.h>
#include <string.h>
#include <stdio.h>
gchar *db_path()
{
const gchar *db_path = g_getenv("CDCC_DB");
if (db_path != NULL) {
return g_strdup(db_path);
}
const gchar *user_cache = g_get_user_cache_dir();
return g_build_filename(user_cache, "cdcc.db", NULL);
}
sqlite3 *db_open(const char *path)
{
sqlite3 *db = NULL;
int res = sqlite3_open(path, &db);
if (res != SQLITE_OK) {
g_warning("Cannot open database: %s\n", sqlite3_errmsg(db));
sqlite3_close(db);
return NULL;
}
res = sqlite3_busy_timeout(db, 1000);
if (res != SQLITE_OK) {
g_warning("SQL: Could not set busy timeout");
}
static const char *sql =
"CREATE TABLE IF NOT EXISTS "
"cflags(dir TEXT, file TEXT, flags TEXT, "
"PRIMARY KEY(dir, file) ON CONFLICT REPLACE);";
char *emsg = NULL;
res = sqlite3_exec(db, sql, 0, 0, &emsg);
if (res != SQLITE_OK) {
g_warning("SQL error: %s\n", emsg);
sqlite3_free(emsg);
sqlite3_close(db);
return NULL;
}
return db;
}
void db_close(sqlite3 *db) {
if (db != NULL) {
sqlite3_close(db);
}
}
void db_insert(sqlite3 *db, GFile *dir, GList *files, const gchar * const *argv) {
static const char *sql =
"INSERT INTO cflags(dir, file, flags) VALUES(?, ?, ?);";
sqlite3_stmt *stmt;
int res = sqlite3_prepare_v2(db, sql, strlen(sql), &stmt, NULL);
if (res != SQLITE_OK) {
g_warning("SQL: Could not prepare statement: %s", sqlite3_errmsg(db));
return;
}
g_autofree gchar *cwd = g_file_get_path(dir);
g_autofree gchar *flags = g_strjoinv(" ", (gchar **) argv);
GList *iter;
for (iter = files; iter; iter = iter ->next) {
GFile *f = (GFile *) iter->data;
g_autofree char *abspath = g_file_get_path(f);
res = sqlite3_bind_text(stmt, 1, cwd, strlen(cwd), 0);
if (res != SQLITE_OK) {
g_warning("SQL: could not bind for %s\n", abspath);
continue;
}
res = sqlite3_bind_text(stmt, 2, abspath, strlen(abspath), 0);
if (res != SQLITE_OK) {
g_warning("SQL: could not bind for %s\n", abspath);
continue;
}
res = sqlite3_bind_text(stmt, 3, flags, strlen(flags), 0);
if (res != SQLITE_OK) {
g_warning("SQL: could not bind for %s\n", abspath);
continue;
}
//insert data now
res = sqlite3_step(stmt);
if (res != SQLITE_DONE) {
g_warning("SQL: could not insert for %s\n", abspath);
break;
} else {
//should not fail after a successful call to _step()
sqlite3_reset(stmt);
}
}
}
gboolean
db_query (sqlite3 *db, const char *path, db_query_result_fn fn, gpointer user_data)
{
const char *sql = "SELECT * from cflags WHERE file GLOB ?";
sqlite3_stmt *stmt;
int res = sqlite3_prepare_v2(db, sql, strlen(sql), &stmt, NULL);
if (res != SQLITE_OK) {
g_warning("SQL: Could not prepare statement: %s", sqlite3_errmsg(db));
return FALSE;
}
res = sqlite3_bind_text(stmt, 1, path, strlen(path), 0);
if (res != SQLITE_OK) {
g_warning("SQL: could not bind for %s\n", path);
return FALSE;
}
for (gboolean keep_going = TRUE; keep_going; ) {
res = sqlite3_step(stmt);
if (res == SQLITE_ROW) {
Record rec;
rec.dir = sqlite3_column_text(stmt, 0);
rec.filename = sqlite3_column_text(stmt, 1);
rec.args = sqlite3_column_text(stmt, 2);
if (rec.dir == NULL || rec.filename == NULL || rec.args == NULL) {
fprintf(stderr, "SQL: NULL values in row. skipping");
continue;
}
keep_going = (*fn)(&rec, user_data);
} else if (res == SQLITE_DONE) {
break;
} else {
fprintf(stderr, "SQL: Could not get data: %s\n", sqlite3_errmsg(db));
return FALSE;
}
}
sqlite3_finalize(stmt);
return TRUE;
}