From b0619497164b97c5ef1640774720269a5c24ead8 Mon Sep 17 00:00:00 2001 From: peri4 Date: Fri, 9 Jun 2023 21:30:25 +0300 Subject: [PATCH] start QAD::SQL library QAD::SQLQuery wrapper to minimize code of QSqlQuery usage --- cmake/FindQAD.cmake | 4 ++- libs/sql/CMakeLists.txt | 1 + libs/sql/lang/update.bat | 2 ++ libs/sql/sql_query.cpp | 45 ++++++++++++++++++++++++++++ libs/sql/sql_query.h | 63 ++++++++++++++++++++++++++++++++++++++++ 5 files changed, 114 insertions(+), 1 deletion(-) create mode 100644 libs/sql/CMakeLists.txt create mode 100644 libs/sql/lang/update.bat create mode 100644 libs/sql/sql_query.cpp create mode 100644 libs/sql/sql_query.h diff --git a/cmake/FindQAD.cmake b/cmake/FindQAD.cmake index 6ba8129..512d34c 100644 --- a/cmake/FindQAD.cmake +++ b/cmake/FindQAD.cmake @@ -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!") 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) list(APPEND __libs "piqt;piqt_utils") endif() @@ -54,6 +54,7 @@ set(__module_widgets Widgets ) set(__module_application Application ) set(__module_blockview Blockview ) set(__module_graphic Graphic ) +set(__module_sql SQL ) set(__module_sql_table SQLTable ) set(__module_touch_widgets TouchWidgets ) set(__module_doc Doc ) @@ -71,6 +72,7 @@ set(__deps_widgets "QAD::Utils") set(__deps_application "QAD::Widgets") set(__deps_blockview "QAD::Widgets") set(__deps_graphic "QAD::Widgets") +set(__deps_sql "QAD::Utils") set(__deps_sql_table "QAD::Widgets") set(__deps_map "QAD::Utils;QAD::PIQt") set(__deps_piqt "QAD::Widgets;PIP") diff --git a/libs/sql/CMakeLists.txt b/libs/sql/CMakeLists.txt new file mode 100644 index 0000000..dfaa05d --- /dev/null +++ b/libs/sql/CMakeLists.txt @@ -0,0 +1 @@ +qad_library(sql "Gui;Widgets;Sql" "qad_utils") diff --git a/libs/sql/lang/update.bat b/libs/sql/lang/update.bat new file mode 100644 index 0000000..0749e95 --- /dev/null +++ b/libs/sql/lang/update.bat @@ -0,0 +1,2 @@ +lupdate ../ -ts qad_sql_ru.ts +lupdate ../ -ts qad_sql_en.ts diff --git a/libs/sql/sql_query.cpp b/libs/sql/sql_query.cpp new file mode 100644 index 0000000..2f1b2f9 --- /dev/null +++ b/libs/sql/sql_query.cpp @@ -0,0 +1,45 @@ +#include "sql_query.h" + +#include + + +QAD::SQLQuery::SQLQuery(QString query_, QVariantMap params, QSqlDatabase db): q(db) { + QMapIterator 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; +} diff --git a/libs/sql/sql_query.h b/libs/sql/sql_query.h new file mode 100644 index 0000000..415a071 --- /dev/null +++ b/libs/sql/sql_query.h @@ -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 . +*/ + +#ifndef sql_query_H +#define sql_query_H + +#include "qad_sql_export.h" + +#include +#include +#include +#include + + +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 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