|
@@ -2,14 +2,9 @@
|
|
|
|
|
|
#include <Cutelyst/Plugins/Utils/Sql>
|
|
#include <Cutelyst/Plugins/Utils/Sql>
|
|
|
|
|
|
-#include <QStringBuilder>
|
|
|
|
|
|
+#include <QSqlQuery>
|
|
|
|
|
|
-#include <QtSql/QSqlQuery>
|
|
|
|
-
|
|
|
|
-#include <QtCore/QThread>
|
|
|
|
-#include <QtCore/QJsonDocument>
|
|
|
|
-#include <QtCore/QJsonObject>
|
|
|
|
-#include <QtCore/QJsonArray>
|
|
|
|
|
|
+#include <QThread>
|
|
|
|
|
|
FortuneTest::FortuneTest(QObject *parent) : Controller(parent)
|
|
FortuneTest::FortuneTest(QObject *parent) : Controller(parent)
|
|
{
|
|
{
|
|
@@ -18,30 +13,20 @@ FortuneTest::FortuneTest(QObject *parent) : Controller(parent)
|
|
|
|
|
|
void FortuneTest::fortunes_raw_postgres(Context *c)
|
|
void FortuneTest::fortunes_raw_postgres(Context *c)
|
|
{
|
|
{
|
|
- QSqlQuery query = postgresQuery();
|
|
|
|
|
|
+ QSqlQuery query = CPreparedSqlQueryForDatabase(
|
|
|
|
+ QLatin1String("SELECT id, message FROM fortune"),
|
|
|
|
+ QSqlDatabase::database(QLatin1String("postgres-") + QThread::currentThread()->objectName()));
|
|
auto fortunes = processQuery(c, query);
|
|
auto fortunes = processQuery(c, query);
|
|
renderRaw(c, fortunes);
|
|
renderRaw(c, fortunes);
|
|
}
|
|
}
|
|
|
|
|
|
void FortuneTest::fortunes_raw_mysql(Context *c)
|
|
void FortuneTest::fortunes_raw_mysql(Context *c)
|
|
{
|
|
{
|
|
- QSqlQuery query = mysqlQuery();
|
|
|
|
- auto fortunes = processQuery(c, query);
|
|
|
|
- renderRaw(c, fortunes);
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-QSqlQuery FortuneTest::postgresQuery()
|
|
|
|
-{
|
|
|
|
- return CPreparedSqlQueryForDatabase(
|
|
|
|
- QLatin1String("SELECT id, message FROM fortune"),
|
|
|
|
- QSqlDatabase::database(QLatin1String("postgres-") + QThread::currentThread()->objectName()));
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-QSqlQuery FortuneTest::mysqlQuery()
|
|
|
|
-{
|
|
|
|
- return CPreparedSqlQueryForDatabase(
|
|
|
|
|
|
+ QSqlQuery query = CPreparedSqlQueryForDatabase(
|
|
QLatin1String("SELECT id, message FROM fortune"),
|
|
QLatin1String("SELECT id, message FROM fortune"),
|
|
QSqlDatabase::database(QLatin1String("mysql-") + QThread::currentThread()->objectName()));
|
|
QSqlDatabase::database(QLatin1String("mysql-") + QThread::currentThread()->objectName()));
|
|
|
|
+ auto fortunes = processQuery(c, query);
|
|
|
|
+ renderRaw(c, fortunes);
|
|
}
|
|
}
|
|
|
|
|
|
static bool caseSensitiveLessThan(const Fortune &a1, const Fortune &a2)
|
|
static bool caseSensitiveLessThan(const Fortune &a1, const Fortune &a2)
|
|
@@ -59,14 +44,12 @@ FortuneList FortuneTest::processQuery(Context *c, QSqlQuery &query)
|
|
}
|
|
}
|
|
|
|
|
|
while (query.next()) {
|
|
while (query.next()) {
|
|
- fortunes.append(qMakePair(query.value(0).toInt(), query.value(1).toString()));
|
|
|
|
|
|
+ fortunes.push_back({query.value(0).toInt(), query.value(1).toString()});
|
|
}
|
|
}
|
|
- fortunes.append(qMakePair(0, QStringLiteral("Additional fortune added at request time.")));
|
|
|
|
|
|
+ fortunes.push_back({0, QStringLiteral("Additional fortune added at request time.")});
|
|
|
|
|
|
qSort(fortunes.begin(), fortunes.end(), caseSensitiveLessThan);
|
|
qSort(fortunes.begin(), fortunes.end(), caseSensitiveLessThan);
|
|
|
|
|
|
- c->response()->setContentType(QStringLiteral("text/html; charset=UTF-8"));
|
|
|
|
-
|
|
|
|
return fortunes;
|
|
return fortunes;
|
|
}
|
|
}
|
|
|
|
|
|
@@ -79,12 +62,19 @@ void FortuneTest::renderRaw(Context *c, const FortuneList &fortunes)
|
|
"<body>"
|
|
"<body>"
|
|
"<table>"
|
|
"<table>"
|
|
"<tr><th>id</th><th>message</th></tr>"));
|
|
"<tr><th>id</th><th>message</th></tr>"));
|
|
-
|
|
|
|
- Q_FOREACH (const Fortune &fortune, fortunes) {
|
|
|
|
- out.append(QLatin1String("<tr><td>") % QString::number(fortune.first) % QLatin1String("</td><td>") % fortune.second.toHtmlEscaped() % QLatin1String("</td></tr>"));
|
|
|
|
|
|
+ out.reserve(4096);
|
|
|
|
+
|
|
|
|
+ for (const Fortune &fortune : fortunes) {
|
|
|
|
+ out.append(QStringLiteral("<tr><td>"))
|
|
|
|
+ .append(QString::number(fortune.first))
|
|
|
|
+ .append(QStringLiteral("</td><td>"))
|
|
|
|
+ .append(fortune.second.toHtmlEscaped())
|
|
|
|
+ .append(QStringLiteral("</td></tr>"));
|
|
}
|
|
}
|
|
|
|
|
|
out.append(QStringLiteral("</table></body></html>"));
|
|
out.append(QStringLiteral("</table></body></html>"));
|
|
|
|
|
|
- c->response()->setBody(out);
|
|
|
|
|
|
+ auto response = c->response();
|
|
|
|
+ response->setBody(out);
|
|
|
|
+ response->setContentType(QStringLiteral("text/html; charset=UTF-8"));
|
|
}
|
|
}
|