From 9739f2c79b097b9ac1c0af1ba8bcfe010f1fb685 Mon Sep 17 00:00:00 2001 From: Greg Bowler Date: Fri, 25 May 2018 16:57:21 +0100 Subject: [PATCH 1/2] Extract parameter flattening to abstract Query class --- src/Query/Query.php | 29 +++++++++++++++++++++++++++++ src/Query/SqlQuery.php | 28 ---------------------------- 2 files changed, 29 insertions(+), 28 deletions(-) diff --git a/src/Query/Query.php b/src/Query/Query.php index edf9f34..19972c4 100644 --- a/src/Query/Query.php +++ b/src/Query/Query.php @@ -23,4 +23,33 @@ public function getFilePath():string { } abstract public function execute(array $bindings = []):ResultSet; + + /** + * $bindings can either be : + * 1) An array of individual values for binding to the question mark placeholder, + * passed in as variable arguments. + * 2) An array containing subarrays containing key-value-pairs for binding to + * named placeholders. + * + * Due to the use of variable arguments on the Database and QueryCollection classes, + * key-value-pair bindings may be double or triple nested. + */ + protected function flattenBindings(array $bindings):array { + if(!isset($bindings[0]) + || !is_array($bindings[0])) { + return $bindings; + } + + $flatArray = []; + foreach($bindings as $i => $b) { + while(isset($b[0]) + && is_array($b[0])) { + $b = $b[0]; + } + + $flatArray = array_merge($flatArray, $b); + } + + return $flatArray; + } } \ No newline at end of file diff --git a/src/Query/SqlQuery.php b/src/Query/SqlQuery.php index 747d03c..84be5bb 100644 --- a/src/Query/SqlQuery.php +++ b/src/Query/SqlQuery.php @@ -128,32 +128,4 @@ protected function bindingsEmptyOrNonAssociative(array $bindings):bool { $bindings === [] || array_keys($bindings) === range(0, count($bindings) - 1); } - - /** - * $bindings can either be : - * 1) An array of individual values for binding to the question mark placeholder, - * passed in as variable arguments. - * 2) An array containing one single subarray containing key-value-pairs for binding to - * named placeholders. - * - * Due to the use of variable arguments on the Database and QueryCollection classes, - * key-value-pair bindings may be double or triple nested. - */ - protected function flattenBindings(array $bindings):array { - if(!isset($bindings[0])) { - return $bindings; - } - - $flatArray = []; - foreach($bindings as $i => $b) { - while(isset($b[0]) - && is_array($b[0])) { - $b = $b[0]; - } - - $flatArray = array_merge($flatArray, $b); - } - - return $flatArray; - } } From 0764151dbe1892cc54557840edb2529a9a3ff97c Mon Sep 17 00:00:00 2001 From: Greg Bowler Date: Fri, 25 May 2018 17:11:58 +0100 Subject: [PATCH 2/2] Merge multiple associative arrays passed into query as var args --- src/Query/Query.php | 14 ++++++++++--- src/Query/QueryCollectionFactory.php | 2 +- src/Query/SqlQuery.php | 4 +++- test/unit/IntegrationTest.php | 30 ++++++++++++++++++++++++++-- 4 files changed, 43 insertions(+), 7 deletions(-) diff --git a/src/Query/Query.php b/src/Query/Query.php index 19972c4..d3de8bf 100644 --- a/src/Query/Query.php +++ b/src/Query/Query.php @@ -32,7 +32,7 @@ abstract public function execute(array $bindings = []):ResultSet; * named placeholders. * * Due to the use of variable arguments on the Database and QueryCollection classes, - * key-value-pair bindings may be double or triple nested. + * key-value-pair bindings may be double or triple nested at this point. */ protected function flattenBindings(array $bindings):array { if(!isset($bindings[0]) @@ -43,8 +43,16 @@ protected function flattenBindings(array $bindings):array { $flatArray = []; foreach($bindings as $i => $b) { while(isset($b[0]) - && is_array($b[0])) { - $b = $b[0]; + && is_array($b[0])) { + $merged = []; + foreach($b as $innerKey => $innerValue) { + $merged = array_merge( + $merged, + $innerValue + ); + } + + $b = $merged; } $flatArray = array_merge($flatArray, $b); diff --git a/src/Query/QueryCollectionFactory.php b/src/Query/QueryCollectionFactory.php index 83b489a..868cb30 100644 --- a/src/Query/QueryCollectionFactory.php +++ b/src/Query/QueryCollectionFactory.php @@ -45,7 +45,7 @@ public function directoryExists(string $name):bool { * @param string $name Name of the QueryCollection * @return string Absolute path to directory */ - protected function locateDirectory(string $name)/* :?string */ { + protected function locateDirectory(string $name):?string { foreach(new DirectoryIterator($this->basePath) as $fileInfo) { if($fileInfo->isDot() || !$fileInfo->isDir()) { diff --git a/src/Query/SqlQuery.php b/src/Query/SqlQuery.php index 84be5bb..04ec1a4 100644 --- a/src/Query/SqlQuery.php +++ b/src/Query/SqlQuery.php @@ -126,6 +126,8 @@ protected function removeUnusedBindings(array $bindings, string $sql):array { protected function bindingsEmptyOrNonAssociative(array $bindings):bool { return $bindings === [] - || array_keys($bindings) === range(0, count($bindings) - 1); + || array_keys($bindings) === range( + 0, + count($bindings) - 1); } } diff --git a/test/unit/IntegrationTest.php b/test/unit/IntegrationTest.php index 05b7bcd..50cfffc 100644 --- a/test/unit/IntegrationTest.php +++ b/test/unit/IntegrationTest.php @@ -130,13 +130,39 @@ public function testMultipleParameterUsage() { "number" => 55, ]); - $rqr = $this->db->executeSql("SELECT id, name FROM test_table"); - static::assertEquals(1, $result1->id); static::assertEquals(2, $result2->id); static::assertNull($resultNull); } + public function testMultipleArrayParameterUsage() { + $queryCollectionPath = $this->queryBase . "/exampleCollection"; + $getByNameNumberQueryPath = $queryCollectionPath . "/getByNameNumber.sql"; + + mkdir($queryCollectionPath, 0775, true); + file_put_contents( + $getByNameNumberQueryPath, + "SELECT id, name, number FROM test_table WHERE name = :name and number = :number" + ); + + $result1 = $this->db->fetch("exampleCollection/getByNameNumber", [ + "name" => "one", + "number" => 1, + ]); + $result2 = $this->db->fetch("exampleCollection/getByNameNumber", + [ "name" => "two"] , + ["number" => 2] + ); + $result3 = $this->db->fetch("exampleCollection/getByNameNumber", [ + ["name" => "three"], + ["number" => 3], + ]); + + static::assertEquals(1, $result1->id); + static::assertEquals(2, $result2->id); + static::assertEquals(3, $result3->id); + } + private function settingsSingleton():Settings { if(is_null($this->settings)) { $this->settings = new Settings(