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