diff --git a/README.md b/README.md index 6304a45..bb44990 100644 --- a/README.md +++ b/README.md @@ -166,6 +166,22 @@ while ($row = $stmt->fetch()) { } ``` +### Select using placeholders with arrays as values +```php +$stmt = $conn->prepare('SELECT authorId FROM articles WHERE categoryId IN (:categoryIds)'); + +$stmt->bindValue('categoryIds', [123, 124], \Doctrine\DBAL\Connection::PARAM_INT_ARRAY); +$stmt->execute(); +$result = $stmt->fetchAll(); +``` +```php +$stmt = $conn->prepare('SELECT authorId FROM articles WHERE categoryName IN (:categoryNames)'); + +$stmt->bindValue('categoryNames', ['Auto', 'News'], \Doctrine\DBAL\Connection::PARAM_STR_ARRAY); +$stmt->execute(); +$result = $stmt->fetchAll(); +``` + ### Additional types If you want to use [Array(T) type](https://clickhouse.yandex/reference_en.html#Array(T)), register additional DBAL types in your code: diff --git a/composer.json b/composer.json index 0c0789a..3dc816d 100644 --- a/composer.json +++ b/composer.json @@ -25,7 +25,7 @@ "ext-PDO": "*", "ext-pcre": "*", "doctrine/dbal": "^2.7", - "smi2/phpClickHouse": "^1.0" + "smi2/phpclickhouse": "^1.0" }, "require-dev": { "doctrine/coding-standard": "^4.0", diff --git a/src/ClickHouseStatement.php b/src/ClickHouseStatement.php index 9587257..7343d63 100644 --- a/src/ClickHouseStatement.php +++ b/src/ClickHouseStatement.php @@ -15,6 +15,7 @@ namespace FOD\DBALClickHouse; use ClickHouseDB\Client; +use Doctrine\DBAL\Connection; use Doctrine\DBAL\Driver\Statement; use Doctrine\DBAL\FetchMode; use Doctrine\DBAL\ParameterType; @@ -387,6 +388,14 @@ function ($value) : void { return (string) (int) (bool) $this->values[$key]; } + if ($type === Connection::PARAM_INT_ARRAY) { + return implode(',', $this->values[$key]); + } + + if ($type === Connection::PARAM_STR_ARRAY) { + return "'" . implode("','", $this->values[$key]) . "'"; + } + return $this->platform->quoteStringLiteral((string) $this->values[$key]); } } diff --git a/tests/SelectTest.php b/tests/SelectTest.php index 52e1dba..c50649b 100644 --- a/tests/SelectTest.php +++ b/tests/SelectTest.php @@ -284,5 +284,27 @@ public function testTrimChar() $this->assertEquals('t2', $stmt->fetchColumn()); } + + public function testStatementSelectWithBindingIntegersArray(): void + { + $statement = $this->connection->prepare('INSERT INTO test_select_table(id, payload) VALUES (:v0, :v1), (:v2, :v3)'); + $statement->execute(['v0' => 11, 'v1' => 'v?11', 'v2' => 12, 'v3' => 'v12']); + + $statement = $this->connection->prepare('SELECT payload from test_select_table WHERE id IN (:array) ORDER BY id'); + $statement->bindValue('array', [11, 12], Connection::PARAM_INT_ARRAY); + $statement->execute(); + $this->assertEquals([['payload' => 'v?11'], ['payload' => 'v12']], $statement->fetchAll()); + } + + public function testStatementSelectWithBindingStringsArray(): void + { + $statement = $this->connection->prepare('INSERT INTO test_select_table(id, payload) VALUES (:v0, :v1), (:v2, :v3)'); + $statement->execute(['v0' => 13, 'v1' => 'v?13', 'v2' => 14, 'v3' => 'v14']); + + $statement = $this->connection->prepare('SELECT id from test_select_table WHERE payload IN (:array) ORDER BY id'); + $statement->bindValue('array', ['v?13', 'v14'], Connection::PARAM_STR_ARRAY); + $statement->execute(); + $this->assertEquals([['id' => 13], ['id' => 14]], $statement->fetchAll()); + } }