diff options
Diffstat (limited to 'db.c')
-rw-r--r-- | db.c | 52 |
1 files changed, 52 insertions, 0 deletions
@@ -743,3 +743,55 @@ int db_remove_wall_tag(sqlite_uint64 wallid, sqlite_uint64 tagid) { return 0; } } + +int db_wall_tags_inconsistent(GArray *wallarray, GHashTable **inconsistent_table) { + sqlite3_stmt *stmt; + int rc; + gchar *join, *query; + + join = gen_joinstring(wallarray->len); + query = g_strdup_printf("SELECT t.id, COUNT(t.id) FROM tag t JOIN walltags wt ON (t.id = wt.tagid AND wt.wallid IN (%s)) GROUP BY t.id", join); + + rc = sqlite3_prepare_v2(db, query, -1, &stmt, NULL); + + g_free(query); + + if(rc != SQLITE_OK) { + return 0; + } + + for(int i = 0; i < wallarray->len; i++) { + sqlite3_uint64 wallid = g_array_index(wallarray, guint64, i); + + rc = sqlite3_bind_int64(stmt, i + 1, wallid); + if(rc != SQLITE_OK) { + sqlite3_finalize(stmt); + return 0; + } + } + + *inconsistent_table = g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL, g_free); + while((rc = sqlite3_step(stmt)) == SQLITE_ROW) { + struct tag_inconsistent_data_t *data = g_malloc(sizeof(struct tag_inconsistent_data_t)); + int count; + sqlite3_uint64 tagid; + + tagid = sqlite3_column_int64(stmt, 0); + count = sqlite3_column_int(stmt, 1); + // set active if selected + data->active = count > 0; + // set inconsistent if not selected by none or all + data->inconsistent = !(count == 0 || count == wallarray->len); + + g_hash_table_insert(*inconsistent_table, (gpointer)tagid, data); + } + + sqlite3_finalize(stmt); + + if(rc != SQLITE_DONE) { + g_hash_table_destroy(*inconsistent_table); + return 0; + } + + return 1; +} |