diff --git a/src/ExtendedPdo.php b/src/ExtendedPdo.php index bb6ff65..72f49b4 100644 --- a/src/ExtendedPdo.php +++ b/src/ExtendedPdo.php @@ -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. @@ -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. @@ -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, @@ -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, 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); } /** @@ -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); + } else { + $this->pdo = new PDO($dsn, $username, $password, $options); + } $this->profiler->finish(); // connection-time queries diff --git a/tests/ExtendedConnectPdoTest.php b/tests/ExtendedConnectPdoTest.php index 25d5269..61bdcb7 100644 --- a/tests/ExtendedConnectPdoTest.php +++ b/tests/ExtendedConnectPdoTest.php @@ -8,4 +8,9 @@ protected function newPdo() { return ExtendedPdo::connect('sqlite::memory:'); } + + public function testPdoType() + { + $this->assertInstanceOf(Pdo\Sqlite::class, $this->pdo->getPdo()); + } } \ No newline at end of file diff --git a/tests/ExtendedPdoTest.php b/tests/ExtendedPdoTest.php index c04a43b..d48c06e 100644 --- a/tests/ExtendedPdoTest.php +++ b/tests/ExtendedPdoTest.php @@ -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')) {