diff options
author | gramanas <anastasis.gramm2@gmail.com> | 2018-11-16 00:04:51 +0200 |
---|---|---|
committer | gramanas <anastasis.gramm2@gmail.com> | 2018-11-16 00:04:51 +0200 |
commit | 97e14c73be6684259e235a92cc575ea39a04fc7e (patch) | |
tree | 6aa652453a118b9863687ba80b5c3c3c425ed1cd /src/dblayer.c | |
parent | 967ad7898243e083f485a03414fce0018bfce881 (diff) | |
download | ck-97e14c73be6684259e235a92cc575ea39a04fc7e.tar.gz ck-97e14c73be6684259e235a92cc575ea39a04fc7e.tar.bz2 ck-97e14c73be6684259e235a92cc575ea39a04fc7e.zip |
[ckdb] simplify open_DB and make_DB
Diffstat (limited to 'src/dblayer.c')
-rw-r--r-- | src/dblayer.c | 44 |
1 files changed, 24 insertions, 20 deletions
diff --git a/src/dblayer.c b/src/dblayer.c index 97a93db..a6e6193 100644 --- a/src/dblayer.c +++ b/src/dblayer.c @@ -79,37 +79,41 @@ void close_DB(DB *db) { sqlite3_close(db->ptr); } -DB init_make_DB(const UserOpt *opt) { - sqlite3 *db; - char db_path[STR_L] = ""; +int open_DB(DB *db, const UserOpt *opt) { + if (!db || !opt) { + return -1; + } int rc; + char db_path[STR_L] = ""; make_db_name(db_path, opt->confDir); - rc = sqlite3_open(db_path, &db); - if (rc != SQLITE_OK) { - return empty_DB(SQL_ERR_NO_DB_FILE); + rc = sqlite3_open(db_path, &db->ptr); + + if (rc) { + ERR("%s is not a path to an sqlite3 database.", db_path); + return -1; + } + + if (check_initialized_DB(db->ptr)) { + ERR("The database file is currupted. Run ck init anew."); + return -1; } - return new_DB(db); + LOG("Opened %s", db_path); + return 0; } -DB open_DB(const UserOpt *opt) { - sqlite3 *db; - int rc; +int init_make_DB(DB *db, const UserOpt *opt) { char db_path[STR_L] = ""; + int rc; - make_db_name(db_path, opt->confDir); - rc = sqlite3_open(db_path, &db); - - if (rc) { - return empty_DB(SQL_ERR_NO_DB_FILE); - } - - if (check_initialized_DB(db)) { - return empty_DB(SQL_ERR_NO_TABLES); + make_db_name(db_path, opt->confDir); + rc = sqlite3_open(db_path, &db->ptr); + if (rc != SQLITE_OK) { + return -1; } - return new_DB(db); + return 0; } void init_make_tables(DB *db) { |