1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Doctrine\DBAL\Schema; |
4
|
|
|
|
5
|
|
|
use Doctrine\DBAL\Platforms\DB2Platform; |
6
|
|
|
use Doctrine\DBAL\Types\Type; |
7
|
|
|
use const CASE_LOWER; |
8
|
|
|
use function array_change_key_case; |
9
|
|
|
use function preg_match; |
10
|
|
|
use function str_replace; |
11
|
|
|
use function strpos; |
12
|
|
|
use function strtolower; |
13
|
|
|
use function substr; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* IBM Db2 Schema Manager. |
17
|
|
|
*/ |
18
|
|
|
class DB2SchemaManager extends AbstractSchemaManager |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* {@inheritdoc} |
22
|
|
|
* |
23
|
|
|
* Apparently creator is the schema not the user who created it: |
24
|
|
|
* {@link http://publib.boulder.ibm.com/infocenter/dzichelp/v2r2/index.jsp?topic=/com.ibm.db29.doc.sqlref/db2z_sysibmsystablestable.htm} |
25
|
|
|
*/ |
26
|
115 |
|
public function listTableNames() |
27
|
|
|
{ |
28
|
115 |
|
$sql = $this->_platform->getListTablesSQL(); |
29
|
115 |
|
$sql .= ' AND CREATOR = UPPER(' . $this->_conn->quote($this->_conn->getUsername()) . ')'; |
30
|
|
|
|
31
|
115 |
|
$tables = $this->_conn->fetchAll($sql); |
32
|
|
|
|
33
|
115 |
|
return $this->filterAssetNames($this->_getPortableTablesList($tables)); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* {@inheritdoc} |
38
|
|
|
*/ |
39
|
|
|
protected function _getPortableTableColumnDefinition($tableColumn) |
40
|
|
|
{ |
41
|
|
|
$tableColumn = array_change_key_case($tableColumn, CASE_LOWER); |
42
|
|
|
|
43
|
|
|
$length = null; |
44
|
|
|
$fixed = null; |
45
|
|
|
$scale = false; |
46
|
|
|
$precision = false; |
47
|
|
|
|
48
|
|
|
$default = null; |
49
|
|
|
|
50
|
|
|
if ($tableColumn['default'] !== null && $tableColumn['default'] !== 'NULL') { |
51
|
|
|
$default = $tableColumn['default']; |
52
|
|
|
|
53
|
|
|
if (preg_match('/^\'(.*)\'$/s', $default, $matches) === 1) { |
54
|
|
|
$default = str_replace("''", "'", $matches[1]); |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
$type = $this->_platform->getDoctrineTypeMapping($tableColumn['typename']); |
59
|
|
|
|
60
|
|
|
if (isset($tableColumn['comment'])) { |
61
|
|
|
$type = $this->extractDoctrineTypeFromComment($tableColumn['comment'], $type); |
62
|
|
|
$tableColumn['comment'] = $this->removeDoctrineTypeFromComment($tableColumn['comment'], $type); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
switch (strtolower($tableColumn['typename'])) { |
66
|
|
|
case 'varchar': |
67
|
|
|
$length = $tableColumn['length']; |
68
|
|
|
$fixed = false; |
69
|
|
|
break; |
70
|
|
|
case 'character': |
71
|
|
|
$length = $tableColumn['length']; |
72
|
|
|
$fixed = true; |
73
|
|
|
break; |
74
|
|
|
case 'clob': |
75
|
|
|
$length = $tableColumn['length']; |
76
|
|
|
break; |
77
|
|
|
case 'decimal': |
78
|
|
|
case 'double': |
79
|
|
|
case 'real': |
80
|
|
|
$scale = $tableColumn['scale']; |
81
|
|
|
$precision = $tableColumn['length']; |
82
|
|
|
break; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
$options = [ |
86
|
|
|
'length' => $length, |
87
|
|
|
'unsigned' => false, |
88
|
|
|
'fixed' => (bool) $fixed, |
89
|
|
|
'default' => $default, |
90
|
|
|
'autoincrement' => (bool) $tableColumn['autoincrement'], |
91
|
|
|
'notnull' => (bool) ($tableColumn['nulls'] === 'N'), |
92
|
|
|
'scale' => null, |
93
|
|
|
'precision' => null, |
94
|
|
|
'comment' => isset($tableColumn['comment']) && $tableColumn['comment'] !== '' |
95
|
|
|
? $tableColumn['comment'] |
96
|
|
|
: null, |
97
|
|
|
'platformOptions' => [], |
98
|
|
|
]; |
99
|
|
|
|
100
|
|
|
if ($scale !== null && $precision !== null) { |
101
|
|
|
$options['scale'] = $scale; |
102
|
|
|
$options['precision'] = $precision; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
return new Column($tableColumn['colname'], Type::getType($type), $options); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* {@inheritdoc} |
110
|
|
|
*/ |
111
|
115 |
|
protected function _getPortableTablesList($tables) |
112
|
|
|
{ |
113
|
115 |
|
$tableNames = []; |
114
|
115 |
|
foreach ($tables as $tableRow) { |
115
|
115 |
|
$tableRow = array_change_key_case($tableRow, CASE_LOWER); |
116
|
115 |
|
$tableNames[] = $tableRow['name']; |
117
|
|
|
} |
118
|
|
|
|
119
|
115 |
|
return $tableNames; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* {@inheritdoc} |
124
|
|
|
*/ |
125
|
|
|
protected function _getPortableTableIndexesList($tableIndexRows, $tableName = null) |
126
|
|
|
{ |
127
|
|
|
foreach ($tableIndexRows as &$tableIndexRow) { |
128
|
|
|
$tableIndexRow = array_change_key_case($tableIndexRow, CASE_LOWER); |
129
|
|
|
$tableIndexRow['primary'] = (bool) $tableIndexRow['primary']; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
return parent::_getPortableTableIndexesList($tableIndexRows, $tableName); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* {@inheritdoc} |
137
|
|
|
*/ |
138
|
|
|
protected function _getPortableTableForeignKeyDefinition($tableForeignKey) |
139
|
|
|
{ |
140
|
|
|
return new ForeignKeyConstraint( |
141
|
|
|
$tableForeignKey['local_columns'], |
142
|
|
|
$tableForeignKey['foreign_table'], |
143
|
|
|
$tableForeignKey['foreign_columns'], |
144
|
|
|
$tableForeignKey['name'], |
145
|
|
|
$tableForeignKey['options'] |
146
|
|
|
); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* {@inheritdoc} |
151
|
|
|
*/ |
152
|
|
|
protected function _getPortableTableForeignKeysList($tableForeignKeys) |
153
|
|
|
{ |
154
|
|
|
$foreignKeys = []; |
155
|
|
|
|
156
|
|
|
foreach ($tableForeignKeys as $tableForeignKey) { |
157
|
|
|
$tableForeignKey = array_change_key_case($tableForeignKey, CASE_LOWER); |
158
|
|
|
|
159
|
|
|
if (! isset($foreignKeys[$tableForeignKey['index_name']])) { |
160
|
|
|
$foreignKeys[$tableForeignKey['index_name']] = [ |
161
|
|
|
'local_columns' => [$tableForeignKey['local_column']], |
162
|
|
|
'foreign_table' => $tableForeignKey['foreign_table'], |
163
|
|
|
'foreign_columns' => [$tableForeignKey['foreign_column']], |
164
|
|
|
'name' => $tableForeignKey['index_name'], |
165
|
|
|
'options' => [ |
166
|
|
|
'onUpdate' => $tableForeignKey['on_update'], |
167
|
|
|
'onDelete' => $tableForeignKey['on_delete'], |
168
|
|
|
], |
169
|
|
|
]; |
170
|
|
|
} else { |
171
|
|
|
$foreignKeys[$tableForeignKey['index_name']]['local_columns'][] = $tableForeignKey['local_column']; |
172
|
|
|
$foreignKeys[$tableForeignKey['index_name']]['foreign_columns'][] = $tableForeignKey['foreign_column']; |
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
return parent::_getPortableTableForeignKeysList($foreignKeys); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* @param string $def |
181
|
|
|
* |
182
|
|
|
* @return string|null |
183
|
|
|
*/ |
184
|
|
|
protected function _getPortableForeignKeyRuleDef($def) |
185
|
|
|
{ |
186
|
|
|
if ($def === 'C') { |
187
|
|
|
return 'CASCADE'; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
if ($def === 'N') { |
191
|
|
|
return 'SET NULL'; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
return null; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* {@inheritdoc} |
199
|
|
|
*/ |
200
|
|
|
protected function _getPortableViewDefinition($view) |
201
|
|
|
{ |
202
|
|
|
$view = array_change_key_case($view, CASE_LOWER); |
203
|
|
|
|
204
|
|
|
$position = strpos($view['text'], ' AS '); |
205
|
|
|
|
206
|
|
|
if ($position !== false) { |
207
|
|
|
$sql = substr($view['text'], $position + 4); |
208
|
|
|
} else { |
209
|
|
|
$sql = ''; |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
return new View($view['name'], $sql); |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
public function listTableDetails($tableName) : Table |
216
|
|
|
{ |
217
|
|
|
$table = parent::listTableDetails($tableName); |
218
|
|
|
|
219
|
|
|
/** @var DB2Platform $platform */ |
220
|
|
|
$platform = $this->_platform; |
221
|
|
|
$sql = $platform->getListTableCommentsSQL($tableName); |
222
|
|
|
|
223
|
|
|
$tableOptions = $this->_conn->fetchAssoc($sql); |
224
|
|
|
|
225
|
|
|
if ($tableOptions !== false) { |
226
|
|
|
$table->addOption('comment', $tableOptions['REMARKS']); |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
return $table; |
230
|
|
|
} |
231
|
|
|
} |
232
|
|
|
|