From 55bc1ebe8ca3425cdec4bf24b9908e120f6c7fca Mon Sep 17 00:00:00 2001 From: Jon Bergli Heier Date: Sat, 8 May 2010 13:40:16 +0200 Subject: Merged db_get_tags and db_get_top_level_tags. --- db.c | 46 ++++++++++++---------------------------------- 1 file changed, 12 insertions(+), 34 deletions(-) diff --git a/db.c b/db.c index 935e9c1..c23fa3d 100644 --- a/db.c +++ b/db.c @@ -632,35 +632,7 @@ int db_get_tags_all(GArray **array) { } int db_get_top_level_tags(GArray **array) { - struct tag_t temp, *temp2; - sqlite3_stmt *stmt; - int rc; - - rc = sqlite3_prepare_v2(db, "SELECT id, name, parent FROM tag WHERE parent IS NULL ORDER BY name", -1, &stmt, NULL); - if(rc != SQLITE_OK) { - return 0; - } - - *array = g_array_new(FALSE, FALSE, sizeof(struct tag_t)); - while((rc = sqlite3_step(stmt)) == SQLITE_ROW) { - temp.name = g_strdup((const gchar*)sqlite3_column_text(stmt, 1)); - temp.id = sqlite3_column_int64(stmt, 0); - temp.parent = sqlite3_column_int64(stmt, 2); - g_array_append_val(*array, temp); - } - - sqlite3_finalize(stmt); - - if(rc != SQLITE_DONE) { - for(int i = 0; i < (*array)->len; i++) { - temp2 = &g_array_index(*array, struct tag_t, i); - g_free(temp2->name); - } - g_array_free(*array, TRUE); - return 0; - } - - return 1; + return db_get_tags(array, 0); } int db_get_tags(GArray **array, sqlite3_uint64 parent) { @@ -668,15 +640,21 @@ int db_get_tags(GArray **array, sqlite3_uint64 parent) { sqlite3_stmt *stmt; int rc; - rc = sqlite3_prepare_v2(db, "SELECT id, name, parent FROM tag WHERE parent = ? ORDER BY name", -1, &stmt, NULL); + if(parent == 0) { + rc = sqlite3_prepare_v2(db, "SELECT id, name, parent FROM tag WHERE parent IS NULL ORDER BY name", -1, &stmt, NULL); + } else { + rc = sqlite3_prepare_v2(db, "SELECT id, name, parent FROM tag WHERE parent = ? ORDER BY name", -1, &stmt, NULL); + } if(rc != SQLITE_OK) { return 0; } - rc = sqlite3_bind_int64(stmt, 1, parent); - if(rc != SQLITE_OK) { - sqlite3_finalize(stmt); - return 0; + if(parent) { + rc = sqlite3_bind_int64(stmt, 1, parent); + if(rc != SQLITE_OK) { + sqlite3_finalize(stmt); + return 0; + } } *array = g_array_new(FALSE, FALSE, sizeof(struct tag_t)); -- cgit v1.2.3