start QAD::SQL library

QAD::SQLQuery wrapper to minimize code of QSqlQuery usage
This commit is contained in:
2023-06-09 21:30:25 +03:00
parent 7ce2b31ec9
commit b061949716
5 changed files with 114 additions and 1 deletions

View File

@@ -44,7 +44,7 @@ if(QAD_FIND_VERSION VERSION_GREATER QAD_VERSION)
message(FATAL_ERROR "QAD version ${QAD_VERSION} is available, but ${QAD_FIND_VERSION} requested!") message(FATAL_ERROR "QAD version ${QAD_VERSION} is available, but ${QAD_FIND_VERSION} requested!")
endif() endif()
set(__libs "utils;widgets;application;blockview;graphic;sql_table;touch_widgets;doc;map") set(__libs "utils;widgets;application;blockview;graphic;sql;sql_table;touch_widgets;doc;map")
if (PIP_FOUND OR BUILDING_PIP) if (PIP_FOUND OR BUILDING_PIP)
list(APPEND __libs "piqt;piqt_utils") list(APPEND __libs "piqt;piqt_utils")
endif() endif()
@@ -54,6 +54,7 @@ set(__module_widgets Widgets )
set(__module_application Application ) set(__module_application Application )
set(__module_blockview Blockview ) set(__module_blockview Blockview )
set(__module_graphic Graphic ) set(__module_graphic Graphic )
set(__module_sql SQL )
set(__module_sql_table SQLTable ) set(__module_sql_table SQLTable )
set(__module_touch_widgets TouchWidgets ) set(__module_touch_widgets TouchWidgets )
set(__module_doc Doc ) set(__module_doc Doc )
@@ -71,6 +72,7 @@ set(__deps_widgets "QAD::Utils")
set(__deps_application "QAD::Widgets") set(__deps_application "QAD::Widgets")
set(__deps_blockview "QAD::Widgets") set(__deps_blockview "QAD::Widgets")
set(__deps_graphic "QAD::Widgets") set(__deps_graphic "QAD::Widgets")
set(__deps_sql "QAD::Utils")
set(__deps_sql_table "QAD::Widgets") set(__deps_sql_table "QAD::Widgets")
set(__deps_map "QAD::Utils;QAD::PIQt") set(__deps_map "QAD::Utils;QAD::PIQt")
set(__deps_piqt "QAD::Widgets;PIP") set(__deps_piqt "QAD::Widgets;PIP")

1
libs/sql/CMakeLists.txt Normal file
View File

@@ -0,0 +1 @@
qad_library(sql "Gui;Widgets;Sql" "qad_utils")

2
libs/sql/lang/update.bat Normal file
View File

@@ -0,0 +1,2 @@
lupdate ../ -ts qad_sql_ru.ts
lupdate ../ -ts qad_sql_en.ts

45
libs/sql/sql_query.cpp Normal file
View File

@@ -0,0 +1,45 @@
#include "sql_query.h"
#include <QDebug>
QAD::SQLQuery::SQLQuery(QString query_, QVariantMap params, QSqlDatabase db): q(db) {
QMapIterator<QString, QVariant> it(params);
while (it.hasNext()) {
it.next();
query_.replace("${" + it.key() + "}", it.value().toString());
}
q.prepare(query_);
it.toFront();
while (it.hasNext()) {
it.next();
q.bindValue(":" + it.key(), it.value());
}
}
QAD::SQLQuery::~SQLQuery() {}
QAD::SQLQuery::Result QAD::SQLQuery::exec(QString query_, QVariantMap params, QSqlDatabase db) {
QAD::SQLQuery::Result ret;
QAD::SQLQuery q(query_, params, db);
if (q.q.exec()) {
ret.ok = true;
ret.insertId = q.q.lastInsertId();
return ret;
}
if (Singleton::instance()->fail_handler) Singleton::instance()->fail_handler(q.q.lastQuery(), params, q.q.lastError());
return ret;
}
void QAD::SQLQuery::setFailHandler(QAD::SQLQuery::HandlerFail h) {
Singleton::instance()->fail_handler = h;
}
QAD::SQLQuery::Singleton * QAD::SQLQuery::Singleton::instance() {
static QAD::SQLQuery::Singleton ret;
return &ret;
}

63
libs/sql/sql_query.h Normal file
View File

@@ -0,0 +1,63 @@
/*
QAD - Qt ADvanced
Ivan Pelipenko peri4ko@yandex.ru
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef sql_query_H
#define sql_query_H
#include "qad_sql_export.h"
#include <QMap>
#include <QSqlError>
#include <QSqlQuery>
#include <QVariant>
namespace QAD {
class QAD_SQL_EXPORT SQLQuery {
public:
SQLQuery(QString query_ = QString(), QVariantMap params = QVariantMap(), QSqlDatabase db = QSqlDatabase());
~SQLQuery();
struct Result {
bool ok = false;
QVariant insertId = -1;
};
typedef std::function<void(QString, QVariantMap, QSqlError)> HandlerFail;
QSqlQuery & query() { return q; }
static Result exec(QString query_, QVariantMap params = QVariantMap(), QSqlDatabase db = QSqlDatabase());
static void setFailHandler(HandlerFail h);
protected:
QSqlQuery q;
private:
struct Singleton {
static Singleton * instance();
HandlerFail fail_handler;
};
};
} // namespace QAD
#endif