Skip to content

Commit 770ae13

Browse files
committed
update code by dbal 2.7 upgrade guide
1 parent 4f1fb31 commit 770ae13

File tree

8 files changed

+50
-44
lines changed

8 files changed

+50
-44
lines changed

src/ClickHouseConnection.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Doctrine\DBAL\ConnectionException;
1515
use ClickHouseDB\Client as Smi2CHClient;
16+
use Doctrine\DBAL\ParameterType;
1617
use Doctrine\DBAL\Platforms\AbstractPlatform;
1718

1819
/**
@@ -88,9 +89,9 @@ public function query()
8889
/**
8990
* {@inheritDoc}
9091
*/
91-
public function quote($input, $type = \PDO::PARAM_STR)
92+
public function quote($input, $type = ParameterType::STRING)
9293
{
93-
if (\PDO::PARAM_INT === $type) {
94+
if (ParameterType::INTEGER === $type) {
9495
return $input;
9596
}
9697

src/ClickHousePlatform.php

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Doctrine\DBAL\DBALException;
1515

16+
use Doctrine\DBAL\ParameterType;
1617
use Doctrine\DBAL\Types\BlobType;
1718
use Doctrine\DBAL\Types\DecimalType;
1819
use Doctrine\DBAL\Types\FloatType;
@@ -28,6 +29,7 @@
2829
use Doctrine\DBAL\Schema\Index;
2930
use Doctrine\DBAL\Schema\ForeignKeyConstraint;
3031
use Doctrine\DBAL\Schema\TableDiff;
32+
use Doctrine\DBAL\Platforms\TrimMode;
3133

3234
/**
3335
* Provides the behavior, features and SQL dialect of the ClickHouse database platform.
@@ -233,7 +235,7 @@ public function getModExpression($expression1, $expression2)
233235
/**
234236
* {@inheritDoc}
235237
*/
236-
public function getTrimExpression($str, $pos = self::TRIM_UNSPECIFIED, $char = false)
238+
public function getTrimExpression($str, $pos = TrimMode::UNSPECIFIED, $char = false)
237239
{
238240
throw DBALException::notSupported(__METHOD__);
239241
}
@@ -1032,12 +1034,12 @@ public function supportsGettingAffectedRows()
10321034
*/
10331035
protected function doModifyLimitQuery($query, $limit, $offset)
10341036
{
1035-
if (is_null($limit)) {
1037+
if (null === $limit) {
10361038
return $query;
10371039
}
10381040

10391041
$query .= ' LIMIT ';
1040-
if (!is_null($offset)) {
1042+
if (null !== $offset) {
10411043
$query .= $offset . ', ';
10421044
}
10431045

@@ -1097,7 +1099,7 @@ public function rollbackSavePoint($savepoint)
10971099
*/
10981100
protected function getReservedKeywordsClass()
10991101
{
1100-
return 'FOD\DBALClickHouse\ClickHouseKeywords';
1102+
return ClickHouseKeywords::class;
11011103
}
11021104

11031105
/**
@@ -1110,19 +1112,16 @@ public function getDefaultValueDeclarationSQL($field)
11101112
}
11111113

11121114
$default = " DEFAULT '" . $field['default'] . "'";
1113-
if (isset($field['type'])) {
1114-
if (in_array((string)$field['type'], [
1115+
if ($fieldType = (string)($field['type'] ?? null)) {
1116+
if (\in_array($fieldType, [
11151117
'Integer',
11161118
'SmallInt',
11171119
'Float'
1118-
]) || ('BigInt' === $field['type'] && \PDO::PARAM_INT === Type::getType('BigInt')->getBindingType())) {
1120+
]) || ('BigInt' === $fieldType && ParameterType::INTEGER === Type::getType('BigInt')->getBindingType())) {
11191121
$default = ' DEFAULT ' . $field['default'];
1120-
} elseif (in_array(
1121-
(string)$field['type'],
1122-
['DateTime']
1123-
) && $field['default'] === $this->getCurrentTimestampSQL()) {
1122+
} elseif ('DateTime' === $fieldType && $field['default'] === $this->getCurrentTimestampSQL()) {
11241123
$default = ' DEFAULT ' . $this->getCurrentTimestampSQL();
1125-
} elseif ('Date' === (string)$field['type']) { // TODO check if string matches constant date like 'dddd-yy-mm' and quote it
1124+
} elseif ('Date' === $fieldType) { // TODO check if string matches constant date like 'dddd-yy-mm' and quote it
11261125
$default = ' DEFAULT ' . $field['default'];
11271126
}
11281127
}
@@ -1136,17 +1135,17 @@ public function getDefaultValueDeclarationSQL($field)
11361135
public function getDoctrineTypeMapping($dbType)
11371136
{
11381137
// FixedString
1139-
if (strpos(strtolower($dbType), 'fixedstring') === 0) {
1138+
if (stripos($dbType, 'fixedstring') === 0) {
11401139
$dbType = 'fixedstring';
11411140
}
11421141

11431142
//Enum8
1144-
if (strpos(strtolower($dbType), 'enum8') === 0) {
1143+
if (stripos($dbType, 'enum8') === 0) {
11451144
$dbType = 'enum8';
11461145
}
11471146

11481147
//Enum16
1149-
if (strpos(strtolower($dbType), 'enum16') === 0) {
1148+
if (stripos($dbType, 'enum16') === 0) {
11501149
$dbType = 'enum16';
11511150
}
11521151
return parent::getDoctrineTypeMapping($dbType);

src/ClickHouseStatement.php

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
namespace FOD\DBALClickHouse;
1313

14+
use Doctrine\DBAL\FetchMode;
15+
use Doctrine\DBAL\ParameterType;
1416
use Doctrine\DBAL\Platforms\AbstractPlatform;
1517

1618
/**
@@ -60,7 +62,7 @@ class ClickHouseStatement implements \IteratorAggregate, \Doctrine\DBAL\Driver\S
6062
/**
6163
* @var integer
6264
*/
63-
private $fetchMode = \PDO::FETCH_BOTH;
65+
private $fetchMode = FetchMode::MIXED;
6466

6567
/**
6668
* @param \ClickHouseDB\Client $client
@@ -125,13 +127,12 @@ protected function assumeFetchMode($fetchMode = null)
125127
{
126128
$mode = $fetchMode ?: $this->fetchMode;
127129
if (!\in_array($mode, [
128-
\PDO::FETCH_ASSOC,
129-
\PDO::FETCH_NUM,
130-
\PDO::FETCH_BOTH,
131-
\PDO::FETCH_OBJ,
130+
FetchMode::ASSOCIATIVE,
131+
FetchMode::NUMERIC,
132+
FetchMode::STANDARD_OBJECT,
132133
\PDO::FETCH_KEY_PAIR,
133134
], true)) {
134-
$mode = \PDO::FETCH_BOTH;
135+
$mode = FetchMode::MIXED;
135136
}
136137

137138
return $mode;
@@ -150,15 +151,15 @@ public function fetch($fetchMode = null, $cursorOrientation = \PDO::FETCH_ORI_NE
150151

151152
$this->getIterator()->next();
152153

153-
if (\PDO::FETCH_NUM === $this->assumeFetchMode($fetchMode)) {
154+
if (FetchMode::NUMERIC === $this->assumeFetchMode($fetchMode)) {
154155
return array_values($data);
155156
}
156157

157-
if (\PDO::FETCH_BOTH === $this->assumeFetchMode($fetchMode)) {
158+
if (FetchMode::MIXED === $this->assumeFetchMode($fetchMode)) {
158159
return array_values($data) + $data;
159160
}
160161

161-
if (\PDO::FETCH_OBJ === $this->assumeFetchMode($fetchMode)) {
162+
if (FetchMode::STANDARD_OBJECT === $this->assumeFetchMode($fetchMode)) {
162163
return (object)$data;
163164
}
164165

@@ -178,14 +179,14 @@ public function fetch($fetchMode = null, $cursorOrientation = \PDO::FETCH_ORI_NE
178179
*/
179180
public function fetchAll($fetchMode = null, $fetchArgument = null, $ctorArgs = null)
180181
{
181-
if (\PDO::FETCH_NUM === $this->assumeFetchMode($fetchMode)) {
182+
if (FetchMode::NUMERIC === $this->assumeFetchMode($fetchMode)) {
182183
return array_map(
183184
'array_values',
184185
$this->rows
185186
);
186187
}
187188

188-
if (\PDO::FETCH_BOTH === $this->assumeFetchMode($fetchMode)) {
189+
if (FetchMode::MIXED === $this->assumeFetchMode($fetchMode)) {
189190
return array_map(
190191
function ($row) {
191192
return array_values($row) + $row;
@@ -194,7 +195,7 @@ function ($row) {
194195
);
195196
}
196197

197-
if (\PDO::FETCH_OBJ === $this->assumeFetchMode($fetchMode)) {
198+
if (FetchMode::STANDARD_OBJECT === $this->assumeFetchMode($fetchMode)) {
198199
return array_map(
199200
function ($row) {
200201
return (object)$row;
@@ -224,7 +225,7 @@ function ($row) {
224225
*/
225226
public function fetchColumn($columnIndex = 0)
226227
{
227-
if ($elem = $this->fetch(\PDO::FETCH_NUM)) {
228+
if ($elem = $this->fetch(FetchMode::NUMERIC)) {
228229
return $elem[$columnIndex] ?? $elem[0];
229230
}
230231

@@ -342,10 +343,10 @@ protected function getTypedParam($key)
342343
// if param type was not setted - trying to get db-type by php-var-type
343344
if (null === $type) {
344345
if (\is_bool($this->values[$key])) {
345-
$type = \PDO::PARAM_BOOL;
346+
$type = ParameterType::BOOLEAN;
346347
} else {
347348
if (\is_int($this->values[$key]) || \is_float($this->values[$key])) {
348-
$type = \PDO::PARAM_INT;
349+
$type = ParameterType::INTEGER;
349350
} else {
350351
if (\is_array($this->values[$key])) {
351352

@@ -372,15 +373,15 @@ function ($value) {
372373
}
373374
}
374375

375-
if (\PDO::PARAM_NULL === $type) {
376+
if (ParameterType::NULL === $type) {
376377
throw new ClickHouseException('NULLs are not supported by ClickHouse');
377378
}
378379

379-
if (\PDO::PARAM_INT === $type) {
380+
if (ParameterType::INTEGER === $type) {
380381
return $this->values[$key];
381382
}
382383

383-
if (\PDO::PARAM_BOOL === $type) {
384+
if (ParameterType::BOOLEAN === $type) {
384385
return (int)(bool)$this->values[$key];
385386
}
386387

src/Types/ArrayDateTimeType.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace FOD\DBALClickHouse\Types;
1313

14+
use Doctrine\DBAL\ParameterType;
1415
use Doctrine\DBAL\Platforms\AbstractPlatform;
1516

1617
/**
@@ -67,6 +68,6 @@ public function convertToDatabaseValue($value, AbstractPlatform $platform)
6768
*/
6869
public function getBindingType()
6970
{
70-
return \PDO::PARAM_INT;
71+
return ParameterType::INTEGER;
7172
}
7273
}

src/Types/ArrayDateType.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace FOD\DBALClickHouse\Types;
1313

14+
use Doctrine\DBAL\ParameterType;
1415
use Doctrine\DBAL\Platforms\AbstractPlatform;
1516

1617
/**
@@ -67,6 +68,6 @@ public function convertToDatabaseValue($value, AbstractPlatform $platform)
6768
*/
6869
public function getBindingType()
6970
{
70-
return \PDO::PARAM_INT;
71+
return ParameterType::INTEGER;
7172
}
7273
}

src/Types/ArrayStringType.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace FOD\DBALClickHouse\Types;
1313

14+
use Doctrine\DBAL\ParameterType;
1415
use Doctrine\DBAL\Platforms\AbstractPlatform;
1516

1617
/**
@@ -56,6 +57,6 @@ function ($value) use ($platform) {
5657
*/
5758
public function getBindingType()
5859
{
59-
return \PDO::PARAM_INT;
60+
return ParameterType::INTEGER;
6061
}
6162
}

src/Types/BigIntType.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace FOD\DBALClickHouse\Types;
1313

14+
use Doctrine\DBAL\ParameterType;
1415
use Doctrine\DBAL\Platforms\AbstractPlatform;
1516

1617
/**
@@ -25,7 +26,7 @@ class BigIntType extends \Doctrine\DBAL\Types\BigIntType
2526
*/
2627
public function getBindingType()
2728
{
28-
return \PDO::PARAM_INT;
29+
return ParameterType::INTEGER;
2930
}
3031

3132
/**

tests/SelectTest.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace FOD\DBALClickHouse\Tests;
1313

14+
use Doctrine\DBAL\FetchMode;
1415
use FOD\DBALClickHouse\Connection;
1516
use PHPUnit\Framework\TestCase;
1617

@@ -65,7 +66,7 @@ public function testFetchAssocSelect()
6566
{
6667
$results = [];
6768
$stmt = $this->connection->query('SELECT id, hits FROM test_select_table WHERE id IN (3, 4)');
68-
while ($result = $stmt->fetch(\PDO::FETCH_ASSOC)) {
69+
while ($result = $stmt->fetch(FetchMode::ASSOCIATIVE)) {
6970
$results[] = $result;
7071
}
7172
$this->assertEquals([['id' => 3, 'hits' => 303], ['id' => 4, 'hits' => 404]], $results);
@@ -74,14 +75,14 @@ public function testFetchAssocSelect()
7475
public function testFetchNumSelect()
7576
{
7677
$stmt = $this->connection->query('SELECT MAX(hits) FROM test_select_table');
77-
$result = $stmt->fetch(\PDO::FETCH_ASSOC);
78+
$result = $stmt->fetch(FetchMode::ASSOCIATIVE);
7879
$this->assertEquals(['MAX(hits)' => 505], $result);
7980
}
8081

8182
public function testFetchObjSelect()
8283
{
8384
$stmt = $this->connection->query('SELECT MAX(hits) as maxHits FROM test_select_table');
84-
$result = $stmt->fetch(\PDO::FETCH_OBJ);
85+
$result = $stmt->fetch(FetchMode::STANDARD_OBJECT);
8586
$this->assertEquals((object)['maxHits' => 505], $result);
8687
}
8788

@@ -111,15 +112,15 @@ public function testFetchAllBothSelect()
111112
public function testFetchAllNumSelect()
112113
{
113114
$stmt = $this->connection->query("SELECT AVG(hits) FROM test_select_table");
114-
$result = $stmt->fetchAll(\PDO::FETCH_NUM);
115+
$result = $stmt->fetchAll(FetchMode::NUMERIC);
115116

116117
$this->assertEquals([[303]], $result);
117118
}
118119

119120
public function testFetchAllObjSelect()
120121
{
121122
$stmt = $this->connection->query("SELECT * FROM test_select_table WHERE id IN (2, 4)");
122-
$result = $stmt->fetchAll(\PDO::FETCH_OBJ);
123+
$result = $stmt->fetchAll(FetchMode::STANDARD_OBJECT);
123124

124125
$this->assertEquals([(object)[
125126
'id' => 2,

0 commit comments

Comments
 (0)