Improved transaction handling.

print
Josef Rokos 9 years ago
parent b1e1388135
commit 71325fa717

@ -19,7 +19,7 @@ QList<IPlugin *> Context::plugins()
void Context::loadPlugins() void Context::loadPlugins()
{ {
QDir pluginsDir(qApp->applicationDirPath() + "/../plugins"); QDir pluginsDir(qApp->applicationDirPath() + "/../../plugins");
foreach (QString fileName, pluginsDir.entryList(QStringList() << "*.so" << "*.dll")) { foreach (QString fileName, pluginsDir.entryList(QStringList() << "*.so" << "*.dll")) {
QPluginLoader pluginLoader(pluginsDir.absoluteFilePath(fileName)); QPluginLoader pluginLoader(pluginsDir.absoluteFilePath(fileName));
@ -47,4 +47,5 @@ void Context::openDb(const QString &path)
Context::Context() Context::Context()
{ {
m_db = NULL; m_db = NULL;
m_inTransaction = false;
} }

@ -3,6 +3,7 @@
#include <QList> #include <QList>
#include "core_global.h" #include "core_global.h"
#include "transaction.h"
#include <odb/database.hxx> #include <odb/database.hxx>
@ -18,9 +19,12 @@ public:
odb::database *db() { return m_db; } odb::database *db() { return m_db; }
private: private:
friend class Transaction;
Context(); Context();
QList<IPlugin*> m_plugins; QList<IPlugin*> m_plugins;
odb::database *m_db; odb::database *m_db;
bool m_inTransaction;
}; };

@ -4,5 +4,6 @@
#include "context.h" #include "context.h"
#include "iplugin.h" #include "iplugin.h"
#include "imetadataplugin.h" #include "imetadataplugin.h"
#include "transaction.h"
#endif // CORE_H #endif // CORE_H

@ -14,7 +14,8 @@ DEFINES += CORE_LIBRARY
SOURCES += \ SOURCES += \
data/user.cpp \ data/user.cpp \
context.cpp \ context.cpp \
imetadataplugin.cpp imetadataplugin.cpp \
transaction.cpp
HEADERS += core.h\ HEADERS += core.h\
core_global.h \ core_global.h \
@ -23,7 +24,9 @@ HEADERS += core.h\
data/user.h \ data/user.h \
context.h \ context.h \
imetadataplugin.h \ imetadataplugin.h \
autotablemodel.h autotablemodel.h \
autoform.h \
transaction.h
unix { unix {
target.path = /usr/lib target.path = /usr/lib

@ -12,6 +12,8 @@
#include "core_global.h" #include "core_global.h"
#include "context.h" #include "context.h"
#include "transaction.h"
template<class T> template<class T>
class Service class Service
{ {
@ -23,7 +25,7 @@ public:
Q_ASSERT(db); Q_ASSERT(db);
odb::transaction tx(db->begin()); Transaction tx;
odb::result<T> res = db->template query<T>(); odb::result<T> res = db->template query<T>();
QList<QSharedPointer<T> > ret; QList<QSharedPointer<T> > ret;
@ -37,14 +39,14 @@ public:
void save(QSharedPointer<T> entity) { void save(QSharedPointer<T> entity) {
odb::database *db = Context::instance().db(); odb::database *db = Context::instance().db();
odb::transaction tx(db->begin()); Transaction tx;
db->persist(entity); db->persist(entity);
tx.commit(); tx.commit();
} }
QSharedPointer<T> loadById(int id) { QSharedPointer<T> loadById(int id) {
odb::database *db = Context::instance().db(); odb::database *db = Context::instance().db();
odb::transaction tx(db->begin()); Transaction tx;
QSharedPointer<T> entity = db->template load<T>(id); QSharedPointer<T> entity = db->template load<T>(id);
tx.commit(); tx.commit();
return entity; return entity;

@ -0,0 +1,12 @@
#include "transaction.h"
Transaction::Transaction()
{
}
Transaction::~Transaction()
{
}

@ -0,0 +1,12 @@
#ifndef TRANSACTION_H
#define TRANSACTION_H
class Transaction
{
public:
Transaction();
~Transaction();
};
#endif // TRANSACTION_H
Loading…
Cancel
Save