Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correct behaviour to match PHP 8.4 PDO returned types for connect and __construct for the internal pdo object #231

Open
wants to merge 6 commits into
base: 6.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 36 additions & 3 deletions src/ExtendedPdo.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
*/
class ExtendedPdo extends AbstractExtendedPdo
{
public const string CONNECT_IMMEDIATELY = 'auraSqlImmediate';
public const string DRIVER_SPECIFIC = 'auraSqlDriverSpecific';

/**
*
* Constructor arguments for instantiating the PDO connection.
Expand All @@ -30,6 +33,14 @@ class ExtendedPdo extends AbstractExtendedPdo
*/
protected array $args = [];


/**
*
* Flag for how to construct the PDO object
*
* @var bool
*/
protected bool $driverSpecific = false;
/**
*
* Constructor.
Expand Down Expand Up @@ -64,6 +75,12 @@ public function __construct(
$options[PDO::ATTR_ERRMODE] = PDO::ERRMODE_EXCEPTION;
}

// check option for driver specific construct and set flag for lazy loading later
if (isset($options[static::DRIVER_SPECIFIC])) {
$this->driverSpecific = (bool) $options[static::DRIVER_SPECIFIC];
unset($options[static::DRIVER_SPECIFIC]);
}

// retain the arguments for later
$this->args = [
$dsn,
Expand All @@ -83,17 +100,29 @@ public function __construct(

// set quotes for identifier names
$this->setQuoteName($parts[0]);

// create a connection immediately
if (isset($options[static::CONNECT_IMMEDIATELY])) {
if($options[static::CONNECT_IMMEDIATELY]) {
unset($options[static::CONNECT_IMMEDIATELY]);
$this->establishConnection();
} else {
unset($options[static::CONNECT_IMMEDIATELY]);
}
}
}

public static function connect(
string $dsn,
?string $username = null,
?string $password = null,
?array $options = [],
?array $options = null,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can't we actually use [] instead of null, this will also remove the usage of $options ??= []; right ?

array $queries = [],
?ProfilerInterface $profiler = null
): static {
return new static($dsn, $username, $password, $options ?? [], $queries, $profiler);
$options ??= [];
$options[static::DRIVER_SPECIFIC] = true;
return new static($dsn, $username, $password, $options, $queries, $profiler);
}

/**
Expand All @@ -111,7 +140,11 @@ public function establishConnection(): void
// connect
$this->profiler->start(__FUNCTION__);
list($dsn, $username, $password, $options, $queries) = $this->args;
$this->pdo = PDO::connect($dsn, $username, $password, $options);
if ($this->driverSpecific) {
$this->pdo = PDO::connect($dsn, $username, $password, $options);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought we don't need the connect method itself as this is implemented internally in the PDO.

Copy link
Contributor Author

@srjlewis srjlewis Nov 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was looking at the final release of the new PDO

PDO::__construct() always returns PDO and PDO::connect() returns Pdo\{driver} or PDO depending on available drivers

So if you use ExtendedPdo::getPdo() you would get the correct typed object.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That behaviour of PDO is expected. But in our case we are not calling the parent construct.

} else {
$this->pdo = new PDO($dsn, $username, $password, $options);
}
$this->profiler->finish();

// connection-time queries
Expand Down
5 changes: 5 additions & 0 deletions tests/ExtendedConnectPdoTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,9 @@ protected function newPdo()
{
return ExtendedPdo::connect('sqlite::memory:');
}

public function testPdoType()
{
$this->assertInstanceOf(Pdo\Sqlite::class, $this->pdo->getPdo());
}
}
5 changes: 5 additions & 0 deletions tests/ExtendedPdoTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@ protected function insert(array $data)
$this->pdo->perform($stm, $data);
}

public function testPdoType()
{
$this->assertNotInstanceOf(Pdo\Sqlite::class, $this->pdo->getPdo());
}

public function testCall()
{
if (defined('HHVM_VERSION')) {
Expand Down
Loading