| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208 |
- #include "sqlquerymanager.h"
- #include <QDebug>
- #include "metaclassparser.h"
- #include "model.h"
- #include "qsqldriver.h"
- CWF_BEGIN_NAMESPACE
- inline void addSpace(QString &value)
- {
- if (!value.isEmpty() && !value.endsWith(" "))
- value += " ";
- }
- inline void executeQry(const QJsonObject &json, const QString &queryText)
- {
- if (!json["success"].toBool()) {
- qDebug() << "SqlQueryManager::exec:"
- << "Error: " << json["message"].toString() << " query: " << queryText;
- }
- }
- void SqlQueryManager::reset()
- {
- queryText = "";
- query.clear();
- bindingDone = 0;
- }
- QString SqlQueryManager::createTable(const QString &tableName)
- {
- const auto &type = connection.getType();
- if (type == "QSQLITE") {
- queryText = "CREATE TABLE " + tableName + " (id INTEGER PRIMARY KEY AUTOINCREMENT);";
- } else if (type == "QPSQL") {
- queryText = "CREATE TABLE " + tableName + " (id serial PRIMARY KEY);";
- } else {
- qFatal("%s", (type + " is not supported yet!").toStdString().data());
- }
- return queryText;
- }
- void SqlQueryManager::createIndex(const QString &indexName,
- const QString &tableName,
- const QString &field,
- bool unique)
- {
- queryText = "CREATE ";
- if (unique)
- queryText += "UNIQUE ";
- queryText += "INDEX ";
- queryText += indexName + " ON " + tableName + " ( " + field + " );";
- }
- SqlQueryManager &SqlQueryManager::alterTable(const QString &tableName)
- {
- addSpace(queryText);
- queryText += "ALTER TABLE " + tableName;
- return *this;
- }
- SqlQueryManager &SqlQueryManager::addColumn(const QString &field, const QString &type)
- {
- addSpace(queryText);
- queryText += "ADD COLUMN " + field + " " + type;
- return *this;
- }
- SqlQueryManager &SqlQueryManager::select(const QString &what, const QString &tableName)
- {
- addSpace(queryText);
- queryText += "SELECT " + what + " FROM " + tableName;
- return *this;
- }
- SqlQueryManager &SqlQueryManager::count(const QString &what, const QString &tableName)
- {
- addSpace(queryText);
- queryText += "COUNT " + what + " FROM " + tableName;
- return *this;
- }
- SqlQueryManager &SqlQueryManager::insert(const QString &tableName, const QString &fields)
- {
- addSpace(queryText);
- queryText += "INSERT INTO " + tableName + " (" + fields + ") ";
- queryText += "VALUES(";
- for (int i = 0, fNum = fields.count(","); i < fNum; ++i) {
- queryText += "?,";
- }
- queryText += "?);";
- return *this;
- }
- SqlQueryManager &SqlQueryManager::remove(const QString &tableName, const QString &cond)
- {
- addSpace(queryText);
- queryText += "DELETE FROM " + tableName + " WHERE " + cond;
- return *this;
- }
- SqlQueryManager &SqlQueryManager::update(const QString &tableName, const QString &fieldValue)
- {
- addSpace(queryText);
- queryText += "UPDATE " + tableName + " SET " + fieldValue;
- return *this;
- }
- SqlQueryManager &SqlQueryManager::where(const QString &c)
- {
- addSpace(queryText);
- queryText += " WHERE " + c;
- return *this;
- }
- SqlQueryManager &SqlQueryManager::orderBy(const QString &c)
- {
- addSpace(queryText);
- queryText += "ORDER BY " + c;
- return *this;
- }
- SqlQueryManager &SqlQueryManager::leftJoin(const QString &tableName, const QString &cond)
- {
- addSpace(queryText);
- queryText += "LEFT JOIN " + tableName + " ON " + cond;
- return *this;
- }
- SqlQueryManager &SqlQueryManager::innerJoin(const QString &tableName, const QString &cond)
- {
- addSpace(queryText);
- queryText += "INNER JOIN " + tableName + " ON " + cond;
- return *this;
- }
- SqlQueryManager &SqlQueryManager::addBindValue(const QVariant &v)
- {
- query.bindValue(bindingDone, v);
- ++bindingDone;
- return *this;
- }
- bool SqlQueryManager::prepare()
- {
- if (!queryText.endsWith(";"))
- queryText += ";";
- if (!query.prepare(queryText)) {
- qDebug() << "****** SqlQueryManager::prepare *******";
- qDebug() << "Error with: " << queryText;
- qDebug() << "************************************";
- return false;
- }
- return true;
- }
- QJsonObject SqlQueryManager::exec()
- {
- const auto &json = query.exec();
- executeQry(json, queryText);
- return json;
- }
- QJsonObject SqlQueryManager::exec(const QString &sql)
- {
- const auto &json = query.exec(sql);
- executeQry(json, queryText);
- return json;
- }
- QString SqlQueryManager::textQuery(bool addEndDot) const
- {
- QString query = queryText;
- if (addEndDot) {
- if (!query.endsWith(";"))
- query += ";";
- }
- return query;
- }
- QString SqlQueryManager::prefixPropNames(Model &model)
- {
- const auto &propNames = MetaClassParser(&model, true).getAllPropertiesNames();
- QString tableName = model.getTableName(), text;
- for (const auto &propName : propNames) {
- if (text != "")
- text += ", ";
- text += propName;
- text += " AS ";
- text += tableName;
- text += "_";
- text += propName;
- }
- return text;
- }
- CWF_END_NAMESPACE
|