aboutsummaryrefslogtreecommitdiffstats
path: root/src/sticker.c
diff options
context:
space:
mode:
authorMax Kellermann <max@duempel.org>2009-04-01 18:42:06 +0200
committerMax Kellermann <max@duempel.org>2009-04-01 18:42:06 +0200
commit3a4d20faa2ee6e5e514b82a92c7973978679d47d (patch)
treec14bd2c9b800851f1d4ab741a31ec0300ed7391b /src/sticker.c
parent7f38c3fc78f7dd3c542cdb73bcd45bfe2208a5a8 (diff)
downloadmpd-3a4d20faa2ee6e5e514b82a92c7973978679d47d.tar.gz
mpd-3a4d20faa2ee6e5e514b82a92c7973978679d47d.tar.xz
mpd-3a4d20faa2ee6e5e514b82a92c7973978679d47d.zip
sticker: added sticker_find()
sticker_find() finds stickers with the specified name.
Diffstat (limited to 'src/sticker.c')
-rw-r--r--src/sticker.c69
1 files changed, 69 insertions, 0 deletions
diff --git a/src/sticker.c b/src/sticker.c
index 255bfe177..e2d774f3d 100644
--- a/src/sticker.c
+++ b/src/sticker.c
@@ -37,6 +37,7 @@ enum sticker_sql {
STICKER_SQL_UPDATE,
STICKER_SQL_INSERT,
STICKER_SQL_DELETE,
+ STICKER_SQL_FIND,
};
static const char *const sticker_sql[] = {
@@ -50,6 +51,8 @@ static const char *const sticker_sql[] = {
"INSERT INTO sticker(type,uri,name,value) VALUES(?, ?, ?, ?)",
[STICKER_SQL_DELETE] =
"DELETE FROM sticker WHERE type=? AND uri=?",
+ [STICKER_SQL_FIND] =
+ "SELECT uri,value FROM sticker WHERE type=? AND uri LIKE (? || '%') AND name=?",
};
static const char sticker_sql_create[] =
@@ -508,3 +511,69 @@ sticker_load(const char *type, const char *uri)
return sticker;
}
+
+bool
+sticker_find(const char *type, const char *base_uri, const char *name,
+ void (*func)(const char *uri, const char *value,
+ gpointer user_data),
+ gpointer user_data)
+{
+ sqlite3_stmt *const stmt = sticker_stmt[STICKER_SQL_FIND];
+ int ret;
+
+ assert(type != NULL);
+ assert(name != NULL);
+ assert(func != NULL);
+ assert(sticker_enabled());
+
+ sqlite3_reset(stmt);
+
+ ret = sqlite3_bind_text(stmt, 1, type, -1, NULL);
+ if (ret != SQLITE_OK) {
+ g_warning("sqlite3_bind_text() failed: %s",
+ sqlite3_errmsg(sticker_db));
+ return false;
+ }
+
+ if (base_uri == NULL)
+ base_uri = "";
+
+ ret = sqlite3_bind_text(stmt, 2, base_uri, -1, NULL);
+ if (ret != SQLITE_OK) {
+ g_warning("sqlite3_bind_text() failed: %s",
+ sqlite3_errmsg(sticker_db));
+ return false;
+ }
+
+ ret = sqlite3_bind_text(stmt, 3, name, -1, NULL);
+ if (ret != SQLITE_OK) {
+ g_warning("sqlite3_bind_text() failed: %s",
+ sqlite3_errmsg(sticker_db));
+ return false;
+ }
+
+ do {
+ ret = sqlite3_step(stmt);
+ switch (ret) {
+ case SQLITE_ROW:
+ func((const char*)sqlite3_column_text(stmt, 0),
+ (const char*)sqlite3_column_text(stmt, 1),
+ user_data);
+ break;
+ case SQLITE_DONE:
+ break;
+ case SQLITE_BUSY:
+ /* no op */
+ break;
+ default:
+ g_warning("sqlite3_step() failed: %s",
+ sqlite3_errmsg(sticker_db));
+ return false;
+ }
+ } while (ret != SQLITE_DONE);
+
+ sqlite3_reset(stmt);
+ sqlite3_clear_bindings(stmt);
+
+ return true;
+}