3 * This is the Oracle database abstraction layer.
10 * The oci8 extension is fairly weak and doesn't support oci_num_rows, among
11 * other things. We use a wrapper class to handle that and other
12 * Oracle-specific bits, like converting column names back to lowercase.
20 private $columns = array();
22 private function array_unique_md( $array_in ) {
24 $array_hashes = array();
26 foreach ( $array_in as $item ) {
27 $hash = md5( serialize( $item ) );
28 if ( !isset( $array_hashes[$hash] ) ) {
29 $array_hashes[$hash] = $hash;
37 function __construct( &$db, $stmt, $unique = false ) {
40 if ( ( $this->nrows = oci_fetch_all( $stmt, $this->rows, 0, - 1, OCI_FETCHSTATEMENT_BY_ROW | OCI_NUM ) ) === false ) {
41 $e = oci_error( $stmt );
42 $db->reportQueryError( $e['message'], $e['code'], '', __METHOD__ );
48 $this->rows = $this->array_unique_md( $this->rows );
49 $this->nrows = count( $this->rows );
52 if ($this->nrows > 0) {
53 foreach ( $this->rows[0] as $k => $v ) {
54 $this->columns[$k] = strtolower( oci_field_name( $stmt, $k + 1 ) );
59 oci_free_statement( $stmt );
62 public function free() {
66 public function seek( $row ) {
67 $this->cursor = min( $row, $this->nrows );
70 public function numRows() {
74 public function numFields() {
75 return count($this->columns);
78 public function fetchObject() {
79 if ( $this->cursor >= $this->nrows ) {
82 $row = $this->rows[$this->cursor++];
83 $ret = new stdClass();
84 foreach ( $row as $k => $v ) {
85 $lc = $this->columns[$k];
92 public function fetchRow() {
93 if ( $this->cursor >= $this->nrows ) {
97 $row = $this->rows[$this->cursor++];
99 foreach ( $row as $k => $v ) {
100 $lc = $this->columns[$k];
112 class ORAField implements Field {
113 private $name, $tablename, $default, $max_length, $nullable,
114 $is_pk, $is_unique, $is_multiple, $is_key, $type;
116 function __construct( $info ) {
117 $this->name = $info['column_name'];
118 $this->tablename = $info['table_name'];
119 $this->default = $info['data_default'];
120 $this->max_length = $info['data_length'];
121 $this->nullable = $info['not_null'];
122 $this->is_pk = isset( $info['prim'] ) && $info['prim'] == 1 ? 1 : 0;
123 $this->is_unique = isset( $info['uniq'] ) && $info['uniq'] == 1 ? 1 : 0;
124 $this->is_multiple = isset( $info['nonuniq'] ) && $info['nonuniq'] == 1 ? 1 : 0;
125 $this->is_key = ( $this->is_pk || $this->is_unique || $this->is_multiple );
126 $this->type = $info['data_type'];
133 function tableName() {
134 return $this->tablename;
137 function defaultValue() {
138 return $this->default;
141 function maxLength() {
142 return $this->max_length;
145 function isNullable() {
146 return $this->nullable;
150 return $this->is_key;
153 function isMultipleKey() {
154 return $this->is_multiple;
165 class DatabaseOracle extends DatabaseBase {
166 var $mInsertId = null;
167 var $mLastResult = null;
168 var $numeric_version = null;
169 var $lastResult = null;
173 var $ignore_DUP_VAL_ON_INDEX = false;
174 var $sequenceData = null;
176 var $defaultCharset = 'AL32UTF8';
178 var $mFieldInfoCache = array();
180 function __construct( $server = false, $user = false, $password = false, $dbName = false,
181 $flags = 0, $tablePrefix = 'get from global' )
183 $tablePrefix = $tablePrefix == 'get from global' ? $tablePrefix : strtoupper( $tablePrefix );
184 parent::__construct( $server, $user, $password, $dbName, $flags, $tablePrefix );
185 wfRunHooks( 'DatabaseOraclePostInit', array( $this ) );
188 function __destruct() {
189 if ($this->mOpened) {
190 wfSuppressWarnings();
200 function cascadingDeletes() {
203 function cleanupTriggers() {
206 function strictIPs() {
209 function realTimestamps() {
212 function implicitGroupby() {
215 function implicitOrderby() {
218 function searchableIPs() {
222 static function newFromParams( $server, $user, $password, $dbName, $flags = 0 )
224 return new DatabaseOracle( $server, $user, $password, $dbName, $flags );
228 * Usually aborts on failure
230 function open( $server, $user, $password, $dbName ) {
231 if ( !function_exists( 'oci_connect' ) ) {
232 throw new DBConnectionError( $this, "Oracle functions missing, have you compiled PHP with the --with-oci8 option?\n (Note: if you recently installed PHP, you may need to restart your webserver and database)\n" );
235 $this->mUser = $user;
236 $this->mPassword = $password;
237 // changed internal variables functions
238 // mServer now holds the TNS endpoint
239 // mDBname is schema name if different from username
241 // backward compatibillity (server used to be null and TNS was supplied in dbname)
242 $this->mServer = $dbName;
243 $this->mDBname = $user;
245 $this->mServer = $server;
247 $this->mDBname = $user;
249 $this->mDBname = $dbName;
253 if ( !strlen( $user ) ) { # e.g. the class is being loaded
257 $session_mode = $this->mFlags & DBO_SYSDBA ? OCI_SYSDBA : OCI_DEFAULT;
258 if ( $this->mFlags & DBO_DEFAULT ) {
259 $this->mConn = oci_new_connect( $this->mUser, $this->mPassword, $this->mServer, $this->defaultCharset, $session_mode );
261 $this->mConn = oci_connect( $this->mUser, $this->mPassword, $this->mServer, $this->defaultCharset, $session_mode );
264 if ( $this->mUser != $this->mDBname ) {
265 //change current schema in session
266 $this->selectDB( $this->mDBname );
269 if ( !$this->mConn ) {
270 throw new DBConnectionError( $this, $this->lastError() );
273 $this->mOpened = true;
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=\'.,\'' );
284 * Closes a database connection, if it is open
285 * Returns success, true if already closed
288 $this->mOpened = false;
289 if ( $this->mConn ) {
290 if ( $this->mTrxLevel ) {
293 return oci_close( $this->mConn );
299 function execFlags() {
300 return $this->mTrxLevel ? OCI_DEFAULT : OCI_COMMIT_ON_SUCCESS;
303 function doQuery( $sql ) {
304 wfDebug( "SQL: [$sql]\n" );
305 if ( !mb_check_encoding( $sql ) ) {
306 throw new MWException( "SQL encoding is invalid\n$sql" );
309 // handle some oracle specifics
310 // remove AS column/table/subquery namings
311 if( !$this->getFlag( DBO_DDLMODE ) ) {
312 $sql = preg_replace( '/ as /i', ' ', $sql );
315 // Oracle has issues with UNION clause if the statement includes LOB fields
316 // So we do a UNION ALL and then filter the results array with array_unique
317 $union_unique = ( preg_match( '/\/\* UNION_UNIQUE \*\/ /', $sql ) != 0 );
318 // EXPLAIN syntax in Oracle is EXPLAIN PLAN FOR and it return nothing
319 // you have to select data from plan table after explain
320 $explain_id = date( 'dmYHis' );
322 $sql = preg_replace( '/^EXPLAIN /', 'EXPLAIN PLAN SET STATEMENT_ID = \'' . $explain_id . '\' FOR', $sql, 1, $explain_count );
324 wfSuppressWarnings();
326 if ( ( $this->mLastResult = $stmt = oci_parse( $this->mConn, $sql ) ) === false ) {
327 $e = oci_error( $this->mConn );
328 $this->reportQueryError( $e['message'], $e['code'], $sql, __METHOD__ );
332 if ( !oci_execute( $stmt, $this->execFlags() ) ) {
333 $e = oci_error( $stmt );
334 if ( !$this->ignore_DUP_VAL_ON_INDEX || $e['code'] != '1' ) {
335 $this->reportQueryError( $e['message'], $e['code'], $sql, __METHOD__ );
342 if ( $explain_count > 0 ) {
343 return $this->doQuery( 'SELECT id, cardinality "ROWS" FROM plan_table WHERE statement_id = \'' . $explain_id . '\'' );
344 } elseif ( oci_statement_type( $stmt ) == 'SELECT' ) {
345 return new ORAResult( $this, $stmt, $union_unique );
347 $this->mAffectedRows = oci_num_rows( $stmt );
352 function queryIgnore( $sql, $fname = '' ) {
353 return $this->query( $sql, $fname, true );
356 function freeResult( $res ) {
357 if ( $res instanceof ResultWrapper ) {
364 function fetchObject( $res ) {
365 if ( $res instanceof ResultWrapper ) {
369 return $res->fetchObject();
372 function fetchRow( $res ) {
373 if ( $res instanceof ResultWrapper ) {
377 return $res->fetchRow();
380 function numRows( $res ) {
381 if ( $res instanceof ResultWrapper ) {
385 return $res->numRows();
388 function numFields( $res ) {
389 if ( $res instanceof ResultWrapper ) {
393 return $res->numFields();
396 function fieldName( $stmt, $n ) {
397 return oci_field_name( $stmt, $n );
401 * This must be called after nextSequenceVal
403 function insertId() {
404 return $this->mInsertId;
407 function dataSeek( $res, $row ) {
408 if ( $res instanceof ORAResult ) {
411 $res->result->seek( $row );
415 function lastError() {
416 if ( $this->mConn === false ) {
419 $e = oci_error( $this->mConn );
421 return $e['message'];
424 function lastErrno() {
425 if ( $this->mConn === false ) {
428 $e = oci_error( $this->mConn );
433 function affectedRows() {
434 return $this->mAffectedRows;
438 * Returns information about an index
439 * If errors are explicitly ignored, returns NULL on failure
441 function indexInfo( $table, $index, $fname = 'DatabaseOracle::indexExists' ) {
445 function indexUnique( $table, $index, $fname = 'DatabaseOracle::indexUnique' ) {
449 function insert( $table, $a, $fname = 'DatabaseOracle::insert', $options = array() ) {
450 if ( !count( $a ) ) {
454 if ( !is_array( $options ) ) {
455 $options = array( $options );
458 if ( in_array( 'IGNORE', $options ) ) {
459 $this->ignore_DUP_VAL_ON_INDEX = true;
462 if ( !is_array( reset( $a ) ) ) {
466 foreach ( $a as &$row ) {
467 $this->insertOneRow( $table, $row, $fname );
471 if ( in_array( 'IGNORE', $options ) ) {
472 $this->ignore_DUP_VAL_ON_INDEX = false;
478 private function fieldBindStatement ( $table, $col, &$val, $includeCol = false ) {
479 $col_info = $this->fieldInfoMulti( $table, $col );
480 $col_type = $col_info != false ? $col_info->type() : 'CONSTANT';
483 if ( is_numeric( $col ) ) {
487 } else if ( $includeCol ) {
491 if ( $val == '' && $val !== 0 && $col_type != 'BLOB' && $col_type != 'CLOB' ) {
495 if ( $val === 'NULL' ) {
499 if ( $val === null ) {
500 if ( $col_info != false && $col_info->isNullable() == 0 && $col_info->defaultValue() != null ) {
512 private function insertOneRow( $table, $row, $fname ) {
515 $table = $this->tableName( $table );
516 // "INSERT INTO tables (a, b, c)"
517 $sql = "INSERT INTO " . $table . " (" . join( ',', array_keys( $row ) ) . ')';
520 // for each value, append ":key"
522 foreach ( $row as $col => &$val ) {
529 $sql .= $this->fieldBindStatement( $table, $col, $val );
533 if ( ( $this->mLastResult = $stmt = oci_parse( $this->mConn, $sql ) ) === false ) {
534 $e = oci_error( $this->mConn );
535 $this->reportQueryError( $e['message'], $e['code'], $sql, __METHOD__ );
538 foreach ( $row as $col => &$val ) {
539 $col_info = $this->fieldInfoMulti( $table, $col );
540 $col_type = $col_info != false ? $col_info->type() : 'CONSTANT';
542 if ( $val === null ) {
543 // do nothing ... null was inserted in statement creation
544 } elseif ( $col_type != 'BLOB' && $col_type != 'CLOB' ) {
545 if ( is_object( $val ) ) {
546 $val = $val->fetch();
549 if ( preg_match( '/^timestamp.*/i', $col_type ) == 1 && strtolower( $val ) == 'infinity' ) {
550 $val = '31-12-2030 12:00:00.000000';
553 $val = ( $wgContLang != null ) ? $wgContLang->checkTitleEncoding( $val ) : $val;
554 if ( oci_bind_by_name( $stmt, ":$col", $val ) === false ) {
555 $e = oci_error( $stmt );
556 $this->reportQueryError( $e['message'], $e['code'], $sql, __METHOD__ );
560 if ( ( $lob[$col] = oci_new_descriptor( $this->mConn, OCI_D_LOB ) ) === false ) {
561 $e = oci_error( $stmt );
562 throw new DBUnexpectedError( $this, "Cannot create LOB descriptor: " . $e['message'] );
565 if ( is_object( $val ) ) {
566 $val = $val->fetch();
569 if ( $col_type == 'BLOB' ) {
570 $lob[$col]->writeTemporary( $val, OCI_TEMP_BLOB );
571 oci_bind_by_name( $stmt, ":$col", $lob[$col], - 1, OCI_B_BLOB );
573 $lob[$col]->writeTemporary( $val, OCI_TEMP_CLOB );
574 oci_bind_by_name( $stmt, ":$col", $lob[$col], - 1, OCI_B_CLOB );
579 wfSuppressWarnings();
581 if ( oci_execute( $stmt, $this->execFlags() ) === false ) {
582 $e = oci_error( $stmt );
583 if ( !$this->ignore_DUP_VAL_ON_INDEX || $e['code'] != '1' ) {
584 $this->reportQueryError( $e['message'], $e['code'], $sql, __METHOD__ );
587 $this->mAffectedRows = oci_num_rows( $stmt );
590 $this->mAffectedRows = oci_num_rows( $stmt );
595 if ( isset( $lob ) ) {
596 foreach ( $lob as $lob_v ) {
601 if ( !$this->mTrxLevel ) {
602 oci_commit( $this->mConn );
605 oci_free_statement( $stmt );
608 function insertSelect( $destTable, $srcTable, $varMap, $conds, $fname = 'DatabaseOracle::insertSelect',
609 $insertOptions = array(), $selectOptions = array() )
611 $destTable = $this->tableName( $destTable );
612 if ( !is_array( $selectOptions ) ) {
613 $selectOptions = array( $selectOptions );
615 list( $startOpts, $useIndex, $tailOpts ) = $this->makeSelectOptions( $selectOptions );
616 if ( is_array( $srcTable ) ) {
617 $srcTable = implode( ',', array_map( array( &$this, 'tableName' ), $srcTable ) );
619 $srcTable = $this->tableName( $srcTable );
622 if ( ( $sequenceData = $this->getSequenceData( $destTable ) ) !== false &&
623 !isset( $varMap[$sequenceData['column']] ) )
625 $varMap[$sequenceData['column']] = 'GET_SEQUENCE_VALUE(\'' . $sequenceData['sequence'] . '\')';
628 // count-alias subselect fields to avoid abigious definition errors
630 foreach ( $varMap as &$val ) {
631 $val = $val . ' field' . ( $i++ );
634 $sql = "INSERT INTO $destTable (" . implode( ',', array_keys( $varMap ) ) . ')' .
635 " SELECT $startOpts " . implode( ',', $varMap ) .
636 " FROM $srcTable $useIndex ";
637 if ( $conds != '*' ) {
638 $sql .= ' WHERE ' . $this->makeList( $conds, LIST_AND );
640 $sql .= " $tailOpts";
642 if ( in_array( 'IGNORE', $insertOptions ) ) {
643 $this->ignore_DUP_VAL_ON_INDEX = true;
646 $retval = $this->query( $sql, $fname );
648 if ( in_array( 'IGNORE', $insertOptions ) ) {
649 $this->ignore_DUP_VAL_ON_INDEX = false;
655 function tableName( $name ) {
656 global $wgSharedDB, $wgSharedPrefix, $wgSharedTables;
658 Replace reserved words with better ones
659 Using uppercase because that's the only way Oracle can handle
667 $name = 'PAGECONTENT';
672 The rest of procedure is equal to generic Databse class
673 except for the quoting style
675 if ( $name[0] == '"' && substr( $name, - 1, 1 ) == '"' ) {
678 if ( preg_match( '/(^|\s)(DISTINCT|JOIN|ON|AS)(\s|$)/i', $name ) !== 0 ) {
681 $dbDetails = array_reverse( explode( '.', $name, 2 ) );
682 if ( isset( $dbDetails[1] ) ) {
683 @list( $table, $database ) = $dbDetails;
685 @list( $table ) = $dbDetails;
688 $prefix = $this->mTablePrefix;
690 if ( isset( $database ) ) {
691 $table = ( $table[0] == '`' ? $table : "`{$table}`" );
694 if ( !isset( $database ) && isset( $wgSharedDB ) && $table[0] != '"'
695 && isset( $wgSharedTables )
696 && is_array( $wgSharedTables )
697 && in_array( $table, $wgSharedTables )
699 $database = $wgSharedDB;
700 $prefix = isset( $wgSharedPrefix ) ? $wgSharedPrefix : $prefix;
703 if ( isset( $database ) ) {
704 $database = ( $database[0] == '"' ? $database : "\"{$database}\"" );
706 $table = ( $table[0] == '"') ? $table : "\"{$prefix}{$table}\"" ;
708 $tableName = ( isset( $database ) ? "{$database}.{$table}" : "{$table}" );
710 return strtoupper( $tableName );
713 function tableNameInternal( $name ) {
714 $name = $this->tableName( $name );
715 return preg_replace( '/.*\."(.*)"/', '$1', $name);
719 * Return the next in a sequence, save the value for retrieval via insertId()
721 function nextSequenceValue( $seqName ) {
722 $res = $this->query( "SELECT $seqName.nextval FROM dual" );
723 $row = $this->fetchRow( $res );
724 $this->mInsertId = $row[0];
725 return $this->mInsertId;
729 * Return sequence_name if table has a sequence
731 private function getSequenceData( $table ) {
732 if ( $this->sequenceData == null ) {
733 $result = $this->doQuery( 'SELECT lower(us.sequence_name), lower(utc.table_name), lower(utc.column_name) from user_sequences us, user_tab_columns utc where us.sequence_name = utc.table_name||\'_\'||utc.column_name||\'_SEQ\'' );
735 while ( ( $row = $result->fetchRow() ) !== false ) {
736 $this->sequenceData[$this->tableName( $row[1] )] = array(
737 'sequence' => $row[0],
743 return ( isset( $this->sequenceData[$table] ) ) ? $this->sequenceData[$table] : false;
747 * REPLACE query wrapper
748 * Oracle simulates this with a DELETE followed by INSERT
749 * $row is the row to insert, an associative array
750 * $uniqueIndexes is an array of indexes. Each element may be either a
751 * field name or an array of field names
753 * It may be more efficient to leave off unique indexes which are unlikely to collide.
754 * However if you do this, you run the risk of encountering errors which wouldn't have
757 * @param $table String: table name
758 * @param $uniqueIndexes Array: array of indexes. Each element may be
759 * either a field name or an array of field names
760 * @param $rows Array: rows to insert to $table
761 * @param $fname String: function name, you can use __METHOD__ here
763 function replace( $table, $uniqueIndexes, $rows, $fname = 'DatabaseOracle::replace' ) {
764 $table = $this->tableName( $table );
766 if ( count( $rows ) == 0 ) {
771 if ( !is_array( reset( $rows ) ) ) {
772 $rows = array( $rows );
775 $sequenceData = $this->getSequenceData( $table );
777 foreach ( $rows as $row ) {
778 # Delete rows which collide
779 if ( $uniqueIndexes ) {
780 $deleteConds = array();
781 foreach ( $uniqueIndexes as $key=>$index ) {
782 if ( is_array( $index ) ) {
783 $deleteConds2 = array();
784 foreach ( $index as $col ) {
785 $deleteConds2[$col] = $row[$col];
787 $deleteConds[$key] = $this->makeList( $deleteConds2, LIST_AND );
789 $deleteConds[$index] = $row[$index];
792 $deleteConds = array( $this->makeList( $deleteConds, LIST_OR ) );
793 $this->delete( $table, $deleteConds, $fname );
797 if ( $sequenceData !== false && !isset( $row[$sequenceData['column']] ) ) {
798 $row[$sequenceData['column']] = $this->nextSequenceValue( $sequenceData['sequence'] );
802 $this->insert( $table, $row, $fname );
806 # DELETE where the condition is a join
807 function deleteJoin( $delTable, $joinTable, $delVar, $joinVar, $conds, $fname = 'DatabaseOracle::deleteJoin' ) {
809 throw new DBUnexpectedError( $this, 'DatabaseOracle::deleteJoin() called with empty $conds' );
812 $delTable = $this->tableName( $delTable );
813 $joinTable = $this->tableName( $joinTable );
814 $sql = "DELETE FROM $delTable WHERE $delVar IN (SELECT $joinVar FROM $joinTable ";
815 if ( $conds != '*' ) {
816 $sql .= 'WHERE ' . $this->makeList( $conds, LIST_AND );
820 $this->query( $sql, $fname );
823 # Returns the size of a text field, or -1 for "unlimited"
824 function textFieldSize( $table, $field ) {
825 $fieldInfoData = $this->fieldInfo( $table, $field );
826 return $fieldInfoData->maxLength();
829 function limitResult( $sql, $limit, $offset = false ) {
830 if ( $offset === false ) {
833 return "SELECT * FROM ($sql) WHERE rownum >= (1 + $offset) AND rownum < (1 + $limit + $offset)";
836 function encodeBlob( $b ) {
837 return new Blob( $b );
840 function decodeBlob( $b ) {
841 if ( $b instanceof Blob ) {
847 function unionQueries( $sqls, $all ) {
848 $glue = ' UNION ALL ';
849 return 'SELECT * ' . ( $all ? '':'/* UNION_UNIQUE */ ' ) . 'FROM (' . implode( $glue, $sqls ) . ')' ;
852 public function unixTimestamp( $field ) {
853 return "((trunc($field) - to_date('19700101','YYYYMMDD')) * 86400)";
856 function wasDeadlock() {
857 return $this->lastErrno() == 'OCI-00060';
860 function duplicateTableStructure( $oldName, $newName, $temporary = false, $fname = 'DatabaseOracle::duplicateTableStructure' ) {
863 $temporary = $temporary ? 'TRUE' : 'FALSE';
865 $newName = trim( strtoupper( $newName ), '"');
866 $oldName = trim( strtoupper( $oldName ), '"');
868 $tabName = substr( $newName, strlen( $wgDBprefix ) );
869 $oldPrefix = substr( $oldName, 0, strlen( $oldName ) - strlen( $tabName ) );
871 return $this->doQuery( 'BEGIN DUPLICATE_TABLE(\'' . $tabName . '\', \'' . $oldPrefix . '\', \'' . strtoupper( $wgDBprefix ) . '\', ' . $temporary . '); END;' );
874 function listTables( $prefix = null, $fname = 'DatabaseOracle::listTables' ) {
876 if (!empty($prefix)) {
877 $listWhere = ' AND table_name LIKE \''.strtoupper($prefix).'%\'';
880 $result = $this->doQuery( "SELECT table_name FROM user_tables WHERE table_name NOT LIKE '%!_IDX$_' ESCAPE '!' $listWhere" );
882 // dirty code ... i know
884 $endArray[] = $prefix.'MWUSER';
885 $endArray[] = $prefix.'PAGE';
886 $endArray[] = $prefix.'IMAGE';
887 $fixedOrderTabs = $endArray;
888 while (($row = $result->fetchRow()) !== false) {
889 if (!in_array($row['table_name'], $fixedOrderTabs))
890 $endArray[] = $row['table_name'];
896 public function dropTable( $tableName, $fName = 'DatabaseOracle::dropTable' ) {
897 $tableName = $this->tableName($tableName);
898 if( !$this->tableExists( $tableName ) ) {
902 return $this->doQuery( "DROP TABLE $tableName CASCADE CONSTRAINTS PURGE" );
905 function timestamp( $ts = 0 ) {
906 return wfTimestamp( TS_ORACLE, $ts );
910 * Return aggregated value function call
912 function aggregateValue ( $valuedata, $valuename = 'value' ) {
916 function reportQueryError( $error, $errno, $sql, $fname, $tempIgnore = false ) {
917 # Ignore errors during error handling to avoid infinite
919 $ignore = $this->ignoreErrors( true );
920 ++$this->mErrorCount;
922 if ( $ignore || $tempIgnore ) {
923 wfDebug( "SQL ERROR (ignored): $error\n" );
924 $this->ignoreErrors( $ignore );
926 throw new DBQueryError( $this, $error, $errno, $sql, $fname );
931 * @return string wikitext of a link to the server software's web site
933 public static function getSoftwareLink() {
934 return '[http://www.oracle.com/ Oracle]';
938 * @return string Version information from the database
940 function getServerVersion() {
941 //better version number, fallback on driver
942 $rset = $this->doQuery( 'SELECT version FROM product_component_version WHERE UPPER(product) LIKE \'ORACLE DATABASE%\'' );
943 if ( !( $row = $rset->fetchRow() ) ) {
944 return oci_server_version( $this->mConn );
946 return $row['version'];
950 * Query whether a given table exists (in the given schema, or the default mw one if not given)
952 function tableExists( $table ) {
953 $table = $this->addQuotes( trim( $this->tableName($table), '"' ) );
954 $owner = $this->addQuotes( strtoupper( $this->mDBname ) );
955 $SQL = "SELECT 1 FROM all_tables WHERE owner=$owner AND table_name=$table";
956 $res = $this->doQuery( $SQL );
958 $count = $res->numRows();
967 * Function translates mysql_fetch_field() functionality on ORACLE.
968 * Caching is present for reducing query time.
969 * For internal calls. Use fieldInfo for normal usage.
970 * Returns false if the field doesn't exist
972 * @param $table Array
973 * @param $field String
975 private function fieldInfoMulti( $table, $field ) {
976 $field = strtoupper( $field );
977 if ( is_array( $table ) ) {
978 $table = array_map( array( &$this, 'tableNameInternal' ), $table );
979 $tableWhere = 'IN (';
980 foreach( $table as &$singleTable ) {
981 $singleTable = strtoupper( trim( $singleTable, '"' ) );
982 if ( isset( $this->mFieldInfoCache["$singleTable.$field"] ) ) {
983 return $this->mFieldInfoCache["$singleTable.$field"];
985 $tableWhere .= '\'' . $singleTable . '\',';
987 $tableWhere = rtrim( $tableWhere, ',' ) . ')';
989 $table = strtoupper( trim( $this->tableNameInternal( $table ), '"' ) );
990 if ( isset( $this->mFieldInfoCache["$table.$field"] ) ) {
991 return $this->mFieldInfoCache["$table.$field"];
993 $tableWhere = '= \''.$table.'\'';
996 $fieldInfoStmt = oci_parse( $this->mConn, 'SELECT * FROM wiki_field_info_full WHERE table_name '.$tableWhere.' and column_name = \''.$field.'\'' );
997 if ( oci_execute( $fieldInfoStmt, $this->execFlags() ) === false ) {
998 $e = oci_error( $fieldInfoStmt );
999 $this->reportQueryError( $e['message'], $e['code'], 'fieldInfo QUERY', __METHOD__ );
1002 $res = new ORAResult( $this, $fieldInfoStmt );
1003 if ( $res->numRows() == 0 ) {
1004 if ( is_array( $table ) ) {
1005 foreach( $table as &$singleTable ) {
1006 $this->mFieldInfoCache["$singleTable.$field"] = false;
1009 $this->mFieldInfoCache["$table.$field"] = false;
1011 $fieldInfoTemp = null;
1013 $fieldInfoTemp = new ORAField( $res->fetchRow() );
1014 $table = $fieldInfoTemp->tableName();
1015 $this->mFieldInfoCache["$table.$field"] = $fieldInfoTemp;
1018 return $fieldInfoTemp;
1021 function fieldInfo( $table, $field ) {
1022 if ( is_array( $table ) ) {
1023 throw new DBUnexpectedError( $this, 'DatabaseOracle::fieldInfo called with table array!' );
1025 return $this->fieldInfoMulti ($table, $field);
1028 function begin( $fname = 'DatabaseOracle::begin' ) {
1029 $this->mTrxLevel = 1;
1032 function commit( $fname = 'DatabaseOracle::commit' ) {
1033 if ( $this->mTrxLevel ) {
1034 oci_commit( $this->mConn );
1035 $this->mTrxLevel = 0;
1039 function rollback( $fname = 'DatabaseOracle::rollback' ) {
1040 if ( $this->mTrxLevel ) {
1041 oci_rollback( $this->mConn );
1042 $this->mTrxLevel = 0;
1046 /* Not even sure why this is used in the main codebase... */
1047 function limitResultForUpdate( $sql, $num ) {
1051 /* defines must comply with ^define\s*([^\s=]*)\s*=\s?'\{\$([^\}]*)\}'; */
1052 function sourceStream( $fp, $lineCallback = false, $resultCallback = false, $fname = 'DatabaseOracle::sourceStream' ) {
1055 $dollarquote = false;
1057 $replacements = array();
1059 while ( ! feof( $fp ) ) {
1060 if ( $lineCallback ) {
1061 call_user_func( $lineCallback );
1063 $line = trim( fgets( $fp, 1024 ) );
1064 $sl = strlen( $line ) - 1;
1069 if ( '-' == $line { 0 } && '-' == $line { 1 } ) {
1073 // Allow dollar quoting for function declarations
1074 if ( substr( $line, 0, 8 ) == '/*$mw$*/' ) {
1075 if ( $dollarquote ) {
1076 $dollarquote = false;
1077 $line = str_replace( '/*$mw$*/', '', $line ); // remove dollarquotes
1080 $dollarquote = true;
1082 } elseif ( !$dollarquote ) {
1083 if ( ';' == $line { $sl } && ( $sl < 2 || ';' != $line { $sl - 1 } ) ) {
1085 $line = substr( $line, 0, $sl );
1095 $cmd = str_replace( ';;', ";", $cmd );
1096 if ( strtolower( substr( $cmd, 0, 6 ) ) == 'define' ) {
1097 if ( preg_match( '/^define\s*([^\s=]*)\s*=\s*\'\{\$([^\}]*)\}\'/', $cmd, $defines ) ) {
1098 $replacements[$defines[2]] = $defines[1];
1101 foreach ( $replacements as $mwVar => $scVar ) {
1102 $cmd = str_replace( '&' . $scVar . '.', '`{$' . $mwVar . '}`', $cmd );
1105 $cmd = $this->replaceVars( $cmd );
1106 $res = $this->doQuery( $cmd );
1107 if ( $resultCallback ) {
1108 call_user_func( $resultCallback, $res, $this );
1111 if ( false === $res ) {
1112 $err = $this->lastError();
1113 return "Query \"{$cmd}\" failed with error code \"$err\".\n";
1124 function selectDB( $db ) {
1125 $this->mDBname = $db;
1126 if ( $db == null || $db == $this->mUser ) {
1129 $sql = 'ALTER SESSION SET CURRENT_SCHEMA=' . strtoupper($db);
1130 $stmt = oci_parse( $this->mConn, $sql );
1131 wfSuppressWarnings();
1132 $success = oci_execute( $stmt );
1133 wfRestoreWarnings();
1135 $e = oci_error( $stmt );
1136 if ( $e['code'] != '1435' ) {
1137 $this->reportQueryError( $e['message'], $e['code'], $sql, __METHOD__ );
1144 function strencode( $s ) {
1145 return str_replace( "'", "''", $s );
1148 function addQuotes( $s ) {
1150 if ( isset( $wgContLang->mLoaded ) && $wgContLang->mLoaded ) {
1151 $s = $wgContLang->checkTitleEncoding( $s );
1153 return "'" . $this->strencode( $s ) . "'";
1156 public function addIdentifierQuotes( $s ) {
1157 if ( !$this->mFlags & DBO_DDLMODE ) {
1158 $s = '"' . str_replace( '"', '""', $s ) . '"';
1163 private function wrapFieldForWhere( $table, &$col, &$val ) {
1166 $col_info = $this->fieldInfoMulti( $table, $col );
1167 $col_type = $col_info != false ? $col_info->type() : 'CONSTANT';
1168 if ( $col_type == 'CLOB' ) {
1169 $col = 'TO_CHAR(' . $col . ')';
1170 $val = $wgContLang->checkTitleEncoding( $val );
1171 } elseif ( $col_type == 'VARCHAR2' && !mb_check_encoding( $val ) ) {
1172 $val = $wgContLang->checkTitleEncoding( $val );
1176 private function wrapConditionsForWhere ( $table, $conds, $parentCol = null ) {
1178 foreach ( $conds as $col => $val ) {
1179 if ( is_array( $val ) ) {
1180 $conds2[$col] = $this->wrapConditionsForWhere ( $table, $val, $col );
1182 if ( is_numeric( $col ) && $parentCol != null ) {
1183 $this->wrapFieldForWhere ( $table, $parentCol, $val );
1185 $this->wrapFieldForWhere ( $table, $col, $val );
1187 $conds2[$col] = $val;
1193 function selectRow( $table, $vars, $conds, $fname = 'DatabaseOracle::selectRow', $options = array(), $join_conds = array() ) {
1194 if ( is_array($conds) ) {
1195 $conds = $this->wrapConditionsForWhere( $table, $conds );
1197 return parent::selectRow( $table, $vars, $conds, $fname, $options, $join_conds );
1201 * Returns an optional USE INDEX clause to go after the table, and a
1202 * string to go at the end of the query
1206 * @param $options Array: an associative array of options to be turned into
1207 * an SQL query, valid keys are listed in the function.
1210 function makeSelectOptions( $options ) {
1211 $preLimitTail = $postLimitTail = '';
1214 $noKeyOptions = array();
1215 foreach ( $options as $key => $option ) {
1216 if ( is_numeric( $key ) ) {
1217 $noKeyOptions[$option] = true;
1221 if ( isset( $options['GROUP BY'] ) ) {
1222 $preLimitTail .= " GROUP BY {$options['GROUP BY']}";
1224 if ( isset( $options['ORDER BY'] ) ) {
1225 $preLimitTail .= " ORDER BY {$options['ORDER BY']}";
1228 # if ( isset( $noKeyOptions['FOR UPDATE'] ) ) $tailOpts .= ' FOR UPDATE';
1229 # if ( isset( $noKeyOptions['LOCK IN SHARE MODE'] ) ) $tailOpts .= ' LOCK IN SHARE MODE';
1230 if ( isset( $noKeyOptions['DISTINCT'] ) || isset( $noKeyOptions['DISTINCTROW'] ) ) {
1231 $startOpts .= 'DISTINCT';
1234 if ( isset( $options['USE INDEX'] ) && ! is_array( $options['USE INDEX'] ) ) {
1235 $useIndex = $this->useIndexClause( $options['USE INDEX'] );
1240 return array( $startOpts, $useIndex, $preLimitTail, $postLimitTail );
1243 public function delete( $table, $conds, $fname = 'DatabaseOracle::delete' ) {
1244 if ( is_array($conds) ) {
1245 $conds = $this->wrapConditionsForWhere( $table, $conds );
1247 return parent::delete( $table, $conds, $fname );
1250 function update( $table, $values, $conds, $fname = 'DatabaseOracle::update', $options = array() ) {
1253 $table = $this->tableName( $table );
1254 $opts = $this->makeUpdateOptions( $options );
1255 $sql = "UPDATE $opts $table SET ";
1258 foreach ( $values as $col => &$val ) {
1259 $sqlSet = $this->fieldBindStatement( $table, $col, $val, true );
1262 $sqlSet = ', ' . $sqlSet;
1269 if ( $conds != '*' ) {
1270 $conds = $this->wrapConditionsForWhere( $table, $conds );
1271 $sql .= ' WHERE ' . $this->makeList( $conds, LIST_AND );
1274 if ( ( $this->mLastResult = $stmt = oci_parse( $this->mConn, $sql ) ) === false ) {
1275 $e = oci_error( $this->mConn );
1276 $this->reportQueryError( $e['message'], $e['code'], $sql, __METHOD__ );
1279 foreach ( $values as $col => &$val ) {
1280 $col_info = $this->fieldInfoMulti( $table, $col );
1281 $col_type = $col_info != false ? $col_info->type() : 'CONSTANT';
1283 if ( $val === null ) {
1284 // do nothing ... null was inserted in statement creation
1285 } elseif ( $col_type != 'BLOB' && $col_type != 'CLOB' ) {
1286 if ( is_object( $val ) ) {
1287 $val = $val->getData();
1290 if ( preg_match( '/^timestamp.*/i', $col_type ) == 1 && strtolower( $val ) == 'infinity' ) {
1291 $val = '31-12-2030 12:00:00.000000';
1294 $val = ( $wgContLang != null ) ? $wgContLang->checkTitleEncoding( $val ) : $val;
1295 if ( oci_bind_by_name( $stmt, ":$col", $val ) === false ) {
1296 $e = oci_error( $stmt );
1297 $this->reportQueryError( $e['message'], $e['code'], $sql, __METHOD__ );
1301 if ( ( $lob[$col] = oci_new_descriptor( $this->mConn, OCI_D_LOB ) ) === false ) {
1302 $e = oci_error( $stmt );
1303 throw new DBUnexpectedError( $this, "Cannot create LOB descriptor: " . $e['message'] );
1306 if ( $col_type == 'BLOB' ) {
1307 $lob[$col]->writeTemporary( $val );
1308 oci_bind_by_name( $stmt, ":$col", $lob[$col], - 1, SQLT_BLOB );
1310 $lob[$col]->writeTemporary( $val );
1311 oci_bind_by_name( $stmt, ":$col", $lob[$col], - 1, OCI_B_CLOB );
1316 wfSuppressWarnings();
1318 if ( oci_execute( $stmt, $this->execFlags() ) === false ) {
1319 $e = oci_error( $stmt );
1320 if ( !$this->ignore_DUP_VAL_ON_INDEX || $e['code'] != '1' ) {
1321 $this->reportQueryError( $e['message'], $e['code'], $sql, __METHOD__ );
1324 $this->mAffectedRows = oci_num_rows( $stmt );
1327 $this->mAffectedRows = oci_num_rows( $stmt );
1330 wfRestoreWarnings();
1332 if ( isset( $lob ) ) {
1333 foreach ( $lob as $lob_v ) {
1338 if ( !$this->mTrxLevel ) {
1339 oci_commit( $this->mConn );
1342 oci_free_statement( $stmt );
1345 function bitNot( $field ) {
1346 // expecting bit-fields smaller than 4bytes
1347 return 'BITNOT(' . $field . ')';
1350 function bitAnd( $fieldLeft, $fieldRight ) {
1351 return 'BITAND(' . $fieldLeft . ', ' . $fieldRight . ')';
1354 function bitOr( $fieldLeft, $fieldRight ) {
1355 return 'BITOR(' . $fieldLeft . ', ' . $fieldRight . ')';
1358 function setFakeMaster( $enabled = true ) { }
1360 function getDBname() {
1361 return $this->mDBname;
1364 function getServer() {
1365 return $this->mServer;
1368 public function getSearchEngine() {
1369 return 'SearchOracle';
1371 } // end DatabaseOracle class