From c62197d613a79b9f817e704edc95b12775e6ff26 Mon Sep 17 00:00:00 2001 From: Greg Sabino Mullane Date: Wed, 28 Jun 2006 18:05:08 +0000 Subject: [PATCH] Remove __construct call Add in global port check for connect Bump error level up Setup initial search_path for user on setup Fix tableExists, schemaExists, tableName, and fieldExists methods Drop setSchema() --- includes/DatabasePostgres.php | 154 ++++++++++++++++++++++------------ 1 file changed, 100 insertions(+), 54 deletions(-) diff --git a/includes/DatabasePostgres.php b/includes/DatabasePostgres.php index 00df8a74d8..7b9e87d1e3 100644 --- a/includes/DatabasePostgres.php +++ b/includes/DatabasePostgres.php @@ -22,7 +22,18 @@ class DatabasePostgres extends Database { function DatabasePostgres($server = false, $user = false, $password = false, $dbName = false, $failFunction = false, $flags = 0 ) { - Database::__construct( $server, $user, $password, $dbName, $failFunction, $flags ); + + global $wgOut, $wgDBprefix, $wgCommandLineMode; + # Can't get a reference if it hasn't been set yet + if ( !isset( $wgOut ) ) { + $wgOut = NULL; + } + $this->mOut =& $wgOut; + $this->mFailFunction = $failFunction; + $this->mFlags = $flags; + + $this->open( $server, $user, $password, $dbName); + } static function newFromParams( $server = false, $user = false, $password = false, $dbName = false, @@ -41,34 +52,76 @@ class DatabasePostgres extends Database { throw new DBConnectionError( $this, "PostgreSQL functions missing, have you compiled PHP with the --with-pgsql option?\n" ); } - global $wgDBschema; + global $wgDBschema, $wgDBport; $this->close(); $this->mServer = $server; + $port = $wgDBport; $this->mUser = $user; $this->mPassword = $password; $this->mDBname = $dbName; - $this->mSchemas = array($wgDBschema,'public'); + $schema = $wgDBschema; $success = false; - if ( '' != $dbName ) { - # start a database connection - $hstring=""; - if ($server!=false && $server!="") { - $hstring="host=$server "; + $hstring=""; + if ($server!=false && $server!="") { + $hstring="host=$server "; + } + if ($port!=false && $port!="") { + $hstring .= "port=$port "; + } + + error_reporting( E_ALL ); + + @$this->mConn = pg_connect("$hstring dbname=$dbName user=$user password=$password"); + + if ( $this->mConn == false ) { + wfDebug( "DB connection error\n" ); + wfDebug( "Server: $server, Database: $dbName, User: $user, Password: " . substr( $password, 0, 3 ) . "...\n" ); + wfDebug( $this->lastError()."\n" ); + return false; + } + + $this->mOpened = true; + ## If this is the initial connection, setup the schema stuff + if (defined('MEDIAWIKI_INSTALL')) { + ## Does the schema already exist? Who owns it? + $result = $this->schemaExists($schema); + if (!$result) { + print "
  • Creating schema $schema ..."; + $result = $this->doQuery("CREATE SCHEMA $schema"); + if (!$result) { + print "FAILED.
  • \n"; + return false; + } + print "ok\n"; + } + else if ($result != $user) { + print "
  • Schema $schema exists but is not owned by $user. Not ideal.
  • \n"; + } + else { + print "
  • Schema $schema exists and is owned by $user ($result). Excellent.
  • \n"; } - @$this->mConn = pg_connect("$hstring dbname=$dbName user=$user password=$password"); - if ( $this->mConn == false ) { - wfDebug( "DB connection error\n" ); - wfDebug( "Server: $server, Database: $dbName, User: $user, Password: " . substr( $password, 0, 3 ) . "...\n" ); - wfDebug( $this->lastError()."\n" ); - } else { - $this->setSchema(); - $this->mOpened = true; + ## Fix up the search paths if needed + print "
  • Setting the search path for user $user ..."; + $SQL = "ALTER USER $user SET search_path = $schema, public"; + $result = pg_query($this->mConn, $SQL); + if (!$result) { + print "FAILED.
  • \n"; + return false; + } + print "ok\n"; + ## Set for the rest of this session + $SQL = "SET search_path = $schema, public"; + $result = pg_query($this->mConn, $SQL); + if (!$result) { + print "
  • Failed to set search_path
  • \n"; + return false; } } + return $this->mConn; } @@ -164,7 +217,7 @@ class DatabasePostgres extends Database { } while ( $row = $this->fetchObject( $res ) ) { - if ( $row->Key_name == $index ) { + if ( $row->indexname == $index ) { return $row; } } @@ -183,23 +236,6 @@ class DatabasePostgres extends Database { } - function fieldInfo( $table, $field ) { - return false; - throw new DBUnexpectedError($this, 'Database::fieldInfo() error : mysql_fetch_field() not implemented for postgre' ); - /* - $res = $this->query( "SELECT * FROM '$table' LIMIT 1" ); - $n = pg_num_fields( $res ); - for( $i = 0; $i < $n; $i++ ) { - // FIXME - throw new DBUnexpectedError($this, "Database::fieldInfo() error : mysql_fetch_field() not implemented for postgre" ); - $meta = mysql_fetch_field( $res, $i ); - if( $field == $meta->name ) { - return $meta; - } - } - return false;*/ - } - function insert( $table, $a, $fname = 'Database::insert', $options = array() ) { # PostgreSQL doesn't support options # We have a go at faking one of them @@ -230,9 +266,6 @@ class DatabasePostgres extends Database { } function tableName( $name ) { - # First run any transformations from the parent object - $name = parent::tableName( $name ); - # Replace backticks into double quotes $name = strtr($name,'`','"'); @@ -427,27 +460,35 @@ class DatabasePostgres extends Database { return $version; } - function setSchema($schema=false) { - $schemas=$this->mSchemas; - if ($schema) { array_unshift($schemas,$schema); } - $searchpath=$this->makeList($schemas,LIST_NAMES); - $this->query("SET search_path = $searchpath"); - } /** - * Query whether a given table exists + * Query whether a given table exists (in the default schema) */ function tableExists( $table, $fname = 'DatabasePostgres:tableExists' ) { global $wgDBschema; $stable = preg_replace("/'/", "''", $table); $SQL = "SELECT 1 FROM pg_catalog.pg_class c, pg_catalog.pg_namespace n " - . "WHERE c.relnamespace = n.oid AND c.relname = '$stable AND n.nspname = '$wgDBschema'"; + . "WHERE c.relnamespace = n.oid AND c.relname = '$stable' AND n.nspname = '$wgDBschema'"; $res = $this->query( $SQL, $fname ); - if ($res) { + $count = $res ? pg_num_rows($res) : 0; + if ($res) $this->freeResult( $res ); - return true; - } - return false; + return $count; + } + + /** + * Query whether a given schema exists. Returns the name of the owner + */ + function schemaExists( $schema, $fname = 'DatabasePostgres:schemaExists' ) { + $sschema = preg_replace("/'/", "''", $schema); + $SQL = "SELECT rolname FROM pg_catalog.pg_namespace n, pg_catalog.pg_roles r " + ."WHERE n.nspowner=r.oid AND n.nspname = '$sschema'"; + $res = $this->query($SQL, $fname); + $res = $this->query( $SQL, $fname ); + $owner = $res ? pg_num_rows($res) ? pg_fetch_result($res, 0, 0) : false : false; + if ($res) + $this->freeResult($res); + return $owner; } /** @@ -459,13 +500,18 @@ class DatabasePostgres extends Database { $scol = preg_replace("/'/", "''", $field); $SQL = "SELECT 1 FROM pg_catalog.pg_class c, pg_catalog.pg_namespace n, pg_catalog.pg_attribute a " . "WHERE c.relnamespace = n.oid AND c.relname = '$stable' AND n.nspname = '$wgDBschema' " - . "AND a.attrelid = c.oid AND a.attname = '$safecol'"; + . "AND a.attrelid = c.oid AND a.attname = '$scol'"; $res = $this->query( $SQL, $fname ); - if ($res) { + $count = $res ? pg_num_rows($res) : 0; + if ($res) $this->freeResult( $res ); - return true; - } - return false; + return $count; + } + + function fieldInfo( $table, $field ) { + $res = $this->query( "SELECT $field FROM $table LIMIT 1" ); + $type = pg_field_type( $res, 0 ); + return $type; } } -- 2.20.1