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