1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @link http://www.yiiframework.com/ |
4
|
|
|
* @copyright Copyright (c) 2008 Yii Software LLC |
5
|
|
|
* @license http://www.yiiframework.com/license/ |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace yii\db\oci; |
9
|
|
|
|
10
|
|
|
use yii\base\InvalidArgumentException; |
11
|
|
|
use yii\db\Connection; |
12
|
|
|
use yii\db\Exception; |
13
|
|
|
use yii\db\Expression; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* QueryBuilder is the query builder for Oracle databases. |
17
|
|
|
* |
18
|
|
|
* @author Qiang Xue <[email protected]> |
19
|
|
|
* @since 2.0 |
20
|
|
|
*/ |
21
|
|
|
class QueryBuilder extends \yii\db\QueryBuilder |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var array mapping from abstract column types (keys) to physical column types (values). |
25
|
|
|
*/ |
26
|
|
|
public $typeMap = [ |
27
|
|
|
Schema::TYPE_PK => 'NUMBER(10) NOT NULL PRIMARY KEY', |
28
|
|
|
Schema::TYPE_UPK => 'NUMBER(10) UNSIGNED NOT NULL PRIMARY KEY', |
29
|
|
|
Schema::TYPE_BIGPK => 'NUMBER(20) NOT NULL PRIMARY KEY', |
30
|
|
|
Schema::TYPE_UBIGPK => 'NUMBER(20) UNSIGNED NOT NULL PRIMARY KEY', |
31
|
|
|
Schema::TYPE_CHAR => 'CHAR(1)', |
32
|
|
|
Schema::TYPE_STRING => 'VARCHAR2(255)', |
33
|
|
|
Schema::TYPE_TEXT => 'CLOB', |
34
|
|
|
Schema::TYPE_SMALLINT => 'NUMBER(5)', |
35
|
|
|
Schema::TYPE_INTEGER => 'NUMBER(10)', |
36
|
|
|
Schema::TYPE_BIGINT => 'NUMBER(20)', |
37
|
|
|
Schema::TYPE_FLOAT => 'NUMBER', |
38
|
|
|
Schema::TYPE_DOUBLE => 'NUMBER', |
39
|
|
|
Schema::TYPE_DECIMAL => 'NUMBER', |
40
|
|
|
Schema::TYPE_DATETIME => 'TIMESTAMP', |
41
|
|
|
Schema::TYPE_TIMESTAMP => 'TIMESTAMP', |
42
|
|
|
Schema::TYPE_TIME => 'TIMESTAMP', |
43
|
|
|
Schema::TYPE_DATE => 'DATE', |
44
|
|
|
Schema::TYPE_BINARY => 'BLOB', |
45
|
|
|
Schema::TYPE_BOOLEAN => 'NUMBER(1)', |
46
|
|
|
Schema::TYPE_MONEY => 'NUMBER(19,4)', |
47
|
|
|
]; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @inheritdoc |
51
|
|
|
*/ |
52
|
|
|
protected $likeEscapeCharacter = '!'; |
53
|
|
|
/** |
54
|
|
|
* `\` is initialized in [[buildLikeCondition()]] method since |
55
|
|
|
* we need to choose replacement value based on [[\yii\db\Schema::quoteValue()]]. |
56
|
|
|
* @inheritdoc |
57
|
|
|
*/ |
58
|
|
|
protected $likeEscapingReplacements = [ |
59
|
|
|
'%' => '!%', |
60
|
|
|
'_' => '!_', |
61
|
|
|
'!' => '!!', |
62
|
|
|
]; |
63
|
|
|
|
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @inheritdoc |
67
|
|
|
*/ |
68
|
|
|
public function buildOrderByAndLimit($sql, $orderBy, $limit, $offset, &$params) |
69
|
|
|
{ |
70
|
|
|
$orderBy = $this->buildOrderBy($orderBy, $params); |
71
|
|
|
if ($orderBy !== '') { |
72
|
|
|
$sql .= $this->separator . $orderBy; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
$filters = []; |
76
|
|
|
if ($this->hasOffset($offset)) { |
77
|
|
|
$filters[] = 'rowNumId > ' . $offset; |
78
|
|
|
} |
79
|
|
|
if ($this->hasLimit($limit)) { |
80
|
|
|
$filters[] = 'rownum <= ' . $limit; |
81
|
|
|
} |
82
|
|
|
if (empty($filters)) { |
83
|
|
|
return $sql; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
$filter = implode(' AND ', $filters); |
87
|
|
|
return <<<EOD |
88
|
|
|
WITH USER_SQL AS ($sql), |
89
|
|
|
PAGINATION AS (SELECT USER_SQL.*, rownum as rowNumId FROM USER_SQL) |
90
|
|
|
SELECT * |
91
|
|
|
FROM PAGINATION |
92
|
|
|
WHERE $filter |
93
|
|
|
EOD; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Builds a SQL statement for renaming a DB table. |
98
|
|
|
* |
99
|
|
|
* @param string $table the table to be renamed. The name will be properly quoted by the method. |
100
|
|
|
* @param string $newName the new table name. The name will be properly quoted by the method. |
101
|
|
|
* @return string the SQL statement for renaming a DB table. |
102
|
|
|
*/ |
103
|
|
|
public function renameTable($table, $newName) |
104
|
|
|
{ |
105
|
|
|
return 'ALTER TABLE ' . $this->db->quoteTableName($table) . ' RENAME TO ' . $this->db->quoteTableName($newName); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Builds a SQL statement for changing the definition of a column. |
110
|
|
|
* |
111
|
|
|
* @param string $table the table whose column is to be changed. The table name will be properly quoted by the method. |
112
|
|
|
* @param string $column the name of the column to be changed. The name will be properly quoted by the method. |
113
|
|
|
* @param string $type the new column type. The [[getColumnType]] method will be invoked to convert abstract column type (if any) |
114
|
|
|
* into the physical one. Anything that is not recognized as abstract type will be kept in the generated SQL. |
115
|
|
|
* For example, 'string' will be turned into 'varchar(255)', while 'string not null' will become 'varchar(255) not null'. |
116
|
|
|
* @return string the SQL statement for changing the definition of a column. |
117
|
|
|
*/ |
118
|
|
|
public function alterColumn($table, $column, $type) |
119
|
|
|
{ |
120
|
|
|
$type = $this->getColumnType($type); |
121
|
|
|
|
122
|
|
|
return 'ALTER TABLE ' . $this->db->quoteTableName($table) . ' MODIFY ' . $this->db->quoteColumnName($column) . ' ' . $this->getColumnType($type); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Builds a SQL statement for dropping an index. |
127
|
|
|
* |
128
|
|
|
* @param string $name the name of the index to be dropped. The name will be properly quoted by the method. |
129
|
|
|
* @param string $table the table whose index is to be dropped. The name will be properly quoted by the method. |
130
|
|
|
* @return string the SQL statement for dropping an index. |
131
|
|
|
*/ |
132
|
|
|
public function dropIndex($name, $table) |
133
|
|
|
{ |
134
|
|
|
return 'DROP INDEX ' . $this->db->quoteTableName($name); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @inheritdoc |
139
|
|
|
*/ |
140
|
|
|
public function resetSequence($table, $value = null) |
141
|
|
|
{ |
142
|
|
|
$tableSchema = $this->db->getTableSchema($table); |
143
|
|
|
if ($tableSchema === null) { |
144
|
|
|
throw new InvalidArgumentException("Unknown table: $table"); |
145
|
|
|
} |
146
|
|
|
if ($tableSchema->sequenceName === null) { |
147
|
|
|
return ''; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
if ($value !== null) { |
151
|
|
|
$value = (int) $value; |
152
|
|
|
} else { |
153
|
|
|
// use master connection to get the biggest PK value |
154
|
|
|
$value = $this->db->useMaster(function (Connection $db) use ($tableSchema) { |
155
|
|
|
return $db->createCommand("SELECT MAX(\"{$tableSchema->primaryKey}\") FROM \"{$tableSchema->name}\"")->queryScalar(); |
156
|
|
|
}) + 1; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
return "DROP SEQUENCE \"{$tableSchema->name}_SEQ\";" |
160
|
|
|
. "CREATE SEQUENCE \"{$tableSchema->name}_SEQ\" START WITH {$value} INCREMENT BY 1 NOMAXVALUE NOCACHE"; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* @inheritdoc |
165
|
|
|
*/ |
166
|
|
|
public function addForeignKey($name, $table, $columns, $refTable, $refColumns, $delete = null, $update = null) |
167
|
|
|
{ |
168
|
|
|
$sql = 'ALTER TABLE ' . $this->db->quoteTableName($table) |
169
|
|
|
. ' ADD CONSTRAINT ' . $this->db->quoteColumnName($name) |
170
|
|
|
. ' FOREIGN KEY (' . $this->buildColumns($columns) . ')' |
171
|
|
|
. ' REFERENCES ' . $this->db->quoteTableName($refTable) |
172
|
|
|
. ' (' . $this->buildColumns($refColumns) . ')'; |
173
|
|
|
if ($delete !== null) { |
174
|
|
|
$sql .= ' ON DELETE ' . $delete; |
175
|
|
|
} |
176
|
|
|
if ($update !== null) { |
177
|
|
|
throw new Exception('Oracle does not support ON UPDATE clause.'); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
return $sql; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* @inheritdoc |
185
|
|
|
*/ |
186
|
|
|
public function insert($table, $columns, &$params) |
187
|
|
|
{ |
188
|
|
|
$schema = $this->db->getSchema(); |
189
|
|
|
if (($tableSchema = $schema->getTableSchema($table)) !== null) { |
190
|
|
|
$columnSchemas = $tableSchema->columns; |
191
|
|
|
} else { |
192
|
|
|
$columnSchemas = []; |
193
|
|
|
} |
194
|
|
|
$names = []; |
195
|
|
|
$placeholders = []; |
196
|
|
|
$values = ' DEFAULT VALUES'; |
197
|
|
|
if ($columns instanceof \yii\db\Query) { |
198
|
|
|
list($names, $values, $params) = $this->prepareInsertSelectSubQuery($columns, $schema, $params); |
199
|
|
|
} else { |
200
|
|
|
foreach ($columns as $name => $value) { |
201
|
|
|
$names[] = $schema->quoteColumnName($name); |
202
|
|
|
if ($value instanceof Expression) { |
203
|
|
|
$placeholders[] = $value->expression; |
204
|
|
|
foreach ($value->params as $n => $v) { |
205
|
|
|
$params[$n] = $v; |
206
|
|
|
} |
207
|
|
|
} elseif ($value instanceof \yii\db\Query) { |
208
|
|
|
list($sql, $params) = $this->build($value, $params); |
209
|
|
|
$placeholders[] = "($sql)"; |
210
|
|
|
} else { |
211
|
|
|
$phName = self::PARAM_PREFIX . count($params); |
212
|
|
|
$placeholders[] = $phName; |
213
|
|
|
$params[$phName] = !is_array($value) && isset($columnSchemas[$name]) ? $columnSchemas[$name]->dbTypecast($value) : $value; |
214
|
|
|
} |
215
|
|
|
} |
216
|
|
|
if (empty($names) && $tableSchema !== null) { |
217
|
|
|
$columns = !empty($tableSchema->primaryKey) ? $tableSchema->primaryKey : [reset($tableSchema->columns)->name]; |
218
|
|
|
foreach ($columns as $name) { |
219
|
|
|
$names[] = $schema->quoteColumnName($name); |
220
|
|
|
$placeholders[] = 'DEFAULT'; |
221
|
|
|
} |
222
|
|
|
} |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
return 'INSERT INTO ' . $schema->quoteTableName($table) |
226
|
|
|
. (!empty($names) ? ' (' . implode(', ', $names) . ')' : '') |
227
|
|
|
. (!empty($placeholders) ? ' VALUES (' . implode(', ', $placeholders) . ')' : $values); |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* Generates a batch INSERT SQL statement. |
232
|
|
|
* For example, |
233
|
|
|
* |
234
|
|
|
* ```php |
235
|
|
|
* $sql = $queryBuilder->batchInsert('user', ['name', 'age'], [ |
236
|
|
|
* ['Tom', 30], |
237
|
|
|
* ['Jane', 20], |
238
|
|
|
* ['Linda', 25], |
239
|
|
|
* ]); |
240
|
|
|
* ``` |
241
|
|
|
* |
242
|
|
|
* Note that the values in each row must match the corresponding column names. |
243
|
|
|
* |
244
|
|
|
* @param string $table the table that new rows will be inserted into. |
245
|
|
|
* @param array $columns the column names |
246
|
|
|
* @param array $rows the rows to be batch inserted into the table |
247
|
|
|
* @return string the batch INSERT SQL statement |
248
|
|
|
*/ |
249
|
|
|
public function batchInsert($table, $columns, $rows) |
250
|
|
|
{ |
251
|
|
|
if (empty($rows)) { |
252
|
|
|
return ''; |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
$schema = $this->db->getSchema(); |
256
|
|
|
if (($tableSchema = $schema->getTableSchema($table)) !== null) { |
257
|
|
|
$columnSchemas = $tableSchema->columns; |
258
|
|
|
} else { |
259
|
|
|
$columnSchemas = []; |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
$values = []; |
263
|
|
|
foreach ($rows as $row) { |
264
|
|
|
$vs = []; |
265
|
|
|
foreach ($row as $i => $value) { |
266
|
|
|
if (isset($columns[$i], $columnSchemas[$columns[$i]]) && !is_array($value)) { |
267
|
|
|
$value = $columnSchemas[$columns[$i]]->dbTypecast($value); |
268
|
|
|
} |
269
|
|
|
if (is_string($value)) { |
270
|
|
|
$value = $schema->quoteValue($value); |
271
|
|
|
} elseif ($value === false) { |
272
|
|
|
$value = 0; |
273
|
|
|
} elseif ($value === null) { |
274
|
|
|
$value = 'NULL'; |
275
|
|
|
} |
276
|
|
|
$vs[] = $value; |
277
|
|
|
} |
278
|
|
|
$values[] = '(' . implode(', ', $vs) . ')'; |
279
|
|
|
} |
280
|
|
|
if (empty($values)) { |
281
|
|
|
return ''; |
282
|
|
|
} |
283
|
|
|
|
284
|
|
|
foreach ($columns as $i => $name) { |
285
|
|
|
$columns[$i] = $schema->quoteColumnName($name); |
286
|
|
|
} |
287
|
|
|
|
288
|
|
|
$tableAndColumns = ' INTO ' . $schema->quoteTableName($table) |
289
|
|
|
. ' (' . implode(', ', $columns) . ') VALUES '; |
290
|
|
|
|
291
|
|
|
return 'INSERT ALL ' . $tableAndColumns . implode($tableAndColumns, $values) . ' SELECT 1 FROM SYS.DUAL'; |
292
|
|
|
} |
293
|
|
|
|
294
|
|
|
/** |
295
|
|
|
* @inheritdoc |
296
|
|
|
* @since 2.0.8 |
297
|
|
|
*/ |
298
|
|
|
public function selectExists($rawSql) |
299
|
|
|
{ |
300
|
|
|
return 'SELECT CASE WHEN EXISTS(' . $rawSql . ') THEN 1 ELSE 0 END FROM DUAL'; |
301
|
|
|
} |
302
|
|
|
|
303
|
|
|
/** |
304
|
|
|
* @inheritdoc |
305
|
|
|
* @since 2.0.8 |
306
|
|
|
*/ |
307
|
|
|
public function dropCommentFromColumn($table, $column) |
308
|
|
|
{ |
309
|
|
|
return 'COMMENT ON COLUMN ' . $this->db->quoteTableName($table) . '.' . $this->db->quoteColumnName($column) . " IS ''"; |
310
|
|
|
} |
311
|
|
|
|
312
|
|
|
/** |
313
|
|
|
* @inheritdoc |
314
|
|
|
* @since 2.0.8 |
315
|
|
|
*/ |
316
|
|
|
public function dropCommentFromTable($table) |
317
|
|
|
{ |
318
|
|
|
return 'COMMENT ON TABLE ' . $this->db->quoteTableName($table) . " IS ''"; |
319
|
|
|
} |
320
|
|
|
|
321
|
|
|
/** |
322
|
|
|
* @inheritDoc |
323
|
|
|
*/ |
324
|
|
|
public function buildLikeCondition($operator, $operands, &$params) |
325
|
|
|
{ |
326
|
|
|
if (!isset($this->likeEscapingReplacements['\\'])) { |
327
|
|
|
/* |
328
|
|
|
* Different pdo_oci8 versions may or may not implement PDO::quote(), so |
329
|
|
|
* yii\db\Schema::quoteValue() may or may not quote \. |
330
|
|
|
*/ |
331
|
|
|
$this->likeEscapingReplacements['\\'] = substr($this->db->quoteValue('\\'), 1, -1); |
332
|
|
|
} |
333
|
|
|
return parent::buildLikeCondition($operator, $operands, $params); |
334
|
|
|
} |
335
|
|
|
|
336
|
|
|
/** |
337
|
|
|
* @inheritdoc |
338
|
|
|
*/ |
339
|
|
|
public function buildInCondition($operator, $operands, &$params) |
340
|
|
|
{ |
341
|
|
|
$splitCondition = $this->splitInCondition($operator, $operands, $params); |
342
|
|
|
if ($splitCondition !== null) { |
343
|
|
|
return $splitCondition; |
344
|
|
|
} |
345
|
|
|
|
346
|
|
|
return parent::buildInCondition($operator, $operands, $params); |
347
|
|
|
} |
348
|
|
|
|
349
|
|
|
/** |
350
|
|
|
* Oracle DBMS does not support more than 1000 parameters in `IN` condition. |
351
|
|
|
* This method splits long `IN` condition into series of smaller ones. |
352
|
|
|
* |
353
|
|
|
* @param string $operator |
354
|
|
|
* @param array $operands |
355
|
|
|
* @param array $params |
356
|
|
|
* @return null|string null when split is not required. Otherwise - built SQL condition. |
357
|
|
|
* @throws Exception |
358
|
|
|
* @since 2.0.12 |
359
|
|
|
*/ |
360
|
|
|
protected function splitInCondition($operator, $operands, &$params) |
361
|
|
|
{ |
362
|
|
|
if (!isset($operands[0], $operands[1])) { |
363
|
|
|
throw new Exception("Operator '$operator' requires two operands."); |
364
|
|
|
} |
365
|
|
|
|
366
|
|
|
list($column, $values) = $operands; |
367
|
|
|
|
368
|
|
|
if ($values instanceof \Traversable) { |
369
|
|
|
$values = iterator_to_array($values); |
370
|
|
|
} |
371
|
|
|
|
372
|
|
|
if (!is_array($values)) { |
373
|
|
|
return null; |
374
|
|
|
} |
375
|
|
|
|
376
|
|
|
$maxParameters = 1000; |
377
|
|
|
$count = count($values); |
378
|
|
|
if ($count <= $maxParameters) { |
379
|
|
|
return null; |
380
|
|
|
} |
381
|
|
|
|
382
|
|
|
$condition = [($operator === 'IN') ? 'OR' : 'AND']; |
383
|
|
|
for ($i = 0; $i < $count; $i += $maxParameters) { |
384
|
|
|
$condition[] = [$operator, $column, array_slice($values, $i, $maxParameters)]; |
385
|
|
|
} |
386
|
|
|
|
387
|
|
|
return $this->buildCondition(['AND', $condition], $params); |
388
|
|
|
} |
389
|
|
|
|
390
|
|
|
} |
391
|
|
|
|