Merge "TableSorter: Fix column order when collecting headers"
[lhc/web/wiklou.git] / includes / db / DatabaseOracle.php
1 <?php
2 /**
3 * This is the Oracle database abstraction layer.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @ingroup Database
22 */
23
24 /**
25 * The oci8 extension is fairly weak and doesn't support oci_num_rows, among
26 * other things. We use a wrapper class to handle that and other
27 * Oracle-specific bits, like converting column names back to lowercase.
28 * @ingroup Database
29 */
30 class ORAResult {
31 private $rows;
32 private $cursor;
33 private $nrows;
34
35 private $columns = array();
36
37 private function array_unique_md( $array_in ) {
38 $array_out = array();
39 $array_hashes = array();
40
41 foreach ( $array_in as $item ) {
42 $hash = md5( serialize( $item ) );
43 if ( !isset( $array_hashes[$hash] ) ) {
44 $array_hashes[$hash] = $hash;
45 $array_out[] = $item;
46 }
47 }
48
49 return $array_out;
50 }
51
52 /**
53 * @param DatabaseBase $db
54 * @param resource $stmt A valid OCI statement identifier
55 * @param bool $unique
56 */
57 function __construct( &$db, $stmt, $unique = false ) {
58 $this->db =& $db;
59
60 $this->nrows = oci_fetch_all( $stmt, $this->rows, 0, -1, OCI_FETCHSTATEMENT_BY_ROW | OCI_NUM );
61 if ( $this->nrows === false ) {
62 $e = oci_error( $stmt );
63 $db->reportQueryError( $e['message'], $e['code'], '', __METHOD__ );
64 $this->free();
65
66 return;
67 }
68
69 if ( $unique ) {
70 $this->rows = $this->array_unique_md( $this->rows );
71 $this->nrows = count( $this->rows );
72 }
73
74 if ( $this->nrows > 0 ) {
75 foreach ( $this->rows[0] as $k => $v ) {
76 $this->columns[$k] = strtolower( oci_field_name( $stmt, $k + 1 ) );
77 }
78 }
79
80 $this->cursor = 0;
81 oci_free_statement( $stmt );
82 }
83
84 public function free() {
85 unset( $this->db );
86 }
87
88 public function seek( $row ) {
89 $this->cursor = min( $row, $this->nrows );
90 }
91
92 public function numRows() {
93 return $this->nrows;
94 }
95
96 public function numFields() {
97 return count( $this->columns );
98 }
99
100 public function fetchObject() {
101 if ( $this->cursor >= $this->nrows ) {
102 return false;
103 }
104 $row = $this->rows[$this->cursor++];
105 $ret = new stdClass();
106 foreach ( $row as $k => $v ) {
107 $lc = $this->columns[$k];
108 $ret->$lc = $v;
109 }
110
111 return $ret;
112 }
113
114 public function fetchRow() {
115 if ( $this->cursor >= $this->nrows ) {
116 return false;
117 }
118
119 $row = $this->rows[$this->cursor++];
120 $ret = array();
121 foreach ( $row as $k => $v ) {
122 $lc = $this->columns[$k];
123 $ret[$lc] = $v;
124 $ret[$k] = $v;
125 }
126
127 return $ret;
128 }
129 }
130
131 /**
132 * Utility class.
133 * @ingroup Database
134 */
135 class ORAField implements Field {
136 private $name, $tablename, $default, $max_length, $nullable,
137 $is_pk, $is_unique, $is_multiple, $is_key, $type;
138
139 function __construct( $info ) {
140 $this->name = $info['column_name'];
141 $this->tablename = $info['table_name'];
142 $this->default = $info['data_default'];
143 $this->max_length = $info['data_length'];
144 $this->nullable = $info['not_null'];
145 $this->is_pk = isset( $info['prim'] ) && $info['prim'] == 1 ? 1 : 0;
146 $this->is_unique = isset( $info['uniq'] ) && $info['uniq'] == 1 ? 1 : 0;
147 $this->is_multiple = isset( $info['nonuniq'] ) && $info['nonuniq'] == 1 ? 1 : 0;
148 $this->is_key = ( $this->is_pk || $this->is_unique || $this->is_multiple );
149 $this->type = $info['data_type'];
150 }
151
152 function name() {
153 return $this->name;
154 }
155
156 function tableName() {
157 return $this->tablename;
158 }
159
160 function defaultValue() {
161 return $this->default;
162 }
163
164 function maxLength() {
165 return $this->max_length;
166 }
167
168 function isNullable() {
169 return $this->nullable;
170 }
171
172 function isKey() {
173 return $this->is_key;
174 }
175
176 function isMultipleKey() {
177 return $this->is_multiple;
178 }
179
180 function type() {
181 return $this->type;
182 }
183 }
184
185 /**
186 * @ingroup Database
187 */
188 class DatabaseOracle extends DatabaseBase {
189 /** @var resource */
190 protected $mLastResult = null;
191
192 /** @var int The number of rows affected as an integer */
193 protected $mAffectedRows;
194
195 /** @var int */
196 private $mInsertId = null;
197
198 /** @var bool */
199 private $ignoreDupValOnIndex = false;
200
201 /** @var bool|array */
202 private $sequenceData = null;
203
204 /** @var string Character set for Oracle database */
205 private $defaultCharset = 'AL32UTF8';
206
207 /** @var array */
208 private $mFieldInfoCache = array();
209
210 function __construct( $p = null ) {
211 global $wgDBprefix;
212
213 if ( !is_array( $p ) ) { // legacy calling pattern
214 wfDeprecated( __METHOD__ . " method called without parameter array.", "1.22" );
215 $args = func_get_args();
216 $p = array(
217 'host' => isset( $args[0] ) ? $args[0] : false,
218 'user' => isset( $args[1] ) ? $args[1] : false,
219 'password' => isset( $args[2] ) ? $args[2] : false,
220 'dbname' => isset( $args[3] ) ? $args[3] : false,
221 'flags' => isset( $args[4] ) ? $args[4] : 0,
222 'tablePrefix' => isset( $args[5] ) ? $args[5] : 'get from global',
223 'schema' => 'get from global',
224 'foreign' => isset( $args[6] ) ? $args[6] : false
225 );
226 }
227 if ( $p['tablePrefix'] == 'get from global' ) {
228 $p['tablePrefix'] = $wgDBprefix;
229 }
230 $p['tablePrefix'] = strtoupper( $p['tablePrefix'] );
231 parent::__construct( $p );
232 wfRunHooks( 'DatabaseOraclePostInit', array( $this ) );
233 }
234
235 function __destruct() {
236 if ( $this->mOpened ) {
237 wfSuppressWarnings();
238 $this->close();
239 wfRestoreWarnings();
240 }
241 }
242
243 function getType() {
244 return 'oracle';
245 }
246
247 function cascadingDeletes() {
248 return true;
249 }
250
251 function cleanupTriggers() {
252 return true;
253 }
254
255 function strictIPs() {
256 return true;
257 }
258
259 function realTimestamps() {
260 return true;
261 }
262
263 function implicitGroupby() {
264 return false;
265 }
266
267 function implicitOrderby() {
268 return false;
269 }
270
271 function searchableIPs() {
272 return true;
273 }
274
275 /**
276 * Usually aborts on failure
277 * @param string $server
278 * @param string $user
279 * @param string $password
280 * @param string $dbName
281 * @throws DBConnectionError
282 * @return DatabaseBase|null
283 */
284 function open( $server, $user, $password, $dbName ) {
285 global $wgDBOracleDRCP;
286 if ( !function_exists( 'oci_connect' ) ) {
287 throw new DBConnectionError(
288 $this,
289 "Oracle functions missing, have you compiled PHP with the --with-oci8 option?\n " .
290 "(Note: if you recently installed PHP, you may need to restart your webserver\n " .
291 "and database)\n" );
292 }
293
294 $this->close();
295 $this->mUser = $user;
296 $this->mPassword = $password;
297 // changed internal variables functions
298 // mServer now holds the TNS endpoint
299 // mDBname is schema name if different from username
300 if ( !$server ) {
301 // backward compatibillity (server used to be null and TNS was supplied in dbname)
302 $this->mServer = $dbName;
303 $this->mDBname = $user;
304 } else {
305 $this->mServer = $server;
306 if ( !$dbName ) {
307 $this->mDBname = $user;
308 } else {
309 $this->mDBname = $dbName;
310 }
311 }
312
313 if ( !strlen( $user ) ) { # e.g. the class is being loaded
314 return null;
315 }
316
317 if ( $wgDBOracleDRCP ) {
318 $this->setFlag( DBO_PERSISTENT );
319 }
320
321 $session_mode = $this->mFlags & DBO_SYSDBA ? OCI_SYSDBA : OCI_DEFAULT;
322
323 wfSuppressWarnings();
324 if ( $this->mFlags & DBO_PERSISTENT ) {
325 $this->mConn = oci_pconnect(
326 $this->mUser,
327 $this->mPassword,
328 $this->mServer,
329 $this->defaultCharset,
330 $session_mode
331 );
332 } elseif ( $this->mFlags & DBO_DEFAULT ) {
333 $this->mConn = oci_new_connect(
334 $this->mUser,
335 $this->mPassword,
336 $this->mServer,
337 $this->defaultCharset,
338 $session_mode
339 );
340 } else {
341 $this->mConn = oci_connect(
342 $this->mUser,
343 $this->mPassword,
344 $this->mServer,
345 $this->defaultCharset,
346 $session_mode
347 );
348 }
349 wfRestoreWarnings();
350
351 if ( $this->mUser != $this->mDBname ) {
352 //change current schema in session
353 $this->selectDB( $this->mDBname );
354 }
355
356 if ( !$this->mConn ) {
357 throw new DBConnectionError( $this, $this->lastError() );
358 }
359
360 $this->mOpened = true;
361
362 # removed putenv calls because they interfere with the system globaly
363 $this->doQuery( 'ALTER SESSION SET NLS_TIMESTAMP_FORMAT=\'DD-MM-YYYY HH24:MI:SS.FF6\'' );
364 $this->doQuery( 'ALTER SESSION SET NLS_TIMESTAMP_TZ_FORMAT=\'DD-MM-YYYY HH24:MI:SS.FF6\'' );
365 $this->doQuery( 'ALTER SESSION SET NLS_NUMERIC_CHARACTERS=\'.,\'' );
366
367 return $this->mConn;
368 }
369
370 /**
371 * Closes a database connection, if it is open
372 * Returns success, true if already closed
373 * @return bool
374 */
375 protected function closeConnection() {
376 return oci_close( $this->mConn );
377 }
378
379 function execFlags() {
380 return $this->mTrxLevel ? OCI_NO_AUTO_COMMIT : OCI_COMMIT_ON_SUCCESS;
381 }
382
383 protected function doQuery( $sql ) {
384 wfDebug( "SQL: [$sql]\n" );
385 if ( !StringUtils::isUtf8( $sql ) ) {
386 throw new MWException( "SQL encoding is invalid\n$sql" );
387 }
388
389 // handle some oracle specifics
390 // remove AS column/table/subquery namings
391 if ( !$this->getFlag( DBO_DDLMODE ) ) {
392 $sql = preg_replace( '/ as /i', ' ', $sql );
393 }
394
395 // Oracle has issues with UNION clause if the statement includes LOB fields
396 // So we do a UNION ALL and then filter the results array with array_unique
397 $union_unique = ( preg_match( '/\/\* UNION_UNIQUE \*\/ /', $sql ) != 0 );
398 // EXPLAIN syntax in Oracle is EXPLAIN PLAN FOR and it return nothing
399 // you have to select data from plan table after explain
400 $explain_id = MWTimestamp::getLocalInstance()->format( 'dmYHis' );
401
402 $sql = preg_replace(
403 '/^EXPLAIN /',
404 'EXPLAIN PLAN SET STATEMENT_ID = \'' . $explain_id . '\' FOR',
405 $sql,
406 1,
407 $explain_count
408 );
409
410 wfSuppressWarnings();
411
412 if ( ( $this->mLastResult = $stmt = oci_parse( $this->mConn, $sql ) ) === false ) {
413 $e = oci_error( $this->mConn );
414 $this->reportQueryError( $e['message'], $e['code'], $sql, __METHOD__ );
415
416 return false;
417 }
418
419 if ( !oci_execute( $stmt, $this->execFlags() ) ) {
420 $e = oci_error( $stmt );
421 if ( !$this->ignoreDupValOnIndex || $e['code'] != '1' ) {
422 $this->reportQueryError( $e['message'], $e['code'], $sql, __METHOD__ );
423
424 return false;
425 }
426 }
427
428 wfRestoreWarnings();
429
430 if ( $explain_count > 0 ) {
431 return $this->doQuery( 'SELECT id, cardinality "ROWS" FROM plan_table ' .
432 'WHERE statement_id = \'' . $explain_id . '\'' );
433 } elseif ( oci_statement_type( $stmt ) == 'SELECT' ) {
434 return new ORAResult( $this, $stmt, $union_unique );
435 } else {
436 $this->mAffectedRows = oci_num_rows( $stmt );
437
438 return true;
439 }
440 }
441
442 function queryIgnore( $sql, $fname = '' ) {
443 return $this->query( $sql, $fname, true );
444 }
445
446 /**
447 * Frees resources associated with the LOB descriptor
448 * @param ResultWrapper|resource $res
449 */
450 function freeResult( $res ) {
451 if ( $res instanceof ResultWrapper ) {
452 $res = $res->result;
453 }
454
455 $res->free();
456 }
457
458 /**
459 * @param ResultWrapper|stdClass $res
460 * @return mixed
461 */
462 function fetchObject( $res ) {
463 if ( $res instanceof ResultWrapper ) {
464 $res = $res->result;
465 }
466
467 return $res->fetchObject();
468 }
469
470 function fetchRow( $res ) {
471 if ( $res instanceof ResultWrapper ) {
472 $res = $res->result;
473 }
474
475 return $res->fetchRow();
476 }
477
478 function numRows( $res ) {
479 if ( $res instanceof ResultWrapper ) {
480 $res = $res->result;
481 }
482
483 return $res->numRows();
484 }
485
486 function numFields( $res ) {
487 if ( $res instanceof ResultWrapper ) {
488 $res = $res->result;
489 }
490
491 return $res->numFields();
492 }
493
494 function fieldName( $stmt, $n ) {
495 return oci_field_name( $stmt, $n );
496 }
497
498 /**
499 * This must be called after nextSequenceVal
500 * @return null|int
501 */
502 function insertId() {
503 return $this->mInsertId;
504 }
505
506 /**
507 * @param mixed $res
508 * @param int $row
509 */
510 function dataSeek( $res, $row ) {
511 if ( $res instanceof ORAResult ) {
512 $res->seek( $row );
513 } else {
514 $res->result->seek( $row );
515 }
516 }
517
518 function lastError() {
519 if ( $this->mConn === false ) {
520 $e = oci_error();
521 } else {
522 $e = oci_error( $this->mConn );
523 }
524
525 return $e['message'];
526 }
527
528 function lastErrno() {
529 if ( $this->mConn === false ) {
530 $e = oci_error();
531 } else {
532 $e = oci_error( $this->mConn );
533 }
534
535 return $e['code'];
536 }
537
538 function affectedRows() {
539 return $this->mAffectedRows;
540 }
541
542 /**
543 * Returns information about an index
544 * If errors are explicitly ignored, returns NULL on failure
545 * @param string $table
546 * @param string $index
547 * @param string $fname
548 * @return bool
549 */
550 function indexInfo( $table, $index, $fname = __METHOD__ ) {
551 return false;
552 }
553
554 function indexUnique( $table, $index, $fname = __METHOD__ ) {
555 return false;
556 }
557
558 function insert( $table, $a, $fname = __METHOD__, $options = array() ) {
559 if ( !count( $a ) ) {
560 return true;
561 }
562
563 if ( !is_array( $options ) ) {
564 $options = array( $options );
565 }
566
567 if ( in_array( 'IGNORE', $options ) ) {
568 $this->ignoreDupValOnIndex = true;
569 }
570
571 if ( !is_array( reset( $a ) ) ) {
572 $a = array( $a );
573 }
574
575 foreach ( $a as &$row ) {
576 $this->insertOneRow( $table, $row, $fname );
577 }
578 $retVal = true;
579
580 if ( in_array( 'IGNORE', $options ) ) {
581 $this->ignoreDupValOnIndex = false;
582 }
583
584 return $retVal;
585 }
586
587 private function fieldBindStatement( $table, $col, &$val, $includeCol = false ) {
588 $col_info = $this->fieldInfoMulti( $table, $col );
589 $col_type = $col_info != false ? $col_info->type() : 'CONSTANT';
590
591 $bind = '';
592 if ( is_numeric( $col ) ) {
593 $bind = $val;
594 $val = null;
595
596 return $bind;
597 } elseif ( $includeCol ) {
598 $bind = "$col = ";
599 }
600
601 if ( $val == '' && $val !== 0 && $col_type != 'BLOB' && $col_type != 'CLOB' ) {
602 $val = null;
603 }
604
605 if ( $val === 'NULL' ) {
606 $val = null;
607 }
608
609 if ( $val === null ) {
610 if ( $col_info != false && $col_info->isNullable() == 0 && $col_info->defaultValue() != null ) {
611 $bind .= 'DEFAULT';
612 } else {
613 $bind .= 'NULL';
614 }
615 } else {
616 $bind .= ':' . $col;
617 }
618
619 return $bind;
620 }
621
622 /**
623 * @param string $table
624 * @param $row
625 * @param string $fname
626 * @return bool
627 * @throws DBUnexpectedError
628 */
629 private function insertOneRow( $table, $row, $fname ) {
630 global $wgContLang;
631
632 $table = $this->tableName( $table );
633 // "INSERT INTO tables (a, b, c)"
634 $sql = "INSERT INTO " . $table . " (" . join( ',', array_keys( $row ) ) . ')';
635 $sql .= " VALUES (";
636
637 // for each value, append ":key"
638 $first = true;
639 foreach ( $row as $col => &$val ) {
640 if ( !$first ) {
641 $sql .= ', ';
642 } else {
643 $first = false;
644 }
645 if ( $this->isQuotedIdentifier( $val ) ) {
646 $sql .= $this->removeIdentifierQuotes( $val );
647 unset( $row[$col] );
648 } else {
649 $sql .= $this->fieldBindStatement( $table, $col, $val );
650 }
651 }
652 $sql .= ')';
653
654 if ( ( $this->mLastResult = $stmt = oci_parse( $this->mConn, $sql ) ) === false ) {
655 $e = oci_error( $this->mConn );
656 $this->reportQueryError( $e['message'], $e['code'], $sql, __METHOD__ );
657
658 return false;
659 }
660 foreach ( $row as $col => &$val ) {
661 $col_info = $this->fieldInfoMulti( $table, $col );
662 $col_type = $col_info != false ? $col_info->type() : 'CONSTANT';
663
664 if ( $val === null ) {
665 // do nothing ... null was inserted in statement creation
666 } elseif ( $col_type != 'BLOB' && $col_type != 'CLOB' ) {
667 if ( is_object( $val ) ) {
668 $val = $val->fetch();
669 }
670
671 // backward compatibility
672 if ( preg_match( '/^timestamp.*/i', $col_type ) == 1 && strtolower( $val ) == 'infinity' ) {
673 $val = $this->getInfinity();
674 }
675
676 $val = ( $wgContLang != null ) ? $wgContLang->checkTitleEncoding( $val ) : $val;
677 if ( oci_bind_by_name( $stmt, ":$col", $val, -1, SQLT_CHR ) === false ) {
678 $e = oci_error( $stmt );
679 $this->reportQueryError( $e['message'], $e['code'], $sql, __METHOD__ );
680
681 return false;
682 }
683 } else {
684 /** @var OCI_Lob[] $lob */
685 if ( ( $lob[$col] = oci_new_descriptor( $this->mConn, OCI_D_LOB ) ) === false ) {
686 $e = oci_error( $stmt );
687 throw new DBUnexpectedError( $this, "Cannot create LOB descriptor: " . $e['message'] );
688 }
689
690 if ( is_object( $val ) ) {
691 $val = $val->fetch();
692 }
693
694 if ( $col_type == 'BLOB' ) {
695 $lob[$col]->writeTemporary( $val, OCI_TEMP_BLOB );
696 oci_bind_by_name( $stmt, ":$col", $lob[$col], -1, OCI_B_BLOB );
697 } else {
698 $lob[$col]->writeTemporary( $val, OCI_TEMP_CLOB );
699 oci_bind_by_name( $stmt, ":$col", $lob[$col], -1, OCI_B_CLOB );
700 }
701 }
702 }
703
704 wfSuppressWarnings();
705
706 if ( oci_execute( $stmt, $this->execFlags() ) === false ) {
707 $e = oci_error( $stmt );
708 if ( !$this->ignoreDupValOnIndex || $e['code'] != '1' ) {
709 $this->reportQueryError( $e['message'], $e['code'], $sql, __METHOD__ );
710
711 return false;
712 } else {
713 $this->mAffectedRows = oci_num_rows( $stmt );
714 }
715 } else {
716 $this->mAffectedRows = oci_num_rows( $stmt );
717 }
718
719 wfRestoreWarnings();
720
721 if ( isset( $lob ) ) {
722 foreach ( $lob as $lob_v ) {
723 $lob_v->free();
724 }
725 }
726
727 if ( !$this->mTrxLevel ) {
728 oci_commit( $this->mConn );
729 }
730
731 return oci_free_statement( $stmt );
732 }
733
734 function insertSelect( $destTable, $srcTable, $varMap, $conds, $fname = __METHOD__,
735 $insertOptions = array(), $selectOptions = array()
736 ) {
737 $destTable = $this->tableName( $destTable );
738 if ( !is_array( $selectOptions ) ) {
739 $selectOptions = array( $selectOptions );
740 }
741 list( $startOpts, $useIndex, $tailOpts ) = $this->makeSelectOptions( $selectOptions );
742 if ( is_array( $srcTable ) ) {
743 $srcTable = implode( ',', array_map( array( &$this, 'tableName' ), $srcTable ) );
744 } else {
745 $srcTable = $this->tableName( $srcTable );
746 }
747
748 if ( ( $sequenceData = $this->getSequenceData( $destTable ) ) !== false &&
749 !isset( $varMap[$sequenceData['column']] )
750 ) {
751 $varMap[$sequenceData['column']] = 'GET_SEQUENCE_VALUE(\'' . $sequenceData['sequence'] . '\')';
752 }
753
754 // count-alias subselect fields to avoid abigious definition errors
755 $i = 0;
756 foreach ( $varMap as &$val ) {
757 $val = $val . ' field' . ( $i++ );
758 }
759
760 $sql = "INSERT INTO $destTable (" . implode( ',', array_keys( $varMap ) ) . ')' .
761 " SELECT $startOpts " . implode( ',', $varMap ) .
762 " FROM $srcTable $useIndex ";
763 if ( $conds != '*' ) {
764 $sql .= ' WHERE ' . $this->makeList( $conds, LIST_AND );
765 }
766 $sql .= " $tailOpts";
767
768 if ( in_array( 'IGNORE', $insertOptions ) ) {
769 $this->ignoreDupValOnIndex = true;
770 }
771
772 $retval = $this->query( $sql, $fname );
773
774 if ( in_array( 'IGNORE', $insertOptions ) ) {
775 $this->ignoreDupValOnIndex = false;
776 }
777
778 return $retval;
779 }
780
781 public function upsert( $table, array $rows, array $uniqueIndexes, array $set,
782 $fname = __METHOD__
783 ) {
784 if ( !count( $rows ) ) {
785 return true; // nothing to do
786 }
787
788 if ( !is_array( reset( $rows ) ) ) {
789 $rows = array( $rows );
790 }
791
792 $sequenceData = $this->getSequenceData( $table );
793 if ( $sequenceData !== false ) {
794 // add sequence column to each list of columns, when not set
795 foreach ( $rows as &$row ) {
796 if ( !isset( $row[$sequenceData['column']] ) ) {
797 $row[$sequenceData['column']] = $this->addIdentifierQuotes('GET_SEQUENCE_VALUE(\'' . $sequenceData['sequence'] . '\')');
798 }
799 }
800 }
801
802 return parent::upsert( $table, $rows, $uniqueIndexes, $set, $fname );
803 }
804
805 function tableName( $name, $format = 'quoted' ) {
806 /*
807 Replace reserved words with better ones
808 Using uppercase because that's the only way Oracle can handle
809 quoted tablenames
810 */
811 switch ( $name ) {
812 case 'user':
813 $name = 'MWUSER';
814 break;
815 case 'text':
816 $name = 'PAGECONTENT';
817 break;
818 }
819
820 return strtoupper( parent::tableName( $name, $format ) );
821 }
822
823 function tableNameInternal( $name ) {
824 $name = $this->tableName( $name );
825
826 return preg_replace( '/.*\.(.*)/', '$1', $name );
827 }
828
829 /**
830 * Return the next in a sequence, save the value for retrieval via insertId()
831 *
832 * @param string $seqName
833 * @return null|int
834 */
835 function nextSequenceValue( $seqName ) {
836 $res = $this->query( "SELECT $seqName.nextval FROM dual" );
837 $row = $this->fetchRow( $res );
838 $this->mInsertId = $row[0];
839
840 return $this->mInsertId;
841 }
842
843 /**
844 * Return sequence_name if table has a sequence
845 *
846 * @param string $table
847 * @return bool
848 */
849 private function getSequenceData( $table ) {
850 if ( $this->sequenceData == null ) {
851 $result = $this->doQuery( "SELECT lower(asq.sequence_name),
852 lower(atc.table_name),
853 lower(atc.column_name)
854 FROM all_sequences asq, all_tab_columns atc
855 WHERE decode(
856 atc.table_name,
857 '{$this->mTablePrefix}MWUSER',
858 '{$this->mTablePrefix}USER',
859 atc.table_name
860 ) || '_' ||
861 atc.column_name || '_SEQ' = '{$this->mTablePrefix}' || asq.sequence_name
862 AND asq.sequence_owner = upper('{$this->mDBname}')
863 AND atc.owner = upper('{$this->mDBname}')" );
864
865 while ( ( $row = $result->fetchRow() ) !== false ) {
866 $this->sequenceData[$row[1]] = array(
867 'sequence' => $row[0],
868 'column' => $row[2]
869 );
870 }
871 }
872 $table = strtolower( $this->removeIdentifierQuotes( $this->tableName( $table ) ) );
873
874 return ( isset( $this->sequenceData[$table] ) ) ? $this->sequenceData[$table] : false;
875 }
876
877 /**
878 * Returns the size of a text field, or -1 for "unlimited"
879 *
880 * @param string $table
881 * @param string $field
882 * @return mixed
883 */
884 function textFieldSize( $table, $field ) {
885 $fieldInfoData = $this->fieldInfo( $table, $field );
886
887 return $fieldInfoData->maxLength();
888 }
889
890 function limitResult( $sql, $limit, $offset = false ) {
891 if ( $offset === false ) {
892 $offset = 0;
893 }
894
895 return "SELECT * FROM ($sql) WHERE rownum >= (1 + $offset) AND rownum < (1 + $limit + $offset)";
896 }
897
898 function encodeBlob( $b ) {
899 return new Blob( $b );
900 }
901
902 function decodeBlob( $b ) {
903 if ( $b instanceof Blob ) {
904 $b = $b->fetch();
905 }
906
907 return $b;
908 }
909
910 function unionQueries( $sqls, $all ) {
911 $glue = ' UNION ALL ';
912
913 return 'SELECT * ' . ( $all ? '' : '/* UNION_UNIQUE */ ' ) .
914 'FROM (' . implode( $glue, $sqls ) . ')';
915 }
916
917 function wasDeadlock() {
918 return $this->lastErrno() == 'OCI-00060';
919 }
920
921 function duplicateTableStructure( $oldName, $newName, $temporary = false,
922 $fname = __METHOD__
923 ) {
924 $temporary = $temporary ? 'TRUE' : 'FALSE';
925
926 $newName = strtoupper( $newName );
927 $oldName = strtoupper( $oldName );
928
929 $tabName = substr( $newName, strlen( $this->mTablePrefix ) );
930 $oldPrefix = substr( $oldName, 0, strlen( $oldName ) - strlen( $tabName ) );
931 $newPrefix = strtoupper( $this->mTablePrefix );
932
933 return $this->doQuery( "BEGIN DUPLICATE_TABLE( '$tabName', " .
934 "'$oldPrefix', '$newPrefix', $temporary ); END;" );
935 }
936
937 function listTables( $prefix = null, $fname = __METHOD__ ) {
938 $listWhere = '';
939 if ( !empty( $prefix ) ) {
940 $listWhere = ' AND table_name LIKE \'' . strtoupper( $prefix ) . '%\'';
941 }
942
943 $owner = strtoupper( $this->mDBname );
944 $result = $this->doQuery( "SELECT table_name FROM all_tables " .
945 "WHERE owner='$owner' AND table_name NOT LIKE '%!_IDX\$_' ESCAPE '!' $listWhere" );
946
947 // dirty code ... i know
948 $endArray = array();
949 $endArray[] = strtoupper( $prefix . 'MWUSER' );
950 $endArray[] = strtoupper( $prefix . 'PAGE' );
951 $endArray[] = strtoupper( $prefix . 'IMAGE' );
952 $fixedOrderTabs = $endArray;
953 while ( ( $row = $result->fetchRow() ) !== false ) {
954 if ( !in_array( $row['table_name'], $fixedOrderTabs ) ) {
955 $endArray[] = $row['table_name'];
956 }
957 }
958
959 return $endArray;
960 }
961
962 public function dropTable( $tableName, $fName = __METHOD__ ) {
963 $tableName = $this->tableName( $tableName );
964 if ( !$this->tableExists( $tableName ) ) {
965 return false;
966 }
967
968 return $this->doQuery( "DROP TABLE $tableName CASCADE CONSTRAINTS PURGE" );
969 }
970
971 function timestamp( $ts = 0 ) {
972 return wfTimestamp( TS_ORACLE, $ts );
973 }
974
975 /**
976 * Return aggregated value function call
977 *
978 * @param $valuedata
979 * @param string $valuename
980 * @return mixed
981 */
982 public function aggregateValue( $valuedata, $valuename = 'value' ) {
983 return $valuedata;
984 }
985
986 function reportQueryError( $error, $errno, $sql, $fname, $tempIgnore = false ) {
987 # Ignore errors during error handling to avoid infinite
988 # recursion
989 $ignore = $this->ignoreErrors( true );
990 ++$this->mErrorCount;
991
992 if ( $ignore || $tempIgnore ) {
993 wfDebug( "SQL ERROR (ignored): $error\n" );
994 $this->ignoreErrors( $ignore );
995 } else {
996 throw new DBQueryError( $this, $error, $errno, $sql, $fname );
997 }
998 }
999
1000 /**
1001 * @return string wikitext of a link to the server software's web site
1002 */
1003 public function getSoftwareLink() {
1004 return '[{{int:version-db-oracle-url}} Oracle]';
1005 }
1006
1007 /**
1008 * @return string Version information from the database
1009 */
1010 function getServerVersion() {
1011 //better version number, fallback on driver
1012 $rset = $this->doQuery(
1013 'SELECT version FROM product_component_version ' .
1014 'WHERE UPPER(product) LIKE \'ORACLE DATABASE%\''
1015 );
1016 if ( !( $row = $rset->fetchRow() ) ) {
1017 return oci_server_version( $this->mConn );
1018 }
1019
1020 return $row['version'];
1021 }
1022
1023 /**
1024 * Query whether a given index exists
1025 * @param string $table
1026 * @param string $index
1027 * @param string $fname
1028 * @return bool
1029 */
1030 function indexExists( $table, $index, $fname = __METHOD__ ) {
1031 $table = $this->tableName( $table );
1032 $table = strtoupper( $this->removeIdentifierQuotes( $table ) );
1033 $index = strtoupper( $index );
1034 $owner = strtoupper( $this->mDBname );
1035 $SQL = "SELECT 1 FROM all_indexes WHERE owner='$owner' AND index_name='{$table}_{$index}'";
1036 $res = $this->doQuery( $SQL );
1037 if ( $res ) {
1038 $count = $res->numRows();
1039 $res->free();
1040 } else {
1041 $count = 0;
1042 }
1043
1044 return $count != 0;
1045 }
1046
1047 /**
1048 * Query whether a given table exists (in the given schema, or the default mw one if not given)
1049 * @param string $table
1050 * @param string $fname
1051 * @return bool
1052 */
1053 function tableExists( $table, $fname = __METHOD__ ) {
1054 $table = $this->tableName( $table );
1055 $table = $this->addQuotes( strtoupper( $this->removeIdentifierQuotes( $table ) ) );
1056 $owner = $this->addQuotes( strtoupper( $this->mDBname ) );
1057 $SQL = "SELECT 1 FROM all_tables WHERE owner=$owner AND table_name=$table";
1058 $res = $this->doQuery( $SQL );
1059 if ( $res && $res->numRows() > 0 ) {
1060 $exists = true;
1061 } else {
1062 $exists = false;
1063 }
1064
1065 $res->free();
1066
1067 return $exists;
1068 }
1069
1070 /**
1071 * Function translates mysql_fetch_field() functionality on ORACLE.
1072 * Caching is present for reducing query time.
1073 * For internal calls. Use fieldInfo for normal usage.
1074 * Returns false if the field doesn't exist
1075 *
1076 * @param array|string $table
1077 * @param string $field
1078 * @return ORAField|ORAResult
1079 */
1080 private function fieldInfoMulti( $table, $field ) {
1081 $field = strtoupper( $field );
1082 if ( is_array( $table ) ) {
1083 $table = array_map( array( &$this, 'tableNameInternal' ), $table );
1084 $tableWhere = 'IN (';
1085 foreach ( $table as &$singleTable ) {
1086 $singleTable = $this->removeIdentifierQuotes( $singleTable );
1087 if ( isset( $this->mFieldInfoCache["$singleTable.$field"] ) ) {
1088 return $this->mFieldInfoCache["$singleTable.$field"];
1089 }
1090 $tableWhere .= '\'' . $singleTable . '\',';
1091 }
1092 $tableWhere = rtrim( $tableWhere, ',' ) . ')';
1093 } else {
1094 $table = $this->removeIdentifierQuotes( $this->tableNameInternal( $table ) );
1095 if ( isset( $this->mFieldInfoCache["$table.$field"] ) ) {
1096 return $this->mFieldInfoCache["$table.$field"];
1097 }
1098 $tableWhere = '= \'' . $table . '\'';
1099 }
1100
1101 $fieldInfoStmt = oci_parse(
1102 $this->mConn,
1103 'SELECT * FROM wiki_field_info_full WHERE table_name ' .
1104 $tableWhere . ' and column_name = \'' . $field . '\''
1105 );
1106 if ( oci_execute( $fieldInfoStmt, $this->execFlags() ) === false ) {
1107 $e = oci_error( $fieldInfoStmt );
1108 $this->reportQueryError( $e['message'], $e['code'], 'fieldInfo QUERY', __METHOD__ );
1109
1110 return false;
1111 }
1112 $res = new ORAResult( $this, $fieldInfoStmt );
1113 if ( $res->numRows() == 0 ) {
1114 if ( is_array( $table ) ) {
1115 foreach ( $table as &$singleTable ) {
1116 $this->mFieldInfoCache["$singleTable.$field"] = false;
1117 }
1118 } else {
1119 $this->mFieldInfoCache["$table.$field"] = false;
1120 }
1121 $fieldInfoTemp = null;
1122 } else {
1123 $fieldInfoTemp = new ORAField( $res->fetchRow() );
1124 $table = $fieldInfoTemp->tableName();
1125 $this->mFieldInfoCache["$table.$field"] = $fieldInfoTemp;
1126 }
1127 $res->free();
1128
1129 return $fieldInfoTemp;
1130 }
1131
1132 /**
1133 * @throws DBUnexpectedError
1134 * @param string $table
1135 * @param string $field
1136 * @return ORAField
1137 */
1138 function fieldInfo( $table, $field ) {
1139 if ( is_array( $table ) ) {
1140 throw new DBUnexpectedError( $this, 'DatabaseOracle::fieldInfo called with table array!' );
1141 }
1142
1143 return $this->fieldInfoMulti( $table, $field );
1144 }
1145
1146 protected function doBegin( $fname = __METHOD__ ) {
1147 $this->mTrxLevel = 1;
1148 $this->doQuery( 'SET CONSTRAINTS ALL DEFERRED' );
1149 }
1150
1151 protected function doCommit( $fname = __METHOD__ ) {
1152 if ( $this->mTrxLevel ) {
1153 $ret = oci_commit( $this->mConn );
1154 if ( !$ret ) {
1155 throw new DBUnexpectedError( $this, $this->lastError() );
1156 }
1157 $this->mTrxLevel = 0;
1158 $this->doQuery( 'SET CONSTRAINTS ALL IMMEDIATE' );
1159 }
1160 }
1161
1162 protected function doRollback( $fname = __METHOD__ ) {
1163 if ( $this->mTrxLevel ) {
1164 oci_rollback( $this->mConn );
1165 $this->mTrxLevel = 0;
1166 $this->doQuery( 'SET CONSTRAINTS ALL IMMEDIATE' );
1167 }
1168 }
1169
1170 /**
1171 * defines must comply with ^define\s*([^\s=]*)\s*=\s?'\{\$([^\}]*)\}';
1172 *
1173 * @param resource $fp
1174 * @param bool|string $lineCallback
1175 * @param bool|callable $resultCallback
1176 * @param string $fname
1177 * @param bool|callable $inputCallback
1178 * @return bool|string
1179 */
1180 function sourceStream( $fp, $lineCallback = false, $resultCallback = false,
1181 $fname = __METHOD__, $inputCallback = false ) {
1182 $cmd = '';
1183 $done = false;
1184 $dollarquote = false;
1185
1186 $replacements = array();
1187
1188 while ( !feof( $fp ) ) {
1189 if ( $lineCallback ) {
1190 call_user_func( $lineCallback );
1191 }
1192 $line = trim( fgets( $fp, 1024 ) );
1193 $sl = strlen( $line ) - 1;
1194
1195 if ( $sl < 0 ) {
1196 continue;
1197 }
1198 if ( '-' == $line[0] && '-' == $line[1] ) {
1199 continue;
1200 }
1201
1202 // Allow dollar quoting for function declarations
1203 if ( substr( $line, 0, 8 ) == '/*$mw$*/' ) {
1204 if ( $dollarquote ) {
1205 $dollarquote = false;
1206 $line = str_replace( '/*$mw$*/', '', $line ); // remove dollarquotes
1207 $done = true;
1208 } else {
1209 $dollarquote = true;
1210 }
1211 } elseif ( !$dollarquote ) {
1212 if ( ';' == $line[$sl] && ( $sl < 2 || ';' != $line[$sl - 1] ) ) {
1213 $done = true;
1214 $line = substr( $line, 0, $sl );
1215 }
1216 }
1217
1218 if ( $cmd != '' ) {
1219 $cmd .= ' ';
1220 }
1221 $cmd .= "$line\n";
1222
1223 if ( $done ) {
1224 $cmd = str_replace( ';;', ";", $cmd );
1225 if ( strtolower( substr( $cmd, 0, 6 ) ) == 'define' ) {
1226 if ( preg_match( '/^define\s*([^\s=]*)\s*=\s*\'\{\$([^\}]*)\}\'/', $cmd, $defines ) ) {
1227 $replacements[$defines[2]] = $defines[1];
1228 }
1229 } else {
1230 foreach ( $replacements as $mwVar => $scVar ) {
1231 $cmd = str_replace( '&' . $scVar . '.', '`{$' . $mwVar . '}`', $cmd );
1232 }
1233
1234 $cmd = $this->replaceVars( $cmd );
1235 if ( $inputCallback ) {
1236 call_user_func( $inputCallback, $cmd );
1237 }
1238 $res = $this->doQuery( $cmd );
1239 if ( $resultCallback ) {
1240 call_user_func( $resultCallback, $res, $this );
1241 }
1242
1243 if ( false === $res ) {
1244 $err = $this->lastError();
1245
1246 return "Query \"{$cmd}\" failed with error code \"$err\".\n";
1247 }
1248 }
1249
1250 $cmd = '';
1251 $done = false;
1252 }
1253 }
1254
1255 return true;
1256 }
1257
1258 function selectDB( $db ) {
1259 $this->mDBname = $db;
1260 if ( $db == null || $db == $this->mUser ) {
1261 return true;
1262 }
1263 $sql = 'ALTER SESSION SET CURRENT_SCHEMA=' . strtoupper( $db );
1264 $stmt = oci_parse( $this->mConn, $sql );
1265 wfSuppressWarnings();
1266 $success = oci_execute( $stmt );
1267 wfRestoreWarnings();
1268 if ( !$success ) {
1269 $e = oci_error( $stmt );
1270 if ( $e['code'] != '1435' ) {
1271 $this->reportQueryError( $e['message'], $e['code'], $sql, __METHOD__ );
1272 }
1273
1274 return false;
1275 }
1276
1277 return true;
1278 }
1279
1280 function strencode( $s ) {
1281 return str_replace( "'", "''", $s );
1282 }
1283
1284 function addQuotes( $s ) {
1285 global $wgContLang;
1286 if ( isset( $wgContLang->mLoaded ) && $wgContLang->mLoaded ) {
1287 $s = $wgContLang->checkTitleEncoding( $s );
1288 }
1289
1290 return "'" . $this->strencode( $s ) . "'";
1291 }
1292
1293 public function addIdentifierQuotes( $s ) {
1294 if ( !$this->getFlag( DBO_DDLMODE ) ) {
1295 $s = '/*Q*/' . $s;
1296 }
1297
1298 return $s;
1299 }
1300
1301 public function removeIdentifierQuotes( $s ) {
1302 return strpos( $s, '/*Q*/' ) === false ? $s : substr( $s, 5 );
1303 }
1304
1305 public function isQuotedIdentifier( $s ) {
1306 return strpos( $s, '/*Q*/' ) !== false;
1307 }
1308
1309 private function wrapFieldForWhere( $table, &$col, &$val ) {
1310 global $wgContLang;
1311
1312 $col_info = $this->fieldInfoMulti( $table, $col );
1313 $col_type = $col_info != false ? $col_info->type() : 'CONSTANT';
1314 if ( $col_type == 'CLOB' ) {
1315 $col = 'TO_CHAR(' . $col . ')';
1316 $val = $wgContLang->checkTitleEncoding( $val );
1317 } elseif ( $col_type == 'VARCHAR2' ) {
1318 $val = $wgContLang->checkTitleEncoding( $val );
1319 }
1320 }
1321
1322 private function wrapConditionsForWhere( $table, $conds, $parentCol = null ) {
1323 $conds2 = array();
1324 foreach ( $conds as $col => $val ) {
1325 if ( is_array( $val ) ) {
1326 $conds2[$col] = $this->wrapConditionsForWhere( $table, $val, $col );
1327 } else {
1328 if ( is_numeric( $col ) && $parentCol != null ) {
1329 $this->wrapFieldForWhere( $table, $parentCol, $val );
1330 } else {
1331 $this->wrapFieldForWhere( $table, $col, $val );
1332 }
1333 $conds2[$col] = $val;
1334 }
1335 }
1336
1337 return $conds2;
1338 }
1339
1340 function selectRow( $table, $vars, $conds, $fname = __METHOD__,
1341 $options = array(), $join_conds = array()
1342 ) {
1343 if ( is_array( $conds ) ) {
1344 $conds = $this->wrapConditionsForWhere( $table, $conds );
1345 }
1346
1347 return parent::selectRow( $table, $vars, $conds, $fname, $options, $join_conds );
1348 }
1349
1350 /**
1351 * Returns an optional USE INDEX clause to go after the table, and a
1352 * string to go at the end of the query
1353 *
1354 * @param array $options An associative array of options to be turned into
1355 * an SQL query, valid keys are listed in the function.
1356 * @return array
1357 */
1358 function makeSelectOptions( $options ) {
1359 $preLimitTail = $postLimitTail = '';
1360 $startOpts = '';
1361
1362 $noKeyOptions = array();
1363 foreach ( $options as $key => $option ) {
1364 if ( is_numeric( $key ) ) {
1365 $noKeyOptions[$option] = true;
1366 }
1367 }
1368
1369 $preLimitTail .= $this->makeGroupByWithHaving( $options );
1370
1371 $preLimitTail .= $this->makeOrderBy( $options );
1372
1373 if ( isset( $noKeyOptions['FOR UPDATE'] ) ) {
1374 $postLimitTail .= ' FOR UPDATE';
1375 }
1376
1377 if ( isset( $noKeyOptions['DISTINCT'] ) || isset( $noKeyOptions['DISTINCTROW'] ) ) {
1378 $startOpts .= 'DISTINCT';
1379 }
1380
1381 if ( isset( $options['USE INDEX'] ) && !is_array( $options['USE INDEX'] ) ) {
1382 $useIndex = $this->useIndexClause( $options['USE INDEX'] );
1383 } else {
1384 $useIndex = '';
1385 }
1386
1387 return array( $startOpts, $useIndex, $preLimitTail, $postLimitTail );
1388 }
1389
1390 public function delete( $table, $conds, $fname = __METHOD__ ) {
1391 if ( is_array( $conds ) ) {
1392 $conds = $this->wrapConditionsForWhere( $table, $conds );
1393 }
1394 // a hack for deleting pages, users and images (which have non-nullable FKs)
1395 // all deletions on these tables have transactions so final failure rollbacks these updates
1396 $table = $this->tableName( $table );
1397 if ( $table == $this->tableName( 'user' ) ) {
1398 $this->update( 'archive', array( 'ar_user' => 0 ),
1399 array( 'ar_user' => $conds['user_id'] ), $fname );
1400 $this->update( 'ipblocks', array( 'ipb_user' => 0 ),
1401 array( 'ipb_user' => $conds['user_id'] ), $fname );
1402 $this->update( 'image', array( 'img_user' => 0 ),
1403 array( 'img_user' => $conds['user_id'] ), $fname );
1404 $this->update( 'oldimage', array( 'oi_user' => 0 ),
1405 array( 'oi_user' => $conds['user_id'] ), $fname );
1406 $this->update( 'filearchive', array( 'fa_deleted_user' => 0 ),
1407 array( 'fa_deleted_user' => $conds['user_id'] ), $fname );
1408 $this->update( 'filearchive', array( 'fa_user' => 0 ),
1409 array( 'fa_user' => $conds['user_id'] ), $fname );
1410 $this->update( 'uploadstash', array( 'us_user' => 0 ),
1411 array( 'us_user' => $conds['user_id'] ), $fname );
1412 $this->update( 'recentchanges', array( 'rc_user' => 0 ),
1413 array( 'rc_user' => $conds['user_id'] ), $fname );
1414 $this->update( 'logging', array( 'log_user' => 0 ),
1415 array( 'log_user' => $conds['user_id'] ), $fname );
1416 } elseif ( $table == $this->tableName( 'image' ) ) {
1417 $this->update( 'oldimage', array( 'oi_name' => 0 ),
1418 array( 'oi_name' => $conds['img_name'] ), $fname );
1419 }
1420
1421 return parent::delete( $table, $conds, $fname );
1422 }
1423
1424 /**
1425 * @param string $table
1426 * @param array $values
1427 * @param array $conds
1428 * @param string $fname
1429 * @param array $options
1430 * @return bool
1431 * @throws DBUnexpectedError
1432 */
1433 function update( $table, $values, $conds, $fname = __METHOD__, $options = array() ) {
1434 global $wgContLang;
1435
1436 $table = $this->tableName( $table );
1437 $opts = $this->makeUpdateOptions( $options );
1438 $sql = "UPDATE $opts $table SET ";
1439
1440 $first = true;
1441 foreach ( $values as $col => &$val ) {
1442 $sqlSet = $this->fieldBindStatement( $table, $col, $val, true );
1443
1444 if ( !$first ) {
1445 $sqlSet = ', ' . $sqlSet;
1446 } else {
1447 $first = false;
1448 }
1449 $sql .= $sqlSet;
1450 }
1451
1452 if ( $conds !== array() && $conds !== '*' ) {
1453 $conds = $this->wrapConditionsForWhere( $table, $conds );
1454 $sql .= ' WHERE ' . $this->makeList( $conds, LIST_AND );
1455 }
1456
1457 if ( ( $this->mLastResult = $stmt = oci_parse( $this->mConn, $sql ) ) === false ) {
1458 $e = oci_error( $this->mConn );
1459 $this->reportQueryError( $e['message'], $e['code'], $sql, __METHOD__ );
1460
1461 return false;
1462 }
1463 foreach ( $values as $col => &$val ) {
1464 $col_info = $this->fieldInfoMulti( $table, $col );
1465 $col_type = $col_info != false ? $col_info->type() : 'CONSTANT';
1466
1467 if ( $val === null ) {
1468 // do nothing ... null was inserted in statement creation
1469 } elseif ( $col_type != 'BLOB' && $col_type != 'CLOB' ) {
1470 if ( is_object( $val ) ) {
1471 $val = $val->getData();
1472 }
1473
1474 if ( preg_match( '/^timestamp.*/i', $col_type ) == 1 && strtolower( $val ) == 'infinity' ) {
1475 $val = '31-12-2030 12:00:00.000000';
1476 }
1477
1478 $val = ( $wgContLang != null ) ? $wgContLang->checkTitleEncoding( $val ) : $val;
1479 if ( oci_bind_by_name( $stmt, ":$col", $val ) === false ) {
1480 $e = oci_error( $stmt );
1481 $this->reportQueryError( $e['message'], $e['code'], $sql, __METHOD__ );
1482
1483 return false;
1484 }
1485 } else {
1486 /** @var OCI_Lob[] $lob */
1487 if ( ( $lob[$col] = oci_new_descriptor( $this->mConn, OCI_D_LOB ) ) === false ) {
1488 $e = oci_error( $stmt );
1489 throw new DBUnexpectedError( $this, "Cannot create LOB descriptor: " . $e['message'] );
1490 }
1491
1492 if ( $col_type == 'BLOB' ) {
1493 $lob[$col]->writeTemporary( $val );
1494 oci_bind_by_name( $stmt, ":$col", $lob[$col], -1, SQLT_BLOB );
1495 } else {
1496 $lob[$col]->writeTemporary( $val );
1497 oci_bind_by_name( $stmt, ":$col", $lob[$col], -1, OCI_B_CLOB );
1498 }
1499 }
1500 }
1501
1502 wfSuppressWarnings();
1503
1504 if ( oci_execute( $stmt, $this->execFlags() ) === false ) {
1505 $e = oci_error( $stmt );
1506 if ( !$this->ignoreDupValOnIndex || $e['code'] != '1' ) {
1507 $this->reportQueryError( $e['message'], $e['code'], $sql, __METHOD__ );
1508
1509 return false;
1510 } else {
1511 $this->mAffectedRows = oci_num_rows( $stmt );
1512 }
1513 } else {
1514 $this->mAffectedRows = oci_num_rows( $stmt );
1515 }
1516
1517 wfRestoreWarnings();
1518
1519 if ( isset( $lob ) ) {
1520 foreach ( $lob as $lob_v ) {
1521 $lob_v->free();
1522 }
1523 }
1524
1525 if ( !$this->mTrxLevel ) {
1526 oci_commit( $this->mConn );
1527 }
1528
1529 return oci_free_statement( $stmt );
1530 }
1531
1532 function bitNot( $field ) {
1533 // expecting bit-fields smaller than 4bytes
1534 return 'BITNOT(' . $field . ')';
1535 }
1536
1537 function bitAnd( $fieldLeft, $fieldRight ) {
1538 return 'BITAND(' . $fieldLeft . ', ' . $fieldRight . ')';
1539 }
1540
1541 function bitOr( $fieldLeft, $fieldRight ) {
1542 return 'BITOR(' . $fieldLeft . ', ' . $fieldRight . ')';
1543 }
1544
1545 function getDBname() {
1546 return $this->mDBname;
1547 }
1548
1549 function getServer() {
1550 return $this->mServer;
1551 }
1552
1553 public function buildGroupConcatField(
1554 $delim, $table, $field, $conds = '', $join_conds = array()
1555 ) {
1556 $fld = "LISTAGG($field," . $this->addQuotes( $delim ) . ") WITHIN GROUP (ORDER BY $field)";
1557
1558 return '(' . $this->selectSQLText( $table, $fld, $conds, null, array(), $join_conds ) . ')';
1559 }
1560
1561 public function getSearchEngine() {
1562 return 'SearchOracle';
1563 }
1564
1565 public function getInfinity() {
1566 return '31-12-2030 12:00:00.000000';
1567 }
1568 }