-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Add support for creating temporary tables in schema #6409
base: 4.3.x
Are you sure you want to change the base?
Changes from all commits
025ec94
8b893ae
2e1be8f
00561f8
2ba17e1
ae72011
3196045
91c522c
6d69ff0
3ed2b96
4283b37
e512409
7f8f8fb
7e3133b
af36e6e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ | |
namespace Doctrine\DBAL\Platforms; | ||
|
||
use Doctrine\DBAL\Connection; | ||
use Doctrine\DBAL\Exception\InvalidArgumentException; | ||
use Doctrine\DBAL\Platforms\Exception\NotSupported; | ||
use Doctrine\DBAL\Platforms\Keywords\DB2Keywords; | ||
use Doctrine\DBAL\Platforms\Keywords\KeywordList; | ||
|
@@ -20,6 +21,8 @@ | |
use Doctrine\DBAL\Types\Types; | ||
|
||
use function array_merge; | ||
use function array_unique; | ||
use function array_values; | ||
use function count; | ||
use function current; | ||
use function explode; | ||
|
@@ -241,20 +244,50 @@ | |
*/ | ||
protected function _getCreateTableSQL(string $name, array $columns, array $options = []): array | ||
{ | ||
$indexes = []; | ||
if (isset($options['indexes'])) { | ||
$indexes = $options['indexes']; | ||
$columnListSql = $this->getColumnDeclarationListSQL($columns); | ||
|
||
if (isset($options['uniqueConstraints']) && ! empty($options['uniqueConstraints'])) { | ||
foreach ($options['uniqueConstraints'] as $definition) { | ||
$columnListSql .= ', ' . $this->getUniqueConstraintDeclarationSQL($definition); | ||
} | ||
} | ||
|
||
$options['indexes'] = []; | ||
if (isset($options['primary']) && ! empty($options['primary'])) { | ||
$columnListSql .= ', PRIMARY KEY(' . implode(', ', array_unique(array_values($options['primary']))) . ')'; | ||
} | ||
|
||
$statement = match ($options['temporary'] ?? '') { | ||
'' => 'CREATE TABLE ', | ||
'created' => 'CREATE GLOBAL TEMPORARY TABLE ', | ||
'declared' => 'DECLARE GLOBAL TEMPORARY TABLE ', | ||
default => throw new InvalidArgumentException(sprintf( | ||
'invalid temporary specification for table %s', | ||
$name, | ||
)) | ||
}; | ||
Comment on lines
+259
to
+267
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Okay, so for MySQL we demand that the |
||
|
||
$query = $statement . $name . ' (' . $columnListSql; | ||
$check = $this->getCheckDeclarationSQL($columns); | ||
|
||
if (! empty($check)) { | ||
$query .= ', ' . $check; | ||
} | ||
|
||
$sqls = parent::_getCreateTableSQL($name, $columns, $options); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Removing the call to the parent implementation seems to bloat this method big time. Can we somehow restore that call and make the diff smaller? |
||
$query .= ')'; | ||
|
||
$sql = [$query]; | ||
|
||
if (isset($options['foreignKeys'])) { | ||
foreach ($options['foreignKeys'] as $definition) { | ||
$sql[] = $this->getCreateForeignKeySQL($definition, $name); | ||
} | ||
} | ||
|
||
foreach ($indexes as $definition) { | ||
$sqls[] = $this->getCreateIndexSQL($definition, $name); | ||
foreach ($options['indexes'] ?? [] as $definition) { | ||
$sql[] = $this->getCreateIndexSQL($definition, $name); | ||
} | ||
|
||
return $sqls; | ||
return $sql; | ||
} | ||
|
||
/** | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ | |
namespace Doctrine\DBAL\Platforms; | ||
|
||
use Doctrine\DBAL\Connection; | ||
use Doctrine\DBAL\Exception\InvalidArgumentException; | ||
use Doctrine\DBAL\Exception\InvalidColumnType\ColumnLengthRequired; | ||
use Doctrine\DBAL\Platforms\Keywords\KeywordList; | ||
use Doctrine\DBAL\Platforms\Keywords\OracleKeywords; | ||
|
@@ -17,15 +18,18 @@ | |
use Doctrine\DBAL\TransactionIsolationLevel; | ||
use Doctrine\DBAL\Types\BinaryType; | ||
use Doctrine\DBAL\Types\Types; | ||
use InvalidArgumentException; | ||
|
||
use function array_merge; | ||
use function array_unique; | ||
use function array_values; | ||
use function count; | ||
use function explode; | ||
use function implode; | ||
use function sprintf; | ||
use function str_contains; | ||
use function str_starts_with; | ||
use function strlen; | ||
use function strtolower; | ||
use function strtoupper; | ||
use function substr; | ||
|
||
|
@@ -313,9 +317,62 @@ | |
*/ | ||
protected function _getCreateTableSQL(string $name, array $columns, array $options = []): array | ||
{ | ||
$indexes = $options['indexes'] ?? []; | ||
$options['indexes'] = []; | ||
$sql = parent::_getCreateTableSQL($name, $columns, $options); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same question here. Do we have to remove the call to the parent method? |
||
$columnListSql = $this->getColumnDeclarationListSQL($columns); | ||
|
||
if (isset($options['uniqueConstraints']) && ! empty($options['uniqueConstraints'])) { | ||
foreach ($options['uniqueConstraints'] as $definition) { | ||
$columnListSql .= ', ' . $this->getUniqueConstraintDeclarationSQL($definition); | ||
} | ||
} | ||
|
||
if (isset($options['primary']) && ! empty($options['primary'])) { | ||
$columnListSql .= ', PRIMARY KEY(' . implode(', ', array_unique(array_values($options['primary']))) . ')'; | ||
} | ||
|
||
$temporary = match ($options['temporary'] ?? '') { | ||
'' => '', | ||
'global' => 'GLOBAL TEMPORARY ', | ||
'private' => 'PRIVATE TEMPORARY ', | ||
default => throw new InvalidArgumentException(sprintf( | ||
'invalid temporary specification for table %s', | ||
$name, | ||
)) | ||
}; | ||
Comment on lines
+332
to
+340
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same question here. Why are my options for |
||
|
||
if (($options['temporary'] ?? '') === 'private' && str_starts_with('ora$ptt_', strtolower($name)) === false) { | ||
Check failure on line 342 in src/Platforms/OraclePlatform.php GitHub Actions / Static Analysis with Psalm (8.3)InvalidLiteralArgument
|
||
throw new InvalidArgumentException(sprintf( | ||
'invalid name "%s" for private temporary table', | ||
$name, | ||
)); | ||
} | ||
|
||
$onCommit = $temporary !== '' | ||
? match ($options['on_commit'] ?? '') { | ||
'' => '', | ||
'preserve' => ' ON COMMIT PRESERVE ROWS', | ||
'delete' => ' ON COMMIT DELETE ROWS', | ||
default => throw new InvalidArgumentException(sprintf( | ||
'invalid on commit clause on table %s', | ||
$name, | ||
)) | ||
} : ''; | ||
|
||
$query = 'CREATE ' . $temporary . 'TABLE ' . $name . ' (' . $columnListSql; | ||
$check = $this->getCheckDeclarationSQL($columns); | ||
|
||
if (! empty($check)) { | ||
$query .= ', ' . $check; | ||
} | ||
|
||
$query .= ')' . $onCommit; | ||
|
||
$sql = [$query]; | ||
|
||
if (isset($options['foreignKeys'])) { | ||
foreach ($options['foreignKeys'] as $definition) { | ||
$sql[] = $this->getCreateForeignKeySQL($definition, $name); | ||
} | ||
} | ||
|
||
foreach ($columns as $column) { | ||
if (isset($column['sequence'])) { | ||
|
@@ -331,7 +388,7 @@ | |
$sql = array_merge($sql, $this->getCreateAutoincrementSql($column['name'], $name)); | ||
} | ||
|
||
foreach ($indexes as $index) { | ||
foreach ($options['indexes'] as $index) { | ||
$sql[] = $this->getCreateIndexSQL($index, $name); | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks like a BC break. Do we need this additional strictness?