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