00001 <?php
00008 class PostgresField {
00009 private $name, $tablename, $type, $nullable, $max_length;
00010
00011 static function fromText($db, $table, $field) {
00012 global $wgDBmwschema;
00013
00014 $q = <<<END
00015 SELECT
00016 CASE WHEN typname = 'int2' THEN 'smallint'
00017 WHEN typname = 'int4' THEN 'integer'
00018 WHEN typname = 'int8' THEN 'bigint'
00019 WHEN typname = 'bpchar' THEN 'char'
00020 ELSE typname END AS typname,
00021 attnotnull, attlen
00022 FROM pg_class, pg_namespace, pg_attribute, pg_type
00023 WHERE relnamespace=pg_namespace.oid
00024 AND relkind='r'
00025 AND attrelid=pg_class.oid
00026 AND atttypid=pg_type.oid
00027 AND nspname=%s
00028 AND relname=%s
00029 AND attname=%s;
00030 END;
00031 $res = $db->query(sprintf($q,
00032 $db->addQuotes($wgDBmwschema),
00033 $db->addQuotes($table),
00034 $db->addQuotes($field)));
00035 $row = $db->fetchObject($res);
00036 if (!$row)
00037 return null;
00038 $n = new PostgresField;
00039 $n->type = $row->typname;
00040 $n->nullable = ($row->attnotnull == 'f');
00041 $n->name = $field;
00042 $n->tablename = $table;
00043 $n->max_length = $row->attlen;
00044 return $n;
00045 }
00046
00047 function name() {
00048 return $this->name;
00049 }
00050
00051 function tableName() {
00052 return $this->tablename;
00053 }
00054
00055 function type() {
00056 return $this->type;
00057 }
00058
00059 function nullable() {
00060 return $this->nullable;
00061 }
00062
00063 function maxLength() {
00064 return $this->max_length;
00065 }
00066 }
00067
00071 class DatabasePostgres extends Database {
00072 var $mInsertId = NULL;
00073 var $mLastResult = NULL;
00074 var $numeric_version = NULL;
00075 var $mAffectedRows = NULL;
00076
00077 function DatabasePostgres($server = false, $user = false, $password = false, $dbName = false,
00078 $failFunction = false, $flags = 0 )
00079 {
00080
00081 $this->mFailFunction = $failFunction;
00082 $this->mFlags = $flags;
00083 $this->open( $server, $user, $password, $dbName);
00084
00085 }
00086
00087 function cascadingDeletes() {
00088 return true;
00089 }
00090 function cleanupTriggers() {
00091 return true;
00092 }
00093 function strictIPs() {
00094 return true;
00095 }
00096 function realTimestamps() {
00097 return true;
00098 }
00099 function implicitGroupby() {
00100 return false;
00101 }
00102 function implicitOrderby() {
00103 return false;
00104 }
00105 function searchableIPs() {
00106 return true;
00107 }
00108 function functionalIndexes() {
00109 return true;
00110 }
00111
00112 function hasConstraint( $name ) {
00113 global $wgDBmwschema;
00114 $SQL = "SELECT 1 FROM pg_catalog.pg_constraint c, pg_catalog.pg_namespace n WHERE c.connamespace = n.oid AND conname = '" . pg_escape_string( $name ) . "' AND n.nspname = '" . pg_escape_string($wgDBmwschema) ."'";
00115 return $this->numRows($res = $this->doQuery($SQL));
00116 }
00117
00118 static function newFromParams( $server, $user, $password, $dbName, $failFunction = false, $flags = 0)
00119 {
00120 return new DatabasePostgres( $server, $user, $password, $dbName, $failFunction, $flags );
00121 }
00122
00127 function open( $server, $user, $password, $dbName ) {
00128 # Test for Postgres support, to avoid suppressed fatal error
00129 if ( !function_exists( 'pg_connect' ) ) {
00130 throw new DBConnectionError( $this, "Postgres functions missing, have you compiled PHP with the --with-pgsql option?\n (Note: if you recently installed PHP, you may need to restart your webserver and database)\n" );
00131 }
00132
00133 global $wgDBport;
00134
00135 if (!strlen($user)) { ## e.g. the class is being loaded
00136 return;
00137 }
00138 $this->close();
00139 $this->mServer = $server;
00140 $this->mPort = $port = $wgDBport;
00141 $this->mUser = $user;
00142 $this->mPassword = $password;
00143 $this->mDBname = $dbName;
00144
00145 $connectVars = array(
00146 'dbname' => $dbName,
00147 'user' => $user,
00148 'password' => $password );
00149 if ($server!=false && $server!="") {
00150 $connectVars['host'] = $server;
00151 }
00152 if ($port!=false && $port!="") {
00153 $connectVars['port'] = $port;
00154 }
00155 $connectString = $this->makeConnectionString( $connectVars );
00156
00157 $this->installErrorHandler();
00158 $this->mConn = pg_connect( $connectString );
00159 $phpError = $this->restoreErrorHandler();
00160
00161 if ( $this->mConn == false ) {
00162 wfDebug( "DB connection error\n" );
00163 wfDebug( "Server: $server, Database: $dbName, User: $user, Password: " . substr( $password, 0, 3 ) . "...\n" );
00164 wfDebug( $this->lastError()."\n" );
00165 if ( !$this->mFailFunction ) {
00166 throw new DBConnectionError( $this, $phpError );
00167 } else {
00168 return false;
00169 }
00170 }
00171
00172 $this->mOpened = true;
00173
00174 global $wgCommandLineMode;
00175 ## If called from the command-line (e.g. importDump), only show errors
00176 if ($wgCommandLineMode) {
00177 $this->doQuery( "SET client_min_messages = 'ERROR'" );
00178 }
00179
00180 $this->doQuery( "SET client_encoding='UTF8'" );
00181
00182 global $wgDBmwschema, $wgDBts2schema;
00183 if (isset( $wgDBmwschema ) && isset( $wgDBts2schema )
00184 && $wgDBmwschema !== 'mediawiki'
00185 && preg_match( '/^\w+$/', $wgDBmwschema )
00186 && preg_match( '/^\w+$/', $wgDBts2schema )
00187 ) {
00188 $safeschema = $this->quote_ident($wgDBmwschema);
00189 $safeschema2 = $this->quote_ident($wgDBts2schema);
00190 $this->doQuery( "SET search_path = $safeschema, $wgDBts2schema, public" );
00191 }
00192
00193 return $this->mConn;
00194 }
00195
00196 function makeConnectionString( $vars ) {
00197 $s = '';
00198 foreach ( $vars as $name => $value ) {
00199 $s .= "$name='" . str_replace( "'", "\\'", $value ) . "' ";
00200 }
00201 return $s;
00202 }
00203
00204
00205 function initial_setup($password, $dbName) {
00206
00207 global $wgDBname, $wgDBuser, $wgDBpassword, $wgDBsuperuser, $wgDBmwschema, $wgDBts2schema;
00208
00209 print "<li>Checking the version of Postgres...";
00210 $version = $this->getServerVersion();
00211 $PGMINVER = '8.1';
00212 if ($version < $PGMINVER) {
00213 print "<b>FAILED</b>. Required version is $PGMINVER. You have " . htmlspecialchars( $version ) . "</li>\n";
00214 dieout("</ul>");
00215 }
00216 print "version " . htmlspecialchars( $this->numeric_version ) . " is OK.</li>\n";
00217
00218 $safeuser = $this->quote_ident($wgDBuser);
00219
00220 if ($wgDBsuperuser) {
00221
00222 $SQL = "SELECT
00223 CASE WHEN usesuper IS TRUE THEN
00224 CASE WHEN usecreatedb IS TRUE THEN 3 ELSE 1 END
00225 ELSE CASE WHEN usecreatedb IS TRUE THEN 2 ELSE 0 END
00226 END AS rights
00227 FROM pg_catalog.pg_user WHERE usename = " . $this->addQuotes($wgDBsuperuser);
00228 $rows = $this->numRows($res = $this->doQuery($SQL));
00229 if (!$rows) {
00230 print "<li>ERROR: Could not read permissions for user \"" . htmlspecialchars( $wgDBsuperuser ) . "\"</li>\n";
00231 dieout('</ul>');
00232 }
00233 $perms = pg_fetch_result($res, 0, 0);
00234
00235 $SQL = "SELECT 1 FROM pg_catalog.pg_user WHERE usename = " . $this->addQuotes($wgDBuser);
00236 $rows = $this->numRows($this->doQuery($SQL));
00237 if ($rows) {
00238 print "<li>User \"" . htmlspecialchars( $wgDBuser ) . "\" already exists, skipping account creation.</li>";
00239 }
00240 else {
00241 if ($perms != 1 and $perms != 3) {
00242 print "<li>ERROR: the user \"" . htmlspecialchars( $wgDBsuperuser ) . "\" cannot create other users. ";
00243 print 'Please use a different Postgres user.</li>';
00244 dieout('</ul>');
00245 }
00246 print "<li>Creating user <b>" . htmlspecialchars( $wgDBuser ) . "</b>...";
00247 $safepass = $this->addQuotes($wgDBpassword);
00248 $SQL = "CREATE USER $safeuser NOCREATEDB PASSWORD $safepass";
00249 $this->doQuery($SQL);
00250 print "OK</li>\n";
00251 }
00252
00253 if ($dbName != $wgDBname) {
00254 $SQL = "SELECT 1 FROM pg_catalog.pg_database WHERE datname = " . $this->addQuotes($wgDBname);
00255 $rows = $this->numRows($this->doQuery($SQL));
00256 if ($rows) {
00257 print "<li>Database \"" . htmlspecialchars( $wgDBname ) . "\" already exists, skipping database creation.</li>";
00258 }
00259 else {
00260 if ($perms < 1) {
00261 print "<li>ERROR: the user \"" . htmlspecialchars( $wgDBsuperuser ) . "\" cannot create databases. ";
00262 print 'Please use a different Postgres user.</li>';
00263 dieout('</ul>');
00264 }
00265 print "<li>Creating database <b>" . htmlspecialchars( $wgDBname ) . "</b>...";
00266 $safename = $this->quote_ident($wgDBname);
00267 $SQL = "CREATE DATABASE $safename OWNER $safeuser ";
00268 $this->doQuery($SQL);
00269 print "OK</li>\n";
00270
00271 }
00272
00273
00274 print "<li>Connecting to \"" . htmlspecialchars( $wgDBname ) . "\" as superuser \"" .
00275 htmlspecialchars( $wgDBsuperuser ) . "\" to check rights...";
00276
00277 $connectVars = array();
00278 if ($this->mServer!=false && $this->mServer!="") {
00279 $connectVars['host'] = $this->mServer;
00280 }
00281 if ($this->mPort!=false && $this->mPort!="") {
00282 $connectVars['port'] = $this->mPort;
00283 }
00284 $connectVars['dbname'] = $wgDBname;
00285 $connectVars['user'] = $wgDBsuperuser;
00286 $connectVars['password'] = $password;
00287
00288 @$this->mConn = pg_connect( $this->makeConnectionString( $connectVars ) );
00289 if ( $this->mConn == false ) {
00290 print "<b>FAILED TO CONNECT!</b></li>";
00291 dieout("</ul>");
00292 }
00293 print "OK</li>\n";
00294 }
00295
00296 if ($this->numeric_version < 8.3) {
00297
00298 print "<li>Checking that tsearch2 is installed in the database \"" .
00299 htmlspecialchars( $wgDBname ) . "\"...";
00300 if (! $this->tableExists("pg_ts_cfg", $wgDBts2schema)) {
00301 print "<b>FAILED</b>. tsearch2 must be installed in the database \"" .
00302 htmlspecialchars( $wgDBname ) . "\".";
00303 print "Please see <a href='http://www.devx.com/opensource/Article/21674/0/page/2'>this article</a>";
00304 print " for instructions or ask on #postgresql on irc.freenode.net</li>\n";
00305 dieout("</ul>");
00306 }
00307 print "OK</li>\n";
00308 print "<li>Ensuring that user \"" . htmlspecialchars( $wgDBuser ) .
00309 "\" has select rights on the tsearch2 tables...";
00310 foreach (array('cfg','cfgmap','dict','parser') as $table) {
00311 $SQL = "GRANT SELECT ON pg_ts_$table TO $safeuser";
00312 $this->doQuery($SQL);
00313 }
00314 print "OK</li>\n";
00315 }
00316
00317
00318 $result = $this->schemaExists($wgDBmwschema);
00319 $safeschema = $this->quote_ident($wgDBmwschema);
00320 if (!$result) {
00321 print "<li>Creating schema <b>" . htmlspecialchars( $wgDBmwschema ) . "</b> ...";
00322 $result = $this->doQuery("CREATE SCHEMA $safeschema AUTHORIZATION $safeuser");
00323 if (!$result) {
00324 print "<b>FAILED</b>.</li>\n";
00325 dieout("</ul>");
00326 }
00327 print "OK</li>\n";
00328 }
00329 else {
00330 print "<li>Schema already exists, explicitly granting rights...\n";
00331 $safeschema2 = $this->addQuotes($wgDBmwschema);
00332 $SQL = "SELECT 'GRANT ALL ON '||pg_catalog.quote_ident(relname)||' TO $safeuser;'\n".
00333 "FROM pg_catalog.pg_class p, pg_catalog.pg_namespace n\n".
00334 "WHERE relnamespace = n.oid AND n.nspname = $safeschema2\n".
00335 "AND p.relkind IN ('r','S','v')\n";
00336 $SQL .= "UNION\n";
00337 $SQL .= "SELECT 'GRANT ALL ON FUNCTION '||pg_catalog.quote_ident(proname)||'('||\n".
00338 "pg_catalog.oidvectortypes(p.proargtypes)||') TO $safeuser;'\n".
00339 "FROM pg_catalog.pg_proc p, pg_catalog.pg_namespace n\n".
00340 "WHERE p.pronamespace = n.oid AND n.nspname = $safeschema2";
00341 $res = $this->doQuery($SQL);
00342 if (!$res) {
00343 print "<b>FAILED</b>. Could not set rights for the user.</li>\n";
00344 dieout("</ul>");
00345 }
00346 $this->doQuery("SET search_path = $safeschema");
00347 $rows = $this->numRows($res);
00348 while ($rows) {
00349 $rows--;
00350 $this->doQuery(pg_fetch_result($res, $rows, 0));
00351 }
00352 print "OK</li>";
00353 }
00354
00355
00356 $this->setup_plpgsql();
00357
00358 $wgDBsuperuser = '';
00359 return true;
00360
00361 }
00362
00363 if (!defined('POSTGRES_SEARCHPATH')) {
00364
00365 if ($this->numeric_version < 8.3) {
00366
00367 print "<li>Checking for tsearch2 in the schema \"" . htmlspecialchars( $wgDBts2schema ) . "\"...";
00368 if (! $this->tableExists("pg_ts_dict", $wgDBts2schema)) {
00369 print "<b>FAILED</b>. Make sure tsearch2 is installed. See <a href=";
00370 print "'http://www.devx.com/opensource/Article/21674/0/page/2'>this article</a>";
00371 print " for instructions.</li>\n";
00372 dieout("</ul>");
00373 }
00374 print "OK</li>\n";
00375
00376
00377 $ctype = pg_fetch_result($this->doQuery("SHOW lc_ctype"),0,0);
00378 print "<li>Checking tsearch2 permissions...";
00379
00380 error_reporting( 0 );
00381 $ts2tables = array('cfg','cfgmap','dict','parser');
00382 $safetsschema = $this->quote_ident($wgDBts2schema);
00383 foreach ( $ts2tables AS $tname ) {
00384 $SQL = "SELECT count(*) FROM $safetsschema.pg_ts_$tname";
00385 $res = $this->doQuery($SQL);
00386 if (!$res) {
00387 print "<b>FAILED</b> to access " . htmlspecialchars( "pg_ts_$tname" ) .
00388 ". Make sure that the user \"". htmlspecialchars( $wgDBuser ) .
00389 "\" has SELECT access to all four tsearch2 tables</li>\n";
00390 dieout("</ul>");
00391 }
00392 }
00393 $SQL = "SELECT ts_name FROM $safetsschema.pg_ts_cfg WHERE locale = " . $this->addQuotes( $ctype ) ;
00394 $SQL .= " ORDER BY CASE WHEN ts_name <> 'default' THEN 1 ELSE 0 END";
00395 $res = $this->doQuery($SQL);
00396 error_reporting( E_ALL );
00397 if (!$res) {
00398 print "<b>FAILED</b>. Could not determine the tsearch2 locale information</li>\n";
00399 dieout("</ul>");
00400 }
00401 print "OK</li>";
00402
00403
00404 print "<li>Verifying tsearch2 locale with " . htmlspecialchars( $ctype ) . "...";
00405 $rows = $this->numRows($res);
00406 $resetlocale = 0;
00407 if (!$rows) {
00408 print "<b>not found</b></li>\n";
00409 print "<li>Attempting to set default tsearch2 locale to \"" . htmlspecialchars( $ctype ) . "\"...";
00410 $resetlocale = 1;
00411 }
00412 else {
00413 $tsname = pg_fetch_result($res, 0, 0);
00414 if ($tsname != 'default') {
00415 print "<b>not set to default (" . htmlspecialchars( $tsname ) . ")</b>";
00416 print "<li>Attempting to change tsearch2 default locale to \"" .
00417 htmlspecialchars( $ctype ) . "\"...";
00418 $resetlocale = 1;
00419 }
00420 }
00421 if ($resetlocale) {
00422 $SQL = "UPDATE $safetsschema.pg_ts_cfg SET locale = " . $this->addQuotes( $ctype ) . " WHERE ts_name = 'default'";
00423 $res = $this->doQuery($SQL);
00424 if (!$res) {
00425 print "<b>FAILED</b>. ";
00426 print "Please make sure that the locale in pg_ts_cfg for \"default\" is set to \"" .
00427 htmlspecialchars( $ctype ) . "\"</li>\n";
00428 dieout("</ul>");
00429 }
00430 print "OK</li>";
00431 }
00432
00433
00434 $SQL = "SELECT $safetsschema.to_tsvector('default','MediaWiki tsearch2 testing')";
00435 $res = $this->doQuery($SQL);
00436 if (!$res) {
00437 print "<b>FAILED</b>. Specifically, \"" . htmlspecialchars( $SQL ) . "\" did not work.</li>";
00438 dieout("</ul>");
00439 }
00440 print "OK</li>";
00441 }
00442
00443
00444 $this->setup_plpgsql();
00445
00446
00447 $result = $this->schemaExists($wgDBmwschema);
00448 if (!$result) {
00449 print "<li>Creating schema <b>" . htmlspecialchars( $wgDBmwschema ) . "</b> ...";
00450 error_reporting( 0 );
00451 $safeschema = $this->quote_ident($wgDBmwschema);
00452 $result = $this->doQuery("CREATE SCHEMA $safeschema");
00453 error_reporting( E_ALL );
00454 if (!$result) {
00455 print "<b>FAILED</b>. The user \"" . htmlspecialchars( $wgDBuser ) .
00456 "\" must be able to access the schema. ".
00457 "You can try making them the owner of the database, or try creating the schema with a ".
00458 "different user, and then grant access to the \"" .
00459 htmlspecialchars( $wgDBuser ) . "\" user.</li>\n";
00460 dieout("</ul>");
00461 }
00462 print "OK</li>\n";
00463 }
00464 else if ($result != $wgDBuser) {
00465 print "<li>Schema \"" . htmlspecialchars( $wgDBmwschema ) . "\" exists but is not owned by \"" .
00466 htmlspecialchars( $wgDBuser ) . "\". Not ideal.</li>\n";
00467 }
00468 else {
00469 print "<li>Schema \"" . htmlspecialchars( $wgDBmwschema ) . "\" exists and is owned by \"" .
00470 htmlspecialchars( $wgDBuser ) . "\". Excellent.</li>\n";
00471 }
00472
00473
00474 print "<li>Setting the timezone to GMT for user \"" . htmlspecialchars( $wgDBuser ) . "\" ...";
00475 $SQL = "ALTER USER $safeuser SET timezone = 'GMT'";
00476 $result = pg_query($this->mConn, $SQL);
00477 if (!$result) {
00478 print "<b>FAILED</b>.</li>\n";
00479 dieout("</ul>");
00480 }
00481 print "OK</li>\n";
00482
00483 $SQL = "SET timezone = 'GMT'";
00484 $result = pg_query($this->mConn, $SQL);
00485 if (!$result) {
00486 print "<li>Failed to set timezone</li>\n";
00487 dieout("</ul>");
00488 }
00489
00490 print "<li>Setting the datestyle to ISO, YMD for user \"" . htmlspecialchars( $wgDBuser ) . "\" ...";
00491 $SQL = "ALTER USER $safeuser SET datestyle = 'ISO, YMD'";
00492 $result = pg_query($this->mConn, $SQL);
00493 if (!$result) {
00494 print "<b>FAILED</b>.</li>\n";
00495 dieout("</ul>");
00496 }
00497 print "OK</li>\n";
00498
00499 $SQL = "SET datestyle = 'ISO, YMD'";
00500 $result = pg_query($this->mConn, $SQL);
00501 if (!$result) {
00502 print "<li>Failed to set datestyle</li>\n";
00503 dieout("</ul>");
00504 }
00505
00506
00507 print "<li>Setting the search path for user \"" . htmlspecialchars( $wgDBuser ) . "\" ...";
00508 $path = $this->quote_ident($wgDBmwschema);
00509 if ($wgDBts2schema !== $wgDBmwschema)
00510 $path .= ", ". $this->quote_ident($wgDBts2schema);
00511 if ($wgDBmwschema !== 'public' and $wgDBts2schema !== 'public')
00512 $path .= ", public";
00513 $SQL = "ALTER USER $safeuser SET search_path = $path";
00514 $result = pg_query($this->mConn, $SQL);
00515 if (!$result) {
00516 print "<b>FAILED</b>.</li>\n";
00517 dieout("</ul>");
00518 }
00519 print "OK</li>\n";
00520
00521 $SQL = "SET search_path = $path";
00522 $result = pg_query($this->mConn, $SQL);
00523 if (!$result) {
00524 print "<li>Failed to set search_path</li>\n";
00525 dieout("</ul>");
00526 }
00527 define( "POSTGRES_SEARCHPATH", $path );
00528 }
00529 }
00530
00531
00532 function setup_plpgsql() {
00533 print "<li>Checking for Pl/Pgsql ...";
00534 $SQL = "SELECT 1 FROM pg_catalog.pg_language WHERE lanname = 'plpgsql'";
00535 $rows = $this->numRows($this->doQuery($SQL));
00536 if ($rows < 1) {
00537
00538 print "not installed. Attempting to install Pl/Pgsql ...";
00539 $SQL = "SELECT 1 FROM pg_catalog.pg_class c JOIN pg_catalog.pg_namespace n ON (n.oid = c.relnamespace) ".
00540 "WHERE relname = 'pg_pltemplate' AND nspname='pg_catalog'";
00541 $rows = $this->numRows($this->doQuery($SQL));
00542 if ($rows >= 1) {
00543 $olde = error_reporting(0);
00544 error_reporting($olde - E_WARNING);
00545 $result = $this->doQuery("CREATE LANGUAGE plpgsql");
00546 error_reporting($olde);
00547 if (!$result) {
00548 print "<b>FAILED</b>. You need to install the language plpgsql in the database <tt>" .
00549 htmlspecialchars( $wgDBname ) . "</tt></li>";
00550 dieout("</ul>");
00551 }
00552 }
00553 else {
00554 print "<b>FAILED</b>. You need to install the language plpgsql in the database <tt>" .
00555 htmlspecialchars( $wgDBname ) . "</tt></li>";
00556 dieout("</ul>");
00557 }
00558 }
00559 print "OK</li>\n";
00560 }
00561
00562
00567 function close() {
00568 $this->mOpened = false;
00569 if ( $this->mConn ) {
00570 return pg_close( $this->mConn );
00571 } else {
00572 return true;
00573 }
00574 }
00575
00576 function doQuery( $sql ) {
00577 if (function_exists('mb_convert_encoding')) {
00578 $sql = mb_convert_encoding($sql,'UTF-8');
00579 }
00580 $this->mLastResult = pg_query( $this->mConn, $sql);
00581 $this->mAffectedRows = NULL;
00582 return $this->mLastResult;
00583 }
00584
00585 function queryIgnore( $sql, $fname = '' ) {
00586 return $this->query( $sql, $fname, true );
00587 }
00588
00589 function freeResult( $res ) {
00590 if ( $res instanceof ResultWrapper ) {
00591 $res = $res->result;
00592 }
00593 if ( !@pg_free_result( $res ) ) {
00594 throw new DBUnexpectedError($this, "Unable to free Postgres result\n" );
00595 }
00596 }
00597
00598 function fetchObject( $res ) {
00599 if ( $res instanceof ResultWrapper ) {
00600 $res = $res->result;
00601 }
00602 @$row = pg_fetch_object( $res );
00603 # FIXME: HACK HACK HACK HACK debug
00604
00605 # TODO:
00606 # hashar : not sure if the following test really trigger if the object
00607 # fetching failed.
00608 if( pg_last_error($this->mConn) ) {
00609 throw new DBUnexpectedError($this, 'SQL error: ' . htmlspecialchars( pg_last_error($this->mConn) ) );
00610 }
00611 return $row;
00612 }
00613
00614 function fetchRow( $res ) {
00615 if ( $res instanceof ResultWrapper ) {
00616 $res = $res->result;
00617 }
00618 @$row = pg_fetch_array( $res );
00619 if( pg_last_error($this->mConn) ) {
00620 throw new DBUnexpectedError($this, 'SQL error: ' . htmlspecialchars( pg_last_error($this->mConn) ) );
00621 }
00622 return $row;
00623 }
00624
00625 function numRows( $res ) {
00626 if ( $res instanceof ResultWrapper ) {
00627 $res = $res->result;
00628 }
00629 @$n = pg_num_rows( $res );
00630 if( pg_last_error($this->mConn) ) {
00631 throw new DBUnexpectedError($this, 'SQL error: ' . htmlspecialchars( pg_last_error($this->mConn) ) );
00632 }
00633 return $n;
00634 }
00635 function numFields( $res ) {
00636 if ( $res instanceof ResultWrapper ) {
00637 $res = $res->result;
00638 }
00639 return pg_num_fields( $res );
00640 }
00641 function fieldName( $res, $n ) {
00642 if ( $res instanceof ResultWrapper ) {
00643 $res = $res->result;
00644 }
00645 return pg_field_name( $res, $n );
00646 }
00647
00651 function insertId() {
00652 return $this->mInsertId;
00653 }
00654
00655 function dataSeek( $res, $row ) {
00656 if ( $res instanceof ResultWrapper ) {
00657 $res = $res->result;
00658 }
00659 return pg_result_seek( $res, $row );
00660 }
00661
00662 function lastError() {
00663 if ( $this->mConn ) {
00664 return pg_last_error();
00665 }
00666 else {
00667 return "No database connection";
00668 }
00669 }
00670 function lastErrno() {
00671 return pg_last_error() ? 1 : 0;
00672 }
00673
00674 function affectedRows() {
00675 if ( !is_null( $this->mAffectedRows ) ) {
00676
00677 return $this->mAffectedRows;
00678 }
00679 if( empty( $this->mLastResult ) )
00680 return 0;
00681 return pg_affected_rows( $this->mLastResult );
00682 }
00683
00692 function estimateRowCount( $table, $vars='*', $conds='', $fname = 'DatabasePostgres::estimateRowCount', $options = array() ) {
00693 $options['EXPLAIN'] = true;
00694 $res = $this->select( $table, $vars, $conds, $fname, $options );
00695 $rows = -1;
00696 if ( $res ) {
00697 $row = $this->fetchRow( $res );
00698 $count = array();
00699 if( preg_match( '/rows=(\d+)/', $row[0], $count ) ) {
00700 $rows = $count[1];
00701 }
00702 $this->freeResult($res);
00703 }
00704 return $rows;
00705 }
00706
00707
00712 function indexInfo( $table, $index, $fname = 'DatabasePostgres::indexInfo' ) {
00713 $sql = "SELECT indexname FROM pg_indexes WHERE tablename='$table'";
00714 $res = $this->query( $sql, $fname );
00715 if ( !$res ) {
00716 return NULL;
00717 }
00718 while ( $row = $this->fetchObject( $res ) ) {
00719 if ( $row->indexname == $this->indexName( $index ) ) {
00720 return $row;
00721 }
00722 }
00723 return false;
00724 }
00725
00726 function indexUnique ($table, $index, $fname = 'DatabasePostgres::indexUnique' ) {
00727 $sql = "SELECT indexname FROM pg_indexes WHERE tablename='{$table}'".
00728 " AND indexdef LIKE 'CREATE UNIQUE%(" .
00729 $this->strencode( $this->indexName( $index ) ) .
00730 ")'";
00731 $res = $this->query( $sql, $fname );
00732 if ( !$res )
00733 return NULL;
00734 while ($row = $this->fetchObject( $res ))
00735 return true;
00736 return false;
00737
00738 }
00739
00753 function insert( $table, $args, $fname = 'DatabasePostgres::insert', $options = array() ) {
00754 global $wgDBversion;
00755
00756 if ( !count( $args ) ) {
00757 return true;
00758 }
00759
00760 $table = $this->tableName( $table );
00761 if (! isset( $wgDBversion ) ) {
00762 $wgDBversion = $this->getServerVersion();
00763 }
00764
00765 if ( !is_array( $options ) )
00766 $options = array( $options );
00767
00768 if ( isset( $args[0] ) && is_array( $args[0] ) ) {
00769 $multi = true;
00770 $keys = array_keys( $args[0] );
00771 }
00772 else {
00773 $multi = false;
00774 $keys = array_keys( $args );
00775 }
00776
00777
00778 $ignore = in_array( 'IGNORE', $options ) ? 'mw' : '';
00779
00780
00781 $didbegin = 0;
00782 if ( $ignore ) {
00783 if (! $this->mTrxLevel) {
00784 $this->begin();
00785 $didbegin = 1;
00786 }
00787 $olde = error_reporting( 0 );
00788
00789
00790 $numrowsinserted = 0;
00791 }
00792
00793 $sql = "INSERT INTO $table (" . implode( ',', $keys ) . ') VALUES ';
00794
00795 if ( $multi ) {
00796 if ( $wgDBversion >= 8.2 && !$ignore ) {
00797 $first = true;
00798 foreach ( $args as $row ) {
00799 if ( $first ) {
00800 $first = false;
00801 } else {
00802 $sql .= ',';
00803 }
00804 $sql .= '(' . $this->makeList( $row ) . ')';
00805 }
00806 $res = (bool)$this->query( $sql, $fname, $ignore );
00807 }
00808 else {
00809 $res = true;
00810 $origsql = $sql;
00811 foreach ( $args as $row ) {
00812 $tempsql = $origsql;
00813 $tempsql .= '(' . $this->makeList( $row ) . ')';
00814
00815 if ( $ignore ) {
00816 pg_query($this->mConn, "SAVEPOINT $ignore");
00817 }
00818
00819 $tempres = (bool)$this->query( $tempsql, $fname, $ignore );
00820
00821 if ( $ignore ) {
00822 $bar = pg_last_error();
00823 if ($bar != false) {
00824 pg_query( $this->mConn, "ROLLBACK TO $ignore" );
00825 }
00826 else {
00827 pg_query( $this->mConn, "RELEASE $ignore" );
00828 $numrowsinserted++;
00829 }
00830 }
00831
00832
00833
00834 if (! $tempres)
00835 $res = false;
00836 }
00837 }
00838 }
00839 else {
00840
00841 if ( $ignore ) {
00842 pg_query($this->mConn, "SAVEPOINT $ignore");
00843 }
00844
00845 $sql .= '(' . $this->makeList( $args ) . ')';
00846 $res = (bool)$this->query( $sql, $fname, $ignore );
00847 if ( $ignore ) {
00848 $bar = pg_last_error();
00849 if ($bar != false) {
00850 pg_query( $this->mConn, "ROLLBACK TO $ignore" );
00851 }
00852 else {
00853 pg_query( $this->mConn, "RELEASE $ignore" );
00854 $numrowsinserted++;
00855 }
00856 }
00857 }
00858 if ( $ignore ) {
00859 $olde = error_reporting( $olde );
00860 if ($didbegin) {
00861 $this->commit();
00862 }
00863
00864
00865 $this->mAffectedRows = $numrowsinserted;
00866
00867
00868 return true;
00869 }
00870
00871
00872 return $res;
00873
00874 }
00875
00876 function tableName( $name ) {
00877 # Replace reserved words with better ones
00878 switch( $name ) {
00879 case 'user':
00880 return 'mwuser';
00881 case 'text':
00882 return 'pagecontent';
00883 default:
00884 return $name;
00885 }
00886 }
00887
00891 function nextSequenceValue( $seqName ) {
00892 $safeseq = preg_replace( "/'/", "''", $seqName );
00893 $res = $this->query( "SELECT nextval('$safeseq')" );
00894 $row = $this->fetchRow( $res );
00895 $this->mInsertId = $row[0];
00896 $this->freeResult( $res );
00897 return $this->mInsertId;
00898 }
00899
00903 function currentSequenceValue( $seqName ) {
00904 $safeseq = preg_replace( "/'/", "''", $seqName );
00905 $res = $this->query( "SELECT currval('$safeseq')" );
00906 $row = $this->fetchRow( $res );
00907 $currval = $row[0];
00908 $this->freeResult( $res );
00909 return $currval;
00910 }
00911
00915 function useIndexClause( $index ) {
00916 return '';
00917 }
00918
00919 # REPLACE query wrapper
00920 # Postgres simulates this with a DELETE followed by INSERT
00921 # $row is the row to insert, an associative array
00922 # $uniqueIndexes is an array of indexes. Each element may be either a
00923 # field name or an array of field names
00924 #
00925 # It may be more efficient to leave off unique indexes which are unlikely to collide.
00926 # However if you do this, you run the risk of encountering errors which wouldn't have
00927 # occurred in MySQL
00928 function replace( $table, $uniqueIndexes, $rows, $fname = 'DatabasePostgres::replace' ) {
00929 $table = $this->tableName( $table );
00930
00931 if (count($rows)==0) {
00932 return;
00933 }
00934
00935 # Single row case
00936 if ( !is_array( reset( $rows ) ) ) {
00937 $rows = array( $rows );
00938 }
00939
00940 foreach( $rows as $row ) {
00941 # Delete rows which collide
00942 if ( $uniqueIndexes ) {
00943 $sql = "DELETE FROM $table WHERE ";
00944 $first = true;
00945 foreach ( $uniqueIndexes as $index ) {
00946 if ( $first ) {
00947 $first = false;
00948 $sql .= "(";
00949 } else {
00950 $sql .= ') OR (';
00951 }
00952 if ( is_array( $index ) ) {
00953 $first2 = true;
00954 foreach ( $index as $col ) {
00955 if ( $first2 ) {
00956 $first2 = false;
00957 } else {
00958 $sql .= ' AND ';
00959 }
00960 $sql .= $col.'=' . $this->addQuotes( $row[$col] );
00961 }
00962 } else {
00963 $sql .= $index.'=' . $this->addQuotes( $row[$index] );
00964 }
00965 }
00966 $sql .= ')';
00967 $this->query( $sql, $fname );
00968 }
00969
00970 # Now insert the row
00971 $sql = "INSERT INTO $table (" . $this->makeList( array_keys( $row ), LIST_NAMES ) .') VALUES (' .
00972 $this->makeList( $row, LIST_COMMA ) . ')';
00973 $this->query( $sql, $fname );
00974 }
00975 }
00976
00977 # DELETE where the condition is a join
00978 function deleteJoin( $delTable, $joinTable, $delVar, $joinVar, $conds, $fname = 'DatabasePostgres::deleteJoin' ) {
00979 if ( !$conds ) {
00980 throw new DBUnexpectedError($this, 'Database::deleteJoin() called with empty $conds' );
00981 }
00982
00983 $delTable = $this->tableName( $delTable );
00984 $joinTable = $this->tableName( $joinTable );
00985 $sql = "DELETE FROM $delTable WHERE $delVar IN (SELECT $joinVar FROM $joinTable ";
00986 if ( $conds != '*' ) {
00987 $sql .= 'WHERE ' . $this->makeList( $conds, LIST_AND );
00988 }
00989 $sql .= ')';
00990
00991 $this->query( $sql, $fname );
00992 }
00993
00994 # Returns the size of a text field, or -1 for "unlimited"
00995 function textFieldSize( $table, $field ) {
00996 $table = $this->tableName( $table );
00997 $sql = "SELECT t.typname as ftype,a.atttypmod as size
00998 FROM pg_class c, pg_attribute a, pg_type t
00999 WHERE relname='$table' AND a.attrelid=c.oid AND
01000 a.atttypid=t.oid and a.attname='$field'";
01001 $res =$this->query($sql);
01002 $row=$this->fetchObject($res);
01003 if ($row->ftype=="varchar") {
01004 $size=$row->size-4;
01005 } else {
01006 $size=$row->size;
01007 }
01008 $this->freeResult( $res );
01009 return $size;
01010 }
01011
01012 function lowPriorityOption() {
01013 return '';
01014 }
01015
01016 function limitResult($sql, $limit, $offset=false) {
01017 return "$sql LIMIT $limit ".(is_numeric($offset)?" OFFSET {$offset} ":"");
01018 }
01019
01029 function conditional( $cond, $trueVal, $falseVal ) {
01030 return " (CASE WHEN $cond THEN $trueVal ELSE $falseVal END) ";
01031 }
01032
01033 function wasDeadlock() {
01034 return $this->lastErrno() == '40P01';
01035 }
01036
01037 function timestamp( $ts=0 ) {
01038 return wfTimestamp(TS_POSTGRES,$ts);
01039 }
01040
01044 function aggregateValue ($valuedata,$valuename='value') {
01045 return $valuedata;
01046 }
01047
01048
01049 function reportQueryError( $error, $errno, $sql, $fname, $tempIgnore = false ) {
01050
01051 $ignore = $this->ignoreErrors( true );
01052 $this->mErrorCount++;
01053
01054 if ($ignore || $tempIgnore) {
01055 wfDebug("SQL ERROR (ignored): $error\n");
01056 $this->ignoreErrors( $ignore );
01057 }
01058 else {
01059 $message = "A database error has occurred\n" .
01060 "Query: $sql\n" .
01061 "Function: $fname\n" .
01062 "Error: $errno $error\n";
01063 throw new DBUnexpectedError($this, $message);
01064 }
01065 }
01066
01070 function getSoftwareLink() {
01071 return "[http://www.postgresql.org/ PostgreSQL]";
01072 }
01073
01077 function getServerVersion() {
01078 $versionInfo = pg_version( $this->mConn );
01079 if ( version_compare( $versionInfo['client'], '7.4.0', 'lt' ) ) {
01080
01081 $this->numeric_version = '7.3 or earlier';
01082 } elseif ( isset( $versionInfo['server'] ) ) {
01083
01084 $this->numeric_version = $versionInfo['server'];
01085 } else {
01086
01087 $this->numeric_version = pg_parameter_status( $this->mConn, 'server_version' );
01088 }
01089 return $this->numeric_version;
01090 }
01091
01096 function relationExists( $table, $types, $schema = false ) {
01097 global $wgDBmwschema;
01098 if ( !is_array( $types ) )
01099 $types = array( $types );
01100 if ( !$schema )
01101 $schema = $wgDBmwschema;
01102 $etable = $this->addQuotes( $table );
01103 $eschema = $this->addQuotes( $schema );
01104 $SQL = "SELECT 1 FROM pg_catalog.pg_class c, pg_catalog.pg_namespace n "
01105 . "WHERE c.relnamespace = n.oid AND c.relname = $etable AND n.nspname = $eschema "
01106 . "AND c.relkind IN ('" . implode("','", $types) . "')";
01107 $res = $this->query( $SQL );
01108 $count = $res ? $res->numRows() : 0;
01109 if ($res)
01110 $this->freeResult( $res );
01111 return $count ? true : false;
01112 }
01113
01114
01115
01116
01117
01118 function tableExists( $table, $schema = false ) {
01119 return $this->relationExists( $table, array( 'r', 'v' ), $schema );
01120 }
01121
01122 function sequenceExists( $sequence, $schema = false ) {
01123 return $this->relationExists( $sequence, 'S', $schema );
01124 }
01125
01126 function triggerExists( $table, $trigger ) {
01127 global $wgDBmwschema;
01128
01129 $q = <<<END
01130 SELECT 1 FROM pg_class, pg_namespace, pg_trigger
01131 WHERE relnamespace=pg_namespace.oid AND relkind='r'
01132 AND tgrelid=pg_class.oid
01133 AND nspname=%s AND relname=%s AND tgname=%s
01134 END;
01135 $res = $this->query(sprintf($q,
01136 $this->addQuotes($wgDBmwschema),
01137 $this->addQuotes($table),
01138 $this->addQuotes($trigger)));
01139 if (!$res)
01140 return NULL;
01141 $rows = $res->numRows();
01142 $this->freeResult( $res );
01143 return $rows;
01144 }
01145
01146 function ruleExists( $table, $rule ) {
01147 global $wgDBmwschema;
01148 $exists = $this->selectField("pg_rules", "rulename",
01149 array( "rulename" => $rule,
01150 "tablename" => $table,
01151 "schemaname" => $wgDBmwschema ) );
01152 return $exists === $rule;
01153 }
01154
01155 function constraintExists( $table, $constraint ) {
01156 global $wgDBmwschema;
01157 $SQL = sprintf("SELECT 1 FROM information_schema.table_constraints ".
01158 "WHERE constraint_schema = %s AND table_name = %s AND constraint_name = %s",
01159 $this->addQuotes($wgDBmwschema),
01160 $this->addQuotes($table),
01161 $this->addQuotes($constraint));
01162 $res = $this->query($SQL);
01163 if (!$res)
01164 return NULL;
01165 $rows = $res->numRows();
01166 $this->freeResult($res);
01167 return $rows;
01168 }
01169
01173 function schemaExists( $schema ) {
01174 $eschema = preg_replace("/'/", "''", $schema);
01175 $SQL = "SELECT rolname FROM pg_catalog.pg_namespace n, pg_catalog.pg_roles r "
01176 ."WHERE n.nspowner=r.oid AND n.nspname = '$eschema'";
01177 $res = $this->query( $SQL );
01178 if ( $res && $res->numRows() ) {
01179 $row = $res->fetchObject();
01180 $owner = $row->rolname;
01181 } else {
01182 $owner = false;
01183 }
01184 if ($res)
01185 $this->freeResult($res);
01186 return $owner;
01187 }
01188
01192 function fieldExists( $table, $field, $fname = 'DatabasePostgres::fieldExists' ) {
01193 global $wgDBmwschema;
01194 $etable = preg_replace("/'/", "''", $table);
01195 $eschema = preg_replace("/'/", "''", $wgDBmwschema);
01196 $ecol = preg_replace("/'/", "''", $field);
01197 $SQL = "SELECT 1 FROM pg_catalog.pg_class c, pg_catalog.pg_namespace n, pg_catalog.pg_attribute a "
01198 . "WHERE c.relnamespace = n.oid AND c.relname = '$etable' AND n.nspname = '$eschema' "
01199 . "AND a.attrelid = c.oid AND a.attname = '$ecol'";
01200 $res = $this->query( $SQL, $fname );
01201 $count = $res ? $res->numRows() : 0;
01202 if ($res)
01203 $this->freeResult( $res );
01204 return $count;
01205 }
01206
01207 function fieldInfo( $table, $field ) {
01208 return PostgresField::fromText($this, $table, $field);
01209 }
01210
01214 function fieldType( $res, $index ) {
01215 if ( $res instanceof ResultWrapper ) {
01216 $res = $res->result;
01217 }
01218 return pg_field_type( $res, $index );
01219 }
01220
01221 function begin( $fname = 'DatabasePostgres::begin' ) {
01222 $this->query( 'BEGIN', $fname );
01223 $this->mTrxLevel = 1;
01224 }
01225 function immediateCommit( $fname = 'DatabasePostgres::immediateCommit' ) {
01226 return true;
01227 }
01228 function commit( $fname = 'DatabasePostgres::commit' ) {
01229 $this->query( 'COMMIT', $fname );
01230 $this->mTrxLevel = 0;
01231 }
01232
01233
01234 function limitResultForUpdate( $sql, $num ) {
01235 return $sql;
01236 }
01237
01238 function setup_database() {
01239 global $wgVersion, $wgDBmwschema, $wgDBts2schema, $wgDBport, $wgDBuser;
01240
01241
01242
01243 $ctest = "mediawiki_test_table";
01244 $safeschema = $this->quote_ident($wgDBmwschema);
01245 if ($this->tableExists($ctest, $wgDBmwschema)) {
01246 $this->doQuery("DROP TABLE $safeschema.$ctest");
01247 }
01248 $SQL = "CREATE TABLE $safeschema.$ctest(a int)";
01249 $olde = error_reporting( 0 );
01250 $res = $this->doQuery($SQL);
01251 error_reporting( $olde );
01252 if (!$res) {
01253 print "<b>FAILED</b>. Make sure that the user \"" . htmlspecialchars( $wgDBuser ) .
01254 "\" can write to the schema \"" . htmlspecialchars( $wgDBmwschema ) . "\"</li>\n";
01255 dieout("</ul>");
01256 }
01257 $this->doQuery("DROP TABLE $safeschema.$ctest");
01258
01259 $res = dbsource( "../maintenance/postgres/tables.sql", $this);
01260
01261 ## Update version information
01262 $mwv = $this->addQuotes($wgVersion);
01263 $pgv = $this->addQuotes($this->getServerVersion());
01264 $pgu = $this->addQuotes($this->mUser);
01265 $mws = $this->addQuotes($wgDBmwschema);
01266 $tss = $this->addQuotes($wgDBts2schema);
01267 $pgp = $this->addQuotes($wgDBport);
01268 $dbn = $this->addQuotes($this->mDBname);
01269 $ctype = $this->addQuotes( pg_fetch_result($this->doQuery("SHOW lc_ctype"),0,0) );
01270
01271 $SQL = "UPDATE mediawiki_version SET mw_version=$mwv, pg_version=$pgv, pg_user=$pgu, ".
01272 "mw_schema = $mws, ts2_schema = $tss, pg_port=$pgp, pg_dbname=$dbn, ".
01273 "ctype = $ctype ".
01274 "WHERE type = 'Creation'";
01275 $this->query($SQL);
01276
01277 ## Avoid the non-standard "REPLACE INTO" syntax
01278 $f = fopen( "../maintenance/interwiki.sql", 'r' );
01279 if ($f == false ) {
01280 dieout( "<li>Could not find the interwiki.sql file");
01281 }
01282 ## We simply assume it is already empty as we have just created it
01283 $SQL = "INSERT INTO interwiki(iw_prefix,iw_url,iw_local) VALUES ";
01284 while ( ! feof( $f ) ) {
01285 $line = fgets($f,1024);
01286 $matches = array();
01287 if (!preg_match('/^\s*(\(.+?),(\d)\)/', $line, $matches)) {
01288 continue;
01289 }
01290 $this->query("$SQL $matches[1],$matches[2])");
01291 }
01292 print " (table interwiki successfully populated)...\n";
01293
01294 $this->doQuery("COMMIT");
01295 }
01296
01297 function encodeBlob( $b ) {
01298 return new Blob ( pg_escape_bytea( $b ) ) ;
01299 }
01300
01301 function decodeBlob( $b ) {
01302 if ($b instanceof Blob) {
01303 $b = $b->fetch();
01304 }
01305 return pg_unescape_bytea( $b );
01306 }
01307
01308 function strencode( $s ) { ## Should not be called by us
01309 return pg_escape_string( $s );
01310 }
01311
01312 function addQuotes( $s ) {
01313 if ( is_null( $s ) ) {
01314 return 'NULL';
01315 } else if ( is_bool( $s ) ) {
01316 return intval( $s );
01317 } else if ($s instanceof Blob) {
01318 return "'".$s->fetch($s)."'";
01319 }
01320 return "'" . pg_escape_string($s) . "'";
01321 }
01322
01323 function quote_ident( $s ) {
01324 return '"' . preg_replace( '/"/', '""', $s) . '"';
01325 }
01326
01327
01328 function selectDB( $db ) {
01329 return true;
01330 }
01331
01342 protected function replaceVars( $ins ) {
01343
01344 $ins = parent::replaceVars( $ins );
01345
01346 if ($this->numeric_version >= 8.3 && ( defined('TEXTSEARCH_COMPAT') && TEXTSEARCH_COMPAT != true )) {
01347
01348 $ins = preg_replace( "/to_tsvector\s*\(\s*'default'\s*,/", 'to_tsvector(', $ins );
01349 }
01350
01351 if ($this->numeric_version <= 8.1) {
01352 $ins = str_replace( 'USING gin', 'USING gist', $ins );
01353 }
01354
01355 return $ins;
01356 }
01357
01367 function makeSelectOptions( $options ) {
01368 $preLimitTail = $postLimitTail = '';
01369 $startOpts = $useIndex = '';
01370
01371 $noKeyOptions = array();
01372 foreach ( $options as $key => $option ) {
01373 if ( is_numeric( $key ) ) {
01374 $noKeyOptions[$option] = true;
01375 }
01376 }
01377
01378 if ( isset( $options['GROUP BY'] ) ) $preLimitTail .= " GROUP BY " . $options['GROUP BY'];
01379 if ( isset( $options['HAVING'] ) ) $preLimitTail .= " HAVING {$options['HAVING']}";
01380 if ( isset( $options['ORDER BY'] ) ) $preLimitTail .= " ORDER BY " . $options['ORDER BY'];
01381
01382
01383
01384
01385
01386
01387
01388 if ( isset( $noKeyOptions['FOR UPDATE'] ) ) $postLimitTail .= ' FOR UPDATE';
01389 if ( isset( $noKeyOptions['LOCK IN SHARE MODE'] ) ) $postLimitTail .= ' LOCK IN SHARE MODE';
01390 if ( isset( $noKeyOptions['DISTINCT'] ) || isset( $noKeyOptions['DISTINCTROW'] ) ) $startOpts .= 'DISTINCT';
01391
01392 return array( $startOpts, $useIndex, $preLimitTail, $postLimitTail );
01393 }
01394
01395 public function setTimeout( $timeout ) {
01396
01397 }
01398
01399 function ping() {
01400 wfDebug( "Function ping() not written for DatabasePostgres.php yet");
01401 return true;
01402 }
01403
01408 public function getLag() {
01409 # Not implemented for PostgreSQL
01410 return false;
01411 }
01412
01413 function setFakeSlaveLag( $lag ) {}
01414 function setFakeMaster( $enabled = true ) {}
01415
01416 function getDBname() {
01417 return $this->mDBname;
01418 }
01419
01420 function getServer() {
01421 return $this->mServer;
01422 }
01423
01424 function buildConcat( $stringList ) {
01425 return implode( ' || ', $stringList );
01426 }
01427
01428
01429
01430 public function lock( $lockName, $method ) {
01431 return true;
01432 }
01433 public function unlock( $lockName, $method ) {
01434 return true;
01435 }
01436
01437 public function getSearchEngine() {
01438 return "SearchPostgres";
01439 }
01440
01441 }