3 * This is the Postgres database abstraction layer.
9 class PostgresField
implements Field
{
10 private $name, $tablename, $type, $nullable, $max_length, $deferred, $deferrable, $conname;
14 * @param $db DatabaseBase
17 * @return null|PostgresField
19 static function fromText( $db, $table, $field ) {
24 attnotnull, attlen, COALESCE(conname, '') AS conname,
25 COALESCE(condeferred, 'f') AS deferred,
26 COALESCE(condeferrable, 'f') AS deferrable,
27 CASE WHEN typname = 'int2' THEN 'smallint'
28 WHEN typname = 'int4' THEN 'integer'
29 WHEN typname = 'int8' THEN 'bigint'
30 WHEN typname = 'bpchar' THEN 'char'
31 ELSE typname END AS typname
33 JOIN pg_namespace n ON (n.oid = c.relnamespace)
34 JOIN pg_attribute a ON (a.attrelid = c.oid)
35 JOIN pg_type t ON (t.oid = a.atttypid)
36 LEFT JOIN pg_constraint o ON (o.conrelid = c.oid AND a.attnum = ANY(o.conkey) AND o.contype = 'f')
43 $table = $db->tableName( $table );
46 $db->addQuotes( $wgDBmwschema ),
47 $db->addQuotes( $table ),
48 $db->addQuotes( $field )
51 $row = $db->fetchObject( $res );
55 $n = new PostgresField
;
56 $n->type
= $row->typname
;
57 $n->nullable
= ( $row->attnotnull
== 'f' );
59 $n->tablename
= $table;
60 $n->max_length
= $row->attlen
;
61 $n->deferrable
= ( $row->deferrable
== 't' );
62 $n->deferred
= ( $row->deferred
== 't' );
63 $n->conname
= $row->conname
;
71 function tableName() {
72 return $this->tablename
;
79 function isNullable() {
80 return $this->nullable
;
83 function maxLength() {
84 return $this->max_length
;
87 function is_deferrable() {
88 return $this->deferrable
;
91 function is_deferred() {
92 return $this->deferred
;
96 return $this->conname
;
104 class DatabasePostgres
extends DatabaseBase
{
105 var $mInsertId = null;
106 var $mLastResult = null;
107 var $numeric_version = null;
108 var $mAffectedRows = null;
114 function cascadingDeletes() {
117 function cleanupTriggers() {
120 function strictIPs() {
123 function realTimestamps() {
126 function implicitGroupby() {
129 function implicitOrderby() {
132 function searchableIPs() {
135 function functionalIndexes() {
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 );
148 * Usually aborts on failure
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" );
158 if ( !strlen( $user ) ) { # e.g. the class is being loaded
163 $this->mServer
= $server;
164 $this->mPort
= $port = $wgDBport;
165 $this->mUser
= $user;
166 $this->mPassword
= $password;
167 $this->mDBname
= $dbName;
169 $connectVars = array(
172 'password' => $password
174 if ( $server != false && $server != '' ) {
175 $connectVars['host'] = $server;
177 if ( $port != false && $port != '' ) {
178 $connectVars['port'] = $port;
180 $connectString = $this->makeConnectionString( $connectVars, PGSQL_CONNECT_FORCE_NEW
);
182 $this->installErrorHandler();
183 $this->mConn
= pg_connect( $connectString );
184 $phpError = $this->restoreErrorHandler();
186 if ( !$this->mConn
) {
187 wfDebug( "DB connection error\n" );
188 wfDebug( "Server: $server, Database: $dbName, User: $user, Password: " . substr( $password, 0, 3 ) . "...\n" );
189 wfDebug( $this->lastError() . "\n" );
190 throw new DBConnectionError( $this, $phpError );
193 $this->mOpened
= true;
195 global $wgCommandLineMode;
196 # If called from the command-line (e.g. importDump), only show errors
197 if ( $wgCommandLineMode ) {
198 $this->doQuery( "SET client_min_messages = 'ERROR'" );
201 $this->query( "SET client_encoding='UTF8'", __METHOD__
);
202 $this->query( "SET datestyle = 'ISO, YMD'", __METHOD__
);
203 $this->query( "SET timezone = 'GMT'", __METHOD__
);
205 global $wgDBmwschema;
206 if ( isset( $wgDBmwschema )
207 && preg_match( '/^\w+$/', $wgDBmwschema )
209 $safeschema = $this->addIdentifierQuotes( $wgDBmwschema );
210 $this->doQuery( "SET search_path = $safeschema, public" );
216 function makeConnectionString( $vars ) {
218 foreach ( $vars as $name => $value ) {
219 $s .= "$name='" . str_replace( "'", "\\'", $value ) . "' ";
225 * Closes a database connection, if it is open
226 * Returns success, true if already closed
229 $this->mOpened
= false;
230 if ( $this->mConn
) {
231 return pg_close( $this->mConn
);
237 function doQuery( $sql ) {
238 if ( function_exists( 'mb_convert_encoding' ) ) {
239 $sql = mb_convert_encoding( $sql, 'UTF-8' );
241 $this->mLastResult
= pg_query( $this->mConn
, $sql );
242 $this->mAffectedRows
= null; // use pg_affected_rows(mLastResult)
243 return $this->mLastResult
;
246 function queryIgnore( $sql, $fname = 'DatabasePostgres::queryIgnore' ) {
247 return $this->query( $sql, $fname, true );
250 function freeResult( $res ) {
251 if ( $res instanceof ResultWrapper
) {
254 if ( !@pg_free_result
( $res ) ) {
255 throw new DBUnexpectedError( $this, "Unable to free Postgres result\n" );
259 function fetchObject( $res ) {
260 if ( $res instanceof ResultWrapper
) {
263 @$row = pg_fetch_object( $res );
264 # FIXME: HACK HACK HACK HACK debug
267 # hashar : not sure if the following test really trigger if the object
269 if( pg_last_error( $this->mConn
) ) {
270 throw new DBUnexpectedError( $this, 'SQL error: ' . htmlspecialchars( pg_last_error( $this->mConn
) ) );
275 function fetchRow( $res ) {
276 if ( $res instanceof ResultWrapper
) {
279 @$row = pg_fetch_array( $res );
280 if( pg_last_error( $this->mConn
) ) {
281 throw new DBUnexpectedError( $this, 'SQL error: ' . htmlspecialchars( pg_last_error( $this->mConn
) ) );
286 function numRows( $res ) {
287 if ( $res instanceof ResultWrapper
) {
290 @$n = pg_num_rows( $res );
291 if( pg_last_error( $this->mConn
) ) {
292 throw new DBUnexpectedError( $this, 'SQL error: ' . htmlspecialchars( pg_last_error( $this->mConn
) ) );
297 function numFields( $res ) {
298 if ( $res instanceof ResultWrapper
) {
301 return pg_num_fields( $res );
304 function fieldName( $res, $n ) {
305 if ( $res instanceof ResultWrapper
) {
308 return pg_field_name( $res, $n );
312 * This must be called after nextSequenceVal
314 function insertId() {
315 return $this->mInsertId
;
318 function dataSeek( $res, $row ) {
319 if ( $res instanceof ResultWrapper
) {
322 return pg_result_seek( $res, $row );
325 function lastError() {
326 if ( $this->mConn
) {
327 return pg_last_error();
329 return 'No database connection';
332 function lastErrno() {
333 return pg_last_error() ?
1 : 0;
336 function affectedRows() {
337 if ( !is_null( $this->mAffectedRows
) ) {
338 // Forced result for simulated queries
339 return $this->mAffectedRows
;
341 if( empty( $this->mLastResult
) ) {
344 return pg_affected_rows( $this->mLastResult
);
348 * Estimate rows in dataset
349 * Returns estimated count, based on EXPLAIN output
350 * This is not necessarily an accurate estimate, so use sparingly
351 * Returns -1 if count cannot be found
352 * Takes same arguments as Database::select()
354 function estimateRowCount( $table, $vars = '*', $conds='', $fname = 'DatabasePostgres::estimateRowCount', $options = array() ) {
355 $options['EXPLAIN'] = true;
356 $res = $this->select( $table, $vars, $conds, $fname, $options );
359 $row = $this->fetchRow( $res );
361 if( preg_match( '/rows=(\d+)/', $row[0], $count ) ) {
369 * Returns information about an index
370 * If errors are explicitly ignored, returns NULL on failure
372 function indexInfo( $table, $index, $fname = 'DatabasePostgres::indexInfo' ) {
373 $sql = "SELECT indexname FROM pg_indexes WHERE tablename='$table'";
374 $res = $this->query( $sql, $fname );
378 foreach ( $res as $row ) {
379 if ( $row->indexname
== $this->indexName( $index ) ) {
386 function indexUnique( $table, $index, $fname = 'DatabasePostgres::indexUnique' ) {
387 $sql = "SELECT indexname FROM pg_indexes WHERE tablename='{$table}'".
388 " AND indexdef LIKE 'CREATE UNIQUE%(" .
389 $this->strencode( $this->indexName( $index ) ) .
391 $res = $this->query( $sql, $fname );
395 foreach ( $res as $row ) {
402 * INSERT wrapper, inserts an array into a table
404 * $args may be a single associative array, or an array of these with numeric keys,
405 * for multi-row insert (Postgres version 8.2 and above only).
407 * @param $table String: Name of the table to insert to.
408 * @param $args Array: Items to insert into the table.
409 * @param $fname String: Name of the function, for profiling
410 * @param $options String or Array. Valid options: IGNORE
412 * @return bool Success of insert operation. IGNORE always returns true.
414 function insert( $table, $args, $fname = 'DatabasePostgres::insert', $options = array() ) {
415 if ( !count( $args ) ) {
419 $table = $this->tableName( $table );
420 if (! isset( $this->numeric_version
) ) {
421 $this->getServerVersion();
424 if ( !is_array( $options ) ) {
425 $options = array( $options );
428 if ( isset( $args[0] ) && is_array( $args[0] ) ) {
430 $keys = array_keys( $args[0] );
433 $keys = array_keys( $args );
436 // If IGNORE is set, we use savepoints to emulate mysql's behavior
437 $ignore = in_array( 'IGNORE', $options ) ?
'mw' : '';
439 // If we are not in a transaction, we need to be for savepoint trickery
442 if ( !$this->mTrxLevel
) {
446 $olde = error_reporting( 0 );
447 // For future use, we may want to track the number of actual inserts
448 // Right now, insert (all writes) simply return true/false
449 $numrowsinserted = 0;
452 $sql = "INSERT INTO $table (" . implode( ',', $keys ) . ') VALUES ';
455 if ( $this->numeric_version
>= 8.2 && !$ignore ) {
457 foreach ( $args as $row ) {
463 $sql .= '(' . $this->makeList( $row ) . ')';
465 $res = (bool)$this->query( $sql, $fname, $ignore );
469 foreach ( $args as $row ) {
471 $tempsql .= '(' . $this->makeList( $row ) . ')';
474 pg_query( $this->mConn
, "SAVEPOINT $ignore" );
477 $tempres = (bool)$this->query( $tempsql, $fname, $ignore );
480 $bar = pg_last_error();
481 if ( $bar != false ) {
482 pg_query( $this->mConn
, "ROLLBACK TO $ignore" );
484 pg_query( $this->mConn
, "RELEASE $ignore" );
489 // If any of them fail, we fail overall for this function call
490 // Note that this will be ignored if IGNORE is set
497 // Not multi, just a lone insert
499 pg_query($this->mConn
, "SAVEPOINT $ignore");
502 $sql .= '(' . $this->makeList( $args ) . ')';
503 $res = (bool)$this->query( $sql, $fname, $ignore );
505 $bar = pg_last_error();
506 if ( $bar != false ) {
507 pg_query( $this->mConn
, "ROLLBACK TO $ignore" );
509 pg_query( $this->mConn
, "RELEASE $ignore" );
515 $olde = error_reporting( $olde );
520 // Set the affected row count for the whole operation
521 $this->mAffectedRows
= $numrowsinserted;
523 // IGNORE always returns true
531 * INSERT SELECT wrapper
532 * $varMap must be an associative array of the form array( 'dest1' => 'source1', ...)
533 * Source items may be literals rather then field names, but strings should be quoted with Database::addQuotes()
534 * $conds may be "*" to copy the whole table
535 * srcTable may be an array of tables.
536 * @todo FIXME: implement this a little better (seperate select/insert)?
538 function insertSelect( $destTable, $srcTable, $varMap, $conds, $fname = 'DatabasePostgres::insertSelect',
539 $insertOptions = array(), $selectOptions = array() )
541 $destTable = $this->tableName( $destTable );
543 // If IGNORE is set, we use savepoints to emulate mysql's behavior
544 $ignore = in_array( 'IGNORE', $insertOptions ) ?
'mw' : '';
546 if( is_array( $insertOptions ) ) {
547 $insertOptions = implode( ' ', $insertOptions );
549 if( !is_array( $selectOptions ) ) {
550 $selectOptions = array( $selectOptions );
552 list( $startOpts, $useIndex, $tailOpts ) = $this->makeSelectOptions( $selectOptions );
553 if( is_array( $srcTable ) ) {
554 $srcTable = implode( ',', array_map( array( &$this, 'tableName' ), $srcTable ) );
556 $srcTable = $this->tableName( $srcTable );
559 // If we are not in a transaction, we need to be for savepoint trickery
562 if( !$this->mTrxLevel
) {
566 $olde = error_reporting( 0 );
567 $numrowsinserted = 0;
568 pg_query( $this->mConn
, "SAVEPOINT $ignore");
571 $sql = "INSERT INTO $destTable (" . implode( ',', array_keys( $varMap ) ) . ')' .
572 " SELECT $startOpts " . implode( ',', $varMap ) .
573 " FROM $srcTable $useIndex";
575 if ( $conds != '*' ) {
576 $sql .= ' WHERE ' . $this->makeList( $conds, LIST_AND
);
579 $sql .= " $tailOpts";
581 $res = (bool)$this->query( $sql, $fname, $ignore );
583 $bar = pg_last_error();
584 if( $bar != false ) {
585 pg_query( $this->mConn
, "ROLLBACK TO $ignore" );
587 pg_query( $this->mConn
, "RELEASE $ignore" );
590 $olde = error_reporting( $olde );
595 // Set the affected row count for the whole operation
596 $this->mAffectedRows
= $numrowsinserted;
598 // IGNORE always returns true
605 function tableName( $name ) {
606 # Replace reserved words with better ones
611 return 'pagecontent';
618 * Return the next in a sequence, save the value for retrieval via insertId()
620 function nextSequenceValue( $seqName ) {
621 $safeseq = str_replace( "'", "''", $seqName );
622 $res = $this->query( "SELECT nextval('$safeseq')" );
623 $row = $this->fetchRow( $res );
624 $this->mInsertId
= $row[0];
625 return $this->mInsertId
;
629 * Return the current value of a sequence. Assumes it has been nextval'ed in this session.
631 function currentSequenceValue( $seqName ) {
632 $safeseq = str_replace( "'", "''", $seqName );
633 $res = $this->query( "SELECT currval('$safeseq')" );
634 $row = $this->fetchRow( $res );
640 * REPLACE query wrapper
641 * Postgres simulates this with a DELETE followed by INSERT
642 * $row is the row to insert, an associative array
643 * $uniqueIndexes is an array of indexes. Each element may be either a
644 * field name or an array of field names
646 * It may be more efficient to leave off unique indexes which are unlikely to collide.
647 * However if you do this, you run the risk of encountering errors which wouldn't have
650 function replace( $table, $uniqueIndexes, $rows, $fname = 'DatabasePostgres::replace' ) {
651 $table = $this->tableName( $table );
653 if ( count( $rows ) == 0 ) {
658 if ( !is_array( reset( $rows ) ) ) {
659 $rows = array( $rows );
662 foreach( $rows as $row ) {
663 # Delete rows which collide
664 if ( $uniqueIndexes ) {
665 $sql = "DELETE FROM $table WHERE ";
667 foreach ( $uniqueIndexes as $index ) {
674 if ( is_array( $index ) ) {
676 foreach ( $index as $col ) {
682 $sql .= $col.'=' . $this->addQuotes( $row[$col] );
685 $sql .= $index.'=' . $this->addQuotes( $row[$index] );
689 $this->query( $sql, $fname );
693 $sql = "INSERT INTO $table (" . $this->makeList( array_keys( $row ), LIST_NAMES
) .') VALUES (' .
694 $this->makeList( $row, LIST_COMMA
) . ')';
695 $this->query( $sql, $fname );
699 # DELETE where the condition is a join
700 function deleteJoin( $delTable, $joinTable, $delVar, $joinVar, $conds, $fname = 'DatabasePostgres::deleteJoin' ) {
702 throw new DBUnexpectedError( $this, 'DatabasePostgres::deleteJoin() called with empty $conds' );
705 $delTable = $this->tableName( $delTable );
706 $joinTable = $this->tableName( $joinTable );
707 $sql = "DELETE FROM $delTable WHERE $delVar IN (SELECT $joinVar FROM $joinTable ";
708 if ( $conds != '*' ) {
709 $sql .= 'WHERE ' . $this->makeList( $conds, LIST_AND
);
713 $this->query( $sql, $fname );
716 # Returns the size of a text field, or -1 for "unlimited"
717 function textFieldSize( $table, $field ) {
718 $table = $this->tableName( $table );
719 $sql = "SELECT t.typname as ftype,a.atttypmod as size
720 FROM pg_class c, pg_attribute a, pg_type t
721 WHERE relname='$table' AND a.attrelid=c.oid AND
722 a.atttypid=t.oid and a.attname='$field'";
723 $res =$this->query( $sql );
724 $row = $this->fetchObject( $res );
725 if ( $row->ftype
== 'varchar' ) {
726 $size = $row->size
- 4;
733 function limitResult( $sql, $limit, $offset = false ) {
734 return "$sql LIMIT $limit " . ( is_numeric( $offset ) ?
" OFFSET {$offset} " : '' );
737 function wasDeadlock() {
738 return $this->lastErrno() == '40P01';
741 function duplicateTableStructure( $oldName, $newName, $temporary = false, $fname = 'DatabasePostgres::duplicateTableStructure' ) {
742 return $this->query( 'CREATE ' . ( $temporary ?
'TEMPORARY ' : '' ) . " TABLE $newName (LIKE $oldName INCLUDING DEFAULTS)", $fname );
745 function timestamp( $ts = 0 ) {
746 return wfTimestamp( TS_POSTGRES
, $ts );
750 * Return aggregated value function call
752 function aggregateValue( $valuedata, $valuename = 'value' ) {
756 function reportQueryError( $error, $errno, $sql, $fname, $tempIgnore = false ) {
757 // Ignore errors during error handling to avoid infinite recursion
758 $ignore = $this->ignoreErrors( true );
759 $this->mErrorCount++
;
761 if ( $ignore ||
$tempIgnore ) {
762 wfDebug( "SQL ERROR (ignored): $error\n" );
763 $this->ignoreErrors( $ignore );
765 $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" .
767 "Function: $fname\n" .
768 "Error: $errno $error\n";
769 throw new DBUnexpectedError( $this, $message );
774 * @return string wikitext of a link to the server software's web site
776 public static function getSoftwareLink() {
777 return '[http://www.postgresql.org/ PostgreSQL]';
781 * @return string Version information from the database
783 function getServerVersion() {
784 if ( !isset( $this->numeric_version
) ) {
785 $versionInfo = pg_version( $this->mConn
);
786 if ( version_compare( $versionInfo['client'], '7.4.0', 'lt' ) ) {
787 // Old client, abort install
788 $this->numeric_version
= '7.3 or earlier';
789 } elseif ( isset( $versionInfo['server'] ) ) {
791 $this->numeric_version
= $versionInfo['server'];
793 // Bug 16937: broken pgsql extension from PHP<5.3
794 $this->numeric_version
= pg_parameter_status( $this->mConn
, 'server_version' );
797 return $this->numeric_version
;
801 * Query whether a given relation exists (in the given schema, or the
802 * default mw one if not given)
804 function relationExists( $table, $types, $schema = false ) {
805 global $wgDBmwschema;
806 if ( !is_array( $types ) ) {
807 $types = array( $types );
810 $schema = $wgDBmwschema;
812 $table = $this->tableName( $table );
813 $etable = $this->addQuotes( $table );
814 $eschema = $this->addQuotes( $schema );
815 $SQL = "SELECT 1 FROM pg_catalog.pg_class c, pg_catalog.pg_namespace n "
816 . "WHERE c.relnamespace = n.oid AND c.relname = $etable AND n.nspname = $eschema "
817 . "AND c.relkind IN ('" . implode( "','", $types ) . "')";
818 $res = $this->query( $SQL );
819 $count = $res ?
$res->numRows() : 0;
824 * For backward compatibility, this function checks both tables and
827 function tableExists( $table, $schema = false ) {
828 return $this->relationExists( $table, array( 'r', 'v' ), $schema );
831 function sequenceExists( $sequence, $schema = false ) {
832 return $this->relationExists( $sequence, 'S', $schema );
835 function triggerExists( $table, $trigger ) {
836 global $wgDBmwschema;
839 SELECT 1 FROM pg_class, pg_namespace, pg_trigger
840 WHERE relnamespace=pg_namespace.oid AND relkind='r'
841 AND tgrelid=pg_class.oid
842 AND nspname=%s AND relname=%s AND tgname=%s
847 $this->addQuotes( $wgDBmwschema ),
848 $this->addQuotes( $table ),
849 $this->addQuotes( $trigger )
855 $rows = $res->numRows();
859 function ruleExists( $table, $rule ) {
860 global $wgDBmwschema;
861 $exists = $this->selectField( 'pg_rules', 'rulename',
864 'tablename' => $table,
865 'schemaname' => $wgDBmwschema
868 return $exists === $rule;
871 function constraintExists( $table, $constraint ) {
872 global $wgDBmwschema;
873 $SQL = sprintf( "SELECT 1 FROM information_schema.table_constraints ".
874 "WHERE constraint_schema = %s AND table_name = %s AND constraint_name = %s",
875 $this->addQuotes( $wgDBmwschema ),
876 $this->addQuotes( $table ),
877 $this->addQuotes( $constraint )
879 $res = $this->query( $SQL );
883 $rows = $res->numRows();
888 * Query whether a given schema exists. Returns the name of the owner
890 function schemaExists( $schema ) {
891 $eschema = str_replace( "'", "''", $schema );
892 $SQL = "SELECT rolname FROM pg_catalog.pg_namespace n, pg_catalog.pg_roles r "
893 ."WHERE n.nspowner=r.oid AND n.nspname = '$eschema'";
894 $res = $this->query( $SQL );
895 if ( $res && $res->numRows() ) {
896 $row = $res->fetchObject();
897 $owner = $row->rolname
;
904 function fieldInfo( $table, $field ) {
905 return PostgresField
::fromText( $this, $table, $field );
909 * pg_field_type() wrapper
911 function fieldType( $res, $index ) {
912 if ( $res instanceof ResultWrapper
) {
915 return pg_field_type( $res, $index );
918 /* Not even sure why this is used in the main codebase... */
919 function limitResultForUpdate( $sql, $num ) {
923 function encodeBlob( $b ) {
924 return new Blob( pg_escape_bytea( $this->mConn
, $b ) );
927 function decodeBlob( $b ) {
928 if ( $b instanceof Blob
) {
931 return pg_unescape_bytea( $b );
934 function strencode( $s ) { # Should not be called by us
935 return pg_escape_string( $this->mConn
, $s );
938 function addQuotes( $s ) {
939 if ( is_null( $s ) ) {
941 } elseif ( is_bool( $s ) ) {
943 } elseif ( $s instanceof Blob
) {
944 return "'" . $s->fetch( $s ) . "'";
946 return "'" . pg_escape_string( $this->mConn
, $s ) . "'";
950 * Postgres specific version of replaceVars.
951 * Calls the parent version in Database.php
955 * @param $ins String: SQL string, read from a stream (usually tables.sql)
957 * @return string SQL string
959 protected function replaceVars( $ins ) {
960 $ins = parent
::replaceVars( $ins );
962 if ( $this->numeric_version
>= 8.3 ) {
963 // Thanks for not providing backwards-compatibility, 8.3
964 $ins = preg_replace( "/to_tsvector\s*\(\s*'default'\s*,/", 'to_tsvector(', $ins );
967 if ( $this->numeric_version
<= 8.1 ) { // Our minimum version
968 $ins = str_replace( 'USING gin', 'USING gist', $ins );
975 * Various select options
979 * @param $options Array: an associative array of options to be turned into
980 * an SQL query, valid keys are listed in the function.
983 function makeSelectOptions( $options ) {
984 $preLimitTail = $postLimitTail = '';
985 $startOpts = $useIndex = '';
987 $noKeyOptions = array();
988 foreach ( $options as $key => $option ) {
989 if ( is_numeric( $key ) ) {
990 $noKeyOptions[$option] = true;
994 if ( isset( $options['GROUP BY'] ) ) {
995 $preLimitTail .= ' GROUP BY ' . $options['GROUP BY'];
997 if ( isset( $options['HAVING'] ) ) {
998 $preLimitTail .= " HAVING {$options['HAVING']}";
1000 if ( isset( $options['ORDER BY'] ) ) {
1001 $preLimitTail .= ' ORDER BY ' . $options['ORDER BY'];
1004 //if ( isset( $options['LIMIT'] ) ) {
1005 // $tailOpts .= $this->limitResult( '', $options['LIMIT'],
1006 // isset( $options['OFFSET'] ) ? $options['OFFSET']
1010 if ( isset( $noKeyOptions['FOR UPDATE'] ) ) {
1011 $postLimitTail .= ' FOR UPDATE';
1013 if ( isset( $noKeyOptions['LOCK IN SHARE MODE'] ) ) {
1014 $postLimitTail .= ' LOCK IN SHARE MODE';
1016 if ( isset( $noKeyOptions['DISTINCT'] ) ||
isset( $noKeyOptions['DISTINCTROW'] ) ) {
1017 $startOpts .= 'DISTINCT';
1020 return array( $startOpts, $useIndex, $preLimitTail, $postLimitTail );
1023 function setFakeMaster( $enabled = true ) {}
1025 function getDBname() {
1026 return $this->mDBname
;
1029 function getServer() {
1030 return $this->mServer
;
1033 function buildConcat( $stringList ) {
1034 return implode( ' || ', $stringList );
1037 public function getSearchEngine() {
1038 return 'SearchPostgres';
1040 } // end DatabasePostgres class