"@todo no-op" sounds a bit peculiar. ;)
[lhc/web/wiklou.git] / includes / DatabaseOracle.php
1 <?php
2
3 /**
4 * This is the Oracle database abstraction layer.
5 */
6 class ORABlob {
7 var $mData;
8
9 function __construct($data) {
10 $this->mData = $data;
11 }
12
13 function getData() {
14 return $this->mData;
15 }
16 }
17
18 /**
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.
22 */
23 class ORAResult {
24 private $rows;
25 private $cursor;
26 private $stmt;
27 private $nrows;
28 private $db;
29
30 function __construct(&$db, $stmt) {
31 $this->db =& $db;
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__);
35 return;
36 }
37
38 $this->cursor = 0;
39 $this->stmt = $stmt;
40 }
41
42 function free() {
43 oci_free_statement($this->stmt);
44 }
45
46 function seek($row) {
47 $this->cursor = min($row, $this->nrows);
48 }
49
50 function numRows() {
51 return $this->nrows;
52 }
53
54 function numFields() {
55 return oci_num_fields($this->stmt);
56 }
57
58 function fetchObject() {
59 if ($this->cursor >= $this->nrows)
60 return false;
61
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));
66 $ret->$lc = $v;
67 }
68
69 return $ret;
70 }
71
72 function fetchAssoc() {
73 if ($this->cursor >= $this->nrows)
74 return false;
75
76 $row = $this->rows[$this->cursor++];
77 $ret = array();
78 foreach ($row as $k => $v) {
79 $lc = strtolower(oci_field_name($this->stmt, $k + 1));
80 $ret[$lc] = $v;
81 $ret[$k] = $v;
82 }
83 return $ret;
84 }
85 };
86
87 class DatabaseOracle extends Database {
88 var $mInsertId = NULL;
89 var $mLastResult = NULL;
90 var $numeric_version = NULL;
91 var $lastResult = null;
92 var $cursor = 0;
93 var $mAffectedRows;
94
95 function DatabaseOracle($server = false, $user = false, $password = false, $dbName = false,
96 $failFunction = false, $flags = 0 )
97 {
98
99 global $wgOut;
100 # Can't get a reference if it hasn't been set yet
101 if ( !isset( $wgOut ) ) {
102 $wgOut = NULL;
103 }
104 $this->mOut =& $wgOut;
105 $this->mFailFunction = $failFunction;
106 $this->mFlags = $flags;
107 $this->open( $server, $user, $password, $dbName);
108
109 }
110
111 function cascadingDeletes() {
112 return true;
113 }
114 function cleanupTriggers() {
115 return true;
116 }
117 function strictIPs() {
118 return true;
119 }
120 function realTimestamps() {
121 return true;
122 }
123 function implicitGroupby() {
124 return false;
125 }
126 function searchableIPs() {
127 return true;
128 }
129
130 static function newFromParams( $server = false, $user = false, $password = false, $dbName = false,
131 $failFunction = false, $flags = 0)
132 {
133 return new DatabaseOracle( $server, $user, $password, $dbName, $failFunction, $flags );
134 }
135
136 /**
137 * Usually aborts on failure
138 * If the failFunction is set to a non-zero integer, returns success
139 */
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" );
143 }
144
145 # Needed for proper UTF-8 functionality
146 putenv("NLS_LANG=AMERICAN_AMERICA.AL32UTF8");
147
148 $this->close();
149 $this->mServer = $server;
150 $this->mUser = $user;
151 $this->mPassword = $password;
152 $this->mDBname = $dbName;
153
154 if (!strlen($user)) { ## e.g. the class is being loaded
155 return;
156 }
157
158 error_reporting( E_ALL );
159 $this->mConn = oci_connect($user, $password, $dbName);
160
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");
165 return false;
166 }
167
168 $this->mOpened = true;
169 return $this->mConn;
170 }
171
172 /**
173 * Closes a database connection, if it is open
174 * Returns success, true if already closed
175 */
176 function close() {
177 $this->mOpened = false;
178 if ( $this->mConn ) {
179 return oci_close( $this->mConn );
180 } else {
181 return true;
182 }
183 }
184
185 function execFlags() {
186 return $this->mTrxLevel ? OCI_DEFAULT : OCI_COMMIT_ON_SUCCESS;
187 }
188
189 function doQuery($sql) {
190 wfDebug("SQL: [$sql]\n");
191 if (!mb_check_encoding($sql)) {
192 throw new MWException("SQL encoding is invalid");
193 }
194
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__);
198 }
199
200 if (oci_execute($stmt, $this->execFlags()) == false) {
201 $e = oci_error($stmt);
202 $this->reportQueryError($e['message'], $e['code'], $sql, __FUNCTION__);
203 }
204 if (oci_statement_type($stmt) == "SELECT")
205 return new ORAResult($this, $stmt);
206 else {
207 $this->mAffectedRows = oci_num_rows($stmt);
208 return true;
209 }
210 }
211
212 function queryIgnore($sql, $fname = '') {
213 return $this->query($sql, $fname, true);
214 }
215
216 function freeResult($res) {
217 $res->free();
218 }
219
220 function fetchObject($res) {
221 return $res->fetchObject();
222 }
223
224 function fetchRow($res) {
225 return $res->fetchAssoc();
226 }
227
228 function numRows($res) {
229 return $res->numRows();
230 }
231
232 function numFields($res) {
233 return $res->numFields();
234 }
235
236 function fieldName($stmt, $n) {
237 return pg_field_name($stmt, $n);
238 }
239
240 /**
241 * This must be called after nextSequenceVal
242 */
243 function insertId() {
244 return $this->mInsertId;
245 }
246
247 function dataSeek($res, $row) {
248 $res->seek($row);
249 }
250
251 function lastError() {
252 if ($this->mConn === false)
253 $e = oci_error();
254 else
255 $e = oci_error($this->mConn);
256 return $e['message'];
257 }
258
259 function lastErrno() {
260 if ($this->mConn === false)
261 $e = oci_error();
262 else
263 $e = oci_error($this->mConn);
264 return $e['code'];
265 }
266
267 function affectedRows() {
268 return $this->mAffectedRows;
269 }
270
271 /**
272 * Returns information about an index
273 * If errors are explicitly ignored, returns NULL on failure
274 */
275 function indexInfo( $table, $index, $fname = 'Database::indexExists' ) {
276 return false;
277 }
278
279 function indexUnique ($table, $index, $fname = 'Database::indexUnique' ) {
280 return false;
281 }
282
283 function insert( $table, $a, $fname = 'Database::insert', $options = array() ) {
284 if (!is_array($options))
285 $options = array($options);
286
287 #if (in_array('IGNORE', $options))
288 # $oldIgnore = $this->ignoreErrors(true);
289
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))) {
294 $a = array($a);
295 }
296 foreach ($a as $row) {
297 $this->insertOneRow($table, $row, $fname);
298 }
299 //$this->ignoreErrors($oldIgnore);
300 $retVal = true;
301
302 //if (in_array('IGNORE', $options))
303 // $this->ignoreErrors($oldIgnore);
304
305 return $retVal;
306 }
307
308 function insertOneRow($table, $row, $fname) {
309 // "INSERT INTO tables (a, b, c)"
310 $sql = "INSERT INTO " . $this->tableName($table) . " (" . join(',', array_keys($row)) . ')';
311 $sql .= " VALUES (";
312
313 // for each value, append ":key"
314 $first = true;
315 $returning = '';
316 foreach ($row as $col => $val) {
317 if (is_object($val)) {
318 $what = "EMPTY_BLOB()";
319 assert($returning === '');
320 $returning = " RETURNING $col INTO :bval";
321 $blobcol = $col;
322 } else
323 $what = ":$col";
324
325 if ($first)
326 $sql .= "$what";
327 else
328 $sql.= ", $what";
329 $first = false;
330 }
331 $sql .= ") $returning";
332
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__);
338 }
339 }
340
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']);
344 }
345
346 if (strlen($returning))
347 oci_bind_by_name($stmt, ":bval", $bval, -1, SQLT_BLOB);
348
349 if (oci_execute($stmt, OCI_DEFAULT) === false) {
350 $e = oci_error($stmt);
351 $this->reportQueryError($e['message'], $e['code'], $sql, __METHOD__);
352 }
353 if (strlen($returning)) {
354 $bval->save($row[$blobcol]->getData());
355 $bval->free();
356 }
357 if (!$this->mTrxLevel)
358 oci_commit($this->mConn);
359
360 oci_free_statement($stmt);
361 }
362
363 function tableName( $name ) {
364 # Replace reserved words with better ones
365 switch( $name ) {
366 case 'user':
367 return 'mwuser';
368 case 'text':
369 return 'pagecontent';
370 default:
371 return $name;
372 }
373 }
374
375 /**
376 * Return the next in a sequence, save the value for retrieval via insertId()
377 */
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;
384 }
385
386 /**
387 * ORacle does not have a "USE INDEX" clause, so return an empty string
388 */
389 function useIndexClause($index) {
390 return '';
391 }
392
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
398 #
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
401 # occurred in MySQL
402 function replace( $table, $uniqueIndexes, $rows, $fname = 'Database::replace' ) {
403 $table = $this->tableName($table);
404
405 if (count($rows)==0) {
406 return;
407 }
408
409 # Single row case
410 if (!is_array(reset($rows))) {
411 $rows = array($rows);
412 }
413
414 foreach( $rows as $row ) {
415 # Delete rows which collide
416 if ( $uniqueIndexes ) {
417 $sql = "DELETE FROM $table WHERE ";
418 $first = true;
419 foreach ( $uniqueIndexes as $index ) {
420 if ( $first ) {
421 $first = false;
422 $sql .= "(";
423 } else {
424 $sql .= ') OR (';
425 }
426 if ( is_array( $index ) ) {
427 $first2 = true;
428 foreach ( $index as $col ) {
429 if ( $first2 ) {
430 $first2 = false;
431 } else {
432 $sql .= ' AND ';
433 }
434 $sql .= $col.'=' . $this->addQuotes( $row[$col] );
435 }
436 } else {
437 $sql .= $index.'=' . $this->addQuotes( $row[$index] );
438 }
439 }
440 $sql .= ')';
441 $this->query( $sql, $fname );
442 }
443
444 # Now insert the row
445 $sql = "INSERT INTO $table (" . $this->makeList( array_keys( $row ), LIST_NAMES ) .') VALUES (' .
446 $this->makeList( $row, LIST_COMMA ) . ')';
447 $this->query($sql, $fname);
448 }
449 }
450
451 # DELETE where the condition is a join
452 function deleteJoin( $delTable, $joinTable, $delVar, $joinVar, $conds, $fname = "Database::deleteJoin" ) {
453 if ( !$conds ) {
454 throw new DBUnexpectedError($this, 'Database::deleteJoin() called with empty $conds' );
455 }
456
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 );
462 }
463 $sql .= ')';
464
465 $this->query( $sql, $fname );
466 }
467
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") {
478 $size=$row->size-4;
479 } else {
480 $size=$row->size;
481 }
482 $this->freeResult( $res );
483 return $size;
484 }
485
486 function lowPriorityOption() {
487 return '';
488 }
489
490 function limitResult($sql, $limit, $offset) {
491 if ($offset === false)
492 $offset = 0;
493 return "SELECT * FROM ($sql) WHERE rownum >= (1 + $offset) AND rownum < 1 + $limit + $offset";
494 }
495
496 /**
497 * Returns an SQL expression for a simple conditional.
498 * Uses CASE on Oracle
499 *
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
504 */
505 function conditional( $cond, $trueVal, $falseVal ) {
506 return " (CASE WHEN $cond THEN $trueVal ELSE $falseVal END) ";
507 }
508
509 function wasDeadlock() {
510 return $this->lastErrno() == 'OCI-00060';
511 }
512
513 function timestamp($ts = 0) {
514 return wfTimestamp(TS_ORACLE, $ts);
515 }
516
517 /**
518 * Return aggregated value function call
519 */
520 function aggregateValue ($valuedata,$valuename='value') {
521 return $valuedata;
522 }
523
524 function reportQueryError($error, $errno, $sql, $fname, $tempIgnore = false) {
525 # Ignore errors during error handling to avoid infinite
526 # recursion
527 $ignore = $this->ignoreErrors(true);
528 ++$this->mErrorCount;
529
530 if ($ignore || $tempIgnore) {
531 echo "error ignored! query = [$sql]\n";
532 wfDebug("SQL ERROR (ignored): $error\n");
533 $this->ignoreErrors( $ignore );
534 }
535 else {
536 echo "error!\n";
537 $message = "A database error has occurred\n" .
538 "Query: $sql\n" .
539 "Function: $fname\n" .
540 "Error: $errno $error\n";
541 throw new DBUnexpectedError($this, $message);
542 }
543 }
544
545 /**
546 * @return string wikitext of a link to the server software's web site
547 */
548 function getSoftwareLink() {
549 return "[http://www.oracle.com/ Oracle]";
550 }
551
552 /**
553 * @return string Version information from the database
554 */
555 function getServerVersion() {
556 return oci_server_version($this->mConn);
557 }
558
559 /**
560 * Query whether a given table exists (in the given schema, or the default mw one if not given)
561 */
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;
567 if ($res)
568 $this->freeResult($res);
569 return $count;
570 }
571
572 /**
573 * Query whether a given column exists in the mediawiki schema
574 */
575 function fieldExists( $table, $field ) {
576 return true; // XXX
577 }
578
579 function fieldInfo( $table, $field ) {
580 return false; // XXX
581 }
582
583 function begin( $fname = '' ) {
584 $this->mTrxLevel = 1;
585 }
586 function immediateCommit( $fname = '' ) {
587 return true;
588 }
589 function commit( $fname = '' ) {
590 oci_commit($this->mConn);
591 $this->mTrxLevel = 0;
592 }
593
594 /* Not even sure why this is used in the main codebase... */
595 function limitResultForUpdate($sql, $num) {
596 return $sql;
597 }
598
599 function strencode($s) {
600 return str_replace("'", "''", $s);
601 }
602
603 function encodeBlob($b) {
604 return new ORABlob($b);
605 }
606 function decodeBlob($b) {
607 return $b; //return $b->load();
608 }
609
610 function addQuotes( $s ) {
611 global $wgLang;
612 $s = $wgLang->checkTitleEncoding($s);
613 return "'" . $this->strencode($s) . "'";
614 }
615
616 function quote_ident( $s ) {
617 return $s;
618 }
619
620 /* For now, does nothing */
621 function selectDB( $db ) {
622 return true;
623 }
624
625 /**
626 * Returns an optional USE INDEX clause to go after the table, and a
627 * string to go at the end of the query
628 *
629 * @private
630 *
631 * @param array $options an associative array of options to be turned into
632 * an SQL query, valid keys are listed in the function.
633 * @return array
634 */
635 function makeSelectOptions( $options ) {
636 $preLimitTail = $postLimitTail = '';
637 $startOpts = '';
638
639 $noKeyOptions = array();
640 foreach ( $options as $key => $option ) {
641 if ( is_numeric( $key ) ) {
642 $noKeyOptions[$option] = true;
643 }
644 }
645
646 if ( isset( $options['GROUP BY'] ) ) $preLimitTail .= " GROUP BY {$options['GROUP BY']}";
647 if ( isset( $options['ORDER BY'] ) ) $preLimitTail .= " ORDER BY {$options['ORDER BY']}";
648
649 if (isset($options['LIMIT'])) {
650 // $tailOpts .= $this->limitResult('', $options['LIMIT'],
651 // isset($options['OFFSET']) ? $options['OFFSET']
652 // : false);
653 }
654
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';
658
659 if ( isset( $options['USE INDEX'] ) && ! is_array( $options['USE INDEX'] ) ) {
660 $useIndex = $this->useIndexClause( $options['USE INDEX'] );
661 } else {
662 $useIndex = '';
663 }
664
665 return array( $startOpts, $useIndex, $preLimitTail, $postLimitTail );
666 }
667
668 public function setTimeout( $timeout ) {
669 // @todo fixme no-op
670 }
671
672 function ping() {
673 wfDebug( "Function ping() not written for DatabasePostgres.php yet");
674 return true;
675 }
676
677
678 } // end DatabaseOracle class
679
680 ?>