diff --git a/frameworks/PHP/webman/app/controller/Index.php b/frameworks/PHP/webman/app/controller/Index.php index 6a72994f1ff..947e85a0d0b 100644 --- a/frameworks/PHP/webman/app/controller/Index.php +++ b/frameworks/PHP/webman/app/controller/Index.php @@ -28,7 +28,7 @@ public function json($request) public function db($request) { - $statement = Db::$statement; + $statement = Db::$random; $statement->execute([\mt_rand(1, 10000)]); return new Response(200, [ @@ -43,13 +43,13 @@ public function fortunes($request) $fortune->execute(); - $arr = $fortune->fetchAll(PDO::FETCH_KEY_PAIR); + $arr = $fortune->fetchAll(\PDO::FETCH_KEY_PAIR); $arr[0] = 'Additional fortune added at request time.'; \asort($arr); $html = ''; foreach ($arr as $id => $message) { - $message = \htmlspecialchars($message, ENT_QUOTES, 'UTF-8'); + $message = \htmlspecialchars($message, \ENT_QUOTES, 'UTF-8'); $html .= "$id$message"; } @@ -61,7 +61,7 @@ public function fortunes($request) public function queries($request, $q = 1) { - $statement = Db::$statement; + $statement = Db::$random; $query_count = 1; if ((int) $q > 1) { @@ -83,41 +83,28 @@ public function queries($request, $q = 1) public function updates($request, $q = 1) { $random = Db::$random; - $update = Db::$update; $query_count = 1; if ((int) $q > 1) { $query_count = \min($q, 500); } - $arr = []; + $worlds = []; while ($query_count--) { - $id = \mt_rand(1, 10000); - $random->execute([$id]); - - //$random->fetchColumn(); // - //$world = ['id' => $id, 'randomNumber' => \mt_rand(1, 10000)]; // + $random->execute([\mt_rand(1, 10000)]); + $world = $random->fetch(); + $world['randomNumber'] = \mt_rand(1, 10000); - $world = ['id' => $id, 'randomNumber' => $random->fetchColumn()]; - $update->execute( - [$world['randomNumber'] = mt_rand(1, 10000), $id] - ); - - $arr[] = $world; + $worlds[] = $world; } - /*$pdo = Db::$pdo; - $pdo->beginTransaction(); - foreach($arr as $world) { - $update->execute([$world['randomNumber'], $world['id']]); - } - $pdo->commit();*/ + Db::update($worlds); return new Response(200, [ 'Content-Type' => 'application/json', 'Date' => Date::$date - ], \json_encode($arr)); + ], \json_encode($worlds)); } diff --git a/frameworks/PHP/webman/support/bootstrap/db/Raw.php b/frameworks/PHP/webman/support/bootstrap/db/Raw.php index 6cf44f8bec8..d27d846bb3c 100644 --- a/frameworks/PHP/webman/support/bootstrap/db/Raw.php +++ b/frameworks/PHP/webman/support/bootstrap/db/Raw.php @@ -24,30 +24,17 @@ */ class Raw implements Bootstrap { - /** - * @var \PDO - */ - public static $pdo; - /** - * @var PDOStatement - */ - public static $statement; + public static PDO $pdo; - /** - * @var PDOStatement - */ - public static $fortune; + public static PDOStatement $fortune; - /** - * @var PDOStatement - */ - public static $random; + public static PDOStatement $random; /** - * @var PDOStatement + * @var PDOStatement[] */ - public static $update; + public static array $update; /** * @param Worker $worker @@ -61,10 +48,37 @@ public static function start($worker) [PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC, PDO::ATTR_EMULATE_PREPARES => false] ); - self::$statement = $pdo->prepare('SELECT id,randomNumber FROM World WHERE id=?'); - self::$fortune = $pdo->prepare('SELECT id,message FROM Fortune'); self::$random = $pdo->prepare('SELECT id,randomNumber FROM World WHERE id=?'); - self::$update = $pdo->prepare('UPDATE World SET randomNumber=? WHERE id=?'); + self::$fortune = $pdo->prepare('SELECT id,message FROM Fortune'); self::$pdo = $pdo; } + + /** + * Postgres bulk update + * + * @param array $worlds + * @return void + */ + public static function update(array $worlds) + { + $rows = count($worlds); + + if (!isset(self::$update[$rows])) { + $sql = 'UPDATE world SET randomNumber = CASE id' + . str_repeat(' WHEN ?::INTEGER THEN ?::INTEGER ', $rows) + . 'END WHERE id IN (' + . str_repeat('?::INTEGER,', $rows - 1) . '?::INTEGER)'; + + self::$update[$rows] = self::$pdo->prepare($sql); + } + + $val = []; + $keys = []; + foreach ($worlds as $world) { + $val[] = $keys[] = $world['id']; + $val[] = $world['randomNumber']; + } + + self::$update[$rows]->execute([...$val, ...$keys]); + } } diff --git a/frameworks/PHP/webman/webman.dockerfile b/frameworks/PHP/webman/webman.dockerfile index 01a722f94b6..923ea5ebf71 100644 --- a/frameworks/PHP/webman/webman.dockerfile +++ b/frameworks/PHP/webman/webman.dockerfile @@ -3,16 +3,17 @@ FROM ubuntu:22.04 ARG DEBIAN_FRONTEND=noninteractive RUN apt-get update -yqq && apt-get install -yqq software-properties-common > /dev/null -RUN LC_ALL=C.UTF-8 add-apt-repository ppa:ondrej/php -RUN apt-get update -yqq > /dev/null && \ - apt-get install -yqq php8.1-cli php8.1-pgsql php8.1-xml > /dev/null +RUN LC_ALL=C.UTF-8 add-apt-repository ppa:ondrej/php > /dev/null && \ + apt-get update -yqq > /dev/null && apt-get upgrade -yqq > /dev/null + +RUN apt-get install -yqq php8.2-cli php8.2-pgsql php8.2-xml > /dev/null COPY --from=composer:latest /usr/bin/composer /usr/local/bin/composer -RUN apt-get update -yqq && apt-get install -y php-pear php8.1-dev libevent-dev git -RUN pecl install event-3.0.8 > /dev/null && echo "extension=event.so" > /etc/php/8.1/cli/conf.d/event.ini +RUN apt-get update -yqq && apt-get install -y php-pear php8.2-dev libevent-dev git > /dev/null +RUN pecl install event-3.0.8 > /dev/null && echo "extension=event.so" > /etc/php/8.2/cli/conf.d/event.ini -COPY php.ini /etc/php/8.1/cli/php.ini +COPY php.ini /etc/php/8.2/cli/php.ini ADD ./ /webman WORKDIR /webman