EasyQtSql_InsertQuery.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. #ifndef EASYQTSQL_INSERTQUERY_H
  2. #define EASYQTSQL_INSERTQUERY_H
  3. /*
  4. * The MIT License (MIT)
  5. * Copyright 2018 Alexey Kramin
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining
  8. * a copy of this software and associated documentation files (the
  9. * "Software"), to deal in the Software without restriction, including
  10. * without limitation the rights to use, copy, modify, merge, publish,
  11. * distribute, sublicense, and/or sell copies of the Software, and to
  12. * permit persons to whom the Software is furnished to do so, subject to
  13. * the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be
  16. * included in all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  19. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  20. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  21. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  22. * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  23. * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  24. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  25. *
  26. */
  27. #ifndef EASY_QT_SQL_MAIN
  28. #include <QtSql>
  29. #include "EasyQtSql_NonQueryResult.h"
  30. #endif
  31. /*!
  32. \brief QSqlQuery wrapper for <em>INSERT INTO table</em> query execution.
  33. */
  34. class InsertQuery
  35. {
  36. public:
  37. InsertQuery(const QString &table, const QSqlDatabase &db)
  38. : m_table(table)
  39. , q(db)
  40. { }
  41. /*!
  42. \brief Adds list of insert-values to INSERT INTO table(...) VALUES ... query
  43. The method supports variable count of QVariant parameters.
  44. Parameters are bound with <em>QSqlQuery::addBindValue</em>.
  45. \code
  46. NonQueryResult res = t.insertInto("table (a, b, c, d)")
  47. .values(1, 2, 3, "a")
  48. .values(4, 5, 6, "b")
  49. .values(7, 8, 9, "c")
  50. .exec();
  51. \endcode
  52. */
  53. InsertQuery &values(const QVariant& t)
  54. {
  55. m_args.append(t);
  56. if (m_insertArray.isEmpty())
  57. {
  58. m_insertArray.resize(m_args.count());
  59. }
  60. else
  61. {
  62. Q_ASSERT(m_insertArray.size() == m_args.size());
  63. }
  64. for (int i = 0; i < m_args.count(); ++i)
  65. {
  66. m_insertArray[i].append(m_args.at(i));
  67. }
  68. m_args.clear();
  69. return *this;
  70. }
  71. template <typename... Rest> InsertQuery &values(const QVariant &first, const Rest&... rest)
  72. {
  73. m_args.append(first);
  74. return values(rest...);
  75. }
  76. /*!
  77. \brief Executes prepared InsertQuery with insert values list
  78. */
  79. NonQueryResult exec()
  80. {
  81. QString sql = QLatin1String("INSERT INTO ");
  82. sql.append(m_table);
  83. sql.append(QLatin1String(" VALUES ("));
  84. QStringList lst;
  85. for (int i = 0; i < m_insertArray.count(); ++i)
  86. {
  87. lst.append(QLatin1String("?"));
  88. }
  89. sql.append(lst.join(QLatin1String(",")));
  90. sql.append(QLatin1String(")"));
  91. q.prepare(sql);
  92. bool res = false;
  93. if (m_insertArray.count() > 0 && m_insertArray[0].count() > 1)
  94. {
  95. for (int i = 0; i < m_insertArray.count(); ++i)
  96. {
  97. q.addBindValue(m_insertArray.at(i));
  98. }
  99. res = q.execBatch();
  100. }
  101. else
  102. {
  103. for (int i = 0; i < m_insertArray.count(); ++i)
  104. {
  105. q.addBindValue(m_insertArray.at(i).first());
  106. }
  107. res = q.exec();
  108. }
  109. m_args.clear();
  110. m_insertArray.clear();
  111. #ifdef DB_EXCEPTIONS_ENABLED
  112. if (!res)
  113. throw DBException(q);
  114. #endif
  115. return NonQueryResult(q);
  116. }
  117. private:
  118. QString m_table;
  119. QSqlQuery q;
  120. QVariantList m_args;
  121. QVector<QVariantList> m_insertArray;
  122. };
  123. #endif // EASYQTSQL_INSERTQUERY_H