mirror of
https://gitlab.com/spaceti-app/integrations/acs/colnod-connector.git
synced 2026-07-12 11:50:37 +02:00
129 lines
3.4 KiB
C++
129 lines
3.4 KiB
C++
#pragma once
|
|
|
|
#include <QSql>
|
|
|
|
#include "data.h"
|
|
|
|
#include "serversdk/databasemanager.h"
|
|
#include "serversdk/databasetable.h"
|
|
#include "serversdk/databaseutils.h"
|
|
#include "serversdk/logger.h"
|
|
#include <QCoreApplication>
|
|
|
|
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<Data> downloadData();
|
|
bool processData(const std::shared_ptr<Data> &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<QString>& 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<QString, std::shared_ptr<Token> > &tokens, const QSet<QString> &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<QString, std::shared_ptr<Subject> > &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<QString> &ids, const QString &tableName);
|
|
|
|
/**
|
|
* @brief Get token from database
|
|
*
|
|
* @param id Id of token accessing
|
|
*
|
|
* @return Return token
|
|
*/
|
|
std::shared_ptr<Token> 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<Data> composeData(const QSqlQuery &updatedQuery, QSqlQuery &deletedQuery);
|
|
|
|
signals:
|
|
void downloadDataFinished(std::shared_ptr<Data> = std::shared_ptr<Data>());
|
|
|
|
private:
|
|
bool mValid = true;
|
|
|
|
bool mDryMode;
|
|
serversdk::DatabaseManager *mDbMan = nullptr;
|
|
std::unique_ptr<serversdk::DatabaseUtils> utils;
|
|
};
|