@@ -6357,7 +6357,7 @@ public function __construct(string $driver)
6357
6357
],
6358
6358
// source: https://docs.microsoft.com/en-us/sql/connect/jdbc/using-basic-data-types?view=sql-server-2017
6359
6359
'sqlsrv ' => [
6360
- 'varbinary(0 ) ' => 'blob ' ,
6360
+ 'varbinary() ' => 'blob ' ,
6361
6361
'bit ' => 'boolean ' ,
6362
6362
'datetime ' => 'timestamp ' ,
6363
6363
'datetime2 ' => 'timestamp ' ,
@@ -8183,6 +8183,7 @@ public function build(): OpenApiDefinition
8183
8183
$ this ->openapi ->set ("servers|0|url " , $ this ->getServerUrl ());
8184
8184
}
8185
8185
$ this ->records ->build ();
8186
+ //$this->columns->build();
8186
8187
return $ this ->openapi ;
8187
8188
}
8188
8189
}
@@ -8199,6 +8200,13 @@ class OpenApiColumnsBuilder
8199
8200
{
8200
8201
private $ openapi ;
8201
8202
private $ reflection ;
8203
+ private $ operations = [
8204
+ 'list ' => 'get ' ,
8205
+ 'create ' => 'post ' ,
8206
+ 'read ' => 'get ' ,
8207
+ 'update ' => 'put ' ,
8208
+ 'delete ' => 'delete ' ,
8209
+ ];
8202
8210
8203
8211
public function __construct (OpenApiDefinition $ openapi , ReflectionService $ reflection )
8204
8212
{
@@ -8208,6 +8216,85 @@ public function __construct(OpenApiDefinition $openapi, ReflectionService $refle
8208
8216
8209
8217
public function build () /*: void*/
8210
8218
{
8219
+ $ tableNames = $ this ->reflection ->getTableNames ();
8220
+ foreach ($ tableNames as $ tableName ) {
8221
+ $ this ->setPath ($ tableName );
8222
+ }
8223
+ }
8224
+
8225
+ private function isOperationOnTableAllowed (string $ operation , string $ tableName ): bool
8226
+ {
8227
+ $ tableHandler = VariableStore::get ('authorization.tableHandler ' );
8228
+ if (!$ tableHandler ) {
8229
+ return true ;
8230
+ }
8231
+ return (bool ) call_user_func ($ tableHandler , $ operation , $ tableName );
8232
+ }
8233
+
8234
+ private function isOperationOnColumnAllowed (string $ operation , string $ tableName , string $ columnName ): bool
8235
+ {
8236
+ $ columnHandler = VariableStore::get ('authorization.columnHandler ' );
8237
+ if (!$ columnHandler ) {
8238
+ return true ;
8239
+ }
8240
+ return (bool ) call_user_func ($ columnHandler , $ operation , $ tableName , $ columnName );
8241
+ }
8242
+
8243
+ private function setPath (string $ tableName ) /*: void*/
8244
+ {
8245
+ $ table = $ this ->reflection ->getTable ($ tableName );
8246
+ $ type = $ table ->getType ();
8247
+ foreach ($ this ->operations as $ operation => $ method ) {
8248
+ if ($ type != 'table ' && $ method != 'get ' ) {
8249
+ continue ;
8250
+ }
8251
+ $ action = $ operation == 'get ' ? 'reflect ' : 'remodel ' ;
8252
+ if (!$ this ->isOperationOnTableAllowed ($ action , $ tableName )) {
8253
+ continue ;
8254
+ }
8255
+ $ parameters = [];
8256
+ if (in_array ($ operation , ['list ' , 'create ' ])) {
8257
+ $ path = sprintf ('/records/%s ' , $ tableName );
8258
+ if ($ operation == 'list ' ) {
8259
+ $ parameters = ['filter ' , 'include ' , 'exclude ' , 'order ' , 'size ' , 'page ' , 'join ' ];
8260
+ }
8261
+ } else {
8262
+ $ path = sprintf ('/records/%s/{%s} ' , $ tableName );
8263
+ if ($ operation == 'read ' ) {
8264
+ $ parameters = ['pk ' , 'include ' , 'exclude ' , 'join ' ];
8265
+ } else {
8266
+ $ parameters = ['pk ' ];
8267
+ }
8268
+ }
8269
+ foreach ($ parameters as $ p => $ parameter ) {
8270
+ $ this ->openapi ->set ("paths| $ path| $ method|parameters| $ p| \$ref " , "#/components/parameters/ $ parameter " );
8271
+ }
8272
+ if (in_array ($ operation , ['create ' , 'update ' , 'increment ' ])) {
8273
+ $ this ->openapi ->set ("paths| $ path| $ method|requestBody| \$ref " , "#/components/requestBodies/ $ operation- " . rawurlencode ($ tableName ));
8274
+ }
8275
+ $ this ->openapi ->set ("paths| $ path| $ method|tags|0 " , "$ tableName " );
8276
+ $ this ->openapi ->set ("paths| $ path| $ method|description " , "$ operation $ tableName " );
8277
+ switch ($ operation ) {
8278
+ case 'list ' :
8279
+ $ this ->openapi ->set ("paths| $ path| $ method|responses|200| \$ref " , "#/components/responses/ $ operation- " . rawurlencode ($ tableName ));
8280
+ break ;
8281
+ case 'create ' :
8282
+ if ($ pk ->getType () == 'integer ' ) {
8283
+ $ this ->openapi ->set ("paths| $ path| $ method|responses|200| \$ref " , "#/components/responses/pk_integer " );
8284
+ } else {
8285
+ $ this ->openapi ->set ("paths| $ path| $ method|responses|200| \$ref " , "#/components/responses/pk_string " );
8286
+ }
8287
+ break ;
8288
+ case 'read ' :
8289
+ $ this ->openapi ->set ("paths| $ path| $ method|responses|200| \$ref " , "#/components/responses/ $ operation- " . rawurlencode ($ tableName ));
8290
+ break ;
8291
+ case 'update ' :
8292
+ case 'delete ' :
8293
+ case 'increment ' :
8294
+ $ this ->openapi ->set ("paths| $ path| $ method|responses|200| \$ref " , "#/components/responses/rows_affected " );
8295
+ break ;
8296
+ }
8297
+ }
8211
8298
}
8212
8299
}
8213
8300
}
0 commit comments