@@ -2084,9 +2084,8 @@ private function getColumnAutoIncrement(ReflectedColumn $column, bool $update):
2084
2084
case 'mysql ' :
2085
2085
return $ column ->getPk () ? ' AUTO_INCREMENT ' : '' ;
2086
2086
case 'pgsql ' :
2087
- return '' ;
2088
2087
case 'sqlsrv ' :
2089
- return ( $ column -> getPk () && ! $ update ) ? ' IDENTITY(1,1) ' : '' ;
2088
+ return '' ;
2090
2089
}
2091
2090
}
2092
2091
@@ -2278,15 +2277,17 @@ private function getAddTableSQL(ReflectedTable $newTable): String
2278
2277
$ fields = [];
2279
2278
$ constraints = [];
2280
2279
foreach ($ newTable ->getColumnNames () as $ columnName ) {
2280
+ $ pkColumn = $ this ->getPrimaryKey ($ tableName );
2281
2281
$ newColumn = $ newTable ->getColumn ($ columnName );
2282
2282
$ f1 = $ this ->quote ($ columnName );
2283
2283
$ f2 = $ this ->getColumnType ($ newColumn , false );
2284
2284
$ f3 = $ this ->quote ($ tableName . '_ ' . $ columnName . '_fkey ' );
2285
2285
$ f4 = $ this ->quote ($ newColumn ->getFk ());
2286
2286
$ f5 = $ this ->quote ($ this ->getPrimaryKey ($ newColumn ->getFk ()));
2287
+ $ f6 = $ this ->quote ($ tableName . '_ ' . $ pkColumn . '_pkey ' );
2287
2288
$ fields [] = "$ f1 $ f2 " ;
2288
2289
if ($ newColumn ->getPk ()) {
2289
- $ constraints [] = "PRIMARY KEY ( $ f1) " ;
2290
+ $ constraints [] = "CONSTRAINT $ f6 PRIMARY KEY ($ f1) " ;
2290
2291
}
2291
2292
if ($ newColumn ->getFk ()) {
2292
2293
$ constraints [] = "CONSTRAINT $ f3 FOREIGN KEY ( $ f1) REFERENCES $ f4 ( $ f5) " ;
@@ -3247,6 +3248,60 @@ public function handle(Request $request): Response
3247
3248
}
3248
3249
}
3249
3250
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
+
3250
3305
// file: src/Tqdev/PhpCrudApi/Middleware/JoinLimitsMiddleware.php
3251
3306
3252
3307
class JoinLimitsMiddleware extends Middleware
@@ -5287,6 +5342,9 @@ public function __construct(Config $config)
5287
5342
case 'validation ' :
5288
5343
new ValidationMiddleware ($ router , $ responder , $ properties , $ reflection );
5289
5344
break ;
5345
+ case 'ipAddress ' :
5346
+ new IpAddressMiddleware ($ router , $ responder , $ properties );
5347
+ break ;
5290
5348
case 'sanitation ' :
5291
5349
new SanitationMiddleware ($ router , $ responder , $ properties , $ reflection );
5292
5350
break ;
0 commit comments