-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
20 changed files
with
1,182 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:noNamespaceSchemaLocation="../../../vendor/phpunit/phpunit/phpunit.xsd" | ||
colors="true" | ||
beStrictAboutOutputDuringTests="true" | ||
beStrictAboutTodoAnnotatedTests="true" | ||
failOnRisky="true" | ||
failOnWarning="true" | ||
convertDeprecationsToExceptions="true" | ||
> | ||
<php> | ||
<ini name="error_reporting" value="-1" /> | ||
|
||
<var name="db_driver" value="pgsql"/> | ||
<var name="db_host" value="localhost" /> | ||
<var name="db_user" value="postgres" /> | ||
<var name="db_password" value="postgres" /> | ||
<var name="db_dbname" value="doctrine_tests" /> | ||
</php> | ||
|
||
<testsuites> | ||
<testsuite name="Doctrine DBAL Test Suite"> | ||
<directory>../../../tests</directory> | ||
</testsuite> | ||
</testsuites> | ||
|
||
<coverage> | ||
<include> | ||
<directory suffix=".php">../../../src</directory> | ||
</include> | ||
</coverage> | ||
</phpunit> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
<?php | ||
|
||
namespace Doctrine\DBAL\Driver\PgSQL; | ||
|
||
use Doctrine\DBAL\Driver\ServerInfoAwareConnection; | ||
use Doctrine\DBAL\ParameterType; | ||
use Doctrine\DBAL\SQL\Parser; | ||
use Doctrine\Deprecations\Deprecation; | ||
use PgSql\Connection as PgSqlConnection; | ||
use TypeError; | ||
|
||
use function assert; | ||
use function get_class; | ||
use function gettype; | ||
use function is_object; | ||
use function is_resource; | ||
use function pg_escape_bytea; | ||
use function pg_escape_literal; | ||
use function pg_get_result; | ||
use function pg_result_error; | ||
use function pg_send_prepare; | ||
use function pg_version; | ||
use function sprintf; | ||
use function uniqid; | ||
|
||
final class Connection implements ServerInfoAwareConnection | ||
{ | ||
/** @var PgSqlConnection|resource */ | ||
private $connection; | ||
|
||
private Parser $parser; | ||
|
||
/** @param PgSqlConnection|resource $connection */ | ||
public function __construct($connection) | ||
{ | ||
if (! is_resource($connection) && ! $connection instanceof PgSqlConnection) { | ||
throw new TypeError(sprintf( | ||
'Expected connection to be a resource or an instance of %s, got %s.', | ||
PgSqlConnection::class, | ||
is_object($connection) ? get_class($connection) : gettype($connection), | ||
)); | ||
} | ||
|
||
$this->connection = $connection; | ||
$this->parser = new Parser(false); | ||
} | ||
|
||
public function prepare(string $sql): Statement | ||
{ | ||
$visitor = new ConvertParameters(); | ||
$this->parser->parse($sql, $visitor); | ||
|
||
$statementName = uniqid('dbal', true); | ||
$success = (bool) pg_send_prepare($this->connection, $statementName, $visitor->getSQL()); | ||
|
||
assert($success); | ||
|
||
$result = @pg_get_result($this->connection); | ||
assert($result !== false); | ||
|
||
if ((bool) pg_result_error($result)) { | ||
throw Exception::fromResult($result); | ||
} | ||
|
||
return new Statement($this->connection, $statementName, $visitor->getParameterMap()); | ||
} | ||
|
||
public function query(string $sql): Result | ||
{ | ||
return $this->prepare($sql)->execute(); | ||
} | ||
|
||
/** {@inheritdoc} */ | ||
public function quote($value, $type = ParameterType::STRING) | ||
{ | ||
if ($type === ParameterType::BINARY || $type === ParameterType::LARGE_OBJECT) { | ||
return sprintf("'%s'", pg_escape_bytea($this->connection, $value)); | ||
} | ||
|
||
return pg_escape_literal($this->connection, $value); | ||
} | ||
|
||
public function exec(string $sql): int | ||
{ | ||
return $this->prepare($sql)->execute()->rowCount(); | ||
} | ||
|
||
/** {@inheritdoc} */ | ||
public function lastInsertId($name = null) | ||
{ | ||
if ($name !== null) { | ||
Deprecation::triggerIfCalledFromOutside( | ||
'doctrine/dbal', | ||
'https://github.com/doctrine/dbal/issues/4687', | ||
'The usage of Connection::lastInsertId() with a sequence name is deprecated.', | ||
); | ||
|
||
return $this->query(sprintf('SELECT CURRVAL(%s)', $this->quote($name)))->fetchOne(); | ||
} | ||
|
||
return $this->query('SELECT LASTVAL()')->fetchOne(); | ||
} | ||
|
||
/** @return true */ | ||
public function beginTransaction(): bool | ||
{ | ||
$this->exec('BEGIN'); | ||
|
||
return true; | ||
} | ||
|
||
/** @return true */ | ||
public function commit(): bool | ||
{ | ||
$this->exec('COMMIT'); | ||
|
||
return true; | ||
} | ||
|
||
/** @return true */ | ||
public function rollBack(): bool | ||
{ | ||
$this->exec('ROLLBACK'); | ||
|
||
return true; | ||
} | ||
|
||
public function getServerVersion(): string | ||
{ | ||
return (string) pg_version($this->connection)['server']; | ||
} | ||
|
||
/** @return PgSqlConnection|resource */ | ||
public function getNativeConnection() | ||
{ | ||
return $this->connection; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Doctrine\DBAL\Driver\PgSQL; | ||
|
||
use Doctrine\DBAL\SQL\Parser\Visitor; | ||
|
||
use function count; | ||
use function implode; | ||
|
||
final class ConvertParameters implements Visitor | ||
{ | ||
/** @var list<string> */ | ||
private array $buffer = []; | ||
|
||
/** @var array<array-key, int> */ | ||
private array $parameterMap = []; | ||
|
||
public function acceptPositionalParameter(string $sql): void | ||
{ | ||
$position = count($this->parameterMap) + 1; | ||
$this->parameterMap[$position] = $position; | ||
$this->buffer[] = '$' . $position; | ||
} | ||
|
||
public function acceptNamedParameter(string $sql): void | ||
{ | ||
$position = count($this->parameterMap) + 1; | ||
$this->parameterMap[$sql] = $position; | ||
$this->buffer[] = '$' . $position; | ||
} | ||
|
||
public function acceptOther(string $sql): void | ||
{ | ||
$this->buffer[] = $sql; | ||
} | ||
|
||
public function getSQL(): string | ||
{ | ||
return implode('', $this->buffer); | ||
} | ||
|
||
/** @return array<array-key, int> */ | ||
public function getParameterMap(): array | ||
{ | ||
return $this->parameterMap; | ||
} | ||
} |
Oops, something went wrong.