|
0
|
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 |
/** |
|
|
25 |
* IBM Db2 Schema Manager |
|
|
26 |
* |
|
|
27 |
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL |
|
|
28 |
* @link www.doctrine-project.com |
|
|
29 |
* @since 1.0 |
|
|
30 |
* @version $Revision$ |
|
|
31 |
* @author Benjamin Eberlei <kontakt@beberlei.de> |
|
|
32 |
*/ |
|
|
33 |
class DB2SchemaManager extends AbstractSchemaManager |
|
|
34 |
{ |
|
|
35 |
/** |
|
|
36 |
* Return a list of all tables in the current database |
|
|
37 |
* |
|
|
38 |
* Apparently creator is the schema not the user who created it: |
|
|
39 |
* {@link http://publib.boulder.ibm.com/infocenter/dzichelp/v2r2/index.jsp?topic=/com.ibm.db29.doc.sqlref/db2z_sysibmsystablestable.htm} |
|
|
40 |
* |
|
|
41 |
* @return array |
|
|
42 |
*/ |
|
|
43 |
public function listTableNames() |
|
|
44 |
{ |
|
|
45 |
$sql = $this->_platform->getListTablesSQL(); |
|
|
46 |
$sql .= " AND CREATOR = UPPER('".$this->_conn->getUsername()."')"; |
|
|
47 |
|
|
|
48 |
$tables = $this->_conn->fetchAll($sql); |
|
|
49 |
|
|
|
50 |
return $this->_getPortableTablesList($tables); |
|
|
51 |
} |
|
|
52 |
|
|
|
53 |
|
|
|
54 |
/** |
|
|
55 |
* Get Table Column Definition |
|
|
56 |
* |
|
|
57 |
* @param array $tableColumn |
|
|
58 |
* @return Column |
|
|
59 |
*/ |
|
|
60 |
protected function _getPortableTableColumnDefinition($tableColumn) |
|
|
61 |
{ |
|
|
62 |
$tableColumn = array_change_key_case($tableColumn, \CASE_LOWER); |
|
|
63 |
|
|
|
64 |
$length = null; |
|
|
65 |
$fixed = null; |
|
|
66 |
$unsigned = false; |
|
|
67 |
$scale = false; |
|
|
68 |
$precision = false; |
|
|
69 |
|
|
|
70 |
$type = $this->_platform->getDoctrineTypeMapping($tableColumn['typename']); |
|
|
71 |
|
|
|
72 |
switch (strtolower($tableColumn['typename'])) { |
|
|
73 |
case 'varchar': |
|
|
74 |
$length = $tableColumn['length']; |
|
|
75 |
$fixed = false; |
|
|
76 |
break; |
|
|
77 |
case 'character': |
|
|
78 |
$length = $tableColumn['length']; |
|
|
79 |
$fixed = true; |
|
|
80 |
break; |
|
|
81 |
case 'clob': |
|
|
82 |
$length = $tableColumn['length']; |
|
|
83 |
break; |
|
|
84 |
case 'decimal': |
|
|
85 |
case 'double': |
|
|
86 |
case 'real': |
|
|
87 |
$scale = $tableColumn['scale']; |
|
|
88 |
$precision = $tableColumn['length']; |
|
|
89 |
break; |
|
|
90 |
} |
|
|
91 |
|
|
|
92 |
$options = array( |
|
|
93 |
'length' => $length, |
|
|
94 |
'unsigned' => (bool)$unsigned, |
|
|
95 |
'fixed' => (bool)$fixed, |
|
|
96 |
'default' => ($tableColumn['default'] == "NULL") ? null : $tableColumn['default'], |
|
|
97 |
'notnull' => (bool) ($tableColumn['nulls'] == 'N'), |
|
|
98 |
'scale' => null, |
|
|
99 |
'precision' => null, |
|
|
100 |
'platformOptions' => array(), |
|
|
101 |
); |
|
|
102 |
|
|
|
103 |
if ($scale !== null && $precision !== null) { |
|
|
104 |
$options['scale'] = $scale; |
|
|
105 |
$options['precision'] = $precision; |
|
|
106 |
} |
|
|
107 |
|
|
|
108 |
return new Column($tableColumn['colname'], \Doctrine\DBAL\Types\Type::getType($type), $options); |
|
|
109 |
} |
|
|
110 |
|
|
|
111 |
protected function _getPortableTablesList($tables) |
|
|
112 |
{ |
|
|
113 |
$tableNames = array(); |
|
|
114 |
foreach ($tables AS $tableRow) { |
|
|
115 |
$tableRow = array_change_key_case($tableRow, \CASE_LOWER); |
|
|
116 |
$tableNames[] = $tableRow['name']; |
|
|
117 |
} |
|
|
118 |
return $tableNames; |
|
|
119 |
} |
|
|
120 |
|
|
|
121 |
protected function _getPortableTableIndexesList($tableIndexes, $tableName=null) |
|
|
122 |
{ |
|
|
123 |
$tableIndexRows = array(); |
|
|
124 |
$indexes = array(); |
|
|
125 |
foreach($tableIndexes AS $indexKey => $data) { |
|
|
126 |
$data = array_change_key_case($data, \CASE_LOWER); |
|
|
127 |
$unique = ($data['uniquerule'] == "D") ? false : true; |
|
|
128 |
$primary = ($data['uniquerule'] == "P"); |
|
|
129 |
|
|
|
130 |
$indexName = strtolower($data['name']); |
|
|
131 |
if ($primary) { |
|
|
132 |
$keyName = 'primary'; |
|
|
133 |
} else { |
|
|
134 |
$keyName = $indexName; |
|
|
135 |
} |
|
|
136 |
|
|
|
137 |
$indexes[$keyName] = new Index($indexName, explode("+", ltrim($data['colnames'], '+')), $unique, $primary); |
|
|
138 |
} |
|
|
139 |
|
|
|
140 |
return $indexes; |
|
|
141 |
} |
|
|
142 |
|
|
|
143 |
protected function _getPortableTableForeignKeyDefinition($tableForeignKey) |
|
|
144 |
{ |
|
|
145 |
$tableForeignKey = array_change_key_case($tableForeignKey, CASE_LOWER); |
|
|
146 |
|
|
|
147 |
$tableForeignKey['deleterule'] = $this->_getPortableForeignKeyRuleDef($tableForeignKey['deleterule']); |
|
|
148 |
$tableForeignKey['updaterule'] = $this->_getPortableForeignKeyRuleDef($tableForeignKey['updaterule']); |
|
|
149 |
|
|
|
150 |
return new ForeignKeyConstraint( |
|
|
151 |
array_map('trim', (array)$tableForeignKey['fkcolnames']), |
|
|
152 |
$tableForeignKey['reftbname'], |
|
|
153 |
array_map('trim', (array)$tableForeignKey['pkcolnames']), |
|
|
154 |
$tableForeignKey['relname'], |
|
|
155 |
array( |
|
|
156 |
'onUpdate' => $tableForeignKey['updaterule'], |
|
|
157 |
'onDelete' => $tableForeignKey['deleterule'], |
|
|
158 |
) |
|
|
159 |
); |
|
|
160 |
} |
|
|
161 |
|
|
|
162 |
protected function _getPortableForeignKeyRuleDef($def) |
|
|
163 |
{ |
|
|
164 |
if ($def == "C") { |
|
|
165 |
return "CASCADE"; |
|
|
166 |
} else if ($def == "N") { |
|
|
167 |
return "SET NULL"; |
|
|
168 |
} |
|
|
169 |
return null; |
|
|
170 |
} |
|
|
171 |
|
|
|
172 |
protected function _getPortableViewDefinition($view) |
|
|
173 |
{ |
|
|
174 |
$view = array_change_key_case($view, \CASE_LOWER); |
|
|
175 |
// sadly this still segfaults on PDO_IBM, see http://pecl.php.net/bugs/bug.php?id=17199 |
|
|
176 |
//$view['text'] = (is_resource($view['text']) ? stream_get_contents($view['text']) : $view['text']); |
|
|
177 |
if (!is_resource($view['text'])) { |
|
|
178 |
$pos = strpos($view['text'], ' AS '); |
|
|
179 |
$sql = substr($view['text'], $pos+4); |
|
|
180 |
} else { |
|
|
181 |
$sql = ''; |
|
|
182 |
} |
|
|
183 |
|
|
|
184 |
return new View($view['name'], $sql); |
|
|
185 |
} |
|
|
186 |
} |