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 | |
---|
20 | namespace Doctrine\DBAL\Platforms; |
---|
21 | |
---|
22 | use Doctrine\DBAL\Schema\TableDiff; |
---|
23 | |
---|
24 | /** |
---|
25 | * OraclePlatform. |
---|
26 | * |
---|
27 | * @since 2.0 |
---|
28 | * @author Roman Borschel <roman@code-factory.org> |
---|
29 | * @author Lukas Smith <smith@pooteeweet.org> (PEAR MDB2 library) |
---|
30 | * @author Benjamin Eberlei <kontakt@beberlei.de> |
---|
31 | */ |
---|
32 | class OraclePlatform extends AbstractPlatform |
---|
33 | { |
---|
34 | /** |
---|
35 | * return string to call a function to get a substring inside an SQL statement |
---|
36 | * |
---|
37 | * Note: Not SQL92, but common functionality. |
---|
38 | * |
---|
39 | * @param string $value an sql string literal or column name/alias |
---|
40 | * @param integer $position where to start the substring portion |
---|
41 | * @param integer $length the substring portion length |
---|
42 | * @return string SQL substring function with given parameters |
---|
43 | * @override |
---|
44 | */ |
---|
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 | |
---|
54 | /** |
---|
55 | * Return string to call a variable with the current timestamp inside an SQL statement |
---|
56 | * There are three special variables for current date and time: |
---|
57 | * - CURRENT_TIMESTAMP (date and time, TIMESTAMP type) |
---|
58 | * - CURRENT_DATE (date, DATE type) |
---|
59 | * - CURRENT_TIME (time, TIME type) |
---|
60 | * |
---|
61 | * @return string to call a variable with the current timestamp |
---|
62 | * @override |
---|
63 | */ |
---|
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 | |
---|
75 | /** |
---|
76 | * returns the position of the first occurrence of substring $substr in string $str |
---|
77 | * |
---|
78 | * @param string $substr literal string to find |
---|
79 | * @param string $str literal string |
---|
80 | * @param int $pos position to start at, beginning of string by default |
---|
81 | * @return integer |
---|
82 | */ |
---|
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 | |
---|
92 | /** |
---|
93 | * Returns global unique identifier |
---|
94 | * |
---|
95 | * @return string to get global unique identifier |
---|
96 | * @override |
---|
97 | */ |
---|
98 | public function getGuidExpression() |
---|
99 | { |
---|
100 | return 'SYS_GUID()'; |
---|
101 | } |
---|
102 | |
---|
103 | /** |
---|
104 | * Get the number of days difference between two dates. |
---|
105 | * |
---|
106 | * Note: Since Oracle timestamp differences are calculated down to the microsecond we have to truncate |
---|
107 | * them to the difference in days. This is obviously a restriction of the original functionality, but we |
---|
108 | * need to make this a portable function. |
---|
109 | * |
---|
110 | * @param type $date1 |
---|
111 | * @param type $date2 |
---|
112 | * @return type |
---|
113 | */ |
---|
114 | public function getDateDiffExpression($date1, $date2) |
---|
115 | { |
---|
116 | return "TRUNC(TO_NUMBER(SUBSTR((" . $date1 . "-" . $date2 . "), 1, INSTR(" . $date1 . "-" . $date2 .", ' '))))"; |
---|
117 | } |
---|
118 | |
---|
119 | /** |
---|
120 | * {@inheritdoc} |
---|
121 | */ |
---|
122 | public function getDateAddDaysExpression($date, $days) |
---|
123 | { |
---|
124 | return '(' . $date . '+' . $days . ')'; |
---|
125 | } |
---|
126 | |
---|
127 | /** |
---|
128 | * {@inheritdoc} |
---|
129 | */ |
---|
130 | public function getDateSubDaysExpression($date, $days) |
---|
131 | { |
---|
132 | return '(' . $date . '-' . $days . ')'; |
---|
133 | } |
---|
134 | |
---|
135 | /** |
---|
136 | * {@inheritdoc} |
---|
137 | */ |
---|
138 | public function getDateAddMonthExpression($date, $months) |
---|
139 | { |
---|
140 | return "ADD_MONTHS(" . $date . ", " . $months . ")"; |
---|
141 | } |
---|
142 | |
---|
143 | /** |
---|
144 | * {@inheritdoc} |
---|
145 | */ |
---|
146 | public function getDateSubMonthExpression($date, $months) |
---|
147 | { |
---|
148 | return "ADD_MONTHS(" . $date . ", -" . $months . ")"; |
---|
149 | } |
---|
150 | |
---|
151 | /** |
---|
152 | * {@inheritdoc} |
---|
153 | */ |
---|
154 | public function getBitAndComparisonExpression($value1, $value2) |
---|
155 | { |
---|
156 | return 'BITAND('.$value1 . ', ' . $value2 . ')'; |
---|
157 | } |
---|
158 | |
---|
159 | /** |
---|
160 | * {@inheritdoc} |
---|
161 | */ |
---|
162 | public function getBitOrComparisonExpression($value1, $value2) |
---|
163 | { |
---|
164 | return '(' . $value1 . '-' . |
---|
165 | $this->getBitAndComparisonExpression($value1, $value2) |
---|
166 | . '+' . $value2 . ')'; |
---|
167 | } |
---|
168 | |
---|
169 | /** |
---|
170 | * Gets the SQL used to create a sequence that starts with a given value |
---|
171 | * and increments by the given allocation size. |
---|
172 | * |
---|
173 | * Need to specifiy minvalue, since start with is hidden in the system and MINVALUE <= START WITH. |
---|
174 | * Therefore we can use MINVALUE to be able to get a hint what START WITH was for later introspection |
---|
175 | * in {@see listSequences()} |
---|
176 | * |
---|
177 | * @param \Doctrine\DBAL\Schema\Sequence $sequence |
---|
178 | * @return string |
---|
179 | */ |
---|
180 | public function getCreateSequenceSQL(\Doctrine\DBAL\Schema\Sequence $sequence) |
---|
181 | { |
---|
182 | return 'CREATE SEQUENCE ' . $sequence->getQuotedName($this) . |
---|
183 | ' START WITH ' . $sequence->getInitialValue() . |
---|
184 | ' MINVALUE ' . $sequence->getInitialValue() . |
---|
185 | ' INCREMENT BY ' . $sequence->getAllocationSize(); |
---|
186 | } |
---|
187 | |
---|
188 | public function getAlterSequenceSQL(\Doctrine\DBAL\Schema\Sequence $sequence) |
---|
189 | { |
---|
190 | return 'ALTER SEQUENCE ' . $sequence->getQuotedName($this) . |
---|
191 | ' INCREMENT BY ' . $sequence->getAllocationSize(); |
---|
192 | } |
---|
193 | |
---|
194 | /** |
---|
195 | * {@inheritdoc} |
---|
196 | * |
---|
197 | * @param string $sequenceName |
---|
198 | * @override |
---|
199 | */ |
---|
200 | public function getSequenceNextValSQL($sequenceName) |
---|
201 | { |
---|
202 | return 'SELECT ' . $sequenceName . '.nextval FROM DUAL'; |
---|
203 | } |
---|
204 | |
---|
205 | /** |
---|
206 | * {@inheritdoc} |
---|
207 | * |
---|
208 | * @param integer $level |
---|
209 | * @override |
---|
210 | */ |
---|
211 | public function getSetTransactionIsolationSQL($level) |
---|
212 | { |
---|
213 | return 'SET TRANSACTION ISOLATION LEVEL ' . $this->_getTransactionIsolationLevelSQL($level); |
---|
214 | } |
---|
215 | |
---|
216 | protected function _getTransactionIsolationLevelSQL($level) |
---|
217 | { |
---|
218 | switch ($level) { |
---|
219 | case \Doctrine\DBAL\Connection::TRANSACTION_READ_UNCOMMITTED: |
---|
220 | return 'READ UNCOMMITTED'; |
---|
221 | case \Doctrine\DBAL\Connection::TRANSACTION_READ_COMMITTED: |
---|
222 | return 'READ COMMITTED'; |
---|
223 | case \Doctrine\DBAL\Connection::TRANSACTION_REPEATABLE_READ: |
---|
224 | case \Doctrine\DBAL\Connection::TRANSACTION_SERIALIZABLE: |
---|
225 | return 'SERIALIZABLE'; |
---|
226 | default: |
---|
227 | return parent::_getTransactionIsolationLevelSQL($level); |
---|
228 | } |
---|
229 | } |
---|
230 | |
---|
231 | /** |
---|
232 | * @override |
---|
233 | */ |
---|
234 | public function getBooleanTypeDeclarationSQL(array $field) |
---|
235 | { |
---|
236 | return 'NUMBER(1)'; |
---|
237 | } |
---|
238 | |
---|
239 | /** |
---|
240 | * @override |
---|
241 | */ |
---|
242 | public function getIntegerTypeDeclarationSQL(array $field) |
---|
243 | { |
---|
244 | return 'NUMBER(10)'; |
---|
245 | } |
---|
246 | |
---|
247 | /** |
---|
248 | * @override |
---|
249 | */ |
---|
250 | public function getBigIntTypeDeclarationSQL(array $field) |
---|
251 | { |
---|
252 | return 'NUMBER(20)'; |
---|
253 | } |
---|
254 | |
---|
255 | /** |
---|
256 | * @override |
---|
257 | */ |
---|
258 | public function getSmallIntTypeDeclarationSQL(array $field) |
---|
259 | { |
---|
260 | return 'NUMBER(5)'; |
---|
261 | } |
---|
262 | |
---|
263 | /** |
---|
264 | * @override |
---|
265 | */ |
---|
266 | public function getDateTimeTypeDeclarationSQL(array $fieldDeclaration) |
---|
267 | { |
---|
268 | return 'TIMESTAMP(0)'; |
---|
269 | } |
---|
270 | |
---|
271 | /** |
---|
272 | * @override |
---|
273 | */ |
---|
274 | public function getDateTimeTzTypeDeclarationSQL(array $fieldDeclaration) |
---|
275 | { |
---|
276 | return 'TIMESTAMP(0) WITH TIME ZONE'; |
---|
277 | } |
---|
278 | |
---|
279 | /** |
---|
280 | * @override |
---|
281 | */ |
---|
282 | public function getDateTypeDeclarationSQL(array $fieldDeclaration) |
---|
283 | { |
---|
284 | return 'DATE'; |
---|
285 | } |
---|
286 | |
---|
287 | /** |
---|
288 | * @override |
---|
289 | */ |
---|
290 | public function getTimeTypeDeclarationSQL(array $fieldDeclaration) |
---|
291 | { |
---|
292 | return 'DATE'; |
---|
293 | } |
---|
294 | |
---|
295 | /** |
---|
296 | * @override |
---|
297 | */ |
---|
298 | protected function _getCommonIntegerTypeDeclarationSQL(array $columnDef) |
---|
299 | { |
---|
300 | return ''; |
---|
301 | } |
---|
302 | |
---|
303 | /** |
---|
304 | * Gets the SQL snippet used to declare a VARCHAR column on the Oracle platform. |
---|
305 | * |
---|
306 | * @params array $field |
---|
307 | * @override |
---|
308 | */ |
---|
309 | protected function getVarcharTypeDeclarationSQLSnippet($length, $fixed) |
---|
310 | { |
---|
311 | return $fixed ? ($length ? 'CHAR(' . $length . ')' : 'CHAR(2000)') |
---|
312 | : ($length ? 'VARCHAR2(' . $length . ')' : 'VARCHAR2(4000)'); |
---|
313 | } |
---|
314 | |
---|
315 | /** @override */ |
---|
316 | public function getClobTypeDeclarationSQL(array $field) |
---|
317 | { |
---|
318 | return 'CLOB'; |
---|
319 | } |
---|
320 | |
---|
321 | public function getListDatabasesSQL() |
---|
322 | { |
---|
323 | return 'SELECT username FROM all_users'; |
---|
324 | } |
---|
325 | |
---|
326 | public function getListSequencesSQL($database) |
---|
327 | { |
---|
328 | return "SELECT sequence_name, min_value, increment_by FROM sys.all_sequences ". |
---|
329 | "WHERE SEQUENCE_OWNER = '".strtoupper($database)."'"; |
---|
330 | } |
---|
331 | |
---|
332 | /** |
---|
333 | * |
---|
334 | * @param string $table |
---|
335 | * @param array $columns |
---|
336 | * @param array $options |
---|
337 | * @return array |
---|
338 | */ |
---|
339 | protected function _getCreateTableSQL($table, array $columns, array $options = array()) |
---|
340 | { |
---|
341 | $indexes = isset($options['indexes']) ? $options['indexes'] : array(); |
---|
342 | $options['indexes'] = array(); |
---|
343 | $sql = parent::_getCreateTableSQL($table, $columns, $options); |
---|
344 | |
---|
345 | foreach ($columns as $name => $column) { |
---|
346 | if (isset($column['sequence'])) { |
---|
347 | $sql[] = $this->getCreateSequenceSQL($column['sequence'], 1); |
---|
348 | } |
---|
349 | |
---|
350 | if (isset($column['autoincrement']) && $column['autoincrement'] || |
---|
351 | (isset($column['autoinc']) && $column['autoinc'])) { |
---|
352 | $sql = array_merge($sql, $this->getCreateAutoincrementSql($name, $table)); |
---|
353 | } |
---|
354 | } |
---|
355 | |
---|
356 | if (isset($indexes) && ! empty($indexes)) { |
---|
357 | foreach ($indexes as $indexName => $index) { |
---|
358 | $sql[] = $this->getCreateIndexSQL($index, $table); |
---|
359 | } |
---|
360 | } |
---|
361 | |
---|
362 | return $sql; |
---|
363 | } |
---|
364 | |
---|
365 | /** |
---|
366 | * @license New BSD License |
---|
367 | * @link http://ezcomponents.org/docs/api/trunk/DatabaseSchema/ezcDbSchemaOracleReader.html |
---|
368 | * @param string $table |
---|
369 | * @return string |
---|
370 | */ |
---|
371 | public function getListTableIndexesSQL($table, $currentDatabase = null) |
---|
372 | { |
---|
373 | $table = strtoupper($table); |
---|
374 | |
---|
375 | return "SELECT uind.index_name AS name, " . |
---|
376 | " uind.index_type AS type, " . |
---|
377 | " decode( uind.uniqueness, 'NONUNIQUE', 0, 'UNIQUE', 1 ) AS is_unique, " . |
---|
378 | " uind_col.column_name AS column_name, " . |
---|
379 | " uind_col.column_position AS column_pos, " . |
---|
380 | " (SELECT ucon.constraint_type FROM user_constraints ucon WHERE ucon.constraint_name = uind.index_name) AS is_primary ". |
---|
381 | "FROM user_indexes uind, user_ind_columns uind_col " . |
---|
382 | "WHERE uind.index_name = uind_col.index_name AND uind_col.table_name = '$table' ORDER BY uind_col.column_position ASC"; |
---|
383 | } |
---|
384 | |
---|
385 | public function getListTablesSQL() |
---|
386 | { |
---|
387 | return 'SELECT * FROM sys.user_tables'; |
---|
388 | } |
---|
389 | |
---|
390 | public function getListViewsSQL($database) |
---|
391 | { |
---|
392 | return 'SELECT view_name, text FROM sys.user_views'; |
---|
393 | } |
---|
394 | |
---|
395 | public function getCreateViewSQL($name, $sql) |
---|
396 | { |
---|
397 | return 'CREATE VIEW ' . $name . ' AS ' . $sql; |
---|
398 | } |
---|
399 | |
---|
400 | public function getDropViewSQL($name) |
---|
401 | { |
---|
402 | return 'DROP VIEW '. $name; |
---|
403 | } |
---|
404 | |
---|
405 | public function getCreateAutoincrementSql($name, $table, $start = 1) |
---|
406 | { |
---|
407 | $table = strtoupper($table); |
---|
408 | $sql = array(); |
---|
409 | |
---|
410 | $indexName = $table . '_AI_PK'; |
---|
411 | $definition = array( |
---|
412 | 'primary' => true, |
---|
413 | 'columns' => array($name => true), |
---|
414 | ); |
---|
415 | |
---|
416 | $idx = new \Doctrine\DBAL\Schema\Index($indexName, array($name), true, true); |
---|
417 | |
---|
418 | $sql[] = 'DECLARE |
---|
419 | constraints_Count NUMBER; |
---|
420 | BEGIN |
---|
421 | SELECT COUNT(CONSTRAINT_NAME) INTO constraints_Count FROM USER_CONSTRAINTS WHERE TABLE_NAME = \''.$table.'\' AND CONSTRAINT_TYPE = \'P\'; |
---|
422 | IF constraints_Count = 0 OR constraints_Count = \'\' THEN |
---|
423 | EXECUTE IMMEDIATE \''.$this->getCreateConstraintSQL($idx, $table).'\'; |
---|
424 | END IF; |
---|
425 | END;'; |
---|
426 | |
---|
427 | $sequenceName = $table . '_SEQ'; |
---|
428 | $sequence = new \Doctrine\DBAL\Schema\Sequence($sequenceName, $start); |
---|
429 | $sql[] = $this->getCreateSequenceSQL($sequence); |
---|
430 | |
---|
431 | $triggerName = $table . '_AI_PK'; |
---|
432 | $sql[] = 'CREATE TRIGGER ' . $triggerName . ' |
---|
433 | BEFORE INSERT |
---|
434 | ON ' . $table . ' |
---|
435 | FOR EACH ROW |
---|
436 | DECLARE |
---|
437 | last_Sequence NUMBER; |
---|
438 | last_InsertID NUMBER; |
---|
439 | BEGIN |
---|
440 | SELECT ' . $sequenceName . '.NEXTVAL INTO :NEW.' . $name . ' FROM DUAL; |
---|
441 | IF (:NEW.' . $name . ' IS NULL OR :NEW.'.$name.' = 0) THEN |
---|
442 | SELECT ' . $sequenceName . '.NEXTVAL INTO :NEW.' . $name . ' FROM DUAL; |
---|
443 | ELSE |
---|
444 | SELECT NVL(Last_Number, 0) INTO last_Sequence |
---|
445 | FROM User_Sequences |
---|
446 | WHERE Sequence_Name = \'' . $sequenceName . '\'; |
---|
447 | SELECT :NEW.' . $name . ' INTO last_InsertID FROM DUAL; |
---|
448 | WHILE (last_InsertID > last_Sequence) LOOP |
---|
449 | SELECT ' . $sequenceName . '.NEXTVAL INTO last_Sequence FROM DUAL; |
---|
450 | END LOOP; |
---|
451 | END IF; |
---|
452 | END;'; |
---|
453 | return $sql; |
---|
454 | } |
---|
455 | |
---|
456 | public function getDropAutoincrementSql($table) |
---|
457 | { |
---|
458 | $table = strtoupper($table); |
---|
459 | $trigger = $table . '_AI_PK'; |
---|
460 | |
---|
461 | if ($trigger) { |
---|
462 | $sql[] = 'DROP TRIGGER ' . $trigger; |
---|
463 | $sql[] = $this->getDropSequenceSQL($table.'_SEQ'); |
---|
464 | |
---|
465 | $indexName = $table . '_AI_PK'; |
---|
466 | $sql[] = $this->getDropConstraintSQL($indexName, $table); |
---|
467 | } |
---|
468 | |
---|
469 | return $sql; |
---|
470 | } |
---|
471 | |
---|
472 | public function getListTableForeignKeysSQL($table) |
---|
473 | { |
---|
474 | $table = strtoupper($table); |
---|
475 | |
---|
476 | return "SELECT alc.constraint_name, |
---|
477 | alc.DELETE_RULE, |
---|
478 | alc.search_condition, |
---|
479 | cols.column_name \"local_column\", |
---|
480 | cols.position, |
---|
481 | r_alc.table_name \"references_table\", |
---|
482 | r_cols.column_name \"foreign_column\" |
---|
483 | FROM user_cons_columns cols |
---|
484 | LEFT JOIN user_constraints alc |
---|
485 | ON alc.constraint_name = cols.constraint_name |
---|
486 | LEFT JOIN user_constraints r_alc |
---|
487 | ON alc.r_constraint_name = r_alc.constraint_name |
---|
488 | LEFT JOIN user_cons_columns r_cols |
---|
489 | ON r_alc.constraint_name = r_cols.constraint_name |
---|
490 | AND cols.position = r_cols.position |
---|
491 | WHERE alc.constraint_name = cols.constraint_name |
---|
492 | AND alc.constraint_type = 'R' |
---|
493 | AND alc.table_name = '".$table."'"; |
---|
494 | } |
---|
495 | |
---|
496 | public function getListTableConstraintsSQL($table) |
---|
497 | { |
---|
498 | $table = strtoupper($table); |
---|
499 | return 'SELECT * FROM user_constraints WHERE table_name = \'' . $table . '\''; |
---|
500 | } |
---|
501 | |
---|
502 | public function getListTableColumnsSQL($table, $database = null) |
---|
503 | { |
---|
504 | $table = strtoupper($table); |
---|
505 | |
---|
506 | $tabColumnsTableName = "user_tab_columns"; |
---|
507 | $ownerCondition = ''; |
---|
508 | if(null !== $database){ |
---|
509 | $database = strtoupper($database); |
---|
510 | $tabColumnsTableName = "all_tab_columns"; |
---|
511 | $ownerCondition = "AND c.owner = '".$database."'"; |
---|
512 | } |
---|
513 | |
---|
514 | return "SELECT c.*, d.comments FROM $tabColumnsTableName c ". |
---|
515 | "INNER JOIN user_col_comments d ON d.TABLE_NAME = c.TABLE_NAME AND d.COLUMN_NAME = c.COLUMN_NAME ". |
---|
516 | "WHERE c.table_name = '" . $table . "' ".$ownerCondition." ORDER BY c.column_name"; |
---|
517 | } |
---|
518 | |
---|
519 | /** |
---|
520 | * |
---|
521 | * @param \Doctrine\DBAL\Schema\Sequence $sequence |
---|
522 | * @return string |
---|
523 | */ |
---|
524 | public function getDropSequenceSQL($sequence) |
---|
525 | { |
---|
526 | if ($sequence instanceof \Doctrine\DBAL\Schema\Sequence) { |
---|
527 | $sequence = $sequence->getQuotedName($this); |
---|
528 | } |
---|
529 | |
---|
530 | return 'DROP SEQUENCE ' . $sequence; |
---|
531 | } |
---|
532 | |
---|
533 | /** |
---|
534 | * @param ForeignKeyConstraint|string $foreignKey |
---|
535 | * @param Table|string $table |
---|
536 | * @return string |
---|
537 | */ |
---|
538 | public function getDropForeignKeySQL($foreignKey, $table) |
---|
539 | { |
---|
540 | if ($foreignKey instanceof \Doctrine\DBAL\Schema\ForeignKeyConstraint) { |
---|
541 | $foreignKey = $foreignKey->getQuotedName($this); |
---|
542 | } |
---|
543 | |
---|
544 | if ($table instanceof \Doctrine\DBAL\Schema\Table) { |
---|
545 | $table = $table->getQuotedName($this); |
---|
546 | } |
---|
547 | |
---|
548 | return 'ALTER TABLE ' . $table . ' DROP CONSTRAINT ' . $foreignKey; |
---|
549 | } |
---|
550 | |
---|
551 | public function getDropDatabaseSQL($database) |
---|
552 | { |
---|
553 | return 'DROP USER ' . $database . ' CASCADE'; |
---|
554 | } |
---|
555 | |
---|
556 | /** |
---|
557 | * Gets the sql statements for altering an existing table. |
---|
558 | * |
---|
559 | * The method returns an array of sql statements, since some platforms need several statements. |
---|
560 | * |
---|
561 | * @param string $diff->name name of the table that is intended to be changed. |
---|
562 | * @param array $changes associative array that contains the details of each type * |
---|
563 | * @param boolean $check indicates whether the function should just check if the DBMS driver |
---|
564 | * can perform the requested table alterations if the value is true or |
---|
565 | * actually perform them otherwise. |
---|
566 | * @return array |
---|
567 | */ |
---|
568 | public function getAlterTableSQL(TableDiff $diff) |
---|
569 | { |
---|
570 | $sql = array(); |
---|
571 | $commentsSQL = array(); |
---|
572 | $columnSql = array(); |
---|
573 | |
---|
574 | $fields = array(); |
---|
575 | foreach ($diff->addedColumns AS $column) { |
---|
576 | if ($this->onSchemaAlterTableAddColumn($column, $diff, $columnSql)) { |
---|
577 | continue; |
---|
578 | } |
---|
579 | |
---|
580 | $fields[] = $this->getColumnDeclarationSQL($column->getQuotedName($this), $column->toArray()); |
---|
581 | if ($comment = $this->getColumnComment($column)) { |
---|
582 | $commentsSQL[] = $this->getCommentOnColumnSQL($diff->name, $column->getName(), $comment); |
---|
583 | } |
---|
584 | } |
---|
585 | if (count($fields)) { |
---|
586 | $sql[] = 'ALTER TABLE ' . $diff->name . ' ADD (' . implode(', ', $fields) . ')'; |
---|
587 | } |
---|
588 | |
---|
589 | $fields = array(); |
---|
590 | foreach ($diff->changedColumns AS $columnDiff) { |
---|
591 | if ($this->onSchemaAlterTableChangeColumn($columnDiff, $diff, $columnSql)) { |
---|
592 | continue; |
---|
593 | } |
---|
594 | |
---|
595 | $column = $columnDiff->column; |
---|
596 | $fields[] = $column->getQuotedName($this). ' ' . $this->getColumnDeclarationSQL('', $column->toArray()); |
---|
597 | if ($columnDiff->hasChanged('comment') && $comment = $this->getColumnComment($column)) { |
---|
598 | $commentsSQL[] = $this->getCommentOnColumnSQL($diff->name, $column->getName(), $comment); |
---|
599 | } |
---|
600 | } |
---|
601 | if (count($fields)) { |
---|
602 | $sql[] = 'ALTER TABLE ' . $diff->name . ' MODIFY (' . implode(', ', $fields) . ')'; |
---|
603 | } |
---|
604 | |
---|
605 | foreach ($diff->renamedColumns AS $oldColumnName => $column) { |
---|
606 | if ($this->onSchemaAlterTableRenameColumn($oldColumnName, $column, $diff, $columnSql)) { |
---|
607 | continue; |
---|
608 | } |
---|
609 | |
---|
610 | $sql[] = 'ALTER TABLE ' . $diff->name . ' RENAME COLUMN ' . $oldColumnName .' TO ' . $column->getQuotedName($this); |
---|
611 | } |
---|
612 | |
---|
613 | $fields = array(); |
---|
614 | foreach ($diff->removedColumns AS $column) { |
---|
615 | if ($this->onSchemaAlterTableRemoveColumn($column, $diff, $columnSql)) { |
---|
616 | continue; |
---|
617 | } |
---|
618 | |
---|
619 | $fields[] = $column->getQuotedName($this); |
---|
620 | } |
---|
621 | if (count($fields)) { |
---|
622 | $sql[] = 'ALTER TABLE ' . $diff->name . ' DROP (' . implode(', ', $fields).')'; |
---|
623 | } |
---|
624 | |
---|
625 | $tableSql = array(); |
---|
626 | |
---|
627 | if (!$this->onSchemaAlterTable($diff, $tableSql)) { |
---|
628 | if ($diff->newName !== false) { |
---|
629 | $sql[] = 'ALTER TABLE ' . $diff->name . ' RENAME TO ' . $diff->newName; |
---|
630 | } |
---|
631 | |
---|
632 | $sql = array_merge($sql, $this->_getAlterTableIndexForeignKeySQL($diff), $commentsSQL); |
---|
633 | } |
---|
634 | |
---|
635 | return array_merge($sql, $tableSql, $columnSql); |
---|
636 | } |
---|
637 | |
---|
638 | /** |
---|
639 | * Whether the platform prefers sequences for ID generation. |
---|
640 | * |
---|
641 | * @return boolean |
---|
642 | */ |
---|
643 | public function prefersSequences() |
---|
644 | { |
---|
645 | return true; |
---|
646 | } |
---|
647 | |
---|
648 | public function supportsCommentOnStatement() |
---|
649 | { |
---|
650 | return true; |
---|
651 | } |
---|
652 | |
---|
653 | /** |
---|
654 | * Get the platform name for this instance |
---|
655 | * |
---|
656 | * @return string |
---|
657 | */ |
---|
658 | public function getName() |
---|
659 | { |
---|
660 | return 'oracle'; |
---|
661 | } |
---|
662 | |
---|
663 | /** |
---|
664 | * Adds an driver-specific LIMIT clause to the query |
---|
665 | * |
---|
666 | * @param string $query query to modify |
---|
667 | * @param integer $limit limit the number of rows |
---|
668 | * @param integer $offset start reading from given offset |
---|
669 | * @return string the modified query |
---|
670 | */ |
---|
671 | protected function doModifyLimitQuery($query, $limit, $offset = null) |
---|
672 | { |
---|
673 | $limit = (int) $limit; |
---|
674 | $offset = (int) $offset; |
---|
675 | if (preg_match('/^\s*SELECT/i', $query)) { |
---|
676 | if (!preg_match('/\sFROM\s/i', $query)) { |
---|
677 | $query .= " FROM dual"; |
---|
678 | } |
---|
679 | if ($limit > 0) { |
---|
680 | $max = $offset + $limit; |
---|
681 | $column = '*'; |
---|
682 | if ($offset > 0) { |
---|
683 | $min = $offset + 1; |
---|
684 | $query = 'SELECT * FROM (SELECT a.' . $column . ', rownum AS doctrine_rownum FROM (' . |
---|
685 | $query . |
---|
686 | ') a WHERE rownum <= ' . $max . ') WHERE doctrine_rownum >= ' . $min; |
---|
687 | } else { |
---|
688 | $query = 'SELECT a.' . $column . ' FROM (' . $query . ') a WHERE ROWNUM <= ' . $max; |
---|
689 | } |
---|
690 | } |
---|
691 | } |
---|
692 | return $query; |
---|
693 | } |
---|
694 | |
---|
695 | /** |
---|
696 | * Gets the character casing of a column in an SQL result set of this platform. |
---|
697 | * |
---|
698 | * Oracle returns all column names in SQL result sets in uppercase. |
---|
699 | * |
---|
700 | * @param string $column The column name for which to get the correct character casing. |
---|
701 | * @return string The column name in the character casing used in SQL result sets. |
---|
702 | */ |
---|
703 | public function getSQLResultCasing($column) |
---|
704 | { |
---|
705 | return strtoupper($column); |
---|
706 | } |
---|
707 | |
---|
708 | public function getCreateTemporaryTableSnippetSQL() |
---|
709 | { |
---|
710 | return "CREATE GLOBAL TEMPORARY TABLE"; |
---|
711 | } |
---|
712 | |
---|
713 | public function getDateTimeTzFormatString() |
---|
714 | { |
---|
715 | return 'Y-m-d H:i:sP'; |
---|
716 | } |
---|
717 | |
---|
718 | public function getDateFormatString() |
---|
719 | { |
---|
720 | return 'Y-m-d 00:00:00'; |
---|
721 | } |
---|
722 | |
---|
723 | public function getTimeFormatString() |
---|
724 | { |
---|
725 | return '1900-01-01 H:i:s'; |
---|
726 | } |
---|
727 | |
---|
728 | public function fixSchemaElementName($schemaElementName) |
---|
729 | { |
---|
730 | if (strlen($schemaElementName) > 30) { |
---|
731 | // Trim it |
---|
732 | return substr($schemaElementName, 0, 30); |
---|
733 | } |
---|
734 | return $schemaElementName; |
---|
735 | } |
---|
736 | |
---|
737 | /** |
---|
738 | * Maximum length of any given databse identifier, like tables or column names. |
---|
739 | * |
---|
740 | * @return int |
---|
741 | */ |
---|
742 | public function getMaxIdentifierLength() |
---|
743 | { |
---|
744 | return 30; |
---|
745 | } |
---|
746 | |
---|
747 | /** |
---|
748 | * Whether the platform supports sequences. |
---|
749 | * |
---|
750 | * @return boolean |
---|
751 | */ |
---|
752 | public function supportsSequences() |
---|
753 | { |
---|
754 | return true; |
---|
755 | } |
---|
756 | |
---|
757 | public function supportsForeignKeyOnUpdate() |
---|
758 | { |
---|
759 | return false; |
---|
760 | } |
---|
761 | |
---|
762 | /** |
---|
763 | * Whether the platform supports releasing savepoints. |
---|
764 | * |
---|
765 | * @return boolean |
---|
766 | */ |
---|
767 | public function supportsReleaseSavepoints() |
---|
768 | { |
---|
769 | return false; |
---|
770 | } |
---|
771 | |
---|
772 | /** |
---|
773 | * @inheritdoc |
---|
774 | */ |
---|
775 | public function getTruncateTableSQL($tableName, $cascade = false) |
---|
776 | { |
---|
777 | return 'TRUNCATE TABLE '.$tableName; |
---|
778 | } |
---|
779 | |
---|
780 | /** |
---|
781 | * This is for test reasons, many vendors have special requirements for dummy statements. |
---|
782 | * |
---|
783 | * @return string |
---|
784 | */ |
---|
785 | public function getDummySelectSQL() |
---|
786 | { |
---|
787 | return 'SELECT 1 FROM DUAL'; |
---|
788 | } |
---|
789 | |
---|
790 | protected function initializeDoctrineTypeMappings() |
---|
791 | { |
---|
792 | $this->doctrineTypeMapping = array( |
---|
793 | 'integer' => 'integer', |
---|
794 | 'number' => 'integer', |
---|
795 | 'pls_integer' => 'boolean', |
---|
796 | 'binary_integer' => 'boolean', |
---|
797 | 'varchar' => 'string', |
---|
798 | 'varchar2' => 'string', |
---|
799 | 'nvarchar2' => 'string', |
---|
800 | 'char' => 'string', |
---|
801 | 'nchar' => 'string', |
---|
802 | 'date' => 'datetime', |
---|
803 | 'timestamp' => 'datetime', |
---|
804 | 'timestamptz' => 'datetimetz', |
---|
805 | 'float' => 'float', |
---|
806 | 'long' => 'string', |
---|
807 | 'clob' => 'text', |
---|
808 | 'nclob' => 'text', |
---|
809 | 'raw' => 'text', |
---|
810 | 'long raw' => 'text', |
---|
811 | 'rowid' => 'string', |
---|
812 | 'urowid' => 'string', |
---|
813 | 'blob' => 'blob', |
---|
814 | ); |
---|
815 | } |
---|
816 | |
---|
817 | /** |
---|
818 | * Generate SQL to release a savepoint |
---|
819 | * |
---|
820 | * @param string $savepoint |
---|
821 | * @return string |
---|
822 | */ |
---|
823 | public function releaseSavePoint($savepoint) |
---|
824 | { |
---|
825 | return ''; |
---|
826 | } |
---|
827 | |
---|
828 | protected function getReservedKeywordsClass() |
---|
829 | { |
---|
830 | return 'Doctrine\DBAL\Platforms\Keywords\OracleKeywords'; |
---|
831 | } |
---|
832 | |
---|
833 | /** |
---|
834 | * Gets the SQL Snippet used to declare a BLOB column type. |
---|
835 | */ |
---|
836 | public function getBlobTypeDeclarationSQL(array $field) |
---|
837 | { |
---|
838 | return 'BLOB'; |
---|
839 | } |
---|
840 | } |
---|