@@ -3850,6 +3850,11 @@ public function isInteger(): bool
3850
3850
return in_array ($ this ->type , ['integer ' , 'bigint ' , 'smallint ' , 'tinyint ' ]);
3851
3851
}
3852
3852
3853
+ public function isText (): bool
3854
+ {
3855
+ return in_array ($ this ->type , ['varchar ' , 'clob ' ]);
3856
+ }
3857
+
3853
3858
public function setPk ($ value ) /*: void*/
3854
3859
{
3855
3860
$ this ->pk = $ value ;
@@ -3874,7 +3879,7 @@ public function serialize()
3874
3879
{
3875
3880
$ json = [
3876
3881
'name ' => $ this ->realName ,
3877
- 'alias ' => $ this ->name != $ this ->realName ? $ this ->name : null ,
3882
+ 'alias ' => $ this ->name != $ this ->realName ? $ this ->name : null ,
3878
3883
'type ' => $ this ->type ,
3879
3884
'length ' => $ this ->length ,
3880
3885
'precision ' => $ this ->precision ,
@@ -5555,6 +5560,7 @@ class GenericDB
5555
5560
private $ address ;
5556
5561
private $ port ;
5557
5562
private $ database ;
5563
+ private $ command ;
5558
5564
private $ tables ;
5559
5565
private $ mapping ;
5560
5566
private $ username ;
@@ -5585,22 +5591,30 @@ private function getCommands(): array
5585
5591
{
5586
5592
switch ($ this ->driver ) {
5587
5593
case 'mysql ' :
5588
- return [
5594
+ $ commands = [
5589
5595
'SET SESSION sql_warnings=1; ' ,
5590
5596
'SET NAMES utf8mb4; ' ,
5591
5597
'SET SESSION sql_mode = "ANSI,TRADITIONAL"; ' ,
5592
5598
];
5599
+ break ;
5593
5600
case 'pgsql ' :
5594
- return [
5601
+ $ commands = [
5595
5602
"SET NAMES 'UTF8'; " ,
5596
5603
];
5604
+ break ;
5597
5605
case 'sqlsrv ' :
5598
- return [];
5606
+ $ commands = [];
5607
+ break ;
5599
5608
case 'sqlite ' :
5600
- return [
5609
+ $ commands = [
5601
5610
'PRAGMA foreign_keys = on; ' ,
5602
5611
];
5612
+ break ;
5613
+ }
5614
+ if ($ this ->command != '' ) {
5615
+ $ commands [] = $ this ->command ;
5603
5616
}
5617
+ return $ commands ;
5604
5618
}
5605
5619
5606
5620
private function getOptions (): array
@@ -5647,20 +5661,21 @@ private function initPdo(): bool
5647
5661
return $ result ;
5648
5662
}
5649
5663
5650
- public function __construct (string $ driver , string $ address , int $ port , string $ database , array $ tables , array $ mapping , string $ username , string $ password )
5664
+ public function __construct (string $ driver , string $ address , int $ port , string $ database , string $ command , array $ tables , array $ mapping , string $ username , string $ password )
5651
5665
{
5652
5666
$ this ->driver = $ driver ;
5653
5667
$ this ->address = $ address ;
5654
5668
$ this ->port = $ port ;
5655
5669
$ this ->database = $ database ;
5670
+ $ this ->command = $ command ;
5656
5671
$ this ->tables = $ tables ;
5657
5672
$ this ->mapping = $ mapping ;
5658
5673
$ this ->username = $ username ;
5659
5674
$ this ->password = $ password ;
5660
5675
$ this ->initPdo ();
5661
5676
}
5662
5677
5663
- public function reconstruct (string $ driver , string $ address , int $ port , string $ database , array $ tables , array $ mapping , string $ username , string $ password ): bool
5678
+ public function reconstruct (string $ driver , string $ address , int $ port , string $ database , string $ command , array $ tables , array $ mapping , string $ username , string $ password ): bool
5664
5679
{
5665
5680
if ($ driver ) {
5666
5681
$ this ->driver = $ driver ;
@@ -5674,6 +5689,9 @@ public function reconstruct(string $driver, string $address, int $port, string $
5674
5689
if ($ database ) {
5675
5690
$ this ->database = $ database ;
5676
5691
}
5692
+ if ($ command ) {
5693
+ $ this ->command = $ command ;
5694
+ }
5677
5695
if ($ tables ) {
5678
5696
$ this ->tables = $ tables ;
5679
5697
}
@@ -8845,6 +8863,15 @@ private function getDatabase(): string
8845
8863
return '' ;
8846
8864
}
8847
8865
8866
+ private function getCommand (): string
8867
+ {
8868
+ $ commandHandler = $ this ->getProperty ('commandHandler ' , '' );
8869
+ if ($ commandHandler ) {
8870
+ return call_user_func ($ commandHandler );
8871
+ }
8872
+ return '' ;
8873
+ }
8874
+
8848
8875
private function getTables (): array
8849
8876
{
8850
8877
$ tablesHandler = $ this ->getProperty ('tablesHandler ' , '' );
@@ -8887,12 +8914,13 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
8887
8914
$ address = $ this ->getAddress ();
8888
8915
$ port = $ this ->getPort ();
8889
8916
$ database = $ this ->getDatabase ();
8917
+ $ command = $ this ->getCommand ();
8890
8918
$ tables = $ this ->getTables ();
8891
8919
$ mapping = $ this ->getMapping ();
8892
8920
$ username = $ this ->getUsername ();
8893
8921
$ password = $ this ->getPassword ();
8894
- if ($ driver || $ address || $ port || $ database || $ tables || $ mapping || $ username || $ password ) {
8895
- $ this ->db ->reconstruct ($ driver , $ address , $ port , $ database , $ tables , $ mapping , $ username , $ password );
8922
+ if ($ driver || $ address || $ port || $ database || $ command || $ tables || $ mapping || $ username || $ password ) {
8923
+ $ this ->db ->reconstruct ($ driver , $ address , $ port , $ database , $ command , $ tables , $ mapping , $ username , $ password );
8896
8924
}
8897
8925
return $ next ->handle ($ request );
8898
8926
}
@@ -9079,6 +9107,61 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
9079
9107
}
9080
9108
}
9081
9109
9110
+ // file: src/Tqdev/PhpCrudApi/Middleware/TextSearchMiddleware.php
9111
+ namespace Tqdev \PhpCrudApi \Middleware {
9112
+
9113
+ use Psr \Http \Message \ResponseInterface ;
9114
+ use Psr \Http \Message \ServerRequestInterface ;
9115
+ use Psr \Http \Server \RequestHandlerInterface ;
9116
+ use Tqdev \PhpCrudApi \Column \ReflectionService ;
9117
+ use Tqdev \PhpCrudApi \Controller \Responder ;
9118
+ use Tqdev \PhpCrudApi \Middleware \Base \Middleware ;
9119
+ use Tqdev \PhpCrudApi \Middleware \Router \Router ;
9120
+ use Tqdev \PhpCrudApi \RequestUtils ;
9121
+
9122
+ class TextSearchMiddleware extends Middleware
9123
+ {
9124
+ private $ reflection ;
9125
+
9126
+ public function __construct (Router $ router , Responder $ responder , array $ properties , ReflectionService $ reflection )
9127
+ {
9128
+ parent ::__construct ($ router , $ responder , $ properties );
9129
+ $ this ->reflection = $ reflection ;
9130
+ }
9131
+
9132
+ public function process (ServerRequestInterface $ request , RequestHandlerInterface $ next ): ResponseInterface
9133
+ {
9134
+ $ operation = RequestUtils::getOperation ($ request );
9135
+ if ($ operation == 'list ' ) {
9136
+ $ tableName = RequestUtils::getPathSegment ($ request , 2 );
9137
+ $ params = RequestUtils::getParams ($ request );
9138
+ $ parameterName = $ this ->getProperty ('parameter ' , 'search ' );
9139
+ if (isset ($ params [$ parameterName ])) {
9140
+ $ search = $ params [$ parameterName ][0 ];
9141
+ unset($ params [$ parameterName ]);
9142
+ $ table = $ this ->reflection ->getTable ($ tableName );
9143
+ $ i = 0 ;
9144
+ foreach ($ table ->getColumnNames () as $ columnName ) {
9145
+ $ column = $ table ->getColumn ($ columnName );
9146
+ while (isset ($ params ["filter $ i " ])) {
9147
+ $ i ++;
9148
+ }
9149
+ if ($ i >= 10 ) {
9150
+ break ;
9151
+ }
9152
+ if ($ column ->isText ()) {
9153
+ $ params ["filter $ i " ] = "$ columnName,cs, $ search " ;
9154
+ $ i ++;
9155
+ }
9156
+ }
9157
+ }
9158
+ $ request = RequestUtils::setParams ($ request , $ params );
9159
+ }
9160
+ return $ next ->handle ($ request );
9161
+ }
9162
+ }
9163
+ }
9164
+
9082
9165
// file: src/Tqdev/PhpCrudApi/Middleware/ValidationMiddleware.php
9083
9166
namespace Tqdev \PhpCrudApi \Middleware {
9084
9167
@@ -11526,6 +11609,7 @@ private function setHabtmValues(ReflectedTable $t1, ReflectedTable $t2, array &$
11526
11609
use Tqdev \PhpCrudApi \Middleware \Router \SimpleRouter ;
11527
11610
use Tqdev \PhpCrudApi \Middleware \SanitationMiddleware ;
11528
11611
use Tqdev \PhpCrudApi \Middleware \SslRedirectMiddleware ;
11612
+ use Tqdev \PhpCrudApi \Middleware \TextSearchMiddleware ;
11529
11613
use Tqdev \PhpCrudApi \Middleware \ValidationMiddleware ;
11530
11614
use Tqdev \PhpCrudApi \Middleware \XmlMiddleware ;
11531
11615
use Tqdev \PhpCrudApi \Middleware \XsrfMiddleware ;
@@ -11545,6 +11629,7 @@ public function __construct(Config $config)
11545
11629
$ config ->getAddress (),
11546
11630
$ config ->getPort (),
11547
11631
$ config ->getDatabase (),
11632
+ $ config ->getCommand (),
11548
11633
$ config ->getTables (),
11549
11634
$ config ->getMapping (),
11550
11635
$ config ->getUsername (),
@@ -11611,6 +11696,9 @@ public function __construct(Config $config)
11611
11696
case 'customization ' :
11612
11697
new CustomizationMiddleware ($ router , $ responder , $ config , $ middleware , $ reflection );
11613
11698
break ;
11699
+ case 'textSearch ' :
11700
+ new TextSearchMiddleware ($ router , $ responder , $ properties , $ reflection );
11701
+ break ;
11614
11702
case 'xml ' :
11615
11703
new XmlMiddleware ($ router , $ responder , $ config , $ middleware , $ reflection );
11616
11704
break ;
@@ -11730,6 +11818,7 @@ class Config
11730
11818
'username ' => '' ,
11731
11819
'password ' => '' ,
11732
11820
'database ' => '' ,
11821
+ 'command ' => '' ,
11733
11822
'tables ' => '' ,
11734
11823
'mapping ' => '' ,
11735
11824
'middlewares ' => 'cors,errors ' ,
@@ -11860,6 +11949,11 @@ public function getDatabase(): string
11860
11949
return $ this ->values ['database ' ];
11861
11950
}
11862
11951
11952
+ public function getCommand (): string
11953
+ {
11954
+ return $ this ->values ['command ' ];
11955
+ }
11956
+
11863
11957
public function getTables (): array
11864
11958
{
11865
11959
return array_filter (array_map ('trim ' , explode (', ' , $ this ->values ['tables ' ])));
0 commit comments