1212use JetBrains \PhpStorm \Language ;
1313use Nette \Utils \Arrays ;
1414use Nette \Utils \DateTime ;
15- use PDO ;
1615use PDOException ;
1716
1817
@@ -27,9 +26,9 @@ class Connection
2726 /** @var array<callable(self, Result|DriverException): void> Occurs after query is executed */
2827 public array $ onQuery = [];
2928 private Drivers \Driver $ driver ;
30- private Drivers \Engine $ engine ;
29+ private ?Drivers \Connection $ connection = null ;
30+ private ?Drivers \Engine $ engine ;
3131 private ?SqlPreprocessor $ preprocessor ;
32- private ?PDO $ pdo = null ;
3332
3433 /** @var callable(array, Result): array */
3534 private $ rowNormalizer = [Helpers::class, 'normalizeRow ' ];
@@ -39,10 +38,10 @@ class Connection
3938
4039 public function __construct (
4140 private readonly string $ dsn ,
42- private readonly ?string $ user = null ,
41+ ?string $ user = null ,
4342 #[\SensitiveParameter]
44- private readonly ?string $ password = null ,
45- private array $ options = [],
43+ ?string $ password = null ,
44+ array $ options = [],
4645 ) {
4746 if (($ options ['newDateTime ' ] ?? null ) === false ) {
4847 $ this ->rowNormalizer = fn ($ row , $ resultSet ) => Helpers::normalizeRow ($ row , $ resultSet , DateTime::class);
@@ -59,13 +58,13 @@ public function __construct(
5958
6059 public function connect (): void
6160 {
62- if ($ this ->pdo ) {
61+ if ($ this ->connection ) {
6362 return ;
6463 }
6564
6665 try {
67- $ this ->pdo = $ this ->driver ->connect ();
68- $ this ->engine = $ this ->driver ->createDatabaseEngine ($ this );
66+ $ this ->connection = $ this ->driver ->connect ();
67+ $ this ->engine = $ this ->driver ->createDatabaseEngine ($ this -> connection );
6968 } catch (PDOException $ e ) {
7069 throw ConnectionException::from ($ e );
7170 }
@@ -83,7 +82,7 @@ public function reconnect(): void
8382
8483 public function disconnect (): void
8584 {
86- $ this ->pdo = null ;
85+ $ this ->connection = null ;
8786 }
8887
8988
@@ -93,19 +92,24 @@ public function getDsn(): string
9392 }
9493
9594
96- public function getPdo (): PDO
95+ public function getPdo (): \ PDO
9796 {
98- $ this ->connect ();
99- return $ this ->pdo ;
97+ return $ this ->getConnectionDriver ()->getNativeConnection ();
10098 }
10199
102100
103- /** @deprecated use getDriver() */
104- public function getSupplementalDriver (): Drivers \Engine
101+ public function getConnectionDriver (): Drivers \Connection
105102 {
106- trigger_error (__METHOD__ . '() is deprecated, use getDriver() ' , E_USER_DEPRECATED );
107103 $ this ->connect ();
108- return $ this ->engine ;
104+ return $ this ->connection ;
105+ }
106+
107+
108+ /** @deprecated use getConnectionDriver() */
109+ public function getSupplementalDriver (): Drivers \Connection
110+ {
111+ trigger_error (__METHOD__ . '() is deprecated, use getConnectionDriver() ' , E_USER_DEPRECATED );
112+ return $ this ->getConnectionDriver ();
109113 }
110114
111115
@@ -132,21 +136,16 @@ public function setRowNormalizer(?callable $normalizer): static
132136 public function getInsertId (?string $ sequence = null ): string
133137 {
134138 try {
135- $ res = $ this ->getPdo ()->lastInsertId ($ sequence );
136- return $ res === false ? '0 ' : $ res ;
139+ return $ this ->getConnectionDriver ()->getInsertId ($ sequence );
137140 } catch (PDOException $ e ) {
138141 throw $ this ->engine ->convertException ($ e );
139142 }
140143 }
141144
142145
143- public function quote (string $ string, int $ type = PDO :: PARAM_STR ): string
146+ public function quote (string $ string ): string
144147 {
145- try {
146- return $ this ->getPdo ()->quote ($ string , $ type );
147- } catch (PDOException $ e ) {
148- throw DriverException::from ($ e );
149- }
148+ return $ this ->getConnectionDriver ()->quote ($ string );
150149 }
151150
152151
0 commit comments