MIOLO20
Carregando...
Procurando...
Nenhuma entrada encontrada
DB2Platform.php
Ir para a documentação deste ficheiro.
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 LGPL. For more information, see
17 * <http://www.doctrine-project.org>.
18*/
19
21
25
27{
29 {
30 $this->doctrineTypeMapping = array(
31 'smallint' => 'smallint',
32 'bigint' => 'bigint',
33 'integer' => 'integer',
34 'time' => 'time',
35 'date' => 'date',
36 'varchar' => 'string',
37 'character' => 'string',
38 'clob' => 'text',
39 'decimal' => 'decimal',
40 'double' => 'float',
41 'real' => 'float',
42 'timestamp' => 'datetime',
43 );
44 }
45
51 public function getVarcharTypeDeclarationSQL(array $field)
52 {
53 if ( ! isset($field['length'])) {
54 if (array_key_exists('default', $field)) {
55 $field['length'] = $this->getVarcharDefaultLength();
56 } else {
57 $field['length'] = false;
58 }
59 }
60
61 $length = ($field['length'] <= $this->getVarcharMaxLength()) ? $field['length'] : false;
62 $fixed = (isset($field['fixed'])) ? $field['fixed'] : false;
63
64 return $fixed ? ($length ? 'CHAR(' . $length . ')' : 'CHAR(255)')
65 : ($length ? 'VARCHAR(' . $length . ')' : 'VARCHAR(255)');
66 }
67
73 public function getClobTypeDeclarationSQL(array $field)
74 {
75 // todo clob(n) with $field['length'];
76 return 'CLOB(1M)';
77 }
78
84 public function getName()
85 {
86 return 'db2';
87 }
88
89
96 public function getBooleanTypeDeclarationSQL(array $columnDef)
97 {
98 return 'SMALLINT';
99 }
100
107 public function getIntegerTypeDeclarationSQL(array $columnDef)
108 {
109 return 'INTEGER' . $this->_getCommonIntegerTypeDeclarationSQL($columnDef);
110 }
111
118 public function getBigIntTypeDeclarationSQL(array $columnDef)
119 {
120 return 'BIGINT' . $this->_getCommonIntegerTypeDeclarationSQL($columnDef);
121 }
122
129 public function getSmallIntTypeDeclarationSQL(array $columnDef)
130 {
131 return 'SMALLINT' . $this->_getCommonIntegerTypeDeclarationSQL($columnDef);
132 }
133
140 protected function _getCommonIntegerTypeDeclarationSQL(array $columnDef)
141 {
142 $autoinc = '';
143 if ( ! empty($columnDef['autoincrement'])) {
144 $autoinc = ' GENERATED BY DEFAULT AS IDENTITY';
145 }
146 return $autoinc;
147 }
148
156 public function getDateTimeTypeDeclarationSQL(array $fieldDeclaration)
157 {
158 if (isset($fieldDeclaration['version']) && $fieldDeclaration['version'] == true) {
159 return "TIMESTAMP(0) WITH DEFAULT";
160 }
161
162 return 'TIMESTAMP(0)';
163 }
164
172 public function getDateTypeDeclarationSQL(array $fieldDeclaration)
173 {
174 return 'DATE';
175 }
176
184 public function getTimeTypeDeclarationSQL(array $fieldDeclaration)
185 {
186 return 'TIME';
187 }
188
189 public function getListDatabasesSQL()
190 {
191 throw DBALException::notSupported(__METHOD__);
192 }
193
194 public function getListSequencesSQL($database)
195 {
196 throw DBALException::notSupported(__METHOD__);
197 }
198
199 public function getListTableConstraintsSQL($table)
200 {
201 throw DBALException::notSupported(__METHOD__);
202 }
203
211 public function getListTableColumnsSQL($table)
212 {
213 return "SELECT DISTINCT c.tabschema, c.tabname, c.colname, c.colno,
214 c.typename, c.default, c.nulls, c.length, c.scale,
215 c.identity, tc.type AS tabconsttype, k.colseq
216 FROM syscat.columns c
217 LEFT JOIN (syscat.keycoluse k JOIN syscat.tabconst tc
218 ON (k.tabschema = tc.tabschema
219 AND k.tabname = tc.tabname
220 AND tc.type = 'P'))
221 ON (c.tabschema = k.tabschema
222 AND c.tabname = k.tabname
223 AND c.colname = k.colname)
224 WHERE UPPER(c.tabname) = UPPER('" . $table . "') ORDER BY c.colno";
225 }
226
227 public function getListTablesSQL()
228 {
229 return "SELECT NAME FROM SYSIBM.SYSTABLES WHERE TYPE = 'T'";
230 }
231
232 public function getListUsersSQL()
233 {
234 throw DBALException::notSupported(__METHOD__);
235 }
236
243 public function getListViewsSQL($database)
244 {
245 return "SELECT NAME, TEXT FROM SYSIBM.SYSVIEWS";
246 }
247
248 public function getListTableIndexesSQL($table)
249 {
250 return "SELECT NAME, COLNAMES, UNIQUERULE FROM SYSIBM.SYSINDEXES WHERE TBNAME = UPPER('" . $table . "')";
251 }
252
253 public function getListTableForeignKeysSQL($table)
254 {
255 return "SELECT TBNAME, RELNAME, REFTBNAME, DELETERULE, UPDATERULE, FKCOLNAMES, PKCOLNAMES ".
256 "FROM SYSIBM.SYSRELS WHERE TBNAME = UPPER('".$table."')";
257 }
258
259 public function getCreateViewSQL($name, $sql)
260 {
261 return "CREATE VIEW ".$name." AS ".$sql;
262 }
263
264 public function getDropViewSQL($name)
265 {
266 return "DROP VIEW ".$name;
267 }
268
269 public function getDropSequenceSQL($sequence)
270 {
271 throw DBALException::notSupported(__METHOD__);
272 }
273
274 public function getSequenceNextValSQL($sequenceName)
275 {
276 throw DBALException::notSupported(__METHOD__);
277 }
278
279 public function getCreateDatabaseSQL($database)
280 {
281 return "CREATE DATABASE ".$database;
282 }
283
284 public function getDropDatabaseSQL($database)
285 {
286 return "DROP DATABASE ".$database.";";
287 }
288
290 {
291 return false;
292 }
293
300 {
301 return false;
302 }
303
309 public function getCurrentDateSQL()
310 {
311 return 'VALUES CURRENT DATE';
312 }
313
319 public function getCurrentTimeSQL()
320 {
321 return 'VALUES CURRENT TIME';
322 }
323
330 public function getCurrentTimestampSQL()
331 {
332 return "VALUES CURRENT TIMESTAMP";
333 }
334
343 public function getIndexDeclarationSQL($name, Index $index)
344 {
345 return $this->getUniqueConstraintDeclarationSQL($name, $index);
346 }
347
354 protected function _getCreateTableSQL($tableName, array $columns, array $options = array())
355 {
356 $indexes = array();
357 if (isset($options['indexes'])) {
358 $indexes = $options['indexes'];
359 }
360 $options['indexes'] = array();
361
362 $sqls = parent::_getCreateTableSQL($tableName, $columns, $options);
363
364 foreach ($indexes as $index => $definition) {
365 $sqls[] = $this->getCreateIndexSQL($definition, $tableName);
366 }
367 return $sqls;
368 }
369
376 public function getAlterTableSQL(TableDiff $diff)
377 {
378 $sql = array();
379
380 $queryParts = array();
381 foreach ($diff->addedColumns AS $fieldName => $column) {
382 $queryParts[] = 'ADD COLUMN ' . $this->getColumnDeclarationSQL($column->getQuotedName($this), $column->toArray());
383 }
384
385 foreach ($diff->removedColumns AS $column) {
386 $queryParts[] = 'DROP COLUMN ' . $column->getQuotedName($this);
387 }
388
389 foreach ($diff->changedColumns AS $columnDiff) {
390 /* @var $columnDiff Doctrine\DBAL\Schema\ColumnDiff */
391 $column = $columnDiff->column;
392 $queryParts[] = 'ALTER ' . ($columnDiff->oldColumnName) . ' '
393 . $this->getColumnDeclarationSQL($column->getQuotedName($this), $column->toArray());
394 }
395
396 foreach ($diff->renamedColumns AS $oldColumnName => $column) {
397 $queryParts[] = 'RENAME ' . $oldColumnName . ' TO ' . $column->getQuotedName($this);
398 }
399
400 if (count($queryParts) > 0) {
401 $sql[] = 'ALTER TABLE ' . $diff->name . ' ' . implode(" ", $queryParts);
402 }
403
404 $sql = array_merge($sql, $this->_getAlterTableIndexForeignKeySQL($diff));
405
406 if ($diff->newName !== false) {
407 $sql[] = 'RENAME TABLE TO ' . $diff->newName;
408 }
409
410 return $sql;
411 }
412
413 public function getDefaultValueDeclarationSQL($field)
414 {
415 if (isset($field['notnull']) && $field['notnull'] && !isset($field['default'])) {
416 if (in_array((string)$field['type'], array("Integer", "BigInteger", "SmallInteger"))) {
417 $field['default'] = 0;
418 } else if((string)$field['type'] == "DateTime") {
419 $field['default'] = "00-00-00 00:00:00";
420 } else if ((string)$field['type'] == "Date") {
421 $field['default'] = "00-00-00";
422 } else if((string)$field['type'] == "Time") {
423 $field['default'] = "00:00:00";
424 } else {
425 $field['default'] = '';
426 }
427 }
428
429 unset($field['default']); // @todo this needs fixing
430 if (isset($field['version']) && $field['version']) {
431 if ((string)$field['type'] != "DateTime") {
432 $field['default'] = "1";
433 }
434 }
435
436 return parent::getDefaultValueDeclarationSQL($field);
437 }
438
446 public function getEmptyIdentityInsertSQL($tableName, $identifierColumnName)
447 {
448 return 'INSERT INTO ' . $tableName . ' (' . $identifierColumnName . ') VALUES (DEFAULT)';
449 }
450
452 {
453 return "DECLARE GLOBAL TEMPORARY TABLE";
454 }
455
462 public function getTemporaryTableName($tableName)
463 {
464 return "SESSION." . $tableName;
465 }
466
467 public function modifyLimitQuery($query, $limit, $offset = null)
468 {
469 if ($limit === null && $offset === null) {
470 return $query;
471 }
472
473 $limit = (int)$limit;
474 $offset = (int)(($offset)?:0);
475
476 // Todo OVER() needs ORDER BY data!
477 $sql = 'SELECT db22.* FROM (SELECT ROW_NUMBER() OVER() AS DC_ROWNUM, db21.* '.
478 'FROM (' . $query . ') db21) db22 WHERE db22.DC_ROWNUM BETWEEN ' . ($offset+1) .' AND ' . ($offset+$limit);
479 return $sql;
480 }
481
490 public function getLocateExpression($str, $substr, $startPos = false)
491 {
492 if ($startPos == false) {
493 return 'LOCATE(' . $substr . ', ' . $str . ')';
494 } else {
495 return 'LOCATE(' . $substr . ', ' . $str . ', '.$startPos.')';
496 }
497 }
498
511 public function getSubstringExpression($value, $from, $len = null)
512 {
513 if ($len === null)
514 return 'SUBSTR(' . $value . ', ' . $from . ')';
515 else {
516 return 'SUBSTR(' . $value . ', ' . $from . ', ' . $len . ')';
517 }
518 }
519
520 public function supportsIdentityColumns()
521 {
522 return true;
523 }
524
525 public function prefersIdentityColumns()
526 {
527 return true;
528 }
529
538 public function getSQLResultCasing($column)
539 {
540 return strtoupper($column);
541 }
542
543 public function getForUpdateSQL()
544 {
545 return ' WITH RR USE AND KEEP UPDATE LOCKS';
546 }
547
548 public function getDummySelectSQL()
549 {
550 return 'SELECT 1 FROM sysibm.sysdummy1';
551 }
552
560 public function supportsSavepoints()
561 {
562 return false;
563 }
564}
static notSupported($method)
getUniqueConstraintDeclarationSQL($name, Index $index)
getTimeTypeDeclarationSQL(array $fieldDeclaration)
modifyLimitQuery($query, $limit, $offset=null)
getIndexDeclarationSQL($name, Index $index)
getLocateExpression($str, $substr, $startPos=false)
_getCommonIntegerTypeDeclarationSQL(array $columnDef)
getBooleanTypeDeclarationSQL(array $columnDef)
Definição DB2Platform.php:96
getSmallIntTypeDeclarationSQL(array $columnDef)
getEmptyIdentityInsertSQL($tableName, $identifierColumnName)
getDateTimeTypeDeclarationSQL(array $fieldDeclaration)
getIntegerTypeDeclarationSQL(array $columnDef)
getDateTypeDeclarationSQL(array $fieldDeclaration)
getBigIntTypeDeclarationSQL(array $columnDef)
getSubstringExpression($value, $from, $len=null)
_getCreateTableSQL($tableName, array $columns, array $options=array())