Skip to content

Commit eed5642

Browse files
JulianMaramotl
authored andcommitted
DBAL4: Making a start
1 parent 2d084bb commit eed5642

File tree

19 files changed

+203
-103
lines changed

19 files changed

+203
-103
lines changed

.github/workflows/tests.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ jobs:
2222
matrix:
2323
os: [ 'ubuntu-latest' ] #, macos-latest, windows-latest ]
2424
php-version: [
25-
'8.0',
2625
'8.1',
2726
'8.2',
2827
'8.3',

README.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ CrateDB DBAL Driver
2222
:target: https://packagist.org/packages/crate/crate-dbal
2323
:alt: Latest stable version
2424

25-
.. image:: https://img.shields.io/badge/PHP-8.0%2C%208.1%2C%208.2%2C%208.3%2C%208.4%2C%208.5-green.svg
25+
.. image:: https://img.shields.io/badge/PHP-8.1%2C%208.2%2C%208.3%2C%208.4%2C%208.5-green.svg
2626
:target: https://packagist.org/packages/crate/crate-dbal
2727
:alt: Supported PHP versions
2828

composer.json

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
"minimum-stability": "dev",
1616
"prefer-stable": true,
1717
"require": {
18-
"php": "^8.0|^8.1|^8.2|^8.3|^8.4|^8.5",
19-
"doctrine/dbal": "^3",
18+
"php": "^8.1|^8.2|^8.3|^8.4|^8.5",
19+
"doctrine/dbal": "^4",
2020
"crate/crate-pdo": "^2.2.4",
2121
"ext-pdo": "*"
2222
},
@@ -27,7 +27,9 @@
2727
},
2828
"require-dev": {
2929
"phpunit/phpunit": "^9.0",
30-
"friendsofphp/php-cs-fixer": "^3.89"
30+
"friendsofphp/php-cs-fixer": "^3.89",
31+
"slam/dbal-debugstack-middleware": "*",
32+
"squizlabs/php_codesniffer": "^3.5"
3133
},
3234
"autoload-dev": {
3335
"psr-0": {
@@ -44,7 +46,7 @@
4446
},
4547
"scripts": {
4648
"test": "XDEBUG_MODE=coverage phpunit --coverage-clover build/logs/clover.xml",
47-
"check-style": "php-cs-fixer check",
48-
"fix-style": "php-cs-fixer fix"
49+
"check-style": "phpcs && php-cs-fixer check --verbose --diff",
50+
"fix-style": "phpcbf && php-cs-fixer fix"
4951
}
5052
}

src/Crate/DBAL/Driver/PDOCrate/CrateStatement.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,9 @@ public function rowCount(): int
9292
/**
9393
* {@inheritDoc}
9494
*/
95-
public function bindValue($param, $value, $type = ParameterType::STRING): bool
95+
public function bindValue($param, $value, $type = ParameterType::STRING): void
9696
{
97-
return $this->stmt->bindValue($param, $value, $type);
97+
$this->stmt->bindValue($param, $value, $type);
9898
}
9999

100100
/**

src/Crate/DBAL/Driver/PDOCrate/Driver.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,13 @@
2828
use Crate\DBAL\Platforms\CratePlatform4;
2929
use Crate\DBAL\Schema\CrateSchemaManager;
3030
use Doctrine\DBAL\Connection;
31+
use Doctrine\DBAL\Driver as DoctrineDriver;
3132
use Doctrine\DBAL\Platforms\AbstractPlatform;
3233
use Doctrine\DBAL\Schema\AbstractSchemaManager;
33-
use Doctrine\DBAL\VersionAwarePlatformDriver;
34+
use Doctrine\DBAL\ServerVersionProvider;
3435
use SensitiveParameter;
3536

36-
class Driver implements VersionAwarePlatformDriver
37+
class Driver implements DoctrineDriver
3738
{
3839
public const VERSION = '4.0.3';
3940
public const NAME = 'crate';
@@ -75,8 +76,9 @@ private function constructPdoDsn(array $params): string
7576

7677
/**
7778
* {@inheritDoc}
79+
* @param ServerVersionProvider $versionProvider
7880
*/
79-
public function getDatabasePlatform(): AbstractPlatform
81+
public function getDatabasePlatform(ServerVersionProvider $versionProvider): AbstractPlatform
8082
{
8183
return new CratePlatform4();
8284
}
@@ -103,7 +105,7 @@ public function getName(): string
103105
*/
104106
public function getDatabase(Connection $conn): string|null
105107
{
106-
return null;
108+
return 'doc';
107109
}
108110

