Skip to content

Commit

Permalink
fix: Implement ability to bind values for arrays
Browse files Browse the repository at this point in the history
Implement binding for Connection::PARAM_INT_ARRAY and Connection::PARAM_STR_ARRAY types
  • Loading branch information
nafigator authored and Alexander Yancharuk committed Nov 4, 2020
1 parent 24eb461 commit ee5c7db
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 0 deletions.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,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:
Expand Down
9 changes: 9 additions & 0 deletions src/ClickHouseStatement.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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]);
}
}
22 changes: 22 additions & 0 deletions tests/SelectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}

0 comments on commit ee5c7db

Please sign in to comment.