From c8b02258fc5044e576a1ee8b92401dde3eb38e2e Mon Sep 17 00:00:00 2001 From: jachoo Date: Tue, 21 Feb 2012 13:42:40 +0100 Subject: [PATCH] Add: removing data from DB --- src/db.cpp | 9 +++++++++ src/db.h | 26 ++++++++++++++++++++++++++ src/scriptapi.cpp | 30 +++++++++++++++++++++++++++++- 3 files changed, 64 insertions(+), 1 deletion(-) diff --git a/src/db.cpp b/src/db.cpp index 62b0248..73e80bb 100644 --- a/src/db.cpp +++ b/src/db.cpp @@ -208,6 +208,13 @@ ITable::ITable(sqlite3* database, const std::string& name, const std::string& ke throw FileNotGoodException("Cannot prepare write statement"); } + q = "DELETE FROM `"+name+"` WHERE `"+id_name+"` = ?"; + d = sqlite3_prepare_v2(m_database,q.c_str(), -1, &m_remove, NULL); + if(d != SQLITE_OK) { + //infostream<<"WARNING: Database list statment failed to prepare: "< + bool remove(const Key& key) + { + DBKeyTypeTraits::bind(m_remove,1,key); + int d = sqlite3_step(m_remove); + sqlite3_reset(m_remove); + return d == SQLITE_DONE; + } + //inserts all ids from tabale to given list template bool getKeys(core::list& list) @@ -212,6 +223,7 @@ protected: sqlite3* m_database; sqlite3_stmt *m_read; sqlite3_stmt *m_write; + sqlite3_stmt *m_remove; sqlite3_stmt *m_list; bool exec(const std::string& query); @@ -248,6 +260,13 @@ public: return ITable::get(key); } + //deletes row with given key + //if failed, returns false + inline bool remove(const Key& key) + { + return ITable::remove(key); + } + //inserts all ids from tabale to given list bool getKeys(core::list& list) { @@ -291,6 +310,13 @@ public: return ITable::get(key); } + //deletes row with given key + //if failed, returns false + inline bool remove(const Key& key) + { + return ITable::remove(key); + } + //inserts all ids from tabale to given list bool getKeys(core::list& list) { diff --git a/src/scriptapi.cpp b/src/scriptapi.cpp index e5b9a11..9610179 100644 --- a/src/scriptapi.cpp +++ b/src/scriptapi.cpp @@ -3746,12 +3746,16 @@ struct Databases { return get_table(table).get(key); } - //sets map meta data template bool set_data(int table, const Key& key, const Data& val) { return get_table(table).put(key,val); } + template bool remove_data(int table, const Key& key) + { + return get_table(table).remove(key); + } + } databases; // get_database(name) -> nil/int @@ -3876,6 +3880,29 @@ int l_set_table_data(lua_State *L) return luaL_error(L,"set_table_data - error occured"); } +// remove_table_data(int table, string key_type, key) +int l_remove_table_data(lua_State *L) +{ + try{ + + const int table_i = luaL_checkint(L, 1); + const std::string key_type = luaL_checkstring(L, 2); + const std::string key = param_to_binary(L, 3, key_type); + + /*std::cout << "Tabela: " << table_i + << ", klucz: " << key_type << ":" << key + << ", typ danych: " << data_type << std::endl;*/ + + databases.remove_data(table_i,key); + + return 0; + + }catch(std::exception&){} + + //we shall not be here if no error + return luaL_error(L,"remove_table_data - error occured"); +} + // get_inventory(location) static int l_get_inventory(lua_State *L) { @@ -3964,6 +3991,7 @@ static const struct luaL_Reg minetest_f [] = { {"get_db_table", l_get_db_table}, {"get_table_data", l_get_table_data}, {"set_table_data", l_set_table_data}, + {"remove_table_data", l_remove_table_data}, {"get_inventory", l_get_inventory}, {"get_digging_properties", l_get_digging_properties}, {"get_hitting_properties", l_get_hitting_properties},