-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathsqlite.c
277 lines (221 loc) · 7.6 KB
/
sqlite.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
275
276
277
/*
* This file is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License, version 2,
* as published by the Free Software Foundation.
*
* In addition to the permissions in the GNU General Public License,
* the authors give you unlimited permission to link the compiled
* version of this file into combinations with other programs,
* and to distribute those combinations without any restriction
* coming from the use of this file. (The General Public License
* restrictions do apply in other respects; for example, they cover
* modification of the file, and distribution when not linked into
* a combined executable.)
*
* This file is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; see the file COPYING. If not, write to
* the Free Software Foundation, 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include <assert.h>
#include <string.h>
#include <git2.h>
#include <git2/sys/odb_backend.h>
#include <sqlite3.h>
#define GIT2_TABLE_NAME "git2_odb"
typedef struct {
git_odb_backend parent;
sqlite3 *db;
sqlite3_stmt *st_read;
sqlite3_stmt *st_write;
sqlite3_stmt *st_read_header;
} sqlite_backend;
int sqlite_backend__read_header(size_t *len_p, git_otype *type_p, git_odb_backend *_backend, const git_oid *oid)
{
sqlite_backend *backend;
int error;
assert(len_p && type_p && _backend && oid);
backend = (sqlite_backend *)_backend;
error = GIT_ERROR;
if (sqlite3_bind_blob(backend->st_read_header, 1, oid->id, sizeof(oid->id), SQLITE_STATIC) == SQLITE_OK) {
if (sqlite3_step(backend->st_read_header) == SQLITE_ROW) {
*type_p = (git_otype)sqlite3_column_int(backend->st_read_header, 0);
*len_p = (size_t)sqlite3_column_int(backend->st_read_header, 1);
assert(sqlite3_step(backend->st_read_header) == SQLITE_DONE);
error = GIT_OK;
} else {
error = GIT_ENOTFOUND;
}
}
sqlite3_reset(backend->st_read_header);
return error;
}
int sqlite_backend__read(void **data_p, size_t *len_p, git_otype *type_p, git_odb_backend *_backend, const git_oid *oid)
{
sqlite_backend *backend;
int error;
assert(data_p && len_p && type_p && _backend && oid);
backend = (sqlite_backend *)_backend;
error = GIT_ERROR;
if (sqlite3_bind_blob(backend->st_read, 1, oid->id, sizeof(oid->id), SQLITE_STATIC) == SQLITE_OK) {
if (sqlite3_step(backend->st_read) == SQLITE_ROW) {
*type_p = (git_otype)sqlite3_column_int(backend->st_read, 0);
*len_p = (size_t)sqlite3_column_int(backend->st_read, 1);
*data_p = git_odb_backend_malloc( _backend, *len_p );
if (*data_p == NULL) {
giterr_set_oom();
error = GIT_ERROR;
} else {
memcpy(*data_p, sqlite3_column_blob(backend->st_read, 2), *len_p);
error = GIT_OK;
}
assert(sqlite3_step(backend->st_read) == SQLITE_DONE);
} else {
error = GIT_ENOTFOUND;
}
}
sqlite3_reset(backend->st_read);
return error;
}
int sqlite_backend__read_prefix(git_oid *out_oid, void **data_p, size_t *len_p, git_otype *type_p, git_odb_backend *_backend,
const git_oid *short_oid, size_t len) {
if (len >= GIT_OID_HEXSZ) {
/* Just match the full identifier */
int error = sqlite_backend__read(data_p, len_p, type_p, _backend, short_oid);
if (error == GIT_OK)
git_oid_cpy(out_oid, short_oid);
return error;
}
/* not implemented (yet) */
return GIT_ERROR;
}
int sqlite_backend__exists(git_odb_backend *_backend, const git_oid *oid)
{
sqlite_backend *backend;
int found;
assert(_backend && oid);
backend = (sqlite_backend *)_backend;
found = 0;
if (sqlite3_bind_blob(backend->st_read_header, 1, oid->id, sizeof(oid->id), SQLITE_STATIC) == SQLITE_OK) {
if (sqlite3_step(backend->st_read_header) == SQLITE_ROW) {
found = 1;
assert(sqlite3_step(backend->st_read_header) == SQLITE_DONE);
}
}
sqlite3_reset(backend->st_read_header);
return found;
}
int sqlite_backend__write(git_odb_backend *_backend, const git_oid *id, const void *data, size_t len, git_otype type)
{
int error;
sqlite_backend *backend;
assert(id && _backend && data);
backend = (sqlite_backend *)_backend;
error = SQLITE_ERROR;
if (sqlite3_bind_blob(backend->st_write, 1, id->id, sizeof(id->id), SQLITE_STATIC) == SQLITE_OK &&
sqlite3_bind_int(backend->st_write, 2, (int)type) == SQLITE_OK &&
sqlite3_bind_int(backend->st_write, 3, len) == SQLITE_OK &&
sqlite3_bind_blob(backend->st_write, 4, data, len, SQLITE_STATIC) == SQLITE_OK) {
error = sqlite3_step(backend->st_write);
}
sqlite3_reset(backend->st_write);
return (error == SQLITE_DONE) ? GIT_OK : GIT_ERROR;
}
void sqlite_backend__free(git_odb_backend *_backend)
{
sqlite_backend *backend;
assert(_backend);
backend = (sqlite_backend *)_backend;
sqlite3_finalize(backend->st_read);
sqlite3_finalize(backend->st_read_header);
sqlite3_finalize(backend->st_write);
sqlite3_close(backend->db);
free(backend);
}
static int create_table(sqlite3 *db)
{
static const char *sql_creat =
"CREATE TABLE '" GIT2_TABLE_NAME "' ("
"'oid' BLOB(20) PRIMARY KEY NOT NULL,"
"'type' INTEGER NOT NULL,"
"'size' INTEGER NOT NULL,"
"'data' BLOB);";
if (sqlite3_exec(db, sql_creat, NULL, NULL, NULL) != SQLITE_OK)
return GIT_ERROR;
return GIT_OK;
}
static int init_db(sqlite3 *db)
{
static const char *sql_check =
"SELECT name FROM sqlite_master WHERE type='table' AND name='" GIT2_TABLE_NAME "';";
sqlite3_stmt *st_check;
int error;
if (sqlite3_prepare_v2(db, sql_check, -1, &st_check, NULL) != SQLITE_OK)
return GIT_ERROR;
switch (sqlite3_step(st_check)) {
case SQLITE_DONE:
/* the table was not found */
error = create_table(db);
break;
case SQLITE_ROW:
/* the table was found */
error = GIT_OK;
break;
default:
error = GIT_ERROR;
break;
}
sqlite3_finalize(st_check);
return error;
}
static int init_statements(sqlite_backend *backend)
{
static const char *sql_read =
"SELECT type, size, data FROM '" GIT2_TABLE_NAME "' WHERE oid = ?;";
static const char *sql_read_header =
"SELECT type, size FROM '" GIT2_TABLE_NAME "' WHERE oid = ?;";
static const char *sql_write =
"INSERT OR IGNORE INTO '" GIT2_TABLE_NAME "' VALUES (?, ?, ?, ?);";
if (sqlite3_prepare_v2(backend->db, sql_read, -1, &backend->st_read, NULL) != SQLITE_OK)
return GIT_ERROR;
if (sqlite3_prepare_v2(backend->db, sql_read_header, -1, &backend->st_read_header, NULL) != SQLITE_OK)
return GIT_ERROR;
if (sqlite3_prepare_v2(backend->db, sql_write, -1, &backend->st_write, NULL) != SQLITE_OK)
return GIT_ERROR;
return GIT_OK;
}
int git_odb_backend_sqlite(git_odb_backend **backend_out, const char *sqlite_db)
{
sqlite_backend *backend;
int error;
backend = (sqlite_backend*)calloc(1, sizeof(sqlite_backend));
if (backend == NULL) {
giterr_set_oom();
return GIT_ERROR;
}
if (sqlite3_open(sqlite_db, &backend->db) != SQLITE_OK)
goto cleanup;
error = init_db(backend->db);
if (error < 0)
goto cleanup;
error = init_statements(backend);
if (error < 0)
goto cleanup;
git_odb_init_backend(&backend->parent, GIT_ODB_BACKEND_VERSION);
backend->parent.read = &sqlite_backend__read;
backend->parent.read_prefix = &sqlite_backend__read_prefix;
backend->parent.read_header = &sqlite_backend__read_header;
backend->parent.write = &sqlite_backend__write;
backend->parent.exists = &sqlite_backend__exists;
backend->parent.free = &sqlite_backend__free;
*backend_out = (git_odb_backend *)backend;
return GIT_OK;
cleanup:
sqlite_backend__free((git_odb_backend *)backend);
return error;
}