@@ -2084,9 +2084,8 @@ private function getColumnAutoIncrement(ReflectedColumn $column, bool $update):
20842084 case 'mysql ' :
20852085 return $ column ->getPk () ? ' AUTO_INCREMENT ' : '' ;
20862086 case 'pgsql ' :
2087- return '' ;
20882087 case 'sqlsrv ' :
2089- return ( $ column -> getPk () && ! $ update ) ? ' IDENTITY(1,1) ' : '' ;
2088+ return '' ;
20902089 }
20912090 }
20922091
@@ -2278,15 +2277,17 @@ private function getAddTableSQL(ReflectedTable $newTable): String
22782277 $ fields = [];
22792278 $ constraints = [];
22802279 foreach ($ newTable ->getColumnNames () as $ columnName ) {
2280+ $ pkColumn = $ this ->getPrimaryKey ($ tableName );
22812281 $ newColumn = $ newTable ->getColumn ($ columnName );
22822282 $ f1 = $ this ->quote ($ columnName );
22832283 $ f2 = $ this ->getColumnType ($ newColumn , false );
22842284 $ f3 = $ this ->quote ($ tableName . '_ ' . $ columnName . '_fkey ' );
22852285 $ f4 = $ this ->quote ($ newColumn ->getFk ());
22862286 $ f5 = $ this ->quote ($ this ->getPrimaryKey ($ newColumn ->getFk ()));
2287+ $ f6 = $ this ->quote ($ tableName . '_ ' . $ pkColumn . '_pkey ' );
22872288 $ fields [] = "$ f1 $ f2 " ;
22882289 if ($ newColumn ->getPk ()) {
2289- $ constraints [] = "PRIMARY KEY ( $ f1) " ;
2290+ $ constraints [] = "CONSTRAINT $ f6 PRIMARY KEY ($ f1) " ;
22902291 }
22912292 if ($ newColumn ->getFk ()) {
22922293 $ constraints [] = "CONSTRAINT $ f3 FOREIGN KEY ( $ f1) REFERENCES $ f4 ( $ f5) " ;
@@ -3247,6 +3248,60 @@ public function handle(Request $request): Response
32473248 }
32483249}
32493250
3251+ // file: src/Tqdev/PhpCrudApi/Middleware/IpAddressMiddleware.php
3252+
3253+ class IpAddressMiddleware extends Middleware
3254+ {
3255+ private $ reflection ;
3256+
3257+ public function __construct (Router $ router , Responder $ responder , array $ properties , ReflectionService $ reflection )
3258+ {
3259+ parent ::__construct ($ router , $ responder , $ properties );
3260+ $ this ->reflection = $ reflection ;
3261+ $ this ->utils = new RequestUtils ($ reflection );
3262+ }
3263+
3264+ private function callHandler ($ handler , $ record , String $ operation , ReflectedTable $ table ) /*: object */
3265+ {
3266+ $ context = (array ) $ record ;
3267+ $ columnName = $ this ->getProperty ('column ' , '' );
3268+ if ($ table ->hasColumn ($ columnName )) {
3269+ if ($ operation == 'create ' ) {
3270+ $ context [$ columnName ] = $ _SERVER ['REMOTE_ADDR ' ];
3271+ } else {
3272+ unset($ context [$ columnName ]);
3273+ }
3274+ }
3275+ return (object ) $ context ;
3276+ }
3277+
3278+ public function handle (Request $ request ): Response
3279+ {
3280+ $ operation = $ this ->utils ->getOperation ($ request );
3281+ if (in_array ($ operation , ['create ' , 'update ' , 'increment ' ])) {
3282+ $ tableName = $ request ->getPathSegment (2 );
3283+ if ($ this ->reflection ->hasTable ($ tableName )) {
3284+ $ record = $ request ->getBody ();
3285+ if ($ record !== null ) {
3286+ $ handler = $ this ->getProperty ('handler ' , '' );
3287+ if ($ handler !== '' ) {
3288+ $ table = $ this ->reflection ->getTable ($ tableName );
3289+ if (is_array ($ record )) {
3290+ foreach ($ record as &$ r ) {
3291+ $ r = $ this ->callHandler ($ handler , $ r , $ operation , $ table );
3292+ }
3293+ } else {
3294+ $ record = $ this ->callHandler ($ handler , $ record , $ operation , $ table );
3295+ }
3296+ $ request ->setBody ($ record );
3297+ }
3298+ }
3299+ }
3300+ }
3301+ return $ this ->next ->handle ($ request );
3302+ }
3303+ }
3304+
32503305// file: src/Tqdev/PhpCrudApi/Middleware/JoinLimitsMiddleware.php
32513306
32523307class JoinLimitsMiddleware extends Middleware
@@ -5287,6 +5342,9 @@ public function __construct(Config $config)
52875342 case 'validation ' :
52885343 new ValidationMiddleware ($ router , $ responder , $ properties , $ reflection );
52895344 break ;
5345+ case 'ipAddress ' :
5346+ new IpAddressMiddleware ($ router , $ responder , $ properties );
5347+ break ;
52905348 case 'sanitation ' :
52915349 new SanitationMiddleware ($ router , $ responder , $ properties , $ reflection );
52925350 break ;
0 commit comments