9 class OracleBlob
extends DBObject
{
22 class DatabaseOracle
extends Database
{
23 var $mInsertId = NULL;
24 var $mLastResult = NULL;
25 var $mFetchCache = array();
26 var $mFetchID = array();
27 var $mNcols = array();
28 var $mFieldNames = array(), $mFieldTypes = array();
29 var $mAffectedRows = array();
32 function DatabaseOracle($server = false, $user = false, $password = false, $dbName = false,
33 $failFunction = false, $flags = 0, $tablePrefix = 'get from global' )
35 Database
::Database( $server, $user, $password, $dbName, $failFunction, $flags, $tablePrefix );
38 /* static */ function newFromParams( $server = false, $user = false, $password = false, $dbName = false,
39 $failFunction = false, $flags = 0, $tablePrefix = 'get from global' )
41 return new DatabaseOracle( $server, $user, $password, $dbName, $failFunction, $flags, $tablePrefix );
45 * Usually aborts on failure
46 * If the failFunction is set to a non-zero integer, returns success
48 function open( $server, $user, $password, $dbName ) {
49 if ( !function_exists( 'oci_connect' ) ) {
50 throw new DBConnectionError( $this, "Oracle functions missing, have you compiled PHP with the --with-oci8 option?\n" );
53 $this->mServer
= $server;
55 $this->mPassword
= $password;
56 $this->mDBname
= $dbName;
61 $this->mConn
= oci_new_connect($user, $password, $dbName, "AL32UTF8");
62 if ( $this->mConn
=== false ) {
63 wfDebug( "DB connection error\n" );
64 wfDebug( "Server: $server, Database: $dbName, User: $user, Password: "
65 . substr( $password, 0, 3 ) . "...\n" );
66 wfDebug( $this->lastError()."\n" );
68 $this->mOpened
= true;
74 * Closes a database connection, if it is open
75 * Returns success, true if already closed
78 $this->mOpened
= false;
80 return oci_close($this->mConn
);
86 function parseStatement($sql) {
87 $this->mErr
= $this->mLastResult
= false;
88 if (($stmt = oci_parse($this->mConn
, $sql)) === false) {
90 return $this->mLastResult
= false;
92 $this->mAffectedRows
[$stmt] = 0;
93 return $this->mLastResult
= $stmt;
96 function doQuery($sql) {
97 if (($stmt = $this->parseStatement($sql)) === false)
99 return $this->executeStatement($stmt);
102 function executeStatement($stmt) {
103 if (!oci_execute($stmt, OCI_DEFAULT
)) {
105 oci_free_statement($stmt);
108 $this->mAffectedRows
[$stmt] = oci_num_rows($stmt);
109 $this->mFetchCache
[$stmt] = array();
110 $this->mFetchID
[$stmt] = 0;
111 $this->mNcols
[$stmt] = oci_num_fields($stmt);
112 if ($this->mNcols
[$stmt] == 0)
113 return $this->mLastResult
;
114 for ($i = 1; $i <= $this->mNcols
[$stmt]; $i++
) {
115 $this->mFieldNames
[$stmt][$i] = oci_field_name($stmt, $i);
116 $this->mFieldTypes
[$stmt][$i] = oci_field_type($stmt, $i);
118 while (($o = oci_fetch_array($stmt)) !== false) {
119 foreach ($o as $key => $value) {
120 if (is_object($value)) {
121 $o[$key] = $value->load();
124 $this->mFetchCache
[$stmt][] = $o;
126 return $this->mLastResult
;
129 function queryIgnore( $sql, $fname = '' ) {
130 return $this->query( $sql, $fname, true );
133 function freeResult( $res ) {
134 if (!oci_free_statement($res)) {
135 throw new DBUnexpectedError( $this, "Unable to free Oracle result\n" );
137 unset($this->mFetchID
[$res]);
138 unset($this->mFetchCache
[$res]);
139 unset($this->mNcols
[$res]);
140 unset($this->mFieldNames
[$res]);
141 unset($this->mFieldTypes
[$res]);
144 function fetchAssoc($res) {
145 if ($this->mFetchID
[$res] >= count($this->mFetchCache
[$res]))
148 for ($i = 1; $i <= $this->mNcols
[$res]; $i++
) {
149 $name = $this->mFieldNames
[$res][$i];
150 $type = $this->mFieldTypes
[$res][$i];
151 if (isset($this->mFetchCache
[$res][$this->mFetchID
[$res]][$name]))
152 $value = $this->mFetchCache
[$res][$this->mFetchID
[$res]][$name];
154 $key = strtolower($name);
155 wfdebug("'$key' => '$value'\n");
158 $this->mFetchID
[$res]++
;
162 function fetchRow($res) {
163 $r = $this->fetchAssoc($res);
168 foreach ($r as $key => $value) {
169 wfdebug("ret[$i]=[$value]\n");
175 function fetchObject($res) {
176 $row = $this->fetchAssoc($res);
180 foreach ($row as $key => $value)
185 function numRows($res) {
186 return count($this->mFetchCache
[$res]);
188 function numFields( $res ) { return pg_num_fields( $res ); }
189 function fieldName( $res, $n ) { return pg_field_name( $res, $n ); }
192 * This must be called after nextSequenceVal
194 function insertId() {
195 return $this->mInsertId
;
198 function dataSeek($res, $row) {
199 $this->mFetchID
[$res] = $row;
202 function lastError() {
203 if ($this->mErr
=== false) {
204 if ($this->mLastResult
!== false) $what = $this->mLastResult
;
205 else if ($this->mConn
!== false) $what = $this->mConn
;
207 $err = ($what !== false) ?
oci_error($what) : oci_error();
209 $this->mErr
= 'no error';
211 $this->mErr
= $err['message'];
213 return str_replace("\n", '<br />', $this->mErr
);
215 function lastErrno() {
219 function affectedRows() {
220 return $this->mAffectedRows
[$this->mLastResult
];
224 * Returns information about an index
225 * If errors are explicitly ignored, returns NULL on failure
227 function indexInfo ($table, $index, $fname = 'Database::indexInfo' ) {
228 $table = $this->tableName($table, true);
229 if ($index == 'PRIMARY')
230 $index = "${table}_pk";
231 $sql = "SELECT uniqueness FROM all_indexes WHERE table_name='" .
232 $table . "' AND index_name='" .
233 $this->strencode(strtoupper($index)) . "'";
234 $res = $this->query($sql, $fname);
237 if (($row = $this->fetchObject($res)) == NULL)
239 $this->freeResult($res);
240 $row->Non_unique
= !$row->uniqueness
;
244 function indexUnique ($table, $index, $fname = 'indexUnique') {
245 if (!($i = $this->indexInfo($table, $index, $fname)))
247 return $i->uniqueness
== 'UNIQUE';
250 function fieldInfo( $table, $field ) {
252 $o->multiple_key
= true; /* XXX */
256 function getColumnInformation($table, $field) {
257 $table = $this->tableName($table, true);
258 $field = strtoupper($field);
260 $res = $this->doQuery("SELECT * FROM all_tab_columns " .
261 "WHERE table_name='".$table."' " .
262 "AND column_name='".$field."'");
265 $o = $this->fetchObject($res);
266 $this->freeResult($res);
270 function fieldExists( $table, $field, $fname = 'Database::fieldExists' ) {
271 $column = $this->getColumnInformation($table, $field);
277 function tableName($name, $forddl = false) {
278 # First run any transformations from the parent object
279 $name = parent
::tableName( $name );
281 # Replace backticks into empty
282 # Note: "foo" and foo are not the same in Oracle!
283 $name = str_replace('`', '', $name);
285 # Now quote Oracle reserved keywords
293 return '"' . $name . '"';
296 return strtoupper($name);
300 function strencode( $s ) {
301 return str_replace("'", "''", $s);
305 * Return the next in a sequence, save the value for retrieval via insertId()
307 function nextSequenceValue( $seqName ) {
308 $r = $this->doQuery("SELECT $seqName.nextval AS val FROM dual");
309 $o = $this->fetchObject($r);
310 $this->freeResult($r);
311 return $this->mInsertId
= (int)$o->val
;
316 * PostgreSQL doesn't have them and returns ""
318 function useIndexClause( $index ) {
322 # REPLACE query wrapper
323 # PostgreSQL simulates this with a DELETE followed by INSERT
324 # $row is the row to insert, an associative array
325 # $uniqueIndexes is an array of indexes. Each element may be either a
326 # field name or an array of field names
328 # It may be more efficient to leave off unique indexes which are unlikely to collide.
329 # However if you do this, you run the risk of encountering errors which wouldn't have
331 function replace( $table, $uniqueIndexes, $rows, $fname = 'Database::replace' ) {
332 $table = $this->tableName( $table );
334 if (count($rows)==0) {
339 if ( !is_array( reset( $rows ) ) ) {
340 $rows = array( $rows );
343 foreach( $rows as $row ) {
344 # Delete rows which collide
345 if ( $uniqueIndexes ) {
346 $sql = "DELETE FROM $table WHERE ";
348 foreach ( $uniqueIndexes as $index ) {
355 if ( is_array( $index ) ) {
357 foreach ( $index as $col ) {
363 $sql .= $col.'=' . $this->addQuotes( $row[$col] );
366 $sql .= $index.'=' . $this->addQuotes( $row[$index] );
370 $this->query( $sql, $fname );
374 $sql = "INSERT INTO $table (" . $this->makeList( array_keys( $row ), LIST_NAMES
) .') VALUES (' .
375 $this->makeList( $row, LIST_COMMA
) . ')';
376 $this->query( $sql, $fname );
380 # DELETE where the condition is a join
381 function deleteJoin( $delTable, $joinTable, $delVar, $joinVar, $conds, $fname = "Database::deleteJoin" ) {
383 throw new DBUnexpectedError( $this, 'Database::deleteJoin() called with empty $conds' );
386 $delTable = $this->tableName( $delTable );
387 $joinTable = $this->tableName( $joinTable );
388 $sql = "DELETE FROM $delTable WHERE $delVar IN (SELECT $joinVar FROM $joinTable ";
389 if ( $conds != '*' ) {
390 $sql .= 'WHERE ' . $this->makeList( $conds, LIST_AND
);
394 $this->query( $sql, $fname );
397 # Returns the size of a text field, or -1 for "unlimited"
398 function textFieldSize( $table, $field ) {
399 $table = $this->tableName( $table );
400 $sql = "SELECT t.typname as ftype,a.atttypmod as size
401 FROM pg_class c, pg_attribute a, pg_type t
402 WHERE relname='$table' AND a.attrelid=c.oid AND
403 a.atttypid=t.oid and a.attname='$field'";
404 $res =$this->query($sql);
405 $row=$this->fetchObject($res);
406 if ($row->ftype
=="varchar") {
411 $this->freeResult( $res );
415 function lowPriorityOption() {
419 function limitResult($sql, $limit, $offset) {
420 $ret = "SELECT * FROM ($sql) WHERE ROWNUM < " . ((int)$limit +
(int)($offset+
1));
421 if (is_numeric($offset))
422 $ret .= " AND ROWNUM >= " . (int)$offset;
425 function limitResultForUpdate($sql, $limit) {
429 * Returns an SQL expression for a simple conditional.
430 * Uses CASE on PostgreSQL.
432 * @param string $cond SQL expression which will result in a boolean value
433 * @param string $trueVal SQL expression to return if true
434 * @param string $falseVal SQL expression to return if false
435 * @return string SQL fragment
437 function conditional( $cond, $trueVal, $falseVal ) {
438 return " (CASE WHEN $cond THEN $trueVal ELSE $falseVal END) ";
441 # FIXME: actually detecting deadlocks might be nice
442 function wasDeadlock() {
446 # Return DB-style timestamp used for MySQL schema
447 function timestamp($ts = 0) {
448 return $this->strencode(wfTimestamp(TS_ORACLE
, $ts));
449 # return "TO_TIMESTAMP('" . $this->strencode(wfTimestamp(TS_DB, $ts)) . "', 'RRRR-MM-DD HH24:MI:SS')";
453 * Return aggregated value function call
455 function aggregateValue ($valuedata,$valuename='value') {
460 function reportQueryError( $error, $errno, $sql, $fname, $tempIgnore = false ) {
461 $message = "A database error has occurred\n" .
463 "Function: $fname\n" .
464 "Error: $errno $error\n";
465 throw new DBUnexpectedError($this, $message);
469 * @return string wikitext of a link to the server software's web site
471 function getSoftwareLink() {
472 return "[http://www.oracle.com/ Oracle]";
476 * @return string Version information from the database
478 function getServerVersion() {
479 return oci_server_version($this->mConn
);
482 function setSchema($schema=false) {
483 $schemas=$this->mSchemas
;
484 if ($schema) { array_unshift($schemas,$schema); }
485 $searchpath=$this->makeList($schemas,LIST_NAMES
);
486 $this->query("SET search_path = $searchpath");
492 function immediateCommit( $fname = 'Database::immediateCommit' ) {
493 oci_commit($this->mConn
);
494 $this->mTrxLevel
= 0;
496 function rollback( $fname = 'Database::rollback' ) {
497 oci_rollback($this->mConn
);
498 $this->mTrxLevel
= 0;
503 function getStatus($which=null) {
504 $result = array('Threads_running' => 0, 'Threads_connected' => 0);
509 * Returns an optional USE INDEX clause to go after the table, and a
510 * string to go at the end of the query
514 * @param array $options an associative array of options to be turned into
515 * an SQL query, valid keys are listed in the function.
518 function makeSelectOptions($options) {
521 if (isset( $options['ORDER BY'])) {
522 $tailOpts .= " ORDER BY {$options['ORDER BY']}";
525 return array('', $tailOpts);
528 function maxListLen() {
533 * Query whether a given table exists
535 function tableExists( $table ) {
536 $table = $this->tableName($table, true);
537 $res = $this->query( "SELECT COUNT(*) as NUM FROM user_tables WHERE table_name='"
541 $row = $this->fetchObject($res);
542 $this->freeResult($res);
543 return $row->num
>= 1;
547 * UPDATE wrapper, takes a condition array and a SET array
549 function update( $table, $values, $conds, $fname = 'Database::update' ) {
550 $table = $this->tableName( $table );
552 $sql = "UPDATE $table SET ";
554 foreach ($values as $field => $v) {
559 $sql .= "$field = :n$field ";
561 if ( $conds != '*' ) {
562 $sql .= " WHERE " . $this->makeList( $conds, LIST_AND
);
564 $stmt = $this->parseStatement($sql);
565 if ($stmt === false) {
566 $this->reportQueryError( $this->lastError(), $this->lastErrno(), $stmt );
570 wfDebug("SQL: $sql\n");
572 foreach ($values as $field => $v) {
573 oci_bind_by_name($stmt, ":n$field", $values[$field]);
575 $s .= " [$field] = [$v]\n";
578 wfdebug(" PH: $s\n");
579 $ret = $this->executeStatement($stmt);
584 * INSERT wrapper, inserts an array into a table
586 * $a may be a single associative array, or an array of these with numeric keys, for
589 * Usually aborts on failure
590 * If errors are explicitly ignored, returns success
592 function insert( $table, $a, $fname = 'Database::insert', $options = array() ) {
593 # No rows to insert, easy just return now
594 if ( !count( $a ) ) {
598 $table = $this->tableName( $table );
599 if (!is_array($options))
600 $options = array($options);
603 if (in_array('IGNORE', $options))
604 $oldIgnore = $this->ignoreErrors( true );
606 if ( isset( $a[0] ) && is_array( $a[0] ) ) {
608 $keys = array_keys( $a[0] );
611 $keys = array_keys( $a );
614 $sql = "INSERT INTO $table (" . implode( ',', $keys ) . ') VALUES (';
617 foreach ($a as $key => $value) {
622 if (is_object($value) && $value->isLOB()) {
623 $sql .= "EMPTY_BLOB()";
624 $return = "RETURNING $key INTO :bobj";
630 if ($this->debug()) {
631 wfDebug("SQL: $sql\n");
634 if (($stmt = $this->parseStatement($sql)) === false) {
635 $this->reportQueryError($this->lastError(), $this->lastErrno(), $sql, $fname);
636 $this->ignoreErrors($oldIgnore);
641 * If we're inserting multiple rows, parse the statement once and
642 * execute it for each set of values. Otherwise, convert it into an
648 foreach ($a as $key => $row) {
652 foreach ($row as $k => $value) {
653 if (is_object($value) && $value->isLOB()) {
654 $blob = oci_new_descriptor($this->mConn
, OCI_D_LOB
);
655 $bdata = $value->data();
656 oci_bind_by_name($stmt, ":bobj", $blob, -1, OCI_B_BLOB
);
658 oci_bind_by_name($stmt, ":$k", $a[$key][$k], -1);
660 $s .= " [$k] = {$row[$k]}";
663 wfDebug(" PH: $s\n");
664 if (($s = $this->executeStatement($stmt)) === false) {
665 $this->reportQueryError($this->lastError(), $this->lastErrno(), $sql, $fname);
666 $this->ignoreErrors($oldIgnore);
674 $this->ignoreErrors($oldIgnore);
675 return $this->mLastResult
= $s;
682 function encodeBlob($b) {
683 return new OracleBlob($b);