initial commit Base app, core lib, plugin system
This commit is contained in:
@@ -0,0 +1,48 @@
|
||||
#ifndef SERVICE_H
|
||||
#define SERVICE_H
|
||||
|
||||
#include <QList>
|
||||
#include <QSharedPointer>
|
||||
|
||||
#include <odb/core.hxx>
|
||||
#include <odb/transaction.hxx>
|
||||
#include <odb/database.hxx>
|
||||
#include <odb/result.hxx>
|
||||
|
||||
template<class T>
|
||||
class Service
|
||||
{
|
||||
public:
|
||||
Service() { }
|
||||
|
||||
QList<QSharedPointer<T> > all() {
|
||||
odb::database *db = Context::instance().db();
|
||||
odb::transaction tx(db->begin());
|
||||
odb::result<T> res = db->template query<T>();
|
||||
|
||||
QList<QSharedPointer<T> > ret;
|
||||
for (typename odb::result<T>::iterator it = res.begin(); it != res.end(); it++) {
|
||||
QSharedPointer<T> entity = db->template load<T>(((T*)*it)->id());
|
||||
ret.append(entity);
|
||||
}
|
||||
|
||||
tx.commit();
|
||||
return ret;
|
||||
}
|
||||
|
||||
void save(QSharedPointer<T> entity) {
|
||||
odb::database *db = Context::instance().db();
|
||||
odb::transaction tx(db->begin());
|
||||
db->persist(entity);
|
||||
tx.commit();
|
||||
}
|
||||
|
||||
QSharedPointer<T> loadById(int id) {
|
||||
odb::database *db = Context::instance().db();
|
||||
odb::transaction tx(db->begin());
|
||||
db->template load<T>(id);
|
||||
tx.commit();
|
||||
}
|
||||
};
|
||||
|
||||
#endif // SERVICE_H
|
||||
Reference in New Issue
Block a user