libinvdb/source/include/dbfield_impl.hh

127 lines
4.2 KiB
C++

#ifndef INVENTORY_DBFIELD_IMPL_HH_
#define INVENTORY_DBFIELD_IMPL_HH_
#include "dbobject.hh"
#include "dbobject_impl.hh"
#include "dbfield.hh"
#include <vector>
#include <string>
namespace inventory {
namespace datamodel {
template<class Data, class Derived>
DBField<Data, Derived>::DBField(const DBObject<Derived> *parent,
const struct DBRecordElementDesc *elem_desc)
: DBRecordElement<Derived>(parent, elem_desc) {}
template<class Derived>
DBInt<Derived>::DBInt(const DBObject<Derived> *parent,
const struct DBRecordElementDesc *elem_desc)
: DBField<int, Derived>(parent, elem_desc) {
// neither clang nor gcc would recognize m_value
// w/o explicit class path, not sure why
DBField<int, Derived>::m_value = -1;
DBRecordElement<Derived>::m_type = DBRecordElementType::INTEGER;
}
template<class Derived>
DBUnsignedInt<Derived>::DBUnsignedInt(const DBObject<Derived> *parent,
const struct DBRecordElementDesc *elem_desc)
: DBField<unsigned int, Derived>(parent, elem_desc) {
DBField<unsigned int, Derived>::m_value = 0;
DBRecordElement<Derived>::m_type = DBRecordElementType::UNSIGNED_INTEGER;
}
template<class Derived>
DBString<Derived>::DBString(const DBObject<Derived> *parent,
const struct DBRecordElementDesc *elem_desc)
: DBField<std::string, Derived>(parent, elem_desc) {
DBRecordElement<Derived>::m_type = DBRecordElementType::TEXT;
}
template<class Derived>
DBBlob<Derived>::DBBlob(const DBObject<Derived> *parent,
const struct DBRecordElementDesc *elem_desc)
: DBField<std::vector<char>, Derived>(parent, elem_desc) {
DBRecordElement<Derived>::m_type = DBRecordElementType::BLOB;
}
template<class Derived>
const char *DBString<Derived>::c_str() const {
return DBField<std::string, Derived>::m_value.c_str();
}
template<class Derived>
DBRecordElement<Derived>::DBRecordElement(const DBObject<Derived> *parent,
const struct DBRecordElementDesc *elem_desc)
: m_parent(parent), mcp_elem_desc(elem_desc) {
if (parent != NULL)
parent->register_element(this);
}
template<class Derived>
void DBInt<Derived>::from_query(ResultSet_T r) {
DBField<int, Derived>::m_value =
ResultSet_getIntByName(r,
DBRecordElement<Derived>::mcp_elem_desc->column_name);
}
template<class Derived>
void DBInt<Derived>::insert_value(PreparedStatement_T stmt, int pos) const {
PreparedStatement_setInt(stmt, pos, DBField<int, Derived>::m_value);
}
template<class Derived>
void DBUnsignedInt<Derived>::from_query(ResultSet_T r) {
DBField<unsigned int, Derived>::m_value =
ResultSet_getIntByName(r,
DBRecordElement<Derived>::mcp_elem_desc->column_name);
}
template<class Derived>
void DBUnsignedInt<Derived>::insert_value(PreparedStatement_T stmt, int pos) const {
PreparedStatement_setInt(stmt, pos, DBField<unsigned int, Derived>::m_value);
}
template<class Derived>
void DBString<Derived>::from_query(ResultSet_T r) {
const char * pname =
ResultSet_getStringByName(r,
DBRecordElement<Derived>::mcp_elem_desc->column_name);
if (pname != NULL)
DBField<std::string, Derived>::m_value = pname;
}
template<class Derived>
void DBString<Derived>::insert_value(PreparedStatement_T stmt, int pos) const {
PreparedStatement_setString(stmt, pos,
DBField<std::string, Derived>::m_value.c_str());
}
template<class Derived>
void DBBlob<Derived>::from_query(ResultSet_T r) {
int symbol_size;
// returns void* by default, and std::copy needs type size info
const char *psymbol = (const char *)(ResultSet_getBlobByName(r,
DBRecordElement<Derived>::mcp_elem_desc->column_name,
&symbol_size));
if (psymbol != NULL) {
DBField<std::vector<char>, Derived>::m_value.resize(symbol_size);
std::copy(psymbol, psymbol + symbol_size,
std::back_inserter(DBField<std::vector<char>, Derived>::m_value));
}
}
template<class Derived>
void DBBlob<Derived>::insert_value(PreparedStatement_T stmt, int pos) const {
PreparedStatement_setBlob(stmt, pos,
&DBField<std::vector<char>, Derived>::m_value.front(),
DBField<std::vector<char>, Derived>::m_value.size());
}
}
}
#endif