2009-01-19 18:51:57 +01:00
|
|
|
/*
|
2016-02-26 17:54:05 +01:00
|
|
|
* Copyright 2003-2016 The Music Player Daemon Project
|
2009-01-19 18:51:57 +01:00
|
|
|
* http://www.musicpd.org
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
2009-03-13 18:43:16 +01:00
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
2009-01-19 18:51:57 +01:00
|
|
|
*/
|
|
|
|
|
2009-11-12 09:12:38 +01:00
|
|
|
#include "config.h"
|
2013-01-02 22:25:17 +01:00
|
|
|
#include "StickerDatabase.hxx"
|
2014-12-12 20:49:00 +01:00
|
|
|
#include "lib/sqlite/Domain.hxx"
|
2014-12-12 20:39:36 +01:00
|
|
|
#include "lib/sqlite/Util.hxx"
|
2013-08-07 19:54:38 +02:00
|
|
|
#include "fs/Path.hxx"
|
2013-01-09 08:36:52 +01:00
|
|
|
#include "Idle.hxx"
|
2013-08-10 18:02:44 +02:00
|
|
|
#include "util/Error.hxx"
|
2013-10-15 22:04:17 +02:00
|
|
|
#include "util/Macros.hxx"
|
2015-11-06 09:37:07 +01:00
|
|
|
#include "util/StringCompare.hxx"
|
2009-01-19 18:51:57 +01:00
|
|
|
|
2013-01-03 02:39:08 +01:00
|
|
|
#include <string>
|
|
|
|
#include <map>
|
|
|
|
|
2009-01-19 18:51:57 +01:00
|
|
|
#include <assert.h>
|
|
|
|
|
2015-01-28 19:33:56 +01:00
|
|
|
struct Sticker {
|
2013-01-03 02:39:08 +01:00
|
|
|
std::map<std::string, std::string> table;
|
2009-03-14 14:20:01 +01:00
|
|
|
};
|
|
|
|
|
2009-03-27 17:05:04 +01:00
|
|
|
enum sticker_sql {
|
|
|
|
STICKER_SQL_GET,
|
|
|
|
STICKER_SQL_LIST,
|
|
|
|
STICKER_SQL_UPDATE,
|
|
|
|
STICKER_SQL_INSERT,
|
|
|
|
STICKER_SQL_DELETE,
|
2009-04-28 20:23:27 +02:00
|
|
|
STICKER_SQL_DELETE_VALUE,
|
2009-04-01 18:42:06 +02:00
|
|
|
STICKER_SQL_FIND,
|
2014-12-12 22:12:19 +01:00
|
|
|
STICKER_SQL_FIND_VALUE,
|
2014-12-12 22:26:04 +01:00
|
|
|
STICKER_SQL_FIND_LT,
|
|
|
|
STICKER_SQL_FIND_GT,
|
2009-03-27 17:05:04 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
static const char *const sticker_sql[] = {
|
2013-01-02 22:25:17 +01:00
|
|
|
//[STICKER_SQL_GET] =
|
2009-03-27 17:05:04 +01:00
|
|
|
"SELECT value FROM sticker WHERE type=? AND uri=? AND name=?",
|
2013-01-02 22:25:17 +01:00
|
|
|
//[STICKER_SQL_LIST] =
|
2009-03-27 17:05:04 +01:00
|
|
|
"SELECT name,value FROM sticker WHERE type=? AND uri=?",
|
2013-01-02 22:25:17 +01:00
|
|
|
//[STICKER_SQL_UPDATE] =
|
2009-03-27 17:05:04 +01:00
|
|
|
"UPDATE sticker SET value=? WHERE type=? AND uri=? AND name=?",
|
2013-01-02 22:25:17 +01:00
|
|
|
//[STICKER_SQL_INSERT] =
|
2009-03-27 17:05:04 +01:00
|
|
|
"INSERT INTO sticker(type,uri,name,value) VALUES(?, ?, ?, ?)",
|
2013-01-02 22:25:17 +01:00
|
|
|
//[STICKER_SQL_DELETE] =
|
2009-03-27 17:05:04 +01:00
|
|
|
"DELETE FROM sticker WHERE type=? AND uri=?",
|
2013-01-02 22:25:17 +01:00
|
|
|
//[STICKER_SQL_DELETE_VALUE] =
|
2009-04-28 20:23:27 +02:00
|
|
|
"DELETE FROM sticker WHERE type=? AND uri=? AND name=?",
|
2013-01-02 22:25:17 +01:00
|
|
|
//[STICKER_SQL_FIND] =
|
2009-04-01 18:42:06 +02:00
|
|
|
"SELECT uri,value FROM sticker WHERE type=? AND uri LIKE (? || '%') AND name=?",
|
2014-12-12 22:12:19 +01:00
|
|
|
|
|
|
|
//[STICKER_SQL_FIND_VALUE] =
|
|
|
|
"SELECT uri,value FROM sticker WHERE type=? AND uri LIKE (? || '%') AND name=? AND value=?",
|
2014-12-12 22:26:04 +01:00
|
|
|
|
|
|
|
//[STICKER_SQL_FIND_LT] =
|
|
|
|
"SELECT uri,value FROM sticker WHERE type=? AND uri LIKE (? || '%') AND name=? AND value<?",
|
|
|
|
|
|
|
|
//[STICKER_SQL_FIND_GT] =
|
|
|
|
"SELECT uri,value FROM sticker WHERE type=? AND uri LIKE (? || '%') AND name=? AND value>?",
|
2009-03-27 17:05:04 +01:00
|
|
|
};
|
|
|
|
|
2009-01-19 18:51:57 +01:00
|
|
|
static const char sticker_sql_create[] =
|
|
|
|
"CREATE TABLE IF NOT EXISTS sticker("
|
|
|
|
" type VARCHAR NOT NULL, "
|
|
|
|
" uri VARCHAR NOT NULL, "
|
|
|
|
" name VARCHAR NOT NULL, "
|
|
|
|
" value VARCHAR NOT NULL"
|
|
|
|
");"
|
|
|
|
"CREATE UNIQUE INDEX IF NOT EXISTS"
|
|
|
|
" sticker_value ON sticker(type, uri, name);"
|
|
|
|
"";
|
|
|
|
|
|
|
|
static sqlite3 *sticker_db;
|
2013-10-15 22:04:17 +02:00
|
|
|
static sqlite3_stmt *sticker_stmt[ARRAY_SIZE(sticker_sql)];
|
2009-01-19 18:51:57 +01:00
|
|
|
|
|
|
|
static sqlite3_stmt *
|
2013-08-10 18:02:44 +02:00
|
|
|
sticker_prepare(const char *sql, Error &error)
|
2009-01-19 18:51:57 +01:00
|
|
|
{
|
|
|
|
sqlite3_stmt *stmt;
|
2014-12-12 14:09:33 +01:00
|
|
|
int ret = sqlite3_prepare_v2(sticker_db, sql, -1, &stmt, nullptr);
|
2009-07-05 08:29:47 +02:00
|
|
|
if (ret != SQLITE_OK) {
|
2014-12-12 20:49:00 +01:00
|
|
|
error.Format(sqlite_domain, ret,
|
2013-08-10 18:02:44 +02:00
|
|
|
"sqlite3_prepare_v2() failed: %s",
|
|
|
|
sqlite3_errmsg(sticker_db));
|
2013-10-19 18:19:03 +02:00
|
|
|
return nullptr;
|
2009-07-05 08:29:47 +02:00
|
|
|
}
|
2009-01-19 18:51:57 +01:00
|
|
|
|
|
|
|
return stmt;
|
|
|
|
}
|
|
|
|
|
2009-07-05 08:29:47 +02:00
|
|
|
bool
|
2013-10-17 21:59:35 +02:00
|
|
|
sticker_global_init(Path path, Error &error)
|
2009-01-19 18:51:57 +01:00
|
|
|
{
|
2013-10-17 21:59:35 +02:00
|
|
|
assert(!path.IsNull());
|
2009-01-19 18:51:57 +01:00
|
|
|
|
2013-10-17 21:59:35 +02:00
|
|
|
int ret;
|
2009-01-19 18:51:57 +01:00
|
|
|
|
|
|
|
/* open/create the sqlite database */
|
|
|
|
|
2013-08-07 19:54:38 +02:00
|
|
|
ret = sqlite3_open(path.c_str(), &sticker_db);
|
2009-07-05 08:29:47 +02:00
|
|
|
if (ret != SQLITE_OK) {
|
2013-08-07 19:54:38 +02:00
|
|
|
const std::string utf8 = path.ToUTF8();
|
2014-12-12 20:49:00 +01:00
|
|
|
error.Format(sqlite_domain, ret,
|
|
|
|
"Failed to open sqlite database '%s': %s",
|
|
|
|
utf8.c_str(), sqlite3_errmsg(sticker_db));
|
2009-07-05 08:29:47 +02:00
|
|
|
return false;
|
|
|
|
}
|
2009-01-19 18:51:57 +01:00
|
|
|
|
|
|
|
/* create the table and index */
|
|
|
|
|
2013-10-19 18:19:03 +02:00
|
|
|
ret = sqlite3_exec(sticker_db, sticker_sql_create,
|
|
|
|
nullptr, nullptr, nullptr);
|
2009-07-05 08:29:47 +02:00
|
|
|
if (ret != SQLITE_OK) {
|
2014-12-12 20:49:00 +01:00
|
|
|
error.Format(sqlite_domain, ret,
|
2013-08-10 18:02:44 +02:00
|
|
|
"Failed to create sticker table: %s",
|
|
|
|
sqlite3_errmsg(sticker_db));
|
2009-07-05 08:29:47 +02:00
|
|
|
return false;
|
|
|
|
}
|
2009-01-19 18:51:57 +01:00
|
|
|
|
|
|
|
/* prepare the statements we're going to use */
|
|
|
|
|
2013-10-15 22:04:17 +02:00
|
|
|
for (unsigned i = 0; i < ARRAY_SIZE(sticker_sql); ++i) {
|
2013-10-19 18:19:03 +02:00
|
|
|
assert(sticker_sql[i] != nullptr);
|
2009-01-19 18:51:57 +01:00
|
|
|
|
2013-08-10 18:02:44 +02:00
|
|
|
sticker_stmt[i] = sticker_prepare(sticker_sql[i], error);
|
2013-10-19 18:19:03 +02:00
|
|
|
if (sticker_stmt[i] == nullptr)
|
2009-07-05 08:29:47 +02:00
|
|
|
return false;
|
2009-03-27 17:05:04 +01:00
|
|
|
}
|
2009-07-05 08:29:47 +02:00
|
|
|
|
|
|
|
return true;
|
2009-01-19 18:51:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-12-12 14:12:31 +01:00
|
|
|
sticker_global_finish()
|
2009-01-19 18:51:57 +01:00
|
|
|
{
|
2013-10-19 18:19:03 +02:00
|
|
|
if (sticker_db == nullptr)
|
2009-01-19 18:51:57 +01:00
|
|
|
/* not configured */
|
|
|
|
return;
|
|
|
|
|
2013-10-15 22:04:17 +02:00
|
|
|
for (unsigned i = 0; i < ARRAY_SIZE(sticker_stmt); ++i) {
|
2013-10-19 18:19:03 +02:00
|
|
|
assert(sticker_stmt[i] != nullptr);
|
2009-03-27 17:05:04 +01:00
|
|
|
|
|
|
|
sqlite3_finalize(sticker_stmt[i]);
|
|
|
|
}
|
|
|
|
|
2009-01-19 18:51:57 +01:00
|
|
|
sqlite3_close(sticker_db);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2014-12-12 14:12:31 +01:00
|
|
|
sticker_enabled()
|
2009-01-19 18:51:57 +01:00
|
|
|
{
|
2013-10-19 18:19:03 +02:00
|
|
|
return sticker_db != nullptr;
|
2009-01-19 18:51:57 +01:00
|
|
|
}
|
|
|
|
|
2013-10-17 19:37:51 +02:00
|
|
|
std::string
|
2014-12-12 14:13:14 +01:00
|
|
|
sticker_load_value(const char *type, const char *uri, const char *name,
|
|
|
|
Error &error)
|
2009-01-19 18:51:57 +01:00
|
|
|
{
|
2009-03-27 17:05:04 +01:00
|
|
|
sqlite3_stmt *const stmt = sticker_stmt[STICKER_SQL_GET];
|
2009-01-19 18:51:57 +01:00
|
|
|
|
|
|
|
assert(sticker_enabled());
|
2013-10-19 18:19:03 +02:00
|
|
|
assert(type != nullptr);
|
|
|
|
assert(uri != nullptr);
|
|
|
|
assert(name != nullptr);
|
2009-01-19 18:51:57 +01:00
|
|
|
|
2015-11-06 09:37:07 +01:00
|
|
|
if (StringIsEmpty(name))
|
2013-10-17 19:37:51 +02:00
|
|
|
return std::string();
|
2009-01-19 18:51:57 +01:00
|
|
|
|
2014-12-12 14:13:14 +01:00
|
|
|
if (!BindAll(error, stmt, type, uri, name))
|
2013-10-17 19:37:51 +02:00
|
|
|
return std::string();
|
2009-01-19 18:51:57 +01:00
|
|
|
|
2013-10-17 19:37:51 +02:00
|
|
|
std::string value;
|
2014-12-12 14:13:14 +01:00
|
|
|
if (ExecuteRow(stmt, error))
|
2013-10-17 19:37:51 +02:00
|
|
|
value = (const char*)sqlite3_column_text(stmt, 0);
|
2009-01-19 18:51:57 +01:00
|
|
|
|
2009-03-27 17:05:04 +01:00
|
|
|
sqlite3_reset(stmt);
|
|
|
|
sqlite3_clear_bindings(stmt);
|
2009-01-19 18:51:57 +01:00
|
|
|
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2009-03-14 14:21:11 +01:00
|
|
|
static bool
|
2013-01-03 02:39:08 +01:00
|
|
|
sticker_list_values(std::map<std::string, std::string> &table,
|
2014-12-12 14:13:14 +01:00
|
|
|
const char *type, const char *uri,
|
|
|
|
Error &error)
|
2009-03-03 07:49:23 +01:00
|
|
|
{
|
2009-03-27 17:05:04 +01:00
|
|
|
sqlite3_stmt *const stmt = sticker_stmt[STICKER_SQL_LIST];
|
2009-03-03 07:49:23 +01:00
|
|
|
|
2013-10-19 18:19:03 +02:00
|
|
|
assert(type != nullptr);
|
|
|
|
assert(uri != nullptr);
|
2009-03-03 07:49:23 +01:00
|
|
|
assert(sticker_enabled());
|
|
|
|
|
2014-12-12 14:13:14 +01:00
|
|
|
if (!BindAll(error, stmt, type, uri))
|
2009-03-14 14:21:11 +01:00
|
|
|
return false;
|
2009-03-03 07:49:23 +01:00
|
|
|
|
2014-12-12 14:13:14 +01:00
|
|
|
const bool success = ExecuteForEach(stmt, error, [stmt, &table](){
|
2014-12-12 20:32:58 +01:00
|
|
|
const char *name = (const char *)sqlite3_column_text(stmt, 0);
|
|
|
|
const char *value = (const char *)sqlite3_column_text(stmt, 1);
|
2013-01-03 02:39:08 +01:00
|
|
|
table.insert(std::make_pair(name, value));
|
2014-12-12 20:32:58 +01:00
|
|
|
});
|
2009-03-03 07:49:23 +01:00
|
|
|
|
2009-03-27 17:05:04 +01:00
|
|
|
sqlite3_reset(stmt);
|
|
|
|
sqlite3_clear_bindings(stmt);
|
2009-03-03 07:49:23 +01:00
|
|
|
|
2014-12-12 20:32:58 +01:00
|
|
|
return success;
|
2009-03-03 07:49:23 +01:00
|
|
|
}
|
|
|
|
|
2009-01-19 18:51:57 +01:00
|
|
|
static bool
|
|
|
|
sticker_update_value(const char *type, const char *uri,
|
2014-12-12 14:13:14 +01:00
|
|
|
const char *name, const char *value,
|
|
|
|
Error &error)
|
2009-01-19 18:51:57 +01:00
|
|
|
{
|
2009-03-27 17:05:04 +01:00
|
|
|
sqlite3_stmt *const stmt = sticker_stmt[STICKER_SQL_UPDATE];
|
2009-01-19 18:51:57 +01:00
|
|
|
|
2013-10-19 18:19:03 +02:00
|
|
|
assert(type != nullptr);
|
|
|
|
assert(uri != nullptr);
|
|
|
|
assert(name != nullptr);
|
2009-01-19 18:51:57 +01:00
|
|
|
assert(*name != 0);
|
2013-10-19 18:19:03 +02:00
|
|
|
assert(value != nullptr);
|
2009-01-19 18:51:57 +01:00
|
|
|
|
|
|
|
assert(sticker_enabled());
|
|
|
|
|
2014-12-12 14:13:14 +01:00
|
|
|
if (!BindAll(error, stmt, value, type, uri, name))
|
2009-01-19 18:51:57 +01:00
|
|
|
return false;
|
|
|
|
|
2014-12-12 14:13:14 +01:00
|
|
|
bool modified = ExecuteModified(stmt, error);
|
2009-01-19 18:51:57 +01:00
|
|
|
|
2009-03-27 17:05:04 +01:00
|
|
|
sqlite3_reset(stmt);
|
|
|
|
sqlite3_clear_bindings(stmt);
|
2009-01-19 18:51:57 +01:00
|
|
|
|
2014-12-12 21:21:56 +01:00
|
|
|
if (modified)
|
|
|
|
idle_add(IDLE_STICKER);
|
|
|
|
return modified;
|
2009-01-19 18:51:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
sticker_insert_value(const char *type, const char *uri,
|
2014-12-12 14:13:14 +01:00
|
|
|
const char *name, const char *value,
|
|
|
|
Error &error)
|
2009-01-19 18:51:57 +01:00
|
|
|
{
|
2009-03-27 17:05:04 +01:00
|
|
|
sqlite3_stmt *const stmt = sticker_stmt[STICKER_SQL_INSERT];
|
2009-01-19 18:51:57 +01:00
|
|
|
|
2013-10-19 18:19:03 +02:00
|
|
|
assert(type != nullptr);
|
|
|
|
assert(uri != nullptr);
|
|
|
|
assert(name != nullptr);
|
2009-01-19 18:51:57 +01:00
|
|
|
assert(*name != 0);
|
2013-10-19 18:19:03 +02:00
|
|
|
assert(value != nullptr);
|
2009-01-19 18:51:57 +01:00
|
|
|
|
|
|
|
assert(sticker_enabled());
|
|
|
|
|
2014-12-12 14:13:14 +01:00
|
|
|
if (!BindAll(error, stmt, type, uri, name, value))
|
2009-01-19 18:51:57 +01:00
|
|
|
return false;
|
|
|
|
|
2014-12-12 14:13:14 +01:00
|
|
|
bool success = ExecuteCommand(stmt, error);
|
2009-01-19 18:51:57 +01:00
|
|
|
|
2009-03-27 17:05:04 +01:00
|
|
|
sqlite3_reset(stmt);
|
|
|
|
sqlite3_clear_bindings(stmt);
|
2009-01-19 18:51:57 +01:00
|
|
|
|
2014-12-12 21:23:22 +01:00
|
|
|
if (success)
|
|
|
|
idle_add(IDLE_STICKER);
|
|
|
|
return success;
|
2009-01-19 18:51:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
sticker_store_value(const char *type, const char *uri,
|
2014-12-12 14:13:14 +01:00
|
|
|
const char *name, const char *value,
|
|
|
|
Error &error)
|
2009-01-19 18:51:57 +01:00
|
|
|
{
|
|
|
|
assert(sticker_enabled());
|
2013-10-19 18:19:03 +02:00
|
|
|
assert(type != nullptr);
|
|
|
|
assert(uri != nullptr);
|
|
|
|
assert(name != nullptr);
|
|
|
|
assert(value != nullptr);
|
2009-01-19 18:51:57 +01:00
|
|
|
|
2015-11-06 09:37:07 +01:00
|
|
|
if (StringIsEmpty(name))
|
2009-01-19 18:51:57 +01:00
|
|
|
return false;
|
|
|
|
|
2014-12-12 14:13:14 +01:00
|
|
|
return sticker_update_value(type, uri, name, value, error) ||
|
|
|
|
sticker_insert_value(type, uri, name, value, error);
|
2009-01-19 18:51:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2014-12-12 14:13:14 +01:00
|
|
|
sticker_delete(const char *type, const char *uri, Error &error)
|
2009-01-19 18:51:57 +01:00
|
|
|
{
|
2009-03-27 17:05:04 +01:00
|
|
|
sqlite3_stmt *const stmt = sticker_stmt[STICKER_SQL_DELETE];
|
2009-01-19 18:51:57 +01:00
|
|
|
|
|
|
|
assert(sticker_enabled());
|
2013-10-19 18:19:03 +02:00
|
|
|
assert(type != nullptr);
|
|
|
|
assert(uri != nullptr);
|
2009-01-19 18:51:57 +01:00
|
|
|
|
2014-12-12 14:13:14 +01:00
|
|
|
if (!BindAll(error, stmt, type, uri))
|
2009-01-19 18:51:57 +01:00
|
|
|
return false;
|
|
|
|
|
2014-12-12 14:13:14 +01:00
|
|
|
bool modified = ExecuteModified(stmt, error);
|
2009-01-19 18:51:57 +01:00
|
|
|
|
2009-03-27 17:05:04 +01:00
|
|
|
sqlite3_reset(stmt);
|
|
|
|
sqlite3_clear_bindings(stmt);
|
2009-01-19 18:51:57 +01:00
|
|
|
|
2014-12-12 21:21:56 +01:00
|
|
|
if (modified)
|
|
|
|
idle_add(IDLE_STICKER);
|
|
|
|
return modified;
|
2009-01-19 18:51:57 +01:00
|
|
|
}
|
2009-03-14 14:20:01 +01:00
|
|
|
|
2009-04-28 20:23:27 +02:00
|
|
|
bool
|
2014-12-12 14:13:14 +01:00
|
|
|
sticker_delete_value(const char *type, const char *uri, const char *name,
|
|
|
|
Error &error)
|
2009-04-28 20:23:27 +02:00
|
|
|
{
|
|
|
|
sqlite3_stmt *const stmt = sticker_stmt[STICKER_SQL_DELETE_VALUE];
|
|
|
|
|
|
|
|
assert(sticker_enabled());
|
2013-10-19 18:19:03 +02:00
|
|
|
assert(type != nullptr);
|
|
|
|
assert(uri != nullptr);
|
2009-04-28 20:23:27 +02:00
|
|
|
|
2014-12-12 14:13:14 +01:00
|
|
|
if (!BindAll(error, stmt, type, uri, name))
|
2009-04-28 20:23:27 +02:00
|
|
|
return false;
|
|
|
|
|
2014-12-12 14:13:14 +01:00
|
|
|
bool modified = ExecuteModified(stmt, error);
|
2009-04-28 20:23:27 +02:00
|
|
|
|
|
|
|
sqlite3_reset(stmt);
|
|
|
|
sqlite3_clear_bindings(stmt);
|
|
|
|
|
2014-12-12 21:21:56 +01:00
|
|
|
if (modified)
|
|
|
|
idle_add(IDLE_STICKER);
|
|
|
|
return modified;
|
2009-04-28 20:23:27 +02:00
|
|
|
}
|
|
|
|
|
2009-03-14 14:20:01 +01:00
|
|
|
void
|
2015-01-28 19:33:56 +01:00
|
|
|
sticker_free(Sticker *sticker)
|
2009-03-14 14:20:01 +01:00
|
|
|
{
|
2013-01-03 02:39:08 +01:00
|
|
|
delete sticker;
|
2009-03-14 14:20:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const char *
|
2015-01-28 19:33:56 +01:00
|
|
|
sticker_get_value(const Sticker &sticker, const char *name)
|
2009-03-14 14:20:01 +01:00
|
|
|
{
|
2013-10-19 18:48:38 +02:00
|
|
|
auto i = sticker.table.find(name);
|
|
|
|
if (i == sticker.table.end())
|
2013-01-03 02:39:08 +01:00
|
|
|
return nullptr;
|
2009-03-14 14:20:01 +01:00
|
|
|
|
2013-01-03 02:39:08 +01:00
|
|
|
return i->second.c_str();
|
2009-03-14 14:20:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-01-28 19:33:56 +01:00
|
|
|
sticker_foreach(const Sticker &sticker,
|
2009-03-14 14:20:01 +01:00
|
|
|
void (*func)(const char *name, const char *value,
|
2013-10-17 19:37:51 +02:00
|
|
|
void *user_data),
|
|
|
|
void *user_data)
|
2009-03-14 14:20:01 +01:00
|
|
|
{
|
2013-10-19 18:48:38 +02:00
|
|
|
for (const auto &i : sticker.table)
|
2013-01-03 02:39:08 +01:00
|
|
|
func(i.first.c_str(), i.second.c_str(), user_data);
|
2009-03-14 14:20:01 +01:00
|
|
|
}
|
|
|
|
|
2015-01-28 19:33:56 +01:00
|
|
|
Sticker *
|
2014-12-12 14:13:14 +01:00
|
|
|
sticker_load(const char *type, const char *uri, Error &error)
|
2009-03-14 14:20:01 +01:00
|
|
|
{
|
2015-01-28 19:33:56 +01:00
|
|
|
Sticker s;
|
2009-03-14 14:21:11 +01:00
|
|
|
|
2014-12-12 14:13:14 +01:00
|
|
|
if (!sticker_list_values(s.table, type, uri, error))
|
2013-10-19 18:19:03 +02:00
|
|
|
return nullptr;
|
2009-03-14 14:20:01 +01:00
|
|
|
|
2013-01-03 02:39:08 +01:00
|
|
|
if (s.table.empty())
|
2009-03-14 14:21:11 +01:00
|
|
|
/* don't return empty sticker objects */
|
2013-10-19 18:19:03 +02:00
|
|
|
return nullptr;
|
2009-03-14 14:20:01 +01:00
|
|
|
|
2015-01-28 19:33:56 +01:00
|
|
|
return new Sticker(std::move(s));
|
2009-03-14 14:20:01 +01:00
|
|
|
}
|
2009-04-01 18:42:06 +02:00
|
|
|
|
2014-12-12 22:04:43 +01:00
|
|
|
static sqlite3_stmt *
|
|
|
|
BindFind(const char *type, const char *base_uri, const char *name,
|
2014-12-12 13:47:57 +01:00
|
|
|
StickerOperator op, const char *value,
|
2014-12-12 22:04:43 +01:00
|
|
|
Error &error)
|
|
|
|
{
|
|
|
|
assert(type != nullptr);
|
|
|
|
assert(name != nullptr);
|
|
|
|
|
|
|
|
if (base_uri == nullptr)
|
|
|
|
base_uri = "";
|
|
|
|
|
2014-12-12 13:47:57 +01:00
|
|
|
switch (op) {
|
|
|
|
case StickerOperator::EXISTS:
|
|
|
|
return BindAllOrNull(error, sticker_stmt[STICKER_SQL_FIND],
|
|
|
|
type, base_uri, name);
|
|
|
|
|
2014-12-12 22:12:19 +01:00
|
|
|
case StickerOperator::EQUALS:
|
|
|
|
return BindAllOrNull(error,
|
|
|
|
sticker_stmt[STICKER_SQL_FIND_VALUE],
|
|
|
|
type, base_uri, name, value);
|
2014-12-12 22:26:04 +01:00
|
|
|
|
|
|
|
case StickerOperator::LESS_THAN:
|
|
|
|
return BindAllOrNull(error,
|
|
|
|
sticker_stmt[STICKER_SQL_FIND_LT],
|
|
|
|
type, base_uri, name, value);
|
|
|
|
|
|
|
|
case StickerOperator::GREATER_THAN:
|
|
|
|
return BindAllOrNull(error,
|
|
|
|
sticker_stmt[STICKER_SQL_FIND_GT],
|
|
|
|
type, base_uri, name, value);
|
2014-12-12 22:12:19 +01:00
|
|
|
}
|
2014-12-12 13:47:57 +01:00
|
|
|
|
|
|
|
assert(false);
|
|
|
|
gcc_unreachable();
|
2014-12-12 22:04:43 +01:00
|
|
|
}
|
|
|
|
|
2009-04-01 18:42:06 +02:00
|
|
|
bool
|
|
|
|
sticker_find(const char *type, const char *base_uri, const char *name,
|
2014-12-12 13:47:57 +01:00
|
|
|
StickerOperator op, const char *value,
|
2009-04-01 18:42:06 +02:00
|
|
|
void (*func)(const char *uri, const char *value,
|
2013-10-17 19:37:51 +02:00
|
|
|
void *user_data),
|
2014-12-12 14:13:14 +01:00
|
|
|
void *user_data,
|
|
|
|
Error &error)
|
2009-04-01 18:42:06 +02:00
|
|
|
{
|
2013-10-19 18:19:03 +02:00
|
|
|
assert(func != nullptr);
|
2009-04-01 18:42:06 +02:00
|
|
|
assert(sticker_enabled());
|
|
|
|
|
2014-12-12 13:47:57 +01:00
|
|
|
sqlite3_stmt *const stmt = BindFind(type, base_uri, name, op, value,
|
|
|
|
error);
|
2014-12-12 22:04:43 +01:00
|
|
|
if (stmt == nullptr)
|
2009-04-01 18:42:06 +02:00
|
|
|
return false;
|
|
|
|
|
2014-12-12 14:13:14 +01:00
|
|
|
const bool success = ExecuteForEach(stmt, error,
|
|
|
|
[stmt, func, user_data](){
|
2009-04-01 18:42:06 +02:00
|
|
|
func((const char*)sqlite3_column_text(stmt, 0),
|
|
|
|
(const char*)sqlite3_column_text(stmt, 1),
|
|
|
|
user_data);
|
2014-12-12 20:32:58 +01:00
|
|
|
});
|
2009-04-01 18:42:06 +02:00
|
|
|
|
|
|
|
sqlite3_reset(stmt);
|
|
|
|
sqlite3_clear_bindings(stmt);
|
|
|
|
|
2014-12-12 20:32:58 +01:00
|
|
|
return success;
|
2009-04-01 18:42:06 +02:00
|
|
|
}
|