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

Binding arrays #30

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
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());
}
}