#pragma once #include #include "data.h" #include "serversdk/databasemanager.h" #include "serversdk/databasetable.h" #include "serversdk/databaseutils.h" #include "serversdk/logger.h" #include Q_DECLARE_LOGGING_CATEGORY(database) class DatabaseAPI : public QObject { Q_OBJECT public: explicit DatabaseAPI(serversdk::DatabaseManager *dbMan, bool dryMode); /** * @brief Connecting to database * @param host * @param name * @param user * @param password * @return True if success False otherwise */ bool open(const QString& host = "192.168.1.94", const QString& name = "deloitte", const QString& user = "postgres", const QString& password = "34rjkciea12"); std::shared_ptr downloadData(); bool processData(const std::shared_ptr &data); /** * @brief Get time of last synchronization * * LastSync time is stored in database, config table */ qint64 getLastSync(); /** * @brief Set time of acctual synchronization * @param timestamp */ bool setLastSync(qint64 timestamp); /** * @brief Update time of tokens which were deleted in actual sync * @param ids Ids of deleted items */ void reupdateDeleted(const QList& ids); /** * @brief Get time from database server * @return Time in miliseconds */ qint64 getNow(); /** * @brief Push tokens prepared in this sync to database * * @param tokens Tokens to be pushed to database * @param filters Can be used to omit tokens properties (Properties in filters won't be pushed to database * * @return True if success, False otherwise */ bool uploadTokens(const QHash > &tokens, const QSet &filters = {}); /** * @brief Push subjects prepared in this sync to database * * @param subjects Subjects prepared to be pushed to database * * @return True if success, False otherwise */ bool uploadSubjects(const QHash > &subjects); /** * @brief Set deleted of tokens/subjects to true * * @param ids Items which was prepared to be deleted from database * @param tableName Name of table with subjects or tokens * * @return True if success, False otherwise */ bool deleteItems(const QList &ids, const QString &tableName); /** * @brief Get token from database * * @param id Id of token accessing * * @return Return token */ std::shared_ptr getToken(const QString &id); /** * @brief Check if this sync was without any problem */ bool isValid(); // Helper functions for naming new tokens qint16 getNumOfTokensOfSubject(const QString &id); QString getSubjectNameByTokenId(const QString &tokenId); private: int exists(const QString &id, const QString &tableName); bool executeQuery(QSqlQuery &query, const QString &statement = QString()); std::shared_ptr composeData(const QSqlQuery &updatedQuery, QSqlQuery &deletedQuery); signals: void downloadDataFinished(std::shared_ptr = std::shared_ptr()); private: bool mValid = true; bool mDryMode; serversdk::DatabaseManager *mDbMan = nullptr; std::unique_ptr utils; };