135 lines
3.2 KiB
C++
135 lines
3.2 KiB
C++
#ifndef INVENTORY_DBFIELD_HH
|
|
#define INVENTORY_DBFIELD_HH
|
|
#include <string>
|
|
#include <vector>
|
|
#include <zdb.h>
|
|
#include "cxx-semantics.hh"
|
|
|
|
namespace inventory {
|
|
namespace datamodel {
|
|
|
|
template<class Derived>
|
|
class DBObject;
|
|
|
|
enum class DBRecordElementType {
|
|
UNKNOWN,
|
|
INTEGER,
|
|
UNSIGNED_INTEGER,
|
|
TEXT,
|
|
BLOB
|
|
};
|
|
|
|
struct DBRecordElementDesc {
|
|
const char *column_name;
|
|
const char *name;
|
|
const char *description;
|
|
};
|
|
|
|
// specialized for each class in DBObject tree
|
|
template<template<class> class Object>
|
|
class DBRecordDescTempl : semantics::non_constructible {};
|
|
|
|
template<class Object>
|
|
class DBRecordDesc : semantics::non_constructible {};
|
|
|
|
template<class Derived>
|
|
class DBRecordElement {
|
|
public:
|
|
DBRecordElement(const DBObject<Derived> *parent,
|
|
const struct DBRecordElementDesc *elem_desc);
|
|
|
|
const struct DBRecordElementDesc *element_description() {
|
|
return mcp_elem_desc;
|
|
}
|
|
|
|
DBRecordElementType type() const {
|
|
return m_type;
|
|
}
|
|
|
|
virtual void from_query(ResultSet_T) {};
|
|
virtual void insert_value(PreparedStatement_T stmt, int pos) const {};
|
|
|
|
protected:
|
|
const DBObject<Derived> * const m_parent;
|
|
const struct DBRecordElementDesc * const mcp_elem_desc;
|
|
|
|
DBRecordElementType m_type;
|
|
};
|
|
|
|
template<class Data, class Derived>
|
|
class DBField : public DBRecordElement<Derived> {
|
|
public:
|
|
DBField(const DBObject<Derived> *parent,
|
|
const struct DBRecordElementDesc *elem_desc);
|
|
|
|
DBField operator=(const DBField<Data, Derived> &assigned) {
|
|
m_value = assigned.m_value;
|
|
return *this;
|
|
}
|
|
|
|
Data operator=(const Data &assigned) {
|
|
return m_value = assigned;
|
|
}
|
|
|
|
operator Data() const {
|
|
return m_value;
|
|
}
|
|
|
|
virtual void from_query(ResultSet_T r) {};
|
|
virtual void insert_value(PreparedStatement_T stmt, int pos) const {};
|
|
|
|
protected:
|
|
Data m_value;
|
|
};
|
|
|
|
template<class Derived>
|
|
class DBInt : public DBField<int, Derived> {
|
|
public:
|
|
DBInt(const DBObject<Derived> *parent,
|
|
const struct DBRecordElementDesc *elem_desc);
|
|
|
|
using DBField<int, Derived>::operator =;
|
|
void from_query(ResultSet_T r);
|
|
void insert_value(PreparedStatement_T stmt, int pos) const;
|
|
};
|
|
|
|
template<class Derived>
|
|
class DBUnsignedInt : public DBField<unsigned int, Derived> {
|
|
public:
|
|
DBUnsignedInt(const DBObject<Derived> *parent,
|
|
const struct DBRecordElementDesc *elem_desc);
|
|
|
|
using DBField<unsigned int, Derived>::operator =;
|
|
void from_query(ResultSet_T r);
|
|
void insert_value(PreparedStatement_T stmt, int pos) const;
|
|
};
|
|
|
|
template<class Derived>
|
|
class DBString : public DBField<std::string, Derived> {
|
|
public:
|
|
DBString(const DBObject<Derived> *parent,
|
|
const struct DBRecordElementDesc *elem_desc);
|
|
|
|
using DBField<std::string, Derived>::operator =;
|
|
void from_query(ResultSet_T r);
|
|
void insert_value(PreparedStatement_T stmt, int pos) const;
|
|
|
|
const char *c_str() const;
|
|
};
|
|
|
|
template<class Derived>
|
|
class DBBlob : public DBField<std::vector<char>, Derived> {
|
|
public:
|
|
DBBlob(const DBObject<Derived> *parent,
|
|
const struct DBRecordElementDesc *elem_desc);
|
|
|
|
using DBField<std::vector<char>, Derived>::operator =;
|
|
void from_query(ResultSet_T r);
|
|
void insert_value(PreparedStatement_T stmt, int pos) const;
|
|
};
|
|
|
|
}
|
|
}
|
|
|
|
#endif
|