MIOLO20
Carregando...
Procurando...
Nenhuma entrada encontrada
PostgreSqlSchemaManager.php
Ir para a documentação deste ficheiro.
1<?php
2
3/*
4 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
5 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
6 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
7 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
8 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
9 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
10 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
11 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
12 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
13 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
14 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
15 *
16 * This software consists of voluntary contributions made by many individuals
17 * and is licensed under the LGPL. For more information, see
18 * <http://www.doctrine-project.org>.
19 */
20
21namespace Doctrine\DBAL\Schema;
22
34{
35
36 protected function _getPortableTableForeignKeyDefinition($tableForeignKey)
37 {
38 $onUpdate = null;
39 $onDelete = null;
40
41 if (preg_match('(ON UPDATE ([a-zA-Z0-9]+))', $tableForeignKey['condef'], $match)) {
42 $onUpdate = $match[1];
43 }
44 if (preg_match('(ON DELETE ([a-zA-Z0-9]+))', $tableForeignKey['condef'], $match)) {
45 $onDelete = $match[1];
46 }
47
48 if (preg_match('/FOREIGN KEY \‍((.+)\‍) REFERENCES (.+)\‍((.+)\‍)/', $tableForeignKey['condef'], $values)) {
49 $localColumns = array_map('trim', explode(",", $values[1]));
50 $foreignColumns = array_map('trim', explode(",", $values[3]));
51 $foreignTable = $values[2];
52 }
53
54 return new ForeignKeyConstraint(
55 $localColumns, $foreignTable, $foreignColumns, $tableForeignKey['conname'],
56 array('onUpdate' => $onUpdate, 'onDelete' => $onDelete)
57 );
58 }
59
60 public function dropDatabase($database)
61 {
62 $params = $this->_conn->getParams();
63 $params["dbname"] = "postgres";
64 $tmpPlatform = $this->_platform;
65 $tmpConn = $this->_conn;
66
67 $this->_conn = \Doctrine\DBAL\DriverManager::getConnection($params);
68 $this->_platform = $this->_conn->getDatabasePlatform();
69
70 parent::dropDatabase($database);
71
72 $this->_platform = $tmpPlatform;
73 $this->_conn = $tmpConn;
74 }
75
76 public function createDatabase($database)
77 {
78 $params = $this->_conn->getParams();
79 $params["dbname"] = "postgres";
80 $tmpPlatform = $this->_platform;
81 $tmpConn = $this->_conn;
82
83 $this->_conn = \Doctrine\DBAL\DriverManager::getConnection($params);
84 $this->_platform = $this->_conn->getDatabasePlatform();
85
86 parent::createDatabase($database);
87
88 $this->_platform = $tmpPlatform;
89 $this->_conn = $tmpConn;
90 }
91
92 protected function _getPortableTriggerDefinition($trigger)
93 {
94 return $trigger['trigger_name'];
95 }
96
97 protected function _getPortableViewDefinition($view)
98 {
99 return new View($view['viewname'], $view['definition']);
100 }
101
102 protected function _getPortableUserDefinition($user)
103 {
104 return array(
105 'user' => $user['usename'],
106 'password' => $user['passwd']
107 );
108 }
109
110 protected function _getPortableTableDefinition($table)
111 {
112 if ($table['schema_name'] == 'public') {
113 return $table['table_name'];
114 } else {
115 return $table['schema_name'] . "." . $table['table_name'];
116 }
117 }
118
126 protected function _getPortableTableIndexesList($tableIndexes, $tableName=null)
127 {
128 $buffer = array();
129 foreach ($tableIndexes AS $row) {
130 $colNumbers = explode(' ', $row['indkey']);
131 $colNumbersSql = 'IN (' . join(' ,', $colNumbers) . ' )';
132 $columnNameSql = "SELECT attnum, attname FROM pg_attribute
133 WHERE attrelid={$row['indrelid']} AND attnum $colNumbersSql ORDER BY attnum ASC;";
134
135 $stmt = $this->_conn->executeQuery($columnNameSql);
136 $indexColumns = $stmt->fetchAll();
137
138 // required for getting the order of the columns right.
139 foreach ($colNumbers AS $colNum) {
140 foreach ($indexColumns as $colRow) {
141 if ($colNum == $colRow['attnum']) {
142 $buffer[] = array(
143 'key_name' => $row['relname'],
144 'column_name' => trim($colRow['attname']),
145 'non_unique' => !$row['indisunique'],
146 'primary' => $row['indisprimary']
147 );
148 }
149 }
150 }
151 }
152 return parent::_getPortableTableIndexesList($buffer);
153 }
154
155 protected function _getPortableDatabaseDefinition($database)
156 {
157 return $database['datname'];
158 }
159
160 protected function _getPortableSequenceDefinition($sequence)
161 {
162 if ($sequence['schemaname'] != 'public') {
163 $sequenceName = $sequence['schemaname'] . "." . $sequence['relname'];
164 } else {
165 $sequenceName = $sequence['relname'];
166 }
167
168 $data = $this->_conn->fetchAll('SELECT min_value, increment_by FROM ' . $sequenceName);
169 return new Sequence($sequenceName, $data[0]['increment_by'], $data[0]['min_value']);
170 }
171
172 protected function _getPortableTableColumnDefinition($tableColumn)
173 {
174 $tableColumn = array_change_key_case($tableColumn, CASE_LOWER);
175
176 if (strtolower($tableColumn['type']) === 'varchar') {
177 // get length from varchar definition
178 $length = preg_replace('~.*\‍(([0-9]*)\‍).*~', '$1', $tableColumn['complete_type']);
179 $tableColumn['length'] = $length;
180 }
181
182 $matches = array();
183
184 $autoincrement = false;
185 if (preg_match("/^nextval\‍('(.*)'(::.*)?\‍)$/", $tableColumn['default'], $matches)) {
186 $tableColumn['sequence'] = $matches[1];
187 $tableColumn['default'] = null;
188 $autoincrement = true;
189 }
190
191 if (stripos($tableColumn['default'], 'NULL') === 0) {
192 $tableColumn['default'] = null;
193 }
194
195 $length = (isset($tableColumn['length'])) ? $tableColumn['length'] : null;
196 if ($length == '-1' && isset($tableColumn['atttypmod'])) {
197 $length = $tableColumn['atttypmod'] - 4;
198 }
199 if ((int) $length <= 0) {
200 $length = null;
201 }
202 $type = array();
203 $fixed = null;
204
205 if (!isset($tableColumn['name'])) {
206 $tableColumn['name'] = '';
207 }
208
209 $precision = null;
210 $scale = null;
211
212 if ($this->_platform->hasDoctrineTypeMappingFor($tableColumn['type'])) {
213 $dbType = strtolower($tableColumn['type']);
214 } else {
215 $dbType = strtolower($tableColumn['domain_type']);
216 $tableColumn['complete_type'] = $tableColumn['domain_complete_type'];
217 }
218
219 $type = $this->_platform->getDoctrineTypeMapping($dbType);
220 switch ($dbType) {
221 case 'smallint':
222 case 'int2':
223 $length = null;
224 break;
225 case 'int':
226 case 'int4':
227 case 'integer':
228 $length = null;
229 break;
230 case 'bigint':
231 case 'int8':
232 $length = null;
233 break;
234 case 'bool':
235 case 'boolean':
236 $length = null;
237 break;
238 case 'text':
239 $fixed = false;
240 break;
241 case 'varchar':
242 case 'interval':
243 case '_varchar':
244 $fixed = false;
245 break;
246 case 'char':
247 case 'bpchar':
248 $fixed = true;
249 break;
250 case 'float':
251 case 'float4':
252 case 'float8':
253 case 'double':
254 case 'double precision':
255 case 'real':
256 case 'decimal':
257 case 'money':
258 case 'numeric':
259 if (preg_match('([A-Za-z]+\‍(([0-9]+)\,([0-9]+)\‍))', $tableColumn['complete_type'], $match)) {
260 $precision = $match[1];
261 $scale = $match[2];
262 $length = null;
263 }
264 break;
265 case 'year':
266 $length = null;
267 break;
268 }
269
270 $options = array(
271 'length' => $length,
272 'notnull' => (bool) $tableColumn['isnotnull'],
273 'default' => $tableColumn['default'],
274 'primary' => (bool) ($tableColumn['pri'] == 't'),
275 'precision' => $precision,
276 'scale' => $scale,
277 'fixed' => $fixed,
278 'unsigned' => false,
279 'autoincrement' => $autoincrement,
280 );
281
282 return new Column($tableColumn['field'], \Doctrine\DBAL\Types\Type::getType($type), $options);
283 }
284
285}
_getPortableTableIndexesList($tableIndexes, $tableName=null)