From b36f2a2ae2db1e61f639f79b04427a5f0e1610a1 Mon Sep 17 00:00:00 2001 From: Sean Harvey Date: Thu, 11 Feb 2010 04:40:07 +0000 Subject: [PATCH] ENHANCEMENT PostgreSQLDatabase now automatically creates the database if it doesn't exist, previously you had to manually create one before it can be used --- code/PostgreSQLDatabase.php | 46 ++++++++++++++++++------------------- 1 file changed, 23 insertions(+), 23 deletions(-) diff --git a/code/PostgreSQLDatabase.php b/code/PostgreSQLDatabase.php index ead2cde..f8b3b11 100644 --- a/code/PostgreSQLDatabase.php +++ b/code/PostgreSQLDatabase.php @@ -73,25 +73,31 @@ class PostgreSQLDatabase extends SS_Database { /* * Uses whatever connection details are in the $parameters array to connect to a database of a given name */ - function connectDatabase(){ - - $parameters=$this->parameters; - - if(!$parameters) - return false; + function connectDatabase() { + $parameters = $this->parameters; + if(!$parameters) return false; ($parameters['username']!='') ? $username=' user=' . $parameters['username'] : $username=''; ($parameters['password']!='') ? $password=' password=' . $parameters['password'] : $password=''; - if(!isset($this->database)) - $dbName=$parameters['database']; - else $dbName=$this->database; + if(!isset($this->database)) $dbName = $parameters['database']; + else $dbName = $this->database; - //assumes that the server and dbname will always be provided: + // PostgreSQL expects the database name to be in lowercase! + $dbName = strtolower($dbName); + + // First of all, we need to connect to the server without a database name since it probably won't exist + $this->dbConn = pg_connect("host=" . $parameters['server'] . ' port=5432' . $username . $password); + + // Attempt to create the database if it doesn't exist - in this case the database will reconnect + if(!$this->databaseExists($dbName)) { + $this->selectDatabase($dbName); + $this->createDatabase($dbName); + } + + // This is the connection to the database WITH the database name that MUST exist! (there is no "USE" in PostgreSQL) $this->dbConn = pg_connect('host=' . $parameters['server'] . ' port=5432 dbname=' . $dbName . $username . $password); - - //By virtue of getting here, the connection is active: - $this->active=true; + $this->active = true; $this->database = $dbName; if(!$this->dbConn) { @@ -198,11 +204,8 @@ class PostgreSQLDatabase extends SS_Database { * So you need to have called $this->selectDatabase() first, or used the __construct method */ public function createDatabase() { - $this->query("CREATE DATABASE $this->database"); - $this->connectDatabase(); - } /** @@ -231,21 +234,18 @@ class PostgreSQLDatabase extends SS_Database { * If the database doesn't exist, you should call createDatabase() after calling selectDatabase() */ public function selectDatabase($dbname) { - $this->database=$dbname; - + $this->database = $dbname; $this->tableList = $this->fieldList = $this->indexList = null; - return true; } - /** * Returns true if the named database exists. */ public function databaseExists($name) { - $SQL_name=Convert::raw2sql($name); - $result=$this->query("SELECT datname FROM pg_database WHERE datname='$SQL_name';")->first(); - return $this->query("SELECT datname FROM pg_database WHERE datname='$SQL_name';")->first() ? true : false; + $SQL_name = strtolower($this->addslashes($name)); + $result = $this->query("SELECT datname FROM pg_database WHERE datname = '$SQL_name';")->value(); + return $result ? true : false; } public function createTable($tableName, $fields = null, $indexes = null, $options = null, $extensions = null) {