Skip to content

Commit

Permalink
Update to Cutelyst v4 and ASql 0.84
Browse files Browse the repository at this point in the history
  • Loading branch information
dantti committed Nov 8, 2023
1 parent b7ddeff commit 01a8dea
Show file tree
Hide file tree
Showing 10 changed files with 93 additions and 97 deletions.
9 changes: 5 additions & 4 deletions frameworks/C++/cutelyst/build.sh
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
#!/bin/bash

export ASQL_VER=0.74.0
export ASQL_VER=0.84.0
export CUTELEE_VER=6.1.0
export CUTELYST_VER=3.7.0
export CUTELYST_TAG=v4.0.0-alpha1
export CUTELYST_VER=4.0.0

apt update -qq && \
apt install -yqq --no-install-recommends \
Expand All @@ -21,8 +22,8 @@ wget -q https://github.com/cutelyst/cutelee/releases/download/v${CUTELEE_VER}/cu
wget -q https://github.com/cutelyst/asql/releases/download/v${ASQL_VER}/libasql0-qt6_${ASQL_VER}_amd64.deb && \
apt install -yqq ./libasql0-qt6_${ASQL_VER}_amd64.deb

wget -q https://github.com/cutelyst/cutelyst/releases/download/v${CUTELYST_VER}/cutelyst3-qt6_${CUTELYST_VER}_amd64.deb && \
apt install -yqq ./cutelyst3-qt6_${CUTELYST_VER}_amd64.deb
wget -q https://github.com/cutelyst/cutelyst/releases/download/${CUTELYST_TAG}/cutelyst4-qt6_${CUTELYST_VER}_amd64.deb && \
apt install -yqq ./cutelyst4-qt6_${CUTELYST_VER}_amd64.deb

