libinvdb/source/include/dbassocobject_impl.hh

82 lines
3.1 KiB
C++

#ifndef INVENTORY_DBASSOCHIERARCHY_IMPL_HH_
#define INVENTORY_DBASSOCHIERARCHY_IMPL_HH_
#include "category.hh"
#include "dbcontainer.hh"
#include "dboperations.hh"
#include "dbsession.hh"
namespace inventory {
namespace datamodel {
template<class Object, class Derived>
template<template<class> class Container>
DBContainerPtr<Object, Container> DBAssocObject<Object, Derived>::get_objects(
db::DBSession &session,
DBObjectInserter<Object, Container> inserter,
DBContainerPtr<Object, Container> pcontainer, int offset, int limit) const {
const std::string prepared_stmt =
std::string("SELECT * FROM ") +
Object::sc_view +
std::string(" AS object JOIN ") + Derived::sc_membership_table +
std::string(" AS membr") +
std::string(" WHERE object.id = membr.object_id AND") +
std::string(" membr.assoc_id = ?") +
std::string(" ORDER BY object.name ASC LIMIT ? OFFSET ?");
auto stmt_cons =
[this](void *, PreparedStatement_T s, int offset, int limit) {
PreparedStatement_setInt(s, 1, (int)(DBObject<Derived>::id));
PreparedStatement_setInt(s, 2, limit);
PreparedStatement_setInt(s, 3, offset);
};
return db::DBOperations::get_all<Object, void *, Container>(session, NULL,
prepared_stmt.c_str(), stmt_cons, inserter, pcontainer, offset, limit);
}
template<class Object, class Derived>
template<template<class> class Container>
DBContainerPtr<Derived, Container> DBAssocObject<Object, Derived>::get_assoc(
db::DBSession &session, const Object& object,
DBObjectInserter<Derived, Container> inserter,
DBContainerPtr<Derived, Container> pcontainer, int offset, int limit) {
const std::string prepared_stmt =
std::string("SELECT * FROM ") +
Derived::sc_view +
std::string(" AS assoc JOIN ") + Derived::sc_membership_table +
std::string(" AS membr") +
std::string(" WHERE assoc.id = membr.object_id AND") +
std::string(" membr.assoc_id = ?") +
std::string(" ORDER BY assoc.name ASC LIMIT ? OFFSET ?");
auto stmt_cons =
[object](void *, PreparedStatement_T s, int offset, int limit) {
PreparedStatement_setInt(s, 1, (int)(object.id));
PreparedStatement_setInt(s, 2, limit);
PreparedStatement_setInt(s, 3, offset);
};
return db::DBOperations::get_all<Derived, void *, Container>(session, NULL,
prepared_stmt.c_str(), stmt_cons, inserter, pcontainer, offset, limit);
}
template<class Object, class Derived>
void DBAssocObject<Object, Derived>::create_membership_table(
db::DBSession &session) {
std::string membership_table =
std::string("CREATE TABLE \"") + Derived::sc_dbclass + "_membership\" ( \n\
`object_id` INTEGER NOT NULL, \n\
`assoc_id` INTEGER NOT NULL, \n\
FOREIGN KEY(`object_id`) REFERENCES " + Object::sc_table + " ( id ), \n\
FOREIGN KEY(`assoc_id`) REFERENCES " + Derived::sc_table + "( id ) );";
db::DBOperations::execute<void *>(session, NULL, membership_table.c_str(),
[](void *, PreparedStatement_T s) {});
}
}
}
#include "dbobject_impl.hh"
#endif