46 lines
1.0 KiB
C++
46 lines
1.0 KiB
C++
#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;
|
|
}
|