Build system changed to Cmake, ORM changed to QxORM, Qt6 compatibility.
This commit is contained in:
+86
-125
@@ -7,11 +7,6 @@
|
||||
#include <QEventLoop>
|
||||
#include <QApplication>
|
||||
|
||||
#include <odb/core.hxx>
|
||||
#include <odb/transaction.hxx>
|
||||
#include <odb/database.hxx>
|
||||
#include <odb/result.hxx>
|
||||
|
||||
#include "core_global.h"
|
||||
#include "context.h"
|
||||
#include "iservice.h"
|
||||
@@ -19,195 +14,158 @@
|
||||
#include "iimporter.h"
|
||||
#include "iimportprogress.h"
|
||||
|
||||
#include "transaction.h"
|
||||
|
||||
template<class T>
|
||||
class Service : public IService
|
||||
{
|
||||
public:
|
||||
explicit Service(QObject *parent = NULL) :IService(parent) { }
|
||||
explicit Service(QObject *parent = nullptr) :IService(parent) { }
|
||||
|
||||
explicit Service(const QString &pluginId) {
|
||||
m_pluginId = pluginId;
|
||||
}
|
||||
|
||||
QList<QSharedPointer<T> > all(const QString &where = "", const QString &order = "") {
|
||||
QList<QSharedPointer<T> > all(const QString &where = "") {
|
||||
QList<QSharedPointer<T> > ret;
|
||||
|
||||
if (!checkPermission(PERM_READ)) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
odb::database *db = Context::instance().db();
|
||||
QScopedPointer<T> entity(new T());
|
||||
auto qEntity = dynamic_cast<QObject*>(entity.data());
|
||||
|
||||
Q_ASSERT(db);
|
||||
QStringList relations;
|
||||
QString sWhere = where.isEmpty() ? "" : "WHERE " + where;
|
||||
QSqlError err;
|
||||
|
||||
Transaction tx;
|
||||
|
||||
try
|
||||
if (qEntity
|
||||
&& QMetaObject::invokeMethod(qEntity, "eagerLoad", Qt::DirectConnection, Q_RETURN_ARG(QStringList, relations))
|
||||
&& !relations.isEmpty())
|
||||
{
|
||||
odb::result<T> res;
|
||||
QString ord = defaultSort();
|
||||
|
||||
if (!order.isEmpty())
|
||||
{
|
||||
ord = order;
|
||||
}
|
||||
|
||||
if (where.isEmpty() && ord.isEmpty())
|
||||
{
|
||||
res = db->template query<T>();
|
||||
}
|
||||
else
|
||||
{
|
||||
QString cond;
|
||||
|
||||
if (where.isEmpty())
|
||||
{
|
||||
cond = "1 ORDER BY " + ord;
|
||||
}
|
||||
else
|
||||
{
|
||||
cond = where;
|
||||
if (!ord.isEmpty())
|
||||
{
|
||||
cond += "ORDER BY " + ord;
|
||||
}
|
||||
}
|
||||
|
||||
res = db->template query<T>(cond.toStdString());
|
||||
}
|
||||
|
||||
for (typename odb::result<T>::iterator it = res.begin(); it != res.end(); it++) {
|
||||
ret.append(it.load());
|
||||
}
|
||||
|
||||
tx.commit();
|
||||
err = qx::dao::fetch_by_query_with_relation(relations, sWhere, ret);
|
||||
}
|
||||
catch (const odb::exception &ex)
|
||||
{
|
||||
emit dbError(ex.what());
|
||||
else {
|
||||
err = qx::dao::fetch_by_query(sWhere, ret);
|
||||
}
|
||||
|
||||
if (!err.isValid()) {
|
||||
return ret;
|
||||
} else {
|
||||
qDebug() << err.text();
|
||||
emit dbError(err.text());
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
void save(QSharedPointer<T> entity) {
|
||||
void save(QSharedPointer<T> entity, qx::QxSession* pSession = nullptr) {
|
||||
if (!checkPermission(PERM_ADD)) {
|
||||
return;
|
||||
}
|
||||
|
||||
odb::database *db = Context::instance().db();
|
||||
QScopedPointer<qx::QxSession> ptrSession;
|
||||
|
||||
Q_ASSERT(db);
|
||||
|
||||
Transaction tx;
|
||||
if (pSession == nullptr) {
|
||||
ptrSession.reset(new qx::QxSession());
|
||||
pSession = ptrSession.data();
|
||||
}
|
||||
|
||||
addDateAndUser(entity, true);
|
||||
*pSession += qx::dao::insert_with_all_relation(entity, pSession->database());
|
||||
|
||||
try
|
||||
{
|
||||
db->persist(entity);
|
||||
tx.commit();
|
||||
}
|
||||
catch (const odb::exception &ex)
|
||||
{
|
||||
emit dbError(ex.what());
|
||||
emit dbErrorUpdate(ex.what());
|
||||
if (!pSession->isValid()) {
|
||||
qDebug() << pSession->firstError().text();
|
||||
emit dbError(pSession->firstError().text());
|
||||
emit dbErrorInsert(pSession->firstError().text());
|
||||
return;
|
||||
}
|
||||
|
||||
emit dataChanged();
|
||||
}
|
||||
|
||||
void update(QSharedPointer<T> entity) {
|
||||
void update(QSharedPointer<T> entity, qx::QxSession* pSession = nullptr) {
|
||||
if (!checkPermission(PERM_EDIT)) {
|
||||
return;
|
||||
}
|
||||
|
||||
odb::database *db = Context::instance().db();
|
||||
QScopedPointer<qx::QxSession> ptrSession;
|
||||
|
||||
Q_ASSERT(db);
|
||||
|
||||
Transaction tx;
|
||||
if (pSession == nullptr) {
|
||||
ptrSession.reset(new qx::QxSession());
|
||||
pSession = ptrSession.data();
|
||||
}
|
||||
|
||||
addDateAndUser(entity, false);
|
||||
|
||||
try
|
||||
{
|
||||
db->update(entity);
|
||||
tx.commit();
|
||||
}
|
||||
catch (const odb::exception &ex)
|
||||
{
|
||||
emit dbError(ex.what());
|
||||
emit dbErrorInsert(ex.what());
|
||||
*pSession += qx::dao::update_with_all_relation(entity, pSession->database());
|
||||
|
||||
if (!pSession->isValid()) {
|
||||
emit dbError(pSession->firstError().text());
|
||||
emit dbErrorUpdate(pSession->firstError().text());
|
||||
return;
|
||||
}
|
||||
|
||||
emit dataChanged();
|
||||
}
|
||||
|
||||
QSharedPointer<T> loadById(int id) {
|
||||
QSharedPointer<T> loadById(long id) {
|
||||
QSharedPointer<T> entity;
|
||||
|
||||
/*if (!checkPermission(PERM_READ)) {
|
||||
if (!checkPermission(PERM_READ)) {
|
||||
return entity;
|
||||
}*/
|
||||
|
||||
odb::database *db = Context::instance().db();
|
||||
|
||||
Q_ASSERT(db);
|
||||
|
||||
Transaction tx;
|
||||
|
||||
try
|
||||
{
|
||||
entity = db->template load<T>(id);
|
||||
tx.commit();
|
||||
}
|
||||
catch (const odb::exception &ex)
|
||||
{
|
||||
emit dbError(ex.what());
|
||||
emit dbErrorRead(ex.what());
|
||||
|
||||
entity = QSharedPointer<T>(new T);
|
||||
entity->setId(id);
|
||||
auto err = qx::dao::fetch_by_id_with_all_relation(entity);
|
||||
|
||||
if (err.isValid()) {
|
||||
emit dbError(err.text());
|
||||
emit dbErrorRead(err.text());
|
||||
}
|
||||
|
||||
return entity;
|
||||
}
|
||||
|
||||
void load(QSharedPointer<T>& entity) {
|
||||
if (!checkPermission(PERM_READ)) {
|
||||
return;
|
||||
}
|
||||
|
||||
auto err = qx::dao::fetch_by_id_with_all_relation(entity);
|
||||
|
||||
if (err.isValid()) {
|
||||
emit dbError(err.text());
|
||||
emit dbErrorRead(err.text());
|
||||
}
|
||||
}
|
||||
|
||||
QSharedPointer<T> reload(int id) {
|
||||
odb::database *db = Context::instance().db();
|
||||
|
||||
Q_ASSERT(db);
|
||||
|
||||
Context::instance().session().cache_erase<T>(*db, id);
|
||||
return loadById(id);
|
||||
}
|
||||
|
||||
void erase(QSharedPointer<T> entity) {
|
||||
void erase(QSharedPointer<T> entity, qx::QxSession* pSession = nullptr) {
|
||||
if (!checkPermission(PERM_DELETE)) {
|
||||
return;
|
||||
}
|
||||
|
||||
odb::database *db = Context::instance().db();
|
||||
QScopedPointer<qx::QxSession> ptrSession;
|
||||
|
||||
Q_ASSERT(db);
|
||||
|
||||
Transaction tx;
|
||||
|
||||
try
|
||||
{
|
||||
db->erase(entity);
|
||||
tx.commit();
|
||||
if (pSession == nullptr) {
|
||||
ptrSession.reset(new qx::QxSession());
|
||||
pSession = ptrSession.data();
|
||||
}
|
||||
catch (const odb::exception &ex)
|
||||
{
|
||||
emit dbError(ex.what());
|
||||
emit dbErrorDelete(ex.what());
|
||||
|
||||
*pSession += qx::dao::delete_by_id(entity);
|
||||
|
||||
if (!pSession->isValid()) {
|
||||
qDebug() << pSession->firstError().text();
|
||||
emit dbError(pSession->firstError().text());
|
||||
emit dbErrorDelete(pSession->firstError().text());
|
||||
}
|
||||
}
|
||||
|
||||
bool importData(IImporter *importer, IImportProgress *progress = NULL) {
|
||||
bool importData(IImporter *importer, IImportProgress *progress = nullptr) {
|
||||
int count = importer->recordCount();
|
||||
|
||||
if (importer->isError()) {
|
||||
@@ -232,12 +190,12 @@ public:
|
||||
|
||||
qApp->processEvents();
|
||||
|
||||
if (progress != NULL && progress->terminate())
|
||||
if (progress != nullptr && progress->terminate())
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
if (progress != NULL)
|
||||
if (progress != nullptr)
|
||||
{
|
||||
progress->updateProgress(i * 100 / count);
|
||||
}
|
||||
@@ -262,16 +220,19 @@ protected:
|
||||
void addDateAndUser(QSharedPointer<T> entity, bool creating) {
|
||||
|
||||
T *inner = entity.data();
|
||||
QObject *obj = dynamic_cast<QObject*>(inner);
|
||||
auto *obj = dynamic_cast<QObject*>(inner);
|
||||
|
||||
if (obj == NULL)
|
||||
if (obj == nullptr)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (creating)
|
||||
{
|
||||
obj->setProperty("createdBy", Context::instance().currentUser()->login());
|
||||
if (!Context::instance().currentUser().isNull()) {
|
||||
obj->setProperty("createdBy", Context::instance().currentUser()->login());
|
||||
}
|
||||
|
||||
obj->setProperty("created", QDateTime::currentDateTime());
|
||||
}
|
||||
else
|
||||
|
||||
Reference in New Issue
Block a user