forked from ondra/colnod-connector
118 lines
3.3 KiB
C++
118 lines
3.3 KiB
C++
#pragma once
|
|
|
|
#include <QSql>
|
|
#include <QSqlDatabase>
|
|
#include <QSqlQuery>
|
|
#include <QSqlError>
|
|
|
|
#include <QVariant>
|
|
#include <QDebug>
|
|
#include "subject.h"
|
|
#include "token.h"
|
|
#include <QDateTime>
|
|
#include "data.h"
|
|
#include "serversdk/databasemanager.h"
|
|
#include "serversdk/databasetable.h"
|
|
|
|
|
|
// TODO: move to token class
|
|
class TokenTable : public serversdk::DatabaseTable {
|
|
Q_OBJECT
|
|
TABLE_PROPERTY(QUuid, id, setId, QUuid())
|
|
TABLE_PROPERTY(int, authErorrs, setAuthErrors, 255)
|
|
TABLE_PROPERTY(QString, description, setDescription, "")
|
|
TABLE_PROPERTY(bool, enabled, setEnabled, true)
|
|
TABLE_PROPERTY(QDateTime, lastUpdate, setLastUpdate, QDateTime())
|
|
TABLE_PROPERTY(QString, name, setName, "")
|
|
TABLE_PROPERTY(bool, noValidity, setNoValidity, true)
|
|
TABLE_PROPERTY(int, timeToLive, setTimeToLive, 255)
|
|
TABLE_PROPERTY(int, tokenBits, setTokenBits, 32)
|
|
TABLE_PROPERTY(QString, tokenType, setTokenType, "Token")
|
|
TABLE_PROPERTY(QUuid, subjectId, setSubjectId, QUuid())
|
|
TABLE_PROPERTY(QDateTime, lastModified, setLastModified, QDateTime())
|
|
TABLE_PROPERTY(bool, deleted, setDeleted, false)
|
|
};
|
|
|
|
class DatabaseAPI : public QObject
|
|
{
|
|
Q_OBJECT
|
|
public:
|
|
DatabaseAPI(serversdk::DatabaseManager *dbMan);
|
|
|
|
bool open();
|
|
|
|
void downloadData();
|
|
void processData(std::shared_ptr<Data> data);
|
|
qint64 getLastSync();
|
|
void setLastSync(qint64 timestamp);
|
|
void setLastUpdate(QHash<QUuid, std::shared_ptr<Token> > tokens);
|
|
qint64 getNow();
|
|
|
|
void initNewTokens();
|
|
|
|
// process data
|
|
void uploadTokens(QHash<QUuid, std::shared_ptr<Token>> tokens);
|
|
void uploadSubjects(QHash<QUuid, std::shared_ptr<Subject>> subjects);
|
|
void deleteTokens(QList<QString> ids);
|
|
void deleteSubjects(QList<QString> ids);
|
|
|
|
private:
|
|
QSqlQuery getUpdatedTokens();
|
|
std::shared_ptr<Data> composeData(QSqlQuery updatedQuery, QSqlQuery deletedQuery);
|
|
qint16 getNumOfTokensOfSubject(const QString &id);
|
|
QString getSubjectNameByTokenId(const QString &tokenId);
|
|
|
|
signals:
|
|
void downloadDataFinished(std::shared_ptr<Data> = std::shared_ptr<Data>());
|
|
|
|
|
|
|
|
private:
|
|
serversdk::DatabaseManager *mDbMan = nullptr;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// old
|
|
// DatabaseAPI(const QString& host, const QString& dbName, const QString& user, const QString& password, const bool& dryMode = false);
|
|
|
|
// int saveSubject(const std::shared_ptr<Subject>& subject, const QString& table);
|
|
// int saveToken(const std::shared_ptr<Token>& token, const QString& table);
|
|
// void saveSubjectAttributes(const std::shared_ptr<Subject>& subject, const QString& table);
|
|
// void setDeleted(const QString& id, const QString& table, quint64 time);
|
|
|
|
// void dropTables();
|
|
// void createTables();
|
|
|
|
// void updateLastSync(quint64 lastSync);
|
|
|
|
// QSqlQuery getToken(const QString& id);
|
|
|
|
// void setLastUpdate(const QString& id, quint64 lastUpdate);
|
|
|
|
// void log(const QString &msg);
|
|
// QString getSubjectNameById(const QString &id);
|
|
// bool checkDeletedToken(const QString &id);
|
|
|
|
|
|
|
|
// bool isOpen();
|
|
|
|
|
|
//private:
|
|
// bool checkIfExists(const QString& id, const QString& table);
|
|
// bool executeQuery(QSqlQuery &query, const QString &statement = QString());
|
|
// QString getLastQuery(const QSqlQuery &query) const;
|
|
|
|
//signals:
|
|
// void error();
|
|
|
|
//private:
|
|
// QSqlDatabase db;
|
|
// bool dryMode = false;
|
|
// bool opened = false;
|
|
};
|