@@ -5583,24 +5583,36 @@ private function getDsn(): string
55835583
55845584 private function getCommands (): array
55855585 {
5586+ $ initcommands = [] ;
55865587 switch ($ this ->driver ) {
55875588 case 'mysql ' :
5588- return [
5589+ $ initcommands = [
55895590 'SET SESSION sql_warnings=1; ' ,
55905591 'SET NAMES utf8mb4; ' ,
55915592 'SET SESSION sql_mode = "ANSI,TRADITIONAL"; ' ,
55925593 ];
5594+ break ;
55935595 case 'pgsql ' :
5594- return [
5596+ $ initcommands = [
55955597 "SET NAMES 'UTF8'; " ,
55965598 ];
5599+ break ;
55975600 case 'sqlsrv ' :
5598- return [];
5601+ $ initcommands = [];
5602+ break ;
55995603 case 'sqlite ' :
5600- return [
5604+ $ initcommands = [
56015605 'PRAGMA foreign_keys = on; ' ,
56025606 ];
5607+ break ;
56035608 }
5609+
5610+ if ($ this ->initcommand !='' ) {
5611+ $ initcommands [] = $ this ->initcommand ;
5612+ }
5613+
5614+ return $ initcommands ;
5615+
56045616 }
56055617
56065618 private function getOptions (): array
@@ -5646,21 +5658,22 @@ private function initPdo(): bool
56465658 $ this ->converter = new DataConverter ($ this ->driver );
56475659 return $ result ;
56485660 }
5649-
5650- public function __construct (string $ driver , string $ address , int $ port , string $ database , array $ tables , array $ mapping , string $ username , string $ password )
5661+
5662+ public function __construct (string $ driver , string $ address , int $ port , string $ database , array $ tables , string $ initcommand , array $ mapping , string $ username , string $ password )
56515663 {
56525664 $ this ->driver = $ driver ;
56535665 $ this ->address = $ address ;
56545666 $ this ->port = $ port ;
56555667 $ this ->database = $ database ;
56565668 $ this ->tables = $ tables ;
5669+ $ this ->initcommand = $ initcommand ;
56575670 $ this ->mapping = $ mapping ;
56585671 $ this ->username = $ username ;
56595672 $ this ->password = $ password ;
56605673 $ this ->initPdo ();
56615674 }
56625675
5663- public function reconstruct (string $ driver , string $ address , int $ port , string $ database , array $ tables , array $ mapping , string $ username , string $ password ): bool
5676+ public function reconstruct (string $ driver , string $ address , int $ port , string $ database , array $ tables , string $ initcommand , array $ mapping , string $ username , string $ password ): bool
56645677 {
56655678 if ($ driver ) {
56665679 $ this ->driver = $ driver ;
@@ -5677,6 +5690,9 @@ public function reconstruct(string $driver, string $address, int $port, string $
56775690 if ($ tables ) {
56785691 $ this ->tables = $ tables ;
56795692 }
5693+ if ($ initcommand ) {
5694+ $ this ->initcommand = $ initcommand ;
5695+ }
56805696 if ($ mapping ) {
56815697 $ this ->mapping = $ mapping ;
56825698 }
@@ -7388,6 +7404,7 @@ public function __construct(string $basePath, Responder $responder, Cache $cache
73887404 $ this ->routes = $ this ->loadPathTree ();
73897405 $ this ->routeHandlers = [];
73907406 $ this ->middlewares = array ();
7407+
73917408 }
73927409
73937410 private function detectBasePath (ServerRequestInterface $ request ): string
@@ -8875,11 +8892,13 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
88758892 $ port = $ this ->getPort ();
88768893 $ database = $ this ->getDatabase ();
88778894 $ tables = $ this ->getTables ();
8895+ $ initcommand = $ this ->getInitcommand ();
88788896 $ mapping = $ this ->getMapping ();
88798897 $ username = $ this ->getUsername ();
88808898 $ password = $ this ->getPassword ();
8899+
88818900 if ($ driver || $ address || $ port || $ database || $ tables || $ mapping || $ username || $ password ) {
8882- $ this ->db ->reconstruct ($ driver , $ address , $ port , $ database , $ tables , $ mapping , $ username , $ password );
8901+ $ this ->db ->reconstruct ($ driver , $ address , $ port , $ database , $ tables ,$ initcommand , $ mapping , $ username , $ password );
88838902 }
88848903 return $ next ->handle ($ request );
88858904 }
@@ -11530,6 +11549,7 @@ public function __construct(Config $config)
1153011549 $ config ->getPort (),
1153111550 $ config ->getDatabase (),
1153211551 $ config ->getTables (),
11552+ $ config ->getInitcommand (),
1153311553 $ config ->getMapping (),
1153411554 $ config ->getUsername (),
1153511555 $ config ->getPassword ()
@@ -11715,6 +11735,7 @@ class Config
1171511735 'password ' => '' ,
1171611736 'database ' => '' ,
1171711737 'tables ' => '' ,
11738+ 'initcommand ' =>'' ,
1171811739 'mapping ' => '' ,
1171911740 'middlewares ' => 'cors,errors ' ,
1172011741 'controllers ' => 'records,geojson,openapi,status ' ,
@@ -11864,6 +11885,10 @@ public function getTables(): array
1186411885 {
1186511886 return array_filter (array_map ('trim ' , explode (', ' , $ this ->values ['tables ' ])));
1186611887 }
11888+ public function getInitcommand (): string
11889+ {
11890+ return $ this ->values ['initcommand ' ];
11891+ }
1186711892
1186811893 public function getMapping (): array
1186911894 {
@@ -12223,6 +12248,8 @@ public static function toString(ResponseInterface $response): string
1222312248 'password ' => 'php-crud-api ' ,
1222412249 'database ' => 'php-crud-api ' ,
1222512250 // 'debug' => false
12251+ //'initcommand'=>'set search_path to sales,public; ' // to allow other schemas in pgsql
12252+
1222612253 ]);
1222712254 $ request = RequestFactory::fromGlobals ();
1222812255 $ api = new Api ($ config );
0 commit comments