Browse Source

PHP PDO use prepared statements in db server (#4862)

* PHP PDO use prepared statements in db server

* No emulated prepared statements in update test too

* Use native prepared statements in raw tests

* Use prepared statements in  ORM tests

* Small change to rerun travis

* Delete native persistent stm from ORM for now

* Use short notation for arrays

* Delete native prepared stm to ORM for now

* More short array notation
Joan Miquel 6 years ago
parent
commit
96a0c5429e

+ 5 - 4
frameworks/PHP/kumbiaphp/bench/app/controllers/raw_controller.php

@@ -9,9 +9,10 @@ class RawController extends AppController
         View::select(null, null);
         header('Content-type: application/json');
 
-        $this->pdo = new PDO('mysql:host=tfb-database;dbname=hello_world', 'benchmarkdbuser', 'benchmarkdbpass', array(
-            PDO::ATTR_PERSISTENT => true
-        ));
+        $this->pdo = new PDO('mysql:host=tfb-database;dbname=hello_world', 'benchmarkdbuser', 'benchmarkdbpass', [
+            PDO::ATTR_PERSISTENT => true,
+            PDO::ATTR_EMULATE_PREPARES => false
+        ]);
     }
 
     public function index()
@@ -44,7 +45,7 @@ class RawController extends AppController
             $id = mt_rand(1, 10000);
             $randomNumber = mt_rand(1, 10000);
 
-            $sth->execute(array($id));
+            $sth->execute([$id]);
             $row = ['id' => $id, 'randomNumber' => $updateSth->fetchColumn()];
             $row['randomNumber'] = $randomNumber;
             $updateSth->execute([$randomNumber, $id]);

+ 6 - 5
frameworks/PHP/php/dbquery.php

@@ -3,9 +3,10 @@ header('Content-type: application/json');
 
 // Database connection
 // http://www.php.net/manual/en/ref.pdo-mysql.php
-$pdo = new PDO('mysql:host=tfb-database;dbname=hello_world', 'benchmarkdbuser', 'benchmarkdbpass', array(
-    PDO::ATTR_PERSISTENT => true
-));
+$pdo = new PDO('mysql:host=tfb-database;dbname=hello_world', 'benchmarkdbuser', 'benchmarkdbpass', [
+    PDO::ATTR_PERSISTENT => true,
+    PDO::ATTR_EMULATE_PREPARES => false
+]);
 
 // Read number of queries to run from URL parameter
 $query_count = 1;
@@ -14,14 +15,14 @@ if ($_GET['queries'] > 1) {
 }
 
 // Create an array with the response string.
-$arr = array();
+$arr = [];
 
 // Define query
 $statement = $pdo->prepare('SELECT id,randomNumber FROM World WHERE id = ?');
 
 // For each query, store the result set values in the response array
 while (0 < $query_count--) {
-  $statement->execute(array( mt_rand(1, 10000)) );
+  $statement->execute( [mt_rand(1, 10000)] );
   
   // Store result in array.
   $arr[] = $statement->fetch(PDO::FETCH_ASSOC);

+ 2 - 2
frameworks/PHP/php/dbraw.php

@@ -3,9 +3,9 @@ header('Content-type: application/json');
 
 // Database connection
 // http://www.php.net/manual/en/ref.pdo-mysql.php
-$pdo = new PDO('mysql:host=tfb-database;dbname=hello_world', 'benchmarkdbuser', 'benchmarkdbpass', array(
+$pdo = new PDO('mysql:host=tfb-database;dbname=hello_world', 'benchmarkdbuser', 'benchmarkdbpass', [
     PDO::ATTR_PERSISTENT => true
-));
+]);
 
 // Define query
 $statement = $pdo->query( 'SELECT id,randomNumber FROM World WHERE id = '. mt_rand(1, 10000) );

+ 2 - 2
frameworks/PHP/php/fortune.php

@@ -5,9 +5,9 @@
 
 // Database connection
 // http://www.php.net/manual/en/ref.pdo-mysql.php
-$pdo = new PDO('mysql:host=tfb-database;dbname=hello_world', 'benchmarkdbuser', 'benchmarkdbpass', array(
+$pdo = new PDO('mysql:host=tfb-database;dbname=hello_world', 'benchmarkdbuser', 'benchmarkdbpass', [
     PDO::ATTR_PERSISTENT => true
-));
+]);
   
 // Define query and store result in array.
 $arr = $pdo->query( 'SELECT id, message FROM Fortune' )->fetchAll(PDO::FETCH_KEY_PAIR); 

+ 1 - 1
frameworks/PHP/php/json.php

@@ -4,4 +4,4 @@ header('Content-type: application/json');
 
 // Use the PHP standard JSON encoder.
 // http://www.php.net/manual/en/function.json-encode.php
-echo json_encode(array('message' => 'Hello, World!'));
+echo json_encode(['message' => 'Hello, World!']);

+ 8 - 7
frameworks/PHP/php/updateraw.php

@@ -3,9 +3,10 @@ header('Content-type: application/json');
 
 // Database connection
 // http://www.php.net/manual/en/ref.pdo-mysql.php
-$pdo = new PDO('mysql:host=tfb-database;dbname=hello_world', 'benchmarkdbuser', 'benchmarkdbpass', array(
-    PDO::ATTR_PERSISTENT => true
-));
+$pdo = new PDO('mysql:host=tfb-database;dbname=hello_world', 'benchmarkdbuser', 'benchmarkdbpass', [
+  PDO::ATTR_PERSISTENT => true,
+  PDO::ATTR_EMULATE_PREPARES => false
+]);
 
 // Read number of queries to run from URL parameter
 $query_count = 1;
@@ -14,7 +15,7 @@ if ($_GET['queries'] > 1) {
 }
 
 // Create an array with the response string.
-$arr = array();
+$arr = [];
 
 // Define query
 $statement = $pdo->prepare('SELECT randomNumber FROM World WHERE id = ?');
@@ -25,12 +26,12 @@ while (0 < $query_count--) {
   $id = mt_rand(1, 10000);
   $randomNumber = mt_rand(1, 10000);
 
-  $statement->execute(array($id));
+  $statement->execute( [$id] );
   
   // Store result in array.
-  $world = array('id' => $id, 'randomNumber' => $statement->fetchColumn());
+  $world = ['id' => $id, 'randomNumber' => $statement->fetchColumn()];
   $world['randomNumber'] = $randomNumber;
-  $updateStatement->execute(array($randomNumber, $id));
+  $updateStatement->execute([$randomNumber, $id]);
   
   $arr[] = $world;
 }