Skip to content

Commit 4db0204

Browse files
JulianMaramotl
authored andcommitted
DBAL4: Making a start
1 parent e537575 commit 4db0204

22 files changed

+216
-112
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: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,11 @@
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",
21-
"ext-pdo": "*"
21+
"ext-pdo": "*",
22+
"slam/dbal-debugstack-middleware": "*"
2223
},
2324
"autoload": {
2425
"psr-0": {
@@ -27,7 +28,8 @@
2728
},
2829
"require-dev": {
2930
"phpunit/phpunit": "^9.0",
30-
"friendsofphp/php-cs-fixer": "^3.89"
31+
"friendsofphp/php-cs-fixer": "^3.89",
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
@@ -91,7 +91,7 @@ public function rowCount(): int
9191
/**
9292
* {@inheritDoc}
9393
*/
94-
public function bindValue($param, $value, $type = ParameterType::STRING): void
94+
public function bindValue($param, $value, ParameterType $type = ParameterType::STRING): void
9595
{
9696
$this->stmt->bindValue($param, $value, $type);
9797
}
@@ -100,7 +100,7 @@ public function bindValue($param, $value, $type = ParameterType::STRING): void
100100
* @deprecated Use bindValue() instead.
101101
* {@inheritDoc}
102102
*/
103-
public function bindParam($param, &$variable, $type = ParameterType::STRING, $length = null)
103+
public function bindParam($param, &$variable, ParameterType $type = ParameterType::STRING, $length = null)
104104
{
105105
Deprecation::trigger(
106106
'doctrine/dbal',

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
use Doctrine\DBAL\Connection;
3131
use Doctrine\DBAL\Platforms\AbstractPlatform;
3232
use Doctrine\DBAL\Schema\AbstractSchemaManager;
33+
use Doctrine\DBAL\ServerVersionProvider;
3334
use SensitiveParameter;
3435

3536
class Driver implements \Doctrine\DBAL\Driver
@@ -74,8 +75,9 @@ private function constructPdoDsn(array $params): string
7475

7576
/**
7677
* {@inheritDoc}
78+
* @param ServerVersionProvider $versionProvider
7779
*/
78-
public function getDatabasePlatform(): AbstractPlatform
80+
public function getDatabasePlatform(ServerVersionProvider $versionProvider): AbstractPlatform
7981
{
8082
return new CratePlatform4();
8183
}
@@ -102,7 +104,7 @@ public function getName(): string
102104
*/
103105
public function getDatabase(Connection $conn): string|null
104106
{
105-
return null;
107+
return 'doc';
106108
}
107109

108110
/**

src/Crate/DBAL/Platforms/CratePlatform.php

Lines changed: 75 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,23 @@
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\Connection;
3132
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;
3237
use Doctrine\DBAL\Schema\ForeignKeyConstraint;
3338
use Doctrine\DBAL\Schema\Identifier;
3439
use Doctrine\DBAL\Schema\Index;
3540
use Doctrine\DBAL\Schema\Table;
3641
use Doctrine\DBAL\Schema\TableDiff;
42+
use Doctrine\DBAL\TransactionIsolationLevel;
3743
use Doctrine\DBAL\Types\Type;
3844
use InvalidArgumentException;
3945

@@ -50,12 +56,16 @@ public function __construct()
5056
{
5157
$this->initializeDoctrineTypeMappings();
5258
if (!Type::hasType(MapType::NAME)) {
53-
Type::addType(MapType::NAME, 'Crate\DBAL\Types\MapType');
59+
Type::addType(MapType::NAME, MapType::class);
60+
$this->registerDoctrineTypeMapping(10, MapType::NAME);
5461
}
5562
if (!Type::hasType(TimestampType::NAME)) {
56-
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);
5768
}
58-
Type::overrideType('array', 'Crate\DBAL\Types\ArrayType');
5969
}
6070

6171
/**
@@ -256,25 +266,25 @@ public function getAlterTableSQL(TableDiff $diff): array
256266
$commentsSQL = array();
257267
$columnSql = array();
258268

259-
foreach ($diff->addedColumns as $column) {
269+
foreach ($diff->getAddedColumns() as $column) {
260270
if ($this->onSchemaAlterTableAddColumn($column, $diff, $columnSql)) {
261271
continue;
262272
}
263273

264274
$query = 'ADD ' . $this->getColumnDeclarationSQL($column->getQuotedName($this), $column->toArray());
265275
$sql[] = 'ALTER TABLE ' . $diff->name . ' ' . $query;
266-
if ($comment = $this->getColumnComment($column)) {
276+
if ($comment = $column->getComment()) {
267277
$commentsSQL[] = $this->getCommentOnColumnSQL($diff->name, $column->getName(), $comment);
268278
}
269279
}
270280

271-
if (count($diff->removedColumns) > 0) {
281+
if (count($diff->getDroppedColumns()) > 0) {
272282
throw Exception::notSupported("Alter Table: drop columns");
273283
}
274-
if (count($diff->changedColumns) > 0) {
284+
if (count($diff->getChangedColumns()) > 0) {
275285
throw Exception::notSupported("Alter Table: change column options");
276286
}
277-
if (count($diff->renamedColumns) > 0) {
287+
if (count($diff->getRenamedColumns()) > 0) {
278288
throw Exception::notSupported("Alter Table: rename columns");
279289
}
280290

@@ -297,7 +307,7 @@ public function getAlterTableSQL(TableDiff $diff): array
297307
public function getColumnDeclarationSQL($name, array $column): string
298308
{
299309
if (isset($column['columnDefinition'])) {
300-
$columnDef = $this->getCustomTypeDeclarationSQL($column);
310+
$columnDef = $column['columnDefinition'];
301311
} else {
302312
$typeDecl = $column['type']->getSqlDeclaration($column, $this);
303313
$columnDef = $typeDecl;
@@ -312,10 +322,10 @@ public function getColumnDeclarationSQL($name, array $column): string
312322
* @param Index $index
313323
* @codeCoverageIgnore
314324
*/
315-
public function getIndexDeclarationSQL($name, Index $index): string
325+
public function getIndexDeclarationSQL(Index $index): string
316326
{
317327
$columns = $index->getQuotedColumns($this);
318-
$name = new Identifier($name);
328+
$name = new Identifier($index->getName());
319329

320330
if (count($columns) == 0) {
321331
throw new \InvalidArgumentException("Incomplete definition. 'columns' required.");
@@ -445,7 +455,7 @@ protected function _getCommonIntegerTypeDeclarationSQL(array $columnDef): string
445455
* @param false|int $length
446456
* @param $fixed
447457
*/
448-
protected function getVarcharTypeDeclarationSQLSnippet($length, $fixed): string
458+
protected function getVarcharTypeDeclarationSQLSnippet($length): string
449459
{
450460
return 'STRING';
451461
}
@@ -600,7 +610,7 @@ public function getCreateTableSQL(Table $table, $createFlags = self::CREATE_INDE
600610
}
601611

602612
if (count($table->getColumns()) === 0) {
603-
throw DBALException::noColumnsSpecifiedForTable($table->getName());
613+
throw NoColumnsSpecifiedForTable::new($table->getName());
604614
}
605615

606616
$tableName = $table->getQuotedName($this);
@@ -632,30 +642,9 @@ public function getCreateTableSQL(Table $table, $createFlags = self::CREATE_INDE
632642
$columns = array();
633643

634644
foreach ($table->getColumns() as $column) {
635-
if (null !== $this->_eventManager &&
636-
$this->_eventManager->hasListeners(Events::onSchemaCreateTableColumn)
637-
) {
638-
$eventArgs = new SchemaCreateTableColumnEventArgs($column, $table, $this);
639-
$this->_eventManager->dispatchEvent(Events::onSchemaCreateTableColumn, $eventArgs);
640-
641-
$columnSql = array_merge($columnSql, $eventArgs->getSql());
642-
643-
if ($eventArgs->isDefaultPrevented()) {
644-
continue;
645-
}
646-
}
647645
$columns[$column->getQuotedName($this)] = self::prepareColumnData($this, $column, $options['primary']);
648646
}
649647

650-
if (null !== $this->_eventManager && $this->_eventManager->hasListeners(Events::onSchemaCreateTable)) {
651-
$eventArgs = new SchemaCreateTableEventArgs($table, $columns, $options, $this);
652-
$this->_eventManager->dispatchEvent(Events::onSchemaCreateTable, $eventArgs);
653-
654-
if ($eventArgs->isDefaultPrevented()) {
655-
return array_merge($eventArgs->getSql(), $columnSql);
656-
}
657-
}
658-
659648
$sql = $this->_getCreateTableSQL($tableName, $columns, $options);
660649
if ($this->supportsCommentOnStatement()) {
661650
foreach ($table->getColumns() as $column) {
@@ -687,7 +676,7 @@ protected function _getCreateTableSQL($name, array $columns, array $options = ar
687676

688677
if (isset($options['indexes']) && ! empty($options['indexes'])) {
689678
foreach ($options['indexes'] as $index => $definition) {
690-
$columnListSql .= ', ' . $this->getIndexDeclarationSQL($index, $definition);
679+
$columnListSql .= ', ' . $this->getIndexDeclarationSQL($definition);
691680
}
692681
}
693682

@@ -786,7 +775,7 @@ private function buildPartitionOptions(array $options)
786775
*/
787776
public static function prepareColumnData(AbstractPlatform $platform, $column, $primaries = array())
788777
{
789-
if ($column->hasCustomSchemaOption("unique") ? $column->getCustomSchemaOption("unique") : false) {
778+
if ($column->hasPlatformOption("unique") ? $column->hasPlatformOption("unique") : false) {
790779
throw Exception::notSupported("Unique constraints are not supported. Use `primary key` instead");
791780
}
792781

@@ -799,8 +788,7 @@ public static function prepareColumnData(AbstractPlatform $platform, $column, $p
799788
$columnData['unique'] = false;
800789
$columnData['version'] = $column->hasPlatformOption("version") ? $column->getPlatformOption("version") : false;
801790

802-
if (strtolower($columnData['type']->getName()) ==
803-
strtolower($platform->getVarcharTypeDeclarationSQLSnippet(0, false))
791+
if ($columnData['type'] == $platform->getVarcharTypeDeclarationSQLSnippet(0, false)
804792
&& $columnData['length'] === null) {
805793
$columnData['length'] = 255;
806794
}
@@ -811,7 +799,7 @@ public static function prepareColumnData(AbstractPlatform $platform, $column, $p
811799
$columnData['default'] = $column->getDefault();
812800
$columnData['columnDefinition'] = $column->getColumnDefinition();
813801
$columnData['autoincrement'] = $column->getAutoincrement();
814-
$columnData['comment'] = $platform->getColumnComment($column);
802+
$columnData['comment'] = $column->getComment();
815803
$columnData['platformOptions'] = $column->getPlatformOptions();
816804

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

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
}

src/Crate/DBAL/Platforms/Exception.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
<?php
22
namespace Crate\DBAL\Platforms;
3-
use Doctrine\DBAL\Exception as DoctrineException;
4-
class Exception extends \Exception implements DoctrineException
3+
4+
use Doctrine\DBAL\Driver\AbstractException;
5+
use Doctrine\DBAL\Platforms\Exception\PlatformException;
6+
7+
class Exception extends AbstractException implements PlatformException
58
{
69
public static function notSupported(string $method): self
710
{
811
return new self(sprintf("Operation '%s' is not supported by platform.", $method));
912
}
1013
}
11-
12-
?>

0 commit comments

Comments
 (0)