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 function __construct( $server = false, $user = false, $password = false, $dbName = false,
186 $failFunction = false, $flags = 0, $tablePrefix = 'get from global' )
188 $tablePrefix = $tablePrefix == 'get from global' ?
$tablePrefix : strtoupper( $tablePrefix );
189 parent
::__construct( $server, $user, $password, $dbName, $failFunction, $flags, $tablePrefix );
190 wfRunHooks( 'DatabaseOraclePostInit', array( &$this ) );
193 function cascadingDeletes() {
196 function cleanupTriggers() {
199 function strictIPs() {
202 function realTimestamps() {
205 function implicitGroupby() {
208 function implicitOrderby() {
211 function searchableIPs() {
215 static function newFromParams( $server, $user, $password, $dbName, $failFunction = false, $flags = 0 )
217 return new DatabaseOracle( $server, $user, $password, $dbName, $failFunction, $flags );
221 * Usually aborts on failure
222 * If the failFunction is set to a non-zero integer, returns success
224 function open( $server, $user, $password, $dbName ) {
225 if ( !function_exists( 'oci_connect' ) ) {
226 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" );
230 $this->mServer
= $server;
231 $this->mUser
= $user;
232 $this->mPassword
= $password;
233 $this->mDBname
= $dbName;
235 if ( !strlen( $user ) ) { # e.g. the class is being loaded
239 $session_mode = $this->mFlags
& DBO_SYSDBA ? OCI_SYSDBA
: OCI_DEFAULT
;
240 if ( $this->mFlags
& DBO_DEFAULT
) {
241 $this->mConn
= oci_new_connect( $user, $password, $dbName, $this->defaultCharset
, $session_mode );
243 $this->mConn
= oci_connect( $user, $password, $dbName, $this->defaultCharset
, $session_mode );
246 if ( $this->mConn
== false ) {
247 wfDebug( "DB connection error\n" );
248 wfDebug( "Server: $server, Database: $dbName, User: $user, Password: " . substr( $password, 0, 3 ) . "...\n" );
249 wfDebug( $this->lastError() . "\n" );
253 $this->mOpened
= true;
255 # removed putenv calls because they interfere with the system globaly
256 $this->doQuery( 'ALTER SESSION SET NLS_TIMESTAMP_FORMAT=\'DD-MM-YYYY HH24:MI:SS.FF6\'' );
257 $this->doQuery( 'ALTER SESSION SET NLS_TIMESTAMP_TZ_FORMAT=\'DD-MM-YYYY HH24:MI:SS.FF6\'' );
262 * Closes a database connection, if it is open
263 * Returns success, true if already closed
266 $this->mOpened
= false;
267 if ( $this->mConn
) {
268 return oci_close( $this->mConn
);
274 function execFlags() {
275 return $this->mTrxLevel ? OCI_DEFAULT
: OCI_COMMIT_ON_SUCCESS
;
278 function doQuery( $sql ) {
279 wfDebug( "SQL: [$sql]\n" );
280 if ( !mb_check_encoding( $sql ) ) {
281 throw new MWException( "SQL encoding is invalid\n$sql" );
284 // handle some oracle specifics
285 // remove AS column/table/subquery namings
286 if ( !defined( 'MEDIAWIKI_INSTALL' ) ) {
287 $sql = preg_replace( '/ as /i', ' ', $sql );
289 // Oracle has issues with UNION clause if the statement includes LOB fields
290 // So we do a UNION ALL and then filter the results array with array_unique
291 $union_unique = ( preg_match( '/\/\* UNION_UNIQUE \*\/ /', $sql ) != 0 );
292 // EXPLAIN syntax in Oracle is EXPLAIN PLAN FOR and it return nothing
293 // you have to select data from plan table after explain
294 $olderr = error_reporting( E_ERROR
);
295 $explain_id = date( 'dmYHis' );
296 error_reporting( $olderr );
298 $sql = preg_replace( '/^EXPLAIN /', 'EXPLAIN PLAN SET STATEMENT_ID = \'' . $explain_id . '\' FOR', $sql, 1, $explain_count );
301 $olderr = error_reporting( E_ERROR
);
303 if ( ( $this->mLastResult
= $stmt = oci_parse( $this->mConn
, $sql ) ) === false ) {
304 $e = oci_error( $this->mConn
);
305 $this->reportQueryError( $e['message'], $e['code'], $sql, __FUNCTION__
);
308 $olderr = error_reporting( E_ERROR
);
309 if ( oci_execute( $stmt, $this->execFlags() ) == false ) {
310 $e = oci_error( $stmt );
311 if ( !$this->ignore_DUP_VAL_ON_INDEX ||
$e['code'] != '1' ) {
312 $this->reportQueryError( $e['message'], $e['code'], $sql, __FUNCTION__
);
315 error_reporting( $olderr );
317 if ( $explain_count > 0 ) {
318 return $this->doQuery( 'SELECT id, cardinality "ROWS" FROM plan_table WHERE statement_id = \'' . $explain_id . '\'' );
319 } elseif ( oci_statement_type( $stmt ) == 'SELECT' ) {
320 return new ORAResult( $this, $stmt, $union_unique );
322 $this->mAffectedRows
= oci_num_rows( $stmt );
327 function queryIgnore( $sql, $fname = '' ) {
328 return $this->query( $sql, $fname, true );
331 function freeResult( $res ) {
332 if ( $res instanceof ORAResult
) {
335 $res->result
->free();
339 function fetchObject( $res ) {
340 if ( $res instanceof ORAResult
) {
341 return $res->numRows();
343 return $res->result
->fetchObject();
347 function fetchRow( $res ) {
348 if ( $res instanceof ORAResult
) {
349 return $res->fetchRow();
351 return $res->result
->fetchRow();
355 function numRows( $res ) {
356 if ( $res instanceof ORAResult
) {
357 return $res->numRows();
359 return $res->result
->numRows();
363 function numFields( $res ) {
364 if ( $res instanceof ORAResult
) {
365 return $res->numFields();
367 return $res->result
->numFields();
371 function fieldName( $stmt, $n ) {
372 return oci_field_name( $stmt, $n );
376 * This must be called after nextSequenceVal
378 function insertId() {
379 return $this->mInsertId
;
382 function dataSeek( $res, $row ) {
383 if ( $res instanceof ORAResult
) {
386 $res->result
->seek( $row );
390 function lastError() {
391 if ( $this->mConn
=== false ) {
394 $e = oci_error( $this->mConn
);
396 return $e['message'];
399 function lastErrno() {
400 if ( $this->mConn
=== false ) {
403 $e = oci_error( $this->mConn
);
408 function affectedRows() {
409 return $this->mAffectedRows
;
413 * Returns information about an index
414 * If errors are explicitly ignored, returns NULL on failure
416 function indexInfo( $table, $index, $fname = 'DatabaseOracle::indexExists' ) {
420 function indexUnique( $table, $index, $fname = 'DatabaseOracle::indexUnique' ) {
424 function insert( $table, $a, $fname = 'DatabaseOracle::insert', $options = array() ) {
425 if ( !count( $a ) ) {
429 if ( !is_array( $options ) ) {
430 $options = array( $options );
433 if ( in_array( 'IGNORE', $options ) ) {
434 $this->ignore_DUP_VAL_ON_INDEX
= true;
437 if ( !is_array( reset( $a ) ) ) {
441 foreach ( $a as &$row ) {
442 $this->insertOneRow( $table, $row, $fname );
446 if ( in_array( 'IGNORE', $options ) ) {
447 $this->ignore_DUP_VAL_ON_INDEX
= false;
453 function insertOneRow( $table, $row, $fname ) {
456 // "INSERT INTO tables (a, b, c)"
457 $sql = "INSERT INTO " . $this->tableName( $table ) . " (" . join( ',', array_keys( $row ) ) . ')';
460 // for each value, append ":key"
462 foreach ( $row as $col => $val ) {
464 $sql .= $val !== null ?
':' . $col : 'NULL';
466 $sql .= $val !== null ?
', :' . $col : ', NULL';
473 $stmt = oci_parse( $this->mConn
, $sql );
474 foreach ( $row as $col => &$val ) {
475 $col_type = $this->fieldInfo( $this->tableName( $table ), $col )->type();
477 if ( $val === null ) {
478 // do nothing ... null was inserted in statement creation
479 } elseif ( $col_type != 'BLOB' && $col_type != 'CLOB' ) {
480 if ( is_object( $val ) ) {
481 $val = $val->getData();
484 if ( preg_match( '/^timestamp.*/i', $col_type ) == 1 && strtolower( $val ) == 'infinity' ) {
485 $val = '31-12-2030 12:00:00.000000';
488 $val = ( $wgLang != null ) ?
$wgLang->checkTitleEncoding( $val ) : $val;
489 if ( oci_bind_by_name( $stmt, ":$col", $val ) === false ) {
490 $this->reportQueryError( $this->lastErrno(), $this->lastError(), $sql, __METHOD__
);
493 if ( ( $lob[$col] = oci_new_descriptor( $this->mConn
, OCI_D_LOB
) ) === false ) {
494 $e = oci_error( $stmt );
495 throw new DBUnexpectedError( $this, "Cannot create LOB descriptor: " . $e['message'] );
498 if ( $col_type == 'BLOB' ) { // is_object($val)) {
499 $lob[$col]->writeTemporary( $val ); // ->getData());
500 oci_bind_by_name( $stmt, ":$col", $lob[$col], - 1, SQLT_BLOB
);
502 $lob[$col]->writeTemporary( $val );
503 oci_bind_by_name( $stmt, ":$col", $lob[$col], - 1, OCI_B_CLOB
);
508 $olderr = error_reporting( E_ERROR
);
509 if ( oci_execute( $stmt, OCI_DEFAULT
) === false ) {
510 $e = oci_error( $stmt );
512 if ( !$this->ignore_DUP_VAL_ON_INDEX ||
$e['code'] != '1' ) {
513 $this->reportQueryError( $e['message'], $e['code'], $sql, __METHOD__
);
515 $this->mAffectedRows
= oci_num_rows( $stmt );
518 $this->mAffectedRows
= oci_num_rows( $stmt );
520 error_reporting( $olderr );
522 if ( isset( $lob ) ) {
523 foreach ( $lob as $lob_i => $lob_v ) {
528 if ( !$this->mTrxLevel
) {
529 oci_commit( $this->mConn
);
532 oci_free_statement( $stmt );
535 function insertSelect( $destTable, $srcTable, $varMap, $conds, $fname = 'DatabaseOracle::insertSelect',
536 $insertOptions = array(), $selectOptions = array() )
538 $destTable = $this->tableName( $destTable );
539 if ( !is_array( $selectOptions ) ) {
540 $selectOptions = array( $selectOptions );
542 list( $startOpts, $useIndex, $tailOpts ) = $this->makeSelectOptions( $selectOptions );
543 if ( is_array( $srcTable ) ) {
544 $srcTable = implode( ',', array_map( array( &$this, 'tableName' ), $srcTable ) );
546 $srcTable = $this->tableName( $srcTable );
549 if ( ( $sequenceData = $this->getSequenceData( $destTable ) ) !== false &&
550 !isset( $varMap[$sequenceData['column']] ) )
551 $varMap[$sequenceData['column']] = 'GET_SEQUENCE_VALUE(\'' . $sequenceData['sequence'] . '\')';
553 // count-alias subselect fields to avoid abigious definition errors
555 foreach ( $varMap as $key => &$val ) {
556 $val = $val . ' field' . ( $i++
);
559 $sql = "INSERT INTO $destTable (" . implode( ',', array_keys( $varMap ) ) . ')' .
560 " SELECT $startOpts " . implode( ',', $varMap ) .
561 " FROM $srcTable $useIndex ";
562 if ( $conds != '*' ) {
563 $sql .= ' WHERE ' . $this->makeList( $conds, LIST_AND
);
565 $sql .= " $tailOpts";
567 if ( in_array( 'IGNORE', $insertOptions ) ) {
568 $this->ignore_DUP_VAL_ON_INDEX
= true;
571 $retval = $this->query( $sql, $fname );
573 if ( in_array( 'IGNORE', $insertOptions ) ) {
574 $this->ignore_DUP_VAL_ON_INDEX
= false;
580 function tableName( $name ) {
581 global $wgSharedDB, $wgSharedPrefix, $wgSharedTables;
583 Replace reserved words with better ones
584 Using uppercase because that's the only way Oracle can handle
592 $name = 'PAGECONTENT';
597 The rest of procedure is equal to generic Databse class
598 except for the quoting style
600 if ( $name[0] == '"' && substr( $name, - 1, 1 ) == '"' ) {
604 if ( preg_match( '/(^|\s)(DISTINCT|JOIN|ON|AS)(\s|$)/i', $name ) !== 0 ) {
607 $dbDetails = array_reverse( explode( '.', $name, 2 ) );
608 if ( isset( $dbDetails[1] ) ) {
609 @list
( $table, $database ) = $dbDetails;
611 @list
( $table ) = $dbDetails;
614 $prefix = $this->mTablePrefix
;
616 if ( isset( $database ) ) {
617 $table = ( $table[0] == '`' ?
$table : "`{$table}`" );
620 if ( !isset( $database ) && isset( $wgSharedDB ) && $table[0] != '"'
621 && isset( $wgSharedTables )
622 && is_array( $wgSharedTables )
623 && in_array( $table, $wgSharedTables )
625 $database = $wgSharedDB;
626 $prefix = isset( $wgSharedPrefix ) ?
$wgSharedPrefix : $prefix;
629 if ( isset( $database ) ) {
630 $database = ( $database[0] == '"' ?
$database : "\"{$database}\"" );
632 $table = ( $table[0] == '"' ?
$table : "\"{$prefix}{$table}\"" );
634 $tableName = ( isset( $database ) ?
"{$database}.{$table}" : "{$table}" );
636 return strtoupper( $tableName );
640 * Return the next in a sequence, save the value for retrieval via insertId()
642 function nextSequenceValue( $seqName ) {
643 $res = $this->query( "SELECT $seqName.nextval FROM dual" );
644 $row = $this->fetchRow( $res );
645 $this->mInsertId
= $row[0];
646 $this->freeResult( $res );
647 return $this->mInsertId
;
651 * Return sequence_name if table has a sequence
653 function getSequenceData( $table ) {
654 if ( $this->sequenceData
== null ) {
655 $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'" );
657 while ( ( $row = $result->fetchRow() ) !== false ) {
658 $this->sequenceData
[$this->tableName( $row[1] )] = array(
659 'sequence' => $row[0],
665 return ( isset( $this->sequenceData
[$table] ) ) ?
$this->sequenceData
[$table] : false;
668 # REPLACE query wrapper
669 # Oracle simulates this with a DELETE followed by INSERT
670 # $row is the row to insert, an associative array
671 # $uniqueIndexes is an array of indexes. Each element may be either a
672 # field name or an array of field names
674 # It may be more efficient to leave off unique indexes which are unlikely to collide.
675 # However if you do this, you run the risk of encountering errors which wouldn't have
677 function replace( $table, $uniqueIndexes, $rows, $fname = 'DatabaseOracle::replace' ) {
678 $table = $this->tableName( $table );
680 if ( count( $rows ) == 0 ) {
685 if ( !is_array( reset( $rows ) ) ) {
686 $rows = array( $rows );
689 $sequenceData = $this->getSequenceData( $table );
691 foreach ( $rows as $row ) {
692 # Delete rows which collide
693 if ( $uniqueIndexes ) {
694 $condsDelete = array();
695 foreach ( $uniqueIndexes as $index )
696 $condsDelete[$index] = $row[$index];
697 $this->delete( $table, $condsDelete, $fname );
699 $sql = "DELETE FROM $table WHERE ";
701 foreach ( $uniqueIndexes as $index ) {
708 if ( is_array( $index ) ) {
710 foreach ( $index as $col ) {
716 $sql .= $col.'=' . $this->addQuotes( $row[$col] );
719 $sql .= $index.'=' . $this->addQuotes( $row[$index] );
724 $this->doQuery( $sql);//, $fname );
728 if ( $sequenceData !== false && !isset( $row[$sequenceData['column']] ) ) {
729 $row[$sequenceData['column']] = $this->nextSequenceValue( $sequenceData['sequence'] );
733 $this->insert( $table, $row, $fname );
737 # DELETE where the condition is a join
738 function deleteJoin( $delTable, $joinTable, $delVar, $joinVar, $conds, $fname = "DatabaseOracle::deleteJoin" ) {
740 throw new DBUnexpectedError( $this, 'DatabaseOracle::deleteJoin() called with empty $conds' );
743 $delTable = $this->tableName( $delTable );
744 $joinTable = $this->tableName( $joinTable );
745 $sql = "DELETE FROM $delTable WHERE $delVar IN (SELECT $joinVar FROM $joinTable ";
746 if ( $conds != '*' ) {
747 $sql .= 'WHERE ' . $this->makeList( $conds, LIST_AND
);
751 $this->query( $sql, $fname );
754 # Returns the size of a text field, or -1 for "unlimited"
755 function textFieldSize( $table, $field ) {
756 $table = $this->tableName( $table );
757 $sql = "SELECT t.typname as ftype,a.atttypmod as size
758 FROM pg_class c, pg_attribute a, pg_type t
759 WHERE relname='$table' AND a.attrelid=c.oid AND
760 a.atttypid=t.oid and a.attname='$field'";
761 $res = $this->query( $sql );
762 $row = $this->fetchObject( $res );
763 if ( $row->ftype
== "varchar" ) {
764 $size = $row->size
- 4;
768 $this->freeResult( $res );
772 function limitResult( $sql, $limit, $offset = false ) {
773 if ( $offset === false ) {
776 return "SELECT * FROM ($sql) WHERE rownum >= (1 + $offset) AND rownum < (1 + $limit + $offset)";
780 function unionQueries( $sqls, $all ) {
781 $glue = ' UNION ALL ';
782 return 'SELECT * ' . ( $all ?
'':'/* UNION_UNIQUE */ ' ) . 'FROM (' . implode( $glue, $sqls ) . ')' ;
785 function wasDeadlock() {
786 return $this->lastErrno() == 'OCI-00060';
790 function duplicateTableStructure( $oldName, $newName, $temporary = false, $fname = 'DatabaseOracle::duplicateTableStructure' ) {
791 $temporary = $temporary ?
'TRUE' : 'FALSE';
792 return $this->query( 'BEGIN DUPLICATE_TABLE(\'' . $oldName . '\', \'' . $newName . '\', ' . $temporary . '); END;', $fname );
795 function timestamp( $ts = 0 ) {
796 return wfTimestamp( TS_ORACLE
, $ts );
800 * Return aggregated value function call
802 function aggregateValue ( $valuedata, $valuename = 'value' ) {
806 function reportQueryError( $error, $errno, $sql, $fname, $tempIgnore = false ) {
807 # Ignore errors during error handling to avoid infinite
809 $ignore = $this->ignoreErrors( true );
810 ++
$this->mErrorCount
;
812 if ( $ignore ||
$tempIgnore ) {
813 wfDebug( "SQL ERROR (ignored): $error\n" );
814 $this->ignoreErrors( $ignore );
816 throw new DBQueryError( $this, $error, $errno, $sql, $fname );
821 * @return string wikitext of a link to the server software's web site
823 function getSoftwareLink() {
824 return '[http://www.oracle.com/ Oracle]';
828 * @return string Version information from the database
830 function getServerVersion() {
831 return oci_server_version( $this->mConn
);
835 * Query whether a given table exists (in the given schema, or the default mw one if not given)
837 function tableExists( $table ) {
838 $SQL = "SELECT 1 FROM user_tables WHERE table_name='$table'";
839 $res = $this->doQuery( $SQL );
841 $count = $res->numRows();
850 * Query whether a given column exists in the mediawiki schema
851 * based on prebuilt table to simulate MySQL field info and keep query speed minimal
853 function fieldExists( $table, $field, $fname = 'DatabaseOracle::fieldExists' ) {
854 if ( !isset( $this->fieldInfo_stmt
) ) {
855 $this->fieldInfo_stmt
= oci_parse( $this->mConn
, 'SELECT * FROM wiki_field_info_full WHERE table_name = upper(:tab) and column_name = UPPER(:col)' );
858 oci_bind_by_name( $this->fieldInfo_stmt
, ':tab', trim( $table, '"' ) );
859 oci_bind_by_name( $this->fieldInfo_stmt
, ':col', $field );
861 if ( oci_execute( $this->fieldInfo_stmt
, OCI_DEFAULT
) === false ) {
862 $e = oci_error( $this->fieldInfo_stmt
);
863 $this->reportQueryError( $e['message'], $e['code'], 'fieldInfo QUERY', __METHOD__
);
866 $res = new ORAResult( $this, $this->fieldInfo_stmt
);
867 return $res->numRows() != 0;
870 function fieldInfo( $table, $field ) {
871 if ( !isset( $this->fieldInfo_stmt
) ) {
872 $this->fieldInfo_stmt
= oci_parse( $this->mConn
, 'SELECT * FROM wiki_field_info_full WHERE table_name = upper(:tab) and column_name = UPPER(:col)' );
875 $table = trim( $table, '"' );
876 oci_bind_by_name( $this->fieldInfo_stmt
, ':tab', $table );
877 oci_bind_by_name( $this->fieldInfo_stmt
, ':col', $field );
879 if ( oci_execute( $this->fieldInfo_stmt
, OCI_DEFAULT
) === false ) {
880 $e = oci_error( $this->fieldInfo_stmt
);
881 $this->reportQueryError( $e['message'], $e['code'], 'fieldInfo QUERY', __METHOD__
);
884 $res = new ORAResult( $this, $this->fieldInfo_stmt
);
885 return new ORAField( $res->fetchRow() );
888 function begin( $fname = '' ) {
889 $this->mTrxLevel
= 1;
892 function immediateCommit( $fname = '' ) {
896 function commit( $fname = '' ) {
897 oci_commit( $this->mConn
);
898 $this->mTrxLevel
= 0;
901 /* Not even sure why this is used in the main codebase... */
902 function limitResultForUpdate( $sql, $num ) {
906 /* defines must comply with ^define\s*([^\s=]*)\s*=\s?'\{\$([^\}]*)\}'; */
907 function sourceStream( $fp, $lineCallback = false, $resultCallback = false ) {
910 $dollarquote = false;
912 $replacements = array();
914 while ( ! feof( $fp ) ) {
915 if ( $lineCallback ) {
916 call_user_func( $lineCallback );
918 $line = trim( fgets( $fp, 1024 ) );
919 $sl = strlen( $line ) - 1;
924 if ( '-' == $line { 0 } && '-' == $line { 1 } ) {
928 // Allow dollar quoting for function declarations
929 if ( substr( $line, 0, 8 ) == '/*$mw$*/' ) {
930 if ( $dollarquote ) {
931 $dollarquote = false;
936 } elseif ( !$dollarquote ) {
937 if ( ';' == $line { $sl } && ( $sl < 2 ||
';' != $line { $sl - 1 } ) ) {
939 $line = substr( $line, 0, $sl );
949 $cmd = str_replace( ';;', ";", $cmd );
950 if ( strtolower( substr( $cmd, 0, 6 ) ) == 'define' ) {
951 if ( preg_match( '/^define\s*([^\s=]*)\s*=\s*\'\{\$([^\}]*)\}\'/', $cmd, $defines ) ) {
952 $replacements[$defines[2]] = $defines[1];
955 foreach ( $replacements as $mwVar => $scVar ) {
956 $cmd = str_replace( '&' . $scVar . '.', '{$' . $mwVar . '}', $cmd );
959 $cmd = $this->replaceVars( $cmd );
960 $res = $this->query( $cmd, __METHOD__
);
961 if ( $resultCallback ) {
962 call_user_func( $resultCallback, $res, $this );
965 if ( false === $res ) {
966 $err = $this->lastError();
967 return "Query \"{$cmd}\" failed with error code \"$err\".\n";
978 function setup_database() {
979 global $wgVersion, $wgDBmwschema, $wgDBts2schema, $wgDBport, $wgDBuser;
981 echo "<li>Creating DB objects</li>\n";
982 $res = $this->sourceFile( "../maintenance/ora/tables.sql" );
984 // Avoid the non-standard "REPLACE INTO" syntax
985 echo "<li>Populating table interwiki</li>\n";
986 $f = fopen( "../maintenance/interwiki.sql", 'r' );
988 dieout( "<li>Could not find the interwiki.sql file</li>" );
991 // do it like the postgres :D
992 $SQL = "INSERT INTO interwiki(iw_prefix,iw_url,iw_local) VALUES ";
993 while ( !feof( $f ) ) {
994 $line = fgets( $f, 1024 );
996 if ( !preg_match( '/^\s*(\(.+?),(\d)\)/', $line, $matches ) ) {
999 $this->query( "$SQL $matches[1],$matches[2])" );
1002 echo "<li>Table interwiki successfully populated</li>\n";
1005 function strencode( $s ) {
1006 return str_replace( "'", "''", $s );
1010 function encodeBlob($b) {
1011 return $b; //new ORABlob($b);
1013 function decodeBlob($b) {
1014 return $b; //return $b->load();
1017 function addQuotes( $s ) {
1019 if ( isset( $wgLang->mLoaded
) && $wgLang->mLoaded
) {
1020 $s = $wgLang->checkTitleEncoding( $s );
1022 return "'" . $this->strencode( $s ) . "'";
1025 function quote_ident( $s ) {
1029 function selectRow( $table, $vars, $conds, $fname = 'DatabaseOracle::selectRow', $options = array(), $join_conds = array() ) {
1033 foreach ( $conds as $col => $val ) {
1034 $col_type = $this->fieldInfo( $this->tableName( $table ), $col )->type();
1035 if ( $col_type == 'CLOB' ) {
1036 $conds2['TO_CHAR(' . $col . ')'] = $wgLang->checkTitleEncoding( $val );
1037 } elseif ( $col_type == 'VARCHAR2' && !mb_check_encoding( $val ) ) {
1038 $conds2[$col] = $wgLang->checkTitleEncoding( $val );
1040 $conds2[$col] = $val;
1044 if ( is_array( $table ) ) {
1045 foreach ( $table as $tab ) {
1046 $tab = $this->tableName( $tab );
1049 $table = $this->tableName( $table );
1052 return parent
::selectRow( $table, $vars, $conds2, $fname, $options, $join_conds );
1056 * Returns an optional USE INDEX clause to go after the table, and a
1057 * string to go at the end of the query
1061 * @param $options Array: an associative array of options to be turned into
1062 * an SQL query, valid keys are listed in the function.
1065 function makeSelectOptions( $options ) {
1066 $preLimitTail = $postLimitTail = '';
1069 $noKeyOptions = array();
1070 foreach ( $options as $key => $option ) {
1071 if ( is_numeric( $key ) ) {
1072 $noKeyOptions[$option] = true;
1076 if ( isset( $options['GROUP BY'] ) ) {
1077 $preLimitTail .= " GROUP BY {$options['GROUP BY']}";
1079 if ( isset( $options['ORDER BY'] ) ) {
1080 $preLimitTail .= " ORDER BY {$options['ORDER BY']}";
1083 # if ( isset( $noKeyOptions['FOR UPDATE'] ) ) $tailOpts .= ' FOR UPDATE';
1084 # if ( isset( $noKeyOptions['LOCK IN SHARE MODE'] ) ) $tailOpts .= ' LOCK IN SHARE MODE';
1085 if ( isset( $noKeyOptions['DISTINCT'] ) ||
isset( $noKeyOptions['DISTINCTROW'] ) ) {
1086 $startOpts .= 'DISTINCT';
1089 if ( isset( $options['USE INDEX'] ) && ! is_array( $options['USE INDEX'] ) ) {
1090 $useIndex = $this->useIndexClause( $options['USE INDEX'] );
1095 return array( $startOpts, $useIndex, $preLimitTail, $postLimitTail );
1098 public function delete( $table, $conds, $fname = 'DatabaseOracle::delete' ) {
1101 if ( $wgLang != null ) {
1103 foreach ( $conds as $col => $val ) {
1104 $col_type = $this->fieldInfo( $this->tableName( $table ), $col )->type();
1105 if ( $col_type == 'CLOB' ) {
1106 $conds2['TO_CHAR(' . $col . ')'] = $wgLang->checkTitleEncoding( $val );
1108 if ( is_array( $val ) ) {
1109 $conds2[$col] = $val;
1110 foreach ( $conds2[$col] as &$val2 ) {
1111 $val2 = $wgLang->checkTitleEncoding( $val2 );
1114 $conds2[$col] = $wgLang->checkTitleEncoding( $val );
1119 return parent
::delete( $table, $conds2, $fname );
1121 return parent
::delete( $table, $conds, $fname );
1125 function bitNot( $field ) {
1126 // expecting bit-fields smaller than 4bytes
1127 return 'BITNOT(' . $bitField . ')';
1130 function bitAnd( $fieldLeft, $fieldRight ) {
1131 return 'BITAND(' . $fieldLeft . ', ' . $fieldRight . ')';
1134 function bitOr( $fieldLeft, $fieldRight ) {
1135 return 'BITOR(' . $fieldLeft . ', ' . $fieldRight . ')';
1139 * How lagged is this slave?
1143 public function getLag() {
1144 # Not implemented for Oracle
1148 function setFakeSlaveLag( $lag ) { }
1149 function setFakeMaster( $enabled = true ) { }
1151 function getDBname() {
1152 return $this->mDBname
;
1155 function getServer() {
1156 return $this->mServer
;
1159 public function replaceVars( $ins ) {
1160 $varnames = array( 'wgDBprefix' );
1161 if ( $this->mFlags
& DBO_SYSDBA
) {
1162 $varnames[] = 'wgDBOracleDefTS';
1163 $varnames[] = 'wgDBOracleTempTS';
1166 // Ordinary variables
1167 foreach ( $varnames as $var ) {
1168 if ( isset( $GLOBALS[$var] ) ) {
1169 $val = addslashes( $GLOBALS[$var] ); // FIXME: safety check?
1170 $ins = str_replace( '{$' . $var . '}', $val, $ins );
1171 $ins = str_replace( '/*$' . $var . '*/`', '`' . $val, $ins );
1172 $ins = str_replace( '/*$' . $var . '*/', $val, $ins );
1176 return parent
::replaceVars( $ins );
1179 public function getSearchEngine() {
1180 return 'SearchOracle';
1182 } // end DatabaseOracle class