8 * This is the Oracle database abstraction layer.
14 function __construct( $data ) {
24 * The oci8 extension is fairly weak and doesn't support oci_num_rows, among
25 * other things. We use a wrapper class to handle that and other
26 * Oracle-specific bits, like converting column names back to lowercase.
36 private function array_unique_md( $array_in ) {
38 $array_hashes = array();
40 foreach ( $array_in as $key => $item ) {
41 $hash = md5( serialize( $item ) );
42 if ( !isset( $array_hashes[$hash] ) ) {
43 $array_hashes[$hash] = $hash;
51 function __construct( &$db, $stmt, $unique = false ) {
54 if ( ( $this->nrows
= oci_fetch_all( $stmt, $this->rows
, 0, - 1, OCI_FETCHSTATEMENT_BY_ROW | OCI_NUM
) ) === false ) {
55 $e = oci_error( $stmt );
56 $db->reportQueryError( $e['message'], $e['code'], '', __FUNCTION__
);
61 $this->rows
= $this->array_unique_md( $this->rows
);
62 $this->nrows
= count( $this->rows
);
69 public function free() {
70 oci_free_statement( $this->stmt
);
73 public function seek( $row ) {
74 $this->cursor
= min( $row, $this->nrows
);
77 public function numRows() {
81 public function numFields() {
82 return oci_num_fields( $this->stmt
);
85 public function fetchObject() {
86 if ( $this->cursor
>= $this->nrows
) {
89 $row = $this->rows
[$this->cursor++
];
90 $ret = new stdClass();
91 foreach ( $row as $k => $v ) {
92 $lc = strtolower( oci_field_name( $this->stmt
, $k +
1 ) );
99 public function fetchRow() {
100 if ( $this->cursor
>= $this->nrows
) {
104 $row = $this->rows
[$this->cursor++
];
106 foreach ( $row as $k => $v ) {
107 $lc = strtolower( oci_field_name( $this->stmt
, $k +
1 ) );
120 private $name, $tablename, $default, $max_length, $nullable,
121 $is_pk, $is_unique, $is_multiple, $is_key, $type;
123 function __construct( $info ) {
124 $this->name
= $info['column_name'];
125 $this->tablename
= $info['table_name'];
126 $this->default = $info['data_default'];
127 $this->max_length
= $info['data_length'];
128 $this->nullable
= $info['not_null'];
129 $this->is_pk
= isset( $info['prim'] ) && $info['prim'] == 1 ?
1 : 0;
130 $this->is_unique
= isset( $info['uniq'] ) && $info['uniq'] == 1 ?
1 : 0;
131 $this->is_multiple
= isset( $info['nonuniq'] ) && $info['nonuniq'] == 1 ?
1 : 0;
132 $this->is_key
= ( $this->is_pk ||
$this->is_unique ||
$this->is_multiple
);
133 $this->type
= $info['data_type'];
140 function tableName() {
141 return $this->tablename
;
144 function defaultValue() {
145 return $this->default;
148 function maxLength() {
149 return $this->max_length
;
152 function nullable() {
153 return $this->nullable
;
157 return $this->is_key
;
160 function isMultipleKey() {
161 return $this->is_multiple
;
172 class DatabaseOracle
extends DatabaseBase
{
173 var $mInsertId = null;
174 var $mLastResult = null;
175 var $numeric_version = null;
176 var $lastResult = null;
180 var $ignore_DUP_VAL_ON_INDEX = false;
181 var $sequenceData = null;
183 var $defaultCharset = 'AL32UTF8';
185 var $mFieldInfoCache = array();
187 function __construct( $server = false, $user = false, $password = false, $dbName = false,
188 $failFunction = false, $flags = 0, $tablePrefix = 'get from global' )
190 $tablePrefix = $tablePrefix == 'get from global' ?
$tablePrefix : strtoupper( $tablePrefix );
191 parent
::__construct( $server, $user, $password, $dbName, $failFunction, $flags, $tablePrefix );
192 wfRunHooks( 'DatabaseOraclePostInit', array( &$this ) );
199 function cascadingDeletes() {
202 function cleanupTriggers() {
205 function strictIPs() {
208 function realTimestamps() {
211 function implicitGroupby() {
214 function implicitOrderby() {
217 function searchableIPs() {
221 static function newFromParams( $server, $user, $password, $dbName, $failFunction = false, $flags = 0 )
223 return new DatabaseOracle( $server, $user, $password, $dbName, $failFunction, $flags );
227 * Usually aborts on failure
228 * If the failFunction is set to a non-zero integer, returns success
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" );
236 $this->mServer
= $server;
237 $this->mUser
= $user;
238 $this->mPassword
= $password;
239 $this->mDBname
= $dbName;
241 if ( !strlen( $user ) ) { # e.g. the class is being loaded
245 $session_mode = $this->mFlags
& DBO_SYSDBA ? OCI_SYSDBA
: OCI_DEFAULT
;
246 if ( $this->mFlags
& DBO_DEFAULT
) {
247 $this->mConn
= oci_new_connect( $user, $password, $dbName, $this->defaultCharset
, $session_mode );
249 $this->mConn
= oci_connect( $user, $password, $dbName, $this->defaultCharset
, $session_mode );
252 if ( $this->mConn
== false ) {
253 wfDebug( "DB connection error\n" );
254 wfDebug( "Server: $server, Database: $dbName, User: $user, Password: " . substr( $password, 0, 3 ) . "...\n" );
255 wfDebug( $this->lastError() . "\n" );
259 $this->mOpened
= true;
261 # removed putenv calls because they interfere with the system globaly
262 $this->doQuery( 'ALTER SESSION SET NLS_TIMESTAMP_FORMAT=\'DD-MM-YYYY HH24:MI:SS.FF6\'' );
263 $this->doQuery( 'ALTER SESSION SET NLS_TIMESTAMP_TZ_FORMAT=\'DD-MM-YYYY HH24:MI:SS.FF6\'' );
268 * Closes a database connection, if it is open
269 * Returns success, true if already closed
272 $this->mOpened
= false;
273 if ( $this->mConn
) {
274 return oci_close( $this->mConn
);
280 function execFlags() {
281 return $this->mTrxLevel ? OCI_DEFAULT
: OCI_COMMIT_ON_SUCCESS
;
284 function doQuery( $sql ) {
285 wfDebug( "SQL: [$sql]\n" );
286 if ( !mb_check_encoding( $sql ) ) {
287 throw new MWException( "SQL encoding is invalid\n$sql" );
290 // handle some oracle specifics
291 // remove AS column/table/subquery namings
292 if ( !defined( 'MEDIAWIKI_INSTALL' ) ) {
293 $sql = preg_replace( '/ as /i', ' ', $sql );
295 // Oracle has issues with UNION clause if the statement includes LOB fields
296 // So we do a UNION ALL and then filter the results array with array_unique
297 $union_unique = ( preg_match( '/\/\* UNION_UNIQUE \*\/ /', $sql ) != 0 );
298 // EXPLAIN syntax in Oracle is EXPLAIN PLAN FOR and it return nothing
299 // you have to select data from plan table after explain
300 $olderr = error_reporting( E_ERROR
);
301 $explain_id = date( 'dmYHis' );
302 error_reporting( $olderr );
304 $sql = preg_replace( '/^EXPLAIN /', 'EXPLAIN PLAN SET STATEMENT_ID = \'' . $explain_id . '\' FOR', $sql, 1, $explain_count );
307 $olderr = error_reporting( E_ERROR
);
309 if ( ( $this->mLastResult
= $stmt = oci_parse( $this->mConn
, $sql ) ) === false ) {
310 $e = oci_error( $this->mConn
);
311 $this->reportQueryError( $e['message'], $e['code'], $sql, __FUNCTION__
);
314 $olderr = error_reporting( E_ERROR
);
315 if ( oci_execute( $stmt, $this->execFlags() ) == false ) {
316 $e = oci_error( $stmt );
317 if ( !$this->ignore_DUP_VAL_ON_INDEX ||
$e['code'] != '1' ) {
318 $this->reportQueryError( $e['message'], $e['code'], $sql, __FUNCTION__
);
321 error_reporting( $olderr );
323 if ( $explain_count > 0 ) {
324 return $this->doQuery( 'SELECT id, cardinality "ROWS" FROM plan_table WHERE statement_id = \'' . $explain_id . '\'' );
325 } elseif ( oci_statement_type( $stmt ) == 'SELECT' ) {
326 return new ORAResult( $this, $stmt, $union_unique );
328 $this->mAffectedRows
= oci_num_rows( $stmt );
333 function queryIgnore( $sql, $fname = '' ) {
334 return $this->query( $sql, $fname, true );
337 function freeResult( $res ) {
338 if ( $res instanceof ORAResult
) {
341 $res->result
->free();
345 function fetchObject( $res ) {
346 if ( $res instanceof ORAResult
) {
347 return $res->numRows();
349 return $res->result
->fetchObject();
353 function fetchRow( $res ) {
354 if ( $res instanceof ORAResult
) {
355 return $res->fetchRow();
357 return $res->result
->fetchRow();
361 function numRows( $res ) {
362 if ( $res instanceof ORAResult
) {
363 return $res->numRows();
365 return $res->result
->numRows();
369 function numFields( $res ) {
370 if ( $res instanceof ORAResult
) {
371 return $res->numFields();
373 return $res->result
->numFields();
377 function fieldName( $stmt, $n ) {
378 return oci_field_name( $stmt, $n );
382 * This must be called after nextSequenceVal
384 function insertId() {
385 return $this->mInsertId
;
388 function dataSeek( $res, $row ) {
389 if ( $res instanceof ORAResult
) {
392 $res->result
->seek( $row );
396 function lastError() {
397 if ( $this->mConn
=== false ) {
400 $e = oci_error( $this->mConn
);
402 return $e['message'];
405 function lastErrno() {
406 if ( $this->mConn
=== false ) {
409 $e = oci_error( $this->mConn
);
414 function affectedRows() {
415 return $this->mAffectedRows
;
419 * Returns information about an index
420 * If errors are explicitly ignored, returns NULL on failure
422 function indexInfo( $table, $index, $fname = 'DatabaseOracle::indexExists' ) {
426 function indexUnique( $table, $index, $fname = 'DatabaseOracle::indexUnique' ) {
430 function insert( $table, $a, $fname = 'DatabaseOracle::insert', $options = array() ) {
431 if ( !count( $a ) ) {
435 if ( !is_array( $options ) ) {
436 $options = array( $options );
439 if ( in_array( 'IGNORE', $options ) ) {
440 $this->ignore_DUP_VAL_ON_INDEX
= true;
443 if ( !is_array( reset( $a ) ) ) {
447 foreach ( $a as &$row ) {
448 $this->insertOneRow( $table, $row, $fname );
452 if ( in_array( 'IGNORE', $options ) ) {
453 $this->ignore_DUP_VAL_ON_INDEX
= false;
459 function insertOneRow( $table, $row, $fname ) {
462 // "INSERT INTO tables (a, b, c)"
463 $sql = "INSERT INTO " . $this->tableName( $table ) . " (" . join( ',', array_keys( $row ) ) . ')';
466 // for each value, append ":key"
468 foreach ( $row as $col => $val ) {
470 $sql .= $val !== null ?
':' . $col : 'NULL';
472 $sql .= $val !== null ?
', :' . $col : ', NULL';
479 $stmt = oci_parse( $this->mConn
, $sql );
480 foreach ( $row as $col => &$val ) {
481 $col_type = $this->fieldInfo( $this->tableName( $table ), $col )->type();
483 if ( $val === null ) {
484 // do nothing ... null was inserted in statement creation
485 } elseif ( $col_type != 'BLOB' && $col_type != 'CLOB' ) {
486 if ( is_object( $val ) ) {
487 $val = $val->getData();
490 if ( preg_match( '/^timestamp.*/i', $col_type ) == 1 && strtolower( $val ) == 'infinity' ) {
491 $val = '31-12-2030 12:00:00.000000';
494 $val = ( $wgLang != null ) ?
$wgLang->checkTitleEncoding( $val ) : $val;
495 if ( oci_bind_by_name( $stmt, ":$col", $val ) === false ) {
496 $this->reportQueryError( $this->lastErrno(), $this->lastError(), $sql, __METHOD__
);
499 if ( ( $lob[$col] = oci_new_descriptor( $this->mConn
, OCI_D_LOB
) ) === false ) {
500 $e = oci_error( $stmt );
501 throw new DBUnexpectedError( $this, "Cannot create LOB descriptor: " . $e['message'] );
504 if ( $col_type == 'BLOB' ) { // is_object($val)) {
505 $lob[$col]->writeTemporary( $val ); // ->getData());
506 oci_bind_by_name( $stmt, ":$col", $lob[$col], - 1, SQLT_BLOB
);
508 $lob[$col]->writeTemporary( $val );
509 oci_bind_by_name( $stmt, ":$col", $lob[$col], - 1, OCI_B_CLOB
);
514 $olderr = error_reporting( E_ERROR
);
515 if ( oci_execute( $stmt, OCI_DEFAULT
) === false ) {
516 $e = oci_error( $stmt );
518 if ( !$this->ignore_DUP_VAL_ON_INDEX ||
$e['code'] != '1' ) {
519 $this->reportQueryError( $e['message'], $e['code'], $sql, __METHOD__
);
521 $this->mAffectedRows
= oci_num_rows( $stmt );
524 $this->mAffectedRows
= oci_num_rows( $stmt );
526 error_reporting( $olderr );
528 if ( isset( $lob ) ) {
529 foreach ( $lob as $lob_i => $lob_v ) {
534 if ( !$this->mTrxLevel
) {
535 oci_commit( $this->mConn
);
538 oci_free_statement( $stmt );
541 function insertSelect( $destTable, $srcTable, $varMap, $conds, $fname = 'DatabaseOracle::insertSelect',
542 $insertOptions = array(), $selectOptions = array() )
544 $destTable = $this->tableName( $destTable );
545 if ( !is_array( $selectOptions ) ) {
546 $selectOptions = array( $selectOptions );
548 list( $startOpts, $useIndex, $tailOpts ) = $this->makeSelectOptions( $selectOptions );
549 if ( is_array( $srcTable ) ) {
550 $srcTable = implode( ',', array_map( array( &$this, 'tableName' ), $srcTable ) );
552 $srcTable = $this->tableName( $srcTable );
555 if ( ( $sequenceData = $this->getSequenceData( $destTable ) ) !== false &&
556 !isset( $varMap[$sequenceData['column']] ) )
557 $varMap[$sequenceData['column']] = 'GET_SEQUENCE_VALUE(\'' . $sequenceData['sequence'] . '\')';
559 // count-alias subselect fields to avoid abigious definition errors
561 foreach ( $varMap as $key => &$val ) {
562 $val = $val . ' field' . ( $i++
);
565 $sql = "INSERT INTO $destTable (" . implode( ',', array_keys( $varMap ) ) . ')' .
566 " SELECT $startOpts " . implode( ',', $varMap ) .
567 " FROM $srcTable $useIndex ";
568 if ( $conds != '*' ) {
569 $sql .= ' WHERE ' . $this->makeList( $conds, LIST_AND
);
571 $sql .= " $tailOpts";
573 if ( in_array( 'IGNORE', $insertOptions ) ) {
574 $this->ignore_DUP_VAL_ON_INDEX
= true;
577 $retval = $this->query( $sql, $fname );
579 if ( in_array( 'IGNORE', $insertOptions ) ) {
580 $this->ignore_DUP_VAL_ON_INDEX
= false;
586 function tableName( $name ) {
587 global $wgSharedDB, $wgSharedPrefix, $wgSharedTables;
589 Replace reserved words with better ones
590 Using uppercase because that's the only way Oracle can handle
598 $name = 'PAGECONTENT';
603 The rest of procedure is equal to generic Databse class
604 except for the quoting style
606 if ( $name[0] == '"' && substr( $name, - 1, 1 ) == '"' ) {
610 if ( preg_match( '/(^|\s)(DISTINCT|JOIN|ON|AS)(\s|$)/i', $name ) !== 0 ) {
613 $dbDetails = array_reverse( explode( '.', $name, 2 ) );
614 if ( isset( $dbDetails[1] ) ) {
615 @list
( $table, $database ) = $dbDetails;
617 @list
( $table ) = $dbDetails;
620 $prefix = $this->mTablePrefix
;
622 if ( isset( $database ) ) {
623 $table = ( $table[0] == '`' ?
$table : "`{$table}`" );
626 if ( !isset( $database ) && isset( $wgSharedDB ) && $table[0] != '"'
627 && isset( $wgSharedTables )
628 && is_array( $wgSharedTables )
629 && in_array( $table, $wgSharedTables )
631 $database = $wgSharedDB;
632 $prefix = isset( $wgSharedPrefix ) ?
$wgSharedPrefix : $prefix;
635 if ( isset( $database ) ) {
636 $database = ( $database[0] == '"' ?
$database : "\"{$database}\"" );
638 $table = ( $table[0] == '"' ?
$table : "\"{$prefix}{$table}\"" );
640 $tableName = ( isset( $database ) ?
"{$database}.{$table}" : "{$table}" );
642 return strtoupper( $tableName );
646 * Return the next in a sequence, save the value for retrieval via insertId()
648 function nextSequenceValue( $seqName ) {
649 $res = $this->query( "SELECT $seqName.nextval FROM dual" );
650 $row = $this->fetchRow( $res );
651 $this->mInsertId
= $row[0];
652 $this->freeResult( $res );
653 return $this->mInsertId
;
657 * Return sequence_name if table has a sequence
659 function getSequenceData( $table ) {
660 if ( $this->sequenceData
== null ) {
661 $result = $this->query( "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'" );
663 while ( ( $row = $result->fetchRow() ) !== false ) {
664 $this->sequenceData
[$this->tableName( $row[1] )] = array(
665 'sequence' => $row[0],
671 return ( isset( $this->sequenceData
[$table] ) ) ?
$this->sequenceData
[$table] : false;
674 # REPLACE query wrapper
675 # Oracle simulates this with a DELETE followed by INSERT
676 # $row is the row to insert, an associative array
677 # $uniqueIndexes is an array of indexes. Each element may be either a
678 # field name or an array of field names
680 # It may be more efficient to leave off unique indexes which are unlikely to collide.
681 # However if you do this, you run the risk of encountering errors which wouldn't have
683 function replace( $table, $uniqueIndexes, $rows, $fname = 'DatabaseOracle::replace' ) {
684 $table = $this->tableName( $table );
686 if ( count( $rows ) == 0 ) {
691 if ( !is_array( reset( $rows ) ) ) {
692 $rows = array( $rows );
695 $sequenceData = $this->getSequenceData( $table );
697 foreach ( $rows as $row ) {
698 # Delete rows which collide
699 if ( $uniqueIndexes ) {
700 $condsDelete = array();
701 foreach ( $uniqueIndexes as $index )
702 $condsDelete[$index] = $row[$index];
703 if (count($condsDelete) > 0) {
704 $this->delete( $table, $condsDelete, $fname );
708 if ( $sequenceData !== false && !isset( $row[$sequenceData['column']] ) ) {
709 $row[$sequenceData['column']] = $this->nextSequenceValue( $sequenceData['sequence'] );
713 $this->insert( $table, $row, $fname );
717 # DELETE where the condition is a join
718 function deleteJoin( $delTable, $joinTable, $delVar, $joinVar, $conds, $fname = "DatabaseOracle::deleteJoin" ) {
720 throw new DBUnexpectedError( $this, 'DatabaseOracle::deleteJoin() called with empty $conds' );
723 $delTable = $this->tableName( $delTable );
724 $joinTable = $this->tableName( $joinTable );
725 $sql = "DELETE FROM $delTable WHERE $delVar IN (SELECT $joinVar FROM $joinTable ";
726 if ( $conds != '*' ) {
727 $sql .= 'WHERE ' . $this->makeList( $conds, LIST_AND
);
731 $this->query( $sql, $fname );
734 # Returns the size of a text field, or -1 for "unlimited"
735 function textFieldSize( $table, $field ) {
736 $table = $this->tableName( $table );
737 $sql = "SELECT t.typname as ftype,a.atttypmod as size
738 FROM pg_class c, pg_attribute a, pg_type t
739 WHERE relname='$table' AND a.attrelid=c.oid AND
740 a.atttypid=t.oid and a.attname='$field'";
741 $res = $this->query( $sql );
742 $row = $this->fetchObject( $res );
743 if ( $row->ftype
== "varchar" ) {
744 $size = $row->size
- 4;
748 $this->freeResult( $res );
752 function limitResult( $sql, $limit, $offset = false ) {
753 if ( $offset === false ) {
756 return "SELECT * FROM ($sql) WHERE rownum >= (1 + $offset) AND rownum < (1 + $limit + $offset)";
760 function unionQueries( $sqls, $all ) {
761 $glue = ' UNION ALL ';
762 return 'SELECT * ' . ( $all ?
'':'/* UNION_UNIQUE */ ' ) . 'FROM (' . implode( $glue, $sqls ) . ')' ;
765 function wasDeadlock() {
766 return $this->lastErrno() == 'OCI-00060';
770 function duplicateTableStructure( $oldName, $newName, $temporary = false, $fname = 'DatabaseOracle::duplicateTableStructure' ) {
771 $temporary = $temporary ?
'TRUE' : 'FALSE';
772 return $this->query( 'BEGIN DUPLICATE_TABLE(\'' . $oldName . '\', \'' . $newName . '\', ' . $temporary . '); END;', $fname );
775 function timestamp( $ts = 0 ) {
776 return wfTimestamp( TS_ORACLE
, $ts );
780 * Return aggregated value function call
782 function aggregateValue ( $valuedata, $valuename = 'value' ) {
786 function reportQueryError( $error, $errno, $sql, $fname, $tempIgnore = false ) {
787 # Ignore errors during error handling to avoid infinite
789 $ignore = $this->ignoreErrors( true );
790 ++
$this->mErrorCount
;
792 if ( $ignore ||
$tempIgnore ) {
793 wfDebug( "SQL ERROR (ignored): $error\n" );
794 $this->ignoreErrors( $ignore );
796 throw new DBQueryError( $this, $error, $errno, $sql, $fname );
801 * @return string wikitext of a link to the server software's web site
803 function getSoftwareLink() {
804 return '[http://www.oracle.com/ Oracle]';
808 * @return string Version information from the database
810 function getServerVersion() {
811 return oci_server_version( $this->mConn
);
815 * Query whether a given table exists (in the given schema, or the default mw one if not given)
817 function tableExists( $table ) {
818 $SQL = "SELECT 1 FROM user_tables WHERE table_name='$table'";
819 $res = $this->doQuery( $SQL );
821 $count = $res->numRows();
830 * Query whether a given column exists in the mediawiki schema
831 * based on prebuilt table to simulate MySQL field info and keep query speed minimal
833 function fieldExists( $table, $field, $fname = 'DatabaseOracle::fieldExists' ) {
834 $table = trim( $table, '"' );
836 if (isset($this->mFieldInfoCache
[$table.'.'.$field])) {
838 } elseif ( !isset( $this->fieldInfo_stmt
) ) {
839 $this->fieldInfo_stmt
= oci_parse( $this->mConn
, 'SELECT * FROM wiki_field_info_full WHERE table_name = upper(:tab) and column_name = UPPER(:col)' );
842 oci_bind_by_name( $this->fieldInfo_stmt
, ':tab', trim( $table, '"' ) );
843 oci_bind_by_name( $this->fieldInfo_stmt
, ':col', $field );
845 if ( oci_execute( $this->fieldInfo_stmt
, OCI_DEFAULT
) === false ) {
846 $e = oci_error( $this->fieldInfo_stmt
);
847 $this->reportQueryError( $e['message'], $e['code'], 'fieldInfo QUERY', __METHOD__
);
850 $res = new ORAResult( $this, $this->fieldInfo_stmt
);
851 if ($res->numRows() != 0) {
852 $this->mFieldInfoCache
[$table.'.'.$field] = new ORAField( $res->fetchRow() );
859 function fieldInfo( $table, $field ) {
860 $table = trim( $table, '"' );
862 if (isset($this->mFieldInfoCache
[$table.'.'.$field])) {
863 return $this->mFieldInfoCache
[$table.'.'.$field];
864 } elseif ( !isset( $this->fieldInfo_stmt
) ) {
865 $this->fieldInfo_stmt
= oci_parse( $this->mConn
, 'SELECT * FROM wiki_field_info_full WHERE table_name = upper(:tab) and column_name = UPPER(:col)' );
868 oci_bind_by_name( $this->fieldInfo_stmt
, ':tab', $table );
869 oci_bind_by_name( $this->fieldInfo_stmt
, ':col', $field );
871 if ( oci_execute( $this->fieldInfo_stmt
, OCI_DEFAULT
) === false ) {
872 $e = oci_error( $this->fieldInfo_stmt
);
873 $this->reportQueryError( $e['message'], $e['code'], 'fieldInfo QUERY', __METHOD__
);
876 $res = new ORAResult( $this, $this->fieldInfo_stmt
);
877 $this->mFieldInfoCache
[$table.'.'.$field] = new ORAField( $res->fetchRow() );
878 return $this->mFieldInfoCache
[$table.'.'.$field];
881 function begin( $fname = '' ) {
882 $this->mTrxLevel
= 1;
885 function immediateCommit( $fname = '' ) {
889 function commit( $fname = '' ) {
890 oci_commit( $this->mConn
);
891 $this->mTrxLevel
= 0;
894 /* Not even sure why this is used in the main codebase... */
895 function limitResultForUpdate( $sql, $num ) {
899 /* defines must comply with ^define\s*([^\s=]*)\s*=\s?'\{\$([^\}]*)\}'; */
900 function sourceStream( $fp, $lineCallback = false, $resultCallback = false ) {
903 $dollarquote = false;
905 $replacements = array();
907 while ( ! feof( $fp ) ) {
908 if ( $lineCallback ) {
909 call_user_func( $lineCallback );
911 $line = trim( fgets( $fp, 1024 ) );
912 $sl = strlen( $line ) - 1;
917 if ( '-' == $line { 0 } && '-' == $line { 1 } ) {
921 // Allow dollar quoting for function declarations
922 if ( substr( $line, 0, 8 ) == '/*$mw$*/' ) {
923 if ( $dollarquote ) {
924 $dollarquote = false;
929 } elseif ( !$dollarquote ) {
930 if ( ';' == $line { $sl } && ( $sl < 2 ||
';' != $line { $sl - 1 } ) ) {
932 $line = substr( $line, 0, $sl );
942 $cmd = str_replace( ';;', ";", $cmd );
943 if ( strtolower( substr( $cmd, 0, 6 ) ) == 'define' ) {
944 if ( preg_match( '/^define\s*([^\s=]*)\s*=\s*\'\{\$([^\}]*)\}\'/', $cmd, $defines ) ) {
945 $replacements[$defines[2]] = $defines[1];
948 foreach ( $replacements as $mwVar => $scVar ) {
949 $cmd = str_replace( '&' . $scVar . '.', '{$' . $mwVar . '}', $cmd );
952 $cmd = $this->replaceVars( $cmd );
953 $res = $this->query( $cmd, __METHOD__
);
954 if ( $resultCallback ) {
955 call_user_func( $resultCallback, $res, $this );
958 if ( false === $res ) {
959 $err = $this->lastError();
960 return "Query \"{$cmd}\" failed with error code \"$err\".\n";
971 function setup_database() {
972 global $wgVersion, $wgDBmwschema, $wgDBts2schema, $wgDBport, $wgDBuser;
974 echo "<li>Creating DB objects</li>\n";
975 $res = $this->sourceFile( "../maintenance/ora/tables.sql" );
977 // Avoid the non-standard "REPLACE INTO" syntax
978 echo "<li>Populating table interwiki</li>\n";
979 $f = fopen( "../maintenance/interwiki.sql", 'r' );
981 dieout( "<li>Could not find the interwiki.sql file</li>" );
984 // do it like the postgres :D
985 $SQL = "INSERT INTO interwiki(iw_prefix,iw_url,iw_local) VALUES ";
986 while ( !feof( $f ) ) {
987 $line = fgets( $f, 1024 );
989 if ( !preg_match( '/^\s*(\(.+?),(\d)\)/', $line, $matches ) ) {
992 $this->query( "$SQL $matches[1],$matches[2])" );
995 echo "<li>Table interwiki successfully populated</li>\n";
998 function strencode( $s ) {
999 return str_replace( "'", "''", $s );
1003 function encodeBlob($b) {
1004 return $b; //new ORABlob($b);
1006 function decodeBlob($b) {
1007 return $b; //return $b->load();
1010 function addQuotes( $s ) {
1012 if ( isset( $wgLang->mLoaded
) && $wgLang->mLoaded
) {
1013 $s = $wgLang->checkTitleEncoding( $s );
1015 return "'" . $this->strencode( $s ) . "'";
1018 function quote_ident( $s ) {
1022 function selectRow( $table, $vars, $conds, $fname = 'DatabaseOracle::selectRow', $options = array(), $join_conds = array() ) {
1026 foreach ( $conds as $col => $val ) {
1027 $col_type = $this->fieldInfo( $this->tableName( $table ), $col )->type();
1028 if ( $col_type == 'CLOB' ) {
1029 $conds2['TO_CHAR(' . $col . ')'] = $wgLang->checkTitleEncoding( $val );
1030 } elseif ( $col_type == 'VARCHAR2' && !mb_check_encoding( $val ) ) {
1031 $conds2[$col] = $wgLang->checkTitleEncoding( $val );
1033 $conds2[$col] = $val;
1037 if ( is_array( $table ) ) {
1038 foreach ( $table as $tab ) {
1039 $tab = $this->tableName( $tab );
1042 $table = $this->tableName( $table );
1045 return parent
::selectRow( $table, $vars, $conds2, $fname, $options, $join_conds );
1049 * Returns an optional USE INDEX clause to go after the table, and a
1050 * string to go at the end of the query
1054 * @param $options Array: an associative array of options to be turned into
1055 * an SQL query, valid keys are listed in the function.
1058 function makeSelectOptions( $options ) {
1059 $preLimitTail = $postLimitTail = '';
1062 $noKeyOptions = array();
1063 foreach ( $options as $key => $option ) {
1064 if ( is_numeric( $key ) ) {
1065 $noKeyOptions[$option] = true;
1069 if ( isset( $options['GROUP BY'] ) ) {
1070 $preLimitTail .= " GROUP BY {$options['GROUP BY']}";
1072 if ( isset( $options['ORDER BY'] ) ) {
1073 $preLimitTail .= " ORDER BY {$options['ORDER BY']}";
1076 # if ( isset( $noKeyOptions['FOR UPDATE'] ) ) $tailOpts .= ' FOR UPDATE';
1077 # if ( isset( $noKeyOptions['LOCK IN SHARE MODE'] ) ) $tailOpts .= ' LOCK IN SHARE MODE';
1078 if ( isset( $noKeyOptions['DISTINCT'] ) ||
isset( $noKeyOptions['DISTINCTROW'] ) ) {
1079 $startOpts .= 'DISTINCT';
1082 if ( isset( $options['USE INDEX'] ) && ! is_array( $options['USE INDEX'] ) ) {
1083 $useIndex = $this->useIndexClause( $options['USE INDEX'] );
1088 return array( $startOpts, $useIndex, $preLimitTail, $postLimitTail );
1091 public function delete( $table, $conds, $fname = 'DatabaseOracle::delete' ) {
1094 if ( $wgLang != null ) {
1096 foreach ( $conds as $col => $val ) {
1097 $col_type = $this->fieldInfo( $this->tableName( $table ), $col )->type();
1098 if ( $col_type == 'CLOB' ) {
1099 $conds2['TO_CHAR(' . $col . ')'] = $wgLang->checkTitleEncoding( $val );
1101 if ( is_array( $val ) ) {
1102 $conds2[$col] = $val;
1103 foreach ( $conds2[$col] as &$val2 ) {
1104 $val2 = $wgLang->checkTitleEncoding( $val2 );
1107 $conds2[$col] = $wgLang->checkTitleEncoding( $val );
1112 return parent
::delete( $table, $conds2, $fname );
1114 return parent
::delete( $table, $conds, $fname );
1118 function bitNot( $field ) {
1119 // expecting bit-fields smaller than 4bytes
1120 return 'BITNOT(' . $bitField . ')';
1123 function bitAnd( $fieldLeft, $fieldRight ) {
1124 return 'BITAND(' . $fieldLeft . ', ' . $fieldRight . ')';
1127 function bitOr( $fieldLeft, $fieldRight ) {
1128 return 'BITOR(' . $fieldLeft . ', ' . $fieldRight . ')';
1132 * How lagged is this slave?
1136 public function getLag() {
1137 # Not implemented for Oracle
1141 function setFakeSlaveLag( $lag ) { }
1142 function setFakeMaster( $enabled = true ) { }
1144 function getDBname() {
1145 return $this->mDBname
;
1148 function getServer() {
1149 return $this->mServer
;
1152 public function replaceVars( $ins ) {
1153 $varnames = array( 'wgDBprefix' );
1154 if ( $this->mFlags
& DBO_SYSDBA
) {
1155 $varnames[] = 'wgDBOracleDefTS';
1156 $varnames[] = 'wgDBOracleTempTS';
1159 // Ordinary variables
1160 foreach ( $varnames as $var ) {
1161 if ( isset( $GLOBALS[$var] ) ) {
1162 $val = addslashes( $GLOBALS[$var] ); // FIXME: safety check?
1163 $ins = str_replace( '{$' . $var . '}', $val, $ins );
1164 $ins = str_replace( '/*$' . $var . '*/`', '`' . $val, $ins );
1165 $ins = str_replace( '/*$' . $var . '*/', $val, $ins );
1169 return parent
::replaceVars( $ins );
1172 public function getSearchEngine() {
1173 return 'SearchOracle';
1175 } // end DatabaseOracle class