-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquery.inc
417 lines (354 loc) · 16.3 KB
/
query.inc
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
<?php
/**
* @ingroup database
* @{
*/
/**
* @file
* Query code for Oracle embedded database engine.
*/
class InsertQuery_oci extends InsertQuery {
protected $schema = array();
public function __construct($connection, $table, array $options = array()) {
parent::__construct($connection, $table, $options);
// Fetch the list of blobs and sequences used on that table.
$schema = $this->connection->schema()->queryTableInformation($table);
$this->schema = isset($schema['fields']) ? $schema : array('fields' => array());
}
public function execute() {
if (!$this->preExecute()) {
return NULL;
}
// Oracle requires the table name to be specified explicitly
// when requesting the last insert ID, so we pass that in via
// the options array.
$options = $this->queryOptions;
$options['sequence_name'] = NULL;
foreach ($this->schema['fields'] as $field => $value) {
if ($value['type'] == 'serial') {
$options['sequence_name'] = $this->connection->makeSequenceName($this->table, $field);
break;
}
}
// If there are no sequences then we can't get a last insert id.
if ($options['sequence_name'] == NULL && $options['return'] == Database::RETURN_INSERT_ID) {
$options['return'] = Database::RETURN_NULL;
}
// Each insert happens in its own query in the degenerate case. However,
// we wrap it in a transaction so that it is atomic where possible. On many
// databases, such as Oracle, this is also a notable performance boost.
$transaction = $this->connection->startTransaction();
$stmt = $this->connection->prepareQuery((string) $this)->getStatement(NULL);
try {
$blobs = array();
$blob_count = 0;
foreach ($this->insertValues as $insert_values) {
$max_placeholder = 0;
foreach ($this->insertFields as $idx => $field) {
if (isset($this->schema['fields'][$field]) && $this->schema['fields'][$field]['type'] == 'blob') {
if (!is_null($insert_values[$idx])) {
// Writing NULL into file handler means write nothing. The file
// handler will contain an empty string but not NULL. Therefore
// NULL will be translated as empty string when save into
// database, and so buggy.
$blobs[$blob_count] = fopen('php://memory', 'a');
fwrite($blobs[$blob_count], $insert_values[$idx]);
rewind($blobs[$blob_count]);
$stmt->bindParam(':db_insert_placeholder_' . $max_placeholder++, $blobs[$blob_count], PDO::PARAM_LOB);
// Pre-increment is faster in PHP than increment.
++$blob_count;
}
else {
// BLOB NULL is a bit different from normal case: we can't use
// StreamAPI, but bind NULL to target field direcly.
$stmt->bindParam(':db_insert_placeholder_' . $max_placeholder++, $insert_values[$idx], PDO::PARAM_LOB);
}
}
elseif ($insert_values[$idx] === '') {
$stmt->bindValue(':db_insert_placeholder_' . $max_placeholder++, OCI_EMPTY_STRING_PLACEHOLDER);
}
else {
$stmt->bindParam(':db_insert_placeholder_' . $max_placeholder++, $insert_values[$idx]);
}
}
// Check if values for a serial field has been passed.
foreach ($this->schema['fields'] as $field => $value) {
if ($value['type'] == 'serial') {
$key = array_search($field, $this->insertFields);
if ($key !== FALSE) {
$last_insert_id = $insert_values[$key] > 1 ? $insert_values[$key] : 1;
// Set the sequence to the bigger value of either the passed
// value or the max value of the column. It can happen that another
// thread calls nextval() which could lead to a serial number being
// used twice. However, trying to insert a value into a serial
// column should only be done in very rare cases and is not thread
// safe by definition.
$current_value = $this->connection->lastInsertId($options['sequence_name']);
$this->connection->query('DROP SEQUENCE ' . $options['sequence_name']);
$this->connection->query('CREATE SEQUENCE ' . $options['sequence_name'] . ' MINVALUE 1 INCREMENT BY 1 START WITH ' . max($last_insert_id, $current_value) . ' NOCACHE NOORDER NOCYCLE');
}
}
}
// Only use the returned last_insert_id if it is not already set.
$last_insert_id = $this->connection->query($stmt, array(), $options);
}
if (!empty($this->fromQuery)) {
// bindParam stores only a reference to the variable that is followed when
// the statement is executed. We pass $arguments[$key] instead of $value
// because the second argument to bindParam is passed by reference and
// the foreach statement assigns the element to the existing reference.
$arguments = $this->fromQuery->getArguments();
foreach ($arguments as $key => $value) {
if ($arguments[$key] === '') {
$stmt->bindValue($key, OCI_EMPTY_STRING_PLACEHOLDER);
}
else {
$stmt->bindParam($key, $arguments[$key]);
}
}
}
// All default query.
if (count($this->insertValues) == 0) {
// Only use the returned last_insert_id if it is not already set.
$last_insert_id = $this->connection->query($stmt, array(), $options);
}
}
catch (Exception $e) {
// One of the INSERTs failed, rollback the whole batch.
$transaction->rollback();
// Rethrow the exception for the calling code.
throw $e;
}
// Re-initialize the values array so that we can re-use this query.
$this->insertValues = array();
// Transaction commits here where $transaction looses scope.
return $last_insert_id;
}
public function __toString() {
// Create a sanitized comment string to prepend to the query.
$comments = $this->connection->makeComment($this->comments);
// Prefix and escape table name manually.
$table_name = $this->connection->prefixTables('{' . $this->table . '}');
$table_name = $this->connection->escapeForOracle($table_name);
// Default fields are always placed first for consistency.
$insert_fields = array_merge($this->defaultFields, $this->insertFields);
foreach ($insert_fields as $key => $value) {
$insert_fields[$key] = $this->connection->escapeForOracle($value);
}
// If we're selecting from a SelectQuery, finish building the query and
// pass it back, as any remaining options are irrelevant.
if (!empty($this->fromQuery)) {
$query = $comments . 'INSERT INTO ' . $table_name . ' (' . implode(', ', $insert_fields) . ') ' . $this->fromQuery;
$query = $this->connection->prefixTables($query);
$query = $this->connection->escapeForOracle($query);
return $query;
}
$query = $comments . 'INSERT INTO ' . $table_name . ' (' . implode(', ', $insert_fields) . ') VALUES ';
$blob_fields = array();
$blob_placeholders = array();
if (count($this->insertValues)) {
$placeholders = array();
// Default fields aren't really placeholders, but this is the most convenient
// way to handle them.
$placeholders = array_pad($placeholders, count($this->defaultFields), 'DEFAULT');
$i = 0;
foreach ($this->insertFields as $field) {
if (isset($this->schema['fields'][$field]) && $this->schema['fields'][$field]['type'] == 'blob') {
// BLOB field insert in Oracle required additional handling with
// EMPTY_BLOB() pleaseholder + RETURNING INTO (see below).
$placeholders[] = 'EMPTY_BLOB()';
$blob_fields[] = $this->connection->escapeForOracle($field);
$blob_placeholders[] = ':db_insert_placeholder_' . $i;
}
else {
$placeholders[] = ':db_insert_placeholder_' . $i;
}
++$i;
}
$query .= '(' . implode(', ', $placeholders) . ')';
}
else {
// If there are no values, then this is a default-only query. We still need to handle that.
$placeholders = array_fill(0, count($this->defaultFields), 'DEFAULT');
$query .= '(' . implode(', ', $placeholders) . ')';
}
// Oracle BLOB additional handling with RETURNING INTO.
if (count($blob_fields)) {
$query .= ' RETURNING ' . implode(', ', $blob_fields) . ' INTO ' . implode (', ', $blob_placeholders);
}
return $query;
}
}
class TruncateQuery_oci extends TruncateQuery {
public function __toString() {
// Create a sanitized comment string to prepend to the query.
$comments = $this->connection->makeComment($this->comments);
return $comments . 'TRUNCATE TABLE {' . $this->table . '} ';
}
}
class UpdateQuery_oci extends UpdateQuery {
protected $schema = array();
public function __construct(DatabaseConnection $connection, $table, array $options = array()) {
parent::__construct($connection, $table, $options);
// Fetch the list of blobs and sequences used on that table.
$schema = $this->connection->schema()->queryTableInformation($table);
$this->schema = isset($schema['fields']) ? $schema : array('fields' => array());
}
protected function removeFieldsInCondition(&$fields, QueryConditionInterface $condition) {
foreach ($condition->conditions() as $child_condition) {
if (isset($child_condition['field']) && $child_condition['field'] instanceof QueryConditionInterface) {
$this->removeFieldsInCondition($fields, $child_condition['field']);
}
elseif (isset($child_condition['field'])) {
unset($fields[$child_condition['field']]);
}
}
}
public function execute() {
$max_placeholder = 0;
$blobs = array();
$blob_count = 0;
// Oracle requires the table name to be specified explicitly
// when requesting the last insert ID, so we pass that in via
// the options array.
$options = $this->queryOptions;
$options['already_prepared'] = TRUE;
// Oracle counts all the rows that match the conditions as modified, even if they
// will not be affected by the query. We workaround this by ensuring that
// we don't select those rows.
if (empty($this->queryOptions['oci_return_matched_rows'])) {
// Get the fields used in the update query, and remove those that are already
// in the condition.
$fields = $this->expressionFields + $this->fields;
$this->removeFieldsInCondition($fields, $this->condition);
// Add the inverse of the fields to the condition.
$condition = new DatabaseCondition('OR');
foreach ($fields as $field => $data) {
if (!(isset($this->schema['fields'][$field]) && $this->schema['fields'][$field]['type'] == 'blob')) {
if (is_array($data)) {
// The field is an expression.
$condition->where($field . ' <> ' . $data['expression']);
$condition->isNull($field);
}
elseif (!isset($data)) {
// The field will be set to NULL.
$condition->isNotNull($field);
}
else {
$condition->condition($field, $data, '<>');
$condition->isNull($field);
}
}
}
if (count($condition)) {
$condition->compile($this->connection, $this);
$this->condition->where((string) $condition, $condition->arguments());
}
}
// Each insert happens in its own query in the degenerate case. However,
// we wrap it in a transaction so that it is atomic where possible. On many
// databases, such as Oracle, this is also a notable performance boost.
$transaction = $this->connection->startTransaction();
// Because we filter $fields the same way here and in __toString(), the
// placeholders will all match up properly.
$stmt = $this->connection->prepareQuery((string) $this)->getStatement(NULL);
// Expressions take priority over literal fields, so we process those first
// and remove any literal fields that conflict.
$fields = $this->fields;
foreach ($this->expressionFields as $field => $data) {
if (!empty($data['arguments'])) {
foreach ($data['arguments'] as $placeholder => $argument) {
// We assume that an expression will never happen on a BLOB field,
// which is a fairly safe assumption to make since in most cases
// it would be an invalid query anyway.
if ($data['arguments'][$placeholder] === '') {
$stmt->bindValue($placeholder, OCI_EMPTY_STRING_PLACEHOLDER);
}
else {
$stmt->bindParam($placeholder, $data['arguments'][$placeholder]);
}
}
}
unset($fields[$field]);
}
foreach ($fields as $field => $value) {
if (isset($this->schema['fields'][$field]) && $this->schema['fields'][$field]['type'] == 'blob') {
if (!is_null($fields[$field])) {
$blobs[$blob_count] = fopen('php://memory', 'a');
fwrite($blobs[$blob_count], $value);
rewind($blobs[$blob_count]);
$stmt->bindParam(':db_update_placeholder_' . $max_placeholder++, $blobs[$blob_count], PDO::PARAM_LOB);
// Pre-increment is faster in PHP than increment.
++$blob_count;
}
else {
// BLOB NULL is a bit different from normal case: we can't use
// StreamAPI, but bind NULL to target field direcly.
$stmt->bindParam(':db_update_placeholder_' . $max_placeholder++, $fields[$field], PDO::PARAM_LOB);
}
}
elseif ($fields[$field] === '') {
$stmt->bindValue(':db_update_placeholder_' . $max_placeholder++, OCI_EMPTY_STRING_PLACEHOLDER);
}
else {
$stmt->bindParam(':db_update_placeholder_' . $max_placeholder++, $fields[$field]);
}
}
if (count($this->condition)) {
$this->condition->compile($this->connection, $this);
$arguments = $this->condition->arguments();
foreach ($arguments as $placeholder => $value) {
if ($arguments[$placeholder] === '') {
$stmt->bindValue($placeholder, OCI_EMPTY_STRING_PLACEHOLDER);
}
else {
$stmt->bindParam($placeholder, $arguments[$placeholder]);
}
}
}
$this->connection->query($stmt, $options);
// Transaction commits here where $transaction looses scope.
return $stmt->rowCount();
}
public function __toString() {
// Create a sanitized comment string to prepend to the query.
$comments = $this->connection->makeComment($this->comments);
// Prefix and escape table name manually.
$table_name = $this->connection->prefixTables('{' . $this->table . '}');
$table_name = $this->connection->escapeForOracle($table_name);
// Expressions take priority over literal fields, so we process those first
// and remove any literal fields that conflict.
$fields = $this->fields;
$update_fields = array();
foreach ($this->expressionFields as $field => $data) {
$update_fields[] = $this->connection->escapeForOracle($field) . ' = ' . $data['expression'];
unset($fields[$field]);
}
$max_placeholder = 0;
$blob_fields = array();
$blob_placeholders = array();
foreach ($fields as $field => $value) {
if (isset($this->schema['fields'][$field]) && $this->schema['fields'][$field]['type'] == 'blob') {
// BLOB field update in Oracle required additional handling with
// EMPTY_BLOB() pleaseholder + RETURNING INTO (see below).
$update_fields[] = $this->connection->escapeForOracle($field) . ' = EMPTY_BLOB()';
$blob_fields[] = $this->connection->escapeForOracle($field);
$blob_placeholders[] = ':db_update_placeholder_' . $max_placeholder++;
}
else {
$update_fields[] = $this->connection->escapeForOracle($field) . ' = :db_update_placeholder_' . $max_placeholder++;
}
}
$query = $comments . 'UPDATE ' . $table_name . ' SET ' . implode(', ', $update_fields);
if (count($this->condition)) {
$this->condition->compile($this->connection, $this);
// There is an implicit string cast on $this->condition.
$query .= " WHERE " . $this->condition;
}
// Oracle BLOB additional handling with RETURNING INTO.
if (count($blob_fields)) {
$query .= ' RETURNING ' . implode(', ', $blob_fields) . ' INTO ' . implode (', ', $blob_placeholders);
}
return $query;
}
}