singledatabasequerytest.cpp 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. #include "singledatabasequerytest.h"
  2. #include <Cutelyst/Plugins/Utils/Sql>
  3. #include <QSqlQuery>
  4. #include <QJsonDocument>
  5. #include <QJsonObject>
  6. SingleDatabaseQueryTest::SingleDatabaseQueryTest(QObject *parent) : Controller(parent)
  7. {
  8. }
  9. void SingleDatabaseQueryTest::db_postgres(Context *c)
  10. {
  11. QSqlQuery query = CPreparedSqlQueryThreadForDB(
  12. QLatin1String("SELECT id, randomNumber FROM world WHERE id = :id"),
  13. QStringLiteral("postgres"));
  14. processQuery(c, query);
  15. }
  16. void SingleDatabaseQueryTest::db_mysql(Context *c)
  17. {
  18. QSqlQuery query = CPreparedSqlQueryThreadForDB(
  19. QLatin1String("SELECT id, randomNumber FROM world WHERE id = :id"),
  20. QStringLiteral("mysql"));
  21. processQuery(c, query);
  22. }
  23. void SingleDatabaseQueryTest::processQuery(Context *c, QSqlQuery &query)
  24. {
  25. int id = (qrand() % 10000) + 1;
  26. query.bindValue(QStringLiteral(":id"), id);
  27. if (Q_UNLIKELY(!query.exec() || !query.next())) {
  28. c->res()->setStatus(Response::InternalServerError);
  29. return;
  30. }
  31. c->response()->setJsonObjectBody({
  32. {QStringLiteral("id"), query.value(0).toInt()},
  33. {QStringLiteral("randomNumber"), query.value(1).toInt()}
  34. });
  35. }