forked from phalcon/incubator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOracle.php
595 lines (523 loc) · 16.5 KB
/
Oracle.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
<?php
/*
+------------------------------------------------------------------------+
| Phalcon Framework |
+------------------------------------------------------------------------+
| Copyright (c) 2011-2016 Phalcon Team (https://phalconphp.com) |
+------------------------------------------------------------------------+
| This source file is subject to the New BSD License that is bundled |
| with this package in the file docs/LICENSE.txt. |
| |
| If you did not receive a copy of the license and are unable to |
| obtain it through the world-wide-web, please send an email |
| to [email protected] so we can send you a copy immediately. |
+------------------------------------------------------------------------+
| Authors: Andres Gutierrez <[email protected]> |
| Eduar Carvajal <[email protected]> |
+------------------------------------------------------------------------+
*/
namespace Phalcon\Db\Dialect;
use Phalcon\Text;
use Phalcon\Db\Column;
use Phalcon\Db\Dialect;
use Phalcon\Db\Exception;
use Phalcon\Db\IndexInterface;
use Phalcon\Db\ColumnInterface;
use Phalcon\Db\ReferenceInterface;
/**
* Phalcon\Db\Dialect\Oracle
*
* Generates database specific SQL for the Oracle RDBMS.
*
* @package Phalcon\Db\Dialect
*/
class Oracle extends Dialect
{
// @codingStandardsIgnoreStart
protected $_escapeChar = "'";
// @codingStandardsIgnoreEnd
/**
* Generates the SQL for LIMIT clause.
*
* @param string $sqlQuery
* @param mixed $number
* @return string
*/
public function limit($sqlQuery, $number)
{
$offset = 0;
if (is_array($number)) {
if (isset($number[1])) {
$offset = intval(trim($number[1], $this->_escapeChar));
}
$limit = intval(trim($number[0], $this->_escapeChar)) + $offset;
} else {
$limit = intval(trim($number, $this->_escapeChar));
}
$sqlQuery = sprintf(
/** @lang text */
'SELECT * FROM (SELECT Z1.*, ROWNUM PHALCON_RN FROM (%s) Z1',
$sqlQuery
);
if (0 != $limit) {
$sqlQuery .= sprintf(' WHERE ROWNUM <= %d', $limit);
}
$sqlQuery .= ')';
if (0 != $offset) {
$sqlQuery .= sprintf(' WHERE PHALCON_RN >= %d', $offset);
}
return $sqlQuery;
}
/**
* Gets the column name in Oracle.
*
* @param ColumnInterface $column
* @return string
*
* @throws Exception
*/
public function getColumnDefinition(ColumnInterface $column)
{
$type = $column->getType();
$size = $column->getSize();
switch ($type) {
case Column::TYPE_INTEGER:
$columnSql = 'INTEGER';
break;
case Column::TYPE_DATE:
$columnSql = 'DATE';
break;
case Column::TYPE_VARCHAR:
$columnSql = 'VARCHAR2(' . $size . ')';
break;
case Column::TYPE_DECIMAL:
$scale = $column->getScale();
$columnSql = 'NUMBER(' . $size . ',' . $scale . ')';
break;
case Column::TYPE_DATETIME:
$columnSql = 'TIMESTAMP';
break;
case Column::TYPE_TIMESTAMP:
$columnSql = 'TIMESTAMP';
break;
case Column::TYPE_CHAR:
$columnSql = 'CHAR(' . $size . ')';
break;
case Column::TYPE_TEXT:
$columnSql = 'TEXT';
break;
case Column::TYPE_FLOAT:
$scale = $column->getScale();
$columnSql = 'FLOAT(' . $size . ',' . $scale . ')';
break;
case Column::TYPE_BOOLEAN:
$columnSql = 'TINYINT(1)';
break;
default:
throw new Exception('Unrecognized Oracle data type at column ' . $column->getName());
}
return $columnSql;
}
/**
* Generates SQL to add a column to a table.
*
* @param string $tableName
* @param string $schemaName
* @param ColumnInterface $column
* @return string
*
* @throws Exception
*/
public function addColumn($tableName, $schemaName, ColumnInterface $column)
{
throw new Exception('Not implemented yet.');
}
/**
* Generates SQL to modify a column in a table.
*
* @param string $tableName
* @param string $schemaName
* @param ColumnInterface $column
* @param ColumnInterface|null $current
* @return string
*
* @throws Exception
*/
public function modifyColumn($tableName, $schemaName, ColumnInterface $column, ColumnInterface $current = null)
{
throw new Exception('Not implemented yet.');
}
/**
* Generates SQL to delete a column from a table.
*
* @param string $tableName
* @param string $schemaName
* @param string $columnName
* @return string
*
* @throws Exception
*/
public function dropColumn($tableName, $schemaName, $columnName)
{
throw new Exception('Not implemented yet.');
}
/**
* Generates SQL to add an index to a table.
*
* @param string $tableName
* @param string $schemaName
* @param IndexInterface $index
* @return string
*
* @throws Exception
*/
public function addIndex($tableName, $schemaName, IndexInterface $index)
{
throw new Exception('Not implemented yet.');
}
/**
* Generates SQL to delete an index from a table.
*
* @param string $tableName
* @param string $schemaName
* @param string $indexName
* @return string
*
* @throws Exception
*/
public function dropIndex($tableName, $schemaName, $indexName)
{
throw new Exception('Not implemented yet.');
}
/**
* Generates SQL to add the primary key to a table.
*
* @param string $tableName
* @param string $schemaName
* @param IndexInterface $index
* @return string
*
* @throws Exception
*/
public function addPrimaryKey($tableName, $schemaName, IndexInterface $index)
{
throw new Exception('Not implemented yet.');
}
/**
* Generates SQL to delete primary key from a table.
*
* @param string $tableName
* @param string $schemaName
* @return string
*
* @throws Exception
*/
public function dropPrimaryKey($tableName, $schemaName)
{
throw new Exception('Not implemented yet.');
}
/**
* Generates SQL to add an index to a table.
*
* @param string $tableName
* @param string $schemaName
* @param ReferenceInterface $reference
* @return string
*
* @throws Exception
*/
public function addForeignKey($tableName, $schemaName, ReferenceInterface $reference)
{
throw new Exception('Not implemented yet.');
}
/**
* Generates SQL to delete a foreign key from a table.
*
* @param string $tableName
* @param string $schemaName
* @param string $referenceName
* @return string
*
* @throws Exception
*/
public function dropForeignKey($tableName, $schemaName, $referenceName)
{
throw new Exception('Not implemented yet.');
}
/**
* Generates SQL to create a table in Oracle.
*
* @param string $tableName
* @param string $schemaName
* @param array $definition
* @return string
*
* @throws Exception
*/
public function createTable($tableName, $schemaName, array $definition)
{
throw new Exception('Not implemented yet.');
}
/**
* Generates SQL to drop a table.
*
* @param string $tableName
* @param string $schemaName
* @param bool $ifExists
* @return string
*/
public function dropTable($tableName, $schemaName, $ifExists = true)
{
$this->_escapeChar = '';
$table = $this->prepareTable($tableName, $schemaName);
$sql = sprintf(
/** @lang text */
'DROP TABLE %s',
$table
);
if ($ifExists) {
$sql = sprintf(
"BEGIN EXECUTE IMMEDIATE '%s'; EXCEPTION WHEN OTHERS THEN IF SQLCODE != -942 THEN RAISE; END IF; END",
$sql
);
}
$this->_escapeChar = "'";
return $sql;
}
/**
* List all tables in database.
*
* <code>
* print_r($dialect->listTables('blog'));
* </code>
*
* @param string $schemaName
* @return string
*/
public function listTables($schemaName = null)
{
$baseQuery = /** @lang text */
"SELECT TABLE_NAME, OWNER FROM ALL_TABLES %s ORDER BY OWNER, TABLE_NAME";
if (!empty($schemaName)) {
$schemaName = $this->escapeSchema($schemaName);
return sprintf($baseQuery . 'WHERE OWNER = %s', Text::upper($schemaName));
}
return sprintf($baseQuery, '');
}
/**
* Generates SQL checking for the existence of a schema.table
*
* <code>
* echo $dialect->tableExists('posts', 'blog');
* echo $dialect->tableExists('posts');
* </code>
*
* @param string $tableName
* @param string $schemaName
* @return string
*/
public function tableExists($tableName, $schemaName = null)
{
$tableName = $this->escape(Text::upper($tableName));
$baseQuery = sprintf(
/** @lang text */
"SELECT CASE WHEN COUNT(*) > 0 THEN 1 ELSE 0 END RET FROM ALL_TABLES WHERE TABLE_NAME = %s",
$tableName
);
if (!empty($schemaName)) {
$schemaName = $this->escapeSchema($schemaName);
return sprintf("%s AND OWNER = %s", $baseQuery, Text::upper($schemaName));
}
return $baseQuery;
}
/**
* Generates SQL to create a view.
*
* @param string $viewName
* @param array $definition
* @param string $schemaName
* @return string
*
* @throws Exception
*/
public function createView($viewName, array $definition, $schemaName = null)
{
if (!isset($definition['sql']) || empty($definition['sql'])) {
throw new Exception("The index 'sql' is required in the definition array");
}
return 'CREATE VIEW ' . Text::upper($this->prepareTable($viewName, $schemaName)) . ' AS ' . $definition['sql'];
}
/**
* Generates SQL to drop a view.
*
* @param string $viewName
* @param string $schemaName
* @param bool $ifExists
* @return string
*/
public function dropView($viewName, $schemaName = null, $ifExists = true)
{
$this->_escapeChar = '';
$view = Text::upper($this->prepareTable($viewName, $schemaName));
$sql = sprintf(
/** @lang text */
'DROP VIEW %s',
$view
);
if ($ifExists) {
$sql = sprintf(
"BEGIN FOR i IN (SELECT NULL FROM ALL_VIEWS WHERE VIEW_NAME = '%s') " .
"LOOP EXECUTE IMMEDIATE '%s'; END LOOP; END",
$view,
$sql
);
}
$this->_escapeChar = "'";
return $sql;
}
/**
* Generates SQL checking for the existence of a schema.view
*
* @param string $viewName
* @param string $schemaName
*
* @return string
*/
public function viewExists($viewName, $schemaName = null)
{
$view = $this->prepareTable($viewName, $schemaName);
$baseSql = sprintf(
/** @lang text */
"SELECT CASE WHEN COUNT(*) > 0 THEN 1 ELSE 0 END RET FROM ALL_VIEWS WHERE VIEW_NAME = %s",
Text::upper($view)
);
if (!empty($schemaName)) {
$schemaName = $this->escapeSchema($schemaName, $this->_escapeChar);
$baseSql .= sprintf("AND OWNER = %s", Text::upper($schemaName));
}
return $baseSql;
}
/**
* Generates the SQL to list all views of a schema or user.
*
* @param string $schemaName
* @return string
*/
public function listViews($schemaName = null)
{
$baseSql = /** @lang text */
'SELECT VIEW_NAME FROM ALL_VIEWS';
if (!empty($schemaName)) {
$schemaName = $this->escapeSchema($schemaName);
$baseSql .= sprintf(" WHERE OWNER = %s", Text::upper($schemaName));
}
return $baseSql . ' ORDER BY VIEW_NAME';
}
/**
* Generates SQL to describe a table.
*
* @param string $table
* @param string $schema
* @return string
*/
public function describeColumns($table, $schema = null)
{
$table = $this->escape($table);
$sql = 'SELECT TC.COLUMN_NAME, TC.DATA_TYPE, TC.DATA_LENGTH, TC.DATA_PRECISION, TC.DATA_SCALE, TC.NULLABLE, ' .
'C.CONSTRAINT_TYPE, TC.DATA_DEFAULT, CC.POSITION FROM ALL_TAB_COLUMNS TC LEFT JOIN ' .
'(ALL_CONS_COLUMNS CC JOIN ALL_CONSTRAINTS C ON (CC.CONSTRAINT_NAME = C.CONSTRAINT_NAME AND ' .
"CC.TABLE_NAME = C.TABLE_NAME AND CC.OWNER = C.OWNER AND C.CONSTRAINT_TYPE = 'P')) ON " .
'TC.TABLE_NAME = CC.TABLE_NAME AND TC.COLUMN_NAME = CC.COLUMN_NAME WHERE TC.TABLE_NAME = %s %s '.
'ORDER BY TC.COLUMN_ID';
if (!empty($schema)) {
$schema = $this->escapeSchema($schema);
return sprintf($sql, Text::upper($table), 'AND TC.OWNER = ' . Text::upper($schema));
}
return sprintf($sql, Text::upper($table), '');
}
/**
* Generates SQL to query indexes on a table.
*
* @param string $table
* @param string $schema
* @return string
*/
public function describeIndexes($table, $schema = null)
{
$table = $this->escape($table);
$sql = 'SELECT I.TABLE_NAME, 0 AS C0, I.INDEX_NAME, IC.COLUMN_POSITION, IC.COLUMN_NAME ' .
'FROM ALL_INDEXES I JOIN ALL_IND_COLUMNS IC ON I.INDEX_NAME = IC.INDEX_NAME WHERE I.TABLE_NAME = ' .
Text::upper($table);
if (!empty($schema)) {
$schema = $this->escapeSchema($schema);
$sql .= ' AND IC.INDEX_OWNER = %s'. Text::upper($schema);
}
return $sql;
}
public function describeReferences($table, $schema = null)
{
$table = $this->escape($table);
$sql = 'SELECT AC.TABLE_NAME, CC.COLUMN_NAME, AC.CONSTRAINT_NAME, AC.R_OWNER, RCC.TABLE_NAME R_TABLE_NAME, ' .
'RCC.COLUMN_NAME R_COLUMN_NAME FROM ALL_CONSTRAINTS AC JOIN ALL_CONS_COLUMNS CC ' .
'ON AC.CONSTRAINT_NAME = CC.CONSTRAINT_NAME JOIN ALL_CONS_COLUMNS RCC ON AC.R_OWNER = RCC.OWNER ' .
"AND AC.R_CONSTRAINT_NAME = RCC.CONSTRAINT_NAME WHERE AC.CONSTRAINT_TYPE='R' ";
if (!empty($schema)) {
$schema = $this->escapeSchema($schema);
$sql .= 'AND AC.OWNER = ' . Text::upper($schema) . ' AND AC.TABLE_NAME = ' . Text::upper($table);
} else {
$sql .= 'AND AC.TABLE_NAME = ' . Text::upper($table);
}
return $sql;
}
/**
* Generates the SQL to describe the table creation options.
*
* @param string $table
* @param string $schema
* @return string
*/
public function tableOptions($table, $schema = null)
{
return '';
}
/**
* Checks whether the platform supports savepoints.
*
* @return bool
*/
public function supportsSavepoints()
{
return false;
}
/**
* Checks whether the platform supports releasing savepoints.
*
* @return bool
*/
public function supportsReleaseSavepoints()
{
return false;
}
/**
* Prepares table for this RDBMS.
*
* @param string $table
* @param string $schema
* @param string $alias
* @param string $escapeChar
* @return string
*/
protected function prepareTable($table, $schema = null, $alias = null, $escapeChar = null)
{
$table = $this->escape($table, $escapeChar);
// Schema
if (!empty($schema)) {
$table = $this->escapeSchema($schema, $escapeChar) . '.' . $table;
}
// Alias
if (!empty($alias)) {
$table .= ' ' . $this->escape($alias, $escapeChar);
}
return $table;
}
}