Remove ancient and deprecated newFromParams() constructor wrappers from various Datab...
[lhc/web/wiklou.git] / includes / db / DatabasePostgres.php
1 <?php
2 /**
3 * This is the Postgres database abstraction layer.
4 *
5 * @file
6 * @ingroup Database
7 */
8
9 class PostgresField implements Field {
10 private $name, $tablename, $type, $nullable, $max_length, $deferred, $deferrable, $conname;
11
12 static function fromText($db, $table, $field) {
13 global $wgDBmwschema;
14
15 $q = <<<SQL
16 SELECT
17 attnotnull, attlen, COALESCE(conname, '') AS conname,
18 COALESCE(condeferred, 'f') AS deferred,
19 COALESCE(condeferrable, 'f') AS deferrable,
20 CASE WHEN typname = 'int2' THEN 'smallint'
21 WHEN typname = 'int4' THEN 'integer'
22 WHEN typname = 'int8' THEN 'bigint'
23 WHEN typname = 'bpchar' THEN 'char'
24 ELSE typname END AS typname
25 FROM pg_class c
26 JOIN pg_namespace n ON (n.oid = c.relnamespace)
27 JOIN pg_attribute a ON (a.attrelid = c.oid)
28 JOIN pg_type t ON (t.oid = a.atttypid)
29 LEFT JOIN pg_constraint o ON (o.conrelid = c.oid AND a.attnum = ANY(o.conkey) AND o.contype = 'f')
30 WHERE relkind = 'r'
31 AND nspname=%s
32 AND relname=%s
33 AND attname=%s;
34 SQL;
35
36 $table = $db->tableName( $table );
37 $res = $db->query(
38 sprintf( $q,
39 $db->addQuotes( $wgDBmwschema ),
40 $db->addQuotes( $table ),
41 $db->addQuotes( $field )
42 )
43 );
44 $row = $db->fetchObject( $res );
45 if ( !$row ) {
46 return null;
47 }
48 $n = new PostgresField;
49 $n->type = $row->typname;
50 $n->nullable = ( $row->attnotnull == 'f' );
51 $n->name = $field;
52 $n->tablename = $table;
53 $n->max_length = $row->attlen;
54 $n->deferrable = ( $row->deferrable == 't' );
55 $n->deferred = ( $row->deferred == 't' );
56 $n->conname = $row->conname;
57 return $n;
58 }
59
60 function name() {
61 return $this->name;
62 }
63
64 function tableName() {
65 return $this->tablename;
66 }
67
68 function type() {
69 return $this->type;
70 }
71
72 function isNullable() {
73 return $this->nullable;
74 }
75
76 function maxLength() {
77 return $this->max_length;
78 }
79
80 function is_deferrable() {
81 return $this->deferrable;
82 }
83
84 function is_deferred() {
85 return $this->deferred;
86 }
87
88 function conname() {
89 return $this->conname;
90 }
91
92 }
93
94 /**
95 * @ingroup Database
96 */
97 class DatabasePostgres extends DatabaseBase {
98 var $mInsertId = null;
99 var $mLastResult = null;
100 var $numeric_version = null;
101 var $mAffectedRows = null;
102
103 function __construct( $server = false, $user = false, $password = false, $dbName = false,
104 $flags = 0 )
105 {
106 $this->mFlags = $flags;
107 $this->open( $server, $user, $password, $dbName );
108 }
109
110 function getType() {
111 return 'postgres';
112 }
113
114 function cascadingDeletes() {
115 return true;
116 }
117 function cleanupTriggers() {
118 return true;
119 }
120 function strictIPs() {
121 return true;
122 }
123 function realTimestamps() {
124 return true;
125 }
126 function implicitGroupby() {
127 return false;
128 }
129 function implicitOrderby() {
130 return false;
131 }
132 function searchableIPs() {
133 return true;
134 }
135 function functionalIndexes() {
136 return true;
137 }
138
139 function hasConstraint( $name ) {
140 global $wgDBmwschema;
141 $SQL = "SELECT 1 FROM pg_catalog.pg_constraint c, pg_catalog.pg_namespace n WHERE c.connamespace = n.oid AND conname = '" .
142 pg_escape_string( $this->mConn, $name ) . "' AND n.nspname = '" . pg_escape_string( $this->mConn, $wgDBmwschema ) ."'";
143 $res = $this->doQuery( $SQL );
144 return $this->numRows( $res );
145 }
146
147 /**
148 * Usually aborts on failure
149 */
150 function open( $server, $user, $password, $dbName ) {
151 # Test for Postgres support, to avoid suppressed fatal error
152 if ( !function_exists( 'pg_connect' ) ) {
153 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" );
154 }
155
156 global $wgDBport;
157
158 if ( !strlen( $user ) ) { # e.g. the class is being loaded
159 return;
160 }
161 $this->close();
162 $this->mServer = $server;
163 $this->mPort = $port = $wgDBport;
164 $this->mUser = $user;
165 $this->mPassword = $password;
166 $this->mDBname = $dbName;
167
168 $connectVars = array(
169 'dbname' => $dbName,
170 'user' => $user,
171 'password' => $password
172 );
173 if ( $server != false && $server != '' ) {
174 $connectVars['host'] = $server;
175 }
176 if ( $port != false && $port != '' ) {
177 $connectVars['port'] = $port;
178 }
179 $connectString = $this->makeConnectionString( $connectVars, PGSQL_CONNECT_FORCE_NEW );
180
181 $this->installErrorHandler();
182 $this->mConn = pg_connect( $connectString );
183 $phpError = $this->restoreErrorHandler();
184
185 if ( !$this->mConn ) {
186 wfDebug( "DB connection error\n" );
187 wfDebug( "Server: $server, Database: $dbName, User: $user, Password: " . substr( $password, 0, 3 ) . "...\n" );
188 wfDebug( $this->lastError() . "\n" );
189 throw new DBConnectionError( $this, $phpError );
190 }
191
192 $this->mOpened = true;
193
194 global $wgCommandLineMode;
195 # If called from the command-line (e.g. importDump), only show errors
196 if ( $wgCommandLineMode ) {
197 $this->doQuery( "SET client_min_messages = 'ERROR'" );
198 }
199
200 $this->doQuery( "SET client_encoding='UTF8'" );
201
202 global $wgDBmwschema, $wgDBts2schema;
203 if ( isset( $wgDBmwschema ) && isset( $wgDBts2schema )
204 && $wgDBmwschema !== 'mediawiki'
205 && preg_match( '/^\w+$/', $wgDBmwschema )
206 && preg_match( '/^\w+$/', $wgDBts2schema )
207 ) {
208 $safeschema = $this->addIdentifierQuotes( $wgDBmwschema );
209 $this->doQuery( "SET search_path = $safeschema, $wgDBts2schema, public" );
210 }
211
212 return $this->mConn;
213 }
214
215 function makeConnectionString( $vars ) {
216 $s = '';
217 foreach ( $vars as $name => $value ) {
218 $s .= "$name='" . str_replace( "'", "\\'", $value ) . "' ";
219 }
220 return $s;
221 }
222
223
224 function initial_setup( $superuser, $password, $dbName ) {
225 // If this is the initial connection, setup the schema stuff and possibly create the user
226 global $wgDBname, $wgDBuser, $wgDBpassword, $wgDBmwschema, $wgDBts2schema;
227
228 $safeuser = $this->addIdentifierQuotes( $wgDBuser );
229 // Are we connecting as a superuser for the first time?
230 if ( $superuser ) {
231 // Are we really a superuser? Check out our rights
232 $SQL = "SELECT
233 CASE WHEN usesuper IS TRUE THEN
234 CASE WHEN usecreatedb IS TRUE THEN 3 ELSE 1 END
235 ELSE CASE WHEN usecreatedb IS TRUE THEN 2 ELSE 0 END
236 END AS rights
237 FROM pg_catalog.pg_user WHERE usename = " . $this->addQuotes( $superuser );
238 $rows = $this->numRows( $res = $this->doQuery( $SQL ) );
239 if ( !$rows ) {
240 print '<li>ERROR: Could not read permissions for user "' . htmlspecialchars( $superuser ) . "\"</li>\n";
241 dieout( );
242 }
243 $perms = pg_fetch_result( $res, 0, 0 );
244
245 $SQL = "SELECT 1 FROM pg_catalog.pg_user WHERE usename = " . $this->addQuotes( $wgDBuser );
246 $rows = $this->numRows( $this->doQuery( $SQL ) );
247 if ( $rows ) {
248 print '<li>User "' . htmlspecialchars( $wgDBuser ) . '" already exists, skipping account creation.</li>';
249 } else {
250 if ( $perms != 1 && $perms != 3 ) {
251 print '<li>ERROR: the user "' . htmlspecialchars( $superuser ) . '" cannot create other users. ';
252 print 'Please use a different Postgres user.</li>';
253 dieout( );
254 }
255 print '<li>Creating user <b>' . htmlspecialchars( $wgDBuser ) . '</b>...';
256 $safepass = $this->addQuotes( $wgDBpassword );
257 $SQL = "CREATE USER $safeuser NOCREATEDB PASSWORD $safepass";
258 $this->doQuery( $SQL );
259 print "OK</li>\n";
260 }
261 // User now exists, check out the database
262 if ( $dbName != $wgDBname ) {
263 $SQL = "SELECT 1 FROM pg_catalog.pg_database WHERE datname = " . $this->addQuotes( $wgDBname );
264 $rows = $this->numRows( $this->doQuery( $SQL ) );
265 if ( $rows ) {
266 print '<li>Database "' . htmlspecialchars( $wgDBname ) . '" already exists, skipping database creation.</li>';
267 } else {
268 if ( $perms < 1 ) {
269 print '<li>ERROR: the user "' . htmlspecialchars( $superuser ) . '" cannot create databases. ';
270 print 'Please use a different Postgres user.</li>';
271 dieout( );
272 }
273 print '<li>Creating database <b>' . htmlspecialchars( $wgDBname ) . '</b>...';
274 $safename = $this->addIdentifierQuotes( $wgDBname );
275 $SQL = "CREATE DATABASE $safename OWNER $safeuser ";
276 $this->doQuery( $SQL );
277 print "OK</li>\n";
278 // Hopefully tsearch2 and plpgsql are in template1...
279 }
280
281 // Reconnect to check out tsearch2 rights for this user
282 print '<li>Connecting to "' . htmlspecialchars( $wgDBname ) . '" as superuser "' .
283 htmlspecialchars( $superuser ) . '" to check rights...';
284
285 $connectVars = array();
286 if ( $this->mServer != false && $this->mServer != '' ) {
287 $connectVars['host'] = $this->mServer;
288 }
289 if ( $this->mPort != false && $this->mPort != '' ) {
290 $connectVars['port'] = $this->mPort;
291 }
292 $connectVars['dbname'] = $wgDBname;
293 $connectVars['user'] = $superuser;
294 $connectVars['password'] = $password;
295
296 @$this->mConn = pg_connect( $this->makeConnectionString( $connectVars ) );
297 if ( !$this->mConn ) {
298 print "<b>FAILED TO CONNECT!</b></li>";
299 dieout( );
300 }
301 print "OK</li>\n";
302 }
303
304 if ( $this->numeric_version < 8.3 ) {
305 // Tsearch2 checks
306 print '<li>Checking that tsearch2 is installed in the database "' .
307 htmlspecialchars( $wgDBname ) . '"...';
308 if ( !$this->tableExists( 'pg_ts_cfg', $wgDBts2schema ) ) {
309 print '<b>FAILED</b>. tsearch2 must be installed in the database "' .
310 htmlspecialchars( $wgDBname ) . '".';
311 print 'Please see <a href="http://www.devx.com/opensource/Article/21674/0/page/2">this article</a>';
312 print " for instructions or ask on #postgresql on irc.freenode.net</li>\n";
313 dieout( );
314 }
315 print "OK</li>\n";
316 print '<li>Ensuring that user "' . htmlspecialchars( $wgDBuser ) .
317 '" has select rights on the tsearch2 tables...';
318 foreach ( array( 'cfg', 'cfgmap', 'dict', 'parser' ) as $table ) {
319 $SQL = "GRANT SELECT ON pg_ts_$table TO $safeuser";
320 $this->doQuery( $SQL );
321 }
322 print "OK</li>\n";
323 }
324
325 // Setup the schema for this user if needed
326 $result = $this->schemaExists( $wgDBmwschema );
327 $safeschema = $this->addIdentifierQuotes( $wgDBmwschema );
328 if ( !$result ) {
329 print '<li>Creating schema <b>' . htmlspecialchars( $wgDBmwschema ) . '</b> ...';
330 $result = $this->doQuery( "CREATE SCHEMA $safeschema AUTHORIZATION $safeuser" );
331 if ( !$result ) {
332 print "<b>FAILED</b>.</li>\n";
333 dieout( );
334 }
335 print "OK</li>\n";
336 } else {
337 print "<li>Schema already exists, explicitly granting rights...\n";
338 $safeschema2 = $this->addQuotes( $wgDBmwschema );
339 $SQL = "SELECT 'GRANT ALL ON '||pg_catalog.quote_ident(relname)||' TO $safeuser;'\n".
340 "FROM pg_catalog.pg_class p, pg_catalog.pg_namespace n\n".
341 "WHERE relnamespace = n.oid AND n.nspname = $safeschema2\n".
342 "AND p.relkind IN ('r','S','v')\n";
343 $SQL .= "UNION\n";
344 $SQL .= "SELECT 'GRANT ALL ON FUNCTION '||pg_catalog.quote_ident(proname)||'('||\n".
345 "pg_catalog.oidvectortypes(p.proargtypes)||') TO $safeuser;'\n".
346 "FROM pg_catalog.pg_proc p, pg_catalog.pg_namespace n\n".
347 "WHERE p.pronamespace = n.oid AND n.nspname = $safeschema2";
348 $res = $this->doQuery( $SQL );
349 if ( !$res ) {
350 print "<b>FAILED</b>. Could not set rights for the user.</li>\n";
351 dieout( );
352 }
353 $this->doQuery( "SET search_path = $safeschema" );
354 $rows = $this->numRows( $res );
355 while ( $rows ) {
356 $rows--;
357 $this->doQuery( pg_fetch_result( $res, $rows, 0 ) );
358 }
359 print "OK</li>";
360 }
361
362 // Install plpgsql if needed
363 $this->setup_plpgsql();
364
365 return true; // Reconnect as regular user
366
367 } // end superuser
368
369 if ( !defined( 'POSTGRES_SEARCHPATH' ) ) {
370
371 if ( $this->numeric_version < 8.3 ) {
372 // Do we have the basic tsearch2 table?
373 print '<li>Checking for tsearch2 in the schema "' . htmlspecialchars( $wgDBts2schema ) . '"...';
374 if ( !$this->tableExists( 'pg_ts_dict', $wgDBts2schema ) ) {
375 print '<b>FAILED</b>. Make sure tsearch2 is installed. See <a href="';
376 print 'http://www.devx.com/opensource/Article/21674/0/page/2">this article</a>';
377 print " for instructions.</li>\n";
378 dieout( );
379 }
380 print "OK</li>\n";
381
382 // Does this user have the rights to the tsearch2 tables?
383 $ctype = pg_fetch_result( $this->doQuery( 'SHOW lc_ctype' ), 0, 0 );
384 print '<li>Checking tsearch2 permissions...';
385 // Let's check all four, just to be safe
386 error_reporting( 0 );
387 $ts2tables = array( 'cfg', 'cfgmap', 'dict', 'parser' );
388 $safetsschema = $this->addIdentifierQuotes( $wgDBts2schema );
389 foreach ( $ts2tables as $tname ) {
390 $SQL = "SELECT count(*) FROM $safetsschema.pg_ts_$tname";
391 $res = $this->doQuery( $SQL );
392 if ( !$res ) {
393 print "<b>FAILED</b> to access " . htmlspecialchars( "pg_ts_$tname" ) .
394 ". Make sure that the user \"". htmlspecialchars( $wgDBuser ) .
395 "\" has SELECT access to all four tsearch2 tables</li>\n";
396 dieout( );
397 }
398 }
399 $SQL = "SELECT ts_name FROM $safetsschema.pg_ts_cfg WHERE locale = " . $this->addQuotes( $ctype ) ;
400 $SQL .= " ORDER BY CASE WHEN ts_name <> 'default' THEN 1 ELSE 0 END";
401 $res = $this->doQuery( $SQL );
402 error_reporting( E_ALL );
403 if ( !$res ) {
404 print "<b>FAILED</b>. Could not determine the tsearch2 locale information</li>\n";
405 dieout("</ul>");
406 }
407 print 'OK</li>';
408
409 // Will the current locale work? Can we force it to?
410 print '<li>Verifying tsearch2 locale with ' . htmlspecialchars( $ctype ) . '...';
411 $rows = $this->numRows( $res );
412 $resetlocale = 0;
413 if ( !$rows ) {
414 print "<b>not found</b></li>\n";
415 print '<li>Attempting to set default tsearch2 locale to "' . htmlspecialchars( $ctype ) . '"...';
416 $resetlocale = 1;
417 } else {
418 $tsname = pg_fetch_result( $res, 0, 0 );
419 if ( $tsname != 'default' ) {
420 print "<b>not set to default (" . htmlspecialchars( $tsname ) . ")</b>";
421 print "<li>Attempting to change tsearch2 default locale to \"" .
422 htmlspecialchars( $ctype ) . "\"...";
423 $resetlocale = 1;
424 }
425 }
426 if ( $resetlocale ) {
427 $SQL = "UPDATE $safetsschema.pg_ts_cfg SET locale = " . $this->addQuotes( $ctype ) . " WHERE ts_name = 'default'";
428 $res = $this->doQuery( $SQL );
429 if ( !$res ) {
430 print '<b>FAILED</b>. ';
431 print 'Please make sure that the locale in pg_ts_cfg for "default" is set to "' .
432 htmlspecialchars( $ctype ) . "\"</li>\n";
433 dieout( );
434 }
435 print 'OK</li>';
436 }
437
438 // Final test: try out a simple tsearch2 query
439 $SQL = "SELECT $safetsschema.to_tsvector('default','MediaWiki tsearch2 testing')";
440 $res = $this->doQuery( $SQL );
441 if ( !$res ) {
442 print '<b>FAILED</b>. Specifically, "' . htmlspecialchars( $SQL ) . '" did not work.</li>';
443 dieout( );
444 }
445 print 'OK</li>';
446 }
447
448 // Install plpgsql if needed
449 $this->setup_plpgsql();
450
451 // Does the schema already exist? Who owns it?
452 $result = $this->schemaExists( $wgDBmwschema );
453 if ( !$result ) {
454 print '<li>Creating schema <b>' . htmlspecialchars( $wgDBmwschema ) . '</b> ...';
455 error_reporting( 0 );
456 $safeschema = $this->addIdentifierQuotes( $wgDBmwschema );
457 $result = $this->doQuery( "CREATE SCHEMA $safeschema" );
458 error_reporting( E_ALL );
459 if ( !$result ) {
460 print '<b>FAILED</b>. The user "' . htmlspecialchars( $wgDBuser ) .
461 '" must be able to access the schema. '.
462 'You can try making them the owner of the database, or try creating the schema with a '.
463 'different user, and then grant access to the "' .
464 htmlspecialchars( $wgDBuser ) . "\" user.</li>\n";
465 dieout( );
466 }
467 print "OK</li>\n";
468 } elseif ( $result != $wgDBuser ) {
469 print '<li>Schema "' . htmlspecialchars( $wgDBmwschema ) . '" exists but is not owned by "' .
470 htmlspecialchars( $wgDBuser ) . "\". Not ideal.</li>\n";
471 } else {
472 print '<li>Schema "' . htmlspecialchars( $wgDBmwschema ) . '" exists and is owned by "' .
473 htmlspecialchars( $wgDBuser ) . "\". Excellent.</li>\n";
474 }
475
476 // Always return GMT time to accomodate the existing integer-based timestamp assumption
477 print "<li>Setting the timezone to GMT for user \"" . htmlspecialchars( $wgDBuser ) . '" ...';
478 $SQL = "ALTER USER $safeuser SET timezone = 'GMT'";
479 $result = pg_query( $this->mConn, $SQL );
480 if ( !$result ) {
481 print "<b>FAILED</b>.</li>\n";
482 dieout( );
483 }
484 print "OK</li>\n";
485 // Set for the rest of this session
486 $SQL = "SET timezone = 'GMT'";
487 $result = pg_query( $this->mConn, $SQL );
488 if ( !$result ) {
489 print "<li>Failed to set timezone</li>\n";
490 dieout( );
491 }
492
493 print '<li>Setting the datestyle to ISO, YMD for user "' . htmlspecialchars( $wgDBuser ) . '" ...';
494 $SQL = "ALTER USER $safeuser SET datestyle = 'ISO, YMD'";
495 $result = pg_query( $this->mConn, $SQL );
496 if ( !$result ) {
497 print "<b>FAILED</b>.</li>\n";
498 dieout( );
499 }
500 print "OK</li>\n";
501 // Set for the rest of this session
502 $SQL = "SET datestyle = 'ISO, YMD'";
503 $result = pg_query( $this->mConn, $SQL );
504 if ( !$result ) {
505 print "<li>Failed to set datestyle</li>\n";
506 dieout( );
507 }
508
509 // Fix up the search paths if needed
510 print '<li>Setting the search path for user "' . htmlspecialchars( $wgDBuser ) . '" ...';
511 $path = $this->addIdentifierQuotes( $wgDBmwschema );
512 if ( $wgDBts2schema !== $wgDBmwschema ) {
513 $path .= ', '. $this->addIdentifierQuotes( $wgDBts2schema );
514 }
515 if ( $wgDBmwschema !== 'public' && $wgDBts2schema !== 'public' ) {
516 $path .= ', public';
517 }
518 $SQL = "ALTER USER $safeuser SET search_path = $path";
519 $result = pg_query( $this->mConn, $SQL );
520 if ( !$result ) {
521 print "<b>FAILED</b>.</li>\n";
522 dieout( );
523 }
524 print "OK</li>\n";
525 // Set for the rest of this session
526 $SQL = "SET search_path = $path";
527 $result = pg_query( $this->mConn, $SQL );
528 if ( !$result ) {
529 print "<li>Failed to set search_path</li>\n";
530 dieout( );
531 }
532 define( 'POSTGRES_SEARCHPATH', $path );
533 }
534 }
535
536 /**
537 * Closes a database connection, if it is open
538 * Returns success, true if already closed
539 */
540 function close() {
541 $this->mOpened = false;
542 if ( $this->mConn ) {
543 return pg_close( $this->mConn );
544 } else {
545 return true;
546 }
547 }
548
549 function doQuery( $sql ) {
550 if ( function_exists( 'mb_convert_encoding' ) ) {
551 $sql = mb_convert_encoding( $sql, 'UTF-8' );
552 }
553 $this->mLastResult = pg_query( $this->mConn, $sql );
554 $this->mAffectedRows = null; // use pg_affected_rows(mLastResult)
555 return $this->mLastResult;
556 }
557
558 function queryIgnore( $sql, $fname = 'DatabasePostgres::queryIgnore' ) {
559 return $this->query( $sql, $fname, true );
560 }
561
562 function freeResult( $res ) {
563 if ( $res instanceof ResultWrapper ) {
564 $res = $res->result;
565 }
566 if ( !@pg_free_result( $res ) ) {
567 throw new DBUnexpectedError( $this, "Unable to free Postgres result\n" );
568 }
569 }
570
571 function fetchObject( $res ) {
572 if ( $res instanceof ResultWrapper ) {
573 $res = $res->result;
574 }
575 @$row = pg_fetch_object( $res );
576 # FIXME: HACK HACK HACK HACK debug
577
578 # TODO:
579 # hashar : not sure if the following test really trigger if the object
580 # fetching failed.
581 if( pg_last_error( $this->mConn ) ) {
582 throw new DBUnexpectedError( $this, 'SQL error: ' . htmlspecialchars( pg_last_error( $this->mConn ) ) );
583 }
584 return $row;
585 }
586
587 function fetchRow( $res ) {
588 if ( $res instanceof ResultWrapper ) {
589 $res = $res->result;
590 }
591 @$row = pg_fetch_array( $res );
592 if( pg_last_error( $this->mConn ) ) {
593 throw new DBUnexpectedError( $this, 'SQL error: ' . htmlspecialchars( pg_last_error( $this->mConn ) ) );
594 }
595 return $row;
596 }
597
598 function numRows( $res ) {
599 if ( $res instanceof ResultWrapper ) {
600 $res = $res->result;
601 }
602 @$n = pg_num_rows( $res );
603 if( pg_last_error( $this->mConn ) ) {
604 throw new DBUnexpectedError( $this, 'SQL error: ' . htmlspecialchars( pg_last_error( $this->mConn ) ) );
605 }
606 return $n;
607 }
608
609 function numFields( $res ) {
610 if ( $res instanceof ResultWrapper ) {
611 $res = $res->result;
612 }
613 return pg_num_fields( $res );
614 }
615
616 function fieldName( $res, $n ) {
617 if ( $res instanceof ResultWrapper ) {
618 $res = $res->result;
619 }
620 return pg_field_name( $res, $n );
621 }
622
623 /**
624 * This must be called after nextSequenceVal
625 */
626 function insertId() {
627 return $this->mInsertId;
628 }
629
630 function dataSeek( $res, $row ) {
631 if ( $res instanceof ResultWrapper ) {
632 $res = $res->result;
633 }
634 return pg_result_seek( $res, $row );
635 }
636
637 function lastError() {
638 if ( $this->mConn ) {
639 return pg_last_error();
640 } else {
641 return 'No database connection';
642 }
643 }
644 function lastErrno() {
645 return pg_last_error() ? 1 : 0;
646 }
647
648 function affectedRows() {
649 if ( !is_null( $this->mAffectedRows ) ) {
650 // Forced result for simulated queries
651 return $this->mAffectedRows;
652 }
653 if( empty( $this->mLastResult ) ) {
654 return 0;
655 }
656 return pg_affected_rows( $this->mLastResult );
657 }
658
659 /**
660 * Estimate rows in dataset
661 * Returns estimated count, based on EXPLAIN output
662 * This is not necessarily an accurate estimate, so use sparingly
663 * Returns -1 if count cannot be found
664 * Takes same arguments as Database::select()
665 */
666 function estimateRowCount( $table, $vars = '*', $conds='', $fname = 'DatabasePostgres::estimateRowCount', $options = array() ) {
667 $options['EXPLAIN'] = true;
668 $res = $this->select( $table, $vars, $conds, $fname, $options );
669 $rows = -1;
670 if ( $res ) {
671 $row = $this->fetchRow( $res );
672 $count = array();
673 if( preg_match( '/rows=(\d+)/', $row[0], $count ) ) {
674 $rows = $count[1];
675 }
676 }
677 return $rows;
678 }
679
680 /**
681 * Returns information about an index
682 * If errors are explicitly ignored, returns NULL on failure
683 */
684 function indexInfo( $table, $index, $fname = 'DatabasePostgres::indexInfo' ) {
685 $sql = "SELECT indexname FROM pg_indexes WHERE tablename='$table'";
686 $res = $this->query( $sql, $fname );
687 if ( !$res ) {
688 return null;
689 }
690 foreach ( $res as $row ) {
691 if ( $row->indexname == $this->indexName( $index ) ) {
692 return $row;
693 }
694 }
695 return false;
696 }
697
698 function indexUnique( $table, $index, $fname = 'DatabasePostgres::indexUnique' ) {
699 $sql = "SELECT indexname FROM pg_indexes WHERE tablename='{$table}'".
700 " AND indexdef LIKE 'CREATE UNIQUE%(" .
701 $this->strencode( $this->indexName( $index ) ) .
702 ")'";
703 $res = $this->query( $sql, $fname );
704 if ( !$res ) {
705 return null;
706 }
707 foreach ( $res as $row ) {
708 return true;
709 }
710 return false;
711 }
712
713 /**
714 * INSERT wrapper, inserts an array into a table
715 *
716 * $args may be a single associative array, or an array of these with numeric keys,
717 * for multi-row insert (Postgres version 8.2 and above only).
718 *
719 * @param $table String: Name of the table to insert to.
720 * @param $args Array: Items to insert into the table.
721 * @param $fname String: Name of the function, for profiling
722 * @param $options String or Array. Valid options: IGNORE
723 *
724 * @return bool Success of insert operation. IGNORE always returns true.
725 */
726 function insert( $table, $args, $fname = 'DatabasePostgres::insert', $options = array() ) {
727 if ( !count( $args ) ) {
728 return true;
729 }
730
731 $table = $this->tableName( $table );
732 if (! isset( $this->numeric_version ) ) {
733 $this->getServerVersion();
734 }
735
736 if ( !is_array( $options ) ) {
737 $options = array( $options );
738 }
739
740 if ( isset( $args[0] ) && is_array( $args[0] ) ) {
741 $multi = true;
742 $keys = array_keys( $args[0] );
743 } else {
744 $multi = false;
745 $keys = array_keys( $args );
746 }
747
748 // If IGNORE is set, we use savepoints to emulate mysql's behavior
749 $ignore = in_array( 'IGNORE', $options ) ? 'mw' : '';
750
751 // If we are not in a transaction, we need to be for savepoint trickery
752 $didbegin = 0;
753 if ( $ignore ) {
754 if ( !$this->mTrxLevel ) {
755 $this->begin();
756 $didbegin = 1;
757 }
758 $olde = error_reporting( 0 );
759 // For future use, we may want to track the number of actual inserts
760 // Right now, insert (all writes) simply return true/false
761 $numrowsinserted = 0;
762 }
763
764 $sql = "INSERT INTO $table (" . implode( ',', $keys ) . ') VALUES ';
765
766 if ( $multi ) {
767 if ( $this->numeric_version >= 8.2 && !$ignore ) {
768 $first = true;
769 foreach ( $args as $row ) {
770 if ( $first ) {
771 $first = false;
772 } else {
773 $sql .= ',';
774 }
775 $sql .= '(' . $this->makeList( $row ) . ')';
776 }
777 $res = (bool)$this->query( $sql, $fname, $ignore );
778 } else {
779 $res = true;
780 $origsql = $sql;
781 foreach ( $args as $row ) {
782 $tempsql = $origsql;
783 $tempsql .= '(' . $this->makeList( $row ) . ')';
784
785 if ( $ignore ) {
786 pg_query( $this->mConn, "SAVEPOINT $ignore" );
787 }
788
789 $tempres = (bool)$this->query( $tempsql, $fname, $ignore );
790
791 if ( $ignore ) {
792 $bar = pg_last_error();
793 if ( $bar != false ) {
794 pg_query( $this->mConn, "ROLLBACK TO $ignore" );
795 } else {
796 pg_query( $this->mConn, "RELEASE $ignore" );
797 $numrowsinserted++;
798 }
799 }
800
801 // If any of them fail, we fail overall for this function call
802 // Note that this will be ignored if IGNORE is set
803 if ( !$tempres ) {
804 $res = false;
805 }
806 }
807 }
808 } else {
809 // Not multi, just a lone insert
810 if ( $ignore ) {
811 pg_query($this->mConn, "SAVEPOINT $ignore");
812 }
813
814 $sql .= '(' . $this->makeList( $args ) . ')';
815 $res = (bool)$this->query( $sql, $fname, $ignore );
816 if ( $ignore ) {
817 $bar = pg_last_error();
818 if ( $bar != false ) {
819 pg_query( $this->mConn, "ROLLBACK TO $ignore" );
820 } else {
821 pg_query( $this->mConn, "RELEASE $ignore" );
822 $numrowsinserted++;
823 }
824 }
825 }
826 if ( $ignore ) {
827 $olde = error_reporting( $olde );
828 if ( $didbegin ) {
829 $this->commit();
830 }
831
832 // Set the affected row count for the whole operation
833 $this->mAffectedRows = $numrowsinserted;
834
835 // IGNORE always returns true
836 return true;
837 }
838
839 return $res;
840 }
841
842 /**
843 * INSERT SELECT wrapper
844 * $varMap must be an associative array of the form array( 'dest1' => 'source1', ...)
845 * Source items may be literals rather then field names, but strings should be quoted with Database::addQuotes()
846 * $conds may be "*" to copy the whole table
847 * srcTable may be an array of tables.
848 * @todo FIXME: implement this a little better (seperate select/insert)?
849 */
850 function insertSelect( $destTable, $srcTable, $varMap, $conds, $fname = 'DatabasePostgres::insertSelect',
851 $insertOptions = array(), $selectOptions = array() )
852 {
853 $destTable = $this->tableName( $destTable );
854
855 // If IGNORE is set, we use savepoints to emulate mysql's behavior
856 $ignore = in_array( 'IGNORE', $insertOptions ) ? 'mw' : '';
857
858 if( is_array( $insertOptions ) ) {
859 $insertOptions = implode( ' ', $insertOptions );
860 }
861 if( !is_array( $selectOptions ) ) {
862 $selectOptions = array( $selectOptions );
863 }
864 list( $startOpts, $useIndex, $tailOpts ) = $this->makeSelectOptions( $selectOptions );
865 if( is_array( $srcTable ) ) {
866 $srcTable = implode( ',', array_map( array( &$this, 'tableName' ), $srcTable ) );
867 } else {
868 $srcTable = $this->tableName( $srcTable );
869 }
870
871 // If we are not in a transaction, we need to be for savepoint trickery
872 $didbegin = 0;
873 if ( $ignore ) {
874 if( !$this->mTrxLevel ) {
875 $this->begin();
876 $didbegin = 1;
877 }
878 $olde = error_reporting( 0 );
879 $numrowsinserted = 0;
880 pg_query( $this->mConn, "SAVEPOINT $ignore");
881 }
882
883 $sql = "INSERT INTO $destTable (" . implode( ',', array_keys( $varMap ) ) . ')' .
884 " SELECT $startOpts " . implode( ',', $varMap ) .
885 " FROM $srcTable $useIndex";
886
887 if ( $conds != '*' ) {
888 $sql .= ' WHERE ' . $this->makeList( $conds, LIST_AND );
889 }
890
891 $sql .= " $tailOpts";
892
893 $res = (bool)$this->query( $sql, $fname, $ignore );
894 if( $ignore ) {
895 $bar = pg_last_error();
896 if( $bar != false ) {
897 pg_query( $this->mConn, "ROLLBACK TO $ignore" );
898 } else {
899 pg_query( $this->mConn, "RELEASE $ignore" );
900 $numrowsinserted++;
901 }
902 $olde = error_reporting( $olde );
903 if( $didbegin ) {
904 $this->commit();
905 }
906
907 // Set the affected row count for the whole operation
908 $this->mAffectedRows = $numrowsinserted;
909
910 // IGNORE always returns true
911 return true;
912 }
913
914 return $res;
915 }
916
917 function tableName( $name ) {
918 # Replace reserved words with better ones
919 switch( $name ) {
920 case 'user':
921 return 'mwuser';
922 case 'text':
923 return 'pagecontent';
924 default:
925 return $name;
926 }
927 }
928
929 /**
930 * Return the next in a sequence, save the value for retrieval via insertId()
931 */
932 function nextSequenceValue( $seqName ) {
933 $safeseq = str_replace( "'", "''", $seqName );
934 $res = $this->query( "SELECT nextval('$safeseq')" );
935 $row = $this->fetchRow( $res );
936 $this->mInsertId = $row[0];
937 return $this->mInsertId;
938 }
939
940 /**
941 * Return the current value of a sequence. Assumes it has been nextval'ed in this session.
942 */
943 function currentSequenceValue( $seqName ) {
944 $safeseq = str_replace( "'", "''", $seqName );
945 $res = $this->query( "SELECT currval('$safeseq')" );
946 $row = $this->fetchRow( $res );
947 $currval = $row[0];
948 return $currval;
949 }
950
951 /**
952 * REPLACE query wrapper
953 * Postgres simulates this with a DELETE followed by INSERT
954 * $row is the row to insert, an associative array
955 * $uniqueIndexes is an array of indexes. Each element may be either a
956 * field name or an array of field names
957 *
958 * It may be more efficient to leave off unique indexes which are unlikely to collide.
959 * However if you do this, you run the risk of encountering errors which wouldn't have
960 * occurred in MySQL
961 */
962 function replace( $table, $uniqueIndexes, $rows, $fname = 'DatabasePostgres::replace' ) {
963 $table = $this->tableName( $table );
964
965 if ( count( $rows ) == 0 ) {
966 return;
967 }
968
969 # Single row case
970 if ( !is_array( reset( $rows ) ) ) {
971 $rows = array( $rows );
972 }
973
974 foreach( $rows as $row ) {
975 # Delete rows which collide
976 if ( $uniqueIndexes ) {
977 $sql = "DELETE FROM $table WHERE ";
978 $first = true;
979 foreach ( $uniqueIndexes as $index ) {
980 if ( $first ) {
981 $first = false;
982 $sql .= '(';
983 } else {
984 $sql .= ') OR (';
985 }
986 if ( is_array( $index ) ) {
987 $first2 = true;
988 foreach ( $index as $col ) {
989 if ( $first2 ) {
990 $first2 = false;
991 } else {
992 $sql .= ' AND ';
993 }
994 $sql .= $col.'=' . $this->addQuotes( $row[$col] );
995 }
996 } else {
997 $sql .= $index.'=' . $this->addQuotes( $row[$index] );
998 }
999 }
1000 $sql .= ')';
1001 $this->query( $sql, $fname );
1002 }
1003
1004 # Now insert the row
1005 $sql = "INSERT INTO $table (" . $this->makeList( array_keys( $row ), LIST_NAMES ) .') VALUES (' .
1006 $this->makeList( $row, LIST_COMMA ) . ')';
1007 $this->query( $sql, $fname );
1008 }
1009 }
1010
1011 # DELETE where the condition is a join
1012 function deleteJoin( $delTable, $joinTable, $delVar, $joinVar, $conds, $fname = 'DatabasePostgres::deleteJoin' ) {
1013 if ( !$conds ) {
1014 throw new DBUnexpectedError( $this, 'DatabasePostgres::deleteJoin() called with empty $conds' );
1015 }
1016
1017 $delTable = $this->tableName( $delTable );
1018 $joinTable = $this->tableName( $joinTable );
1019 $sql = "DELETE FROM $delTable WHERE $delVar IN (SELECT $joinVar FROM $joinTable ";
1020 if ( $conds != '*' ) {
1021 $sql .= 'WHERE ' . $this->makeList( $conds, LIST_AND );
1022 }
1023 $sql .= ')';
1024
1025 $this->query( $sql, $fname );
1026 }
1027
1028 # Returns the size of a text field, or -1 for "unlimited"
1029 function textFieldSize( $table, $field ) {
1030 $table = $this->tableName( $table );
1031 $sql = "SELECT t.typname as ftype,a.atttypmod as size
1032 FROM pg_class c, pg_attribute a, pg_type t
1033 WHERE relname='$table' AND a.attrelid=c.oid AND
1034 a.atttypid=t.oid and a.attname='$field'";
1035 $res =$this->query( $sql );
1036 $row = $this->fetchObject( $res );
1037 if ( $row->ftype == 'varchar' ) {
1038 $size = $row->size - 4;
1039 } else {
1040 $size = $row->size;
1041 }
1042 return $size;
1043 }
1044
1045 function limitResult( $sql, $limit, $offset = false ) {
1046 return "$sql LIMIT $limit " . ( is_numeric( $offset ) ? " OFFSET {$offset} " : '' );
1047 }
1048
1049 function wasDeadlock() {
1050 return $this->lastErrno() == '40P01';
1051 }
1052
1053 function duplicateTableStructure( $oldName, $newName, $temporary = false, $fname = 'DatabasePostgres::duplicateTableStructure' ) {
1054 return $this->query( 'CREATE ' . ( $temporary ? 'TEMPORARY ' : '' ) . " TABLE $newName (LIKE $oldName INCLUDING DEFAULTS)", $fname );
1055 }
1056
1057 function timestamp( $ts = 0 ) {
1058 return wfTimestamp( TS_POSTGRES, $ts );
1059 }
1060
1061 /**
1062 * Return aggregated value function call
1063 */
1064 function aggregateValue( $valuedata, $valuename = 'value' ) {
1065 return $valuedata;
1066 }
1067
1068 function reportQueryError( $error, $errno, $sql, $fname, $tempIgnore = false ) {
1069 // Ignore errors during error handling to avoid infinite recursion
1070 $ignore = $this->ignoreErrors( true );
1071 $this->mErrorCount++;
1072
1073 if ( $ignore || $tempIgnore ) {
1074 wfDebug( "SQL ERROR (ignored): $error\n" );
1075 $this->ignoreErrors( $ignore );
1076 } else {
1077 $message = "A database error has occurred. Did you forget to run maintenance/update.php after upgrading? See: http://www.mediawiki.org/wiki/Manual:Upgrading#Run_the_update_script\n" .
1078 "Query: $sql\n" .
1079 "Function: $fname\n" .
1080 "Error: $errno $error\n";
1081 throw new DBUnexpectedError( $this, $message );
1082 }
1083 }
1084
1085 /**
1086 * @return string wikitext of a link to the server software's web site
1087 */
1088 public static function getSoftwareLink() {
1089 return '[http://www.postgresql.org/ PostgreSQL]';
1090 }
1091
1092 /**
1093 * @return string Version information from the database
1094 */
1095 function getServerVersion() {
1096 if ( !isset( $this->numeric_version ) ) {
1097 $versionInfo = pg_version( $this->mConn );
1098 if ( version_compare( $versionInfo['client'], '7.4.0', 'lt' ) ) {
1099 // Old client, abort install
1100 $this->numeric_version = '7.3 or earlier';
1101 } elseif ( isset( $versionInfo['server'] ) ) {
1102 // Normal client
1103 $this->numeric_version = $versionInfo['server'];
1104 } else {
1105 // Bug 16937: broken pgsql extension from PHP<5.3
1106 $this->numeric_version = pg_parameter_status( $this->mConn, 'server_version' );
1107 }
1108 }
1109 return $this->numeric_version;
1110 }
1111
1112 /**
1113 * Query whether a given relation exists (in the given schema, or the
1114 * default mw one if not given)
1115 */
1116 function relationExists( $table, $types, $schema = false ) {
1117 global $wgDBmwschema;
1118 if ( !is_array( $types ) ) {
1119 $types = array( $types );
1120 }
1121 if ( !$schema ) {
1122 $schema = $wgDBmwschema;
1123 }
1124 $etable = $this->addQuotes( $table );
1125 $eschema = $this->addQuotes( $schema );
1126 $SQL = "SELECT 1 FROM pg_catalog.pg_class c, pg_catalog.pg_namespace n "
1127 . "WHERE c.relnamespace = n.oid AND c.relname = $etable AND n.nspname = $eschema "
1128 . "AND c.relkind IN ('" . implode( "','", $types ) . "')";
1129 $res = $this->query( $SQL );
1130 $count = $res ? $res->numRows() : 0;
1131 return (bool)$count;
1132 }
1133
1134 /**
1135 * For backward compatibility, this function checks both tables and
1136 * views.
1137 */
1138 function tableExists( $table, $schema = false ) {
1139 return $this->relationExists( $table, array( 'r', 'v' ), $schema );
1140 }
1141
1142 function sequenceExists( $sequence, $schema = false ) {
1143 return $this->relationExists( $sequence, 'S', $schema );
1144 }
1145
1146 function triggerExists( $table, $trigger ) {
1147 global $wgDBmwschema;
1148
1149 $q = <<<SQL
1150 SELECT 1 FROM pg_class, pg_namespace, pg_trigger
1151 WHERE relnamespace=pg_namespace.oid AND relkind='r'
1152 AND tgrelid=pg_class.oid
1153 AND nspname=%s AND relname=%s AND tgname=%s
1154 SQL;
1155 $res = $this->query(
1156 sprintf(
1157 $q,
1158 $this->addQuotes( $wgDBmwschema ),
1159 $this->addQuotes( $table ),
1160 $this->addQuotes( $trigger )
1161 )
1162 );
1163 if ( !$res ) {
1164 return null;
1165 }
1166 $rows = $res->numRows();
1167 return $rows;
1168 }
1169
1170 function ruleExists( $table, $rule ) {
1171 global $wgDBmwschema;
1172 $exists = $this->selectField( 'pg_rules', 'rulename',
1173 array(
1174 'rulename' => $rule,
1175 'tablename' => $table,
1176 'schemaname' => $wgDBmwschema
1177 )
1178 );
1179 return $exists === $rule;
1180 }
1181
1182 function constraintExists( $table, $constraint ) {
1183 global $wgDBmwschema;
1184 $SQL = sprintf( "SELECT 1 FROM information_schema.table_constraints ".
1185 "WHERE constraint_schema = %s AND table_name = %s AND constraint_name = %s",
1186 $this->addQuotes( $wgDBmwschema ),
1187 $this->addQuotes( $table ),
1188 $this->addQuotes( $constraint )
1189 );
1190 $res = $this->query( $SQL );
1191 if ( !$res ) {
1192 return null;
1193 }
1194 $rows = $res->numRows();
1195 return $rows;
1196 }
1197
1198 /**
1199 * Query whether a given schema exists. Returns the name of the owner
1200 */
1201 function schemaExists( $schema ) {
1202 $eschema = str_replace( "'", "''", $schema );
1203 $SQL = "SELECT rolname FROM pg_catalog.pg_namespace n, pg_catalog.pg_roles r "
1204 ."WHERE n.nspowner=r.oid AND n.nspname = '$eschema'";
1205 $res = $this->query( $SQL );
1206 if ( $res && $res->numRows() ) {
1207 $row = $res->fetchObject();
1208 $owner = $row->rolname;
1209 } else {
1210 $owner = false;
1211 }
1212 return $owner;
1213 }
1214
1215 function fieldInfo( $table, $field ) {
1216 return PostgresField::fromText( $this, $table, $field );
1217 }
1218
1219 /**
1220 * pg_field_type() wrapper
1221 */
1222 function fieldType( $res, $index ) {
1223 if ( $res instanceof ResultWrapper ) {
1224 $res = $res->result;
1225 }
1226 return pg_field_type( $res, $index );
1227 }
1228
1229 /* Not even sure why this is used in the main codebase... */
1230 function limitResultForUpdate( $sql, $num ) {
1231 return $sql;
1232 }
1233
1234 function encodeBlob( $b ) {
1235 return new Blob( pg_escape_bytea( $this->mConn, $b ) );
1236 }
1237
1238 function decodeBlob( $b ) {
1239 if ( $b instanceof Blob ) {
1240 $b = $b->fetch();
1241 }
1242 return pg_unescape_bytea( $b );
1243 }
1244
1245 function strencode( $s ) { # Should not be called by us
1246 return pg_escape_string( $this->mConn, $s );
1247 }
1248
1249 function addQuotes( $s ) {
1250 if ( is_null( $s ) ) {
1251 return 'NULL';
1252 } elseif ( is_bool( $s ) ) {
1253 return intval( $s );
1254 } elseif ( $s instanceof Blob ) {
1255 return "'" . $s->fetch( $s ) . "'";
1256 }
1257 return "'" . pg_escape_string( $this->mConn, $s ) . "'";
1258 }
1259
1260 /**
1261 * Postgres specific version of replaceVars.
1262 * Calls the parent version in Database.php
1263 *
1264 * @private
1265 *
1266 * @param $ins String: SQL string, read from a stream (usually tables.sql)
1267 *
1268 * @return string SQL string
1269 */
1270 protected function replaceVars( $ins ) {
1271 $ins = parent::replaceVars( $ins );
1272
1273 if ( $this->numeric_version >= 8.3 ) {
1274 // Thanks for not providing backwards-compatibility, 8.3
1275 $ins = preg_replace( "/to_tsvector\s*\(\s*'default'\s*,/", 'to_tsvector(', $ins );
1276 }
1277
1278 if ( $this->numeric_version <= 8.1 ) { // Our minimum version
1279 $ins = str_replace( 'USING gin', 'USING gist', $ins );
1280 }
1281
1282 return $ins;
1283 }
1284
1285 /**
1286 * Various select options
1287 *
1288 * @private
1289 *
1290 * @param $options Array: an associative array of options to be turned into
1291 * an SQL query, valid keys are listed in the function.
1292 * @return array
1293 */
1294 function makeSelectOptions( $options ) {
1295 $preLimitTail = $postLimitTail = '';
1296 $startOpts = $useIndex = '';
1297
1298 $noKeyOptions = array();
1299 foreach ( $options as $key => $option ) {
1300 if ( is_numeric( $key ) ) {
1301 $noKeyOptions[$option] = true;
1302 }
1303 }
1304
1305 if ( isset( $options['GROUP BY'] ) ) {
1306 $preLimitTail .= ' GROUP BY ' . $options['GROUP BY'];
1307 }
1308 if ( isset( $options['HAVING'] ) ) {
1309 $preLimitTail .= " HAVING {$options['HAVING']}";
1310 }
1311 if ( isset( $options['ORDER BY'] ) ) {
1312 $preLimitTail .= ' ORDER BY ' . $options['ORDER BY'];
1313 }
1314
1315 //if ( isset( $options['LIMIT'] ) ) {
1316 // $tailOpts .= $this->limitResult( '', $options['LIMIT'],
1317 // isset( $options['OFFSET'] ) ? $options['OFFSET']
1318 // : false );
1319 //}
1320
1321 if ( isset( $noKeyOptions['FOR UPDATE'] ) ) {
1322 $postLimitTail .= ' FOR UPDATE';
1323 }
1324 if ( isset( $noKeyOptions['LOCK IN SHARE MODE'] ) ) {
1325 $postLimitTail .= ' LOCK IN SHARE MODE';
1326 }
1327 if ( isset( $noKeyOptions['DISTINCT'] ) || isset( $noKeyOptions['DISTINCTROW'] ) ) {
1328 $startOpts .= 'DISTINCT';
1329 }
1330
1331 return array( $startOpts, $useIndex, $preLimitTail, $postLimitTail );
1332 }
1333
1334 function setFakeMaster( $enabled = true ) {}
1335
1336 function getDBname() {
1337 return $this->mDBname;
1338 }
1339
1340 function getServer() {
1341 return $this->mServer;
1342 }
1343
1344 function buildConcat( $stringList ) {
1345 return implode( ' || ', $stringList );
1346 }
1347
1348 public function getSearchEngine() {
1349 return 'SearchPostgres';
1350 }
1351 } // end DatabasePostgres class