Browse Source

Add DAO prepared queries for async platforms (#5417)

jcheron 5 years ago
parent
commit
82f921dff7

+ 4 - 2
frameworks/PHP/ubiquity/app/config/swooleServices.php

@@ -1,8 +1,8 @@
 <?php
 \Ubiquity\cache\CacheManager::startProd($config);
 \Ubiquity\orm\DAO::setModelsDatabases([
-	"models\\Fortune" => 'pgsql',
-	"models\\World" => 'pgsql'
+	'models\\Fortune' => 'pgsql',
+	'models\\World' => 'pgsql'
 ]);
 \Ubiquity\cache\CacheManager::warmUpControllers([
 	'controllers\\Plaintext',
@@ -12,4 +12,6 @@
 ]);
 $swooleServer->on('workerStart', function ($srv) use (&$config) {
 	\Ubiquity\orm\DAO::startDatabase($config, 'pgsql');
+	\Ubiquity\orm\DAO::prepareGetById('world', 'models\\World');
+	\Ubiquity\orm\DAO::prepareGetAll('fortune', 'models\\Fortune');
 });

+ 2 - 0
frameworks/PHP/ubiquity/app/config/workerServices.php

@@ -12,5 +12,7 @@
 ]);
 $workerServer->onWorkerStart = function () use ($config) {
 	\Ubiquity\orm\DAO::startDatabase($config, 'pgsql');
+	\Ubiquity\orm\DAO::prepareGetById('world', 'models\\World');
+	\Ubiquity\orm\DAO::prepareGetAll('fortune', 'models\\Fortune');
 };
 

+ 5 - 5
frameworks/PHP/ubiquity/app/controllers/SwooleDb.php

@@ -16,9 +16,9 @@ class SwooleDb extends \Ubiquity\controllers\Controller {
 	}
 
 	public function index() {
-		$world = DAO::getById(World::class, [
+		$world = DAO::executePrepared('world', [
 			'id' => \mt_rand(1, 10000)
-		], false);
+		]);
 		echo \json_encode($world->_rest);
 	}
 
@@ -26,9 +26,9 @@ class SwooleDb extends \Ubiquity\controllers\Controller {
 		$worlds = [];
 		$queries = \min(\max($queries, 1), 500);
 		for ($i = 0; $i < $queries; ++ $i) {
-			$worlds[] = (DAO::getById(World::class, [
+			$worlds[] = (DAO::executePrepared('world', [
 				'id' => \mt_rand(1, 10000)
-			], false))->_rest;
+			]))->_rest;
 		}
 		echo \json_encode($worlds);
 	}
@@ -37,7 +37,7 @@ class SwooleDb extends \Ubiquity\controllers\Controller {
 		$worlds = [];
 		$queries = \min(\max($queries, 1), 500);
 		for ($i = 0; $i < $queries; ++ $i) {
-			$world = DAO::getById(World::class, [
+			$world = DAO::executePrepared('world', [
 				'id' => \mt_rand(1, 10000)
 			], false);
 			$world->randomNumber = \mt_rand(1, 10000);

+ 1 - 2
frameworks/PHP/ubiquity/app/controllers/SwooleFortunes.php

@@ -2,12 +2,11 @@
 namespace controllers;
 
 use models\Fortune;
-use Ubiquity\orm\DAO;
 
 class SwooleFortunes extends \Ubiquity\controllers\SimpleViewAsyncController {
 
 	public function index() {
-		$fortunes = DAO::getAll(Fortune::class, '', false);
+		$fortunes = \Ubiquity\orm\DAO::executePrepared('fortune');
 		$fortunes[] = (new Fortune())->setId(0)->setMessage('Additional fortune added at request time.');
 		\usort($fortunes, function ($left, $right) {
 			return $left->message <=> $right->message;

+ 6 - 6
frameworks/PHP/ubiquity/app/controllers/WorkerDb.php

@@ -16,9 +16,9 @@ class WorkerDb extends \Ubiquity\controllers\Controller {
 	}
 
 	public function index() {
-		$world = DAO::getById(World::class, [
+		$world = DAO::executePrepared('world', [
 			'id' => \mt_rand(1, 10000)
-		], false);
+		]);
 		echo \json_encode($world->_rest);
 	}
 
@@ -26,9 +26,9 @@ class WorkerDb extends \Ubiquity\controllers\Controller {
 		$worlds = [];
 		$queries = \min(\max($queries, 1), 500);
 		for ($i = 0; $i < $queries; ++ $i) {
-			$worlds[] = (DAO::getById(World::class, [
+			$worlds[] = (DAO::executePrepared('world', [
 				'id' => \mt_rand(1, 10000)
-			], false))->_rest;
+			]))->_rest;
 		}
 		echo \json_encode($worlds);
 	}
@@ -37,9 +37,9 @@ class WorkerDb extends \Ubiquity\controllers\Controller {
 		$worlds = [];
 		$queries = \min(\max($queries, 1), 500);
 		for ($i = 0; $i < $queries; ++ $i) {
-			$world = DAO::getById(World::class, [
+			$world = DAO::executePrepared('world', [
 				'id' => \mt_rand(1, 10000)
-			], false);
+			]);
 			$world->randomNumber = \mt_rand(1, 10000);
 			DAO::toUpdate($world);
 			$worlds[] = $world->_rest;

+ 1 - 2
frameworks/PHP/ubiquity/app/controllers/WorkerFortunes.php

@@ -2,7 +2,6 @@
 namespace controllers;
 
 use models\Fortune;
-use Ubiquity\orm\DAO;
 
 class WorkerFortunes extends \Ubiquity\controllers\SimpleViewAsyncController {
 
@@ -11,7 +10,7 @@ class WorkerFortunes extends \Ubiquity\controllers\SimpleViewAsyncController {
 	}
 
 	public function index() {
-		$fortunes = DAO::getAll(Fortune::class, '', false);
+		$fortunes = \Ubiquity\orm\DAO::executePrepared('fortune');
 		$fortunes[] = (new Fortune())->setId(0)->setMessage('Additional fortune added at request time.');
 		\usort($fortunes, function ($left, $right) {
 			return $left->message <=> $right->message;