libinvdb/source/item.cc

169 lines
4.6 KiB
C++

#include "dbcontainer.hh"
#include "dbobjectptr.hh"
#include "item.hh"
#include "category.hh"
#include "dboperations.hh"
#include "dbsession.hh"
#include <stdexcept>
#include <zdb.h>
// refer to libzdb docs
#include <Exception.h>
namespace inventory {
namespace datamodel {
const char *Item::sc_table = "items";
const char *Item::sc_view = "item_view";
const char *Item::sc_dbclass = "item";
const struct DBRecordElementDesc DBRecordDesc<Item>::desc_desc = {
"description",
"Description",
""
};
const struct DBRecordElementDesc DBRecordDesc<Item>::time_added_desc = {
"time_added",
"Time added",
""
};
const struct DBRecordElementDesc DBRecordDesc<Item>::time_spoiled_desc = {
"time_spoiled",
"Time spoiled",
""
};
const struct DBRecordElementDesc DBRecordDesc<Item>::cash_worth_desc = {
"cash_worth",
"",
""
};
const struct DBRecordElementDesc DBRecordDesc<Item>::currency_desc = {
"currency_id",
"",
""
};
const struct DBRecordElementDesc DBRecordDesc<Item>::long_axis_desc = {
"long_axis",
"",
""
};
const struct DBRecordElementDesc DBRecordDesc<Item>::short_axis_desc = {
"short_axis",
"",
""
};
const struct DBRecordElementDesc DBRecordDesc<Item>::height_desc = {
"height",
"",
""
};
const struct DBRecordElementDesc DBRecordDesc<Item>::mass_desc = {
"mass",
"",
""
};
const struct DBRecordElementDesc DBRecordDesc<Item>::url_desc = {
"url",
"",
""
};
void Item::create_tables(db::DBSession &session) {
const char *create_table =
"CREATE TABLE \"items\" ( \n\
`id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE, \n\
`name` TEXT NOT NULL, \n\
`sup` INTEGER NOT NULL DEFAULT -1, \n\
`description` TEXT, \n\
`time_added` INTEGER DEFAULT -1, \n\
`time_spoiled` INTEGER DEFAULT -1, \n\
`cash_worth` INTEGER DEFAULT -1, \n\
`currency_id` INTEGER DEFAULT -1, \n\
`long_axis` INTEGER DEFAULT -1, \n\
`short_axis` INTEGER DEFAULT -1, \n\
`height` INTEGER DEFAULT -1, \n\
`mass` INTEGER DEFAULT -1, \n\
`url` TEXT, \n\
FOREIGN KEY(`sup`) REFERENCES items ( id ) );";
db::DBOperations::execute<void *>(session, NULL, create_table,
[](void *, PreparedStatement_T s) {});
DBHierarchicalObject<Item>::create_sub_view(session);
DBHierarchicalObject<Item>::create_sup_view(session);
const char *item_view =
"CREATE VIEW item_view AS SELECT * FROM item_sub JOIN items ON items.id = item_sub.id;";
db::DBOperations::execute<void *>(session, NULL, item_view,
[](void *, PreparedStatement_T s) {});
}
void Item::validate_tables(db::DBSession &session) {
}
void Item::remove_tables(db::DBSession &session) {
const char *drop_table = "DROP TABLE items";
db::DBOperations::execute<void *>(session, NULL, drop_table,
[](void *, PreparedStatement_T s) {});
const char *drop_item_sub = "DROP VIEW item_sub";
db::DBOperations::execute<void *>(session, NULL, drop_item_sub,
[](void *, PreparedStatement_T s) {});
const char *drop_item_sup = "DROP VIEW item_sup";
db::DBOperations::execute<void *>(session, NULL, drop_item_sup,
[](void *, PreparedStatement_T s) {});
const char *drop_item_view = "DROP VIEW item_view;";
db::DBOperations::execute<void *>(session, NULL, drop_item_view,
[](void *, PreparedStatement_T s) {});
}
/*
void Item::add_category(db::DBSession &session, const Category& category) const {
const std::string prepared_stmt =
std::string("INSERT INTO ") +
Category::sc_membership_table +
std::string(" (item_id, category_id) VALUES (?, ?)");
auto stmt_cons =
[this, category](void *, PreparedStatement_T s) {
PreparedStatement_setInt(s, 1, id);
PreparedStatement_setInt(s, 2, category.id);
};
db::DBOperations::execute<void *>(session, NULL, prepared_stmt.c_str(),
stmt_cons);
}
void Item::remove_category(db::DBSession &session, const Category& category) const {
const std::string prepared_stmt =
std::string("DELETE FROM ") +
Category::sc_membership_table +
std::string("WHERE item_id = ?");
auto stmt_cons =
[this](void *, PreparedStatement_T s) {
PreparedStatement_setInt(s, 1, id);
};
db::DBOperations::execute<void *>(session, NULL, prepared_stmt.c_str(),
stmt_cons);
}
*/
}
}