109111
/**

src/Crate/DBAL/Driver/PDOCrate/PDOConnection.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -116,28 +116,28 @@ public function lastInsertId($name = null): string
116116
}
117117
}
118118

119-
public function beginTransaction()
119+
public function beginTransaction(): void
120120
{
121121
try {
122-
return $this->connection->beginTransaction();
122+
$this->connection->beginTransaction();
123123
} catch (PDOException $exception) {
124124
throw Exception::new($exception);
125125
}
126126
}
127127

128-
public function commit()
128+
public function commit(): void
129129
{
130130
try {
131-
return $this->connection->commit();
131+
$this->connection->commit();
132132
} catch (PDOException $exception) {
133133
throw Exception::new($exception);
134134
}
135135
}
136136

137-
public function rollBack()
137+
public function rollBack(): void
138138
{
139139
try {
140-
return $this->connection->rollBack();
140+
$this->connection->rollBack();
141141
} catch (PDOException $exception) {
142142
throw Exception::new($exception);
143143
}

src/Crate/DBAL/Platforms/CratePlatform.php

Lines changed: 79 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,17 @@
2323

2424
namespace Crate\DBAL\Platforms;
2525

26+
use Crate\DBAL\Platforms\Keywords\CrateKeywords;
27+
use Crate\DBAL\Schema\CrateSchemaManager;
28+
use Crate\DBAL\Types\ArrayType;
2629
use Crate\DBAL\Types\MapType;
2730
use Crate\DBAL\Types\TimestampType;
28-
use Doctrine\DBAL\Event\SchemaCreateTableColumnEventArgs;
29-
use Doctrine\DBAL\Event\SchemaCreateTableEventArgs;
30-
use Doctrine\DBAL\Events;
31-
use Doctrine\DBAL\Exception as DBALException;
31+
use Doctrine\DBAL\Connection;
3232
use Doctrine\DBAL\Platforms\AbstractPlatform;
33+
use Doctrine\DBAL\Platforms\DateIntervalUnit;
34+
use Doctrine\DBAL\Platforms\Exception\NoColumnsSpecifiedForTable;
35+
use Doctrine\DBAL\Platforms\Keywords\KeywordList;
36+
use Doctrine\DBAL\Schema\AbstractSchemaManager;
3337
use Doctrine\DBAL\Schema\ForeignKeyConstraint;
3438
use Doctrine\DBAL\Schema\Identifier;
3539
use Doctrine\DBAL\Schema\Index;
@@ -52,18 +56,22 @@ public function __construct()
5256
{
5357
$this->initializeDoctrineTypeMappings();
5458
if (!Type::hasType(MapType::NAME)) {
55-
Type::addType(MapType::NAME, 'Crate\DBAL\Types\MapType');
59+
Type::addType(MapType::NAME, MapType::class);
60+
$this->registerDoctrineTypeMapping(10, MapType::NAME);
5661
}
5762
if (!Type::hasType(TimestampType::NAME)) {
58-
Type::addType(TimestampType::NAME, 'Crate\DBAL\Types\TimestampType');
63+
Type::addType(TimestampType::NAME, TimestampType::class);
64+
}
65+
if (!Type::hasType(ArrayType::NAME)) {
66+
Type::addType(ArrayType::NAME, ArrayType::class);
67+
$this->registerDoctrineTypeMapping(9, ArrayType::NAME);
5968
}
60-
Type::overrideType('array', 'Crate\DBAL\Types\ArrayType');
6169
}
6270

6371
/**
6472
* {@inheritDoc}
6573
*/
66-
public function getDefaultTransactionIsolationLevel()
74+
public function getDefaultTransactionIsolationLevel(): TransactionIsolationLevel
6775
{
6876
// CrateDB provides `READ_UNCOMMITTED` isolation levels.
6977
// https://github.com/crate/crate/blob/master/server/src/main/java/io/crate/analyze/ShowStatementAnalyzer.java#L69-L85
@@ -268,26 +276,26 @@ public function getAlterTableSQL(TableDiff $diff): array
268276
$commentsSQL = array();
269277
$columnSql = array();
270278

271-
foreach ($diff->addedColumns as $column) {
279+
foreach ($diff->getAddedColumns() as $column) {
272280
if ($this->onSchemaAlterTableAddColumn($column, $diff, $columnSql)) {
273281
continue;
274282
}
275283

276284
$query = 'ADD ' . $this->getColumnDeclarationSQL($column->getQuotedName($this), $column->toArray());
277285
$sql[] = 'ALTER TABLE ' . $diff->name . ' ' . $query;
278-
if ($comment = $this->getColumnComment($column)) {
286+
if ($comment = $column->getComment()) {
279287
$commentsSQL[] = $this->getCommentOnColumnSQL($diff->name, $column->getName(), $comment);
280288
}
281289
}
282290

283-
if (count($diff->removedColumns) > 0) {
284-
throw DBALException::notSupported("Alter Table: drop columns");
291+
if (count($diff->getDroppedColumns()) > 0) {
292+
throw Exception::notSupported("Alter Table: drop columns");
285293
}
286-
if (count($diff->changedColumns) > 0) {
287-
throw DBALException::notSupported("Alter Table: change column options");
294+
if (count($diff->getChangedColumns()) > 0) {
295+
throw Exception::notSupported("Alter Table: change column options");
288296
}
289-
if (count($diff->renamedColumns) > 0) {
290-
throw DBALException::notSupported("Alter Table: rename columns");
297+
if (count($diff->getRenamedColumns()) > 0) {
298+
throw Exception::notSupported("Alter Table: rename columns");
291299
}
292300

293301
$tableSql = array();
@@ -309,7 +317,7 @@ public function getAlterTableSQL(TableDiff $diff): array
309317
public function getColumnDeclarationSQL($name, array $column): string
310318
{
311319
if (isset($column['columnDefinition'])) {
312-
$columnDef = $this->getCustomTypeDeclarationSQL($column);
320+
$columnDef = $column['columnDefinition'];
313321
} else {
314322
$typeDecl = $column['type']->getSqlDeclaration($column, $this);
315323
$columnDef = $typeDecl;
@@ -324,10 +332,10 @@ public function getColumnDeclarationSQL($name, array $column): string
324332
* @param Index $index
325333
* @codeCoverageIgnore
326334
*/
327-
public function getIndexDeclarationSQL($name, Index $index): string
335+
public function getIndexDeclarationSQL(Index $index): string
328336
{
329337
$columns = $index->getQuotedColumns($this);
330-
$name = new Identifier($name);
338+
$name = new Identifier($index->getName());
331339

332340
if (count($columns) == 0) {
333341
throw new \InvalidArgumentException("Incomplete definition. 'columns' required.");
@@ -457,7 +465,7 @@ protected function _getCommonIntegerTypeDeclarationSQL(array $columnDef): string
457465
* @param false|int $length
458466
* @param $fixed
459467
*/
460-
protected function getVarcharTypeDeclarationSQLSnippet($length, $fixed): string
468+
protected function getVarcharTypeDeclarationSQLSnippet($length): string
461469
{
462470
return 'STRING';
463471
}
@@ -612,7 +620,7 @@ public function getCreateTableSQL(Table $table, $createFlags = self::CREATE_INDE
612620
}
613621

614622
if (count($table->getColumns()) === 0) {
615-
throw DBALException::noColumnsSpecifiedForTable($table->getName());
623+
throw NoColumnsSpecifiedForTable::new($table->getName());
616624
}
617625

618626
$tableName = $table->getQuotedName($this);
@@ -644,30 +652,9 @@ public function getCreateTableSQL(Table $table, $createFlags = self::CREATE_INDE
644652
$columns = array();
645653

646654
foreach ($table->getColumns() as $column) {
647-
if (null !== $this->_eventManager &&
648-
$this->_eventManager->hasListeners(Events::onSchemaCreateTableColumn)
649-
) {
650-
$eventArgs = new SchemaCreateTableColumnEventArgs($column, $table, $this);
651-
$this->_eventManager->dispatchEvent(Events::onSchemaCreateTableColumn, $eventArgs);
652-
653-
$columnSql = array_merge($columnSql, $eventArgs->getSql());
654-
655-
if ($eventArgs->isDefaultPrevented()) {
656-
continue;
657-
}
658-
}
659655
$columns[$column->getQuotedName($this)] = self::prepareColumnData($this, $column, $options['primary']);
660656
}
661657

662-
if (null !== $this->_eventManager && $this->_eventManager->hasListeners(Events::onSchemaCreateTable)) {
663-
$eventArgs = new SchemaCreateTableEventArgs($table, $columns, $options, $this);
664-
$this->_eventManager->dispatchEvent(Events::onSchemaCreateTable, $eventArgs);
665-
666-
if ($eventArgs->isDefaultPrevented()) {
667-
return array_merge($eventArgs->getSql(), $columnSql);
668-
}
669-
}
670-
671658
$sql = $this->_getCreateTableSQL($tableName, $columns, $options);
672659
if ($this->supportsCommentOnStatement()) {
673660
foreach ($table->getColumns() as $column) {
@@ -699,7 +686,7 @@ protected function _getCreateTableSQL($name, array $columns, array $options = ar
699686

700687
if (isset($options['indexes']) && ! empty($options['indexes'])) {
701688
foreach ($options['indexes'] as $index => $definition) {
702-
$columnListSql .= ', ' . $this->getIndexDeclarationSQL($index, $definition);
689+
$columnListSql .= ', ' . $this->getIndexDeclarationSQL($definition);
703690
}
704691
}
705692

@@ -798,8 +785,8 @@ private function buildPartitionOptions(array $options)
798785
*/
799786
public static function prepareColumnData(AbstractPlatform $platform, $column, $primaries = array())
800787
{
801-
if ($column->hasCustomSchemaOption("unique") ? $column->getCustomSchemaOption("unique") : false) {
802-
throw DBALException::notSupported("Unique constraints are not supported. Use `primary key` instead");
788+
if ($column->hasPlatformOption("unique") ? $column->hasPlatformOption("unique") : false) {
789+
throw Exception::notSupported("Unique constraints are not supported. Use `primary key` instead");
803790
}
804791

805792
$columnData = array();
@@ -811,8 +798,7 @@ public static function prepareColumnData(AbstractPlatform $platform, $column, $p
811798
$columnData['unique'] = false;
812799
$columnData['version'] = $column->hasPlatformOption("version") ? $column->getPlatformOption("version") : false;
813800

814-
if (strtolower($columnData['type']->getName()) ==
815-
strtolower($platform->getVarcharTypeDeclarationSQLSnippet(0, false))
801+
if ($columnData['type'] == $platform->getVarcharTypeDeclarationSQLSnippet(0, false)
816802
&& $columnData['length'] === null) {
817803
$columnData['length'] = 255;
818804
}
@@ -823,7 +809,7 @@ public static function prepareColumnData(AbstractPlatform $platform, $column, $p
823809
$columnData['default'] = $column->getDefault();
824810
$columnData['columnDefinition'] = $column->getColumnDefinition();
825811
$columnData['autoincrement'] = $column->getAutoincrement();
826-
$columnData['comment'] = $platform->getColumnComment($column);
812+
$columnData['comment'] = $column->getComment();
827813
$columnData['platformOptions'] = $column->getPlatformOptions();
828814

829815
if (in_array($column->getName(), $primaries)) {
@@ -885,4 +871,49 @@ public function getCurrentDatabaseExpression(): string
885871
// Added by Doctrine 3.
886872
return 'CURRENT_DATABASE()';
887873
}
874+
875+
/**
876+
* Obtains DBMS specific SQL code portion needed to set an index
877+
* declaration to be used in statements like CREATE TABLE.
878+
*
879+
* @deprecated
880+
*/
881+
public function getIndexFieldDeclarationListSQL(Index $index): string
882+
{
883+
return implode(', ', $index->getQuotedColumns($this));
884+
}
885+
886+
public function createSchemaManager(Connection $connection): AbstractSchemaManager
887+
{
888+
return new CrateSchemaManager($connection, $this);
889+
}
890+
891+
public function getLocateExpression(string $string, string $substring, ?string $start = null): string
892+
{
893+
throw Exception::notSupported(__METHOD__);
894+
}
895+
896+
protected function getDateArithmeticIntervalExpression(
897+
string $date,
898+
string $operator,
899+
string $interval,
900+
DateIntervalUnit $unit,
901+
): string {
902+
throw Exception::notSupported(__METHOD__);
903+
}
904+
905+
public function getListViewsSQL(string $database): string
906+
{
907+
throw Exception::notSupported(__METHOD__);
908+
}
909+
910+
public function getSetTransactionIsolationSQL(TransactionIsolationLevel $level): string
911+
{
912+
throw Exception::notSupported(__METHOD__);
913+
}
914+
915+
protected function createReservedKeywordsList(): KeywordList
916+
{
917+
return new CrateKeywords();
918+
}
888919
}

src/Crate/DBAL/Platforms/CratePlatform4.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public function getFloatDeclarationSQL(array $field): string
7979
/**
8080
* {@inheritDoc}
8181
*/
82-
protected function getVarcharTypeDeclarationSQLSnippet($length, $fixed): string
82+
protected function getVarcharTypeDeclarationSQLSnippet($length): string
8383
{
8484
return 'TEXT';
8585
}

0 commit comments

Comments
 (0)