165 lines
4.7 KiB
C++
165 lines
4.7 KiB
C++
#ifndef INVENTORY_DBOBJECT_HH_
|
|
#define INVENTORY_DBOBJECT_HH_
|
|
#include <string>
|
|
#include <map>
|
|
#include <zdb.h>
|
|
#include "dbcontainer.hh"
|
|
#include "dbobjectptr.hh"
|
|
#include "dbcontainerptr.hh"
|
|
#include "dbfield.hh"
|
|
#include "cxx-semantics.hh"
|
|
|
|
namespace inventory {
|
|
namespace db {
|
|
class DBSession;
|
|
}
|
|
|
|
namespace datamodel {
|
|
template<class Derived>
|
|
class DBObject;
|
|
|
|
template<>
|
|
class DBRecordDescTempl<DBObject> : semantics::non_constructible {
|
|
public:
|
|
static const struct DBRecordElementDesc id_desc;
|
|
};
|
|
|
|
template<class Derived>
|
|
class DBObject {
|
|
template<class T>
|
|
friend class DBRecordElement;
|
|
|
|
public:
|
|
typedef std::map<std::string, DBRecordElement<Derived> *> RecordElementMap;
|
|
|
|
protected:
|
|
void register_element(DBRecordElement<Derived> *el) const;
|
|
|
|
// must be initialized before table elements
|
|
mutable RecordElementMap m_record_elements;
|
|
|
|
public:
|
|
enum class Id {
|
|
ROOT = -1,
|
|
UNDEFINED = -2
|
|
};
|
|
|
|
DBInt<Derived> id;
|
|
|
|
DBObject();
|
|
virtual ~DBObject() {}
|
|
|
|
static datamodel::DBObjectPtr<Derived> get(db::DBSession &session, int id);
|
|
|
|
template<template<class> class Container = datamodel::DBObjectVector>
|
|
static datamodel::DBContainerPtr<Derived, Container> get_all(
|
|
db::DBSession &session,
|
|
datamodel::DBObjectInserter<Derived, Container> inserter =
|
|
datamodel::DBObjectInserterImpl<Derived, Container>::inserter,
|
|
datamodel::DBContainerPtr<Derived, Container> pcontainer = {},
|
|
int offset = 0, int limit = -1);
|
|
|
|
// Table fields are initialized only in DBObject class,
|
|
// but there may be some additional information to be
|
|
// taken care of lower in the hierarchy, like children
|
|
// count in DBHierarchicalObject.
|
|
|
|
// Implementations still must call DBObject function.
|
|
virtual void from_query(ResultSet_T);
|
|
|
|
void insert(db::DBSession &sess) const;
|
|
void update(db::DBSession &sess) const;
|
|
void remove(db::DBSession &sess) const;
|
|
void refresh(db::DBSession &sess);
|
|
|
|
DBRecordElement<Derived> *operator[](const char *column_name) {
|
|
return m_record_elements[column_name];
|
|
}
|
|
|
|
const RecordElementMap &record_element_map() {
|
|
return m_record_elements;
|
|
}
|
|
|
|
std::string prepared_statement_columns() const;
|
|
std::string prepared_statement_value_placeholders() const;
|
|
|
|
bool is_root() const {
|
|
return id == (int)(Id::ROOT);
|
|
}
|
|
|
|
private:
|
|
void insert_values(PreparedStatement_T s) const;
|
|
};
|
|
|
|
template<class Derived>
|
|
class DBNamedObject;
|
|
|
|
template<>
|
|
class DBRecordDescTempl<DBNamedObject> : semantics::non_constructible {
|
|
public:
|
|
static const struct DBRecordElementDesc name_desc;
|
|
};
|
|
|
|
template<class Derived>
|
|
class DBNamedObject : public virtual DBObject<Derived> {
|
|
public:
|
|
DBString<Derived> name;
|
|
|
|
DBNamedObject();
|
|
virtual ~DBNamedObject() {}
|
|
|
|
static datamodel::DBObjectPtr<Derived> get(db::DBSession &session,
|
|
const char *name);
|
|
};
|
|
|
|
template<class Derived>
|
|
class DBHierarchicalObject;
|
|
|
|
template<>
|
|
class DBRecordDescTempl<DBHierarchicalObject> : semantics::non_constructible {
|
|
public:
|
|
static const struct DBRecordElementDesc sup_desc;
|
|
static const struct DBRecordElementDesc nsub_desc;
|
|
};
|
|
|
|
template<class Derived>
|
|
class DBHierarchicalObject : public virtual DBObject<Derived> {
|
|
public:
|
|
DBInt<Derived> sup;
|
|
|
|
// generated, not part of the table
|
|
DBInt<Derived> nsub;
|
|
|
|
DBHierarchicalObject();
|
|
virtual ~DBHierarchicalObject() {}
|
|
|
|
template<template<class> class Container = datamodel::DBObjectVector>
|
|
static datamodel::DBContainerPtr<Derived, Container> get_children(
|
|
db::DBSession &session, int id,
|
|
datamodel::DBObjectInserter<Derived, Container> inserter =
|
|
datamodel::DBObjectInserterImpl<Derived, Container>::inserter,
|
|
datamodel::DBContainerPtr<Derived, Container> pcontainer = {},
|
|
int offset = 0, int limit = -1);
|
|
|
|
template<template<class> class Container = datamodel::DBObjectVector>
|
|
datamodel::DBContainerPtr<Derived, Container> get_children(
|
|
db::DBSession &session,
|
|
datamodel::DBObjectInserter<Derived, Container> inserter =
|
|
datamodel::DBObjectInserterImpl<Derived, Container>::inserter,
|
|
datamodel::DBContainerPtr<Derived, Container> pcontainer = {},
|
|
int offset = 0, int limit = -1);
|
|
|
|
datamodel::DBObjectPtr<Derived> get_parent(db::DBSession &session);
|
|
|
|
virtual void from_query(ResultSet_T);
|
|
|
|
protected:
|
|
static void create_sub_view(db::DBSession &session);
|
|
static void create_sup_view(db::DBSession &session);
|
|
};
|
|
|
|
}
|
|
}
|
|
|
|
#endif
|