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.

71 lines
1.3 KiB
C

#ifndef SERVICE_H
#define SERVICE_H
#include <QList>
#include <QSharedPointer>
#include <QString>
#include <odb/core.hxx>
#include <odb/transaction.hxx>
#include <odb/database.hxx>
#include <odb/result.hxx>
#include "core_global.h"
#include "context.h"
#include "transaction.h"
template<class T>
class Service
{
public:
Service() { }
QList<QSharedPointer<T> > all() {
odb::database *db = Context::instance().db();
Q_ASSERT(db);
Transaction tx;
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++) {
ret.append(it.load());
}
tx.commit();
return ret;
}
void save(QSharedPointer<T> entity) {
odb::database *db = Context::instance().db();
Q_ASSERT(db);
Transaction tx;
db->persist(entity);
tx.commit();
}
QSharedPointer<T> loadById(int id) {
odb::database *db = Context::instance().db();
Q_ASSERT(db);
Transaction tx;
QSharedPointer<T> entity = db->template load<T>(id);
tx.commit();
return entity;
}
void setPluginId(const QString &pluginId) {
m_pluginId = pluginId;
}
private:
QString m_pluginId;
};
#endif // SERVICE_H