|
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\QueryBuilder\DDLQueryBuilder as AbstractDDLQueryBuilder; |
|
10
|
|
|
use Yiisoft\Db\QueryBuilder\QueryBuilderInterface; |
|
11
|
|
|
use Yiisoft\Db\Schema\ColumnSchemaBuilder; |
|
|
|
|
|
|
12
|
|
|
use Yiisoft\Db\Schema\QuoterInterface; |
|
13
|
|
|
use Yiisoft\Db\Schema\SchemaInterface; |
|
14
|
|
|
|
|
15
|
|
|
final class DDLQueryBuilder extends AbstractDDLQueryBuilder |
|
16
|
|
|
{ |
|
17
|
451 |
|
public function __construct( |
|
18
|
|
|
private QueryBuilderInterface $queryBuilder, |
|
19
|
|
|
private QuoterInterface $quoter, |
|
20
|
|
|
SchemaInterface $schema |
|
21
|
|
|
) { |
|
22
|
451 |
|
parent::__construct($queryBuilder, $quoter, $schema); |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @throws NotSupportedException |
|
27
|
|
|
*/ |
|
28
|
2 |
|
public function addDefaultValue(string $name, string $table, string $column, mixed $value): string |
|
29
|
|
|
{ |
|
30
|
2 |
|
throw new NotSupportedException(__METHOD__ . ' is not supported by Oracle.'); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
8 |
|
public function addForeignKey( |
|
34
|
|
|
string $name, |
|
35
|
|
|
string $table, |
|
36
|
|
|
array|string $columns, |
|
37
|
|
|
string $refTable, |
|
38
|
|
|
array|string $refColumns, |
|
39
|
|
|
string $delete = null, |
|
40
|
|
|
string $update = null |
|
41
|
|
|
): string { |
|
42
|
8 |
|
$sql = 'ALTER TABLE ' . $this->quoter->quoteTableName($table) |
|
43
|
8 |
|
. ' ADD CONSTRAINT ' . $this->quoter->quoteColumnName($name) |
|
44
|
8 |
|
. ' FOREIGN KEY (' . $this->queryBuilder->buildColumns($columns) . ')' |
|
45
|
8 |
|
. ' REFERENCES ' . $this->quoter->quoteTableName($refTable) |
|
46
|
8 |
|
. ' (' . $this->queryBuilder->buildColumns($refColumns) . ')'; |
|
47
|
|
|
|
|
48
|
8 |
|
if ($delete !== null) { |
|
49
|
3 |
|
$sql .= ' ON DELETE ' . $delete; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
8 |
|
if ($update !== null) { |
|
53
|
1 |
|
throw new Exception('Oracle does not support ON UPDATE clause.'); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
7 |
|
return $sql; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
1 |
|
public function alterColumn(string $table, string $column, ColumnSchemaBuilder|string $type): string |
|
60
|
|
|
{ |
|
61
|
|
|
return 'ALTER TABLE ' |
|
62
|
1 |
|
. $this->quoter->quoteTableName($table) |
|
63
|
|
|
. ' MODIFY ' |
|
64
|
1 |
|
. $this->quoter->quoteColumnName($column) |
|
65
|
1 |
|
. ' ' . $this->queryBuilder->getColumnType($type); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
1 |
|
public function checkIntegrity(string $schema = '', string $table = '', bool $check = true): string |
|
69
|
|
|
{ |
|
70
|
1 |
|
throw new NotSupportedException(__METHOD__ . ' is not supported by Oracle.'); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
2 |
|
public function dropCommentFromColumn(string $table, string $column): string |
|
74
|
|
|
{ |
|
75
|
|
|
return 'COMMENT ON COLUMN ' |
|
76
|
2 |
|
. $this->quoter->quoteTableName($table) |
|
77
|
|
|
. '.' |
|
78
|
2 |
|
. $this->quoter->quoteColumnName($column) |
|
79
|
|
|
. " IS ''"; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
2 |
|
public function dropCommentFromTable(string $table): string |
|
83
|
|
|
{ |
|
84
|
2 |
|
return 'COMMENT ON TABLE ' . $this->quoter->quoteTableName($table) . " IS ''"; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* @throws NotSupportedException |
|
89
|
|
|
*/ |
|
90
|
2 |
|
public function dropDefaultValue(string $name, string $table): string |
|
91
|
|
|
{ |
|
92
|
2 |
|
throw new NotSupportedException(__METHOD__ . ' is not supported by Oracle.'); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
2 |
|
public function dropIndex(string $name, string $table): string |
|
96
|
|
|
{ |
|
97
|
2 |
|
return 'DROP INDEX ' . $this->quoter->quoteTableName($name); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
3 |
|
public function renameTable(string $oldName, string $newName): string |
|
101
|
|
|
{ |
|
102
|
3 |
|
return 'ALTER TABLE ' . $this->quoter->quoteTableName($oldName) . ' RENAME TO ' . |
|
103
|
3 |
|
$this->quoter->quoteTableName($newName); |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|
Let?s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let?s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare 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.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/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: