@@ -5583,24 +5583,36 @@ private function getDsn(): string
5583
5583
5584
5584
private function getCommands (): array
5585
5585
{
5586
+ $ initcommands = [] ;
5586
5587
switch ($ this ->driver ) {
5587
5588
case 'mysql ' :
5588
- return [
5589
+ $ initcommands = [
5589
5590
'SET SESSION sql_warnings=1; ' ,
5590
5591
'SET NAMES utf8mb4; ' ,
5591
5592
'SET SESSION sql_mode = "ANSI,TRADITIONAL"; ' ,
5592
5593
];
5594
+ break ;
5593
5595
case 'pgsql ' :
5594
- return [
5596
+ $ initcommands = [
5595
5597
"SET NAMES 'UTF8'; " ,
5596
5598
];
5599
+ break ;
5597
5600
case 'sqlsrv ' :
5598
- return [];
5601
+ $ initcommands = [];
5602
+ break ;
5599
5603
case 'sqlite ' :
5600
- return [
5604
+ $ initcommands = [
5601
5605
'PRAGMA foreign_keys = on; ' ,
5602
5606
];
5607
+ break ;
5603
5608
}
5609
+
5610
+ if ($ this ->initcommand !='' ) {
5611
+ $ initcommands [] = $ this ->initcommand ;
5612
+ }
5613
+
5614
+ return $ initcommands ;
5615
+
5604
5616
}
5605
5617
5606
5618
private function getOptions (): array
@@ -5646,21 +5658,22 @@ private function initPdo(): bool
5646
5658
$ this ->converter = new DataConverter ($ this ->driver );
5647
5659
return $ result ;
5648
5660
}
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 )
5651
5663
{
5652
5664
$ this ->driver = $ driver ;
5653
5665
$ this ->address = $ address ;
5654
5666
$ this ->port = $ port ;
5655
5667
$ this ->database = $ database ;
5656
5668
$ this ->tables = $ tables ;
5669
+ $ this ->initcommand = $ initcommand ;
5657
5670
$ this ->mapping = $ mapping ;
5658
5671
$ this ->username = $ username ;
5659
5672
$ this ->password = $ password ;
5660
5673
$ this ->initPdo ();
5661
5674
}
5662
5675
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
5664
5677
{
5665
5678
if ($ driver ) {
5666
5679
$ this ->driver = $ driver ;
@@ -5677,6 +5690,9 @@ public function reconstruct(string $driver, string $address, int $port, string $
5677
5690
if ($ tables ) {
5678
5691
$ this ->tables = $ tables ;
5679
5692
}
5693
+ if ($ initcommand ) {
5694
+ $ this ->initcommand = $ initcommand ;
5695
+ }
5680
5696
if ($ mapping ) {
5681
5697
$ this ->mapping = $ mapping ;
5682
5698
}
@@ -7388,6 +7404,7 @@ public function __construct(string $basePath, Responder $responder, Cache $cache
7388
7404
$ this ->routes = $ this ->loadPathTree ();
7389
7405
$ this ->routeHandlers = [];
7390
7406
$ this ->middlewares = array ();
7407
+
7391
7408
}
7392
7409
7393
7410
private function detectBasePath (ServerRequestInterface $ request ): string
@@ -8875,11 +8892,13 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
8875
8892
$ port = $ this ->getPort ();
8876
8893
$ database = $ this ->getDatabase ();
8877
8894
$ tables = $ this ->getTables ();
8895
+ $ initcommand = $ this ->getInitcommand ();
8878
8896
$ mapping = $ this ->getMapping ();
8879
8897
$ username = $ this ->getUsername ();
8880
8898
$ password = $ this ->getPassword ();
8899
+
8881
8900
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 );
8883
8902
}
8884
8903
return $ next ->handle ($ request );
8885
8904
}
@@ -11530,6 +11549,7 @@ public function __construct(Config $config)
11530
11549
$ config ->getPort (),
11531
11550
$ config ->getDatabase (),
11532
11551
$ config ->getTables (),
11552
+ $ config ->getInitcommand (),
11533
11553
$ config ->getMapping (),
11534
11554
$ config ->getUsername (),
11535
11555
$ config ->getPassword ()
@@ -11715,6 +11735,7 @@ class Config
11715
11735
'password ' => '' ,
11716
11736
'database ' => '' ,
11717
11737
'tables ' => '' ,
11738
+ 'initcommand ' =>'' ,
11718
11739
'mapping ' => '' ,
11719
11740
'middlewares ' => 'cors,errors ' ,
11720
11741
'controllers ' => 'records,geojson,openapi,status ' ,
@@ -11864,6 +11885,10 @@ public function getTables(): array
11864
11885
{
11865
11886
return array_filter (array_map ('trim ' , explode (', ' , $ this ->values ['tables ' ])));
11866
11887
}
11888
+ public function getInitcommand (): string
11889
+ {
11890
+ return $ this ->values ['initcommand ' ];
11891
+ }
11867
11892
11868
11893
public function getMapping (): array
11869
11894
{
@@ -12223,6 +12248,8 @@ public static function toString(ResponseInterface $response): string
12223
12248
'password ' => 'php-crud-api ' ,
12224
12249
'database ' => 'php-crud-api ' ,
12225
12250
// 'debug' => false
12251
+ //'initcommand'=>'set search_path to sales,public; ' // to allow other schemas in pgsql
12252
+
12226
12253
]);
12227
12254
$ request = RequestFactory::fromGlobals ();
12228
12255
$ api = new Api ($ config );
0 commit comments