Localisation updates from https://translatewiki.net.
[mediawiki.git] / includes / db / DatabaseOracle.php
bloba3d7c1b9e1c5832a2176200350905cae1734d02a
1 <?php
2 /**
3 * This is the Oracle database abstraction layer.
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.
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.
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
20 * @file
21 * @ingroup Database
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
30 class ORAResult {
31 private $rows;
32 private $cursor;
33 private $nrows;
35 private $columns = array();
37 private function array_unique_md( $array_in ) {
38 $array_out = array();
39 $array_hashes = array();
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;
49 return $array_out;
52 /**
53 * @param DatabaseBase $db
54 * @param resource $stmt A valid OCI statement identifier
55 * @param bool $unique
57 function __construct( &$db, $stmt, $unique = false ) {
58 $this->db =& $db;
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();
66 return;
69 if ( $unique ) {
70 $this->rows = $this->array_unique_md( $this->rows );
71 $this->nrows = count( $this->rows );
74 if ( $this->nrows > 0 ) {
75 foreach ( $this->rows[0] as $k => $v ) {
76 $this->columns[$k] = strtolower( oci_field_name( $stmt, $k + 1 ) );
80 $this->cursor = 0;
81 oci_free_statement( $stmt );
84 public function free() {
85 unset( $this->db );
88 public function seek( $row ) {
89 $this->cursor = min( $row, $this->nrows );
92 public function numRows() {
93 return $this->nrows;
96 public function numFields() {
97 return count( $this->columns );
100 public function fetchObject() {
101 if ( $this->cursor >= $this->nrows ) {
102 return false;
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;
111 return $ret;
114 public function fetchRow() {
115 if ( $this->cursor >= $this->nrows ) {
116 return false;
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;
127 return $ret;
132 * Utility class.
133 * @ingroup Database
135 class ORAField implements Field {
136 private $name, $tablename, $default, $max_length, $nullable,
137 $is_pk, $is_unique, $is_multiple, $is_key, $type;
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'];
152 function name() {
153 return $this->name;
156 function tableName() {
157 return $this->tablename;
160 function defaultValue() {
161 return $this->default;
164 function maxLength() {
165 return $this->max_length;
168 function isNullable() {
169 return $this->nullable;
172 function isKey() {
173 return $this->is_key;
176 function isMultipleKey() {
177 return $this->is_multiple;
180 function type() {
181 return $this->type;
186 * @ingroup Database
188 class DatabaseOracle extends Database {
189 /** @var resource */
190 protected $mLastResult = null;
192 /** @var int The number of rows affected as an integer */
193 protected $mAffectedRows;
195 /** @var int */
196 private $mInsertId = null;
198 /** @var bool */
199 private $ignoreDupValOnIndex = false;
201 /** @var bool|array */
202 private $sequenceData = null;
204 /** @var string Character set for Oracle database */
205 private $defaultCharset = 'AL32UTF8';
207 /** @var array */
208 private $mFieldInfoCache = array();
210 function __construct( array $p ) {
211 global $wgDBprefix;
213 if ( $p['tablePrefix'] == 'get from global' ) {
214 $p['tablePrefix'] = $wgDBprefix;
216 $p['tablePrefix'] = strtoupper( $p['tablePrefix'] );
217 parent::__construct( $p );
218 Hooks::run( 'DatabaseOraclePostInit', array( $this ) );
221 function __destruct() {
222 if ( $this->mOpened ) {
223 MediaWiki\suppressWarnings();
224 $this->close();
225 MediaWiki\restoreWarnings();
229 function getType() {
230 return 'oracle';
233 function cascadingDeletes() {
234 return true;
237 function cleanupTriggers() {
238 return true;
241 function strictIPs() {
242 return true;
245 function realTimestamps() {
246 return true;
249 function implicitGroupby() {
250 return false;
253 function implicitOrderby() {
254 return false;
257 function searchableIPs() {
258 return true;
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
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" );
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;
299 if ( !strlen( $user ) ) { # e.g. the class is being loaded
300 return null;
303 if ( $wgDBOracleDRCP ) {
304 $this->setFlag( DBO_PERSISTENT );
307 $session_mode = $this->mFlags & DBO_SYSDBA ? OCI_SYSDBA : OCI_DEFAULT;
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
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
326 } else {
327 $this->mConn = oci_connect(
328 $this->mUser,
329 $this->mPassword,
330 $this->mServer,
331 $this->defaultCharset,
332 $session_mode
335 MediaWiki\restoreWarnings();
337 if ( $this->mUser != $this->mDBname ) {
338 // change current schema in session
339 $this->selectDB( $this->mDBname );
342 if ( !$this->mConn ) {
343 throw new DBConnectionError( $this, $this->lastError() );
346 $this->mOpened = true;
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=\'.,\'' );
353 return $this->mConn;
357 * Closes a database connection, if it is open
358 * Returns success, true if already closed
359 * @return bool
361 protected function closeConnection() {
362 return oci_close( $this->mConn );
365 function execFlags() {
366 return $this->mTrxLevel ? OCI_NO_AUTO_COMMIT : OCI_COMMIT_ON_SUCCESS;
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" );
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 );
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' );
388 $sql = preg_replace(
389 '/^EXPLAIN /',
390 'EXPLAIN PLAN SET STATEMENT_ID = \'' . $explain_id . '\' FOR',
391 $sql,
393 $explain_count
396 MediaWiki\suppressWarnings();
398 $this->mLastResult = $stmt = oci_parse( $this->mConn, $sql );
399 if ( $stmt === false ) {
400 $e = oci_error( $this->mConn );
401 $this->reportQueryError( $e['message'], $e['code'], $sql, __METHOD__ );
403 return false;
406 if ( !oci_execute( $stmt, $this->execFlags() ) ) {
407 $e = oci_error( $stmt );
408 if ( !$this->ignoreDupValOnIndex || $e['code'] != '1' ) {
409 $this->reportQueryError( $e['message'], $e['code'], $sql, __METHOD__ );
411 return false;
415 MediaWiki\restoreWarnings();
417 if ( $explain_count > 0 ) {
418 return $this->doQuery( 'SELECT id, cardinality "ROWS" FROM plan_table ' .
419 'WHERE statement_id = \'' . $explain_id . '\'' );
420 } elseif ( oci_statement_type( $stmt ) == 'SELECT' ) {
421 return new ORAResult( $this, $stmt, $union_unique );
422 } else {
423 $this->mAffectedRows = oci_num_rows( $stmt );
425 return true;
429 function queryIgnore( $sql, $fname = '' ) {
430 return $this->query( $sql, $fname, true );
434 * Frees resources associated with the LOB descriptor
435 * @param ResultWrapper|resource $res
437 function freeResult( $res ) {
438 if ( $res instanceof ResultWrapper ) {
439 $res = $res->result;
442 $res->free();
446 * @param ResultWrapper|stdClass $res
447 * @return mixed
449 function fetchObject( $res ) {
450 if ( $res instanceof ResultWrapper ) {
451 $res = $res->result;
454 return $res->fetchObject();
457 function fetchRow( $res ) {
458 if ( $res instanceof ResultWrapper ) {
459 $res = $res->result;
462 return $res->fetchRow();
465 function numRows( $res ) {
466 if ( $res instanceof ResultWrapper ) {
467 $res = $res->result;
470 return $res->numRows();
473 function numFields( $res ) {
474 if ( $res instanceof ResultWrapper ) {
475 $res = $res->result;
478 return $res->numFields();
481 function fieldName( $stmt, $n ) {
482 return oci_field_name( $stmt, $n );
486 * This must be called after nextSequenceVal
487 * @return null|int
489 function insertId() {
490 return $this->mInsertId;
494 * @param mixed $res
495 * @param int $row
497 function dataSeek( $res, $row ) {
498 if ( $res instanceof ORAResult ) {
499 $res->seek( $row );
500 } else {
501 $res->result->seek( $row );
505 function lastError() {
506 if ( $this->mConn === false ) {
507 $e = oci_error();
508 } else {
509 $e = oci_error( $this->mConn );
512 return $e['message'];
515 function lastErrno() {
516 if ( $this->mConn === false ) {
517 $e = oci_error();
518 } else {
519 $e = oci_error( $this->mConn );
522 return $e['code'];
525 function affectedRows() {
526 return $this->mAffectedRows;
530 * Returns information about an index
531 * If errors are explicitly ignored, returns NULL on failure
532 * @param string $table
533 * @param string $index
534 * @param string $fname
535 * @return bool
537 function indexInfo( $table, $index, $fname = __METHOD__ ) {
538 return false;
541 function indexUnique( $table, $index, $fname = __METHOD__ ) {
542 return false;
545 function insert( $table, $a, $fname = __METHOD__, $options = array() ) {
546 if ( !count( $a ) ) {
547 return true;
550 if ( !is_array( $options ) ) {
551 $options = array( $options );
554 if ( in_array( 'IGNORE', $options ) ) {
555 $this->ignoreDupValOnIndex = true;
558 if ( !is_array( reset( $a ) ) ) {
559 $a = array( $a );
562 foreach ( $a as &$row ) {
563 $this->insertOneRow( $table, $row, $fname );
565 $retVal = true;
567 if ( in_array( 'IGNORE', $options ) ) {
568 $this->ignoreDupValOnIndex = false;
571 return $retVal;
574 private function fieldBindStatement( $table, $col, &$val, $includeCol = false ) {
575 $col_info = $this->fieldInfoMulti( $table, $col );
576 $col_type = $col_info != false ? $col_info->type() : 'CONSTANT';
578 $bind = '';
579 if ( is_numeric( $col ) ) {
580 $bind = $val;
581 $val = null;
583 return $bind;
584 } elseif ( $includeCol ) {
585 $bind = "$col = ";
588 if ( $val == '' && $val !== 0 && $col_type != 'BLOB' && $col_type != 'CLOB' ) {
589 $val = null;
592 if ( $val === 'NULL' ) {
593 $val = null;
596 if ( $val === null ) {
597 if ( $col_info != false && $col_info->isNullable() == 0 && $col_info->defaultValue() != null ) {
598 $bind .= 'DEFAULT';
599 } else {
600 $bind .= 'NULL';
602 } else {
603 $bind .= ':' . $col;
606 return $bind;
610 * @param string $table
611 * @param array $row
612 * @param string $fname
613 * @return bool
614 * @throws DBUnexpectedError
616 private function insertOneRow( $table, $row, $fname ) {
617 global $wgContLang;
619 $table = $this->tableName( $table );
620 // "INSERT INTO tables (a, b, c)"
621 $sql = "INSERT INTO " . $table . " (" . join( ',', array_keys( $row ) ) . ')';
622 $sql .= " VALUES (";
624 // for each value, append ":key"
625 $first = true;
626 foreach ( $row as $col => &$val ) {
627 if ( !$first ) {
628 $sql .= ', ';
629 } else {
630 $first = false;
632 if ( $this->isQuotedIdentifier( $val ) ) {
633 $sql .= $this->removeIdentifierQuotes( $val );
634 unset( $row[$col] );
635 } else {
636 $sql .= $this->fieldBindStatement( $table, $col, $val );
639 $sql .= ')';
641 $this->mLastResult = $stmt = oci_parse( $this->mConn, $sql );
642 if ( $stmt === false ) {
643 $e = oci_error( $this->mConn );
644 $this->reportQueryError( $e['message'], $e['code'], $sql, __METHOD__ );
646 return false;
648 foreach ( $row as $col => &$val ) {
649 $col_info = $this->fieldInfoMulti( $table, $col );
650 $col_type = $col_info != false ? $col_info->type() : 'CONSTANT';
652 if ( $val === null ) {
653 // do nothing ... null was inserted in statement creation
654 } elseif ( $col_type != 'BLOB' && $col_type != 'CLOB' ) {
655 if ( is_object( $val ) ) {
656 $val = $val->fetch();
659 // backward compatibility
660 if ( preg_match( '/^timestamp.*/i', $col_type ) == 1 && strtolower( $val ) == 'infinity' ) {
661 $val = $this->getInfinity();
664 $val = ( $wgContLang != null ) ? $wgContLang->checkTitleEncoding( $val ) : $val;
665 if ( oci_bind_by_name( $stmt, ":$col", $val, -1, SQLT_CHR ) === false ) {
666 $e = oci_error( $stmt );
667 $this->reportQueryError( $e['message'], $e['code'], $sql, __METHOD__ );
669 return false;
671 } else {
672 /** @var OCI_Lob[] $lob */
673 $lob[$col] = oci_new_descriptor( $this->mConn, OCI_D_LOB );
674 if ( $lob[$col] === false ) {
675 $e = oci_error( $stmt );
676 throw new DBUnexpectedError( $this, "Cannot create LOB descriptor: " . $e['message'] );
679 if ( is_object( $val ) ) {
680 $val = $val->fetch();
683 if ( $col_type == 'BLOB' ) {
684 $lob[$col]->writeTemporary( $val, OCI_TEMP_BLOB );
685 oci_bind_by_name( $stmt, ":$col", $lob[$col], -1, OCI_B_BLOB );
686 } else {
687 $lob[$col]->writeTemporary( $val, OCI_TEMP_CLOB );
688 oci_bind_by_name( $stmt, ":$col", $lob[$col], -1, OCI_B_CLOB );
693 MediaWiki\suppressWarnings();
695 if ( oci_execute( $stmt, $this->execFlags() ) === false ) {
696 $e = oci_error( $stmt );
697 if ( !$this->ignoreDupValOnIndex || $e['code'] != '1' ) {
698 $this->reportQueryError( $e['message'], $e['code'], $sql, __METHOD__ );
700 return false;
701 } else {
702 $this->mAffectedRows = oci_num_rows( $stmt );
704 } else {
705 $this->mAffectedRows = oci_num_rows( $stmt );
708 MediaWiki\restoreWarnings();
710 if ( isset( $lob ) ) {
711 foreach ( $lob as $lob_v ) {
712 $lob_v->free();
716 if ( !$this->mTrxLevel ) {
717 oci_commit( $this->mConn );
720 return oci_free_statement( $stmt );
723 function insertSelect( $destTable, $srcTable, $varMap, $conds, $fname = __METHOD__,
724 $insertOptions = array(), $selectOptions = array()
726 $destTable = $this->tableName( $destTable );
727 if ( !is_array( $selectOptions ) ) {
728 $selectOptions = array( $selectOptions );
730 list( $startOpts, $useIndex, $tailOpts ) = $this->makeSelectOptions( $selectOptions );
731 if ( is_array( $srcTable ) ) {
732 $srcTable = implode( ',', array_map( array( &$this, 'tableName' ), $srcTable ) );
733 } else {
734 $srcTable = $this->tableName( $srcTable );
737 $sequenceData = $this->getSequenceData( $destTable );
738 if ( $sequenceData !== false &&
739 !isset( $varMap[$sequenceData['column']] )
741 $varMap[$sequenceData['column']] = 'GET_SEQUENCE_VALUE(\'' . $sequenceData['sequence'] . '\')';
744 // count-alias subselect fields to avoid abigious definition errors
745 $i = 0;
746 foreach ( $varMap as &$val ) {
747 $val = $val . ' field' . ( $i++ );
750 $sql = "INSERT INTO $destTable (" . implode( ',', array_keys( $varMap ) ) . ')' .
751 " SELECT $startOpts " . implode( ',', $varMap ) .
752 " FROM $srcTable $useIndex ";
753 if ( $conds != '*' ) {
754 $sql .= ' WHERE ' . $this->makeList( $conds, LIST_AND );
756 $sql .= " $tailOpts";
758 if ( in_array( 'IGNORE', $insertOptions ) ) {
759 $this->ignoreDupValOnIndex = true;
762 $retval = $this->query( $sql, $fname );
764 if ( in_array( 'IGNORE', $insertOptions ) ) {
765 $this->ignoreDupValOnIndex = false;
768 return $retval;
771 public function upsert( $table, array $rows, array $uniqueIndexes, array $set,
772 $fname = __METHOD__
774 if ( !count( $rows ) ) {
775 return true; // nothing to do
778 if ( !is_array( reset( $rows ) ) ) {
779 $rows = array( $rows );
782 $sequenceData = $this->getSequenceData( $table );
783 if ( $sequenceData !== false ) {
784 // add sequence column to each list of columns, when not set
785 foreach ( $rows as &$row ) {
786 if ( !isset( $row[$sequenceData['column']] ) ) {
787 $row[$sequenceData['column']] =
788 $this->addIdentifierQuotes( 'GET_SEQUENCE_VALUE(\'' .
789 $sequenceData['sequence'] . '\')' );
794 return parent::upsert( $table, $rows, $uniqueIndexes, $set, $fname );
797 function tableName( $name, $format = 'quoted' ) {
799 Replace reserved words with better ones
800 Using uppercase because that's the only way Oracle can handle
801 quoted tablenames
803 switch ( $name ) {
804 case 'user':
805 $name = 'MWUSER';
806 break;
807 case 'text':
808 $name = 'PAGECONTENT';
809 break;
812 return strtoupper( parent::tableName( $name, $format ) );
815 function tableNameInternal( $name ) {
816 $name = $this->tableName( $name );
818 return preg_replace( '/.*\.(.*)/', '$1', $name );
822 * Return the next in a sequence, save the value for retrieval via insertId()
824 * @param string $seqName
825 * @return null|int
827 function nextSequenceValue( $seqName ) {
828 $res = $this->query( "SELECT $seqName.nextval FROM dual" );
829 $row = $this->fetchRow( $res );
830 $this->mInsertId = $row[0];
832 return $this->mInsertId;
836 * Return sequence_name if table has a sequence
838 * @param string $table
839 * @return bool
841 private function getSequenceData( $table ) {
842 if ( $this->sequenceData == null ) {
843 $result = $this->doQuery( "SELECT lower(asq.sequence_name),
844 lower(atc.table_name),
845 lower(atc.column_name)
846 FROM all_sequences asq, all_tab_columns atc
847 WHERE decode(
848 atc.table_name,
849 '{$this->mTablePrefix}MWUSER',
850 '{$this->mTablePrefix}USER',
851 atc.table_name
852 ) || '_' ||
853 atc.column_name || '_SEQ' = '{$this->mTablePrefix}' || asq.sequence_name
854 AND asq.sequence_owner = upper('{$this->mDBname}')
855 AND atc.owner = upper('{$this->mDBname}')" );
857 while ( ( $row = $result->fetchRow() ) !== false ) {
858 $this->sequenceData[$row[1]] = array(
859 'sequence' => $row[0],
860 'column' => $row[2]
864 $table = strtolower( $this->removeIdentifierQuotes( $this->tableName( $table ) ) );
866 return ( isset( $this->sequenceData[$table] ) ) ? $this->sequenceData[$table] : false;
870 * Returns the size of a text field, or -1 for "unlimited"
872 * @param string $table
873 * @param string $field
874 * @return mixed
876 function textFieldSize( $table, $field ) {
877 $fieldInfoData = $this->fieldInfo( $table, $field );
879 return $fieldInfoData->maxLength();
882 function limitResult( $sql, $limit, $offset = false ) {
883 if ( $offset === false ) {
884 $offset = 0;
887 return "SELECT * FROM ($sql) WHERE rownum >= (1 + $offset) AND rownum < (1 + $limit + $offset)";
890 function encodeBlob( $b ) {
891 return new Blob( $b );
894 function decodeBlob( $b ) {
895 if ( $b instanceof Blob ) {
896 $b = $b->fetch();
899 return $b;
902 function unionQueries( $sqls, $all ) {
903 $glue = ' UNION ALL ';
905 return 'SELECT * ' . ( $all ? '' : '/* UNION_UNIQUE */ ' ) .
906 'FROM (' . implode( $glue, $sqls ) . ')';
909 function wasDeadlock() {
910 return $this->lastErrno() == 'OCI-00060';
913 function duplicateTableStructure( $oldName, $newName, $temporary = false,
914 $fname = __METHOD__
916 $temporary = $temporary ? 'TRUE' : 'FALSE';
918 $newName = strtoupper( $newName );
919 $oldName = strtoupper( $oldName );
921 $tabName = substr( $newName, strlen( $this->mTablePrefix ) );
922 $oldPrefix = substr( $oldName, 0, strlen( $oldName ) - strlen( $tabName ) );
923 $newPrefix = strtoupper( $this->mTablePrefix );
925 return $this->doQuery( "BEGIN DUPLICATE_TABLE( '$tabName', " .
926 "'$oldPrefix', '$newPrefix', $temporary ); END;" );
929 function listTables( $prefix = null, $fname = __METHOD__ ) {
930 $listWhere = '';
931 if ( !empty( $prefix ) ) {
932 $listWhere = ' AND table_name LIKE \'' . strtoupper( $prefix ) . '%\'';
935 $owner = strtoupper( $this->mDBname );
936 $result = $this->doQuery( "SELECT table_name FROM all_tables " .
937 "WHERE owner='$owner' AND table_name NOT LIKE '%!_IDX\$_' ESCAPE '!' $listWhere" );
939 // dirty code ... i know
940 $endArray = array();
941 $endArray[] = strtoupper( $prefix . 'MWUSER' );
942 $endArray[] = strtoupper( $prefix . 'PAGE' );
943 $endArray[] = strtoupper( $prefix . 'IMAGE' );
944 $fixedOrderTabs = $endArray;
945 while ( ( $row = $result->fetchRow() ) !== false ) {
946 if ( !in_array( $row['table_name'], $fixedOrderTabs ) ) {
947 $endArray[] = $row['table_name'];
951 return $endArray;
954 public function dropTable( $tableName, $fName = __METHOD__ ) {
955 $tableName = $this->tableName( $tableName );
956 if ( !$this->tableExists( $tableName ) ) {
957 return false;
960 return $this->doQuery( "DROP TABLE $tableName CASCADE CONSTRAINTS PURGE" );
963 function timestamp( $ts = 0 ) {
964 return wfTimestamp( TS_ORACLE, $ts );
968 * Return aggregated value function call
970 * @param array $valuedata
971 * @param string $valuename
972 * @return mixed
974 public function aggregateValue( $valuedata, $valuename = 'value' ) {
975 return $valuedata;
979 * @return string Wikitext of a link to the server software's web site
981 public function getSoftwareLink() {
982 return '[{{int:version-db-oracle-url}} Oracle]';
986 * @return string Version information from the database
988 function getServerVersion() {
989 // better version number, fallback on driver
990 $rset = $this->doQuery(
991 'SELECT version FROM product_component_version ' .
992 'WHERE UPPER(product) LIKE \'ORACLE DATABASE%\''
994 $row = $rset->fetchRow();
995 if ( !$row ) {
996 return oci_server_version( $this->mConn );
999 return $row['version'];
1003 * Query whether a given index exists
1004 * @param string $table
1005 * @param string $index
1006 * @param string $fname
1007 * @return bool
1009 function indexExists( $table, $index, $fname = __METHOD__ ) {
1010 $table = $this->tableName( $table );
1011 $table = strtoupper( $this->removeIdentifierQuotes( $table ) );
1012 $index = strtoupper( $index );
1013 $owner = strtoupper( $this->mDBname );
1014 $sql = "SELECT 1 FROM all_indexes WHERE owner='$owner' AND index_name='{$table}_{$index}'";
1015 $res = $this->doQuery( $sql );
1016 if ( $res ) {
1017 $count = $res->numRows();
1018 $res->free();
1019 } else {
1020 $count = 0;
1023 return $count != 0;
1027 * Query whether a given table exists (in the given schema, or the default mw one if not given)
1028 * @param string $table
1029 * @param string $fname
1030 * @return bool
1032 function tableExists( $table, $fname = __METHOD__ ) {
1033 $table = $this->tableName( $table );
1034 $table = $this->addQuotes( strtoupper( $this->removeIdentifierQuotes( $table ) ) );
1035 $owner = $this->addQuotes( strtoupper( $this->mDBname ) );
1036 $sql = "SELECT 1 FROM all_tables WHERE owner=$owner AND table_name=$table";
1037 $res = $this->doQuery( $sql );
1038 if ( $res && $res->numRows() > 0 ) {
1039 $exists = true;
1040 } else {
1041 $exists = false;
1044 $res->free();
1046 return $exists;
1050 * Function translates mysql_fetch_field() functionality on ORACLE.
1051 * Caching is present for reducing query time.
1052 * For internal calls. Use fieldInfo for normal usage.
1053 * Returns false if the field doesn't exist
1055 * @param array|string $table
1056 * @param string $field
1057 * @return ORAField|ORAResult
1059 private function fieldInfoMulti( $table, $field ) {
1060 $field = strtoupper( $field );
1061 if ( is_array( $table ) ) {
1062 $table = array_map( array( &$this, 'tableNameInternal' ), $table );
1063 $tableWhere = 'IN (';
1064 foreach ( $table as &$singleTable ) {
1065 $singleTable = $this->removeIdentifierQuotes( $singleTable );
1066 if ( isset( $this->mFieldInfoCache["$singleTable.$field"] ) ) {
1067 return $this->mFieldInfoCache["$singleTable.$field"];
1069 $tableWhere .= '\'' . $singleTable . '\',';
1071 $tableWhere = rtrim( $tableWhere, ',' ) . ')';
1072 } else {
1073 $table = $this->removeIdentifierQuotes( $this->tableNameInternal( $table ) );
1074 if ( isset( $this->mFieldInfoCache["$table.$field"] ) ) {
1075 return $this->mFieldInfoCache["$table.$field"];
1077 $tableWhere = '= \'' . $table . '\'';
1080 $fieldInfoStmt = oci_parse(
1081 $this->mConn,
1082 'SELECT * FROM wiki_field_info_full WHERE table_name ' .
1083 $tableWhere . ' and column_name = \'' . $field . '\''
1085 if ( oci_execute( $fieldInfoStmt, $this->execFlags() ) === false ) {
1086 $e = oci_error( $fieldInfoStmt );
1087 $this->reportQueryError( $e['message'], $e['code'], 'fieldInfo QUERY', __METHOD__ );
1089 return false;
1091 $res = new ORAResult( $this, $fieldInfoStmt );
1092 if ( $res->numRows() == 0 ) {
1093 if ( is_array( $table ) ) {
1094 foreach ( $table as &$singleTable ) {
1095 $this->mFieldInfoCache["$singleTable.$field"] = false;
1097 } else {
1098 $this->mFieldInfoCache["$table.$field"] = false;
1100 $fieldInfoTemp = null;
1101 } else {
1102 $fieldInfoTemp = new ORAField( $res->fetchRow() );
1103 $table = $fieldInfoTemp->tableName();
1104 $this->mFieldInfoCache["$table.$field"] = $fieldInfoTemp;
1106 $res->free();
1108 return $fieldInfoTemp;
1112 * @throws DBUnexpectedError
1113 * @param string $table
1114 * @param string $field
1115 * @return ORAField
1117 function fieldInfo( $table, $field ) {
1118 if ( is_array( $table ) ) {
1119 throw new DBUnexpectedError( $this, 'DatabaseOracle::fieldInfo called with table array!' );
1122 return $this->fieldInfoMulti( $table, $field );
1125 protected function doBegin( $fname = __METHOD__ ) {
1126 $this->mTrxLevel = 1;
1127 $this->doQuery( 'SET CONSTRAINTS ALL DEFERRED' );
1130 protected function doCommit( $fname = __METHOD__ ) {
1131 if ( $this->mTrxLevel ) {
1132 $ret = oci_commit( $this->mConn );
1133 if ( !$ret ) {
1134 throw new DBUnexpectedError( $this, $this->lastError() );
1136 $this->mTrxLevel = 0;
1137 $this->doQuery( 'SET CONSTRAINTS ALL IMMEDIATE' );
1141 protected function doRollback( $fname = __METHOD__ ) {
1142 if ( $this->mTrxLevel ) {
1143 oci_rollback( $this->mConn );
1144 $this->mTrxLevel = 0;
1145 $this->doQuery( 'SET CONSTRAINTS ALL IMMEDIATE' );
1150 * defines must comply with ^define\s*([^\s=]*)\s*=\s?'\{\$([^\}]*)\}';
1152 * @param resource $fp
1153 * @param bool|string $lineCallback
1154 * @param bool|callable $resultCallback
1155 * @param string $fname
1156 * @param bool|callable $inputCallback
1157 * @return bool|string
1159 function sourceStream( $fp, $lineCallback = false, $resultCallback = false,
1160 $fname = __METHOD__, $inputCallback = false ) {
1161 $cmd = '';
1162 $done = false;
1163 $dollarquote = false;
1165 $replacements = array();
1167 while ( !feof( $fp ) ) {
1168 if ( $lineCallback ) {
1169 call_user_func( $lineCallback );
1171 $line = trim( fgets( $fp, 1024 ) );
1172 $sl = strlen( $line ) - 1;
1174 if ( $sl < 0 ) {
1175 continue;
1177 if ( '-' == $line[0] && '-' == $line[1] ) {
1178 continue;
1181 // Allow dollar quoting for function declarations
1182 if ( substr( $line, 0, 8 ) == '/*$mw$*/' ) {
1183 if ( $dollarquote ) {
1184 $dollarquote = false;
1185 $line = str_replace( '/*$mw$*/', '', $line ); // remove dollarquotes
1186 $done = true;
1187 } else {
1188 $dollarquote = true;
1190 } elseif ( !$dollarquote ) {
1191 if ( ';' == $line[$sl] && ( $sl < 2 || ';' != $line[$sl - 1] ) ) {
1192 $done = true;
1193 $line = substr( $line, 0, $sl );
1197 if ( $cmd != '' ) {
1198 $cmd .= ' ';
1200 $cmd .= "$line\n";
1202 if ( $done ) {
1203 $cmd = str_replace( ';;', ";", $cmd );
1204 if ( strtolower( substr( $cmd, 0, 6 ) ) == 'define' ) {
1205 if ( preg_match( '/^define\s*([^\s=]*)\s*=\s*\'\{\$([^\}]*)\}\'/', $cmd, $defines ) ) {
1206 $replacements[$defines[2]] = $defines[1];
1208 } else {
1209 foreach ( $replacements as $mwVar => $scVar ) {
1210 $cmd = str_replace( '&' . $scVar . '.', '`{$' . $mwVar . '}`', $cmd );
1213 $cmd = $this->replaceVars( $cmd );
1214 if ( $inputCallback ) {
1215 call_user_func( $inputCallback, $cmd );
1217 $res = $this->doQuery( $cmd );
1218 if ( $resultCallback ) {
1219 call_user_func( $resultCallback, $res, $this );
1222 if ( false === $res ) {
1223 $err = $this->lastError();
1225 return "Query \"{$cmd}\" failed with error code \"$err\".\n";
1229 $cmd = '';
1230 $done = false;
1234 return true;
1237 function selectDB( $db ) {
1238 $this->mDBname = $db;
1239 if ( $db == null || $db == $this->mUser ) {
1240 return true;
1242 $sql = 'ALTER SESSION SET CURRENT_SCHEMA=' . strtoupper( $db );
1243 $stmt = oci_parse( $this->mConn, $sql );
1244 MediaWiki\suppressWarnings();
1245 $success = oci_execute( $stmt );
1246 MediaWiki\restoreWarnings();
1247 if ( !$success ) {
1248 $e = oci_error( $stmt );
1249 if ( $e['code'] != '1435' ) {
1250 $this->reportQueryError( $e['message'], $e['code'], $sql, __METHOD__ );
1253 return false;
1256 return true;
1259 function strencode( $s ) {
1260 return str_replace( "'", "''", $s );
1263 function addQuotes( $s ) {
1264 global $wgContLang;
1265 if ( isset( $wgContLang->mLoaded ) && $wgContLang->mLoaded ) {
1266 $s = $wgContLang->checkTitleEncoding( $s );
1269 return "'" . $this->strencode( $s ) . "'";
1272 public function addIdentifierQuotes( $s ) {
1273 if ( !$this->getFlag( DBO_DDLMODE ) ) {
1274 $s = '/*Q*/' . $s;
1277 return $s;
1280 public function removeIdentifierQuotes( $s ) {
1281 return strpos( $s, '/*Q*/' ) === false ? $s : substr( $s, 5 );
1284 public function isQuotedIdentifier( $s ) {
1285 return strpos( $s, '/*Q*/' ) !== false;
1288 private function wrapFieldForWhere( $table, &$col, &$val ) {
1289 global $wgContLang;
1291 $col_info = $this->fieldInfoMulti( $table, $col );
1292 $col_type = $col_info != false ? $col_info->type() : 'CONSTANT';
1293 if ( $col_type == 'CLOB' ) {
1294 $col = 'TO_CHAR(' . $col . ')';
1295 $val = $wgContLang->checkTitleEncoding( $val );
1296 } elseif ( $col_type == 'VARCHAR2' ) {
1297 $val = $wgContLang->checkTitleEncoding( $val );
1301 private function wrapConditionsForWhere( $table, $conds, $parentCol = null ) {
1302 $conds2 = array();
1303 foreach ( $conds as $col => $val ) {
1304 if ( is_array( $val ) ) {
1305 $conds2[$col] = $this->wrapConditionsForWhere( $table, $val, $col );
1306 } else {
1307 if ( is_numeric( $col ) && $parentCol != null ) {
1308 $this->wrapFieldForWhere( $table, $parentCol, $val );
1309 } else {
1310 $this->wrapFieldForWhere( $table, $col, $val );
1312 $conds2[$col] = $val;
1316 return $conds2;
1319 function selectRow( $table, $vars, $conds, $fname = __METHOD__,
1320 $options = array(), $join_conds = array()
1322 if ( is_array( $conds ) ) {
1323 $conds = $this->wrapConditionsForWhere( $table, $conds );
1326 return parent::selectRow( $table, $vars, $conds, $fname, $options, $join_conds );
1330 * Returns an optional USE INDEX clause to go after the table, and a
1331 * string to go at the end of the query
1333 * @param array $options An associative array of options to be turned into
1334 * an SQL query, valid keys are listed in the function.
1335 * @return array
1337 function makeSelectOptions( $options ) {
1338 $preLimitTail = $postLimitTail = '';
1339 $startOpts = '';
1341 $noKeyOptions = array();
1342 foreach ( $options as $key => $option ) {
1343 if ( is_numeric( $key ) ) {
1344 $noKeyOptions[$option] = true;
1348 $preLimitTail .= $this->makeGroupByWithHaving( $options );
1350 $preLimitTail .= $this->makeOrderBy( $options );
1352 if ( isset( $noKeyOptions['FOR UPDATE'] ) ) {
1353 $postLimitTail .= ' FOR UPDATE';
1356 if ( isset( $noKeyOptions['DISTINCT'] ) || isset( $noKeyOptions['DISTINCTROW'] ) ) {
1357 $startOpts .= 'DISTINCT';
1360 if ( isset( $options['USE INDEX'] ) && !is_array( $options['USE INDEX'] ) ) {
1361 $useIndex = $this->useIndexClause( $options['USE INDEX'] );
1362 } else {
1363 $useIndex = '';
1366 return array( $startOpts, $useIndex, $preLimitTail, $postLimitTail );
1369 public function delete( $table, $conds, $fname = __METHOD__ ) {
1370 if ( is_array( $conds ) ) {
1371 $conds = $this->wrapConditionsForWhere( $table, $conds );
1373 // a hack for deleting pages, users and images (which have non-nullable FKs)
1374 // all deletions on these tables have transactions so final failure rollbacks these updates
1375 $table = $this->tableName( $table );
1376 if ( $table == $this->tableName( 'user' ) ) {
1377 $this->update( 'archive', array( 'ar_user' => 0 ),
1378 array( 'ar_user' => $conds['user_id'] ), $fname );
1379 $this->update( 'ipblocks', array( 'ipb_user' => 0 ),
1380 array( 'ipb_user' => $conds['user_id'] ), $fname );
1381 $this->update( 'image', array( 'img_user' => 0 ),
1382 array( 'img_user' => $conds['user_id'] ), $fname );
1383 $this->update( 'oldimage', array( 'oi_user' => 0 ),
1384 array( 'oi_user' => $conds['user_id'] ), $fname );
1385 $this->update( 'filearchive', array( 'fa_deleted_user' => 0 ),
1386 array( 'fa_deleted_user' => $conds['user_id'] ), $fname );
1387 $this->update( 'filearchive', array( 'fa_user' => 0 ),
1388 array( 'fa_user' => $conds['user_id'] ), $fname );
1389 $this->update( 'uploadstash', array( 'us_user' => 0 ),
1390 array( 'us_user' => $conds['user_id'] ), $fname );
1391 $this->update( 'recentchanges', array( 'rc_user' => 0 ),
1392 array( 'rc_user' => $conds['user_id'] ), $fname );
1393 $this->update( 'logging', array( 'log_user' => 0 ),
1394 array( 'log_user' => $conds['user_id'] ), $fname );
1395 } elseif ( $table == $this->tableName( 'image' ) ) {
1396 $this->update( 'oldimage', array( 'oi_name' => 0 ),
1397 array( 'oi_name' => $conds['img_name'] ), $fname );
1400 return parent::delete( $table, $conds, $fname );
1404 * @param string $table
1405 * @param array $values
1406 * @param array $conds
1407 * @param string $fname
1408 * @param array $options
1409 * @return bool
1410 * @throws DBUnexpectedError
1412 function update( $table, $values, $conds, $fname = __METHOD__, $options = array() ) {
1413 global $wgContLang;
1415 $table = $this->tableName( $table );
1416 $opts = $this->makeUpdateOptions( $options );
1417 $sql = "UPDATE $opts $table SET ";
1419 $first = true;
1420 foreach ( $values as $col => &$val ) {
1421 $sqlSet = $this->fieldBindStatement( $table, $col, $val, true );
1423 if ( !$first ) {
1424 $sqlSet = ', ' . $sqlSet;
1425 } else {
1426 $first = false;
1428 $sql .= $sqlSet;
1431 if ( $conds !== array() && $conds !== '*' ) {
1432 $conds = $this->wrapConditionsForWhere( $table, $conds );
1433 $sql .= ' WHERE ' . $this->makeList( $conds, LIST_AND );
1436 $this->mLastResult = $stmt = oci_parse( $this->mConn, $sql );
1437 if ( $stmt === false ) {
1438 $e = oci_error( $this->mConn );
1439 $this->reportQueryError( $e['message'], $e['code'], $sql, __METHOD__ );
1441 return false;
1443 foreach ( $values as $col => &$val ) {
1444 $col_info = $this->fieldInfoMulti( $table, $col );
1445 $col_type = $col_info != false ? $col_info->type() : 'CONSTANT';
1447 if ( $val === null ) {
1448 // do nothing ... null was inserted in statement creation
1449 } elseif ( $col_type != 'BLOB' && $col_type != 'CLOB' ) {
1450 if ( is_object( $val ) ) {
1451 $val = $val->getData();
1454 if ( preg_match( '/^timestamp.*/i', $col_type ) == 1 && strtolower( $val ) == 'infinity' ) {
1455 $val = '31-12-2030 12:00:00.000000';
1458 $val = ( $wgContLang != null ) ? $wgContLang->checkTitleEncoding( $val ) : $val;
1459 if ( oci_bind_by_name( $stmt, ":$col", $val ) === false ) {
1460 $e = oci_error( $stmt );
1461 $this->reportQueryError( $e['message'], $e['code'], $sql, __METHOD__ );
1463 return false;
1465 } else {
1466 /** @var OCI_Lob[] $lob */
1467 $lob[$col] = oci_new_descriptor( $this->mConn, OCI_D_LOB );
1468 if ( $lob[$col] === false ) {
1469 $e = oci_error( $stmt );
1470 throw new DBUnexpectedError( $this, "Cannot create LOB descriptor: " . $e['message'] );
1473 if ( is_object( $val ) ) {
1474 $val = $val->getData();
1477 if ( $col_type == 'BLOB' ) {
1478 $lob[$col]->writeTemporary( $val );
1479 oci_bind_by_name( $stmt, ":$col", $lob[$col], -1, SQLT_BLOB );
1480 } else {
1481 $lob[$col]->writeTemporary( $val );
1482 oci_bind_by_name( $stmt, ":$col", $lob[$col], -1, OCI_B_CLOB );
1487 MediaWiki\suppressWarnings();
1489 if ( oci_execute( $stmt, $this->execFlags() ) === false ) {
1490 $e = oci_error( $stmt );
1491 if ( !$this->ignoreDupValOnIndex || $e['code'] != '1' ) {
1492 $this->reportQueryError( $e['message'], $e['code'], $sql, __METHOD__ );
1494 return false;
1495 } else {
1496 $this->mAffectedRows = oci_num_rows( $stmt );
1498 } else {
1499 $this->mAffectedRows = oci_num_rows( $stmt );
1502 MediaWiki\restoreWarnings();
1504 if ( isset( $lob ) ) {
1505 foreach ( $lob as $lob_v ) {
1506 $lob_v->free();
1510 if ( !$this->mTrxLevel ) {
1511 oci_commit( $this->mConn );
1514 return oci_free_statement( $stmt );
1517 function bitNot( $field ) {
1518 // expecting bit-fields smaller than 4bytes
1519 return 'BITNOT(' . $field . ')';
1522 function bitAnd( $fieldLeft, $fieldRight ) {
1523 return 'BITAND(' . $fieldLeft . ', ' . $fieldRight . ')';
1526 function bitOr( $fieldLeft, $fieldRight ) {
1527 return 'BITOR(' . $fieldLeft . ', ' . $fieldRight . ')';
1530 function getDBname() {
1531 return $this->mDBname;
1534 function getServer() {
1535 return $this->mServer;
1538 public function buildGroupConcatField(
1539 $delim, $table, $field, $conds = '', $join_conds = array()
1541 $fld = "LISTAGG($field," . $this->addQuotes( $delim ) . ") WITHIN GROUP (ORDER BY $field)";
1543 return '(' . $this->selectSQLText( $table, $fld, $conds, null, array(), $join_conds ) . ')';
1546 public function getSearchEngine() {
1547 return 'SearchOracle';
1550 public function getInfinity() {
1551 return '31-12-2030 12:00:00.000000';