@@ -28,7 +28,7 @@ class Connection
2828 public array $ onQuery = [];
2929 private Drivers \Driver $ driver ;
3030 private Drivers \Engine $ engine ;
31- private SqlPreprocessor $ preprocessor ;
31+ private ? SqlPreprocessor $ preprocessor ;
3232 private ?PDO $ pdo = null ;
3333
3434 /** @var callable(array, Result): array */
@@ -39,7 +39,6 @@ class Connection
3939
4040 public function __construct (
4141 private readonly string $ dsn ,
42- #[\SensitiveParameter]
4342 private readonly ?string $ user = null ,
4443 #[\SensitiveParameter]
4544 private readonly ?string $ password = null ,
@@ -48,7 +47,11 @@ public function __construct(
4847 if (($ options ['newDateTime ' ] ?? null ) === false ) {
4948 $ this ->rowNormalizer = fn ($ row , $ resultSet ) => Helpers::normalizeRow ($ row , $ resultSet , DateTime::class);
5049 }
51- if (empty ($ options ['lazy ' ])) {
50+ $ lazy = $ options ['lazy ' ] ?? false ;
51+ unset($ options ['newDateTime ' ], $ options ['lazy ' ]);
52+
53+ $ this ->driver = (new Factory )->createDriverFromDsn ($ dsn , $ user , $ password , $ options );
54+ if (!$ lazy ) {
5255 $ this ->connect ();
5356 }
5457 }
@@ -66,9 +69,7 @@ public function connect(): void
6669 throw ConnectionException::from ($ e );
6770 }
6871
69- $ this ->driver = (new Factory )->createDriverFromDsn ($ this ->dsn , $ this ->user , $ this ->password , $ this ->options );
7072 $ this ->engine = $ this ->driver ->createDatabaseEngine ();
71- $ this ->preprocessor = new SqlPreprocessor ($ this );
7273 $ this ->engine ->initialize ($ this , $ this ->options );
7374 Arrays::invoke ($ this ->onConnect , $ this );
7475 }
@@ -241,6 +242,7 @@ public function queryArgs(string $sql, array $params): Result
241242 public function preprocess (string $ sql , ...$ params ): array
242243 {
243244 $ this ->connect ();
245+ $ this ->preprocessor ??= new SqlPreprocessor ($ this );
244246 return $ params
245247 ? $ this ->preprocessor ->process (func_get_args ())
246248 : [$ sql , []];
0 commit comments