1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
4
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
5
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
6
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
7
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
8
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
9
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
10
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
11
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
12
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
13
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
14
|
|
|
* |
15
|
|
|
* This software consists of voluntary contributions made by many individuals |
16
|
|
|
* and is licensed under the MIT license. For more information, see |
17
|
|
|
* <http://www.doctrine-project.org>. |
18
|
|
|
*/ |
19
|
|
|
|
20
|
|
|
namespace Doctrine\DBAL\Schema; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* IBM Db2 Schema Manager. |
24
|
|
|
* |
25
|
|
|
* @link www.doctrine-project.org |
26
|
|
|
* @since 1.0 |
27
|
|
|
* @author Benjamin Eberlei <[email protected]> |
28
|
|
|
*/ |
29
|
|
|
class DB2SchemaManager extends AbstractSchemaManager |
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* {@inheritdoc} |
33
|
|
|
* |
34
|
|
|
* Apparently creator is the schema not the user who created it: |
35
|
|
|
* {@link http://publib.boulder.ibm.com/infocenter/dzichelp/v2r2/index.jsp?topic=/com.ibm.db29.doc.sqlref/db2z_sysibmsystablestable.htm} |
36
|
|
|
*/ |
37
|
1 |
|
public function listTableNames() |
38
|
|
|
{ |
39
|
1 |
|
$sql = $this->_platform->getListTablesSQL(); |
40
|
1 |
|
$sql .= " AND CREATOR = UPPER('".$this->_conn->getUsername()."')"; |
41
|
|
|
|
42
|
1 |
|
$tables = $this->_conn->fetchAll($sql); |
43
|
|
|
|
44
|
1 |
|
return $this->filterAssetNames($this->_getPortableTablesList($tables)); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* {@inheritdoc} |
49
|
|
|
*/ |
50
|
|
|
protected function _getPortableTableColumnDefinition($tableColumn) |
51
|
|
|
{ |
52
|
|
|
$tableColumn = array_change_key_case($tableColumn, \CASE_LOWER); |
53
|
|
|
|
54
|
|
|
$length = null; |
55
|
|
|
$fixed = null; |
56
|
|
|
$unsigned = false; |
57
|
|
|
$scale = false; |
58
|
|
|
$precision = false; |
59
|
|
|
|
60
|
|
|
$default = null; |
61
|
|
|
|
62
|
|
|
if (null !== $tableColumn['default'] && 'NULL' != $tableColumn['default']) { |
63
|
|
|
$default = trim($tableColumn['default'], "'"); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
$type = $this->_platform->getDoctrineTypeMapping($tableColumn['typename']); |
67
|
|
|
|
68
|
|
|
if (isset($tableColumn['comment'])) { |
69
|
|
|
$type = $this->extractDoctrineTypeFromComment($tableColumn['comment'], $type); |
70
|
|
|
$tableColumn['comment'] = $this->removeDoctrineTypeFromComment($tableColumn['comment'], $type); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
switch (strtolower($tableColumn['typename'])) { |
74
|
|
|
case 'varchar': |
75
|
|
|
$length = $tableColumn['length']; |
76
|
|
|
$fixed = false; |
77
|
|
|
break; |
78
|
|
|
case 'character': |
79
|
|
|
$length = $tableColumn['length']; |
80
|
|
|
$fixed = true; |
81
|
|
|
break; |
82
|
|
|
case 'clob': |
83
|
|
|
$length = $tableColumn['length']; |
84
|
|
|
break; |
85
|
|
|
case 'decimal': |
86
|
|
|
case 'double': |
87
|
|
|
case 'real': |
88
|
|
|
$scale = $tableColumn['scale']; |
89
|
|
|
$precision = $tableColumn['length']; |
90
|
|
|
break; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
$options = [ |
94
|
|
|
'length' => $length, |
95
|
|
|
'unsigned' => (bool) $unsigned, |
96
|
|
|
'fixed' => (bool) $fixed, |
97
|
|
|
'default' => $default, |
98
|
|
|
'autoincrement' => (boolean) $tableColumn['autoincrement'], |
99
|
|
|
'notnull' => (bool) ($tableColumn['nulls'] == 'N'), |
100
|
|
|
'scale' => null, |
101
|
|
|
'precision' => null, |
102
|
|
|
'comment' => isset($tableColumn['comment']) && $tableColumn['comment'] !== '' |
103
|
|
|
? $tableColumn['comment'] |
104
|
|
|
: null, |
105
|
|
|
'platformOptions' => [], |
106
|
|
|
]; |
107
|
|
|
|
108
|
|
|
if ($scale !== null && $precision !== null) { |
109
|
|
|
$options['scale'] = $scale; |
110
|
|
|
$options['precision'] = $precision; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
return new Column($tableColumn['colname'], \Doctrine\DBAL\Types\Type::getType($type), $options); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* {@inheritdoc} |
118
|
|
|
*/ |
119
|
1 |
|
protected function _getPortableTablesList($tables) |
120
|
|
|
{ |
121
|
1 |
|
$tableNames = []; |
122
|
1 |
|
foreach ($tables as $tableRow) { |
123
|
1 |
|
$tableRow = array_change_key_case($tableRow, \CASE_LOWER); |
124
|
1 |
|
$tableNames[] = $tableRow['name']; |
125
|
|
|
} |
126
|
|
|
|
127
|
1 |
|
return $tableNames; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* {@inheritdoc} |
132
|
|
|
*/ |
133
|
|
|
protected function _getPortableTableIndexesList($tableIndexRows, $tableName = null) |
134
|
|
|
{ |
135
|
|
|
foreach ($tableIndexRows as &$tableIndexRow) { |
136
|
|
|
$tableIndexRow = array_change_key_case($tableIndexRow, \CASE_LOWER); |
137
|
|
|
$tableIndexRow['primary'] = (boolean) $tableIndexRow['primary']; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
return parent::_getPortableTableIndexesList($tableIndexRows, $tableName); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* {@inheritdoc} |
145
|
|
|
*/ |
146
|
|
View Code Duplication |
protected function _getPortableTableForeignKeyDefinition($tableForeignKey) |
|
|
|
|
147
|
|
|
{ |
148
|
|
|
return new ForeignKeyConstraint( |
149
|
|
|
$tableForeignKey['local_columns'], |
150
|
|
|
$tableForeignKey['foreign_table'], |
151
|
|
|
$tableForeignKey['foreign_columns'], |
152
|
|
|
$tableForeignKey['name'], |
153
|
|
|
$tableForeignKey['options'] |
154
|
|
|
); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* {@inheritdoc} |
159
|
|
|
*/ |
160
|
|
|
protected function _getPortableTableForeignKeysList($tableForeignKeys) |
161
|
|
|
{ |
162
|
|
|
$foreignKeys = []; |
163
|
|
|
|
164
|
|
|
foreach ($tableForeignKeys as $tableForeignKey) { |
165
|
|
|
$tableForeignKey = array_change_key_case($tableForeignKey, \CASE_LOWER); |
166
|
|
|
|
167
|
|
|
if (!isset($foreignKeys[$tableForeignKey['index_name']])) { |
168
|
|
|
$foreignKeys[$tableForeignKey['index_name']] = [ |
169
|
|
|
'local_columns' => [$tableForeignKey['local_column']], |
170
|
|
|
'foreign_table' => $tableForeignKey['foreign_table'], |
171
|
|
|
'foreign_columns' => [$tableForeignKey['foreign_column']], |
172
|
|
|
'name' => $tableForeignKey['index_name'], |
173
|
|
|
'options' => [ |
174
|
|
|
'onUpdate' => $tableForeignKey['on_update'], |
175
|
|
|
'onDelete' => $tableForeignKey['on_delete'], |
176
|
|
|
] |
177
|
|
|
]; |
178
|
|
View Code Duplication |
} else { |
|
|
|
|
179
|
|
|
$foreignKeys[$tableForeignKey['index_name']]['local_columns'][] = $tableForeignKey['local_column']; |
180
|
|
|
$foreignKeys[$tableForeignKey['index_name']]['foreign_columns'][] = $tableForeignKey['foreign_column']; |
181
|
|
|
} |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
return parent::_getPortableTableForeignKeysList($foreignKeys); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* {@inheritdoc} |
189
|
|
|
*/ |
190
|
|
|
protected function _getPortableForeignKeyRuleDef($def) |
191
|
|
|
{ |
192
|
|
|
if ($def == "C") { |
193
|
|
|
return "CASCADE"; |
194
|
|
|
} elseif ($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
|
|
|
// sadly this still segfaults on PDO_IBM, see http://pecl.php.net/bugs/bug.php?id=17199 |
208
|
|
|
//$view['text'] = (is_resource($view['text']) ? stream_get_contents($view['text']) : $view['text']); |
|
|
|
|
209
|
|
|
if (!is_resource($view['text'])) { |
210
|
|
|
$pos = strpos($view['text'], ' AS '); |
211
|
|
|
$sql = substr($view['text'], $pos+4); |
212
|
|
|
} else { |
213
|
|
|
$sql = ''; |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
return new View($view['name'], $sql); |
|
|
|
|
217
|
|
|
} |
218
|
|
|
} |
219
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.