@@ -5226,7 +5226,7 @@ public function __construct(string $driver, string $address, int $port, string $
5226
5226
$ this ->database = $ database ;
5227
5227
$ dsn = $ this ->getDsn ($ address , $ port , $ database );
5228
5228
$ options = $ this ->getOptions ();
5229
- $ this ->pdo = new \ PDO ($ dsn , $ username , $ password , $ options );
5229
+ $ this ->pdo = new LazyPdo ($ dsn , $ username , $ password , $ options );
5230
5230
$ commands = $ this ->getCommands ();
5231
5231
foreach ($ commands as $ command ) {
5232
5232
$ this ->pdo ->query ($ command );
@@ -5420,6 +5420,7 @@ private function query(string $sql, array $parameters): \PDOStatement
5420
5420
5421
5421
use Tqdev \PhpCrudApi \Column \Reflection \ReflectedColumn ;
5422
5422
use Tqdev \PhpCrudApi \Column \Reflection \ReflectedTable ;
5423
+ use Tqdev \PhpCrudApi \Database \LazyPdo ;
5423
5424
5424
5425
class GenericDefinition
5425
5426
{
@@ -5429,7 +5430,7 @@ class GenericDefinition
5429
5430
private $ typeConverter ;
5430
5431
private $ reflection ;
5431
5432
5432
- public function __construct (\ PDO $ pdo , string $ driver , string $ database )
5433
+ public function __construct (LazyPdo $ pdo , string $ driver , string $ database )
5433
5434
{
5434
5435
$ this ->pdo = $ pdo ;
5435
5436
$ this ->driver = $ driver ;
@@ -5842,14 +5843,16 @@ private function query(string $sql): bool
5842
5843
// file: src/Tqdev/PhpCrudApi/Database/GenericReflection.php
5843
5844
namespace Tqdev \PhpCrudApi \Database {
5844
5845
5846
+ use Tqdev \PhpCrudApi \Database \LazyPdo ;
5847
+
5845
5848
class GenericReflection
5846
5849
{
5847
5850
private $ pdo ;
5848
5851
private $ driver ;
5849
5852
private $ database ;
5850
5853
private $ typeConverter ;
5851
5854
5852
- public function __construct (\ PDO $ pdo , string $ driver , string $ database )
5855
+ public function __construct (LazyPdo $ pdo , string $ driver , string $ database )
5853
5856
{
5854
5857
$ this ->pdo = $ pdo ;
5855
5858
$ this ->driver = $ driver ;
@@ -5979,6 +5982,125 @@ private function query(string $sql, array $parameters): array
5979
5982
}
5980
5983
}
5981
5984
5985
+ // file: src/Tqdev/PhpCrudApi/Database/LazyPdo.php
5986
+ namespace Tqdev \PhpCrudApi \Database {
5987
+
5988
+ class LazyPdo extends \PDO
5989
+ {
5990
+ private $ dsn ;
5991
+ private $ user ;
5992
+ private $ password ;
5993
+ private $ options = array ();
5994
+
5995
+ private $ pdo = null ;
5996
+
5997
+ public function __construct (string $ dsn , /*?string*/ $ user = null , /*?string*/ $ password = null , array $ options = array ())
5998
+ {
5999
+ $ this ->dsn = $ dsn ;
6000
+ $ this ->user = $ user ;
6001
+ $ this ->password = $ password ;
6002
+ $ this ->options = $ options ;
6003
+ // explicitly NOT calling super::__construct
6004
+ }
6005
+
6006
+ private function pdo ()
6007
+ {
6008
+ if (!$ this ->pdo ) {
6009
+ $ this ->pdo = new \PDO ($ this ->dsn , $ this ->user , $ this ->password , $ this ->options );
6010
+ }
6011
+ return $ this ->pdo ;
6012
+ }
6013
+
6014
+ public function setUser (/*?string*/ $ user ): bool
6015
+ {
6016
+ if ($ this ->pdo ) {
6017
+ return false ;
6018
+ }
6019
+ $ this ->user = $ user ;
6020
+ return true ;
6021
+ }
6022
+
6023
+ public function setPassword (/*?string*/ $ password ): bool
6024
+ {
6025
+ if ($ this ->pdo ) {
6026
+ return false ;
6027
+ }
6028
+ $ this ->password = $ password ;
6029
+ return true ;
6030
+ }
6031
+
6032
+ public function inTransaction (): bool
6033
+ {
6034
+ // Do not call parent method if there is no pdo object
6035
+ return $ this ->pdo && parent ::inTransaction ();
6036
+ }
6037
+
6038
+ public function setAttribute ($ attribute , $ value ): bool
6039
+ {
6040
+ if ($ this ->pdo ) {
6041
+ return $ this ->pdo ()->setAttribute ($ attribute , $ value );
6042
+ }
6043
+ $ this ->options [$ attribute ] = $ value ;
6044
+ return true ;
6045
+ }
6046
+
6047
+ public function getAttribute ($ attribute ): mixed
6048
+ {
6049
+ return $ this ->pdo ()->getAttribute ($ attribute );
6050
+ }
6051
+
6052
+ public function beginTransaction (): bool
6053
+ {
6054
+ return $ this ->pdo ()->beginTransaction ();
6055
+ }
6056
+
6057
+ public function commit (): bool
6058
+ {
6059
+ return $ this ->pdo ()->commit ();
6060
+ }
6061
+
6062
+ public function rollBack (): bool
6063
+ {
6064
+ return $ this ->pdo ()->rollBack ();
6065
+ }
6066
+
6067
+ public function errorCode (): mixed
6068
+ {
6069
+ return $ this ->pdo ()->errorCode ();
6070
+ }
6071
+
6072
+ public function errorInfo (): array
6073
+ {
6074
+ return $ this ->pdo ()->errorInfo ();
6075
+ }
6076
+
6077
+ public function exec ($ query ): int
6078
+ {
6079
+ return $ this ->pdo ()->exec ($ query );
6080
+ }
6081
+
6082
+ public function prepare ($ statement , $ options = array ())
6083
+ {
6084
+ return $ this ->pdo ()->prepare ($ statement , $ options );
6085
+ }
6086
+
6087
+ public function quote ($ string , $ parameter_type = null ): string
6088
+ {
6089
+ return $ this ->pdo ()->quote ($ string , $ parameter_type );
6090
+ }
6091
+
6092
+ public function lastInsertId (/* ?string */ $ name = null ): string
6093
+ {
6094
+ return $ this ->pdo ()->lastInsertId ($ name );
6095
+ }
6096
+
6097
+ public function query (string $ statement ): \PDOStatement
6098
+ {
6099
+ return call_user_func_array (array ($ this ->pdo (), __FUNCTION__ ), func_get_args ());
6100
+ }
6101
+ }
6102
+ }
6103
+
5982
6104
// file: src/Tqdev/PhpCrudApi/Database/TypeConverter.php
5983
6105
namespace Tqdev \PhpCrudApi \Database {
5984
6106
0 commit comments