Skip to content

Commit

Permalink
Merge pull request #2 from thhan/main
Browse files Browse the repository at this point in the history
Removed deprecations and modify Driver class.
  • Loading branch information
iamwildtuna committed Aug 1, 2021
2 parents d023a75 + 956e7f3 commit 963b94a
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 61 deletions.
134 changes: 109 additions & 25 deletions src/Driver/CockroachDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,54 +2,138 @@

namespace LapayGroup\DoctrineCockroach\Driver;

use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Driver\PDOPgSql;
use Doctrine\DBAL\Driver\PDO;
use Doctrine\DBAL;
use Doctrine\DBAL\Driver\PDO\Connection;
use Doctrine\DBAL\Driver\AbstractPostgreSQLDriver;
use Doctrine\DBAL\Exception;
use Doctrine\Deprecations\Deprecation;
use LapayGroup\DoctrineCockroach\Platforms\CockroachPlatform;
use LapayGroup\DoctrineCockroach\Schema\CockroachSchemaManager;
use PDO;
use PDOException;


/**
* Driver that connects through pdo_pgsql.
*
* @deprecated Use {@link PDO\PgSQL\Driver} instead.
*/
class CockroachDriver extends PDOPgSql\Driver
class CockroachDriver extends AbstractPostgreSQLDriver
{
public function connect(array $params, $username = null, $password = null, array $driverOptions = []): Connection
{
try {
$pdo = new Connection(
$this->_constructPdoDsn($params),
$username,
$password,
$driverOptions
);

if (
defined('PDO::PGSQL_ATTR_DISABLE_PREPARES')
&& (! isset($driverOptions[PDO::PGSQL_ATTR_DISABLE_PREPARES])
|| $driverOptions[PDO::PGSQL_ATTR_DISABLE_PREPARES] === true
)
) {
$pdo->setAttribute(PDO::PGSQL_ATTR_DISABLE_PREPARES, true);
}

/* defining client_encoding via SET NAMES to avoid inconsistent DSN support
* - the 'client_encoding' connection param only works with postgres >= 9.1
* - passing client_encoding via the 'options' param breaks pgbouncer support
*/
if (isset($params['charset'])) {
$pdo->exec('SET NAMES \'' . $params['charset'] . '\'');
}

return $pdo;
} catch (PDOException $e) {
throw Exception::driverException($this, $e);
}
}

/**
* {@inheritdoc}
* Constructs the Postgres PDO DSN.
*
* @param array $params
*
* @return string The DSN.
*/
public function connect(array $params, $username = null, $password = null, array $driverOptions = [])
private function _constructPdoDsn(array $params): string
{
/** @var PDO\Connection $pdo */
$pdo = call_user_func_array(array($this, 'parent::connect'), func_get_args());
$dsn = 'pgsql:';

if (isset($params['host']) && $params['host'] !== '') {
$dsn .= 'host=' . $params['host'] . ';';
}

if (isset($params['port']) && $params['port'] !== '') {
$dsn .= 'port=' . $params['port'] . ';';
}

if (isset($params['dbname'])) {
$dsn .= 'dbname=' . $params['dbname'] . ';';
} elseif (isset($params['default_dbname'])) {
$dsn .= 'dbname=' . $params['default_dbname'] . ';';
} else {
// Used for temporary connections to allow operations like dropping the database currently connected to.
// Connecting without an explicit database does not work, therefore "postgres" database is used
// as it is mostly present in every server setup.
$dsn .= 'dbname=postgres;';
}

if (isset($params['sslmode'])) {
$dsn .= 'sslmode=' . $params['sslmode'] . ';';
}

if (isset($params['sslrootcert'])) {
$dsn .= 'sslrootcert=' . $params['sslrootcert'] . ';';
}

if (isset($params['sslcert'])) {
$dsn .= 'sslcert=' . $params['sslcert'] . ';';
}

// $res = $pdo->exec('set experimental_serial_normalization=sql_sequence;');
if (isset($params['sslkey'])) {
$dsn .= 'sslkey=' . $params['sslkey'] . ';';
}

return $pdo;
if (isset($params['sslcrl'])) {
$dsn .= 'sslcrl=' . $params['sslcrl'] . ';';
}

if (isset($params['application_name'])) {
$dsn .= 'application_name=' . $params['application_name'] . ';';
}

return $dsn;
}

/**
* {@inheritdoc}
*
* @deprecated
*/
public function getDatabasePlatform()
public function getName(): string
{
Deprecation::trigger(
'doctrine/dbal',
'https://github.com/doctrine/dbal/issues/3580',
'Driver::getName() is deprecated'
);

return 'pdo_pgsql';
}

public function getDatabasePlatform(): CockroachPlatform
{
return new CockroachPlatform();
}

/**
* {@inheritdoc}
*/
public function createDatabasePlatformForVersion($version)
public function createDatabasePlatformForVersion($version): CockroachPlatform
{
return new CockroachPlatform();
}

/**
* {@inheritdoc}
*/
public function getSchemaManager(Connection $conn)

public function getSchemaManager(DBAL\Connection $conn): CockroachSchemaManager
{
return new CockroachSchemaManager($conn);
}
}
}
38 changes: 9 additions & 29 deletions src/Platforms/CockroachPlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@

