diff options
Diffstat (limited to '')
-rw-r--r-- | src/sqlite.c | 38 | ||||
-rw-r--r-- | src/sqlite.h | 4 | ||||
-rw-r--r-- | src/sqlite/v01.sql | 17 |
3 files changed, 51 insertions, 8 deletions
diff --git a/src/sqlite.c b/src/sqlite.c index 7a2e482..88ec7bf 100644 --- a/src/sqlite.c +++ b/src/sqlite.c @@ -111,6 +111,12 @@ int sqlite_prepare(sqlite3 *db, const char *stmt, sqlite3_stmt **result) return ret; } +void sqlite_reset(sqlite3_stmt *stmt) +{ + sqlite_errno_if(sqlite3_reset(stmt), "sqlite3_reset"); + sqlite_errno_if(sqlite3_clear_bindings(stmt), "sqlite3_clear_bindings"); +} + void sqlite_finalize(sqlite3_stmt *stmt) { sqlite_errno_if(sqlite3_finalize(stmt), "sqlite3_finalize"); @@ -124,6 +130,7 @@ int sqlite_step(sqlite3_stmt *stmt) switch (ret) { case SQLITE_ROW: + return 1; case SQLITE_DONE: return 0; @@ -198,6 +205,32 @@ int sqlite_column_blob(sqlite3_stmt *stmt, int index, unsigned char **_result) return 0; } +int sqlite_bind_int(sqlite3_stmt *stmt, int index, int value) +{ + int ret = 0; + + ret = sqlite3_bind_int(stmt, index, value); + if (ret) { + sqlite_errno(ret, "sqlite3_bind_int"); + return ret; + } + + return ret; +} + +int sqlite_bind_text(sqlite3_stmt *stmt, int index, const char *value) +{ + int ret = 0; + + ret = sqlite3_bind_text(stmt, index, value, -1, SQLITE_STATIC); + if (ret) { + sqlite_errno(ret, "sqlite3_bind_text"); + return ret; + } + + return ret; +} + int sqlite_exec_as_transaction(sqlite3 *db, const char *stmt) { static const char *const fmt = "BEGIN; %s COMMIT;"; @@ -236,6 +269,11 @@ int sqlite_get_user_version(sqlite3 *db, unsigned int *output) ret = sqlite_step(stmt); if (ret < 0) goto finalize; + if (!ret) { + ret = -1; + log_err("Failed to read database version\n"); + goto finalize; + } result = sqlite_column_int(stmt, 0); if (result < 0) { diff --git a/src/sqlite.h b/src/sqlite.h index 5a6a548..28c9b7a 100644 --- a/src/sqlite.h +++ b/src/sqlite.h @@ -22,9 +22,13 @@ int sqlite_exec(sqlite3 *db, const char *stmt, sqlite3_callback callback); int sqlite_log_result(void *, int, char **, char **); int sqlite_prepare(sqlite3 *db, const char *stmt, sqlite3_stmt **result); +void sqlite_reset(sqlite3_stmt *); void sqlite_finalize(sqlite3_stmt *); int sqlite_step(sqlite3_stmt *); +int sqlite_bind_int(sqlite3_stmt *, int column_index, int value); +int sqlite_bind_text(sqlite3_stmt *, int column_index, const char *value); + int sqlite_column_int(sqlite3_stmt *, int column_index); int sqlite_column_text(sqlite3_stmt *, int column_index, char **result); int sqlite_column_blob(sqlite3_stmt *, int column_index, unsigned char **result); diff --git a/src/sqlite/v01.sql b/src/sqlite/v01.sql index 2793b8b..44c5116 100644 --- a/src/sqlite/v01.sql +++ b/src/sqlite/v01.sql @@ -1,19 +1,19 @@ -CREATE TABLE cimple_repositories ( +CREATE TABLE cimple_repos ( id INTEGER PRIMARY KEY, url TEXT NOT NULL ) STRICT; -CREATE UNIQUE INDEX cimple_repositories_url_index ON cimple_repositories(url); +CREATE UNIQUE INDEX cimple_repos_index_url ON cimple_repos(url); CREATE TABLE cimple_run_status ( id INTEGER PRIMARY KEY, label TEXT NOT NULL ) STRICT; -CREATE UNIQUE INDEX cimple_run_status_label_index ON cimple_run_status(label); +CREATE UNIQUE INDEX cimple_run_status_index_label ON cimple_run_status(label); -INSERT INTO cimple_run_status(id, label) VALUES (0, 'created'); -INSERT INTO cimple_run_status(id, label) VALUES (1, 'finished'); +INSERT INTO cimple_run_status(id, label) VALUES (1, 'created'); +INSERT INTO cimple_run_status(id, label) VALUES (2, 'finished'); CREATE TABLE cimple_runs ( id INTEGER PRIMARY KEY, @@ -21,10 +21,11 @@ CREATE TABLE cimple_runs ( result INTEGER NOT NULL, output BLOB NOT NULL, repo_id INTEGER NOT NULL, + rev TEXT NOT NULL, FOREIGN KEY (status) REFERENCES cimple_run_status(id), - FOREIGN KEY (repo_id) REFERENCES cimple_repositories(id) + FOREIGN KEY (repo_id) REFERENCES cimple_repos(id) ON DELETE CASCADE ON UPDATE CASCADE ) STRICT; -CREATE INDEX cimple_runs_status_index ON cimple_runs(status); -CREATE INDEX cimple_runs_repo_id_index ON cimple_runs(repo_id); +CREATE INDEX cimple_runs_index_status ON cimple_runs(status); +CREATE INDEX cimple_runs_index_repo_id ON cimple_runs(repo_id); |