Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parameter flattening #92

Merged
merged 3 commits into from
May 26, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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