Passed
Push — dev ( 7ae04d...cd5ace )
by Def
30:02 queued 08:14
created

DDLQueryBuilder::renameTable()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 2
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Db\Oracle;
6
7
use Yiisoft\Db\Exception\Exception;
8
use Yiisoft\Db\Exception\NotSupportedException;
9
use Yiisoft\Db\Query\DDLQueryBuilder as AbstractDDLQueryBuilder;
10
use Yiisoft\Db\Query\QueryBuilderInterface;
11
use Yiisoft\Db\Schema\ColumnSchemaBuilder;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Yiisoft\Db\Oracle\ColumnSchemaBuilder. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
12
13
final class DDLQueryBuilder extends AbstractDDLQueryBuilder
14
{
15 344
    public function __construct(private QueryBuilderInterface $queryBuilder)
16
    {
17 344
        parent::__construct($queryBuilder);
18
    }
19
20 4
    public function addForeignKey(string $name, string $table, array|string $columns, string $refTable, array|string $refColumns, ?string $delete = null, ?string $update = null): string
21
    {
22 4
        $sql = 'ALTER TABLE ' . $this->quoter->quoteTableName($table)
23 4
            . ' ADD CONSTRAINT ' . $this->quoter->quoteColumnName($name)
24 4
            . ' FOREIGN KEY (' . $this->queryBuilder->buildColumns($columns) . ')'
25 4
            . ' REFERENCES ' . $this->quoter->quoteTableName($refTable)
26 4
            . ' (' . $this->queryBuilder->buildColumns($refColumns) . ')';
27
28 4
        if ($delete !== null) {
29 2
            $sql .= ' ON DELETE ' . $delete;
30
        }
31
32 4
        if ($update !== null) {
33
            throw new Exception('Oracle does not support ON UPDATE clause.');
34
        }
35
36 4
        return $sql;
37
    }
38
39
    public function alterColumn(string $table, string $column, ColumnSchemaBuilder|string $type): string
40
    {
41
        return 'ALTER TABLE '
42
            . $this->quoter->quoteTableName($table)
43
            . ' MODIFY '
44
            . $this->quoter->quoteColumnName($column)
45
            . ' ' . $this->queryBuilder->getColumnType($type);
46
    }
47
48
    public function checkIntegrity(string $schema = '', string $table = '', bool $check = true): string
49
    {
50
        throw new NotSupportedException('Oracle does not support enabling/disabling integrity check.');
51
    }
52
53 2
    public function dropCommentFromColumn(string $table, string $column): string
54
    {
55
        return 'COMMENT ON COLUMN '
56 2
            . $this->quoter->quoteTableName($table)
57
            . '.'
58 2
            . $this->quoter->quoteColumnName($column)
59
            . " IS ''";
60
    }
61
62 1
    public function dropCommentFromTable(string $table): string
63
    {
64 1
        return 'COMMENT ON TABLE ' . $this->quoter->quoteTableName($table) . " IS ''";
65
    }
66
67 2
    public function dropIndex(string $name, string $table): string
68
    {
69 2
        return 'DROP INDEX ' . $this->quoter->quoteTableName($name);
70
    }
71
72 2
    public function renameTable(string $oldName, string $newName): string
73
    {
74 2
        return 'ALTER TABLE ' . $this->quoter->quoteTableName($oldName) . ' RENAME TO ' .
75 2
            $this->quoter->quoteTableName($newName);
76
    }
77
}
78