Skip to content

Commit

Permalink
Merge pull request #127 from PhpGt/126-in
Browse files Browse the repository at this point in the history
Implement bound parameters with array values
  • Loading branch information
g105b authored Nov 8, 2018
2 parents 98098e2 + ecbd9f1 commit 04f0831
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 1 deletion.
32 changes: 31 additions & 1 deletion src/Query/SqlQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
namespace Gt\Database\Query;

use Gt\Database\Connection\Connection;
use Gt\Database\Connection\Driver;
use PDO;
use PDOException;
use PDOStatement;
Expand All @@ -18,7 +19,10 @@ class SqlQuery extends Query {

public function getSql(array $bindings = []):string {
$sql = file_get_contents($this->getFilePath());
$sql = $this->injectSpecialBindings($sql, $bindings);
$sql = $this->injectSpecialBindings(
$sql,
$bindings
);

return $sql;
}
Expand Down Expand Up @@ -86,6 +90,25 @@ public function injectSpecialBindings(
unset($bindings[$special]);
}

foreach($bindings as $key => $value) {
if(is_array($value)) {
$inString = "";

foreach($value as $i => $innerValue) {
$newKey = $key . "__" . $i;
$keyParamString = ":$newKey";
$inString .= "$keyParamString, ";
}

$inString = rtrim($inString, " ,");
$sql = str_replace(
":$key",
$inString,
$sql
);
}
}

return $sql;
}

Expand All @@ -97,6 +120,13 @@ public function prepareBindings(array $bindings):array {
if($value instanceof DateTime) {
$bindings[$key] = $value->format("Y-m-d H:i:s");
}
if(is_array($value)) {
foreach($value as $i => $innerValue) {
$newKey = $key . "__" . $i;
$bindings[$newKey] = $innerValue;
}
unset($bindings[$key]);
}
}

return $bindings;
Expand Down
1 change: 1 addition & 0 deletions test/unit/Migration/MigratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,7 @@ public function testForcedMigration(array $fileList) {

/**
* @dataProvider dataMigrationFileList
* @runInSeparateProcess
*/
public function testPerformMigrationGood(array $fileList):void {
$path = $this->getMigrationDirectory();
Expand Down
77 changes: 77 additions & 0 deletions test/unit/Query/SqlQueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,83 @@ public function testSpecialBindingsAscDesc(
self::assertContains("order by sortColumn desc", $injectedSql);
}

/**
* @dataProvider \Gt\Database\Test\Helper\Helper::queryPathNotExistsProvider()
*/
public function testSpecialBindingsInClause(
string $queryName,
string $queryCollectionPath,
string $filePath
) {
$sql = "select * from something where `status` in (:statusList)";
file_put_contents($filePath, $sql);
$query = new SqlQuery($filePath, $this->driverSingleton());
$bindings = [
"statusList" => [
"good",
"very-good",
"excellent"
],
];
$injectedSql = $query->injectSpecialBindings(
$sql,
$bindings
);

for($i = 0; $i < count($bindings["statusList"]); $i++) {
self::assertContains(
"statusList__$i",
$injectedSql
);
}

$i++;

self::assertNotContains(
"statusList__$i",
$injectedSql
);
}

/**
* @dataProvider \Gt\Database\Test\Helper\Helper::queryPathNotExistsProvider()
*/
public function testPrepareBindingsWithArray(
string $queryName,
string $queryCollectionPath,
string $filePath
) {
$sql = "select * from something where `status` in (:statusList)";
file_put_contents($filePath, $sql);
$query = new SqlQuery($filePath, $this->driverSingleton());
$bindings = [
"statusList" => [
"good",
"very-good",
"excellent"
],
];
$preparedBindings = $query->prepareBindings(
$bindings
);

self::assertArrayHasKey("statusList", $bindings);
self::assertArrayNotHasKey("statusList", $preparedBindings);

foreach($bindings["statusList"] as $i => $binding) {
self::assertArrayHasKey(
"statusList__$i",
$preparedBindings
);
}

$i++;
self::assertArrayNotHasKey(
"statusList__$i",
$preparedBindings
);
}

private function driverSingleton():Driver {
if(is_null($this->driver)) {
$settings = new Settings(
Expand Down

0 comments on commit 04f0831

Please sign in to comment.