2010-02-11 08:38:51 +01:00
|
|
|
<?php
|
2016-07-04 02:44:09 +02:00
|
|
|
|
|
|
|
namespace SilverStripe\PostgreSQL;
|
|
|
|
|
2016-09-01 06:22:17 +02:00
|
|
|
use SilverStripe\Dev\Install\DatabaseAdapterRegistry;
|
|
|
|
use SilverStripe\Dev\Install\DatabaseConfigurationHelper;
|
2016-07-04 02:44:09 +02:00
|
|
|
use Exception;
|
2016-09-01 06:22:17 +02:00
|
|
|
use PDO;
|
2016-07-04 02:44:09 +02:00
|
|
|
|
2010-02-11 08:38:51 +01:00
|
|
|
/**
|
|
|
|
* This is a helper class for the SS installer.
|
2016-07-04 02:44:09 +02:00
|
|
|
*
|
2010-02-11 08:38:51 +01:00
|
|
|
* It does all the specific checking for PostgreSQLDatabase
|
|
|
|
* to ensure that the configuration is setup correctly.
|
2016-07-04 02:44:09 +02:00
|
|
|
*
|
2010-02-11 12:04:53 +01:00
|
|
|
* @package postgresql
|
2010-02-11 08:38:51 +01:00
|
|
|
*/
|
2015-12-17 19:18:01 +01:00
|
|
|
class PostgreSQLDatabaseConfigurationHelper implements DatabaseConfigurationHelper
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Create a connection of the appropriate type
|
2016-07-04 02:44:09 +02:00
|
|
|
*
|
2015-12-17 19:18:01 +01:00
|
|
|
* @param array $databaseConfig
|
|
|
|
* @param string $error Error message passed by value
|
|
|
|
* @return mixed|null Either the connection object, or null if error
|
|
|
|
*/
|
|
|
|
protected function createConnection($databaseConfig, &$error)
|
|
|
|
{
|
|
|
|
$error = null;
|
|
|
|
$username = empty($databaseConfig['username']) ? '' : $databaseConfig['username'];
|
|
|
|
$password = empty($databaseConfig['password']) ? '' : $databaseConfig['password'];
|
|
|
|
$server = $databaseConfig['server'];
|
2016-07-04 02:44:09 +02:00
|
|
|
|
2016-07-05 06:27:31 +02:00
|
|
|
/** @skipUpgrade */
|
2015-12-17 19:18:01 +01:00
|
|
|
try {
|
|
|
|
switch ($databaseConfig['type']) {
|
|
|
|
case 'PostgreSQLDatabase':
|
|
|
|
$userPart = $username ? " user=$username" : '';
|
|
|
|
$passwordPart = $password ? " password=$password" : '';
|
|
|
|
$connstring = "host=$server port=5432 dbname=postgres{$userPart}{$passwordPart}";
|
|
|
|
$conn = pg_connect($connstring);
|
|
|
|
break;
|
|
|
|
case 'PostgrePDODatabase':
|
|
|
|
// May throw a PDOException if fails
|
|
|
|
$conn = @new PDO('postgresql:host='.$server.';dbname=postgres;port=5432', $username, $password);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
$error = 'Invalid connection type';
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
} catch (Exception $ex) {
|
|
|
|
$error = $ex->getMessage();
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
if ($conn) {
|
|
|
|
return $conn;
|
|
|
|
} else {
|
|
|
|
$error = 'PostgreSQL requires a valid username and password to determine if the server exists.';
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
2013-04-03 06:27:11 +02:00
|
|
|
|
2015-12-17 19:18:01 +01:00
|
|
|
public function requireDatabaseFunctions($databaseConfig)
|
|
|
|
{
|
|
|
|
$data = DatabaseAdapterRegistry::get_adapter($databaseConfig['type']);
|
|
|
|
return !empty($data['supported']);
|
|
|
|
}
|
2013-04-03 06:27:11 +02:00
|
|
|
|
2015-12-17 19:18:01 +01:00
|
|
|
public function requireDatabaseServer($databaseConfig)
|
|
|
|
{
|
|
|
|
$conn = $this->createConnection($databaseConfig, $error);
|
|
|
|
$success = !empty($conn);
|
|
|
|
return array(
|
|
|
|
'success' => $success,
|
|
|
|
'error' => $error
|
|
|
|
);
|
|
|
|
}
|
2010-02-11 08:38:51 +01:00
|
|
|
|
2015-12-17 19:18:01 +01:00
|
|
|
public function requireDatabaseConnection($databaseConfig)
|
|
|
|
{
|
|
|
|
$conn = $this->createConnection($databaseConfig, $error);
|
|
|
|
$success = !empty($conn);
|
|
|
|
return array(
|
|
|
|
'success' => $success,
|
|
|
|
'connection' => $conn,
|
|
|
|
'error' => $error
|
|
|
|
);
|
|
|
|
}
|
2010-05-15 06:03:25 +02:00
|
|
|
|
2015-12-17 19:18:01 +01:00
|
|
|
public function getDatabaseVersion($databaseConfig)
|
|
|
|
{
|
|
|
|
$conn = $this->createConnection($databaseConfig, $error);
|
|
|
|
if (!$conn) {
|
|
|
|
return false;
|
|
|
|
} elseif ($conn instanceof PDO) {
|
|
|
|
return $conn->getAttribute(PDO::ATTR_SERVER_VERSION);
|
|
|
|
} elseif (is_resource($conn)) {
|
|
|
|
$info = pg_version($conn);
|
|
|
|
return $info['server'];
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2010-05-26 07:14:05 +02:00
|
|
|
|
2015-12-17 19:18:01 +01:00
|
|
|
/**
|
|
|
|
* Ensure that the PostgreSQL version is at least 8.3.
|
2016-07-04 02:44:09 +02:00
|
|
|
*
|
2015-12-17 19:18:01 +01:00
|
|
|
* @param array $databaseConfig Associative array of db configuration, e.g. "server", "username" etc
|
|
|
|
* @return array Result - e.g. array('success' => true, 'error' => 'details of error')
|
|
|
|
*/
|
|
|
|
public function requireDatabaseVersion($databaseConfig)
|
|
|
|
{
|
|
|
|
$success = false;
|
|
|
|
$error = '';
|
|
|
|
$version = $this->getDatabaseVersion($databaseConfig);
|
2010-05-26 07:14:05 +02:00
|
|
|
|
2015-12-17 19:18:01 +01:00
|
|
|
if ($version) {
|
|
|
|
$success = version_compare($version, '8.3', '>=');
|
|
|
|
if (!$success) {
|
|
|
|
$error = "Your PostgreSQL version is $version. It's recommended you use at least 8.3.";
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$error = "Your PostgreSQL version could not be determined.";
|
|
|
|
}
|
2010-05-15 06:03:25 +02:00
|
|
|
|
2015-12-17 19:18:01 +01:00
|
|
|
return array(
|
|
|
|
'success' => $success,
|
|
|
|
'error' => $error
|
|
|
|
);
|
|
|
|
}
|
2016-07-04 02:44:09 +02:00
|
|
|
|
2015-12-17 19:18:01 +01:00
|
|
|
/**
|
|
|
|
* Helper function to execute a query
|
2016-07-04 02:44:09 +02:00
|
|
|
*
|
2015-12-17 19:18:01 +01:00
|
|
|
* @param mixed $conn Connection object/resource
|
|
|
|
* @param string $sql SQL string to execute
|
|
|
|
* @return array List of first value from each resulting row
|
|
|
|
*/
|
|
|
|
protected function query($conn, $sql)
|
|
|
|
{
|
|
|
|
$items = array();
|
|
|
|
if ($conn instanceof PDO) {
|
|
|
|
foreach ($conn->query($sql) as $row) {
|
|
|
|
$items[] = $row[0];
|
|
|
|
}
|
|
|
|
} elseif (is_resource($conn)) {
|
|
|
|
$result = pg_query($conn, $sql);
|
|
|
|
while ($row = pg_fetch_row($result)) {
|
|
|
|
$items[] = $row[0];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $items;
|
|
|
|
}
|
2013-04-03 06:27:11 +02:00
|
|
|
|
2015-12-17 19:18:01 +01:00
|
|
|
public function requireDatabaseOrCreatePermissions($databaseConfig)
|
|
|
|
{
|
|
|
|
$success = false;
|
|
|
|
$alreadyExists = false;
|
|
|
|
$conn = $this->createConnection($databaseConfig, $error);
|
|
|
|
if ($conn) {
|
|
|
|
// Check if db already exists
|
|
|
|
$existingDatabases = $this->query($conn, "SELECT datname FROM pg_database");
|
|
|
|
$alreadyExists = in_array($databaseConfig['database'], $existingDatabases);
|
|
|
|
if ($alreadyExists) {
|
|
|
|
$success = true;
|
|
|
|
} else {
|
|
|
|
// Check if this user has create privileges
|
|
|
|
$allowedUsers = $this->query($conn, "select rolname from pg_authid where rolcreatedb = true;");
|
|
|
|
$success = in_array($databaseConfig['username'], $allowedUsers);
|
|
|
|
}
|
|
|
|
}
|
2016-07-04 02:44:09 +02:00
|
|
|
|
2015-12-17 19:18:01 +01:00
|
|
|
return array(
|
|
|
|
'success' => $success,
|
|
|
|
'alreadyExists' => $alreadyExists
|
|
|
|
);
|
|
|
|
}
|
2013-04-03 06:27:11 +02:00
|
|
|
|
2015-12-17 19:18:01 +01:00
|
|
|
public function requireDatabaseAlterPermissions($databaseConfig)
|
|
|
|
{
|
|
|
|
$conn = $this->createConnection($databaseConfig, $error);
|
|
|
|
if ($conn) {
|
|
|
|
// if the account can even log in, it can alter tables
|
|
|
|
return array(
|
|
|
|
'success' => true,
|
|
|
|
'applies' => true
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return array(
|
|
|
|
'success' => false,
|
|
|
|
'applies' => true
|
|
|
|
);
|
|
|
|
}
|
2010-02-11 11:00:13 +01:00
|
|
|
}
|