4 * This is the Oracle database abstraction layer.
10 function __construct($data) {
20 * The oci8 extension is fairly weak and doesn't support oci_num_rows, among
21 * other things. We use a wrapper class to handle that and other
22 * Oracle-specific bits, like converting column names back to lowercase.
23 * @addtogroup Database
32 function __construct(&$db, $stmt) {
34 if (($this->nrows
= oci_fetch_all($stmt, $this->rows
, 0, -1, OCI_FETCHSTATEMENT_BY_ROW | OCI_NUM
)) === false) {
35 $e = oci_error($stmt);
36 $db->reportQueryError($e['message'], $e['code'], '', __FUNCTION__
);
45 oci_free_statement($this->stmt
);
49 $this->cursor
= min($row, $this->nrows
);
56 function numFields() {
57 return oci_num_fields($this->stmt
);
60 function fetchObject() {
61 if ($this->cursor
>= $this->nrows
)
64 $row = $this->rows
[$this->cursor++
];
65 $ret = new stdClass();
66 foreach ($row as $k => $v) {
67 $lc = strtolower(oci_field_name($this->stmt
, $k +
1));
74 function fetchAssoc() {
75 if ($this->cursor
>= $this->nrows
)
78 $row = $this->rows
[$this->cursor++
];
80 foreach ($row as $k => $v) {
81 $lc = strtolower(oci_field_name($this->stmt
, $k +
1));
90 * @addtogroup Database
92 class DatabaseOracle
extends Database
{
93 var $mInsertId = NULL;
94 var $mLastResult = NULL;
95 var $numeric_version = NULL;
96 var $lastResult = null;
100 function DatabaseOracle($server = false, $user = false, $password = false, $dbName = false,
101 $failFunction = false, $flags = 0 )
105 # Can't get a reference if it hasn't been set yet
106 if ( !isset( $wgOut ) ) {
109 $this->mOut
=& $wgOut;
110 $this->mFailFunction
= $failFunction;
111 $this->mFlags
= $flags;
112 $this->open( $server, $user, $password, $dbName);
116 function cascadingDeletes() {
119 function cleanupTriggers() {
122 function strictIPs() {
125 function realTimestamps() {
128 function implicitGroupby() {
131 function searchableIPs() {
135 static function newFromParams( $server = false, $user = false, $password = false, $dbName = false,
136 $failFunction = false, $flags = 0)
138 return new DatabaseOracle( $server, $user, $password, $dbName, $failFunction, $flags );
142 * Usually aborts on failure
143 * If the failFunction is set to a non-zero integer, returns success
145 function open( $server, $user, $password, $dbName ) {
146 if ( !function_exists( 'oci_connect' ) ) {
147 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" );
150 # Needed for proper UTF-8 functionality
151 putenv("NLS_LANG=AMERICAN_AMERICA.AL32UTF8");
154 $this->mServer
= $server;
155 $this->mUser
= $user;
156 $this->mPassword
= $password;
157 $this->mDBname
= $dbName;
159 if (!strlen($user)) { ## e.g. the class is being loaded
163 error_reporting( E_ALL
);
164 $this->mConn
= oci_connect($user, $password, $dbName);
166 if ($this->mConn
== false) {
167 wfDebug("DB connection error\n");
168 wfDebug("Server: $server, Database: $dbName, User: $user, Password: " . substr( $password, 0, 3 ) . "...\n");
169 wfDebug($this->lastError()."\n");
173 $this->mOpened
= true;
178 * Closes a database connection, if it is open
179 * Returns success, true if already closed
182 $this->mOpened
= false;
183 if ( $this->mConn
) {
184 return oci_close( $this->mConn
);
190 function execFlags() {
191 return $this->mTrxLevel ? OCI_DEFAULT
: OCI_COMMIT_ON_SUCCESS
;
194 function doQuery($sql) {
195 wfDebug("SQL: [$sql]\n");
196 if (!mb_check_encoding($sql)) {
197 throw new MWException("SQL encoding is invalid");
200 if (($this->mLastResult
= $stmt = oci_parse($this->mConn
, $sql)) === false) {
201 $e = oci_error($this->mConn
);
202 $this->reportQueryError($e['message'], $e['code'], $sql, __FUNCTION__
);
205 if (oci_execute($stmt, $this->execFlags()) == false) {
206 $e = oci_error($stmt);
207 $this->reportQueryError($e['message'], $e['code'], $sql, __FUNCTION__
);
209 if (oci_statement_type($stmt) == "SELECT")
210 return new ORAResult($this, $stmt);
212 $this->mAffectedRows
= oci_num_rows($stmt);
217 function queryIgnore($sql, $fname = '') {
218 return $this->query($sql, $fname, true);
221 function freeResult($res) {
225 function fetchObject($res) {
226 return $res->fetchObject();
229 function fetchRow($res) {
230 return $res->fetchAssoc();
233 function numRows($res) {
234 return $res->numRows();
237 function numFields($res) {
238 return $res->numFields();
241 function fieldName($stmt, $n) {
242 return pg_field_name($stmt, $n);
246 * This must be called after nextSequenceVal
248 function insertId() {
249 return $this->mInsertId
;
252 function dataSeek($res, $row) {
256 function lastError() {
257 if ($this->mConn
=== false)
260 $e = oci_error($this->mConn
);
261 return $e['message'];
264 function lastErrno() {
265 if ($this->mConn
=== false)
268 $e = oci_error($this->mConn
);
272 function affectedRows() {
273 return $this->mAffectedRows
;
277 * Returns information about an index
278 * If errors are explicitly ignored, returns NULL on failure
280 function indexInfo( $table, $index, $fname = 'Database::indexExists' ) {
284 function indexUnique ($table, $index, $fname = 'Database::indexUnique' ) {
288 function insert( $table, $a, $fname = 'Database::insert', $options = array() ) {
289 if (!is_array($options))
290 $options = array($options);
292 #if (in_array('IGNORE', $options))
293 # $oldIgnore = $this->ignoreErrors(true);
295 # IGNORE is performed using single-row inserts, ignoring errors in each
296 # FIXME: need some way to distiguish between key collision and other types of error
297 //$oldIgnore = $this->ignoreErrors(true);
298 if (!is_array(reset($a))) {
301 foreach ($a as $row) {
302 $this->insertOneRow($table, $row, $fname);
304 //$this->ignoreErrors($oldIgnore);
307 //if (in_array('IGNORE', $options))
308 // $this->ignoreErrors($oldIgnore);
313 function insertOneRow($table, $row, $fname) {
314 // "INSERT INTO tables (a, b, c)"
315 $sql = "INSERT INTO " . $this->tableName($table) . " (" . join(',', array_keys($row)) . ')';
318 // for each value, append ":key"
321 foreach ($row as $col => $val) {
322 if (is_object($val)) {
323 $what = "EMPTY_BLOB()";
324 assert($returning === '');
325 $returning = " RETURNING $col INTO :bval";
336 $sql .= ") $returning";
338 $stmt = oci_parse($this->mConn
, $sql);
339 foreach ($row as $col => $val) {
340 if (!is_object($val)) {
341 if (oci_bind_by_name($stmt, ":$col", $row[$col]) === false)
342 $this->reportQueryError($this->lastErrno(), $this->lastError(), $sql, __METHOD__
);
346 if (($bval = oci_new_descriptor($this->mConn
, OCI_D_LOB
)) === false) {
347 $e = oci_error($stmt);
348 throw new DBUnexpectedError($this, "Cannot create LOB descriptor: " . $e['message']);
351 if (strlen($returning))
352 oci_bind_by_name($stmt, ":bval", $bval, -1, SQLT_BLOB
);
354 if (oci_execute($stmt, OCI_DEFAULT
) === false) {
355 $e = oci_error($stmt);
356 $this->reportQueryError($e['message'], $e['code'], $sql, __METHOD__
);
358 if (strlen($returning)) {
359 $bval->save($row[$blobcol]->getData());
362 if (!$this->mTrxLevel
)
363 oci_commit($this->mConn
);
365 oci_free_statement($stmt);
368 function tableName( $name ) {
369 # Replace reserved words with better ones
374 return 'pagecontent';
381 * Return the next in a sequence, save the value for retrieval via insertId()
383 function nextSequenceValue($seqName) {
384 $res = $this->query("SELECT $seqName.nextval FROM dual");
385 $row = $this->fetchRow($res);
386 $this->mInsertId
= $row[0];
387 $this->freeResult($res);
388 return $this->mInsertId
;
392 * Oracle does not have a "USE INDEX" clause, so return an empty string
394 function useIndexClause($index) {
398 # REPLACE query wrapper
399 # Oracle simulates this with a DELETE followed by INSERT
400 # $row is the row to insert, an associative array
401 # $uniqueIndexes is an array of indexes. Each element may be either a
402 # field name or an array of field names
404 # It may be more efficient to leave off unique indexes which are unlikely to collide.
405 # However if you do this, you run the risk of encountering errors which wouldn't have
407 function replace( $table, $uniqueIndexes, $rows, $fname = 'Database::replace' ) {
408 $table = $this->tableName($table);
410 if (count($rows)==0) {
415 if (!is_array(reset($rows))) {
416 $rows = array($rows);
419 foreach( $rows as $row ) {
420 # Delete rows which collide
421 if ( $uniqueIndexes ) {
422 $sql = "DELETE FROM $table WHERE ";
424 foreach ( $uniqueIndexes as $index ) {
431 if ( is_array( $index ) ) {
433 foreach ( $index as $col ) {
439 $sql .= $col.'=' . $this->addQuotes( $row[$col] );
442 $sql .= $index.'=' . $this->addQuotes( $row[$index] );
446 $this->query( $sql, $fname );
450 $sql = "INSERT INTO $table (" . $this->makeList( array_keys( $row ), LIST_NAMES
) .') VALUES (' .
451 $this->makeList( $row, LIST_COMMA
) . ')';
452 $this->query($sql, $fname);
456 # DELETE where the condition is a join
457 function deleteJoin( $delTable, $joinTable, $delVar, $joinVar, $conds, $fname = "Database::deleteJoin" ) {
459 throw new DBUnexpectedError($this, 'Database::deleteJoin() called with empty $conds' );
462 $delTable = $this->tableName( $delTable );
463 $joinTable = $this->tableName( $joinTable );
464 $sql = "DELETE FROM $delTable WHERE $delVar IN (SELECT $joinVar FROM $joinTable ";
465 if ( $conds != '*' ) {
466 $sql .= 'WHERE ' . $this->makeList( $conds, LIST_AND
);
470 $this->query( $sql, $fname );
473 # Returns the size of a text field, or -1 for "unlimited"
474 function textFieldSize( $table, $field ) {
475 $table = $this->tableName( $table );
476 $sql = "SELECT t.typname as ftype,a.atttypmod as size
477 FROM pg_class c, pg_attribute a, pg_type t
478 WHERE relname='$table' AND a.attrelid=c.oid AND
479 a.atttypid=t.oid and a.attname='$field'";
480 $res =$this->query($sql);
481 $row=$this->fetchObject($res);
482 if ($row->ftype
=="varchar") {
487 $this->freeResult( $res );
491 function lowPriorityOption() {
495 function limitResult($sql, $limit, $offset) {
496 if ($offset === false)
498 return "SELECT * FROM ($sql) WHERE rownum >= (1 + $offset) AND rownum < 1 + $limit + $offset";
502 * Returns an SQL expression for a simple conditional.
503 * Uses CASE on Oracle
505 * @param string $cond SQL expression which will result in a boolean value
506 * @param string $trueVal SQL expression to return if true
507 * @param string $falseVal SQL expression to return if false
508 * @return string SQL fragment
510 function conditional( $cond, $trueVal, $falseVal ) {
511 return " (CASE WHEN $cond THEN $trueVal ELSE $falseVal END) ";
514 function wasDeadlock() {
515 return $this->lastErrno() == 'OCI-00060';
518 function timestamp($ts = 0) {
519 return wfTimestamp(TS_ORACLE
, $ts);
523 * Return aggregated value function call
525 function aggregateValue ($valuedata,$valuename='value') {
529 function reportQueryError($error, $errno, $sql, $fname, $tempIgnore = false) {
530 # Ignore errors during error handling to avoid infinite
532 $ignore = $this->ignoreErrors(true);
533 ++
$this->mErrorCount
;
535 if ($ignore ||
$tempIgnore) {
536 echo "error ignored! query = [$sql]\n";
537 wfDebug("SQL ERROR (ignored): $error\n");
538 $this->ignoreErrors( $ignore );
542 $message = "A database error has occurred\n" .
544 "Function: $fname\n" .
545 "Error: $errno $error\n";
546 throw new DBUnexpectedError($this, $message);
551 * @return string wikitext of a link to the server software's web site
553 function getSoftwareLink() {
554 return "[http://www.oracle.com/ Oracle]";
558 * @return string Version information from the database
560 function getServerVersion() {
561 return oci_server_version($this->mConn
);
565 * Query whether a given table exists (in the given schema, or the default mw one if not given)
567 function tableExists($table) {
568 $etable= $this->addQuotes($table);
569 $SQL = "SELECT 1 FROM user_tables WHERE table_name='$etable'";
570 $res = $this->query($SQL);
571 $count = $res ?
oci_num_rows($res) : 0;
573 $this->freeResult($res);
578 * Query whether a given column exists in the mediawiki schema
580 function fieldExists( $table, $field ) {
584 function fieldInfo( $table, $field ) {
588 function begin( $fname = '' ) {
589 $this->mTrxLevel
= 1;
591 function immediateCommit( $fname = '' ) {
594 function commit( $fname = '' ) {
595 oci_commit($this->mConn
);
596 $this->mTrxLevel
= 0;
599 /* Not even sure why this is used in the main codebase... */
600 function limitResultForUpdate($sql, $num) {
604 function strencode($s) {
605 return str_replace("'", "''", $s);
608 function encodeBlob($b) {
609 return new ORABlob($b);
611 function decodeBlob($b) {
612 return $b; //return $b->load();
615 function addQuotes( $s ) {
617 $s = $wgLang->checkTitleEncoding($s);
618 return "'" . $this->strencode($s) . "'";
621 function quote_ident( $s ) {
625 /* For now, does nothing */
626 function selectDB( $db ) {
631 * Returns an optional USE INDEX clause to go after the table, and a
632 * string to go at the end of the query
636 * @param array $options an associative array of options to be turned into
637 * an SQL query, valid keys are listed in the function.
640 function makeSelectOptions( $options ) {
641 $preLimitTail = $postLimitTail = '';
644 $noKeyOptions = array();
645 foreach ( $options as $key => $option ) {
646 if ( is_numeric( $key ) ) {
647 $noKeyOptions[$option] = true;
651 if ( isset( $options['GROUP BY'] ) ) $preLimitTail .= " GROUP BY {$options['GROUP BY']}";
652 if ( isset( $options['ORDER BY'] ) ) $preLimitTail .= " ORDER BY {$options['ORDER BY']}";
654 if (isset($options['LIMIT'])) {
655 // $tailOpts .= $this->limitResult('', $options['LIMIT'],
656 // isset($options['OFFSET']) ? $options['OFFSET']
660 #if ( isset( $noKeyOptions['FOR UPDATE'] ) ) $tailOpts .= ' FOR UPDATE';
661 #if ( isset( $noKeyOptions['LOCK IN SHARE MODE'] ) ) $tailOpts .= ' LOCK IN SHARE MODE';
662 if ( isset( $noKeyOptions['DISTINCT'] ) && isset( $noKeyOptions['DISTINCTROW'] ) ) $startOpts .= 'DISTINCT';
664 if ( isset( $options['USE INDEX'] ) && ! is_array( $options['USE INDEX'] ) ) {
665 $useIndex = $this->useIndexClause( $options['USE INDEX'] );
670 return array( $startOpts, $useIndex, $preLimitTail, $postLimitTail );
673 public function setTimeout( $timeout ) {
678 wfDebug( "Function ping() not written for DatabaseOracle.php yet");
683 * How lagged is this slave?
687 public function getLag() {
688 # Not implemented for Oracle
692 } // end DatabaseOracle class