1 | <?php |
---|
2 | /* |
---|
3 | * $Id$ |
---|
4 | * |
---|
5 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
---|
6 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
---|
7 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
---|
8 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
---|
9 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
---|
10 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
---|
11 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
---|
12 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
---|
13 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
---|
14 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
---|
15 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
---|
16 | * |
---|
17 | * This software consists of voluntary contributions made by many individuals |
---|
18 | * and is licensed under the LGPL. For more information, see |
---|
19 | * <http://www.doctrine-project.org>. |
---|
20 | */ |
---|
21 | |
---|
22 | namespace Doctrine\DBAL\Schema; |
---|
23 | |
---|
24 | use Doctrine\DBAL\Event\SchemaIndexDefinitionEventArgs; |
---|
25 | |
---|
26 | /** |
---|
27 | * IBM Db2 Schema Manager |
---|
28 | * |
---|
29 | * @license http://www.opensource.org/licenses/lgpl-license.php LGPL |
---|
30 | * @link www.doctrine-project.com |
---|
31 | * @since 1.0 |
---|
32 | * @version $Revision$ |
---|
33 | * @author Benjamin Eberlei <kontakt@beberlei.de> |
---|
34 | */ |
---|
35 | class DB2SchemaManager extends AbstractSchemaManager |
---|
36 | { |
---|
37 | /** |
---|
38 | * Return a list of all tables in the current database |
---|
39 | * |
---|
40 | * Apparently creator is the schema not the user who created it: |
---|
41 | * {@link http://publib.boulder.ibm.com/infocenter/dzichelp/v2r2/index.jsp?topic=/com.ibm.db29.doc.sqlref/db2z_sysibmsystablestable.htm} |
---|
42 | * |
---|
43 | * @return array |
---|
44 | */ |
---|
45 | public function listTableNames() |
---|
46 | { |
---|
47 | $sql = $this->_platform->getListTablesSQL(); |
---|
48 | $sql .= " AND CREATOR = UPPER('".$this->_conn->getUsername()."')"; |
---|
49 | |
---|
50 | $tables = $this->_conn->fetchAll($sql); |
---|
51 | |
---|
52 | return $this->_getPortableTablesList($tables); |
---|
53 | } |
---|
54 | |
---|
55 | |
---|
56 | /** |
---|
57 | * Get Table Column Definition |
---|
58 | * |
---|
59 | * @param array $tableColumn |
---|
60 | * @return Column |
---|
61 | */ |
---|
62 | protected function _getPortableTableColumnDefinition($tableColumn) |
---|
63 | { |
---|
64 | $tableColumn = array_change_key_case($tableColumn, \CASE_LOWER); |
---|
65 | |
---|
66 | $length = null; |
---|
67 | $fixed = null; |
---|
68 | $unsigned = false; |
---|
69 | $scale = false; |
---|
70 | $precision = false; |
---|
71 | |
---|
72 | $type = $this->_platform->getDoctrineTypeMapping($tableColumn['typename']); |
---|
73 | |
---|
74 | switch (strtolower($tableColumn['typename'])) { |
---|
75 | case 'varchar': |
---|
76 | $length = $tableColumn['length']; |
---|
77 | $fixed = false; |
---|
78 | break; |
---|
79 | case 'character': |
---|
80 | $length = $tableColumn['length']; |
---|
81 | $fixed = true; |
---|
82 | break; |
---|
83 | case 'clob': |
---|
84 | $length = $tableColumn['length']; |
---|
85 | break; |
---|
86 | case 'decimal': |
---|
87 | case 'double': |
---|
88 | case 'real': |
---|
89 | $scale = $tableColumn['scale']; |
---|
90 | $precision = $tableColumn['length']; |
---|
91 | break; |
---|
92 | } |
---|
93 | |
---|
94 | $options = array( |
---|
95 | 'length' => $length, |
---|
96 | 'unsigned' => (bool)$unsigned, |
---|
97 | 'fixed' => (bool)$fixed, |
---|
98 | 'default' => ($tableColumn['default'] == "NULL") ? null : $tableColumn['default'], |
---|
99 | 'notnull' => (bool) ($tableColumn['nulls'] == 'N'), |
---|
100 | 'scale' => null, |
---|
101 | 'precision' => null, |
---|
102 | 'platformOptions' => array(), |
---|
103 | ); |
---|
104 | |
---|
105 | if ($scale !== null && $precision !== null) { |
---|
106 | $options['scale'] = $scale; |
---|
107 | $options['precision'] = $precision; |
---|
108 | } |
---|
109 | |
---|
110 | return new Column($tableColumn['colname'], \Doctrine\DBAL\Types\Type::getType($type), $options); |
---|
111 | } |
---|
112 | |
---|
113 | protected function _getPortableTablesList($tables) |
---|
114 | { |
---|
115 | $tableNames = array(); |
---|
116 | foreach ($tables AS $tableRow) { |
---|
117 | $tableRow = array_change_key_case($tableRow, \CASE_LOWER); |
---|
118 | $tableNames[] = $tableRow['name']; |
---|
119 | } |
---|
120 | return $tableNames; |
---|
121 | } |
---|
122 | |
---|
123 | protected function _getPortableTableIndexesList($tableIndexes, $tableName=null) |
---|
124 | { |
---|
125 | $eventManager = $this->_platform->getEventManager(); |
---|
126 | |
---|
127 | $tableIndexRows = array(); |
---|
128 | $indexes = array(); |
---|
129 | foreach($tableIndexes AS $indexKey => $data) { |
---|
130 | $data = array_change_key_case($data, \CASE_LOWER); |
---|
131 | $unique = ($data['uniquerule'] == "D") ? false : true; |
---|
132 | $primary = ($data['uniquerule'] == "P"); |
---|
133 | |
---|
134 | $indexName = strtolower($data['name']); |
---|
135 | if ($primary) { |
---|
136 | $keyName = 'primary'; |
---|
137 | } else { |
---|
138 | $keyName = $indexName; |
---|
139 | } |
---|
140 | |
---|
141 | $data = array( |
---|
142 | 'name' => $indexName, |
---|
143 | 'columns' => explode("+", ltrim($data['colnames'], '+')), |
---|
144 | 'unique' => $unique, |
---|
145 | 'primary' => $primary |
---|
146 | ); |
---|
147 | |
---|
148 | $index = null; |
---|
149 | $defaultPrevented = false; |
---|
150 | |
---|
151 | if (null !== $eventManager && $eventManager->hasListeners(Events::onSchemaIndexDefinition)) { |
---|
152 | $eventArgs = new SchemaIndexDefinitionEventArgs($data, $tableName, $this->_conn); |
---|
153 | $eventManager->dispatchEvent(Events::onSchemaIndexDefinition, $eventArgs); |
---|
154 | |
---|
155 | $defaultPrevented = $eventArgs->isDefaultPrevented(); |
---|
156 | $index = $eventArgs->getIndex(); |
---|
157 | } |
---|
158 | |
---|
159 | if (!$defaultPrevented) { |
---|
160 | $index = new Index($data['name'], $data['columns'], $data['unique'], $data['primary']); |
---|
161 | } |
---|
162 | |
---|
163 | if ($index) { |
---|
164 | $indexes[$indexKey] = $index; |
---|
165 | } |
---|
166 | } |
---|
167 | |
---|
168 | return $indexes; |
---|
169 | } |
---|
170 | |
---|
171 | protected function _getPortableTableForeignKeyDefinition($tableForeignKey) |
---|
172 | { |
---|
173 | $tableForeignKey = array_change_key_case($tableForeignKey, CASE_LOWER); |
---|
174 | |
---|
175 | $tableForeignKey['deleterule'] = $this->_getPortableForeignKeyRuleDef($tableForeignKey['deleterule']); |
---|
176 | $tableForeignKey['updaterule'] = $this->_getPortableForeignKeyRuleDef($tableForeignKey['updaterule']); |
---|
177 | |
---|
178 | return new ForeignKeyConstraint( |
---|
179 | array_map('trim', (array)$tableForeignKey['fkcolnames']), |
---|
180 | $tableForeignKey['reftbname'], |
---|
181 | array_map('trim', (array)$tableForeignKey['pkcolnames']), |
---|
182 | $tableForeignKey['relname'], |
---|
183 | array( |
---|
184 | 'onUpdate' => $tableForeignKey['updaterule'], |
---|
185 | 'onDelete' => $tableForeignKey['deleterule'], |
---|
186 | ) |
---|
187 | ); |
---|
188 | } |
---|
189 | |
---|
190 | protected function _getPortableForeignKeyRuleDef($def) |
---|
191 | { |
---|
192 | if ($def == "C") { |
---|
193 | return "CASCADE"; |
---|
194 | } else if ($def == "N") { |
---|
195 | return "SET NULL"; |
---|
196 | } |
---|
197 | return null; |
---|
198 | } |
---|
199 | |
---|
200 | protected function _getPortableViewDefinition($view) |
---|
201 | { |
---|
202 | $view = array_change_key_case($view, \CASE_LOWER); |
---|
203 | // sadly this still segfaults on PDO_IBM, see http://pecl.php.net/bugs/bug.php?id=17199 |
---|
204 | //$view['text'] = (is_resource($view['text']) ? stream_get_contents($view['text']) : $view['text']); |
---|
205 | if (!is_resource($view['text'])) { |
---|
206 | $pos = strpos($view['text'], ' AS '); |
---|
207 | $sql = substr($view['text'], $pos+4); |
---|
208 | } else { |
---|
209 | $sql = ''; |
---|
210 | } |
---|
211 | |
---|
212 | return new View($view['name'], $sql); |
---|
213 | } |
---|
214 | } |
---|