class SqlWorker : public QObject{ Q_OBJECTpublic: explicit SqlWorker(QObject *parent = nullptr); ~SqlWorker (); void addDbServer (QString serverName, QHostAddress address); QList <QString> getDbServers (); QString getLastUsedDbName () { return currentDbName; } bool openDbConnection(QString serverName, QString login, QString password);private: QString currentDbName; QHash<QString, QHostAddress> dbServers; QThread * executorThread; SqlQueryExecutor * executor;signals: void stopExecutor (); void executorOpenDbConnection (QString, QString, QString, QString); void dbErrorOccured (STATE, QString, QString, bool);private slots: void slotHandleDbError (qint8 error, QString errorDescription, QString dbName); void slotDbConnected (QString);};class SqlQueryExecutor : public QObject{ Q_OBJECTpublic: explicit SqlQueryExecutor(QObject *parent = nullptr); ~SqlQueryExecutor ();private: const QString strQueryA618; QString strWhereA618; A618SqlModel * model618; void changeModel618 (const QString& query);signals: void dbConnected (QString); void dbDbErrorAppears (qint8 error, QString errorDescription, QString dbName); void return618Data (A618SqlModel & model); void finished ();public slots: void slotStartExecutorThread (); void slotStopExecutorThread (); void slotOpenDbConnection (QString adress, QString serverName, QString login, QString password); void slotGet618Messages (unsigned int turplesLimit);};class A618SqlModel : public QSqlQueryModel{ Q_OBJECTpublic: A618SqlModel(QObject *parent = 0); QVariant data(const QModelIndex &index, int role) const;};Q_DECLARE_METATYPE(A618SqlModel)
QObject::connect(modelObject, SIGNAL(return618Data(A618SqlModel &)), this, SLOT(slotSet618Messages(A618SqlModel &)));
qRegisterMetaType<A618SqlModel>();
void SqlQueryExecutor::slotGet618Data(uint turplesCount){ if (!QSqlDatabase::database().isOpen()) { emit dbDbErrorAppears(QSqlError::ConnectionError, tr("Ошибка подключения к БД."), ""); return; } QSqlQuery * query618 = new QSqlQuery (); QString sty; if (turplesCount == 0) sty = QString ("%1 %2 ORDER BY id DESC ").arg(strQueryA618, strWhereA618); else sty = QString ("%1 %2 ORDER BY id DESC %3 %4").arg(strQueryA618, strWhereA618, "LIMIT").arg(turplesCount); query618->exec(sty); if (query618->lastError().isValid()) { emit dbDbErrorAppears(query618->lastError().type(), query618->lastError().text(), ""); return; } emit set618Data(query618);}