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