diff options
author | Jon Bergli Heier <snakebite@jvnv.net> | 2010-05-08 19:39:22 +0200 |
---|---|---|
committer | Jon Bergli Heier <snakebite@jvnv.net> | 2010-05-08 19:39:22 +0200 |
commit | 7cf18de071f28f2968624371a489d7dc9d437f87 (patch) | |
tree | df54f8a6049c3589ef2bd36619c2f88964b2aec7 | |
parent | 457650226a75249c3918cc0a892bfcf710ff4089 (diff) |
Merged db_get_top_level_directories and db_get_directories.
-rw-r--r-- | db.c | 46 |
1 files changed, 13 insertions, 33 deletions
@@ -154,34 +154,7 @@ sqlite_uint64 db_get_directory(const char *path) { } int db_get_top_level_directories(GArray **array) { - struct directory_t temp, *temp2; - sqlite3_stmt *stmt; - int rc; - - rc = sqlite3_prepare_v2(db, "SELECT id, name FROM directory WHERE parent ISNULL ORDER BY name", -1, &stmt, NULL); - if(rc != SQLITE_OK) { - return 0; - } - - *array = g_array_new(TRUE, FALSE, sizeof(struct directory_t)); - while((rc = sqlite3_step(stmt)) == SQLITE_ROW) { - temp.name = g_strdup((const gchar*)sqlite3_column_text(stmt, 1)); - temp.dirid = sqlite3_column_int64(stmt, 0); - g_array_append_val(*array, temp); - } - - sqlite3_finalize(stmt); - - if(rc != SQLITE_DONE) { - for(int i = 0; (*array)->len; i++) { - temp2 = &g_array_index(*array, struct directory_t, i); - g_free(temp2->name); - } - g_array_free(*array, TRUE); - return 0; - } - - return 1; + return db_get_directories(0, array); } int db_get_directories(sqlite_uint64 parent, GArray **array) { @@ -189,17 +162,24 @@ int db_get_directories(sqlite_uint64 parent, GArray **array) { sqlite3_stmt *stmt; int rc; - rc = sqlite3_prepare_v2(db, "SELECT id, name FROM directory WHERE parent = ? ORDER BY name", -1, &stmt, NULL); - if(rc != SQLITE_OK) { - return 0; + if(parent == 0) { + rc = sqlite3_prepare_v2(db, "SELECT id, name FROM directory WHERE parent ISNULL ORDER BY name", -1, &stmt, NULL); + } else { + rc = sqlite3_prepare_v2(db, "SELECT id, name FROM directory WHERE parent = ? ORDER BY name", -1, &stmt, NULL); } - 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(TRUE, FALSE, sizeof(struct directory_t)); while((rc = sqlite3_step(stmt)) == SQLITE_ROW) { temp.name = g_strdup((const gchar*)sqlite3_column_text(stmt, 1)); |