You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
49 lines
1.1 KiB
C
49 lines
1.1 KiB
C
9 years ago
|
#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
|