4 * This is the Oracle database abstraction layer.
9 function __construct($data) {
19 * The oci8 extension is fairly weak and doesn't support oci_num_rows, among
20 * other things. We use a wrapper class to handle that and other
21 * Oracle-specific bits, like converting column names back to lowercase.
30 function __construct(&$db, $stmt) {
32 if (($this->nrows
= oci_fetch_all($stmt, $this->rows
, 0, -1, OCI_FETCHSTATEMENT_BY_ROW | OCI_NUM
)) === false) {
33 $e = oci_error($stmt);
34 $db->reportQueryError($e['message'], $e['code'], '', __FUNCTION__
);
43 oci_free_statement($this->stmt
);
47 $this->cursor
= min($row, $this->nrows
);
54 function numFields() {
55 return oci_num_fields($this->stmt
);
58 function fetchObject() {
59 if ($this->cursor
>= $this->nrows
)
62 $row = $this->rows
[$this->cursor++
];
63 $ret = new stdClass();
64 foreach ($row as $k => $v) {
65 $lc = strtolower(oci_field_name($this->stmt
, $k +
1));
72 function fetchAssoc() {
73 if ($this->cursor
>= $this->nrows
)
76 $row = $this->rows
[$this->cursor++
];
78 foreach ($row as $k => $v) {
79 $lc = strtolower(oci_field_name($this->stmt
, $k +
1));
87 class DatabaseOracle
extends Database
{
88 var $mInsertId = NULL;
89 var $mLastResult = NULL;
90 var $numeric_version = NULL;
91 var $lastResult = null;
95 function DatabaseOracle($server = false, $user = false, $password = false, $dbName = false,
96 $failFunction = false, $flags = 0 )
100 # Can't get a reference if it hasn't been set yet
101 if ( !isset( $wgOut ) ) {
104 $this->mOut
=& $wgOut;
105 $this->mFailFunction
= $failFunction;
106 $this->mFlags
= $flags;
107 $this->open( $server, $user, $password, $dbName);
111 function cascadingDeletes() {
114 function cleanupTriggers() {
117 function strictIPs() {
120 function realTimestamps() {
123 function implicitGroupby() {
126 function searchableIPs() {
130 static function newFromParams( $server = false, $user = false, $password = false, $dbName = false,
131 $failFunction = false, $flags = 0)
133 return new DatabaseOracle( $server, $user, $password, $dbName, $failFunction, $flags );
137 * Usually aborts on failure
138 * If the failFunction is set to a non-zero integer, returns success
140 function open( $server, $user, $password, $dbName ) {
141 if ( !function_exists( 'oci_connect' ) ) {
142 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" );
145 # Needed for proper UTF-8 functionality
146 putenv("NLS_LANG=AMERICAN_AMERICA.AL32UTF8");
149 $this->mServer
= $server;
150 $this->mUser
= $user;
151 $this->mPassword
= $password;
152 $this->mDBname
= $dbName;
154 if (!strlen($user)) { ## e.g. the class is being loaded
158 error_reporting( E_ALL
);
159 $this->mConn
= oci_connect($user, $password, $dbName);
161 if ($this->mConn
== false) {
162 wfDebug("DB connection error\n");
163 wfDebug("Server: $server, Database: $dbName, User: $user, Password: " . substr( $password, 0, 3 ) . "...\n");
164 wfDebug($this->lastError()."\n");
168 $this->mOpened
= true;
173 * Closes a database connection, if it is open
174 * Returns success, true if already closed
177 $this->mOpened
= false;
178 if ( $this->mConn
) {
179 return oci_close( $this->mConn
);
185 function execFlags() {
186 return $this->mTrxLevel ? OCI_DEFAULT
: OCI_COMMIT_ON_SUCCESS
;
189 function doQuery($sql) {
190 wfDebug("SQL: [$sql]\n");
191 if (!mb_check_encoding($sql)) {
192 throw new MWException("SQL encoding is invalid");
195 if (($this->mLastResult
= $stmt = oci_parse($this->mConn
, $sql)) === false) {
196 $e = oci_error($this->mConn
);
197 $this->reportQueryError($e['message'], $e['code'], $sql, __FUNCTION__
);
200 if (oci_execute($stmt, $this->execFlags()) == false) {
201 $e = oci_error($stmt);
202 $this->reportQueryError($e['message'], $e['code'], $sql, __FUNCTION__
);
204 if (oci_statement_type($stmt) == "SELECT")
205 return new ORAResult($this, $stmt);
207 $this->mAffectedRows
= oci_num_rows($stmt);
212 function queryIgnore($sql, $fname = '') {
213 return $this->query($sql, $fname, true);
216 function freeResult($res) {
220 function fetchObject($res) {
221 return $res->fetchObject();
224 function fetchRow($res) {
225 return $res->fetchAssoc();
228 function numRows($res) {
229 return $res->numRows();
232 function numFields($res) {
233 return $res->numFields();
236 function fieldName($stmt, $n) {
237 return pg_field_name($stmt, $n);
241 * This must be called after nextSequenceVal
243 function insertId() {
244 return $this->mInsertId
;
247 function dataSeek($res, $row) {
251 function lastError() {
252 if ($this->mConn
=== false)
255 $e = oci_error($this->mConn
);
256 return $e['message'];
259 function lastErrno() {
260 if ($this->mConn
=== false)
263 $e = oci_error($this->mConn
);
267 function affectedRows() {
268 return $this->mAffectedRows
;
272 * Returns information about an index
273 * If errors are explicitly ignored, returns NULL on failure
275 function indexInfo( $table, $index, $fname = 'Database::indexExists' ) {
279 function indexUnique ($table, $index, $fname = 'Database::indexUnique' ) {
283 function insert( $table, $a, $fname = 'Database::insert', $options = array() ) {
284 if (!is_array($options))
285 $options = array($options);
287 #if (in_array('IGNORE', $options))
288 # $oldIgnore = $this->ignoreErrors(true);
290 # IGNORE is performed using single-row inserts, ignoring errors in each
291 # FIXME: need some way to distiguish between key collision and other types of error
292 //$oldIgnore = $this->ignoreErrors(true);
293 if (!is_array(reset($a))) {
296 foreach ($a as $row) {
297 $this->insertOneRow($table, $row, $fname);
299 //$this->ignoreErrors($oldIgnore);
302 //if (in_array('IGNORE', $options))
303 // $this->ignoreErrors($oldIgnore);
308 function insertOneRow($table, $row, $fname) {
309 // "INSERT INTO tables (a, b, c)"
310 $sql = "INSERT INTO " . $this->tableName($table) . " (" . join(',', array_keys($row)) . ')';
313 // for each value, append ":key"
316 foreach ($row as $col => $val) {
317 if (is_object($val)) {
318 $what = "EMPTY_BLOB()";
319 assert($returning === '');
320 $returning = " RETURNING $col INTO :bval";
331 $sql .= ") $returning";
333 $stmt = oci_parse($this->mConn
, $sql);
334 foreach ($row as $col => $val) {
335 if (!is_object($val)) {
336 if (oci_bind_by_name($stmt, ":$col", $row[$col]) === false)
337 $this->reportQueryError($this->lastErrno(), $this->lastError(), $sql, __METHOD__
);
341 if (($bval = oci_new_descriptor($this->mConn
, OCI_D_LOB
)) === false) {
342 $e = oci_error($stmt);
343 throw new DBUnexpectedError($this, "Cannot create LOB descriptor: " . $e['message']);
346 if (strlen($returning))
347 oci_bind_by_name($stmt, ":bval", $bval, -1, SQLT_BLOB
);
349 if (oci_execute($stmt, OCI_DEFAULT
) === false) {
350 $e = oci_error($stmt);
351 $this->reportQueryError($e['message'], $e['code'], $sql, __METHOD__
);
353 if (strlen($returning)) {
354 $bval->save($row[$blobcol]->getData());
357 if (!$this->mTrxLevel
)
358 oci_commit($this->mConn
);
360 oci_free_statement($stmt);
363 function tableName( $name ) {
364 # Replace reserved words with better ones
369 return 'pagecontent';
376 * Return the next in a sequence, save the value for retrieval via insertId()
378 function nextSequenceValue($seqName) {
379 $res = $this->query("SELECT $seqName.nextval FROM dual");
380 $row = $this->fetchRow($res);
381 $this->mInsertId
= $row[0];
382 $this->freeResult($res);
383 return $this->mInsertId
;
387 * ORacle does not have a "USE INDEX" clause, so return an empty string
389 function useIndexClause($index) {
393 # REPLACE query wrapper
394 # Oracle simulates this with a DELETE followed by INSERT
395 # $row is the row to insert, an associative array
396 # $uniqueIndexes is an array of indexes. Each element may be either a
397 # field name or an array of field names
399 # It may be more efficient to leave off unique indexes which are unlikely to collide.
400 # However if you do this, you run the risk of encountering errors which wouldn't have
402 function replace( $table, $uniqueIndexes, $rows, $fname = 'Database::replace' ) {
403 $table = $this->tableName($table);
405 if (count($rows)==0) {
410 if (!is_array(reset($rows))) {
411 $rows = array($rows);
414 foreach( $rows as $row ) {
415 # Delete rows which collide
416 if ( $uniqueIndexes ) {
417 $sql = "DELETE FROM $table WHERE ";
419 foreach ( $uniqueIndexes as $index ) {
426 if ( is_array( $index ) ) {
428 foreach ( $index as $col ) {
434 $sql .= $col.'=' . $this->addQuotes( $row[$col] );
437 $sql .= $index.'=' . $this->addQuotes( $row[$index] );
441 $this->query( $sql, $fname );
445 $sql = "INSERT INTO $table (" . $this->makeList( array_keys( $row ), LIST_NAMES
) .') VALUES (' .
446 $this->makeList( $row, LIST_COMMA
) . ')';
447 $this->query($sql, $fname);
451 # DELETE where the condition is a join
452 function deleteJoin( $delTable, $joinTable, $delVar, $joinVar, $conds, $fname = "Database::deleteJoin" ) {
454 throw new DBUnexpectedError($this, 'Database::deleteJoin() called with empty $conds' );
457 $delTable = $this->tableName( $delTable );
458 $joinTable = $this->tableName( $joinTable );
459 $sql = "DELETE FROM $delTable WHERE $delVar IN (SELECT $joinVar FROM $joinTable ";
460 if ( $conds != '*' ) {
461 $sql .= 'WHERE ' . $this->makeList( $conds, LIST_AND
);
465 $this->query( $sql, $fname );
468 # Returns the size of a text field, or -1 for "unlimited"
469 function textFieldSize( $table, $field ) {
470 $table = $this->tableName( $table );
471 $sql = "SELECT t.typname as ftype,a.atttypmod as size
472 FROM pg_class c, pg_attribute a, pg_type t
473 WHERE relname='$table' AND a.attrelid=c.oid AND
474 a.atttypid=t.oid and a.attname='$field'";
475 $res =$this->query($sql);
476 $row=$this->fetchObject($res);
477 if ($row->ftype
=="varchar") {
482 $this->freeResult( $res );
486 function lowPriorityOption() {
490 function limitResult($sql, $limit, $offset) {
491 if ($offset === false)
493 return "SELECT * FROM ($sql) WHERE rownum >= (1 + $offset) AND rownum < 1 + $limit + $offset";
497 * Returns an SQL expression for a simple conditional.
498 * Uses CASE on Oracle
500 * @param string $cond SQL expression which will result in a boolean value
501 * @param string $trueVal SQL expression to return if true
502 * @param string $falseVal SQL expression to return if false
503 * @return string SQL fragment
505 function conditional( $cond, $trueVal, $falseVal ) {
506 return " (CASE WHEN $cond THEN $trueVal ELSE $falseVal END) ";
509 function wasDeadlock() {
510 return $this->lastErrno() == 'OCI-00060';
513 function timestamp($ts = 0) {
514 return wfTimestamp(TS_ORACLE
, $ts);
518 * Return aggregated value function call
520 function aggregateValue ($valuedata,$valuename='value') {
524 function reportQueryError($error, $errno, $sql, $fname, $tempIgnore = false) {
525 # Ignore errors during error handling to avoid infinite
527 $ignore = $this->ignoreErrors(true);
528 ++
$this->mErrorCount
;
530 if ($ignore ||
$tempIgnore) {
531 echo "error ignored! query = [$sql]\n";
532 wfDebug("SQL ERROR (ignored): $error\n");
533 $this->ignoreErrors( $ignore );
537 $message = "A database error has occurred\n" .
539 "Function: $fname\n" .
540 "Error: $errno $error\n";
541 throw new DBUnexpectedError($this, $message);
546 * @return string wikitext of a link to the server software's web site
548 function getSoftwareLink() {
549 return "[http://www.oracle.com/ Oracle]";
553 * @return string Version information from the database
555 function getServerVersion() {
556 return oci_server_version($this->mConn
);
560 * Query whether a given table exists (in the given schema, or the default mw one if not given)
562 function tableExists($table) {
563 $etable= $this->addQuotes($table);
564 $SQL = "SELECT 1 FROM user_tables WHERE table_name='$etable'";
565 $res = $this->query($SQL);
566 $count = $res ?
oci_num_rows($res) : 0;
568 $this->freeResult($res);
573 * Query whether a given column exists in the mediawiki schema
575 function fieldExists( $table, $field ) {
579 function fieldInfo( $table, $field ) {
583 function begin( $fname = '' ) {
584 $this->mTrxLevel
= 1;
586 function immediateCommit( $fname = '' ) {
589 function commit( $fname = '' ) {
590 oci_commit($this->mConn
);
591 $this->mTrxLevel
= 0;
594 /* Not even sure why this is used in the main codebase... */
595 function limitResultForUpdate($sql, $num) {
599 function strencode($s) {
600 return str_replace("'", "''", $s);
603 function encodeBlob($b) {
604 return new ORABlob($b);
606 function decodeBlob($b) {
607 return $b; //return $b->load();
610 function addQuotes( $s ) {
612 $s = $wgLang->checkTitleEncoding($s);
613 return "'" . $this->strencode($s) . "'";
616 function quote_ident( $s ) {
620 /* For now, does nothing */
621 function selectDB( $db ) {
626 * Returns an optional USE INDEX clause to go after the table, and a
627 * string to go at the end of the query
631 * @param array $options an associative array of options to be turned into
632 * an SQL query, valid keys are listed in the function.
635 function makeSelectOptions( $options ) {
636 $preLimitTail = $postLimitTail = '';
639 $noKeyOptions = array();
640 foreach ( $options as $key => $option ) {
641 if ( is_numeric( $key ) ) {
642 $noKeyOptions[$option] = true;
646 if ( isset( $options['GROUP BY'] ) ) $preLimitTail .= " GROUP BY {$options['GROUP BY']}";
647 if ( isset( $options['ORDER BY'] ) ) $preLimitTail .= " ORDER BY {$options['ORDER BY']}";
649 if (isset($options['LIMIT'])) {
650 // $tailOpts .= $this->limitResult('', $options['LIMIT'],
651 // isset($options['OFFSET']) ? $options['OFFSET']
655 #if ( isset( $noKeyOptions['FOR UPDATE'] ) ) $tailOpts .= ' FOR UPDATE';
656 #if ( isset( $noKeyOptions['LOCK IN SHARE MODE'] ) ) $tailOpts .= ' LOCK IN SHARE MODE';
657 if ( isset( $noKeyOptions['DISTINCT'] ) && isset( $noKeyOptions['DISTINCTROW'] ) ) $startOpts .= 'DISTINCT';
659 if ( isset( $options['USE INDEX'] ) && ! is_array( $options['USE INDEX'] ) ) {
660 $useIndex = $this->useIndexClause( $options['USE INDEX'] );
665 return array( $startOpts, $useIndex, $preLimitTail, $postLimitTail );
668 public function setTimeout( $timeout ) {
673 wfDebug( "Function ping() not written for DatabasePostgres.php yet");
678 } // end DatabaseOracle class