1
0
Fork 0
mirror of https://github.com/luanti-org/luanti.git synced 2025-07-02 16:38:41 +00:00

Add MetaDataRef:get_keys (#12841)

This commit is contained in:
Jude Melton-Houghton 2022-11-15 10:45:12 -05:00 committed by GitHub
parent 1a045da0dd
commit cd8a7fe472
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
20 changed files with 125 additions and 0 deletions

View file

@ -92,6 +92,17 @@ bool Database_Dummy::getModEntries(const std::string &modname, StringMap *storag
return true;
}
bool Database_Dummy::getModKeys(const std::string &modname, std::vector<std::string> *storage)
{
const auto mod_pair = m_mod_meta_database.find(modname);
if (mod_pair != m_mod_meta_database.cend()) {
storage->reserve(storage->size() + mod_pair->second.size());
for (const auto &pair : mod_pair->second)
storage->push_back(pair.first);
}
return true;
}
bool Database_Dummy::getModEntry(const std::string &modname,
const std::string &key, std::string *value)
{

View file

@ -38,6 +38,7 @@ public:
void listPlayers(std::vector<std::string> &res);
bool getModEntries(const std::string &modname, StringMap *storage);
bool getModKeys(const std::string &modname, std::vector<std::string> *storage);
bool getModEntry(const std::string &modname,
const std::string &key, std::string *value);
bool hasModEntry(const std::string &modname, const std::string &key);

View file

@ -396,6 +396,21 @@ bool ModMetadataDatabaseFiles::getModEntries(const std::string &modname, StringM
return true;
}
bool ModMetadataDatabaseFiles::getModKeys(const std::string &modname,
std::vector<std::string> *storage)
{
Json::Value *meta = getOrCreateJson(modname);
if (!meta)
return false;
std::vector<std::string> keys = meta->getMemberNames();
storage->reserve(storage->size() + keys.size());
for (std::string &key : keys)
storage->push_back(std::move(key));
return true;
}
bool ModMetadataDatabaseFiles::getModEntry(const std::string &modname,
const std::string &key, std::string *value)
{

View file

@ -79,6 +79,7 @@ public:
virtual ~ModMetadataDatabaseFiles() = default;
virtual bool getModEntries(const std::string &modname, StringMap *storage);
virtual bool getModKeys(const std::string &modname, std::vector<std::string> *storage);
virtual bool getModEntry(const std::string &modname,
const std::string &key, std::string *value);
virtual bool hasModEntry(const std::string &modname, const std::string &key);

View file

@ -775,6 +775,7 @@ ModMetadataDatabaseSQLite3::~ModMetadataDatabaseSQLite3()
FINALIZE_STATEMENT(m_stmt_set)
FINALIZE_STATEMENT(m_stmt_has)
FINALIZE_STATEMENT(m_stmt_get)
FINALIZE_STATEMENT(m_stmt_get_keys)
FINALIZE_STATEMENT(m_stmt_get_all)
}
@ -796,6 +797,7 @@ void ModMetadataDatabaseSQLite3::createDatabase()
void ModMetadataDatabaseSQLite3::initStatements()
{
PREPARE_STATEMENT(get_all, "SELECT `key`, `value` FROM `entries` WHERE `modname` = ?");
PREPARE_STATEMENT(get_keys, "SELECT `key` FROM `entries` WHERE `modname` = ?");
PREPARE_STATEMENT(get,
"SELECT `value` FROM `entries` WHERE `modname` = ? AND `key` = ? LIMIT 1");
PREPARE_STATEMENT(has,
@ -825,6 +827,24 @@ bool ModMetadataDatabaseSQLite3::getModEntries(const std::string &modname, Strin
return true;
}
bool ModMetadataDatabaseSQLite3::getModKeys(const std::string &modname,
std::vector<std::string> *storage)
{
verifyDatabase();
str_to_sqlite(m_stmt_get_keys, 1, modname);
while (sqlite3_step(m_stmt_get_keys) == SQLITE_ROW) {
const char *key_data = (const char *) sqlite3_column_blob(m_stmt_get_keys, 0);
size_t key_len = sqlite3_column_bytes(m_stmt_get_keys, 0);
storage->emplace_back(key_data, key_len);
}
sqlite3_vrfy(sqlite3_errcode(m_database), SQLITE_DONE);
sqlite3_reset(m_stmt_get_keys);
return true;
}
bool ModMetadataDatabaseSQLite3::getModEntry(const std::string &modname,
const std::string &key, std::string *value)
{

View file

@ -240,6 +240,7 @@ public:
virtual ~ModMetadataDatabaseSQLite3();
virtual bool getModEntries(const std::string &modname, StringMap *storage);
virtual bool getModKeys(const std::string &modname, std::vector<std::string> *storage);
virtual bool getModEntry(const std::string &modname,
const std::string &key, std::string *value);
virtual bool hasModEntry(const std::string &modname, const std::string &key);
@ -258,6 +259,7 @@ protected:
private:
sqlite3_stmt *m_stmt_get_all = nullptr;
sqlite3_stmt *m_stmt_get_keys = nullptr;
sqlite3_stmt *m_stmt_get = nullptr;
sqlite3_stmt *m_stmt_has = nullptr;
sqlite3_stmt *m_stmt_set = nullptr;

View file

@ -92,6 +92,7 @@ public:
virtual ~ModMetadataDatabase() = default;
virtual bool getModEntries(const std::string &modname, StringMap *storage) = 0;
virtual bool getModKeys(const std::string &modname, std::vector<std::string> *storage) = 0;
virtual bool hasModEntry(const std::string &modname, const std::string &key) = 0;
virtual bool getModEntry(const std::string &modname,
const std::string &key, std::string *value) = 0;