From 5251bf3a4b053b7e27754dce13ada5611ba6582b Mon Sep 17 00:00:00 2001 From: Steven de Ridder Date: Tue, 25 Jan 2022 09:20:16 +0100 Subject: [PATCH] Initial commit. dependencies not resolved yet. --- .gitignore | 2 ++ CMakeLists.txt | 25 +++++++++++++++++++++++++ README.md | 0 src/CMakeLists.txt | 69 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/multisortfilterproxymodel.cpp | 71 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/multisortfilterproxymodel.h | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/ormbatchchange.cpp | 114 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/ormbatchchange.h | 123 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/ormhandler.cpp | 183 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/ormhandler.h | 121 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/ormtable.cpp | 792 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/ormtable.h | 337 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/ormtablerow.cpp | 45 +++++++++++++++++++++++++++++++++++++++++++++ src/ormtablerow.h | 33 +++++++++++++++++++++++++++++++++ src/ormthread.cpp | 38 ++++++++++++++++++++++++++++++++++++++ src/ormthread.h | 86 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/ormtransqueue.cpp | 110 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/ormtransqueue.h | 79 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/timeline.cpp | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ src/timeline.h | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/timestamp.cpp | 83 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/timestamp.h | 82 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ tests/CMakeLists.txt | 30 ++++++++++++++++++++++++++++++ 23 files changed, 2591 insertions(+), 0 deletions(-) create mode 100644 .gitignore create mode 100644 CMakeLists.txt create mode 100644 README.md create mode 100644 src/CMakeLists.txt create mode 100644 src/multisortfilterproxymodel.cpp create mode 100644 src/multisortfilterproxymodel.h create mode 100644 src/ormbatchchange.cpp create mode 100644 src/ormbatchchange.h create mode 100644 src/ormhandler.cpp create mode 100644 src/ormhandler.h create mode 100644 src/ormtable.cpp create mode 100644 src/ormtable.h create mode 100644 src/ormtablerow.cpp create mode 100644 src/ormtablerow.h create mode 100644 src/ormthread.cpp create mode 100644 src/ormthread.h create mode 100644 src/ormtransqueue.cpp create mode 100644 src/ormtransqueue.h create mode 100644 src/timeline.cpp create mode 100644 src/timeline.h create mode 100644 src/timestamp.cpp create mode 100644 src/timestamp.h create mode 100644 tests/CMakeLists.txt diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..0ff047c --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +build/ +CMakeLists.txt.user diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..a91ceaf --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,25 @@ +cmake_minimum_required(VERSION 3.0) + +# Check to see where cmake is located. +if( IS_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/cmake ) + LIST(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake) +elseif( IS_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/../cmake ) + LIST(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/../cmake) +else() + return() +endif() + +# Check to see if there is versioning information available +if(IS_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/osdev_versioning/cmake) + LIST(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/osdev_versioning/cmake) + include(osdevversion) +endif() + +include(projectheader) +project_header(osdev_orm) + +add_subdirectory(src) +add_subdirectory(tests) + +# include(packaging) +# package_component() diff --git a/README.md b/README.md new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/README.md diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt new file mode 100644 index 0000000..86363a2 --- /dev/null +++ b/src/CMakeLists.txt @@ -0,0 +1,69 @@ +cmake_minimum_required(VERSION 3.0) +include(projectheader) +project_header(orm) + +find_package( Qt5Core REQUIRED ) +find_package( Qt5Sql REQUIRED ) + +include_directories( SYSTEM + ${Qt5Core_INCLUDE_DIRS} + ${Qt5Sql_INCLUDE_DIRS} +) + +include(compiler) + +include_directories( + ${CMAKE_CURRENT_SOURCE_DIR}/../config + ${CMAKE_CURRENT_SOURCE_DIR}/../global + ${CMAKE_CURRENT_SOURCE_DIR}/../logutils + ${CMAKE_CURRENT_SOURCE_DIR}/../../interfaces + ${CMAKE_CURRENT_SOURCE_DIR}/../datatypes + ${CMAKE_CURRENT_SOURCE_DIR}/../dbconnector + ${CMAKE_CURRENT_SOURCE_DIR}/../transqueue + ${CMAKE_CURRENT_SOURCE_DIR}/../pugixml +) + +set(SRC_LIST + ${CMAKE_CURRENT_SOURCE_DIR}/multisortfilterproxymodel.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/ormhandler.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/ormthread.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/ormtable.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/ormtablerow.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/ormbatchchange.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/ormbatchchange.h + ${CMAKE_CURRENT_SOURCE_DIR}/multisortfilterproxymodel.h + ${CMAKE_CURRENT_SOURCE_DIR}/ormhandler.h + ${CMAKE_CURRENT_SOURCE_DIR}/ormthread.h + ${CMAKE_CURRENT_SOURCE_DIR}/ormtable.h + ${CMAKE_CURRENT_SOURCE_DIR}/ormtablerow.h + ${CMAKE_CURRENT_SOURCE_DIR}/timeline.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/timeline.h + ${CMAKE_CURRENT_SOURCE_DIR}/timestamp.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/timestamp.h +) + +include(qtmoc) +create_mocs( SRC_LIST MOC_LIST + ${CMAKE_CURRENT_SOURCE_DIR}/multisortfilterproxymodel.h + ${CMAKE_CURRENT_SOURCE_DIR}/ormhandler.h + ${CMAKE_CURRENT_SOURCE_DIR}/ormthread.h + ${CMAKE_CURRENT_SOURCE_DIR}/ormtable.h +) + +link_directories( + ${CMAKE_BINARY_DIR}/lib +) + +include(library) +add_libraries( + ${Qt5Core_LIBRARIES} + ${Qt5Sql_LIBRARIES} + global + logutils + datatypes + dbconnector + transqueue +) + +include(installation) +install_component() diff --git a/src/multisortfilterproxymodel.cpp b/src/multisortfilterproxymodel.cpp new file mode 100644 index 0000000..8a09cce --- /dev/null +++ b/src/multisortfilterproxymodel.cpp @@ -0,0 +1,71 @@ +/* **************************************************************************** + * Copyright 2019 Open Systems Development BV * + * * + * Permission is hereby granted, free of charge, to any person obtaining a * + * copy of this software and associated documentation files (the "Software"), * + * to deal in the Software without restriction, including without limitation * + * the rights to use, copy, modify, merge, publish, distribute, sublicense, * + * and/or sell copies of the Software, and to permit persons to whom the * + * Software is furnished to do so, subject to the following conditions: * + * * + * The above copyright notice and this permission notice shall be included in * + * all copies or substantial portions of the Software. * + * * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL * + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING * + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER * + * DEALINGS IN THE SOFTWARE. * + * ***************************************************************************/ +#include "multisortfilterproxymodel.h" + +#include "log.h" + +#include + +using namespace osdev::components; + +MultiSortFilterProxyModel::MultiSortFilterProxyModel(QObject* _parent) + : QSortFilterProxyModel(_parent) + , m_columnPatterns() +{ +} + +void MultiSortFilterProxyModel::setFilterKeyColumns(const QList &filterColumns) +{ + m_columnPatterns.clear(); + foreach(qint32 column, filterColumns) + { + m_columnPatterns.insert(column, QString()); // Add columns with empty patterns + } +} + +void MultiSortFilterProxyModel::setFilterFixedString(qint32 column, const QString &pattern) +{ + if(!m_columnPatterns.contains(column)) + { + return; + } + + m_columnPatterns[column] = pattern; +} + +bool MultiSortFilterProxyModel::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const +{ + if(m_columnPatterns.isEmpty()) + { + return true; // No pattern -> accept all + } + + for(QMap::const_iterator iter = m_columnPatterns.constBegin(); iter != m_columnPatterns.constEnd(); ++iter) + { + QModelIndex idx = sourceModel()->index(sourceRow, iter.key(), sourceParent); + if (idx.data().toString() != iter.value()) + { + return false; + } + } + return true; +} diff --git a/src/multisortfilterproxymodel.h b/src/multisortfilterproxymodel.h new file mode 100644 index 0000000..7bc106d --- /dev/null +++ b/src/multisortfilterproxymodel.h @@ -0,0 +1,62 @@ +/* **************************************************************************** + * Copyright 2019 Open Systems Development BV * + * * + * Permission is hereby granted, free of charge, to any person obtaining a * + * copy of this software and associated documentation files (the "Software"), * + * to deal in the Software without restriction, including without limitation * + * the rights to use, copy, modify, merge, publish, distribute, sublicense, * + * and/or sell copies of the Software, and to permit persons to whom the * + * Software is furnished to do so, subject to the following conditions: * + * * + * The above copyright notice and this permission notice shall be included in * + * all copies or substantial portions of the Software. * + * * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL * + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING * + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER * + * DEALINGS IN THE SOFTWARE. * + * ***************************************************************************/ + +#ifndef OSDEV_COMPONENTS_MULTISORTFILTERPROXYMODEL_H +#define OSDEV_COMPONENTS_MULTISORTFILTERPROXYMODEL_H + +#include +#include +#include +#include + +namespace osdev { +namespace components { + +//! @brief Filter that can filter a model on multiple columns. +//! @note Do not mix uasge of setFilterKeyColumns and setFilterKeyColumn. +class MultiSortFilterProxyModel : public QSortFilterProxyModel +{ + Q_OBJECT + +public: + explicit MultiSortFilterProxyModel(QObject* parent = 0); + + //! @brief Set the columns to filter on. + //! This will overwrite the previous filter settings (including the patterns). + void setFilterKeyColumns(const QList& filterColumns); + + //! @brief Add a pattern on a specific column index. + //! If the index is not known the pattern is not applied. + void setFilterFixedString(qint32 column, const QString& pattern); + +protected: + //! @brief Called from the base class to do the actual filtering. + virtual bool filterAcceptsRow(int sourceRow, const QModelIndex& sourceParent) const; + +private: + QMap m_columnPatterns; ///< holds the filter pattern for column indices. +}; + +} // End namespace components +} // End namespace osdev + +#endif // OSDEV_COMPONENTS_MULTISORTFILTERPROXYMODEL_H diff --git a/src/ormbatchchange.cpp b/src/ormbatchchange.cpp new file mode 100644 index 0000000..29ba1ea --- /dev/null +++ b/src/ormbatchchange.cpp @@ -0,0 +1,114 @@ +/* **************************************************************************** + * Copyright 2019 Open Systems Development BV * + * * + * Permission is hereby granted, free of charge, to any person obtaining a * + * copy of this software and associated documentation files (the "Software"), * + * to deal in the Software without restriction, including without limitation * + * the rights to use, copy, modify, merge, publish, distribute, sublicense, * + * and/or sell copies of the Software, and to permit persons to whom the * + * Software is furnished to do so, subject to the following conditions: * + * * + * The above copyright notice and this permission notice shall be included in * + * all copies or substantial portions of the Software. * + * * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL * + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING * + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER * + * DEALINGS IN THE SOFTWARE. * + * ***************************************************************************/ +#include "ormbatchchange.h" +#include "log.h" +#include + +using namespace osdev::components; + +OrmBatchChange::OrmBatchChange() + : m_changeTimestamp() + , m_foreignId() + , m_changeSet() + , m_valid(false) +{ +} + +OrmBatchChange::OrmBatchChange(const Timestamp& ts, const QVariant& _foreignId, const std::set& _changeSet) + : m_changeTimestamp(ts) + , m_foreignId(_foreignId) + , m_changeSet(_changeSet) + , m_valid(!_foreignId.isNull() && ts.valid()) +{ + if (!m_valid) + { + LogInfo("[OrmBatchChange ctor]", QString("Change is invalid : Timestamp %1, foreignId::isNull %2").arg(m_changeTimestamp.toString()).arg(m_foreignId.isNull())); + } +} + +bool OrmBatchChange::processChange(const OrmBatchChange& change, bool exclusiveUpdate) +{ + LogDebug("[OrmBatchChange::processChange]", QString("Check %1 against change with timestamp %2, exlusiveUpdate is %3") + .arg(m_changeTimestamp.toString()) + .arg(change.timestamp().toString()) + .arg(exclusiveUpdate)); + if (!m_valid) + { + LogInfo("[OrmBatchChange::processChange]", "Incoming change is invalid"); + return false; // This is an invalid change and can never become valid so signal to stop processing. + } + + if (*this < change) + { + if (exclusiveUpdate && (change.m_foreignId == m_foreignId)) + { + // this change is not necessary because it is already superseded. Invalidate it and signal to stop processing. + LogInfo("[OrmBatchChange::processChange]", "Change is superseded by newer change"); + m_valid = false; + return false; + } + + // Find the items that are in the incoming change but not in the already applied change that we are checking against. + // These items still need to change w.r.t to this change. + if (!m_changeSet.empty()) + { + std::set result; + std::set_difference(m_changeSet.begin(), m_changeSet.end(), change.m_changeSet.begin(), change.m_changeSet.end(), std::inserter(result, result.begin())); + m_changeSet.swap(result); + } + + // If the changeset is empty and the update is not exclusive then nothing has to be done. + // In case of an exclusive update an empty set means that none of the items can point to the given foreignId + // and processing needs to be continued. + if (!exclusiveUpdate && m_changeSet.empty()) + { + // Invalidate the incoming change and signal to stop processing. + LogInfo("[OrmBatchChange::processChange]", "Nothing to do for non exclusive update"); + m_valid = false; + return false; + } + } + return true; +} + +// static +std::set OrmBatchChange::toSet(const QList& lst) +{ + std::set result; + for (const auto& item : lst) + { + result.insert(item); + } + return result; +} + +// static +QList OrmBatchChange::toList(const std::set& s) +{ + QList result; + for (const auto& item : s) + { + result.push_back(item); + } + return result; +} + diff --git a/src/ormbatchchange.h b/src/ormbatchchange.h new file mode 100644 index 0000000..0dffdf0 --- /dev/null +++ b/src/ormbatchchange.h @@ -0,0 +1,123 @@ +#ifndef OSDEV_COMPONENTS_ORMBATCHCHANGE_H +#define OSDEV_COMPONENTS_ORMBATCHCHANGE_H + +#include + +#include + +#include "timestamp.h" + +namespace osdev { +namespace components { + +/** + * @brief Describes a change that should happen or has happened on a BatchUpdate or MergeUpdate table. + */ +class OrmBatchChange +{ +public: + /** + * @brief Creates an invalid OrmBatchChange. + * @note The only way to make an OrmBatchChange valid is by assigning a valid OrmBatchChange to it. + */ + OrmBatchChange(); + + /** + * @brief Create an OrmBatchChange with a change description. + * @param ts A valid timestamp (not checked). + * @param foreignId The value that should be pointed to. + * @param changeSet The set of record identifiers of records that should point to the given foreignId. + */ + OrmBatchChange(const Timestamp& ts, const QVariant& foreignId, const std::set& changeSet); + + // Default copyable and movable. + OrmBatchChange(const OrmBatchChange&) = default; + OrmBatchChange& operator=(const OrmBatchChange&) = default; + OrmBatchChange(OrmBatchChange&&) = default; + OrmBatchChange& operator=(OrmBatchChange&&) = default; + + /** + * @brief Process an already executed change on this change. + * The changeset of this object can be reshaped as a result. + * @param change A known change. + * @param exclusiveUpdate Flag that indicates if the change should be processed in an exclusive way. + * @return true to indicate this change is still valid and false otherwise. + * @note An exclusiveUpdate means that all records that are not mentioned in the change set will not point to the given foreignId. + */ + bool processChange(const OrmBatchChange& change, bool exclusiveUpdate); + + /** + * @brief Changes are ordered w.r.t. each other by their timestamp. + */ + bool operator<(const OrmBatchChange& rhs) const + { + return m_changeTimestamp < rhs.m_changeTimestamp; + } + + /** + * @brief Change the timestamp of this change. + * This is necessary when the change is recorded to be permanent. It then becomes the latest change. + * @param ts The new timestamp (no checks). + */ + void setTimestamp(const Timestamp& ts) + { + m_changeTimestamp = ts; + } + + /** + * @return The timestamp of this change. + */ + const Timestamp& timestamp() const + { + return m_changeTimestamp; + } + + /** + * @return The foreinId of this change. + */ + QVariant foreignId() const + { + return m_foreignId; + } + + /** + * @return The change set of this change. + */ + std::set changeSet() const + { + return m_changeSet; + } + + /** + * @return true if this change is valid, false otherwise. + */ + bool valid() const + { + return m_valid; + } + + /** + * @brief Static method to convert a QList to an std::set. + * @param lst The QList to convert. + * @return the converted set. + */ + static std::set toSet(const QList& lst); + + /** + * @brief Static method to convert an std::set to a QList. + * @param variantSet The set of variants to convert. + * @return the converted QList. + */ + static QList toList(const std::set& variantSet); + +private: + Timestamp m_changeTimestamp; ///< The timestamp of this change. + QVariant m_foreignId; ///< The foreignId that should be pointed to. + std::set m_changeSet; ///< The set of record identifiers that describe the change. + bool m_valid; ///< Flag that indicates if this change is valid. +}; + +} /* End namespace components */ +} /* End namespace osdev */ + +#endif /* OSDEV_COMPONENTS_ORMBATCHCHANGE_H */ diff --git a/src/ormhandler.cpp b/src/ormhandler.cpp new file mode 100644 index 0000000..5ceda61 --- /dev/null +++ b/src/ormhandler.cpp @@ -0,0 +1,183 @@ +#include "ormhandler.h" +#include "dcxmlconfig.h" +#include "log.h" +#include "dbrelation.h" +#include "threadcontext.h" + +#include +#include +#include +#include + +#include + +using namespace osdev::components; + +typedef QList Relations; + +OrmHandler::OrmHandler(QObject *_parent) + : QObject( _parent ) + , m_qhOrmTables() + , m_dbConnection() + , m_className( "OrmHandler" ) +{ + m_dbConnection.setCredentials( DCXmlConfig::Instance().getDbCredentials() ); + + // Check if a watchdog is required and connect those. If a database goes offline, all data should be redirected to the TransQueue. + if( DCXmlConfig::Instance().getEnableDbWatchDog() ) + { + // Start the watchdog and set the interval. + m_dbConnection.startDbWatchDog( DCXmlConfig::Instance().getDbCheckInterval() ); + } +} + +OrmHandler::~OrmHandler() +{ +} + +void OrmHandler::start() +{ + // connect the database. + LogInfo( m_className, "Connecting to database.." ); + if( m_dbConnection.connectDatabase() ) + { + // Read configuration + QStringList tableList = DCXmlConfig::Instance().getOrmTables(); + + if( 0 == tableList.count() ) + { + tableList = m_dbConnection.getTableNames(); + } + + LogInfo( m_className, + QString("Connected to database %1") .arg( m_dbConnection.getDatabaseName() ) ); + + // First make sure we have all tables created, before we create all relations... + for( const QString& tableName : tableList ) + { + createOrmObject( tableName ); + } + // ------------------------------------------------- + // Create all relations on the same list of tables. + for( const QString& tableName : tableList ) + { + QString status; + // If creation of the object succeeded it is safe to add the relations. + switch( createOrmRelation( tableName ) ) + { + case OrmStatus::OrmStatusFailed: + status = "failed."; + break; + case OrmStatus::OrmStatusNoRelation: + status = "not created (As there are none.)"; + break; + case OrmStatus::OrmStatusSuccess: + status = "succeeded."; + break; + } + LogInfo( "[OrmHandler::start]", QString( "Creation of ORM Relation for table : %1 %2" ).arg( tableName ).arg( status ) ); + } + } + else + { + LogInfo( m_className, + QString( "Connection to database %1 failed with error : %2" ) + .arg( m_dbConnection.getDatabaseName() ) + .arg( m_dbConnection.getLastError() ) ); + + //@todo : This will produce a SegmentationFault for now due to mutlihreaded pluginloading. + // This should be fixed in future releases by implementing a nice rolldown with locking + // mechanisms and shutdown events. + QMetaObject::invokeMethod( qApp, "quit", Qt::QueuedConnection ); + } +} + +bool OrmHandler::createOrmObject( const QString& _table ) +{ + bool l_result = false; + + OrmTable *pModel = new OrmTable( m_dbConnection, this ); + if ( pModel ) + { + pModel->setTrackField( DCXmlConfig::Instance().getRecordTrackFieldName() ); + pModel->setTable( m_dbConnection.quoteTableName( _table ) ); + m_qhOrmTables.insert( _table, pModel ); + + // cascade connect the rejectedsignal. + connect( pModel, &OrmTable::signalRejectedData, this, &OrmHandler::signalRejectedData ); + l_result = true; + } + + return l_result; +} + +OrmHandler::OrmStatus OrmHandler::createOrmRelation( const QString& _table ) +{ + OrmStatus ormResult = OrmStatus::OrmStatusFailed; + + Relations lstRelations = m_dbConnection.getRelationByTableName( _table ); + if( lstRelations.isEmpty() ) + { + return OrmStatus::OrmStatusNoRelation; + } + + // Each relation found should be added to the table. + for( auto& relation : lstRelations ) + { + // Save the relation to the table administration. + m_qhOrmTables.value( _table )->saveRelation( relation->constraintName(), relation ); + + // Create a QSortProxyFilterModel and add to the table. + QSortFilterProxyModel *pModel = new QSortFilterProxyModel(); + + pModel->setSourceModel( m_qhOrmTables.value( relation->foreignTable() ) ); + m_qhOrmTables.value( _table )->setRelatedTable( relation->foreignTable(), pModel ); + } + + // Check if the number of relations found is equal to the number of relations + // added to the ORM-table. + if( lstRelations.count() == m_qhOrmTables.value( _table )->numberOfRelations() ) + { + ormResult = OrmStatus::OrmStatusSuccess; + } + else + { + LogDebug( "[OrmHandler::createOrmRelation]", QString( "Number of relations : %1 => Number of relations registered : %2" ).arg( lstRelations.count() ).arg( m_qhOrmTables.value( _table )->numberOfRelations() ) ); + ormResult = OrmStatus::OrmStatusFailed; + } + + return ormResult; +} + +void OrmHandler::receiveData( const QSharedPointer& dataContainer ) +{ + ThreadContextScope tcs( dataContainer->traceId() ); + LogDebug( "[OrmHandler::receiveData]", dataContainer->asString() ); + // Check if we have an actual database running. *if* it died, redirect immediately + if( m_dbConnection.isOpen() ) + { + if( m_qhOrmTables.contains( dataContainer->getMainTableName() ) ) + { + LogDebug("[OrmHandler::receiveData]", QString("Table entry found for %1").arg( dataContainer->getMainTableName() ) ); + m_qhOrmTables.value( dataContainer->getMainTableName() )->writeData( dataContainer ); + } + else + { + LogWarning("[OrmHandler::receiveData]", QString("No table entry found for %1").arg( dataContainer->getMainTableName() ) ); + emit signalRejectedData( dataContainer ); + } + } + else + { + emit signalRejectedData( dataContainer ); + } +} + +QString OrmHandler::getPrimaryKey( const QString& tableName ) const +{ + if( m_qhOrmTables.contains( tableName ) ) + { + return m_qhOrmTables.value( tableName )->primaryKey().name(); + } + return QString(); +} diff --git a/src/ormhandler.h b/src/ormhandler.h new file mode 100644 index 0000000..26348e3 --- /dev/null +++ b/src/ormhandler.h @@ -0,0 +1,121 @@ +/* **************************************************************************** + * Copyright 2019 Open Systems Development BV * + * * + * Permission is hereby granted, free of charge, to any person obtaining a * + * copy of this software and associated documentation files (the "Software"), * + * to deal in the Software without restriction, including without limitation * + * the rights to use, copy, modify, merge, publish, distribute, sublicense, * + * and/or sell copies of the Software, and to permit persons to whom the * + * Software is furnished to do so, subject to the following conditions: * + * * + * The above copyright notice and this permission notice shall be included in * + * all copies or substantial portions of the Software. * + * * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL * + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING * + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER * + * DEALINGS IN THE SOFTWARE. * + * ***************************************************************************/ +#ifndef OSDEV_COMPONENTS_ORMHANDLER_H +#define OSDEV_COMPONENTS_ORMHANDLER_H + +#include + +#include +#include +#include +#include + +#include "dbconnector.h" +#include "ormtable.h" +#include "ormreldata.h" + +namespace osdev { +namespace components { + +/** + * @brief Handles the Object Relational Model + */ +class OrmHandler : public QObject +{ + Q_OBJECT + +public: + enum class OrmStatus + { + OrmStatusSuccess = 0, + OrmStatusNoRelation, + OrmStatusFailed + }; + + /** + * @brief Constructor + * @param parent Parent QObject + */ + explicit OrmHandler(QObject *parent = nullptr); + + /// Deleted copy-constructor + OrmHandler(const OrmHandler&) = delete; + /// Deleted assignment operator + OrmHandler& operator=(const OrmHandler&) = delete; + /// Deleted move-constructor + OrmHandler(const OrmHandler&&) = delete; + /// Deleted move operator + OrmHandler& operator=(const OrmHandler&&) = delete; + + virtual ~OrmHandler(); + + /// @brief Start the ORM handler + void start(); + +public slots: + /** + * @brief Receive data from an external source + * @param dataContainer - Data received Each layer has shared ownership of the + * pointer it receives. (See SSD @TODO Actualy SSD needs updating on this issue.) + */ + void receiveData( const QSharedPointer& dataContainer ); + + /*! + * \brief Gets the primary key from the given tableName + * \param tableName - The table we're interested in. + * \return The primary keyfield name. + */ + QString getPrimaryKey( const QString& tableName ) const; + +signals: + /*! + * \brief Signal for RejectedData. + * \param dataContainer The data container for this signal. + */ + void signalRejectedData( const QSharedPointer& dataContainer ); + +private: + /** + * @brief Create an Object Relational Model object for a specific table + * @param _table Table name + * @return True if successful, false otherwise + */ + bool createOrmObject( const QString& _table ); + + /*! + * \brief Create an ORM object for a specific relation or constraint. + * + * \param table The name of the table for which to create an ORM relation. + * \return true if the object was created. False if something went wrong. + */ + OrmStatus createOrmRelation( const QString& table ); + + // Internal structures and objects. + QHash m_qhOrmTables; ///< Maps table-name to TableModel* + DbConnector m_dbConnection; ///< Database connection + QString m_className; ///< Name of the class for logging purposes +}; + +} // End namespace components +} // End namespace osdev + +#endif // OSDEV_COMPONENTS_ORMHANDLER_H diff --git a/src/ormtable.cpp b/src/ormtable.cpp new file mode 100644 index 0000000..5651a63 --- /dev/null +++ b/src/ormtable.cpp @@ -0,0 +1,792 @@ +#include "ormdata.h" +#include "ormtable.h" +#include "ormhandler.h" +#include "log.h" +#include "multisortfilterproxymodel.h" + +#include +#include +#include +#include + +#include +#include + +using namespace osdev::components; + +OrmTable::OrmTable(const DbConnector &db, QObject *_parent) + : QSqlRelationalTableModel( _parent, db.getDatabase() ) + , m_className( "OrmTable" ) + , m_recTrackField() + , m_db( db ) + , m_qhRelTables() + , m_qhRelations() + , m_qhFieldTypes() + , m_qhTimelines() +{ + if ( !QSqlRelationalTableModel::database().isOpen() ) + { + LogWarning( m_className, "Database is closed."); + } +} + +OrmTable::~OrmTable() +{ +} + +void OrmTable::setTable( const QString& _table ) +{ + // Set tablename in the base class + QSqlRelationalTableModel::setTable( m_db.quoteTableName( _table ) ); + m_className += "::" + this->tableName(); + + // Make every change being written to the database. + QSqlRelationalTableModel::setEditStrategy( QSqlTableModel::OnRowChange ); + + // Now we know the tablename, we can retrieve the fieldtypes by their name. To prevent a functioncall + // For each and every fieldname we're looking for, we store this in an internal Hash. Must be quicker than + // then accessing the database layer. + m_qhFieldTypes = m_db.getFieldNamesAndTypes( _table ); + + QSqlRecord _headerData = QSqlRelationalTableModel::database().record( m_db.quoteTableName( _table ) ); + for( int nCount = 0; nCount < _headerData.count(); nCount++ ) + { + QSqlRelationalTableModel::setHeaderData( nCount, Qt::Horizontal, _headerData.fieldName(nCount) ); + } + + // Now the table is set, headerinfo loaded and commit behaviour set : + // Open the table and read from the database. + QSqlRelationalTableModel::select(); +} + +bool OrmTable::insertRecord( int _row, const QSqlRecord &_record ) +{ + if ( QSqlRelationalTableModel::database().isOpen() ) + { + LogDebug( m_className, "Database is open." ); + } + else + { + LogWarning( m_className, "Database is closed." ); + } + + bool b_result = QSqlRelationalTableModel::insertRecord( _row, _record ); + + if ( !b_result ) + { + LogError( QString( "[OrmTable - %1]" ).arg( QSqlRelationalTableModel::tableName() ), + QString( "Database gave error : %1") + .arg( this->lastError().text() ) ); + } + + return b_result; +} + +bool OrmTable::insertRecords(int row, const QList& _records ) +{ + bool l_bResult = true; + + QSqlRelationalTableModel::setEditStrategy( QSqlTableModel::OnRowChange ); + for( const QSqlRecord& _record : _records ) + { + l_bResult &= insertRecord( row, _record ); + } + QSqlRelationalTableModel::submitAll(); + + // Reset the transaction method. + QSqlRelationalTableModel::setEditStrategy( QSqlTableModel::OnRowChange ); + + return l_bResult; +} + +void OrmTable::writeData( const QSharedPointer& dataObject ) +{ + // Reset the filter for this table before doing anything else + this->resetFilter(); + + // Relations can be set on different fields. We have to take in account those + // fields could be given in a list. + ORMData* pMainTable = this->solveRelations( dataObject ); + if( nullptr == pMainTable || pMainTable->getContainerName().isEmpty() ) + { + LogDebug( "[OrmTable::writeData]", QString( "No maintable defined. Exiting..." ) ); + emit signalRejectedData( dataObject ); + return; + } + + bool updateSuccess = false; + /// Here we check for the variable list. See if one of the variables is of type : + /// QVariant( QListhasLists() ) + { + /// @todo: This is kind of a special case. For now we assume there is an inclusive + /// and exclusive way of updating these records. All these updates should be done + /// in a single transaction. The 'not-mentioned records' should be de-coupled. + /// If those fail, the entire package should be rejected, including the ones that were + /// mentioned. Default behaviour is "Each - change - a - transaction" so we need some + /// extra precautions here. + + // Check if all items in the list exist in the database. If not the package is rejected. + if (!m_db.recordsExist(pMainTable->getContainerName(), pMainTable->getKeyField(), pMainTable->getValue( pMainTable->getKeyField() ).toList())) + { + LogInfo( "[OrmTable::writeData]", "Not all records are present. Package is rejected." ); + emit signalRejectedData( dataObject ); + return; + } + + // Set the TransactionScopeGuard. If it goes out-of-scope, for whatever reason, + // it will do a Rollback if a transaction was started. + OrmTransGuard l_scopeGuard; + + // Check to see if the database supports transactions + if( m_db.supportTransactions() ) + { + if( !m_db.transactionBegin() ) + { + LogDebug( "[OrmTable::writeData]", "Transaction failed to start." ); + emit signalRejectedData( dataObject ); + return; + } + l_scopeGuard = OrmTransGuard( m_db ); + } + + std::unique_ptr mainTableCopy(pMainTable->clone()); + updateSuccess = batchUpdate( mainTableCopy.get(), dataObject->getSourceId() ); + if( !updateSuccess ) + { + emit signalRejectedData( dataObject ); + return; + } + + if( ( static_cast(TableActions::actionMergeUpdate) == mainTableCopy->getDefaultAction() ) + && ( !mainTableCopy->isMergeable() ) ) // If there is more than one key besides the keyfield, we cannot handle this (yet) + { + LogWarning( "[OrmTable::writeData]", QString( "Dataobject (ORMData) is not fit for mergeUpdate and is discarded : %1" ).arg( mainTableCopy->asString() ) ); + return; + } + + if( ( static_cast(TableActions::actionMergeUpdate) == mainTableCopy->getDefaultAction() ) + && ( mainTableCopy->isMergeable() ) ) + { + deCoupleRecords( mainTableCopy.get() ); + } + else if (static_cast(TableActions::actionUpdate) == mainTableCopy->getDefaultAction()) + { + LogDebug( "[OrmTable::writeData]", QString( "Action is a \"normal\" update : %1" ).arg( mainTableCopy->asString() ) ); + } + + if( !this->commit() ) + { + emit signalRejectedData( dataObject ); + } + auto timeline = m_qhTimelines[dataObject->getSourceId()]; + if (timeline) + { + timeline->commit(); + } + return; + } + else + { + if( static_cast(TableActions::actionUpdate) == pMainTable->getDefaultAction() ) + { + // =================================================================================== + // == S I N G L E R E C O R D U P D A T E == + // =================================================================================== + + // See if we can actually update this record by checking its timestamp validity. + // + if( m_recTrackField.isEmpty() || m_recTrackField.isNull() ) + { + updateSuccess = this->updateRecord( pMainTable ); + } + else + { + if( this->isPackageValid( pMainTable ) ) + { + pMainTable->setValue( m_recTrackField, QVariant( pMainTable->timeStamp() ) ); + updateSuccess = this->updateRecord( pMainTable ); + } + else + { + // Nope. Package is older. Discard. + LogInfo( "[OrmTable::writeData]", QString( "Discarding package because it is older than information in the database" ) ); + return; + } + } + } + + if( updateSuccess ) + { + return; + } + } + + // =================================================================================== + // == I N S E R T == + // =================================================================================== + // If a timestamp is used in the database, add it to the table definition. + if( !m_recTrackField.isEmpty() || !m_recTrackField.isNull() ) + { + pMainTable->setValue( m_recTrackField, pMainTable->timeStamp() ); + } + + + // If we get to here, we're ready to create the new SQLRecord and add it to the table. + QSqlRecord newRecord = this->buildSqlRecord( pMainTable ); + if( !newRecord.isEmpty() ) + { + if( !m_db.createRecord( newRecord, this->tableName() ) ) + { + // @todo : Change to a more database dependant, generic error checking based on keywords.... + if( this->lastError().text().contains( "violates unique constraint" ) || + this->lastError().text().contains( "violates not-null constraint" ) ) + { + // Just drop the package and be done with it. This is a unique constraint violation + LogDebug( "[OrmTable::writeData]", QString( "There was and error creating the record : %1" ).arg( this->lastError().text() ) ); + return; + } + else + { + LogDebug( "[OrmTable::writeData]", QString( "There was and error creating the record : %1" ).arg( this->lastError().text() ) ); + LogDebug( "[OrmTable::writeData]", "Deferring the datapackage to the TransQueue." ); + emit signalRejectedData( dataObject ); + } + + } + else + { + return; + } + } + else + { + LogDebug( "[OrmTable::writeData]", QString( "Creating the new record failed.! : %1" ).arg( this->lastError().text() ) ); + } +} + +bool OrmTable::updateRecord( ORMData* dataObject ) +{ + + auto lNumRecords = tableFilter( dataObject ); + if( lNumRecords > 1 ) + { + LogError( "[OrmTable::updateRecord]", QString( "Multiple records found..." ) ); + return false; + } + else if( lNumRecords == 0 ) + { + LogWarning( "[OrmTable::updateRecord]", QString( "No record found...Falling back to insert...." ) ); + return false; + } + else + { + // All criteria are met.. Time to update the record. + // Each and and every field will be written, with the exception of the keyfield.. + // First get a list of all field names. + bool updateSuccess = true; + QStringList fieldNames = dataObject->getFieldNames(); + for( const QString& fieldName : fieldNames ) + { + if( fieldName != dataObject->getKeyField() ) + { + int colIndex = this->fieldIndex( fieldName ); + if( -1 == colIndex ) + { + LogError( "[OrmTable::updateRecord]", QString( "No index found for : %1" ).arg( fieldName ) ); + } + else + { + LogDebug( "[OrmTable::updateRecord]", QString( "Updating field : %1 (Index : %2) with value : %3" ) + .arg( fieldName ) + .arg( colIndex ) + .arg( dataObject->getValue( fieldName ).toString() ) ); + + this->setData( this->index( 0, colIndex ), + ConversionUtils::convertToType(dataObject->getValue( fieldName ) ,m_qhFieldTypes.value( fieldName )), + Qt::EditRole ); + } + } + else + { + LogDebug( "[OrmTable::updateRecord]", QString( "Skipping keyfield %1 ..." ).arg( fieldName ) ); + } + } + + // Process all changes. + updateSuccess = this->submit(); + if( !updateSuccess ) + { + LogError( "[OrmTable::updateRecord]", + QString( "Record failed to update, Database gave error : %1").arg( this->lastError().text() ) ); + } + + this->setFilter( "" ); + this->select(); + return updateSuccess; + } +} + +bool OrmTable::batchUpdate( ORMData* dataObject, const QUuid& sourceId ) +{ + // If MergedUpdate = true, we have to do an ex- and inclusive update. + // (Inclusive meaning everyting that is mentioned, Exclusive : everything that is not in range..) + // Similar to WHERE NOT IN ( , , ) + + QString fieldName; + + // Get all fields to update, without the keyfield. + // We only support a single field update at this moment. + for( const auto& fn : dataObject->getFieldNames() ) + { + if( fn != dataObject->getKeyField() ) + { + fieldName = fn; + break; + } + } + + auto timeline = m_qhTimelines[sourceId]; + if (!timeline) + { + auto pTimeline = QSharedPointer::create(); + timeline.swap(pTimeline); + m_qhTimelines[sourceId] = timeline; + } + OrmBatchChange obc(Timestamp(dataObject->timeStamp()), dataObject->getValue( fieldName ), OrmBatchChange::toSet(dataObject->getValue( dataObject->getKeyField() ).toList())); + auto proposed = timeline->evaluate(obc, static_cast(TableActions::actionMergeUpdate) == dataObject->getDefaultAction()); + + if (!proposed.valid()) + { + LogInfo( "[OrmTable::batchUpdate]", "Proposed update will not be executed" ); + return false; + } + dataObject->setValue( dataObject->getKeyField(), OrmBatchChange::toList(proposed.changeSet())); + QString strTable = dataObject->getContainerName(); + return m_db.associate(strTable, dataObject->getKeyField(), dataObject->getValue( dataObject->getKeyField() ).toList(), fieldName, dataObject->getValue( fieldName )); +} + +bool OrmTable::deCoupleRecords( ORMData* dataObject ) +{ + QString fieldName; + + // Get all fields to update, without the keyfield. + // We only support a single field update at this moment. + for( const auto& fn : dataObject->getFieldNames() ) + { + if( fn != dataObject->getKeyField() ) + { + fieldName = fn; + break; + } + } + QString strTable = dataObject->getContainerName(); + return m_db.disassociate(strTable, dataObject->getKeyField(), dataObject->getValue( dataObject->getKeyField() ).toList(), fieldName, dataObject->getValue( fieldName )); +} + +QList OrmTable::readData( const QString& fieldName, const QString& filterExp ) +{ + int columnIndex = this->fieldIndex( fieldName ); + QSortFilterProxyModel filterModel; + filterModel.setSourceModel( this ); + + if( columnIndex > -1 ) + { + + filterModel.setFilterKeyColumn( columnIndex ); + filterModel.setFilterRegExp( QRegExp( filterExp, Qt::CaseSensitive, QRegExp::Wildcard ) ); + } + + if( filterModel.rowCount() > 0 ) + { + // Looks like there was a result. Pack into a structure.. + int rows = filterModel.rowCount(); + int columns = filterModel.columnCount(); + + // For each row and column, iterate the data structure.. + for( int _rowCount = 0; _rowCount < rows; _rowCount++ ) + { + qDebug() << "=____"; + for( int _colCount = 0; _colCount < columns; _colCount++ ) + { + qDebug() << filterModel.headerData( _colCount, Qt::Horizontal, Qt::DisplayRole ) << " : " + << filterModel.data( filterModel.index( _rowCount, _colCount ), Qt::DisplayRole ); + } + } + } + + return QList(); +} + +QList OrmTable::getRelationsByTableName( const QString& _tableName ) +{ + QList lstResult; + + for( auto relName : m_qhRelations.keys() ) + { + if( m_qhRelations.value( relName )->foreignTable() == _tableName ) + { + lstResult.append( m_qhRelations.value( relName ) ); + } + } + + return lstResult; +} + +QSqlRecord OrmTable::buildSqlRecord( ORMData* tableObject ) +{ + LogDebug( "[OrmTable::buildSqlRecord]", QString( "Building record with : %1 " ).arg( tableObject->asString() ) ); + QStringList fieldNames = tableObject->getFieldNames(); + QSqlRecord sqlRecord; + for( const QString& fieldName : fieldNames ) + { + if( fieldName != "container" ) + { + QSqlField sqlField( fieldName, m_qhFieldTypes.value( fieldName ) ); + sqlField.setValue( ConversionUtils::convertToType( tableObject->getValue( fieldName ), // The value + m_qhFieldTypes.value( fieldName ) ) ); // The Type + sqlRecord.append( sqlField ); + } + } + + return sqlRecord; +} + +void OrmTable::setRelatedTable(const QString& _tableName, QSortFilterProxyModel* pTable) +{ + m_qhRelTables.insert( _tableName, pTable ); +} + +QSortFilterProxyModel* OrmTable::getRelatedTable(const QString& _tableName) const +{ + return m_qhRelTables.value( _tableName ); +} + + +void OrmTable::saveRelation( const QString& relationName, DbRelation* pRelation ) +{ + LogDebug( "[OrmTable::saveRelation]", QString( "Saving Relation : %1" ).arg( relationName ) ); + m_qhRelations.insert( relationName, pRelation ); +} + +DbRelation* OrmTable::getRelation( const QString& relationName ) const +{ + return m_qhRelations.value( relationName, nullptr ); +} + +QStringList OrmTable::getRelationNames() const +{ + return QStringList( m_qhRelations.keys() ); +} + +ORMData* OrmTable::solveRelations( const QSharedPointer& dataObject ) +{ + // First we get the Maintable.... This will be our return object. + ORMData* pMainTable = dataObject->getMainTableContainer(); + if( nullptr == pMainTable ) + { + return nullptr; + } + + if( pMainTable->hasLists() ) + { + LogWarning( "OrmTable::solveRelations", QString( "Maintable has Lists.... : %1" ).arg( pMainTable->asString() ) ); + } + + QStringList constraintList = this->getRelationNames(); + + for( auto constraint : constraintList ) + { + const DbRelation* l_relation = QPointer( this->getRelation( constraint ) ); + QString foreignPrimKey; + QString foreignFieldName; + QVariant foreignFieldValue; + + LogDebug("[OrmTable::solveRelations]", QString("Get related table %1").arg(l_relation->foreignTable())); + ORMData* pTable = dataObject->getRelatedContainer( l_relation->foreignTable() ); + if( nullptr != pTable ) + { + // We assume there is only 1 relation between two tables + int numFields = pTable->getFieldCount(); + if( 0 == numFields ) + { + LogWarning( "[OrmTable::solveRelations]", QString( "No relations found to table : %1 " ).arg( l_relation->foreignTable() ) ); + return nullptr; + } + else if( 1 == numFields ) + { + foreignPrimKey = l_relation->foreignPrimKey(); + foreignFieldName = pTable->getFieldNames().first(); + // =========================================================================================== + // L I S T O R S I N G L E V A L U E R E L A T I O N + // =========================================================================================== + foreignFieldValue = pTable->getValue( foreignFieldName ); + if( foreignFieldValue.isNull() ) + { + // Some constraints are allowed to be absent. For now treat this constraint as such. + // If it is needed afterall it will prevent the write in a later stage + pMainTable->setValue( l_relation->indexColumn(), QVariant() ); + continue; + } + + // Looks like we have all data collected. Retrieve the Primary key value needed. + // If foreignPrimKey is empty it will be retrieved from the database meta data. + QVariant l_primKeyValue = m_db.getRelationKey( l_relation->foreignTable(), foreignFieldName, foreignFieldValue, foreignPrimKey ); + if( !l_primKeyValue.isNull() ) + { + // We have a value of the IndexColumn. Complete the mainTable.... + pMainTable->setValue( l_relation->indexColumn(), l_primKeyValue ); + } + else + { + LogError( "[OrmTable::solveRelations]", QString( "No primary key found for table : %1 " ).arg( l_relation->foreignTable() ) ); + return nullptr; + } + // =========================================================================================== + } + else + { + /// @todo : At this moment we don't handle multiple relations. Lotsplitting is requiring this, so it must be implemented as soon as + /// the requirements are defined. + LogError( "[OrmTable::solveRelations]", QString( "multiple relations found to table : %1 " ).arg( l_relation->foreignTable() ) ); + return nullptr; + } + } + else + { + LogDebug( "[OrmTable::solveRelations]", QString( "Unresolved constraint %1. We assume all data is provided by the object.." ).arg( constraint ) ); + } + } + + return pMainTable; +} + +int OrmTable::tableFilter( ORMData* dataObject ) +{ + int nResult = -1; + + // Check for a valid pointer. + if( nullptr == dataObject ) + { + LogError( "[OrmTable::tableFilter]", QString( "No data structure given." ) ); + return 0; + } + + // Check if update is the default action + if( static_cast(TableActions::actionUpdate) != dataObject->getDefaultAction() ) + { + LogWarning( "[OrmTable::tableFilter]", QString( "Update is not the default action for table : %1" ).arg( dataObject->getContainerName() ) ); + return 0; + } + + /// @todo : Create an AND / OR filter. + // +----------------------------------+ + // | Keyfield | KeyValue | TypeFilter | + // +----------+----------+------------+ + // | = 1 | = 1 | equals | + // | = 1 | > 1 | OR | ( WHERE IN ( a, b, c, d...) ) + // | > x | > y | AND | ( x equals y) + // | <> KV | <> KF | INVALID | + // +----------+----------+------------+ + // + // Check if there is a record that matches our criteria. + + const auto keyFields = dataObject->getKeyFieldList(); + + for (const auto& field : keyFields) + { + if( this->fieldIndex(field) == -1 ) + { + LogWarning( "[OrmTable::tableFilter]", QString( "No columnIndex found for : %1" ).arg(field) ); + return 0; + } + } + + QString strFilter; + const auto numberOfKeys = keyFields.length(); + + if (0 == numberOfKeys) + { + LogWarning( "[OrmTable::tableFilter]", QString( "No keyField given for table : %1" ).arg( dataObject->getContainerName() ) ); + return 0; + } + else if (1 == numberOfKeys) + { + const auto& keyField = *keyFields.begin(); + + if( dataObject->getValue( keyField ).type() == QVariant::List ) // OR + { + QList tmpList = dataObject->getValue( keyField ).toList(); + for( int lstCount = 0; lstCount < tmpList.count(); lstCount++ ) + { + if( tmpList.at(lstCount).type() == QVariant::Int ) + { + strFilter += keyField + "=" + tmpList.at( lstCount ).toString(); + } + else + { + strFilter += "\"" + keyField + "\"='" + tmpList.at( lstCount ).toString().replace("{","").replace("}","") + "'"; + } + + if( ( lstCount + 1 ) < tmpList.count() ) + { + strFilter += " OR "; + } + } + } + else // equals + { + if( dataObject->getValue( keyField ).type() == QVariant::Int ) + { + strFilter = "\"" + keyField + "\"="+ dataObject->getValue( keyField ).toString(); + } + else + { + strFilter = "\"" + keyField + "\"='" + dataObject->getValue( keyField ).toString().replace("{","").replace("}","") + "'"; + } + } + } + else // numberOfKeys > 1, AND + { + // ========================================================= + // @todo: Setup a multiple fields filter. For now it is just a multiple values value filter. + + auto iter = keyFields.constBegin(); + while (keyFields.constEnd() != iter) + { + if( dataObject->getValue( *iter ).type() == QVariant::Int ) + { + strFilter += "\"" + *iter + "\"=" + dataObject->getValue( *iter ).toString(); + } + else + { + strFilter += "\"" + *iter + "\"='" + dataObject->getValue( *iter ).toString().replace("{","").replace("}","") + "'"; + } + + ++iter; + + if( keyFields.end() != iter ) + { + strFilter += " AND "; + } + } + } + + this->setFilter( strFilter ); + LogDebug( "[OrmTable::tableFilter]", QString( "Filter : %1" ).arg( this->filter() ) ); + if( this->select() ) + { + nResult = this->rowCount(); + } + else + { + LogError( "[OrmTable::tableFilter]", QString( "There was a problem executing the query : %1" ).arg( this->selectStatement() ) ); + } + // ========================================================= + return nResult; +} + +int OrmTable::resetFilter() +{ + this->setFilter( "" ); + this->select(); + return rowCount(); +} + +bool OrmTable::isPackageValid( ORMData* dataObject ) +{ + if( nullptr == dataObject ) + { + LogError( "[OrmTable::isPackageValid]", "No ORMData dataobject passed." ); + return false; + } + + // Determine the field indices for the proxy filter + auto keyFields = dataObject->getKeyFieldList(); + QList fieldIndices; + auto iter = keyFields.begin(); + while (keyFields.end() != iter) + { + auto idx = this->fieldIndex(*iter); + if (idx > -1) + { + fieldIndices.push_back(idx); + ++iter; + } + else + { + LogWarning("[OrmTable::isPackageValid]", QString("Keyfield \"%1\" is unknown. Not using it in filter.").arg(*iter)); + iter = keyFields.erase(iter); + } + } + + // Create the filter model and set its filter criteria + MultiSortFilterProxyModel oModelFilter; + oModelFilter.setFilterKeyColumns( fieldIndices ); + + // Build the filter expression. + for (qint32 i = 0; i < fieldIndices.size(); ++i) + { + oModelFilter.setFilterFixedString( fieldIndices[i], QString( "%1" ).arg( dataObject->getValue( keyFields[i] ).toString() ) ); + } + + // Attach filter to this table model. + oModelFilter.setSourceModel( this ); + + // If the number of record = 1, we seem to have found our record. + if( 0 == oModelFilter.rowCount() ) + { + // Seems like we are good to go. + // This is a first timer... + LogDebug("[OrmTable::isPackageValid]", "No record found"); + return true; + } + else if ( 1 == oModelFilter.rowCount() ) + { + QModelIndex modIndex = oModelFilter.index( 0, this->fieldIndex( m_recTrackField ) ); + unsigned long long tmp_timestamp = oModelFilter.data( modIndex, Qt::DisplayRole ).toULongLong(); + + if( dataObject->timeStamp() > tmp_timestamp ) + { + return true; + } + } + else + { + LogError( "[OrmTable::isPackageValid]", QString( "Multiple records found (%1) for %2 with value %3" ) + .arg( oModelFilter.rowCount() ) + .arg( dataObject->getKeyField() ) + .arg( dataObject->getValue( dataObject->getKeyField() ).toString() ) ); + } + return false; +} + +bool OrmTable::commit() +{ + if( m_db.supportTransactions() ) + { + if( m_db.transactionCommit() ) + { + return true; + } + else + { + LogWarning( "[OrmTable::commit]", QString( "Transaction failed to commit with error : %1" ) + .arg( m_db.getDatabase().driver()->lastError().text() ) ); + } + } + else + { + if( m_db.getDatabase().driver()->lastError().driverText().isEmpty() ) + { + return true; + } + else + { + LogDebug( "[OrmTable::commit]", QString( "Transaction not supported but last query failed with error : %1" ) + .arg( m_db.getDatabase().driver()->lastError().driverText() ) ); + } + } + return false; +} diff --git a/src/ormtable.h b/src/ormtable.h new file mode 100644 index 0000000..ec9a5e7 --- /dev/null +++ b/src/ormtable.h @@ -0,0 +1,337 @@ +/* **************************************************************************** + * Copyright 2019 Open Systems Development BV * + * * + * Permission is hereby granted, free of charge, to any person obtaining a * + * copy of this software and associated documentation files (the "Software"), * + * to deal in the Software without restriction, including without limitation * + * the rights to use, copy, modify, merge, publish, distribute, sublicense, * + * and/or sell copies of the Software, and to permit persons to whom the * + * Software is furnished to do so, subject to the following conditions: * + * * + * The above copyright notice and this permission notice shall be included in * + * all copies or substantial portions of the Software. * + * * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL * + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING * + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER * + * DEALINGS IN THE SOFTWARE. * + * ***************************************************************************/ +#ifndef OSDEV_COMPONENTS_ORMTABLE_H +#define OSDEV_COMPONENTS_ORMTABLE_H + +#include +#include +#include +#include +#include +#include +#include + +#include "dbconnector.h" +#include "ormreldata.h" +#include "ormtablerow.h" +#include "dbrelation.h" +#include "conversionutils.h" +#include "timeline.h" + +namespace osdev { +namespace components { + +/*! + * \brief This class is used as a TransActionsscopeGuard + * It takes a database connection at its constructor + * and calls the rollBack method in its destructor. + */ +class OrmTransGuard +{ +public: + OrmTransGuard() + : m_db(nullptr) + { + } + + explicit OrmTransGuard( DbConnector& _db ) + : m_db( &_db ) + { + } + + ~OrmTransGuard() + { + if (nullptr != m_db) + { + m_db->transactionRollback(); + } + } + + OrmTransGuard(const OrmTransGuard&) = delete; + OrmTransGuard& operator=(const OrmTransGuard&) = delete; + + OrmTransGuard(OrmTransGuard&& rhs) + : m_db(std::move(rhs.m_db)) + { + rhs.m_db = nullptr; + } + + OrmTransGuard& operator=(OrmTransGuard&& rhs) + { + if (this == &rhs) + { + return *this; + } + + if (nullptr != m_db) + { + m_db->transactionRollback(); + } + m_db = std::move(rhs.m_db); + rhs.m_db = nullptr; + return *this; + } + +private: + DbConnector* m_db; +}; + +/*! + * \brief The OrmTable represents a database table. Depending + * on the EditStrategy, it updates the database by each + * inserted record or after all records are inserted. + * \note Internally setTable is used. Please do not use setQuery + * also because that will invalidate the OrmTable meta data. + */ +class OrmTable : public QSqlRelationalTableModel +{ + Q_OBJECT + +public: + /*! + * \brief Default constructor. + * \param parent The parent object as a QObject pointer + * \param db The database layer we're connected through. + */ + explicit OrmTable(const DbConnector& db, QObject *parent = nullptr ); + + // Deleted copy- and move constructors + OrmTable( const OrmTable& ) = delete; + OrmTable( const OrmTable&& ) = delete; + OrmTable& operator=( const OrmTable& ) = delete; + OrmTable& operator=( const OrmTable&& ) = delete; + + /// \brief Destructor + virtual ~OrmTable(); + + /*! + * \brief Sets the tablename, this model represents. + * \param table The name of the table. + */ + void setTable( const QString& table ); + + /*! + * \brief Insert a single QSqlRecord into the model. + * \param row Inserts the record at position row. + * If row is negative, the record will be appended to the end. + * \param record Record to insert + * \return true if the record could be inserted, otherwise false. + */ + bool insertRecord( int row, const QSqlRecord &record ); + + /*! + * \brief Insert multiple QSqlRecords into the model. + * \param row Inserts the records at position row. + * If row is negative, the records will be appended to the end. + * calls insertRecord internally. + * \param records Records to insert + * \return true if the record could be inserted, otherwise false. + */ + bool insertRecords(int row, const QList& records ); + + /*! + * \brief writes the data given in the dataobject to the table and to the database. + * \param dataObject - Contains all relational data. + */ + void writeData( const QSharedPointer& dataObject ); + + /*! + * \brief writes the values to a given record, identified by the fieldname in "KeyField". + * \param dataObject - Contains "flat" data + * \return True if successful, false if not. + */ + bool updateRecord( ORMData* dataObject ); + + /*! + * \brief This method is for convenience only. It will retrieve (a set of) records, based on the criteria given. + * \param fieldName - The fieldname we like to filter on + * \param filterExp - The filter expression + * \return A QList of SQLRecords containing the results. + * The list will be empty if no records were found or the table is empty. + */ + QList readData( const QString& fieldName = QString(), const QString& filterExp = QString() ); + + + /*! + * \brief Returns the number of relations added to this table. + * \return The number of relations. 0 if none. + */ + int numberOfRelations() const { return m_qhRelations.count(); } + + /*! + * \brief setRelatedTable + * \param tableName The name of the related table. + * \todo Verify pointer ownership + * \param pTable Owning pointer to the Table. Ownership is transferred to this instance. + */ + void setRelatedTable( const QString& tableName, QSortFilterProxyModel* pTable ); + + /*! + * \brief Gets the RelatedTable for the specified table name. + * \param tableName The name of the table for which to get the relation. + * \return The relation to the table with the specified table name. + */ + QSortFilterProxyModel* getRelatedTable( const QString& tableName ) const; + + /*! + * \brief Store the relation to another table, stored by its unique name. + * ORMTable is owner of the relation pointer. + * \param relationName - the Name of the relation as known by the database. + */ + void saveRelation( const QString& relationName, DbRelation* pRelation ); + + /*! + * \brief Get the relationstructure + * \param relationName The name of the relation to get. + * \return The pointer to the datastructure. NULL if none exist. + */ + DbRelation* getRelation( const QString& relationName ) const; + + /*! + * \brief Get all registered constraintnames by their true name. + * \return The list of all relation by their contraint names. If no relations exist, the list will be empty. + */ + QStringList getRelationNames() const; + + /*! + * \brief Gets the attached relation based on a given tablename. + * \param tableName - The tablename we want the relation on. + * \return The relation we're searching for. isValid is true if there is a relation, false if not. + */ + QList getRelationsByTableName( const QString& tableName ); + + /*! + * \brief This method will solve any relations in the structure. + * \param dataObject - The ORMRelData object describing the data + * \return The ORMData object with all solved references, if there were any. + * If the main container can't be retrieved, it will return a nullptr. Check for validity. + */ + ORMData* solveRelations( const QSharedPointer& dataObject ); + + /*! + * \brief Do a "batched" update. The dataobject contains lists which represents different records. + * \param dataObject - The "maintable" object after resolving the relations. + * It is the responsibility of this TableObject to delete this object if successful, + * else it will be rejected and ownership will be transferred to the next subsystem. + * before calling this method, a transaction should be started on the referenced database + * connection. If this method ( and possible following statements like deCoupleRecords ) + * the transaction should be committed. + * \param sourceId The source from which the dataObject originates. + * \return True if the complete update was successful. False if not and should result in a rejected + * transaction. ( See signalRejectedData() ) Rejecting should be done by the calling method. + */ + bool batchUpdate( ORMData* dataObject, const QUuid& sourceId ); + + /*! + * \brief Decouple records from their previous value (Normally a foreign key relation). + * This method will fail if a field in the database is not-nullable or the number + * of fields is larger than 2 (1 searchField and 1 updateable field. + * \param dataObject - The dataobject containing all the information needed to update the record(s). + * It is the responsibility of this TableObject to delete this object if successful, + * else it will be rejected and ownership will be transferred to the next subsystem. + * before calling this method, a transaction should be started on the referenced database + * connection. If this method ( and possible previous statements like batchUpdate() ) + * the transaction should be committed. + * \return true if successful. false if not. + */ + bool deCoupleRecords( ORMData* dataObject ); + + /*! + * \brief Get the name of the field that will track the mutation date / time in uSecs since EPOCH. + * \return The name of the field. Empty if no field was configured. + */ + QString trackField() { return m_recTrackField; } + + /*! + * \brief Get the name of the field that will track the mutation date / time in uSecs since EPOCH. + * \param _recTrackFieldName - The name of the field, coming from the configuration. + */ + void setTrackField( const QString& _recTrackFieldName ) { m_recTrackField = _recTrackFieldName; } + + +signals: + /*! + * \brief Signal for RejectedData. + * \param dataContainer The data container for this signal. + */ + void signalRejectedData( const QSharedPointer& dataContainer ); + +private: + + /*! + * \brief Translates an ORMData into an QSqlRecord + * \param tableObject - The tableObject we like to translate into an sql-record to store. + * \return The QSqlRecord object representing an entry. If the creation of the record fails, + * the result will be an empty SqlRecord structure. + */ + QSqlRecord buildSqlRecord( ORMData* tableObject ); + + /*! + * \brief Builds a query and filters the table data. Data isn't removed, but a resultset is shown. + * + * \param dataObject - The dataobject of this particular table as extracted from ORMRelData. Based on the + * keyField(s) and values, the filter is set. ( See also resetFilter() ) + * \return The number of resulting records that match the criteria. If the filter fails, the result will be -1. + */ + int tableFilter( ORMData* dataObject ); + + /*! + * \brief Checks if the ORMData package is valid against the record it tries to update. Each table should have a + * field of type INT which contains the timestamp since epoch. Before updating a record, it should check if the timestamp + * of the package is actually newer than the timestamp of that record. If the package is newer ( a.k.a. Package timestamp > timestamp_record ) + * it is valid to update the record with its values, including the timestamp. This will prevent the situation where records will be overwritten + * with "old" data. This method works together with the internal variable 'm_recTrackField'. Before calling this method, we have to check if this + * variable isn't empty. If so, we don't keep track of record changes in a timeline and we can update the records anyway. + * + * \param dataObject - The ORMData object containing a complete transaction. + * Does not have to be resolved by relations yet, this will be done after the validity check. + * + * \return True if the record can be updated, False if the package is older than the actual record. + */ + bool isPackageValid( ORMData* dataObject ); + + /*! + * \brief resets the filter, allowing the table to show all records. ( See also tableFilter() ) + * \return The number of records show by the table. + */ + int resetFilter(); + + /*! + * \brief Commit a database transaction if one is in progress. + * \note If the driver does not support transactions this method will look at the last driver error and return false if it contains a message. + * \return True if commit succeeds or if there was no transaction in progress, false if commit fails. + */ + bool commit(); + + QString m_className; ///< Class name for logging purposes + QString m_recTrackField; ///< Name of the field, which keep tracks of changes on each record. If Empty, we don't track. + DbConnector m_db; ///< Database layer we're connected through. We need the reference to access its helper methods. + QHash m_qhRelTables; ///< Filtered view on a related table. + QHash m_qhRelations; ///< The actual relation Meta information stored for reference. + QHash m_qhFieldTypes; ///< The internal storage for all fieldtypes by their fieldname. + QHash> m_qhTimelines; ///< Timelines used for the batch update +}; + +} // End namespace components +} // End namespace osdev + +#endif /* OSDEV_COMPONENTS_ORMTABLE_H */ diff --git a/src/ormtablerow.cpp b/src/ormtablerow.cpp new file mode 100644 index 0000000..b004fd1 --- /dev/null +++ b/src/ormtablerow.cpp @@ -0,0 +1,45 @@ +#include "ormtablerow.h" + +using namespace osdev::components; + +OrmTableRow::OrmTableRow() + : m_stoData() +{ + +} + +OrmTableRow::~OrmTableRow() +{ + m_stoData.clear(); +} + +void OrmTableRow::setField( QVariant vData, int column ) +{ + if( -1 == column || column >= m_stoData.count() ) + { + m_stoData.append( vData ); + } + else if( -1 < column && column < m_stoData.count() ) + { + m_stoData.replace( column, vData ); + } +} + +QVariant OrmTableRow::getField( int column ) +{ + if( -1 < column && column < m_stoData.count() ) + { + return m_stoData.at( column ); + } + return QVariant(); +} + +void OrmTableRow::setFields( QList values ) +{ + // Replace the entire internal structure with the new list. + m_stoData.clear(); + for( const QVariant& value : values ) + { + m_stoData.append( value ); + } +} diff --git a/src/ormtablerow.h b/src/ormtablerow.h new file mode 100644 index 0000000..a214c85 --- /dev/null +++ b/src/ormtablerow.h @@ -0,0 +1,33 @@ +#ifndef OSDEV_COMPONENTS_ORMTABLEROW_H +#define OSDEV_COMPONENTS_ORMTABLEROW_H + +#include +#include + +namespace osdev { +namespace components { + +/*! + * \brief This class represents a single record, completely decoupled. + * No fieldnames and no constraints. + */ +class OrmTableRow +{ +public: + OrmTableRow(); + virtual ~OrmTableRow(); + + void setField( QVariant vData, int column = -1 ); + QVariant getField( int column ); + + int getNumFields(); + void setFields( QList values ); + +private: + QList m_stoData; +}; + +} /* End namespace components */ +} /* End namespace osdev */ + +#endif /* OSDEV_COMPONENTS_ORMTABLEROW_H */ diff --git a/src/ormthread.cpp b/src/ormthread.cpp new file mode 100644 index 0000000..4a7ddb4 --- /dev/null +++ b/src/ormthread.cpp @@ -0,0 +1,38 @@ +#include "ormthread.h" +#include "ormhandler.h" +#include "log.h" + +using namespace osdev::components; + +ORMThread::ORMThread() +{ +} + +ORMThread::~ORMThread() +{ +} + +void ORMThread::run() +{ + OrmHandler *p_OrmHandler = new OrmHandler(); + + // Connect incoming data + connect( this, &ORMThread::signalSendData, p_OrmHandler, &OrmHandler::receiveData ); + // Cascade connect the rejectedData + connect( p_OrmHandler, &OrmHandler::signalRejectedData, this, &ORMThread::signalRejectedData ); + + p_OrmHandler->start(); + + this->exec(); +} + +void ORMThread::dataToThread( const QSharedPointer& data ) +{ + QCoreApplication::processEvents(); + LogDebug( "[ORMThread::dataToThread]", QString( "Data received for container : %1" ) + .arg( data->getMainTableName() ) ); + LogDebug( "[ORMThread::dataToThread]", data->asString() ); + + emit signalSendData( data ); + QCoreApplication::processEvents(); +} diff --git a/src/ormthread.h b/src/ormthread.h new file mode 100644 index 0000000..37b1151 --- /dev/null +++ b/src/ormthread.h @@ -0,0 +1,86 @@ +#ifndef OSDEV_COMPONENTS_ORMTHREAD_H +#define OSDEV_COMPONENTS_ORMTHREAD_H + +#include +#include + +#include "ormreldata.h" + +/* ______________________________________ + * / Good day to deal with people in high \ + * | places; particularly lonely | + * \ stewardesses. / + * -------------------------------------- + * \ + * \ + * .--. + * |o_o | + * |:_/ | + * // \ \ + * (| | ) + * /'\_ _/`\ + * \___)=(___/ + *//*! + * \brief This class *manages* the thread, the ORM layer will be started in. + * Derived from QThread it isn't the thread itself but implements the + * 'run()' method that'll start the thread. (QThread isn't meant to be + * instantiated. It has to be derived.. :) ) + * + * And sometimes it can be a Stewardess. That's polymorphism for yah! + */ +namespace osdev { +namespace components { + +class ORMThread : public QThread +{ + Q_OBJECT + +public: + ORMThread(); + + // Deleted copy-constructor + ORMThread( const ORMThread& ) = delete; + ORMThread( const ORMThread&& ) = delete; + ORMThread& operator=( const ORMThread& ) = delete; + ORMThread& operator=( const ORMThread&& ) = delete; + + /*! + * \brief Destructor + */ + virtual ~ORMThread(); + + /*! + * \brief Override from QThread. This will actually create and start the thread. + * Objects used here are "moved" to the new thread and run + */ + void run() override; + + /*! + * \brief Send the data to the intended thread. + * \param data Data coming from the plugin. Ownership is moved to the thread and that + * specific thread is responsible for cleaning up. + */ + void dataToThread( const QSharedPointer& data ); + +signals: + /*! + * \brief Used to send the datastructure to the thread. + * \param data Data coming from the plugin. + */ + void signalSendData( const QSharedPointer& data ); + + /*! + * \brief If the thread is unable to process the data that was sent, it will "reject" this. + * Normally it would then be send to a subsystem, capable of storing the data and + * resend it for re-processing. + * \param data Data originally comming from the plugin (it can be enriched along the way). + */ + void signalRejectedData( const QSharedPointer& data ); + +}; + + +} /* End namespace components */ +} /* End namespace osdev */ + +#endif /* OSDEV_COMPONENTS_ORMTHREAD_H */ diff --git a/src/ormtransqueue.cpp b/src/ormtransqueue.cpp new file mode 100644 index 0000000..bb46c30 --- /dev/null +++ b/src/ormtransqueue.cpp @@ -0,0 +1,110 @@ +#include "ormtransqueue.h" + +#include +#include "log.h" + +namespace osdev { +namespace caelus { + +OrmTransQueue::OrmTransQueue( QObject *_parent ) + : QObject(_parent) + , m_pQueueTimer( nullptr ) + , m_pQueueMutex( nullptr ) + , m_ormQueue() +{ + m_pQueueMutex = new QMutex( QMutex::NonRecursive ); + setTimeOut( 1000 ); +} + +void OrmTransQueue::setTimeOut( int mseconds ) +{ + if( nullptr == m_pQueueTimer ) + { + m_pQueueTimer = new QTimer( this ); + m_pQueueTimer->setSingleShot( false ); + + // Connect signal / slot.. + connect( m_pQueueTimer, SIGNAL( timeout() ), + this, SLOT( slotProcessQueue() ), Qt::QueuedConnection ); + } + + // Set the interval in milliseconds and start. + m_pQueueTimer->setInterval( mseconds ); +} + +bool OrmTransQueue::processing() const +{ + return m_pQueueTimer->isActive(); +} + +void OrmTransQueue::startProcessing( bool force ) +{ + if( force ) + { + m_pQueueTimer->stop(); + this->slotProcessQueue(); + } + else + { + if( !m_pQueueTimer->isActive() ) + { + m_pQueueTimer->start(); + } + } +} + +void OrmTransQueue::stopProcessing( bool force ) +{ + if( force && !m_pQueueTimer->isActive() ) + { + m_pQueueTimer->stop(); + } +} + +bool OrmTransQueue::setTransaction( ORMRelData *pData ) +{ + QMutexLocker mutLock( m_pQueueMutex ); // << Later on we should do a conditional wait + LogDebug( "[OrmTransQueue::setTransaction]", "Adding transaction to the transaction queue : "); + m_ormQueue.enqueue( pData ); + LogDebug("[OrmTransQueue::setTransaction]", QString("Number of transactions in the queue : %1").arg( m_ormQueue.size() ) ); + this->startProcessing(); + + return true; // << Rethink about it.. +} + +void OrmTransQueue::slotProcessQueue() +{ + m_pQueueTimer->stop(); + LogInfo( "[OrmTransQueue::slotProcessQueue]", "Start processing the transaction queue." ); + if( m_pQueueMutex->tryLock() ) + { + LogDebug( "[OrmTransQueue::slotProcessQueue]", "!!!!!!!!!!!!!!!!!!!!! Lock Acquired !!!!!!!!!!!!!!!" ); + if( !m_ormQueue.isEmpty() ) + { + LogInfo( "[OrmTransQueue::slotProcessQueue]", QString( "{ Before Emit } Number of transactions in the queue : %1 ").arg( m_ormQueue.size() ) ); + ORMRelData *pData = m_ormQueue.dequeue(); + Q_UNUSED( pData ); + LogDebug( "[OrmTransQueue::slotProcessQueue]", QString("{ Before Emit, after dequeue} Number of transactions in the queue : %1 ").arg( m_ormQueue.size() ) ); + // emit signalProcessData( pData ); + LogDebug( "[OrmTransQueue::slotProcessQueue]", QString("{ After Emit, after dequeue} Number of transactions in the queue : %1 ").arg( m_ormQueue.size() ) ); + QCoreApplication::processEvents(); + } + m_pQueueMutex->unlock(); + LogDebug( "[OrmTransQueue::slotProcessQueue]", "!!!!!!!!!!!!!!!!!!!!! Lock Freed !!!!!!!!!!!!!!!" ); + } + else + { + LogDebug( "[OrmTransQueue::slotProcessQueue]", "!!!!!!!!!!!!!!!!!!!!! No Lock !!!!!!!!!!!!!!!" ); + } + + LogInfo( "[OrmTransQueue::slotProcessQueue]", "Done processing the transaction queue." ); + if( m_ormQueue.size() > 0 ) + { + m_pQueueTimer->start(); + } +} + + + +} /* End namespace caelus */ +} /* End namespace osdev */ diff --git a/src/ormtransqueue.h b/src/ormtransqueue.h new file mode 100644 index 0000000..8ca98ed --- /dev/null +++ b/src/ormtransqueue.h @@ -0,0 +1,79 @@ +#ifndef OSDEV_CAELUS_ORMTRANSQUEUE_H +#define OSDEV_CAELUS_ORMTRANSQUEUE_H + +// Qt +#include +#include +#include +#include +#include + +// Local +#include "ormreldata.h" + +namespace osdev { +namespace caelus { + +class OrmTransQueue : public QObject +{ + Q_OBJECT + +public: + explicit OrmTransQueue( QObject *_parent = 0 ); + + // Copy constructors. + + // Deleted copy- and move constructors + OrmTransQueue( const OrmTransQueue& ) = delete; + OrmTransQueue( const OrmTransQueue&& ) = delete; + OrmTransQueue& operator=( const OrmTransQueue& ) = delete; + OrmTransQueue& operator=( const OrmTransQueue&& ) = delete; + + // Timer control. + /*! + * \brief setTimeOut + * \param msec + */ + void setTimeOut(int mseconds = 1 ); + + // Queue control + /*! + * \brief Inserts a ORMReldata objectpointer into the queue. + * \param pData - the (valid) pointer of the ORMRelData structure we want to queue for later processing + * \return True if transaction was saved to the queue successfully, false if not. + */ + bool setTransaction( ORMRelData *pData ); + + bool processing() const; + + void startProcessing( bool force = false ); + + void stopProcessing( bool force = false ); + + /*! + * \brief Returns the number of transactions stored in the queue. + * \return The number of transactions, or 0 if empty. + */ + int transactions_count() { return m_ormQueue.size(); } + +signals: + void signalProcessData( ORMRelData *pData ); + +private: + QTimer *m_pQueueTimer; ///< Timer controlling the processing of the queue. + QMutex *m_pQueueMutex; ///< Mutex to prevent race conditions. + QQueue m_ormQueue; ///< The actual FIFO taking a ORMRelData pointer as input. + +private slots: + /*! + * \brief slotProcessQueue + */ + void slotProcessQueue(); + + +}; + +} /* End namespace caelus */ +} /* End namespace osdev */ + +#endif /* OSDEV_CAELUS_ORMTRANSQUEUE_H */ diff --git a/src/timeline.cpp b/src/timeline.cpp new file mode 100644 index 0000000..bc84f13 --- /dev/null +++ b/src/timeline.cpp @@ -0,0 +1,49 @@ +#include "timeline.h" + +#include "log.h" + +using namespace osdev::components; + +Timeline::Timeline() + : m_timeline(10) + , m_proposedChange() +{ +} + +const OrmBatchChange& Timeline::evaluate(const OrmBatchChange& desiredChange, bool exclusiveUpdate) +{ + m_proposedChange = OrmBatchChange {}; + if (m_timeline.full() && desiredChange < m_timeline.front()) + { + LogWarning("[Timeline::evaluate]", QString("Incoming change (%1) is older then the oldest registered change (%2).").arg(desiredChange.timestamp().toString()).arg(m_timeline.front().timestamp().toString())); + return m_proposedChange; // change is older then anything we know, discard. + } + + m_proposedChange = desiredChange; + for (const auto& ch : m_timeline) + { + if (!m_proposedChange.processChange(ch, exclusiveUpdate)) + { + break; + } + } + if ( m_proposedChange.valid() + && !m_timeline.empty() + && m_proposedChange < m_timeline.back()) + { + // reset the timestamp so that the proposed change will be the latest in the timeline when committed. + auto ts = m_timeline.back().timestamp(); + m_proposedChange.setTimestamp(++ts); + } + return m_proposedChange; +} + +void Timeline::commit() +{ + if (m_proposedChange.valid()) + { + m_timeline.push_back(m_proposedChange); + m_proposedChange = OrmBatchChange {}; + } +} + diff --git a/src/timeline.h b/src/timeline.h new file mode 100644 index 0000000..37e2a31 --- /dev/null +++ b/src/timeline.h @@ -0,0 +1,57 @@ +#ifndef OSDEV_COMPONENTS_TIMELINE_H +#define OSDEV_COMPONENTS_TIMELINE_H + +// boost +#include + +#include "ormbatchchange.h" + +namespace osdev { +namespace components { + +/** + * @brief The timeline contains the changes that where made to a specific Batch/Merge update table. + * Only a small number of changes is recorded. A change arrives that is older then the oldest recorded + * change this change will not be considered. + */ +class Timeline +{ +public: + /** + * @brief Creates an empty timeline. + */ + Timeline(); + + // Non copyable, non movable. + Timeline(const Timeline&) = delete; + Timeline& operator=(const Timeline&) = delete; + Timeline(Timeline&&) = delete; + Timeline& operator=(Timeline&&) = delete; + + /** + * @brief Evaluate an incoming change against the already registered changes. + * A previously evaluated change is discarded. + * @param desiredChange The incoming change. + * @param exclusiveUpdate Flag that indicates if this change should be evaluated in an exclusive manner. + * @return The reshaped and possibly invalidated change that is left. + * @note The evaluated change is cached so that it can be commited if the database update actually succeeds. + */ + const OrmBatchChange& evaluate(const OrmBatchChange& desiredChange, bool exclusiveUpdate); + + /** + * @brief Commit an evaluated change. + * Only valid proposals are commited. This method should be called when the change + * is sucessfuly written to the database. + * @note To make this somewhat trustworthy the underlying database should support transactions. + */ + void commit(); + +private: + boost::circular_buffer m_timeline; ///< Only a small number of changes are buffered. + OrmBatchChange m_proposedChange; ///< Cache the evaluated proposed change for commiting. +}; + +} /* End namespace components */ +} /* End namespace osdev */ + +#endif /* OSDEV_COMPONENTS_TIMELINE_H */ diff --git a/src/timestamp.cpp b/src/timestamp.cpp new file mode 100644 index 0000000..cef6a72 --- /dev/null +++ b/src/timestamp.cpp @@ -0,0 +1,83 @@ +/* **************************************************************************** + * Copyright 2019 Open Systems Development BV * + * * + * Permission is hereby granted, free of charge, to any person obtaining a * + * copy of this software and associated documentation files (the "Software"), * + * to deal in the Software without restriction, including without limitation * + * the rights to use, copy, modify, merge, publish, distribute, sublicense, * + * and/or sell copies of the Software, and to permit persons to whom the * + * Software is furnished to do so, subject to the following conditions: * + * * + * The above copyright notice and this permission notice shall be included in * + * all copies or substantial portions of the Software. * + * * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL * + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING * + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER * + * DEALINGS IN THE SOFTWARE. * + * ***************************************************************************/ +#include "timestamp.h" + +using namespace osdev::components; + +Timestamp::Timestamp() + : m_msecsSinceEpoch(0) + , m_seqNr(0) +{ +} + +Timestamp::Timestamp(unsigned long long msecsSinceEpoch) + : m_msecsSinceEpoch(msecsSinceEpoch) + , m_seqNr(1) +{ +} + +bool Timestamp::operator<(const Timestamp& rhs) const +{ + if (m_msecsSinceEpoch < rhs.m_msecsSinceEpoch) + { + return true; + } + + if (m_msecsSinceEpoch == rhs.m_msecsSinceEpoch) + { + return m_seqNr < rhs.m_seqNr; + } + return false; +} + +Timestamp& Timestamp::operator++() // prefix +{ + if (!valid()) + { + return *this; // an invalid timestamp can only be made valid by assigning a valid timestamp to it. + } + + if (m_seqNr < std::numeric_limits::max()) + { + ++m_seqNr; + } + else + { + // The idea is to never reach this point by choosing a large datatype. + // Overflowing is not an option because then the < relation doesn't hold anymore. + throw std::out_of_range("sequence number overflow"); + } + return *this; +} + +Timestamp Timestamp::operator++(int) // postfix +{ + auto cpy(*this); + ++(*this); + return cpy; +} + +QString Timestamp::toString() const +{ + return QString("%1:%2").arg(m_msecsSinceEpoch).arg(m_seqNr); +} + diff --git a/src/timestamp.h b/src/timestamp.h new file mode 100644 index 0000000..a19fdb2 --- /dev/null +++ b/src/timestamp.h @@ -0,0 +1,82 @@ +#ifndef OSDEV_COMPONENTS_TIMESTAMP_H +#define OSDEV_COMPONENTS_TIMESTAMP_H + +#include +#include + +#include + +namespace osdev { +namespace components { + +/** + * @brief Timestamp class that allows formation of a sequence of ordered events. + */ +class Timestamp +{ +public: + /** + * @brief Creates an invalid timestamp. This is the oldest possible timestamp. + * @note A timestamp can only become valid by assigning a valid timestamp to it. + */ + Timestamp(); + + /** + * @brief Creates a timestamp based on miliseconds since the epoch. + * @param msecsSinceEpoch Number of miliseconds since the epoch. Value 0 leads to valid timestamp. + * @note Timestamp with value 0 is larger then the invalid timestamp. + */ + explicit Timestamp(unsigned long long msecsSinceEpoch); + + + // default constructable and movable. + Timestamp(const Timestamp&) = default; + Timestamp& operator=(const Timestamp&) = default; + Timestamp(Timestamp&&) = default; + Timestamp& operator=(Timestamp&&) = default; + + /** + * @return true if this timestamp is valid, false otherwise. + */ + bool valid() const + { + return (m_seqNr > 0); + } + + /** + * @return true if this timestamp is smaller (older) then rhs. + */ + bool operator<(const Timestamp& rhs) const; + + /** + * @brief Prefix addition operator. + * Increases the Timestamp sequencenumber so that timestamps with the same number of msecs since the epoch can also be ordered. + * @note An invalid timestamp remains invalid. + * @return Reference to this timestamp. + * @throw std::out_of_range exception when seqnr is about to overflow. + */ + Timestamp& operator++(); + + /** + * @brief Postfix addition operator. + * Increases the Timestamp sequencenumber so that timestamps with the same number of msecs since the epoch can also be ordered. + * @note An invalid timestamp remains invalid. + * @return The timestamp as it was before increment. + * @throw std::out_of_range exception when seqnr is about to overflow. + */ + Timestamp operator++(int); + + /** + * @return Stringified timestamp as msecsSinceEpoch:seqNr. + */ + QString toString() const; + +private: + unsigned long long m_msecsSinceEpoch; ///< Number of miliseconds since the epoch. + unsigned long long m_seqNr; ///< Sequence number. +}; + +} /* End namespace components */ +} /* End namespace osdev */ + +#endif /* OSDEV_COMPONENTS_TIMESTAMP_H */ diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt new file mode 100644 index 0000000..3635320 --- /dev/null +++ b/tests/CMakeLists.txt @@ -0,0 +1,30 @@ +cmake_minimum_required(VERSION 3.0) +LIST(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/../cmake) + +include(projectheader) +project_header(test_logutils) + +include_directories( SYSTEM + ${CMAKE_CURRENT_SOURCE_DIR}/../../src +) + +include(compiler) +set(SRC_LIST +) + +# add_executable( ${PROJECT_NAME} +# ${SRC_LIST} +# ) + +# target_link_libraries( +# ${PROJECT_NAME} +# ) + +# set_target_properties( ${PROJECT_NAME} PROPERTIES +# RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin +# LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib +# ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/archive +# ) + +# include(installation) +# install_application() -- libgit2 0.21.4