sqlquerymanager.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. #include "sqlquerymanager.h"
  2. #include <QDebug>
  3. #include "metaclassparser.h"
  4. #include "model.h"
  5. #include "qsqldriver.h"
  6. CWF_BEGIN_NAMESPACE
  7. inline void addSpace(QString &value)
  8. {
  9. if (!value.isEmpty() && !value.endsWith(" "))
  10. value += " ";
  11. }
  12. inline void executeQry(const QJsonObject &json, const QString &queryText)
  13. {
  14. if (!json["success"].toBool()) {
  15. qDebug() << "SqlQueryManager::exec:"
  16. << "Error: " << json["message"].toString() << " query: " << queryText;
  17. }
  18. }
  19. void SqlQueryManager::reset()
  20. {
  21. queryText = "";
  22. query.clear();
  23. bindingDone = 0;
  24. }
  25. QString SqlQueryManager::createTable(const QString &tableName)
  26. {
  27. const auto &type = connection.getType();
  28. if (type == "QSQLITE") {
  29. queryText = "CREATE TABLE " + tableName + " (id INTEGER PRIMARY KEY AUTOINCREMENT);";
  30. } else if (type == "QPSQL") {
  31. queryText = "CREATE TABLE " + tableName + " (id serial PRIMARY KEY);";
  32. } else {
  33. qFatal("%s", (type + " is not supported yet!").toStdString().data());
  34. }
  35. return queryText;
  36. }
  37. void SqlQueryManager::createIndex(const QString &indexName,
  38. const QString &tableName,
  39. const QString &field,
  40. bool unique)
  41. {
  42. queryText = "CREATE ";
  43. if (unique)
  44. queryText += "UNIQUE ";
  45. queryText += "INDEX ";
  46. queryText += indexName + " ON " + tableName + " ( " + field + " );";
  47. }
  48. SqlQueryManager &SqlQueryManager::alterTable(const QString &tableName)
  49. {
  50. addSpace(queryText);
  51. queryText += "ALTER TABLE " + tableName;
  52. return *this;
  53. }
  54. SqlQueryManager &SqlQueryManager::addColumn(const QString &field, const QString &type)
  55. {
  56. addSpace(queryText);
  57. queryText += "ADD COLUMN " + field + " " + type;
  58. return *this;
  59. }
  60. SqlQueryManager &SqlQueryManager::select(const QString &what, const QString &tableName)
  61. {
  62. addSpace(queryText);
  63. queryText += "SELECT " + what + " FROM " + tableName;
  64. return *this;
  65. }
  66. SqlQueryManager &SqlQueryManager::count(const QString &what, const QString &tableName)
  67. {
  68. addSpace(queryText);
  69. queryText += "COUNT " + what + " FROM " + tableName;
  70. return *this;
  71. }
  72. SqlQueryManager &SqlQueryManager::insert(const QString &tableName, const QString &fields)
  73. {
  74. addSpace(queryText);
  75. queryText += "INSERT INTO " + tableName + " (" + fields + ") ";
  76. queryText += "VALUES(";
  77. for (int i = 0, fNum = fields.count(","); i < fNum; ++i) {
  78. queryText += "?,";
  79. }
  80. queryText += "?);";
  81. return *this;
  82. }
  83. SqlQueryManager &SqlQueryManager::remove(const QString &tableName, const QString &cond)
  84. {
  85. addSpace(queryText);
  86. queryText += "DELETE FROM " + tableName + " WHERE " + cond;
  87. return *this;
  88. }
  89. SqlQueryManager &SqlQueryManager::update(const QString &tableName, const QString &fieldValue)
  90. {
  91. addSpace(queryText);
  92. queryText += "UPDATE " + tableName + " SET " + fieldValue;
  93. return *this;
  94. }
  95. SqlQueryManager &SqlQueryManager::where(const QString &c)
  96. {
  97. addSpace(queryText);
  98. queryText += " WHERE " + c;
  99. return *this;
  100. }
  101. SqlQueryManager &SqlQueryManager::orderBy(const QString &c)
  102. {
  103. addSpace(queryText);
  104. queryText += "ORDER BY " + c;
  105. return *this;
  106. }
  107. SqlQueryManager &SqlQueryManager::leftJoin(const QString &tableName, const QString &cond)
  108. {
  109. addSpace(queryText);
  110. queryText += "LEFT JOIN " + tableName + " ON " + cond;
  111. return *this;
  112. }
  113. SqlQueryManager &SqlQueryManager::innerJoin(const QString &tableName, const QString &cond)
  114. {
  115. addSpace(queryText);
  116. queryText += "INNER JOIN " + tableName + " ON " + cond;
  117. return *this;
  118. }
  119. SqlQueryManager &SqlQueryManager::addBindValue(const QVariant &v)
  120. {
  121. query.bindValue(bindingDone, v);
  122. ++bindingDone;
  123. return *this;
  124. }
  125. bool SqlQueryManager::prepare()
  126. {
  127. if (!queryText.endsWith(";"))
  128. queryText += ";";
  129. if (!query.prepare(queryText)) {
  130. qDebug() << "****** SqlQueryManager::prepare *******";
  131. qDebug() << "Error with: " << queryText;
  132. qDebug() << "************************************";
  133. return false;
  134. }
  135. return true;
  136. }
  137. QJsonObject SqlQueryManager::exec()
  138. {
  139. const auto &json = query.exec();
  140. executeQry(json, queryText);
  141. return json;
  142. }
  143. QJsonObject SqlQueryManager::exec(const QString &sql)
  144. {
  145. const auto &json = query.exec(sql);
  146. executeQry(json, queryText);
  147. return json;
  148. }
  149. QString SqlQueryManager::textQuery(bool addEndDot) const
  150. {
  151. QString query = queryText;
  152. if (addEndDot) {
  153. if (!query.endsWith(";"))
  154. query += ";";
  155. }
  156. return query;
  157. }
  158. QString SqlQueryManager::prefixPropNames(Model &model)
  159. {
  160. const auto &propNames = MetaClassParser(&model, true).getAllPropertiesNames();
  161. QString tableName = model.getTableName(), text;
  162. for (const auto &propName : propNames) {
  163. if (text != "")
  164. text += ", ";
  165. text += propName;
  166. text += " AS ";
  167. text += tableName;
  168. text += "_";
  169. text += propName;
  170. }
  171. return text;
  172. }
  173. CWF_END_NAMESPACE