Update the SQLite backend
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
diff --git a/src/backends/sqlite.c b/src/backends/sqlite.c
index 72d7b4d..a4c6d48 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 fecf788..61ecf98 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); \
}