Merge pull request #145 from creative-commoners/pulls/3.0/pdo

API Remove PDO support
This commit is contained in:
Guy Sartorelli 2023-03-20 14:25:49 +13:00 committed by GitHub
commit e484046171
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 15 additions and 55 deletions

View File

@ -2,12 +2,6 @@
name: postgresqlconnectors name: postgresqlconnectors
--- ---
SilverStripe\Core\Injector\Injector: SilverStripe\Core\Injector\Injector:
PostgrePDODatabase:
class: 'SilverStripe\PostgreSQL\PostgreSQLDatabase'
properties:
connector: '%$PDOConnector'
schemaManager: '%$PostgreSQLSchemaManager'
queryBuilder: '%$PostgreSQLQueryBuilder'
PostgreSQLDatabase: PostgreSQLDatabase:
class: 'SilverStripe\PostgreSQL\PostgreSQLDatabase' class: 'SilverStripe\PostgreSQL\PostgreSQLDatabase'
properties: properties:

View File

@ -3,23 +3,6 @@
use SilverStripe\Dev\Install\DatabaseAdapterRegistry; use SilverStripe\Dev\Install\DatabaseAdapterRegistry;
use SilverStripe\PostgreSQL\PostgreSQLDatabaseConfigurationHelper; use SilverStripe\PostgreSQL\PostgreSQLDatabaseConfigurationHelper;
// PDO Postgre database
DatabaseAdapterRegistry::register(array(
/** @skipUpgrade */
'class' => 'PostgrePDODatabase',
'module' => 'postgresql',
'title' => 'PostgreSQL 8.3+ (using PDO)',
'helperPath' => __DIR__.'/code/PostgreSQLDatabaseConfigurationHelper.php',
'helperClass' => PostgreSQLDatabaseConfigurationHelper::class,
'supported' => (class_exists('PDO') && in_array('postgresql', PDO::getAvailableDrivers())),
'missingExtensionText' =>
'Either the <a href="http://www.php.net/manual/en/book.pdo.php">PDO Extension</a> or
the <a href="http://www.php.net/manual/en/ref.pdo-sqlsrv.php">SQL Server PDO Driver</a>
are unavailable. Please install or enable these and refresh this page.'
));
// PDO Postgre database
DatabaseAdapterRegistry::register(array( DatabaseAdapterRegistry::register(array(
/** @skipUpgrade */ /** @skipUpgrade */
'class' => 'PostgreSQLDatabase', 'class' => 'PostgreSQLDatabase',

View File

@ -72,7 +72,7 @@ class PostgreSQLConnector extends DBConnector
// Note: Postgres always behaves as though $selectDB = true, ignoring // Note: Postgres always behaves as though $selectDB = true, ignoring
// any value actually passed in. The controller passes in true for other // any value actually passed in. The controller passes in true for other
// connectors such as PDOConnector. // connectors
// Escape parameters // Escape parameters
$arguments = array( $arguments = array(
@ -218,7 +218,6 @@ class PostgreSQLConnector extends DBConnector
// Execute query // Execute query
// Unfortunately error-suppression is required in order to handle sql errors elegantly. // Unfortunately error-suppression is required in order to handle sql errors elegantly.
// Please use PDO if you can help it
if (!empty($parameters)) { if (!empty($parameters)) {
$result = @pg_query_params($this->dbConn, $sql, $parameters); $result = @pg_query_params($this->dbConn, $sql, $parameters);
} else { } else {

View File

@ -214,7 +214,7 @@ class PostgreSQLDatabase extends Database
} }
$this->schemaOriginal = $parameters['schema']; $this->schemaOriginal = $parameters['schema'];
// Ensure that driver is available (required by PDO) // Ensure that driver is available
if (empty($parameters['driver'])) { if (empty($parameters['driver'])) {
$parameters['driver'] = $this->getDatabaseServer(); $parameters['driver'] = $this->getDatabaseServer();
} }

View File

@ -5,7 +5,6 @@ namespace SilverStripe\PostgreSQL;
use SilverStripe\Dev\Install\DatabaseAdapterRegistry; use SilverStripe\Dev\Install\DatabaseAdapterRegistry;
use SilverStripe\Dev\Install\DatabaseConfigurationHelper; use SilverStripe\Dev\Install\DatabaseConfigurationHelper;
use Exception; use Exception;
use PDO;
/** /**
* This is a helper class for the SS installer. * This is a helper class for the SS installer.
@ -40,10 +39,6 @@ class PostgreSQLDatabaseConfigurationHelper implements DatabaseConfigurationHelp
$connstring = "host=$server port=5432 dbname=postgres{$userPart}{$passwordPart}"; $connstring = "host=$server port=5432 dbname=postgres{$userPart}{$passwordPart}";
$conn = pg_connect($connstring); $conn = pg_connect($connstring);
break; break;
case 'PostgrePDODatabase':
// May throw a PDOException if fails
$conn = @new PDO('postgresql:host='.$server.';dbname=postgres;port=5432', $username, $password);
break;
default: default:
$error = 'Invalid connection type: ' . $databaseConfig['type']; $error = 'Invalid connection type: ' . $databaseConfig['type'];
return null; return null;
@ -92,8 +87,6 @@ class PostgreSQLDatabaseConfigurationHelper implements DatabaseConfigurationHelp
$conn = $this->createConnection($databaseConfig, $error); $conn = $this->createConnection($databaseConfig, $error);
if (!$conn) { if (!$conn) {
return false; return false;
} elseif ($conn instanceof PDO) {
return $conn->getAttribute(PDO::ATTR_SERVER_VERSION);
} elseif (is_resource($conn)) { } elseif (is_resource($conn)) {
$info = pg_version($conn); $info = pg_version($conn);
return $info['server']; return $info['server'];
@ -139,11 +132,7 @@ class PostgreSQLDatabaseConfigurationHelper implements DatabaseConfigurationHelp
protected function query($conn, $sql) protected function query($conn, $sql)
{ {
$items = array(); $items = array();
if ($conn instanceof PDO) { if (is_resource($conn)) {
foreach ($conn->query($sql) as $row) {
$items[] = $row[0];
}
} elseif (is_resource($conn)) {
$result = pg_query($conn, $sql); $result = pg_query($conn, $sql);
while ($row = pg_fetch_row($result)) { while ($row = pg_fetch_row($result)) {
$items[] = $row[0]; $items[] = $row[0];

View File

@ -849,23 +849,18 @@ class PostgreSQLSchemaManager extends DBSchemaManager
$trigger['tgargs'] = stream_get_contents($trigger['tgargs']); $trigger['tgargs'] = stream_get_contents($trigger['tgargs']);
} }
if (strpos($trigger['tgargs'], "\000") !== false) { // hex-encoded (pg_sql non-pdo)
// Option 1: output as a string (PDO) $bytes = str_split($trigger['tgargs'], 2);
$argList = array_filter(explode("\000", $trigger['tgargs'])); $argList = array();
} else { $nextArg = "";
// Option 2: hex-encoded (pg_sql non-pdo) foreach ($bytes as $byte) {
$bytes = str_split($trigger['tgargs'], 2); if ($byte == '\x') {
$argList = array(); continue;
$nextArg = ""; } elseif ($byte == "00") {
foreach ($bytes as $byte) { $argList[] = $nextArg;
if ($byte == '\x') { $nextArg = "";
continue; } else {
} elseif ($byte == "00") { $nextArg .= chr(hexdec($byte));
$argList[] = $nextArg;
$nextArg = "";
} else {
$nextArg .= chr(hexdec($byte));
}
} }
} }