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