diff --git a/src/Query/SqlQuery.php b/src/Query/SqlQuery.php index 23aad03..ac01f35 100644 --- a/src/Query/SqlQuery.php +++ b/src/Query/SqlQuery.php @@ -2,6 +2,7 @@ namespace Gt\Database\Query; use Gt\Database\Connection\Connection; +use Gt\Database\Connection\Driver; use PDO; use PDOException; use PDOStatement; @@ -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; } @@ -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; } @@ -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; diff --git a/test/unit/Migration/MigratorTest.php b/test/unit/Migration/MigratorTest.php index d077fdd..4537a7c 100644 --- a/test/unit/Migration/MigratorTest.php +++ b/test/unit/Migration/MigratorTest.php @@ -282,6 +282,7 @@ public function testForcedMigration(array $fileList) { /** * @dataProvider dataMigrationFileList + * @runInSeparateProcess */ public function testPerformMigrationGood(array $fileList):void { $path = $this->getMigrationDirectory(); diff --git a/test/unit/Query/SqlQueryTest.php b/test/unit/Query/SqlQueryTest.php index c70d45f..035b3ff 100644 --- a/test/unit/Query/SqlQueryTest.php +++ b/test/unit/Query/SqlQueryTest.php @@ -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(