Discussion:
[zathura] [Zathura PATCH] Eliminate the need for sqlite3_table_column_metadata
Marwan Tanager
2013-06-21 08:07:40 UTC
Permalink
This is so that Zathura would compile without errors when enabling sqlite
support on systems that doesn't have sqlite compiled with
SQLITE_ENABLE_COLUMN_METADATA (which are the majority).

---
database-sqlite.c | 49 ++++++++++++++++++++++++++++++++++++++++++-------
1 file changed, 42 insertions(+), 7 deletions(-)

diff --git a/database-sqlite.c b/database-sqlite.c
index a35d66a..0839b98 100644
--- a/database-sqlite.c
+++ b/database-sqlite.c
@@ -15,6 +15,7 @@ G_DEFINE_TYPE_WITH_CODE(ZathuraSQLDatabase, zathura_sqldatabase, G_TYPE_OBJECT,
G_IMPLEMENT_INTERFACE(ZATHURA_TYPE_DATABASE, zathura_database_interface_init)
G_IMPLEMENT_INTERFACE(GIRARA_TYPE_INPUT_HISTORY_IO, io_interface_init))

+static bool check_column(sqlite3* session, const char* table, const char* col, bool* result);
static void sqlite_finalize(GObject* object);
static bool sqlite_add_bookmark(zathura_database_t* db, const char* file,
zathura_bookmark_t* bookmark);
@@ -180,25 +181,30 @@ sqlite_db_init(ZathuraSQLDatabase* db, const char* path)
return;
}

- const char* data_type = NULL;
- if (sqlite3_table_column_metadata(session, NULL, "fileinfo", "pages_per_row", &data_type, NULL, NULL, NULL, NULL) != SQLITE_OK) {
+ bool res1, res2, ret1, ret2;
+
+ ret1 = check_column(session, "fileinfo", "pages_per_row", &res1);
+
+ if (ret1 == true && res1 == false) {
girara_debug("old database table layout detected; updating ...");
if (sqlite3_exec(session, SQL_FILEINFO_ALTER, NULL, 0, NULL) != SQLITE_OK) {
girara_warning("failed to update database table layout");
}
}

- data_type = NULL;
- if (sqlite3_table_column_metadata(session, NULL, "fileinfo", "first_page_column", &data_type, NULL, NULL, NULL, NULL) != SQLITE_OK) {
+ ret1 = check_column(session, "fileinfo", "first_page_column", &res1);
+
+ if (ret1 == true && res1 == false) {
girara_debug("old database table layout detected; updating ...");
if (sqlite3_exec(session, SQL_FILEINFO_ALTER2, NULL, 0, NULL) != SQLITE_OK) {
girara_warning("failed to update database table layout");
}
}

- data_type = NULL;
- if (sqlite3_table_column_metadata(session, NULL, "bookmarks", "hadj_ratio", &data_type, NULL, NULL, NULL, NULL) != SQLITE_OK &&
- sqlite3_table_column_metadata(session, NULL, "bookmarks", "vadj_ratio", &data_type, NULL, NULL, NULL, NULL) != SQLITE_OK) {
+ ret1 = check_column(session, "bookmarks", "hadj_ratio", &res1);
+ ret2 = check_column(session, "bookmarks", "vadj_ratio", &res2);
+
+ if (ret1 == true && ret2 == true && res1 == false && res2 == false) {
girara_debug("old database table layout detected; updating ...");
if (sqlite3_exec(session, SQL_BOOKMARK_ALTER, NULL, 0, NULL) != SQLITE_OK) {
girara_warning("failed to update database table layout");
@@ -248,6 +254,35 @@ prepare_statement(sqlite3* session, const char* statement)
}

static bool
+check_column(sqlite3* session, const char* table, const char* col, bool* res)
+{
+ char* query = g_strdup_printf("PRAGMA table_info(%s);", table);
+ sqlite3_stmt* stmt = prepare_statement(session, query);
+
+ if (stmt == NULL) {
+ return false;
+ }
+
+ *res = false;
+
+ while (sqlite3_step(stmt) == SQLITE_ROW) {
+ if (strcmp((const char*) sqlite3_column_text(stmt, 1), col) == 0) {
+ *res = true;
+ break;
+ }
+ }
+
+ if (*res == false) {
+ girara_debug("column %s in table %s is NOT found", col, table);
+ }
+
+ sqlite3_finalize(stmt);
+ g_free(query);
+
+ return true;
+}
+
+static bool
sqlite_add_bookmark(zathura_database_t* db, const char* file,
zathura_bookmark_t* bookmark)
{
--
1.7.10.4
Sebastian Ramacher
2013-06-21 08:40:58 UTC
Permalink
Cool, thanks!
Post by Marwan Tanager
+ char* query = g_strdup_printf("PRAGMA table_info(%s);", table);
I replaced that with sqlite3_mprintf. We control all the possible values
of table, but better be safe then sorry.

Regards
--
Sebastian Ramacher
Marwan Tanager
2013-06-21 09:21:07 UTC
Permalink
Post by Sebastian Ramacher
Cool, thanks!
Post by Marwan Tanager
+ char* query = g_strdup_printf("PRAGMA table_info(%s);", table);
I replaced that with sqlite3_mprintf. We control all the possible values
of table, but better be safe then sorry.
Yes, you're right.

BTW, would commit b4f290d4f92e4b82569c4c78b22194e4bc2cedb3 be necessary with
this patch?


--
Marwan
Sebastian Ramacher
2013-06-21 09:35:00 UTC
Permalink
Post by Marwan Tanager
Post by Sebastian Ramacher
Cool, thanks!
Post by Marwan Tanager
+ char* query = g_strdup_printf("PRAGMA table_info(%s);", table);
I replaced that with sqlite3_mprintf. We control all the possible values
of table, but better be safe then sorry.
Yes, you're right.
BTW, would commit b4f290d4f92e4b82569c4c78b22194e4bc2cedb3 be necessary with
this patch?
It's not necessary, I just like it to be consistent with the other
CREATE TABLE code.

Regards
--
Sebastian Ramacher
Marwan Tanager
2013-06-21 09:43:06 UTC
Permalink
Post by Sebastian Ramacher
Post by Marwan Tanager
BTW, would commit b4f290d4f92e4b82569c4c78b22194e4bc2cedb3 be necessary
with this patch?
It's not necessary, I just like it to be consistent with the other
CREATE TABLE code.
I see. No probs.


--
Marwan
Marwan Tanager
2013-06-21 10:02:26 UTC
Permalink
Post by Sebastian Ramacher
Post by Marwan Tanager
Post by Sebastian Ramacher
Cool, thanks!
Post by Marwan Tanager
+ char* query = g_strdup_printf("PRAGMA table_info(%s);", table);
I replaced that with sqlite3_mprintf. We control all the possible values
of table, but better be safe then sorry.
Yes, you're right.
BTW, would commit b4f290d4f92e4b82569c4c78b22194e4bc2cedb3 be necessary with
this patch?
It's not necessary, I just like it to be consistent with the other
CREATE TABLE code.
Actually, it's more logical that way, because it makes no sense to create the
old schema then update it; I just haven't thought enough about it :)


--
Marwan

Loading...