cd ${TROOT} && \
mkdir -p build && \
Expand Down
4 changes: 2 additions & 2 deletions frameworks/C++/cutelyst/src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ FetchContent_Declare(
FetchContent_MakeAvailable(mimalloc)

find_package(Qt6 6.2.0 REQUIRED COMPONENTS Core Network Sql)
find_package(ASqlQt6 0.74 REQUIRED)
find_package(Cutelyst3Qt6 3.6 REQUIRED)
find_package(ASqlQt6 0.84 REQUIRED)
find_package(Cutelyst4Qt6 3.6 REQUIRED)
find_package(Cutelee6Qt6 6.1.0 REQUIRED)
find_package(PostgreSQL REQUIRED)

Expand Down
6 changes: 3 additions & 3 deletions frameworks/C++/cutelyst/src/cachedqueries.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ CachedQueries::CachedQueries(QObject *parent)

void CachedQueries::cached_queries(Context *c)
{
int queries = c->request()->queryParam(QStringLiteral("count")).toInt();
int queries = c->request()->queryParam(u"count"_qs).toInt();
if (queries < 1) {
queries = 1;
} else if (queries > 500) {
Expand Down Expand Up @@ -48,8 +48,8 @@ void CachedQueries::cached_queries(Context *c)
auto it = result.begin();
int id = it[0].toInt();
auto obj = new QJsonObject({
{QStringLiteral("id"), id},
{QStringLiteral("randomNumber"), it[1].toInt()}
{u"id"_qs, id},
{u"randomNumber"_qs, it[1].toInt()}
});
array->append(*obj);
cache.insert(id, obj, 1);
Expand Down
41 changes: 18 additions & 23 deletions frameworks/C++/cutelyst/src/cutelyst-benchmarks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ cutelyst_benchmarks::~cutelyst_benchmarks()

bool cutelyst_benchmarks::init()
{
if (config(QStringLiteral("SendDate")).value<bool>()) {
if (config(u"SendDate"_qs).value<bool>()) {
qDebug() << "Manually send date";
auto dateT = new QTimer(this);
dateT->setInterval(1000);
Expand All @@ -63,9 +63,9 @@ bool cutelyst_benchmarks::init()
new CachedQueries(this);

if (defaultHeaders().server().isEmpty()) {
defaultHeaders().setServer(QStringLiteral("Cutelyst"));
defaultHeaders().setServer("Cutelyst"_qba);
}
defaultHeaders().removeHeader(QStringLiteral("X-Cutelyst"));
defaultHeaders().removeHeader("X-Cutelyst");

return true;
}
Expand All @@ -75,48 +75,43 @@ bool cutelyst_benchmarks::postFork()
QMutexLocker locker(&mutex); // QSqlDatabase::addDatabase is not thread-safe

QSqlDatabase db;
const auto driver = config(QStringLiteral("Driver")).toString();
const auto driver = config(u"Driver"_qs).toString();
if (driver == u"QPSQL") {
db = QSqlDatabase::addDatabase(driver, Sql::databaseNameThread(QStringLiteral("postgres")));
db.setDatabaseName(QStringLiteral("hello_world"));
db.setUserName(QStringLiteral("benchmarkdbuser"));
db.setPassword(QStringLiteral("benchmarkdbpass"));
db.setHostName(config(QStringLiteral("DatabaseHostName")).toString());
db = QSqlDatabase::addDatabase(driver, Sql::databaseNameThread(u"postgres"_qs));
db.setDatabaseName(u"hello_world"_qs);
db.setUserName(u"benchmarkdbuser"_qs);
db.setPassword(u"benchmarkdbpass"_qs);
db.setHostName(config(u"DatabaseHostName"_qs).toString());
if (!db.open()) {
qDebug() << "Error opening PostgreSQL db:" << db << db.connectionName() << db.lastError().databaseText();
return false;
}
} else if (driver == u"QMYSQL") {
db = QSqlDatabase::addDatabase(driver, Sql::databaseNameThread(QStringLiteral("mysql")));
db.setDatabaseName(QStringLiteral("hello_world"));
db.setUserName(QStringLiteral("benchmarkdbuser"));
db.setPassword(QStringLiteral("benchmarkdbpass"));
db.setHostName(config(QStringLiteral("DatabaseHostName")).toString());
db = QSqlDatabase::addDatabase(driver, Sql::databaseNameThread(u"mysql"_qs));
db.setDatabaseName(u"hello_world"_qs);
db.setUserName(u"benchmarkdbuser"_qs);
db.setPassword(u"benchmarkdbpass"_qs);
db.setHostName(config(u"DatabaseHostName"_qs).toString());
if (!db.open()) {
qDebug() << "Error opening MySQL db:" << db << db.connectionName() << db.lastError().databaseText();
return false;
}
} else if (driver == u"postgres") {
QUrl uri(QStringLiteral("postgresql://benchmarkdbuser:benchmarkdbpass@server/hello_world"));
uri.setHost(config(QStringLiteral("DatabaseHostName")).toString());
QUrl uri(u"postgresql://benchmarkdbuser:benchmarkdbpass@server/hello_world"_qs);
uri.setHost(config(u"DatabaseHostName"_qs).toString());
qDebug() << "ASql URI:" << uri.toString();

APool::create(ASql::APg::factory(uri.toString()));
APool::setMaxIdleConnections(128);
APool::setSetupCallback([](ADatabase &db) {
APool::setSetupCallback([](ADatabase db) {
// Enable Pipeline mode
db.enterPipelineMode(500);
});
}

qDebug() << "Connections" << QCoreApplication::applicationPid() << QThread::currentThread() << QSqlDatabase::connectionNames();
// db = QSqlDatabase::addDatabase(QLatin1String("QSQLITE"), QLatin1String("sqlite"));
// if (!db.open()) {
// qDebug() << "Error opening db:" << db << db.lastError().databaseText();
// return false;
// }

return true;
}

//#include "moc_cutelyst-benchmarks.cpp"
#include "moc_cutelyst-benchmarks.cpp"
42 changes: 21 additions & 21 deletions frameworks/C++/cutelyst/src/databaseupdatestest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ DatabaseUpdatesTest::DatabaseUpdatesTest(QObject *parent) : Controller(parent)

void DatabaseUpdatesTest::updatep(Context *c)
{
int queries = c->request()->queryParam(QStringLiteral("queries"), QStringLiteral("1")).toInt();
int queries = c->request()->queryParam(u"queries"_qs, u"1"_qs).toInt();
if (queries < 1) {
queries = 1;
} else if (queries > 500) {
Expand Down Expand Up @@ -66,7 +66,7 @@ void DatabaseUpdatesTest::updatep(Context *c)

void DatabaseUpdatesTest::updateb(Context *c)
{
int queries = c->request()->queryParam(QStringLiteral("queries"), QStringLiteral("1")).toInt();
int queries = c->request()->queryParam(u"queries"_qs, u"1"_qs).toInt();
if (queries < 1) {
queries = 1;
} else if (queries > 500) {
Expand Down Expand Up @@ -117,30 +117,30 @@ void DatabaseUpdatesTest::updateb(Context *c)
void DatabaseUpdatesTest::updates_postgres(Context *c)
{
QSqlQuery query = CPreparedSqlQueryThreadForDB(
QLatin1String("SELECT id, randomNumber FROM world WHERE id = :id"),
QStringLiteral("postgres"));
u"SELECT id, randomNumber FROM world WHERE id = :id"_qs,
u"postgres"_qs);
QSqlQuery updateQuery = CPreparedSqlQueryThreadForDB(
QLatin1String("UPDATE world SET randomNumber = :randomNumber WHERE id = :id"),
QStringLiteral("postgres"));
u"UPDATE world SET randomNumber = :randomNumber WHERE id = :id"_qs,
u"postgres"_qs);
processQuery(c, query, updateQuery);
}

void DatabaseUpdatesTest::updates_mysql(Context *c)
{
QSqlQuery query = CPreparedSqlQueryThreadForDB(
QLatin1String("SELECT randomNumber, id FROM world WHERE id = :id"),
QStringLiteral("mysql"));
u"SELECT randomNumber, id FROM world WHERE id = :id"_qs,
u"mysql"_qs);
QSqlQuery updateQuery = CPreparedSqlQueryThreadForDB(
QLatin1String("UPDATE world SET randomNumber = :randomNumber WHERE id = :id"),
QStringLiteral("mysql"));
u"UPDATE world SET randomNumber = :randomNumber WHERE id = :id"_qs,
u"mysql"_qs);
processQuery(c, query, updateQuery);
}

void DatabaseUpdatesTest::processQuery(Context *c, QSqlQuery &query, QSqlQuery &updateQuery)
{
QJsonArray array;

int queries = c->request()->queryParam(QStringLiteral("queries"), QStringLiteral("1")).toInt();
int queries = c->request()->queryParam(u"queries"_qs, u"1"_qs).toInt();
if (queries < 1) {
queries = 1;
} else if (queries > 500) {
Expand All @@ -153,7 +153,7 @@ void DatabaseUpdatesTest::processQuery(Context *c, QSqlQuery &query, QSqlQuery &
for (int i = 0; i < queries; ++i) {
int id = (rand() % 10000) + 1;

query.bindValue(QStringLiteral(":id"), id);
query.bindValue(u":id"_qs, id);
if (Q_UNLIKELY(!query.exec() || !query.next())) {
c->res()->setStatus(Response::InternalServerError);
return;
Expand All @@ -164,13 +164,13 @@ void DatabaseUpdatesTest::processQuery(Context *c, QSqlQuery &query, QSqlQuery &
randomNumbers.append(randomNumber);

array.append(QJsonObject{
{QStringLiteral("id"), id},
{QStringLiteral("randomNumber"), randomNumber}
{u"id"_qs, id},
{u"randomNumber"_qs, randomNumber}
});
}

updateQuery.bindValue(QStringLiteral(":id"), ids);
updateQuery.bindValue(QStringLiteral(":randomNumber"), randomNumbers);
updateQuery.bindValue(u":id"_qs, ids);
updateQuery.bindValue(u":randomNumber"_qs, randomNumbers);
if (Q_LIKELY(updateQuery.execBatch())) {
c->response()->setJsonArrayBody(array);
} else {
Expand All @@ -182,24 +182,24 @@ APreparedQuery DatabaseUpdatesTest::getSql(int count)
{
auto iter = m_sqlMap.find(count);
if (Q_UNLIKELY(iter == m_sqlMap.end())) {
QString sql = QStringLiteral("UPDATE WORLD SET randomnumber=CASE id ");
QString sql = u"UPDATE WORLD SET randomnumber=CASE id "_qs;
sql.reserve(80 + count * 25);
int placeholdersCounter = 1;
for (int i = 0; i < count; i++) {
sql.append(QStringLiteral("WHEN $%1 THEN $%2 ").arg(placeholdersCounter).arg(placeholdersCounter + 1));
sql.append(u"WHEN $%1 THEN $%2 "_qs.arg(placeholdersCounter).arg(placeholdersCounter + 1));
placeholdersCounter += 2;
}
sql.append(QStringLiteral("ELSE randomnumber END WHERE id IN ("));
sql.append(u"ELSE randomnumber END WHERE id IN (");

for (int i = 0; i < count; i++) {
sql.append(QLatin1Char('$') + QString::number(placeholdersCounter) + QLatin1Char(','));
sql.append(u'$' + QString::number(placeholdersCounter) + u',');
++placeholdersCounter;
}

if (count) {
sql.remove(sql.size() - 1, 1);
}
sql.append(QLatin1Char(')'));
sql.append(u')');

iter = m_sqlMap.insert(count, APreparedQuery(sql));
}
Expand Down
46 changes: 23 additions & 23 deletions frameworks/C++/cutelyst/src/fortunetest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ void FortuneTest::fortunes_raw_p(Context *c)
fortunes.emplace_back(Fortune{it[0].toInt(), it[1].toString()});
++it;
}
fortunes.emplace_back(Fortune{0, QStringLiteral("Additional fortune added at request time.")});
fortunes.emplace_back(Fortune{0, u"Additional fortune added at request time."_qs});

std::sort(fortunes.begin(), fortunes.end(), [] (const Fortune &a1, const Fortune &a2) {
return a1.message < a2.message;
Expand All @@ -46,17 +46,17 @@ void FortuneTest::fortunes_raw_p(Context *c)
void FortuneTest::fortunes_raw_postgres(Context *c)
{
QSqlQuery query = CPreparedSqlQueryThreadForDB(
QLatin1String("SELECT id, message FROM fortune"),
QStringLiteral("postgres"));
u"SELECT id, message FROM fortune"_qs,
u"postgres"_qs);
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"));
u"SELECT id, message FROM fortune"_qs,
u"mysql"_qs);
auto fortunes = processQuery(c, query);
renderRaw(c, fortunes);
}
Expand All @@ -82,59 +82,59 @@ void FortuneTest::fortunes_c_p(Context *c)
}

fortunes.append(QVariant::fromValue(QVariantList{
{0, QStringLiteral("Additional fortune added at request time.")},
{0, u"Additional fortune added at request time."_qs},
}));
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);
c->setStash(u"template"_qs, u"fortunes.html"_qs);
c->setStash(u"fortunes"_qs, fortunes);
static thread_local View *view = c->view();
view->execute(c);
c->response()->setContentType(QStringLiteral("text/html; charset=UTF-8"));
c->response()->setContentType("text/html; charset=UTF-8"_qba);
});
}

void FortuneTest::fortunes_cutelee_postgres(Context *c)
{
QSqlQuery query = CPreparedSqlQueryThreadForDB(
QLatin1String("SELECT id, message FROM fortune"),
QStringLiteral("postgres"));
u"SELECT id, message FROM fortune"_qs,
u"postgres"_qs);
if (query.exec()) {
QVariantList fortunes = Sql::queryToList(query);
fortunes.append(QVariant::fromValue(QVariantList{
{0, QStringLiteral("Additional fortune added at request time.")},
{0, u"Additional fortune added at request time."_qs},
}));
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);
c->setStash(u"template"_qs, u"fortunes.html"_qs);
c->setStash(u"fortunes"_qs, fortunes);
static thread_local View *view = c->view();
view->execute(c);
c->response()->setContentType(QStringLiteral("text/html; charset=UTF-8"));
c->response()->setContentType("text/html; charset=UTF-8"_qba);
}
}

void FortuneTest::fortunes_cutelee_mysql(Context *c)
{
QSqlQuery query = CPreparedSqlQueryThreadForDB(
QLatin1String("SELECT id, message FROM fortune"),
QStringLiteral("mysql"));
u"SELECT id, message FROM fortune"_qs,
u"mysql"_qs);
if (query.exec()) {
QVariantList fortunes = Sql::queryToList(query);
fortunes.append(QVariant::fromValue(QVariantList{
{0, QStringLiteral("Additional fortune added at request time.")},
{0, u"Additional fortune added at request time."_qs},
}));
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);
c->setStash(u"template"_qs, u"fortunes.html"_qs);
c->setStash(u"fortunes"_qs, fortunes);
static thread_local View *view = c->view();
view->execute(c);
c->response()->setContentType(QStringLiteral("text/html; charset=UTF-8"));
c->response()->setContentType("text/html; charset=UTF-8"_qba);
}
}

Expand All @@ -151,7 +151,7 @@ FortuneList FortuneTest::processQuery(Context *c, QSqlQuery &query)
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.")});
fortunes.push_back({0, u"Additional fortune added at request time."_qs});

std::sort(fortunes.begin(), fortunes.end(), [] (const Fortune &a1, const Fortune &a2) {
return a1.message < a2.message;
Expand Down Expand Up @@ -183,5 +183,5 @@ void FortuneTest::renderRaw(Context *c, const FortuneList &fortunes) const

auto response = c->response();
response->setBody(out);
response->setContentType(QStringLiteral("text/html; charset=UTF-8"));
response->setContentType("text/html; charset=UTF-8"_qba);
}
2 changes: 1 addition & 1 deletion frameworks/C++/cutelyst/src/jsontest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ JsonTest::JsonTest(QObject *parent) : Controller(parent)

void JsonTest::json(Context *c)
{
c->response()->setJsonObjectBody({ {QStringLiteral("message"), QStringLiteral("Hello, World!")} });
c->response()->setJsonObjectBody({ {u"message"_qs, u"Hello, World!"_qs} });
}

void JsonTest::pson(Context *c)
Expand Down
Loading

0 comments on commit 01a8dea

Please sign in to comment.