start QAD::SQL library
QAD::SQLQuery wrapper to minimize code of QSqlQuery usage
This commit is contained in:
1
libs/sql/CMakeLists.txt
Normal file
1
libs/sql/CMakeLists.txt
Normal file
@@ -0,0 +1 @@
|
||||
qad_library(sql "Gui;Widgets;Sql" "qad_utils")
|
||||
2
libs/sql/lang/update.bat
Normal file
2
libs/sql/lang/update.bat
Normal 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
45
libs/sql/sql_query.cpp
Normal 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
63
libs/sql/sql_query.h
Normal 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
|
||||
Reference in New Issue
Block a user