Implement sqlite backend for input history

Signed-off-by: Sebastian Ramacher <sebastian+dev@ramacher.at>
This commit is contained in:
Sebastian Ramacher 2013-03-14 22:13:39 +01:00
parent b5dbe9ff6d
commit 47eddd0e86
2 changed files with 77 additions and 1 deletions

View file

@ -3,14 +3,17 @@
#include <sqlite3.h> #include <sqlite3.h>
#include <girara/utils.h> #include <girara/utils.h>
#include <girara/datastructures.h> #include <girara/datastructures.h>
#include <girara/input-history.h>
#include <string.h> #include <string.h>
#include "database-sqlite.h" #include "database-sqlite.h"
static void zathura_database_interface_init(ZathuraDatabaseInterface* iface); static void zathura_database_interface_init(ZathuraDatabaseInterface* iface);
static void io_interface_init(GiraraInputHistoryIOInterface* iface);
G_DEFINE_TYPE_WITH_CODE(ZathuraSQLDatabase, zathura_sqldatabase, G_TYPE_OBJECT, G_DEFINE_TYPE_WITH_CODE(ZathuraSQLDatabase, zathura_sqldatabase, G_TYPE_OBJECT,
G_IMPLEMENT_INTERFACE(ZATHURA_TYPE_DATABASE, zathura_database_interface_init)) G_IMPLEMENT_INTERFACE(ZATHURA_TYPE_DATABASE, zathura_database_interface_init)
G_IMPLEMENT_INTERFACE(GIRARA_TYPE_INPUT_HISTORY_IO, io_interface_init))
static void sqlite_finalize(GObject* object); static void sqlite_finalize(GObject* object);
static bool sqlite_add_bookmark(zathura_database_t* db, const char* file, static bool sqlite_add_bookmark(zathura_database_t* db, const char* file,
@ -25,6 +28,8 @@ static bool sqlite_get_fileinfo(zathura_database_t* db, const char* file,
zathura_fileinfo_t* file_info); zathura_fileinfo_t* file_info);
static void sqlite_set_property(GObject* object, guint prop_id, static void sqlite_set_property(GObject* object, guint prop_id,
const GValue* value, GParamSpec* pspec); const GValue* value, GParamSpec* pspec);
static void sqlite_io_append(GiraraInputHistoryIO* db, const char*);
static girara_list_t* sqlite_io_read(GiraraInputHistoryIO* db);
typedef struct zathura_sqldatabase_private_s { typedef struct zathura_sqldatabase_private_s {
sqlite3* session; sqlite3* session;
@ -49,6 +54,14 @@ zathura_database_interface_init(ZathuraDatabaseInterface* iface)
iface->get_fileinfo = sqlite_get_fileinfo; iface->get_fileinfo = sqlite_get_fileinfo;
} }
static void
io_interface_init(GiraraInputHistoryIOInterface* iface)
{
/* initialize interface */
iface->append = sqlite_io_append;
iface->read = sqlite_io_read;
}
static void static void
zathura_sqldatabase_class_init(ZathuraSQLDatabaseClass* class) zathura_sqldatabase_class_init(ZathuraSQLDatabaseClass* class)
{ {
@ -132,6 +145,12 @@ sqlite_db_init(ZathuraSQLDatabase* db, const char* path)
static const char SQL_FILEINFO_ALTER2[] = static const char SQL_FILEINFO_ALTER2[] =
"ALTER TABLE fileinfo ADD COLUMN first_page_column INTEGER;"; "ALTER TABLE fileinfo ADD COLUMN first_page_column INTEGER;";
static const char SQL_HISTORY_INIT[] =
"CREATE TABLE IF NOT EXISTS history ("
"time TIMESTAMP,"
"line TEXT,"
"PRIMARY KEY(line));";
sqlite3* session = NULL; sqlite3* session = NULL;
if (sqlite3_open(path, &session) != SQLITE_OK) { if (sqlite3_open(path, &session) != SQLITE_OK) {
girara_error("Could not open database: %s\n", path); girara_error("Could not open database: %s\n", path);
@ -150,6 +169,12 @@ sqlite_db_init(ZathuraSQLDatabase* db, const char* path)
return; 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;
}
const char* data_type = NULL; const char* data_type = NULL;
if (sqlite3_table_column_metadata(session, NULL, "fileinfo", "pages_per_row", &data_type, NULL, NULL, NULL, NULL) != SQLITE_OK) { if (sqlite3_table_column_metadata(session, NULL, "fileinfo", "pages_per_row", &data_type, NULL, NULL, NULL, NULL) != SQLITE_OK) {
girara_debug("old database table layout detected; updating ..."); girara_debug("old database table layout detected; updating ...");
@ -380,3 +405,51 @@ sqlite_get_fileinfo(zathura_database_t* db, const char* file,
return true; return true;
} }
static void
sqlite_io_append(GiraraInputHistoryIO* db, const char* input)
{
static const char SQL_HISTORY_SET[] =
"REPLACE INTO history (line, time) VALUES (?, DATETIME('now'));";
zathura_sqldatabase_private_t* priv = ZATHURA_SQLDATABASE_GET_PRIVATE(db);
sqlite3_stmt* stmt = prepare_statement(priv->session, SQL_HISTORY_SET);
if (stmt == NULL) {
return;
}
if (sqlite3_bind_text(stmt, 1, input, -1, NULL) != SQLITE_OK) {
sqlite3_finalize(stmt);
girara_error("Failed to bind arguments.");
return;
}
sqlite3_step(stmt);
sqlite3_finalize(stmt);
}
static girara_list_t*
sqlite_io_read(GiraraInputHistoryIO* db)
{
static const char SQL_HISTORY_GET[] =
"SELECT line FROM history ORDER BY time";
zathura_sqldatabase_private_t* priv = ZATHURA_SQLDATABASE_GET_PRIVATE(db);
sqlite3_stmt* stmt = prepare_statement(priv->session, SQL_HISTORY_GET);
if (stmt == NULL) {
return NULL;
}
girara_list_t* list = girara_list_new2((girara_free_function_t) g_free);
if (list == NULL) {
sqlite3_finalize(stmt);
return NULL;
}
while (sqlite3_step(stmt) == SQLITE_ROW) {
girara_list_append(list, g_strdup((const char*) sqlite3_column_text(stmt, 0)));
}
sqlite3_finalize(stmt);
return list;
}

View file

@ -219,6 +219,9 @@ zathura_init(zathura_t* zathura)
girara_debug("Using sqlite database backend."); girara_debug("Using sqlite database backend.");
char* tmp = g_build_filename(zathura->config.data_dir, "bookmarks.sqlite", NULL); char* tmp = g_build_filename(zathura->config.data_dir, "bookmarks.sqlite", NULL);
zathura->database = zathura_sqldatabase_new(tmp); zathura->database = zathura_sqldatabase_new(tmp);
if (zathura->database != NULL) {
g_object_set(zathura->ui.session->global.command_history, "io", zathura->database, NULL);
}
g_free(tmp); g_free(tmp);
#endif #endif
} else { } else {