diff options
author | Vicent Marti <tanoku@gmail.com> | 2011-03-28 23:23:44 +0400 |
---|---|---|
committer | Vicent Marti <tanoku@gmail.com> | 2011-03-28 23:23:44 +0400 |
commit | 483526ebec8b50682b6edd3e1db92df7569ca7b0 (patch) | |
tree | f390a1dc2004de506368aa0e047d33f51dcb1122 | |
parent | 683581a3e91032d9fdc203933d691b9353f24492 (diff) |
Update the SQLite backend
-rw-r--r-- | src/backends/sqlite.c | 38 | ||||
-rw-r--r-- | tests/t11-sqlite.c | 21 |
2 files changed, 29 insertions, 30 deletions
diff --git a/src/backends/sqlite.c b/src/backends/sqlite.c index 72d7b4d8e..a4c6d4825 100644 --- a/src/backends/sqlite.c +++ b/src/backends/sqlite.c @@ -44,21 +44,20 @@ typedef struct { sqlite3_stmt *st_read_header; } sqlite_backend; -int sqlite_backend__read_header(git_rawobj *obj, git_odb_backend *_backend, const git_oid *oid) +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(obj && _backend && oid); + assert(len_p && type_p && _backend && oid); backend = (sqlite_backend *)_backend; error = GIT_ERROR; - obj->data = NULL; if (sqlite3_bind_text(backend->st_read_header, 1, (char *)oid->id, 20, SQLITE_TRANSIENT) == SQLITE_OK) { if (sqlite3_step(backend->st_read_header) == SQLITE_ROW) { - obj->type = sqlite3_column_int(backend->st_read_header, 0); - obj->len = sqlite3_column_int(backend->st_read_header, 1); + *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_SUCCESS; } else { @@ -71,26 +70,26 @@ int sqlite_backend__read_header(git_rawobj *obj, git_odb_backend *_backend, cons } -int sqlite_backend__read(git_rawobj *obj, git_odb_backend *_backend, const git_oid *oid) +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(obj && _backend && oid); + assert(data_p && len_p && type_p && _backend && oid); backend = (sqlite_backend *)_backend; error = GIT_ERROR; if (sqlite3_bind_text(backend->st_read, 1, (char *)oid->id, 20, SQLITE_TRANSIENT) == SQLITE_OK) { if (sqlite3_step(backend->st_read) == SQLITE_ROW) { - obj->type = sqlite3_column_int(backend->st_read, 0); - obj->len = sqlite3_column_int(backend->st_read, 1); - obj->data = git__malloc(obj->len); + *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__malloc(*len_p); - if (obj->data == NULL) { + if (*data_p == NULL) { error = GIT_ENOMEM; } else { - memcpy(obj->data, sqlite3_column_blob(backend->st_read, 2), obj->len); + memcpy(*data_p, sqlite3_column_blob(backend->st_read, 2), *len_p); error = GIT_SUCCESS; } @@ -126,27 +125,24 @@ int sqlite_backend__exists(git_odb_backend *_backend, const git_oid *oid) } -int sqlite_backend__write(git_oid *id, git_odb_backend *_backend, git_rawobj *obj) +int sqlite_backend__write(git_oid *id, git_odb_backend *_backend, const void *data, size_t len, git_otype type) { - char hdr[64]; - int hdrlen; - int error; sqlite_backend *backend; - assert(id && _backend && obj); + assert(id && _backend && data); backend = (sqlite_backend *)_backend; - if ((error = git_odb__hash_obj(id, hdr, sizeof(hdr), &hdrlen, obj)) < 0) + if ((error = git_odb_hash(id, data, len, type)) < 0) return error; error = SQLITE_ERROR; if (sqlite3_bind_text(backend->st_write, 1, (char *)id->id, 20, SQLITE_TRANSIENT) == SQLITE_OK && - sqlite3_bind_int(backend->st_write, 2, (int)obj->type) == SQLITE_OK && - sqlite3_bind_int(backend->st_write, 3, obj->len) == SQLITE_OK && - sqlite3_bind_blob(backend->st_write, 4, obj->data, obj->len, SQLITE_TRANSIENT) == 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_TRANSIENT) == SQLITE_OK) { error = sqlite3_step(backend->st_write); } diff --git a/tests/t11-sqlite.c b/tests/t11-sqlite.c index fecf7886f..61ecf98ac 100644 --- a/tests/t11-sqlite.c +++ b/tests/t11-sqlite.c @@ -23,7 +23,7 @@ * Boston, MA 02110-1301, USA. */ #include "test_lib.h" - +#include "odb.h" #ifdef GIT2_SQLITE_BACKEND #include "t03-data.h" @@ -31,14 +31,17 @@ #include "git2/odb_backend.h" -static int cmp_objects(raw_object *o1, raw_object *o2) +static int cmp_objects(git_odb_object *odb_obj, git_rawobj *raw) { - if (o1->type != o2->type) + if (raw->type != git_odb_object_type(odb_obj)) return -1; - if (o1->len != o2->len) + + if (raw->len != git_odb_object_size(odb_obj)) return -1; - if ((o1->len > 0) && (memcmp(o1->data, o2->data, o1->len) != 0)) + + if ((raw->len > 0) && (memcmp(raw->data, git_odb_object_data(odb_obj), raw->len) != 0)) return -1; + return 0; } @@ -62,15 +65,15 @@ static git_odb *open_sqlite_odb(void) #define TEST_WRITE(PTR) {\ git_odb *db; \ git_oid id1, id2; \ - raw_object obj; \ + git_odb_object *obj; \ db = open_sqlite_odb(); \ must_be_true(db != NULL); \ must_pass(git_oid_mkstr(&id1, PTR.id)); \ - must_pass(git_odb_write(&id2, db, &PTR##_obj)); \ + must_pass(git_odb_write(&id2, db, PTR##_obj.data, PTR##_obj.len, PTR##_obj.type)); \ must_be_true(git_oid_cmp(&id1, &id2) == 0); \ must_pass(git_odb_read(&obj, db, &id1)); \ - must_pass(cmp_objects(&obj, &PTR##_obj)); \ - git_rawobj_close(&obj); \ + must_pass(cmp_objects(obj, &PTR##_obj)); \ + git_odb_object_close(obj); \ git_odb_close(db); \ } |