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