MIOLO20
Carregando...
Procurando...
Nenhuma entrada encontrada
OraclePlatform.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
23
33{
45 public function getSubstringExpression($value, $position, $length = null)
46 {
47 if ($length !== null) {
48 return "SUBSTR($value, $position, $length)";
49 }
50
51 return "SUBSTR($value, $position)";
52 }
53
64 public function getNowExpression($type = 'timestamp')
65 {
66 switch ($type) {
67 case 'date':
68 case 'time':
69 case 'timestamp':
70 default:
71 return 'TO_CHAR(CURRENT_TIMESTAMP, \'YYYY-MM-DD HH24:MI:SS\')';
72 }
73 }
74
83 public function getLocateExpression($str, $substr, $startPos = false)
84 {
85 if ($startPos == false) {
86 return 'INSTR('.$str.', '.$substr.')';
87 } else {
88 return 'INSTR('.$str.', '.$substr.', '.$startPos.')';
89 }
90 }
91
98 public function getGuidExpression()
99 {
100 return 'SYS_GUID()';
101 }
102
114 public function getCreateSequenceSQL(\Doctrine\DBAL\Schema\Sequence $sequence)
115 {
116 return 'CREATE SEQUENCE ' . $sequence->getQuotedName($this) .
117 ' START WITH ' . $sequence->getInitialValue() .
118 ' MINVALUE ' . $sequence->getInitialValue() .
119 ' INCREMENT BY ' . $sequence->getAllocationSize();
120 }
121
128 public function getSequenceNextValSQL($sequenceName)
129 {
130 return 'SELECT ' . $sequenceName . '.nextval FROM DUAL';
131 }
132
139 public function getSetTransactionIsolationSQL($level)
140 {
141 return 'SET TRANSACTION ISOLATION LEVEL ' . $this->_getTransactionIsolationLevelSQL($level);
142 }
143
144 protected function _getTransactionIsolationLevelSQL($level)
145 {
146 switch ($level) {
147 case \Doctrine\DBAL\Connection::TRANSACTION_READ_UNCOMMITTED:
148 return 'READ UNCOMMITTED';
149 case \Doctrine\DBAL\Connection::TRANSACTION_READ_COMMITTED:
150 return 'READ COMMITTED';
151 case \Doctrine\DBAL\Connection::TRANSACTION_REPEATABLE_READ:
152 case \Doctrine\DBAL\Connection::TRANSACTION_SERIALIZABLE:
153 return 'SERIALIZABLE';
154 default:
155 return parent::_getTransactionIsolationLevelSQL($level);
156 }
157 }
158
162 public function getBooleanTypeDeclarationSQL(array $field)
163 {
164 return 'NUMBER(1)';
165 }
166
170 public function getIntegerTypeDeclarationSQL(array $field)
171 {
172 return 'NUMBER(10)';
173 }
174
178 public function getBigIntTypeDeclarationSQL(array $field)
179 {
180 return 'NUMBER(20)';
181 }
182
186 public function getSmallIntTypeDeclarationSQL(array $field)
187 {
188 return 'NUMBER(5)';
189 }
190
194 public function getDateTimeTypeDeclarationSQL(array $fieldDeclaration)
195 {
196 return 'TIMESTAMP(0)';
197 }
198
202 public function getDateTimeTzTypeDeclarationSQL(array $fieldDeclaration)
203 {
204 return 'TIMESTAMP(0) WITH TIME ZONE';
205 }
206
210 public function getDateTypeDeclarationSQL(array $fieldDeclaration)
211 {
212 return 'DATE';
213 }
214
218 public function getTimeTypeDeclarationSQL(array $fieldDeclaration)
219 {
220 return 'DATE';
221 }
222
226 protected function _getCommonIntegerTypeDeclarationSQL(array $columnDef)
227 {
228 return '';
229 }
230
237 public function getVarcharTypeDeclarationSQL(array $field)
238 {
239 if ( ! isset($field['length'])) {
240 if (array_key_exists('default', $field)) {
241 $field['length'] = $this->getVarcharDefaultLength();
242 } else {
243 $field['length'] = false;
244 }
245 }
246
247 $length = ($field['length'] <= $this->getVarcharMaxLength()) ? $field['length'] : false;
248 $fixed = (isset($field['fixed'])) ? $field['fixed'] : false;
249
250 return $fixed ? ($length ? 'CHAR(' . $length . ')' : 'CHAR(2000)')
251 : ($length ? 'VARCHAR2(' . $length . ')' : 'VARCHAR2(4000)');
252 }
253
255 public function getClobTypeDeclarationSQL(array $field)
256 {
257 return 'CLOB';
258 }
259
260 public function getListDatabasesSQL()
261 {
262 return 'SELECT username FROM all_users';
263 }
264
265 public function getListSequencesSQL($database)
266 {
267 return "SELECT sequence_name, min_value, increment_by FROM sys.all_sequences ".
268 "WHERE SEQUENCE_OWNER = '".strtoupper($database)."'";
269 }
270
278 protected function _getCreateTableSQL($table, array $columns, array $options = array())
279 {
280 $indexes = isset($options['indexes']) ? $options['indexes'] : array();
281 $options['indexes'] = array();
282 $sql = parent::_getCreateTableSQL($table, $columns, $options);
283
284 foreach ($columns as $name => $column) {
285 if (isset($column['sequence'])) {
286 $sql[] = $this->getCreateSequenceSQL($column['sequence'], 1);
287 }
288
289 if (isset($column['autoincrement']) && $column['autoincrement'] ||
290 (isset($column['autoinc']) && $column['autoinc'])) {
291 $sql = array_merge($sql, $this->getCreateAutoincrementSql($name, $table));
292 }
293 }
294
295 if (isset($indexes) && ! empty($indexes)) {
296 foreach ($indexes as $indexName => $index) {
297 $sql[] = $this->getCreateIndexSQL($index, $table);
298 }
299 }
300
301 return $sql;
302 }
303
310 public function getListTableIndexesSQL($table)
311 {
312 $table = strtoupper($table);
313
314 return "SELECT uind.index_name AS name, " .
315 " uind.index_type AS type, " .
316 " decode( uind.uniqueness, 'NONUNIQUE', 0, 'UNIQUE', 1 ) AS is_unique, " .
317 " uind_col.column_name AS column_name, " .
318 " uind_col.column_position AS column_pos, " .
319 " (SELECT ucon.constraint_type FROM user_constraints ucon WHERE ucon.constraint_name = uind.index_name) AS is_primary ".
320 "FROM user_indexes uind, user_ind_columns uind_col " .
321 "WHERE uind.index_name = uind_col.index_name AND uind_col.table_name = '$table' ORDER BY uind_col.column_position ASC";
322 }
323
324 public function getListTablesSQL()
325 {
326 return 'SELECT * FROM sys.user_tables';
327 }
328
329 public function getListViewsSQL($database)
330 {
331 return 'SELECT view_name, text FROM sys.user_views';
332 }
333
334 public function getCreateViewSQL($name, $sql)
335 {
336 return 'CREATE VIEW ' . $name . ' AS ' . $sql;
337 }
338
339 public function getDropViewSQL($name)
340 {
341 return 'DROP VIEW '. $name;
342 }
343
344 public function getCreateAutoincrementSql($name, $table, $start = 1)
345 {
346 $table = strtoupper($table);
347 $sql = array();
348
349 $indexName = $table . '_AI_PK';
350 $definition = array(
351 'primary' => true,
352 'columns' => array($name => true),
353 );
354
355 $idx = new \Doctrine\DBAL\Schema\Index($indexName, array($name), true, true);
356
357 $sql[] = 'DECLARE
358 constraints_Count NUMBER;
359BEGIN
360 SELECT COUNT(CONSTRAINT_NAME) INTO constraints_Count FROM USER_CONSTRAINTS WHERE TABLE_NAME = \''.$table.'\' AND CONSTRAINT_TYPE = \'P\';
361 IF constraints_Count = 0 OR constraints_Count = \'\' THEN
362 EXECUTE IMMEDIATE \''.$this->getCreateConstraintSQL($idx, $table).'\';
363 END IF;
364END;';
365
366 $sequenceName = $table . '_SEQ';
367 $sequence = new \Doctrine\DBAL\Schema\Sequence($sequenceName, $start);
368 $sql[] = $this->getCreateSequenceSQL($sequence);
369
370 $triggerName = $table . '_AI_PK';
371 $sql[] = 'CREATE TRIGGER ' . $triggerName . '
372 BEFORE INSERT
373 ON ' . $table . '
374 FOR EACH ROW
375DECLARE
376 last_Sequence NUMBER;
377 last_InsertID NUMBER;
378BEGIN
379 SELECT ' . $sequenceName . '.NEXTVAL INTO :NEW.' . $name . ' FROM DUAL;
380 IF (:NEW.' . $name . ' IS NULL OR :NEW.'.$name.' = 0) THEN
381 SELECT ' . $sequenceName . '.NEXTVAL INTO :NEW.' . $name . ' FROM DUAL;
382 ELSE
383 SELECT NVL(Last_Number, 0) INTO last_Sequence
384 FROM User_Sequences
385 WHERE Sequence_Name = \'' . $sequenceName . '\';
386 SELECT :NEW.' . $name . ' INTO last_InsertID FROM DUAL;
387 WHILE (last_InsertID > last_Sequence) LOOP
388 SELECT ' . $sequenceName . '.NEXTVAL INTO last_Sequence FROM DUAL;
389 END LOOP;
390 END IF;
391END;';
392 return $sql;
393 }
394
395 public function getDropAutoincrementSql($table)
396 {
397 $table = strtoupper($table);
398 $trigger = $table . '_AI_PK';
399
400 if ($trigger) {
401 $sql[] = 'DROP TRIGGER ' . $trigger;
402 $sql[] = $this->getDropSequenceSQL($table.'_SEQ');
403
404 $indexName = $table . '_AI_PK';
405 $sql[] = $this->getDropConstraintSQL($indexName, $table);
406 }
407
408 return $sql;
409 }
410
411 public function getListTableForeignKeysSQL($table)
412 {
413 $table = strtoupper($table);
414
415 return "SELECT alc.constraint_name,
416 alc.DELETE_RULE,
417 alc.search_condition,
418 cols.column_name \"local_column\",
419 cols.position,
420 r_alc.table_name \"references_table\",
421 r_cols.column_name \"foreign_column\"
422 FROM all_cons_columns cols
423LEFT JOIN all_constraints alc
424 ON alc.constraint_name = cols.constraint_name
425 AND alc.owner = cols.owner
426LEFT JOIN all_constraints r_alc
427 ON alc.r_constraint_name = r_alc.constraint_name
428 AND alc.r_owner = r_alc.owner
429LEFT JOIN all_cons_columns r_cols
430 ON r_alc.constraint_name = r_cols.constraint_name
431 AND r_alc.owner = r_cols.owner
432 AND cols.position = r_cols.position
433 WHERE alc.constraint_name = cols.constraint_name
434 AND alc.constraint_type = 'R'
435 AND alc.table_name = '".$table."'";
436 }
437
438 public function getListTableConstraintsSQL($table)
439 {
440 $table = strtoupper($table);
441 return 'SELECT * FROM user_constraints WHERE table_name = \'' . $table . '\'';
442 }
443
444 public function getListTableColumnsSQL($table)
445 {
446 $table = strtoupper($table);
447 return "SELECT * FROM all_tab_columns WHERE table_name = '" . $table . "' ORDER BY column_name";
448 }
449
455 public function getDropSequenceSQL($sequence)
456 {
457 if ($sequence instanceof \Doctrine\DBAL\Schema\Sequence) {
458 $sequence = $sequence->getQuotedName($this);
459 }
460
461 return 'DROP SEQUENCE ' . $sequence;
462 }
463
469 public function getDropForeignKeySQL($foreignKey, $table)
470 {
471 if ($foreignKey instanceof \Doctrine\DBAL\Schema\ForeignKeyConstraint) {
472 $foreignKey = $foreignKey->getQuotedName($this);
473 }
474
475 if ($table instanceof \Doctrine\DBAL\Schema\Table) {
476 $table = $table->getQuotedName($this);
477 }
478
479 return 'ALTER TABLE ' . $table . ' DROP CONSTRAINT ' . $foreignKey;
480 }
481
482 public function getDropDatabaseSQL($database)
483 {
484 return 'DROP USER ' . $database . ' CASCADE';
485 }
486
499 public function getAlterTableSQL(TableDiff $diff)
500 {
501 $sql = array();
502
503 $fields = array();
504 foreach ($diff->addedColumns AS $column) {
505 $fields[] = $this->getColumnDeclarationSQL($column->getQuotedName($this), $column->toArray());
506 }
507 if (count($fields)) {
508 $sql[] = 'ALTER TABLE ' . $diff->name . ' ADD (' . implode(', ', $fields) . ')';
509 }
510
511 $fields = array();
512 foreach ($diff->changedColumns AS $columnDiff) {
513 $column = $columnDiff->column;
514 $fields[] = $column->getQuotedName($this). ' ' . $this->getColumnDeclarationSQL('', $column->toArray());
515 }
516 if (count($fields)) {
517 $sql[] = 'ALTER TABLE ' . $diff->name . ' MODIFY (' . implode(', ', $fields) . ')';
518 }
519
520 foreach ($diff->renamedColumns AS $oldColumnName => $column) {
521 $sql[] = 'ALTER TABLE ' . $diff->name . ' RENAME COLUMN ' . $oldColumnName .' TO ' . $column->getQuotedName($this);
522 }
523
524 $fields = array();
525 foreach ($diff->removedColumns AS $column) {
526 $fields[] = $column->getQuotedName($this);
527 }
528 if (count($fields)) {
529 $sql[] = 'ALTER TABLE ' . $diff->name . ' DROP COLUMN ' . implode(', ', $fields);
530 }
531
532 if ($diff->newName !== false) {
533 $sql[] = 'ALTER TABLE ' . $diff->name . ' RENAME TO ' . $diff->newName;
534 }
535
536 $sql = array_merge($sql, $this->_getAlterTableIndexForeignKeySQL($diff));
537
538 return $sql;
539 }
540
546 public function prefersSequences()
547 {
548 return true;
549 }
550
556 public function getName()
557 {
558 return 'oracle';
559 }
560
569 public function modifyLimitQuery($query, $limit, $offset = null)
570 {
571 $limit = (int) $limit;
572 $offset = (int) $offset;
573 if (preg_match('/^\s*SELECT/i', $query)) {
574 if ( ! preg_match('/\sFROM\s/i', $query)) {
575 $query .= " FROM dual";
576 }
577 if ($limit > 0) {
578 $max = $offset + $limit;
579 $column = '*';
580 if ($offset > 0) {
581 $min = $offset + 1;
582 $query = 'SELECT b.'.$column.' FROM ('.
583 'SELECT a.*, ROWNUM AS doctrine_rownum FROM ('
584 . $query . ') a '.
585 ') b '.
586 'WHERE doctrine_rownum BETWEEN ' . $min . ' AND ' . $max;
587 } else {
588 $query = 'SELECT a.'.$column.' FROM (' . $query .') a WHERE ROWNUM <= ' . $max;
589 }
590 }
591 }
592 return $query;
593 }
594
603 public function getSQLResultCasing($column)
604 {
605 return strtoupper($column);
606 }
607
609 {
610 return "CREATE GLOBAL TEMPORARY TABLE";
611 }
612
614 {
615 return 'Y-m-d H:i:sP';
616 }
617
618 public function getDateFormatString()
619 {
620 return 'Y-m-d 00:00:00';
621 }
622
623 public function getTimeFormatString()
624 {
625 return '1900-01-01 H:i:s';
626 }
627
628 public function fixSchemaElementName($schemaElementName)
629 {
630 if (strlen($schemaElementName) > 30) {
631 // Trim it
632 return substr($schemaElementName, 0, 30);
633 }
634 return $schemaElementName;
635 }
636
642 public function getMaxIdentifierLength()
643 {
644 return 30;
645 }
646
652 public function supportsSequences()
653 {
654 return true;
655 }
656
658 {
659 return false;
660 }
661
668 {
669 return false;
670 }
671
675 public function getTruncateTableSQL($tableName, $cascade = false)
676 {
677 return 'TRUNCATE TABLE '.$tableName;
678 }
679
685 public function getDummySelectSQL()
686 {
687 return 'SELECT 1 FROM DUAL';
688 }
689
691 {
692 $this->doctrineTypeMapping = array(
693 'integer' => 'integer',
694 'number' => 'integer',
695 'pls_integer' => 'boolean',
696 'binary_integer' => 'boolean',
697 'varchar' => 'string',
698 'varchar2' => 'string',
699 'nvarchar2' => 'string',
700 'char' => 'string',
701 'nchar' => 'string',
702 'date' => 'datetime',
703 'timestamp' => 'datetime',
704 'timestamptz' => 'datetimetz',
705 'float' => 'float',
706 'long' => 'string',
707 'clob' => 'text',
708 'nclob' => 'text',
709 'rowid' => 'string',
710 'urowid' => 'string'
711 );
712 }
713
720 public function releaseSavePoint($savepoint)
721 {
722 return '';
723 }
724}
getTimeTypeDeclarationSQL(array $fieldDeclaration)
modifyLimitQuery($query, $limit, $offset=null)
_getCreateTableSQL($table, array $columns, array $options=array())
getLocateExpression($str, $substr, $startPos=false)
getSubstringExpression($value, $position, $length=null)
getCreateSequenceSQL(\Doctrine\DBAL\Schema\Sequence $sequence)
_getCommonIntegerTypeDeclarationSQL(array $columnDef)
getDateTimeTypeDeclarationSQL(array $fieldDeclaration)
getDateTimeTzTypeDeclarationSQL(array $fieldDeclaration)
getDateTypeDeclarationSQL(array $fieldDeclaration)
getCreateAutoincrementSql($name, $table, $start=1)
getTruncateTableSQL($tableName, $cascade=false)