colnod-connector/src/colnod/databaseapi.h
2020-12-22 16:05:21 +01:00

131 lines
3.4 KiB
C++

#pragma once
#include <QSql>
#include <QCoreApplication>
#include <serversdk/databasemanager.h>
#include <serversdk/databasetable.h>
#include <serversdk/databaseutils.h>
#include <serversdk/logger.h>
#include "data.h"
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 = "penta",
const QString& host = "127.0.0.1",
const QString& name = "spaceti",
const QString& user = "spaceti",
const QString& password = "27dML4aKJr");
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();
bool isOpen();
// 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;
};