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