| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187 |
- #include "fortunetest.h"
- #include <Cutelyst/Plugins/Utils/Sql>
- #include <Cutelyst/View>
- #include <apool.h>
- #include <aresult.h>
- #include <apreparedquery.h>
- #include <QSqlQuery>
- using namespace ASql;
- FortuneTest::FortuneTest(QObject *parent) : Controller(parent)
- {
- }
- void FortuneTest::fortunes_raw_p(Context *c)
- {
- ASync async(c);
- static thread_local auto db = APool::database();
- db.exec(APreparedQueryLiteral(u8"SELECT id, message FROM fortune"), c, [c, async, this] (AResult &result) {
- if (Q_UNLIKELY(result.error() && !result.size())) {
- c->res()->setStatus(Response::InternalServerError);
- return;
- }
- FortuneList fortunes;
- fortunes.reserve(result.size());
- auto it = result.begin();
- while (it != result.end()) {
- fortunes.emplace_back(Fortune{it[0].toInt(), it[1].toString()});
- ++it;
- }
- fortunes.emplace_back(Fortune{0, QStringLiteral("Additional fortune added at request time.")});
- std::sort(fortunes.begin(), fortunes.end(), [] (const Fortune &a1, const Fortune &a2) {
- return a1.message < a2.message;
- });
- renderRaw(c, fortunes);
- });
- }
- void FortuneTest::fortunes_raw_postgres(Context *c)
- {
- QSqlQuery query = CPreparedSqlQueryThreadForDB(
- QLatin1String("SELECT id, message FROM fortune"),
- QStringLiteral("postgres"));
- auto fortunes = processQuery(c, query);
- renderRaw(c, fortunes);
- }
- void FortuneTest::fortunes_raw_mysql(Context *c)
- {
- QSqlQuery query = CPreparedSqlQueryThreadForDB(
- QLatin1String("SELECT id, message FROM fortune"),
- QStringLiteral("mysql"));
- auto fortunes = processQuery(c, query);
- renderRaw(c, fortunes);
- }
- void FortuneTest::fortunes_c_p(Context *c)
- {
- ASync async(c);
- static thread_local auto db = APool::database();
- db.exec(APreparedQueryLiteral(u8"SELECT id, message FROM fortune"), c, [c, async] (AResult &result) {
- if (Q_UNLIKELY(result.error() && !result.size())) {
- c->res()->setStatus(Response::InternalServerError);
- return;
- }
- QVariantList fortunes;
- fortunes.reserve(result.size());
- auto it = result.begin();
- while (it != result.end()) {
- fortunes.append(QVariant::fromValue(QVariantList{
- {it[0].toInt(), it[1].toString()},
- }));
- ++it;
- }
- fortunes.append(QVariant::fromValue(QVariantList{
- {0, QStringLiteral("Additional fortune added at request time.")},
- }));
- std::sort(fortunes.begin(), fortunes.end(), [] (const QVariant &a1, const QVariant &a2) {
- return a1.toList()[1].toString() < a2.toList()[1].toString();
- });
- c->setStash(QStringLiteral("template"), QStringLiteral("fortunes.html"));
- c->setStash(QStringLiteral("fortunes"), fortunes);
- static thread_local View *view = c->view();
- view->execute(c);
- c->response()->setContentType(QStringLiteral("text/html; charset=UTF-8"));
- });
- }
- void FortuneTest::fortunes_cutelee_postgres(Context *c)
- {
- QSqlQuery query = CPreparedSqlQueryThreadForDB(
- QLatin1String("SELECT id, message FROM fortune"),
- QStringLiteral("postgres"));
- if (query.exec()) {
- QVariantList fortunes = Sql::queryToList(query);
- fortunes.append(QVariant::fromValue(QVariantList{
- {0, QStringLiteral("Additional fortune added at request time.")},
- }));
- std::sort(fortunes.begin(), fortunes.end(), [] (const QVariant &a1, const QVariant &a2) {
- return a1.toList()[1].toString() < a2.toList()[1].toString();
- });
- c->setStash(QStringLiteral("template"), QStringLiteral("fortunes.html"));
- c->setStash(QStringLiteral("fortunes"), fortunes);
- static thread_local View *view = c->view();
- view->execute(c);
- c->response()->setContentType(QStringLiteral("text/html; charset=UTF-8"));
- }
- }
- void FortuneTest::fortunes_cutelee_mysql(Context *c)
- {
- QSqlQuery query = CPreparedSqlQueryThreadForDB(
- QLatin1String("SELECT id, message FROM fortune"),
- QStringLiteral("mysql"));
- if (query.exec()) {
- QVariantList fortunes = Sql::queryToList(query);
- fortunes.append(QVariant::fromValue(QVariantList{
- {0, QStringLiteral("Additional fortune added at request time.")},
- }));
- std::sort(fortunes.begin(), fortunes.end(), [] (const QVariant &a1, const QVariant &a2) {
- return a1.toList()[1].toString() < a2.toList()[1].toString();
- });
- c->setStash(QStringLiteral("template"), QStringLiteral("fortunes.html"));
- c->setStash(QStringLiteral("fortunes"), fortunes);
- static thread_local View *view = c->view();
- view->execute(c);
- c->response()->setContentType(QStringLiteral("text/html; charset=UTF-8"));
- }
- }
- FortuneList FortuneTest::processQuery(Context *c, QSqlQuery &query)
- {
- FortuneList fortunes;
- if (Q_UNLIKELY(!query.exec())) {
- c->res()->setStatus(Response::InternalServerError);
- return fortunes;
- }
- fortunes.reserve(query.size());
- while (query.next()) {
- fortunes.push_back({query.value(0).toInt(), query.value(1).toString()});
- }
- fortunes.push_back({0, QStringLiteral("Additional fortune added at request time.")});
- std::sort(fortunes.begin(), fortunes.end(), [] (const Fortune &a1, const Fortune &a2) {
- return a1.message < a2.message;
- });
- return fortunes;
- }
- void FortuneTest::renderRaw(Context *c, const FortuneList &fortunes) const
- {
- QByteArray out;
- out.append("<!DOCTYPE html>"
- "<html>"
- "<head><title>Fortunes</title></head>"
- "<body>"
- "<table>"
- "<tr><th>id</th><th>message</th></tr>");
- out.reserve(4096);
- for (const Fortune &fortune : fortunes) {
- out.append("<tr><td>")
- .append(QByteArray::number(fortune.id))
- .append("</td><td>")
- .append(fortune.message.toHtmlEscaped().toUtf8())
- .append("</td></tr>");
- }
- out.append("</table></body></html>");
- auto response = c->response();
- response->setBody(out);
- response->setContentType(QStringLiteral("text/html; charset=UTF-8"));
- }
|