93 lines
2.5 KiB
C++
93 lines
2.5 KiB
C++
#ifndef INVENTORY_DBOPERATIONS_IMPL_HH_
|
|
#define INVENTORY_DBOPERATIONS_IMPL_HH_
|
|
#include "dboperations.hh"
|
|
#include "dbsession.hh"
|
|
#include "dbobject.hh"
|
|
#include "log.hh"
|
|
|
|
#include <zdb.h>
|
|
// refer to libzdb docs
|
|
#include <Exception.h>
|
|
|
|
namespace inventory {
|
|
namespace db {
|
|
|
|
template<class K>
|
|
void DBOperations::execute(
|
|
DBSession &session, const K &stmt_arg, const char *prepared_stmt,
|
|
StatementConstructorSingle<K> cons) {
|
|
LIBINVDB_LOG(Loglevel::DEBUG, std::string("[QUERY] ") + prepared_stmt);
|
|
|
|
Connection_T conn = session.get_connection();
|
|
PreparedStatement_T s = Connection_prepareStatement(conn, prepared_stmt);
|
|
|
|
cons(stmt_arg, s);
|
|
|
|
TRY {
|
|
PreparedStatement_execute(s);
|
|
} CATCH(SQLException) {
|
|
// TODO exception wrapper
|
|
}
|
|
END_TRY;
|
|
|
|
session.return_connection(conn);
|
|
}
|
|
|
|
template<class T, class K>
|
|
datamodel::DBObjectPtr<T> DBOperations::get_single(
|
|
DBSession &session, const K &stmt_arg, const char *prepared_stmt,
|
|
StatementConstructorSingle<K> cons) {
|
|
LIBINVDB_LOG(Loglevel::DEBUG, std::string("[QUERY] ") + prepared_stmt);
|
|
|
|
Connection_T conn = session.get_connection();
|
|
PreparedStatement_T s = Connection_prepareStatement(conn, prepared_stmt);
|
|
|
|
cons(stmt_arg, s);
|
|
|
|
ResultSet_T r = PreparedStatement_executeQuery(s);
|
|
ResultSet_next(r); // with unique key, only one item is expected
|
|
if (r == NULL)
|
|
throw std::runtime_error("No such item");
|
|
|
|
datamodel::DBObjectPtr<T> ret(new T);
|
|
ret->from_query(r);
|
|
session.return_connection(conn);
|
|
|
|
return ret;
|
|
}
|
|
|
|
template<class T, class K, template<class> class Container>
|
|
datamodel::DBContainerPtr<T, Container> DBOperations::get_all(
|
|
DBSession &session, const K &stmt_arg,
|
|
const char *prepared_stmt, StatementConstructor<K> cons,
|
|
datamodel::DBObjectInserter<T, Container> inserter,
|
|
datamodel::DBContainerPtr<T, Container> pcontainer,
|
|
int offset, int limit) {
|
|
LIBINVDB_LOG(Loglevel::DEBUG, std::string("[QUERY] ") + prepared_stmt);
|
|
|
|
if (!pcontainer)
|
|
pcontainer.reset(new Container<T>);
|
|
|
|
Connection_T conn = session.get_connection();
|
|
PreparedStatement_T s = Connection_prepareStatement(conn,
|
|
prepared_stmt);
|
|
|
|
cons(stmt_arg, s, offset, limit);
|
|
|
|
ResultSet_T r = PreparedStatement_executeQuery(s);
|
|
while (ResultSet_next(r)) {
|
|
datamodel::DBObjectPtr<T> el(new T);
|
|
el->from_query(r);
|
|
inserter(el, *pcontainer);
|
|
}
|
|
|
|
session.return_connection(conn);
|
|
return pcontainer;
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
#include "dbobject_impl.hh"
|
|
#endif
|