fortunetest.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. #include "fortunetest.h"
  2. #include <Cutelyst/Plugins/Utils/Sql>
  3. #include <Cutelyst/View>
  4. #include <QSqlQuery>
  5. FortuneTest::FortuneTest(QObject *parent) : Controller(parent)
  6. {
  7. }
  8. void FortuneTest::fortunes_raw_postgres(Context *c)
  9. {
  10. QSqlQuery query = CPreparedSqlQueryThreadForDB(
  11. QLatin1String("SELECT id, message FROM fortune"),
  12. QStringLiteral("postgres"));
  13. auto fortunes = processQuery(c, query);
  14. renderRaw(c, fortunes);
  15. }
  16. void FortuneTest::fortunes_raw_mysql(Context *c)
  17. {
  18. QSqlQuery query = CPreparedSqlQueryThreadForDB(
  19. QLatin1String("SELECT id, message FROM fortune"),
  20. QStringLiteral("mysql"));
  21. auto fortunes = processQuery(c, query);
  22. renderRaw(c, fortunes);
  23. }
  24. void FortuneTest::fortunes_grantlee_postgres(Context *c)
  25. {
  26. QSqlQuery query = CPreparedSqlQueryThreadForDB(
  27. QLatin1String("SELECT id, message FROM fortune"),
  28. QStringLiteral("postgres"));
  29. if (query.exec()) {
  30. QVariantList fortunes = Sql::queryToList(query);
  31. fortunes.append(QVariant::fromValue(QVariantList{
  32. {0, QStringLiteral("Additional fortune added at request time.")},
  33. }));
  34. std::sort(fortunes.begin(), fortunes.end(), [] (const QVariant &a1, const QVariant &a2) {
  35. return a1.toList()[1].toString() < a2.toList()[1].toString();
  36. });
  37. c->setStash(QStringLiteral("template"), QStringLiteral("fortunes.html"));
  38. c->setStash(QStringLiteral("fortunes"), fortunes);
  39. static thread_local View *view = c->view();
  40. view->execute(c);
  41. c->response()->setContentType(QStringLiteral("text/html; charset=UTF-8"));
  42. }
  43. }
  44. void FortuneTest::fortunes_grantlee_mysql(Context *c)
  45. {
  46. QSqlQuery query = CPreparedSqlQueryThreadForDB(
  47. QLatin1String("SELECT id, message FROM fortune"),
  48. QStringLiteral("mysql"));
  49. if (query.exec()) {
  50. QVariantList fortunes = Sql::queryToList(query);
  51. fortunes.append(QVariant::fromValue(QVariantList{
  52. {0, QStringLiteral("Additional fortune added at request time.")},
  53. }));
  54. std::sort(fortunes.begin(), fortunes.end(), [] (const QVariant &a1, const QVariant &a2) {
  55. return a1.toList()[1].toString() < a2.toList()[1].toString();
  56. });
  57. c->setStash(QStringLiteral("template"), QStringLiteral("fortunes.html"));
  58. c->setStash(QStringLiteral("fortunes"), fortunes);
  59. static thread_local View *view = c->view();
  60. view->execute(c);
  61. c->response()->setContentType(QStringLiteral("text/html; charset=UTF-8"));
  62. }
  63. }
  64. FortuneList FortuneTest::processQuery(Context *c, QSqlQuery &query)
  65. {
  66. FortuneList fortunes;
  67. if (Q_UNLIKELY(!query.exec())) {
  68. c->res()->setStatus(Response::InternalServerError);
  69. return fortunes;
  70. }
  71. while (query.next()) {
  72. fortunes.push_back({query.value(0).toInt(), query.value(1).toString()});
  73. }
  74. fortunes.push_back({0, QStringLiteral("Additional fortune added at request time.")});
  75. std::sort(fortunes.begin(), fortunes.end(), [] (const Fortune &a1, const Fortune &a2) {
  76. return a1.second < a2.second;
  77. });
  78. return fortunes;
  79. }
  80. void FortuneTest::renderRaw(Context *c, const FortuneList &fortunes)
  81. {
  82. QString out;
  83. out.append(QStringLiteral("<!DOCTYPE html>"
  84. "<html>"
  85. "<head><title>Fortunes</title></head>"
  86. "<body>"
  87. "<table>"
  88. "<tr><th>id</th><th>message</th></tr>"));
  89. out.reserve(4096);
  90. for (const Fortune &fortune : fortunes) {
  91. out.append(QStringLiteral("<tr><td>"))
  92. .append(QString::number(fortune.first))
  93. .append(QStringLiteral("</td><td>"))
  94. .append(fortune.second.toHtmlEscaped())
  95. .append(QStringLiteral("</td></tr>"));
  96. }
  97. out.append(QStringLiteral("</table></body></html>"));
  98. auto response = c->response();
  99. response->setBody(out);
  100. response->setContentType(QStringLiteral("text/html; charset=UTF-8"));
  101. }