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