class CockroachPlatform extends PostgreSQL100Platform
{
/**
* {@inheritDoc}
*/
public function getListNamespacesSQL(): string
{
return "SELECT schema_name AS nspname
Expand All @@ -19,9 +16,6 @@ public function getListNamespacesSQL(): string
AND schema_name != 'crdb_internal'";
}

/**
* {@inheritDoc}
*/
public function getListSequencesSQL($database): string
{
return "SELECT sequence_name AS relname,
Expand All @@ -32,9 +26,6 @@ public function getListSequencesSQL($database): string
AND sequence_schema != 'crdb_internal'";
}

/**
* {@inheritDoc}
*/
public function getListTablesSQL(): string
{
return "SELECT quote_ident(table_name) AS table_name,
Expand All @@ -48,10 +39,7 @@ public function getListTablesSQL(): string
AND table_schema != 'crdb_internal'";
}

/**
* {@inheritDoc}
*/
protected function initializeDoctrineTypeMappings()
protected function initializeDoctrineTypeMappings(): void
{
parent::initializeDoctrineTypeMappings();

Expand All @@ -62,38 +50,30 @@ protected function initializeDoctrineTypeMappings()
]);
}

/**
* {@inheritDoc}
*/
public function getIntegerTypeDeclarationSQL(array $column)
public function getIntegerTypeDeclarationSQL(array $column): string
{
if (! empty($column['autoincrement'])) {
return 'SERIAL';
}

return 'INT4';
return 'INT';
}

/**
* {@inheritDoc}
*/
public function getAdvancedForeignKeyOptionsSQL(ForeignKeyConstraint $foreignKey)
{
// Waiting for resolved https://github.com/cockroachdb/cockroach/issues/31632
$query = '';
public function getAdvancedForeignKeyOptionsSQL(ForeignKeyConstraint $foreignKey): string
{$query = '';

if ($foreignKey->hasOption('match')) {
$query .= ' MATCH ' . $foreignKey->getOption('match');
}

if (parent::supportsForeignKeyOnUpdate() && $foreignKey->hasOption('onUpdate')) {
$query .= ' ON UPDATE ' . parent::getForeignKeyReferentialActionSQL($foreignKey->getOption('onUpdate'));
if ($this->supportsForeignKeyConstraints() && $foreignKey->hasOption('onUpdate')) {
$query .= ' ON UPDATE ' . $this->getForeignKeyReferentialActionSQL($foreignKey->getOption('onUpdate'));
}

if ($foreignKey->hasOption('onDelete')) {
$query .= ' ON DELETE ' . parent::getForeignKeyReferentialActionSQL($foreignKey->getOption('onDelete'));
$query .= ' ON DELETE ' . $this->getForeignKeyReferentialActionSQL($foreignKey->getOption('onDelete'));
}

return $query;
}
}
}
11 changes: 4 additions & 7 deletions src/Schema/CockroachSchemaManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@

class CockroachSchemaManager extends PostgreSqlSchemaManager
{
/**
* {@inheritdoc}
*/
protected function _getPortableSequenceDefinition($sequence)
protected function _getPortableSequenceDefinition($sequence): Sequence
{
if ($sequence['schemaname'] !== 'public') {
$sequenceName = $sequence['schemaname'] . '.' . $sequence['relname'];
Expand All @@ -23,7 +20,7 @@ protected function _getPortableSequenceDefinition($sequence)
$sequence['increment_by'] = 0;

/** @var string[] $data */
$data = $this->_conn->fetchAssoc('SHOW CREATE ' . $this->_platform->quoteIdentifier($sequenceName));
$data = $this->_conn->fetchAssociative('SHOW CREATE ' . $this->_platform->quoteIdentifier($sequenceName));
if (!empty($data['create_statement'])) {
$matches = [];
preg_match_all('/ -?\d+/', $data['create_statement'], $matches);
Expand All @@ -35,6 +32,6 @@ protected function _getPortableSequenceDefinition($sequence)
}
}

return new Sequence($sequenceName, (int) $sequence['increment_by'], (int) $sequence['min_value']);
return new Sequence($sequenceName, (int) $sequence['increment_by'], $sequence['min_value']);
}
}
}

0 comments on commit 963b94a

Please sign in to comment.