@@ -25,9 +25,8 @@ class Connection
2525
2626 /** @var array<callable(self, ResultSet|DriverException): void> Occurs after query is executed */
2727 public array $ onQuery = [];
28- private Driver $ driver ;
28+ private ? Driver $ driver = null ;
2929 private SqlPreprocessor $ preprocessor ;
30- private ?PDO $ pdo = null ;
3130
3231 /** @var callable(array, ResultSet): array */
3332 private $ rowNormalizer ;
@@ -53,23 +52,21 @@ public function __construct(
5352
5453 public function connect (): void
5554 {
56- if ($ this ->pdo ) {
55+ if ($ this ->driver ) {
5756 return ;
5857 }
5958
60- try {
61- $ this ->pdo = new PDO ($ this ->dsn , $ this ->user , $ this ->password , $ this ->options );
62- $ this ->pdo ->setAttribute (PDO ::ATTR_ERRMODE , PDO ::ERRMODE_EXCEPTION );
63- } catch (PDOException $ e ) {
64- throw ConnectionException::from ($ e );
65- }
66-
59+ $ dsn = explode (': ' , $ this ->dsn )[0 ];
6760 $ class = empty ($ this ->options ['driverClass ' ])
68- ? 'Nette\Database\Drivers \\' . ucfirst (str_replace ('sql ' , 'Sql ' , $ this -> pdo -> getAttribute ( PDO :: ATTR_DRIVER_NAME ) )) . 'Driver '
61+ ? 'Nette\Database\Drivers \\' . ucfirst (str_replace ('sql ' , 'Sql ' , $ dsn )) . 'Driver '
6962 : $ this ->options ['driverClass ' ];
63+ if (!class_exists ($ class )) {
64+ throw new ConnectionException ("Invalid data source ' $ dsn'. " );
65+ }
66+
7067 $ this ->driver = new $ class ;
68+ $ this ->driver ->connect ($ this ->dsn , $ this ->user , $ this ->password , $ this ->options );
7169 $ this ->preprocessor = new SqlPreprocessor ($ this );
72- $ this ->driver ->initialize ($ this , $ this ->options );
7370 Arrays::invoke ($ this ->onConnect , $ this );
7471 }
7572
@@ -83,7 +80,7 @@ public function reconnect(): void
8380
8481 public function disconnect (): void
8582 {
86- $ this ->pdo = null ;
83+ $ this ->driver = null ;
8784 }
8885
8986
@@ -93,10 +90,11 @@ public function getDsn(): string
9390 }
9491
9592
93+ /** deprecated use getDriver()->getPdo() */
9694 public function getPdo (): PDO
9795 {
9896 $ this ->connect ();
99- return $ this ->pdo ;
97+ return $ this ->driver -> getPdo () ;
10098 }
10199
102100
0 commit comments