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