2019-10-22 17:59:56 +03:00
|
|
|
#ifndef SQLDBWRITER_H
|
|
|
|
#define SQLDBWRITER_H
|
|
|
|
|
|
|
|
#include <QObject>
|
|
|
|
#include <QSqlDatabase>
|
|
|
|
#include <QDir>
|
2019-10-22 21:51:21 +03:00
|
|
|
#include <QSqlQuery>
|
2019-10-22 17:59:56 +03:00
|
|
|
#include "clientprotocol_global.h"
|
2019-10-22 21:51:21 +03:00
|
|
|
#include "config.h"
|
2019-10-26 20:53:47 +03:00
|
|
|
#include "dbtablebase.h"
|
2019-10-27 12:08:16 +03:00
|
|
|
#include "iobjectprovider.h"
|
2019-10-25 18:18:28 +03:00
|
|
|
#include <QVariant>
|
2019-10-22 17:59:56 +03:00
|
|
|
|
|
|
|
class QSqlQuery;
|
|
|
|
class QSqlDatabase;
|
|
|
|
class QSqlQuery;
|
|
|
|
class PlayerDBData;
|
|
|
|
|
2019-10-22 21:51:21 +03:00
|
|
|
namespace ClientProtocol {
|
|
|
|
|
2019-10-26 20:53:47 +03:00
|
|
|
class DBObject;
|
2019-10-25 18:18:28 +03:00
|
|
|
|
2019-10-22 21:51:21 +03:00
|
|
|
/**
|
|
|
|
* @brief The SqlDBWriter class
|
|
|
|
*/
|
2019-10-27 12:08:16 +03:00
|
|
|
class CLIENTPROTOCOLSHARED_EXPORT SqlDBWriter : public iObjectProvider
|
2019-10-22 17:59:56 +03:00
|
|
|
{
|
|
|
|
private:
|
2019-10-26 20:53:47 +03:00
|
|
|
|
|
|
|
QString tablesListMySql();
|
|
|
|
QString tablesListSqlite();
|
|
|
|
|
|
|
|
QString describeQueryMySql(const QString& tabme);
|
|
|
|
QString describeQuerySqlite(const QString& tabme);
|
|
|
|
|
|
|
|
QString getTablesQuery();
|
|
|
|
QString describeQuery(const QString& tabme);
|
|
|
|
|
2019-10-22 17:59:56 +03:00
|
|
|
bool exec(QSqlQuery *sq, const QString &sqlFile);
|
|
|
|
|
|
|
|
bool initSuccessful = false;
|
2019-10-26 20:53:47 +03:00
|
|
|
QVariantMap _config;
|
2019-10-22 17:59:56 +03:00
|
|
|
|
|
|
|
protected:
|
|
|
|
|
2019-10-22 21:51:21 +03:00
|
|
|
/**
|
|
|
|
* @brief enableFK - enavle forign ke for sqlite db
|
|
|
|
* @return return true if all good
|
|
|
|
*/
|
|
|
|
bool enableFK();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief enableFK - disavle forign ke for sqlite db
|
|
|
|
* @return return true if all good
|
|
|
|
*/
|
|
|
|
bool disableFK();
|
|
|
|
|
|
|
|
|
2019-11-03 16:03:00 +03:00
|
|
|
|
2019-10-22 21:51:21 +03:00
|
|
|
/**
|
|
|
|
* @brief getInitPararm
|
|
|
|
* @param initFile
|
|
|
|
* @return
|
|
|
|
*
|
|
|
|
* Params :
|
|
|
|
* DBDriver - driver of db see https://doc.qt.io/qt-5/sql-driver.html
|
|
|
|
* DBFilePath - path to file of data base (sqlite only)
|
|
|
|
* DBInitFile - sql file with init state database
|
|
|
|
* DBPass - pass of remote db
|
|
|
|
* DBLogin - login of remote db
|
|
|
|
* DBHost - host addres of reote db
|
|
|
|
* DBPort - port of reote db
|
|
|
|
|
|
|
|
*/
|
|
|
|
virtual QVariantMap getInitParams(const QString& initFile) const;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief defaultInitPararm
|
|
|
|
* @param initFile
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
virtual QVariantMap defaultInitPararm() const;
|
|
|
|
|
2019-10-25 18:18:28 +03:00
|
|
|
/**
|
|
|
|
* @brief getType
|
|
|
|
* @param str
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
virtual QVariant::Type getType(const QString& str);
|
|
|
|
|
2019-10-22 21:51:21 +03:00
|
|
|
QSqlQuery query;
|
|
|
|
QSqlDatabase db;
|
2019-10-26 20:53:47 +03:00
|
|
|
QHash<QString, DbTableBase> _dbStruct;
|
2019-10-22 17:59:56 +03:00
|
|
|
|
2019-11-03 16:03:00 +03:00
|
|
|
/**
|
2019-11-04 10:29:54 +03:00
|
|
|
* @brief generateHeaderOfQuery - generate list of columns header for update
|
|
|
|
* @param retQuery return value
|
2019-11-03 16:03:00 +03:00
|
|
|
* @return true if all good
|
|
|
|
*/
|
|
|
|
virtual bool generateHeaderOfQuery(QString& retQuery,
|
|
|
|
const DbTableBase& tableStruct) const;
|
|
|
|
|
|
|
|
/**
|
2019-11-04 10:29:54 +03:00
|
|
|
* @brief generateSourceOfQuery - enerate list of columns header for update
|
|
|
|
* @param retQuery return value
|
|
|
|
* @param retBindValue list of bind value, after invoce of this method need invoce
|
2019-11-03 16:03:00 +03:00
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
virtual bool generateSourceOfQuery(QString& retQuery,
|
|
|
|
QList<QPair<QString, QVariant> > &retBindValue,
|
|
|
|
const DbTableBase& tableStruct,
|
|
|
|
const QVariantMap &map) const;
|
|
|
|
|
|
|
|
/**
|
2019-11-04 10:29:54 +03:00
|
|
|
* @brief getBaseQueryString private implementation of getQueryMethods
|
2019-11-03 16:03:00 +03:00
|
|
|
* @param queryString
|
|
|
|
* @param query
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
virtual bool getBaseQueryString(QString queryString,
|
|
|
|
QSqlQuery *query,
|
|
|
|
const DbTableBase &tableStruct,
|
|
|
|
const QVariantMap &objMap = {}) const;
|
|
|
|
|
|
|
|
// 0 - table name
|
|
|
|
// 1 - headers of update values
|
|
|
|
// 2 - update values
|
|
|
|
virtual bool saveQuery(const QWeakPointer<DBObject> &ptr) const;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief selectQuery generate select query to database from parameters
|
|
|
|
* @param returnList - return values
|
|
|
|
* @param table - table name of query
|
|
|
|
* @param key - compare key (column) for select is default it is id
|
|
|
|
* @param val - compare value
|
|
|
|
* @return true if all goodelse false
|
|
|
|
*/
|
|
|
|
virtual bool selectQuery(QList<QSharedPointer<DBObject>>& returnList,
|
|
|
|
const QString& table,
|
|
|
|
const QString &key,
|
|
|
|
const QVariant &val);
|
|
|
|
|
|
|
|
virtual bool deleteQuery(const QString &table, int id) const;
|
|
|
|
|
|
|
|
virtual bool checkTableStruct(const QWeakPointer<DBObject> &ptr);
|
|
|
|
|
2019-10-22 17:59:56 +03:00
|
|
|
|
|
|
|
public:
|
|
|
|
SqlDBWriter();
|
|
|
|
|
2019-10-22 21:51:21 +03:00
|
|
|
/**
|
|
|
|
* @brief initDb
|
|
|
|
* @param path
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
virtual bool initDb(const QString &initDbParams = DEFAULT_DB_PATH);
|
|
|
|
|
2019-10-31 18:15:20 +03:00
|
|
|
/**
|
|
|
|
* @brief initDb
|
|
|
|
* @param path
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
virtual bool initDb(const QVariantMap ¶ms);
|
|
|
|
|
2019-10-22 21:51:21 +03:00
|
|
|
/**
|
|
|
|
* @brief isValid
|
|
|
|
* @return
|
|
|
|
*/
|
2019-10-22 17:59:56 +03:00
|
|
|
virtual bool isValid() const;
|
|
|
|
|
2019-10-26 20:53:47 +03:00
|
|
|
/**
|
|
|
|
* @brief getObject
|
|
|
|
* @return
|
|
|
|
*/
|
2019-11-03 16:03:00 +03:00
|
|
|
bool getObject(const QString &table, int id, QSharedPointer<DBObject> *result) override;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief getObjects
|
|
|
|
* @param table
|
|
|
|
* @param key - the key by which the value will be searched
|
|
|
|
* @param val - value for compare
|
|
|
|
* @param result list of db objects (ret value)
|
|
|
|
* @return true if all good
|
|
|
|
*/
|
|
|
|
bool getObjects(const QString &table, const QString &key,
|
|
|
|
QVariant val, QList<QSharedPointer<DBObject> > &result) override;
|
2019-10-26 20:53:47 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief saveObject
|
|
|
|
* @return
|
|
|
|
*/
|
2019-10-28 21:44:41 +03:00
|
|
|
bool saveObject(QWeakPointer<DBObject> saveObject) override;
|
2019-10-26 20:53:47 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief deleteObject
|
|
|
|
* @return
|
|
|
|
*/
|
2019-10-27 12:08:16 +03:00
|
|
|
bool deleteObject(const QString &table, int id) override;
|
2019-10-26 20:53:47 +03:00
|
|
|
|
2019-11-03 16:03:00 +03:00
|
|
|
|
|
|
|
|
2019-10-27 12:08:16 +03:00
|
|
|
virtual ~SqlDBWriter() override;
|
2019-10-22 17:59:56 +03:00
|
|
|
|
2019-11-03 16:03:00 +03:00
|
|
|
|
|
|
|
QHash<QString, QSharedPointer<DbTableBase> > getDbStruct() const;
|
|
|
|
void setDbStruct(const QHash<QString, QSharedPointer<DbTableBase> > &dbStruct);
|
2019-10-22 17:59:56 +03:00
|
|
|
};
|
|
|
|
|
2019-10-22 21:51:21 +03:00
|
|
|
}
|
2019-10-22 17:59:56 +03:00
|
|
|
#endif // SQLDBWRITER_H
|