Add: removing data from DB
parent
6b633307de
commit
c8b02258fc
|
@ -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: "<<sqlite3_errmsg(m_database)<<std::endl;
|
||||
throw FileNotGoodException("Cannot prepare remove statement");
|
||||
}
|
||||
|
||||
q = "SELECT `"+id_name+"` FROM `"+name+"`";
|
||||
d = sqlite3_prepare_v2(m_database,q.c_str(), -1, &m_list, NULL);
|
||||
if(d != SQLITE_OK) {
|
||||
|
@ -221,6 +228,8 @@ ITable::~ITable() {
|
|||
sqlite3_finalize(m_read);
|
||||
if(m_write)
|
||||
sqlite3_finalize(m_write);
|
||||
if(m_remove)
|
||||
sqlite3_finalize(m_remove);
|
||||
if(m_list)
|
||||
sqlite3_finalize(m_list);
|
||||
}
|
||||
|
|
26
src/db.h
26
src/db.h
|
@ -190,6 +190,17 @@ public:
|
|||
return d;
|
||||
}
|
||||
|
||||
//deletes row with given key
|
||||
//if failed, returns false
|
||||
template<class Key>
|
||||
bool remove(const Key& key)
|
||||
{
|
||||
DBKeyTypeTraits<Key>::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<class Key>
|
||||
bool getKeys(core::list<Key>& 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<Data>(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<Key>& list)
|
||||
{
|
||||
|
@ -291,6 +310,13 @@ public:
|
|||
return ITable::get<Data>(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<Key>& list)
|
||||
{
|
||||
|
|
|
@ -3746,12 +3746,16 @@ struct Databases {
|
|||
return get_table(table).get<Data>(key);
|
||||
}
|
||||
|
||||
//sets map meta data
|
||||
template<class Data, class Key> bool set_data(int table, const Key& key, const Data& val)
|
||||
{
|
||||
return get_table(table).put(key,val);
|
||||
}
|
||||
|
||||
template<class Key> 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},
|
||||
|
|
Loading…
Reference in New Issue