From 9120a30db940b4e2a4f08ff6436161810b4f8302 Mon Sep 17 00:00:00 2001 From: Jon Bergli Heier Date: Mon, 4 Jan 2010 02:18:51 +0100 Subject: Implemented tag searching. --- db.c | 77 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) (limited to 'db.c') diff --git a/db.c b/db.c index d47793a..0688d75 100644 --- a/db.c +++ b/db.c @@ -402,6 +402,83 @@ int db_get_wallpapers(sqlite_uint64 dirid, GArray **array) { return 1; } +static gchar *gen_joinstring(int c) { + char **split, *join; + + split = g_malloc(sizeof(gchar*) * (c+1)); + split[c] = NULL; + + for(int i = 0; i < c; i++) { + split[i] = "?"; + } + + join = g_strjoinv(", ", split); + + g_free(split); + + return join; +} + +/* + * tags = uint64 + * walls = struct tag_t * + */ +int db_get_walls_by_tags(GArray *tags, GArray **array) { + sqlite3_stmt *stmt; + int rc; + char *join, *query; + struct wallpaper_t temp, *temp2; + + join = gen_joinstring(tags->len); + query = g_strdup_printf("SELECT w.id, w.filepath, w.size, w.width, w.height " + "FROM wallpaper w JOIN walltags t ON (t.wallid = w.id) WHERE t.tagid IN (%s)", + join); + g_free(join); + + rc = sqlite3_prepare_v2(db, query, -1, &stmt, NULL); + + g_free(query); + + if(rc != SQLITE_OK) { + return 0; + } + + for(int i = 0; i < tags->len; i++) { + sqlite_uint64 id; + + id = g_array_index(tags, sqlite_uint64, i); + rc = sqlite3_bind_int64(stmt, i + 1, id); + + if(rc != SQLITE_OK) { + sqlite3_finalize(stmt); + return 0; + } + } + + *array = g_array_new(FALSE, FALSE, sizeof(struct wallpaper_t)); + while((rc = sqlite3_step(stmt)) == SQLITE_ROW) { + temp.filepath = g_strdup(sqlite3_column_text(stmt, 1)); + temp.id = sqlite3_column_int64(stmt, 0); + temp.size = sqlite3_column_int(stmt, 2); + temp.width = sqlite3_column_int(stmt, 3); + temp.height = sqlite3_column_int(stmt, 4); + 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 wallpaper_t, i); + g_free(temp2->filepath); + } + g_array_free(*array, TRUE); + return 0; + } + + return 1; +} + sqlite_uint64 db_add_tag(const char *name) { sqlite3_stmt *stmt; int rc; -- cgit v1.2.3