Skip to content

Commit

Permalink
Improve
Browse files Browse the repository at this point in the history
  • Loading branch information
Tigrov committed Oct 9, 2024
1 parent 1d46ee6 commit 508f947
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
15 changes: 15 additions & 0 deletions src/Column/ColumnDefinitionBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

use function ceil;
use function log10;
use function strtoupper;

final class ColumnDefinitionBuilder extends AbstractColumnDefinitionBuilder
{
Expand Down Expand Up @@ -46,6 +47,20 @@ final class ColumnDefinitionBuilder extends AbstractColumnDefinitionBuilder
'number',
];

protected function buildOnDeleteClause(string $onDelete): string
{
return match ($onDelete = strtoupper($onDelete)) {
'CASCADE',
'SET NULL' => " ON DELETE $onDelete",
default => '',
};
}

protected function buildOnUpdateClause(string $onUpdate): string
{
return '';
}

protected function getDbType(ColumnSchemaInterface $column): string
{
$size = $column->getSize();
Expand Down
20 changes: 18 additions & 2 deletions tests/Provider/QueryBuilderProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@

use Exception;
use Yiisoft\Db\Constant\PseudoType;
use Yiisoft\Db\Constraint\ForeignKeyConstraint;
use Yiisoft\Db\Expression\Expression;
use Yiisoft\Db\Oracle\Column\ColumnBuilder;
use Yiisoft\Db\Oracle\Tests\Support\TestTrait;
use Yiisoft\Db\Query\Query;
use Yiisoft\Db\Tests\Support\DbHelper;
Expand Down Expand Up @@ -239,6 +241,14 @@ public static function upsert(): array

public static function buildColumnDefinition(): array
{
$referenceRestrict = new ForeignKeyConstraint();
$referenceRestrict->foreignColumnNames(['id']);
$referenceRestrict->foreignTableName('ref_table');
$referenceRestrict->onDelete('restrict');

$referenceSetNull = clone $referenceRestrict;
$referenceSetNull->onDelete('set null');

$values = parent::buildColumnDefinition();

$values[PseudoType::PK][0] = 'number(10) GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY';
Expand Down Expand Up @@ -319,8 +329,14 @@ public static function buildColumnDefinition(): array
$values['unsigned()'][0] = 'number(10)';
$values['scale(2)'][0] = 'number(10,2)';
$values['integer(8)->scale(2)'][0] = 'number(8)';
$values['reference($reference)'][0] = 'number(10) REFERENCES "ref_table" ("id") ON DELETE CASCADE ON UPDATE CASCADE';
$values['reference($reference)'][0] = 'number(10) REFERENCES "ref_table" ("id") ON DELETE CASCADE';
$values['reference($referenceWithSchema)'][0] = 'number(10) REFERENCES "ref_schema"."ref_table" ("id") ON DELETE CASCADE';

return $values;
return [
...$values,

['number(10) REFERENCES "ref_table" ("id")', ColumnBuilder::integer()->reference($referenceRestrict)],
['number(10) REFERENCES "ref_table" ("id") ON DELETE SET NULL', ColumnBuilder::integer()->reference($referenceSetNull)],
];
}
}

0 comments on commit 508f947

Please sign in to comment.