mirror of
https://github.com/silverstripe/silverstripe-postgresql
synced 2024-09-29 20:59:01 +02:00
API CHANGE: database creation now supported, dbDataType function now included
This commit is contained in:
parent
77a76c28c5
commit
11d16b3c7f
@ -87,7 +87,12 @@ class PostgreSQLDatabase extends SS_Database {
|
|||||||
$dbName=$parameters['database'];
|
$dbName=$parameters['database'];
|
||||||
else $dbName=$this->database;
|
else $dbName=$this->database;
|
||||||
|
|
||||||
//assumes that the server and dbname will always be provided:
|
//First, we need to check that this database exists. To do this, we will connect to the 'postgres' database first
|
||||||
|
$this->dbConn = pg_connect('host=' . $parameters['server'] . ' port=5432 dbname=postgres' . $username . $password);
|
||||||
|
if(!$this->databaseExists($dbName))
|
||||||
|
$this->createDatabase($dbName);
|
||||||
|
|
||||||
|
//Now we can be sure that this database exists, so we can connect to it
|
||||||
$this->dbConn = pg_connect('host=' . $parameters['server'] . ' port=5432 dbname=' . $dbName . $username . $password);
|
$this->dbConn = pg_connect('host=' . $parameters['server'] . ' port=5432 dbname=' . $dbName . $username . $password);
|
||||||
|
|
||||||
//By virtue of getting here, the connection is active:
|
//By virtue of getting here, the connection is active:
|
||||||
@ -194,12 +199,13 @@ class PostgreSQLDatabase extends SS_Database {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* This will create a database based on whatever is in the $this->database value
|
* You can create a database based either on a supplied name, or from whatever is in the $this->database value
|
||||||
* So you need to have called $this->selectDatabase() first, or used the __construct method
|
|
||||||
*/
|
*/
|
||||||
public function createDatabase() {
|
public function createDatabase($name=false) {
|
||||||
|
if(!$name)
|
||||||
|
$name=$this->database;
|
||||||
|
|
||||||
$this->query("CREATE DATABASE $this->database");
|
$this->query("CREATE DATABASE \"$name\";");
|
||||||
|
|
||||||
$this->connectDatabase();
|
$this->connectDatabase();
|
||||||
|
|
||||||
@ -243,8 +249,9 @@ class PostgreSQLDatabase extends SS_Database {
|
|||||||
* Returns true if the named database exists.
|
* Returns true if the named database exists.
|
||||||
*/
|
*/
|
||||||
public function databaseExists($name) {
|
public function databaseExists($name) {
|
||||||
$SQL_name=Convert::raw2sql($name);
|
// We have to use addslashes here, since there may not be a database connection to base the Convert::raw2sql
|
||||||
$result=$this->query("SELECT datname FROM pg_database WHERE datname='$SQL_name';")->first();
|
// function off.
|
||||||
|
$SQL_name=addslashes($name);
|
||||||
return $this->query("SELECT datname FROM pg_database WHERE datname='$SQL_name';")->first() ? true : false;
|
return $this->query("SELECT datname FROM pg_database WHERE datname='$SQL_name';")->first() ? true : false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1177,6 +1184,21 @@ class PostgreSQLDatabase extends SS_Database {
|
|||||||
return 'RANDOM()';
|
return 'RANDOM()';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* This is a lookup table for data types.
|
||||||
|
* For instance, Postgres uses 'INT', while MySQL uses 'UNSIGNED'
|
||||||
|
* So this is a DB-specific list of equivilents.
|
||||||
|
*/
|
||||||
|
function dbDataType($type){
|
||||||
|
$values=Array(
|
||||||
|
'unsigned integer'=>'INT'
|
||||||
|
);
|
||||||
|
|
||||||
|
if(isset($values[$type]))
|
||||||
|
return $values[$type];
|
||||||
|
else return '';
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Convert a SQLQuery object into a SQL statement
|
* Convert a SQLQuery object into a SQL statement
|
||||||
* @todo There is a lot of duplication between this and MySQLDatabase::sqlQueryToString(). Perhaps they could both call a common
|
* @todo There is a lot of duplication between this and MySQLDatabase::sqlQueryToString(). Perhaps they could both call a common
|
||||||
|
Loading…
Reference in New Issue
Block a user