@@ -5171,23 +5171,27 @@ public function convertColumnValues(ReflectedTable $table, array &$columnValues)
51715171 class GenericDB
51725172 {
51735173 private $ driver ;
5174+ private $ address ;
5175+ private $ port ;
51745176 private $ database ;
5177+ private $ username ;
5178+ private $ password ;
51755179 private $ pdo ;
51765180 private $ reflection ;
51775181 private $ definition ;
51785182 private $ conditions ;
51795183 private $ columns ;
51805184 private $ converter ;
51815185
5182- private function getDsn (string $ address , int $ port , string $ database ): string
5186+ private function getDsn (): string
51835187 {
51845188 switch ($ this ->driver ) {
51855189 case 'mysql ' :
5186- return "$ this ->driver :host= $ address;port= $ port;dbname= $ database;charset=utf8mb4 " ;
5190+ return "$ this ->driver :host= $ this -> address ;port= $ this -> port ;dbname= $ this -> database ;charset=utf8mb4 " ;
51875191 case 'pgsql ' :
5188- return "$ this ->driver :host= $ address port= $ port dbname= $ database options='--client_encoding=UTF8' " ;
5192+ return "$ this ->driver :host= $ this -> address port= $ this -> port dbname= $ this -> database options='--client_encoding=UTF8' " ;
51895193 case 'sqlsrv ' :
5190- return "$ this ->driver :Server= $ address, $ port;Database= $ database " ;
5194+ return "$ this ->driver :Server= $ this -> address , $ this -> port ;Database= $ this -> database " ;
51915195 }
51925196 }
51935197
@@ -5235,22 +5239,58 @@ private function getOptions(): array
52355239 }
52365240 }
52375241
5238- public function __construct ( string $ driver , string $ address , int $ port , string $ database , string $ username , string $ password )
5242+ private function initPdo (): bool
52395243 {
5240- $ this ->driver = $ driver ;
5241- $ this ->database = $ database ;
5242- $ dsn = $ this ->getDsn ($ address , $ port , $ database );
5243- $ options = $ this ->getOptions ();
5244- $ this ->pdo = new LazyPdo ($ dsn , $ username , $ password , $ options );
5244+ if ($ this ->pdo ) {
5245+ $ result = $ this ->pdo ->reconstruct ($ this ->getDsn (), $ this ->username , $ this ->password , $ this ->getOptions ());
5246+ } else {
5247+ $ this ->pdo = new LazyPdo ($ this ->getDsn (), $ this ->username , $ this ->password , $ this ->getOptions ());
5248+ $ result = true ;
5249+ }
52455250 $ commands = $ this ->getCommands ();
52465251 foreach ($ commands as $ command ) {
52475252 $ this ->pdo ->addInitCommand ($ command );
52485253 }
5249- $ this ->reflection = new GenericReflection ($ this ->pdo , $ driver , $ database );
5250- $ this ->definition = new GenericDefinition ($ this ->pdo , $ driver , $ database );
5251- $ this ->conditions = new ConditionsBuilder ($ driver );
5252- $ this ->columns = new ColumnsBuilder ($ driver );
5253- $ this ->converter = new DataConverter ($ driver );
5254+ $ this ->reflection = new GenericReflection ($ this ->pdo , $ this ->driver , $ this ->database );
5255+ $ this ->definition = new GenericDefinition ($ this ->pdo , $ this ->driver , $ this ->database );
5256+ $ this ->conditions = new ConditionsBuilder ($ this ->driver );
5257+ $ this ->columns = new ColumnsBuilder ($ this ->driver );
5258+ $ this ->converter = new DataConverter ($ this ->driver );
5259+ return $ result ;
5260+ }
5261+
5262+ public function __construct (string $ driver , string $ address , int $ port , string $ database , string $ username , string $ password )
5263+ {
5264+ $ this ->driver = $ driver ;
5265+ $ this ->address = $ address ;
5266+ $ this ->port = $ port ;
5267+ $ this ->database = $ database ;
5268+ $ this ->username = $ username ;
5269+ $ this ->password = $ password ;
5270+ $ this ->initPdo ();
5271+ }
5272+
5273+ public function reconstruct (string $ driver , string $ address , int $ port , string $ database , string $ username , string $ password ): bool
5274+ {
5275+ if ($ driver ) {
5276+ $ this ->driver = $ driver ;
5277+ }
5278+ if ($ address ) {
5279+ $ this ->address = $ address ;
5280+ }
5281+ if ($ port ) {
5282+ $ this ->port = $ port ;
5283+ }
5284+ if ($ database ) {
5285+ $ this ->database = $ database ;
5286+ }
5287+ if ($ username ) {
5288+ $ this ->username = $ username ;
5289+ }
5290+ if ($ password ) {
5291+ $ this ->password = $ password ;
5292+ }
5293+ return $ this ->initPdo ();
52545294 }
52555295
52565296 public function pdo (): LazyPdo
@@ -6036,15 +6076,18 @@ private function pdo()
60366076 return $ this ->pdo ;
60376077 }
60386078
6039- public function reauthenticate ( /*?string*/ $ user , /*?string*/ $ password ): bool
6079+ public function reconstruct ( string $ dsn , /*?string*/ $ user = null , /*?string*/ $ password = null , array $ options = array () ): bool
60406080 {
6081+ $ this ->dsn = $ dsn ;
60416082 $ this ->user = $ user ;
60426083 $ this ->password = $ password ;
6084+ $ this ->options = $ options ;
6085+ $ this ->commands = array ();
60436086 if ($ this ->pdo ) {
60446087 $ this ->pdo = null ;
6045- return false ;
6088+ return true ;
60466089 }
6047- return true ;
6090+ return false ;
60486091 }
60496092
60506093 public function inTransaction (): bool
@@ -7739,7 +7782,7 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
77397782 }
77407783}
77417784
7742- // file: src/Tqdev/PhpCrudApi/Middleware/ReAuthMiddleware .php
7785+ // file: src/Tqdev/PhpCrudApi/Middleware/ReconnectMiddleware .php
77437786namespace Tqdev \PhpCrudApi \Middleware {
77447787
77457788 use Psr \Http \Message \ResponseInterface ;
@@ -7751,7 +7794,7 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
77517794 use Tqdev \PhpCrudApi \Middleware \Base \Middleware ;
77527795 use Tqdev \PhpCrudApi \Middleware \Router \Router ;
77537796
7754- class ReAuthMiddleware extends Middleware
7797+ class ReconnectMiddleware extends Middleware
77557798 {
77567799 private $ reflection ;
77577800 private $ db ;
@@ -7763,6 +7806,42 @@ public function __construct(Router $router, Responder $responder, array $propert
77637806 $ this ->db = $ db ;
77647807 }
77657808
7809+ private function getDriver (): string
7810+ {
7811+ $ driverHandler = $ this ->getProperty ('driverHandler ' , '' );
7812+ if ($ driverHandler ) {
7813+ return call_user_func ($ driverHandler );
7814+ }
7815+ return '' ;
7816+ }
7817+
7818+ private function getAddress (): string
7819+ {
7820+ $ addressHandler = $ this ->getProperty ('addressHandler ' , '' );
7821+ if ($ addressHandler ) {
7822+ return call_user_func ($ addressHandler );
7823+ }
7824+ return '' ;
7825+ }
7826+
7827+ private function getPort (): int
7828+ {
7829+ $ portHandler = $ this ->getProperty ('portHandler ' , '' );
7830+ if ($ portHandler ) {
7831+ return call_user_func ($ portHandler );
7832+ }
7833+ return 0 ;
7834+ }
7835+
7836+ private function getDatabase (): string
7837+ {
7838+ $ databaseHandler = $ this ->getProperty ('databaseHandler ' , '' );
7839+ if ($ databaseHandler ) {
7840+ return call_user_func ($ databaseHandler );
7841+ }
7842+ return '' ;
7843+ }
7844+
77667845 private function getUsername (): string
77677846 {
77687847 $ usernameHandler = $ this ->getProperty ('usernameHandler ' , '' );
@@ -7783,10 +7862,14 @@ private function getPassword(): string
77837862
77847863 public function process (ServerRequestInterface $ request , RequestHandlerInterface $ next ): ResponseInterface
77857864 {
7865+ $ driver = $ this ->getDriver ();
7866+ $ address = $ this ->getAddress ();
7867+ $ port = $ this ->getPort ();
7868+ $ database = $ this ->getDatabase ();
77867869 $ username = $ this ->getUsername ();
77877870 $ password = $ this ->getPassword ();
7788- if ($ username && $ password ) {
7789- $ this ->db ->pdo ()-> reauthenticate ( $ username , $ password );
7871+ if ($ driver || $ address || $ port || $ database || $ username || $ password ) {
7872+ $ this ->db ->reconstruct ( $ driver , $ address , $ port , $ database , $ username , $ password );
77907873 }
77917874 return $ next ->handle ($ request );
77927875 }
@@ -9577,7 +9660,7 @@ private function setHabtmValues(ReflectedTable $t1, ReflectedTable $t2, array &$
95779660 use Tqdev \PhpCrudApi \Middleware \IpAddressMiddleware ;
95789661 use Tqdev \PhpCrudApi \Middleware \JoinLimitsMiddleware ;
95799662 use Tqdev \PhpCrudApi \Middleware \JwtAuthMiddleware ;
9580- use Tqdev \PhpCrudApi \Middleware \ReAuthMiddleware ;
9663+ use Tqdev \PhpCrudApi \Middleware \ReconnectMiddleware ;
95819664 use Tqdev \PhpCrudApi \Middleware \MultiTenancyMiddleware ;
95829665 use Tqdev \PhpCrudApi \Middleware \PageLimitsMiddleware ;
95839666 use Tqdev \PhpCrudApi \Middleware \Router \SimpleRouter ;
@@ -9627,8 +9710,8 @@ public function __construct(Config $config)
96279710 case 'dbAuth ' :
96289711 new DbAuthMiddleware ($ router , $ responder , $ properties , $ reflection , $ db );
96299712 break ;
9630- case 'reAuth ' :
9631- new ReAuthMiddleware ($ router , $ responder , $ properties , $ reflection , $ db );
9713+ case 'reconnect ' :
9714+ new ReconnectMiddleware ($ router , $ responder , $ properties , $ reflection , $ db );
96329715 break ;
96339716 case 'validation ' :
96349717 new ValidationMiddleware ($ router , $ responder , $ properties , $ reflection );
0 commit comments