Skip to content

Commit

Permalink
Merge pull request #92 from PhpGt/parameter-flattening
Browse files Browse the repository at this point in the history
Parameter flattening
  • Loading branch information
g105b authored May 26, 2018
2 parents 698d283 + c802447 commit 5bf813b
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 32 deletions.
37 changes: 37 additions & 0 deletions src/Query/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,41 @@ 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 at this point.
*/
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])) {
$merged = [];
foreach($b as $innerKey => $innerValue) {
$merged = array_merge(
$merged,
$innerValue
);
}

$b = $merged;
}

$flatArray = array_merge($flatArray, $b);
}

return $flatArray;
}
}
2 changes: 1 addition & 1 deletion src/Query/QueryCollectionFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down
32 changes: 3 additions & 29 deletions src/Query/SqlQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,34 +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);
}

/**
* $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;
|| array_keys($bindings) === range(
0,
count($bindings) - 1);
}
}
30 changes: 28 additions & 2 deletions test/unit/IntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit 5bf813b

Please sign in to comment.