Remove code duplication

This commit is contained in:
Sebastian Ramacher 2014-09-07 18:48:50 +02:00
parent 1608249709
commit 7721b237e4

View file

@ -7,6 +7,7 @@
#include <string.h> #include <string.h>
#include "database-sqlite.h" #include "database-sqlite.h"
#include "utils.h"
static void zathura_database_interface_init(ZathuraDatabaseInterface* iface); static void zathura_database_interface_init(ZathuraDatabaseInterface* iface);
static void io_interface_init(GiraraInputHistoryIOInterface* iface); static void io_interface_init(GiraraInputHistoryIOInterface* iface);
@ -126,6 +127,7 @@ sqlite_db_init(ZathuraSQLDatabase* db, const char* path)
"vadj_ratio FLOAT," "vadj_ratio FLOAT,"
"PRIMARY KEY(file, id));"; "PRIMARY KEY(file, id));";
/* ceate jumplist table */
static const char SQL_JUMPLIST_INIT[] = static const char SQL_JUMPLIST_INIT[] =
"CREATE TABLE IF NOT EXISTS jumplist (" "CREATE TABLE IF NOT EXISTS jumplist ("
"id INTEGER PRIMARY KEY AUTOINCREMENT," "id INTEGER PRIMARY KEY AUTOINCREMENT,"
@ -156,6 +158,13 @@ sqlite_db_init(ZathuraSQLDatabase* db, const char* path)
"line TEXT," "line TEXT,"
"PRIMARY KEY(line));"; "PRIMARY KEY(line));";
static const char* ALL_INIT[] = {
SQL_BOOKMARK_INIT,
SQL_JUMPLIST_INIT,
SQL_FILEINFO_INIT,
SQL_HISTORY_INIT
};
/* update fileinfo table (part 1) */ /* update fileinfo table (part 1) */
static const char SQL_FILEINFO_ALTER[] = static const char SQL_FILEINFO_ALTER[] =
"ALTER TABLE fileinfo ADD COLUMN pages_per_row INTEGER;" "ALTER TABLE fileinfo ADD COLUMN pages_per_row INTEGER;"
@ -178,28 +187,12 @@ sqlite_db_init(ZathuraSQLDatabase* db, const char* path)
} }
/* create tables if they don't exist */ /* create tables if they don't exist */
if (sqlite3_exec(session, SQL_BOOKMARK_INIT, NULL, 0, NULL) != SQLITE_OK) { for (size_t s = 0; s < LENGTH(ALL_INIT); ++s) {
if (sqlite3_exec(session, ALL_INIT[s], NULL, 0, NULL) != SQLITE_OK) {
girara_error("Failed to initialize database: %s\n", path); girara_error("Failed to initialize database: %s\n", path);
sqlite3_close(session); sqlite3_close(session);
return; return;
} }
if (sqlite3_exec(session, SQL_JUMPLIST_INIT, NULL, 0, NULL) != SQLITE_OK) {
girara_error("Failed to initialize database: %s\n", path);
sqlite3_close(session);
return;
}
if (sqlite3_exec(session, SQL_FILEINFO_INIT, NULL, 0, NULL) != SQLITE_OK) {
girara_error("Failed to initialize database: %s\n", path);
sqlite3_close(session);
return;
}
if (sqlite3_exec(session, SQL_HISTORY_INIT, NULL, 0, NULL) != SQLITE_OK) {
girara_error("Failed to initialize database: %s\n", path);
sqlite3_close(session);
return;
} }
/* check existing tables for missing columns */ /* check existing tables for missing columns */
@ -686,3 +679,4 @@ sqlite_io_read(GiraraInputHistoryIO* db)
sqlite3_finalize(stmt); sqlite3_finalize(stmt);
return list; return list;
} }