|
@@ -58,6 +58,56 @@ void DatabaseUpdatesTest::updatep(Context *c)
|
|
|
c->response()->setJsonArrayBody(array);
|
|
|
}
|
|
|
|
|
|
+void DatabaseUpdatesTest::updateb(Context *c)
|
|
|
+{
|
|
|
+ int queries = c->request()->queryParam(QStringLiteral("queries"), QStringLiteral("1")).toInt();
|
|
|
+ if (queries < 1) {
|
|
|
+ queries = 1;
|
|
|
+ } else if (queries > 500) {
|
|
|
+ queries = 500;
|
|
|
+ }
|
|
|
+
|
|
|
+ QVariantList args;
|
|
|
+ QVariantList argsIds;
|
|
|
+
|
|
|
+ QJsonArray array;
|
|
|
+ ASync async(c);
|
|
|
+ static thread_local auto db = APool::database();
|
|
|
+ for (int i = 0; i < queries; ++i) {
|
|
|
+ int id = (qrand() % 10000) + 1;
|
|
|
+
|
|
|
+ int randomNumber = (qrand() % 10000) + 1;
|
|
|
+
|
|
|
+ argsIds.append(id);
|
|
|
+ args.append(id);
|
|
|
+ args.append(randomNumber);
|
|
|
+
|
|
|
+ array.append(QJsonObject{
|
|
|
+ {QStringLiteral("id"), id},
|
|
|
+ {QStringLiteral("randomNumber"), randomNumber}
|
|
|
+ });
|
|
|
+
|
|
|
+ db.execPrepared(APreparedQueryLiteral("SELECT randomNumber, id FROM world WHERE id=$1"),
|
|
|
+ {id}, [c, async] (AResult &result) {
|
|
|
+ if (Q_UNLIKELY(result.error() && !result.size())) {
|
|
|
+ c->res()->setStatus(Response::InternalServerError);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ }, c);
|
|
|
+ }
|
|
|
+ args.append(argsIds);
|
|
|
+
|
|
|
+ const APreparedQuery pq = getSql(queries);
|
|
|
+ db.execPrepared(pq, args, [c, async] (AResult &result) {
|
|
|
+ if (Q_UNLIKELY(result.error() && !result.size())) {
|
|
|
+ c->res()->setStatus(Response::InternalServerError);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ }, c);
|
|
|
+
|
|
|
+ c->response()->setJsonArrayBody(array);
|
|
|
+}
|
|
|
+
|
|
|
void DatabaseUpdatesTest::updates_postgres(Context *c)
|
|
|
{
|
|
|
QSqlQuery query = CPreparedSqlQueryThreadForDB(
|
|
@@ -121,3 +171,29 @@ void DatabaseUpdatesTest::processQuery(Context *c, QSqlQuery &query, QSqlQuery &
|
|
|
c->res()->setStatus(Response::InternalServerError);
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+APreparedQuery DatabaseUpdatesTest::getSql(int count)
|
|
|
+{
|
|
|
+ auto iter = m_sqlMap.find(count);
|
|
|
+ if (iter != m_sqlMap.end())
|
|
|
+ {
|
|
|
+ return iter.value();
|
|
|
+ }
|
|
|
+ QString sql = QStringLiteral("UPDATE WORLD SET randomnumber=CASE id ");
|
|
|
+ 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));
|
|
|
+ placeholdersCounter += 2;
|
|
|
+ }
|
|
|
+ sql.append(QStringLiteral("ELSE randomnumber END WHERE id IN ("));
|
|
|
+
|
|
|
+ for (int i = 0; i < count; i++) {
|
|
|
+ sql.append(QLatin1Char('$') + QString::number(placeholdersCounter));
|
|
|
+ ++placeholdersCounter;
|
|
|
+ }
|
|
|
+ sql.append(QLatin1Char(')'));
|
|
|
+ m_sqlMap.insert(count, sql);
|
|
|
+
|
|
|
+ return sql;
|
|
|
+}
|