Skip to content

Commit cd5eb8c

Browse files
committed
Fix sqlsrv regression
1 parent d6032c9 commit cd5eb8c

File tree

3 files changed

+175
-1
lines changed

3 files changed

+175
-1
lines changed

api.php

Lines changed: 88 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6357,7 +6357,7 @@ public function __construct(string $driver)
63576357
],
63586358
// source: https://docs.microsoft.com/en-us/sql/connect/jdbc/using-basic-data-types?view=sql-server-2017
63596359
'sqlsrv' => [
6360-
'varbinary(0)' => 'blob',
6360+
'varbinary()' => 'blob',
63616361
'bit' => 'boolean',
63626362
'datetime' => 'timestamp',
63636363
'datetime2' => 'timestamp',
@@ -8183,6 +8183,7 @@ public function build(): OpenApiDefinition
81838183
$this->openapi->set("servers|0|url", $this->getServerUrl());
81848184
}
81858185
$this->records->build();
8186+
//$this->columns->build();
81868187
return $this->openapi;
81878188
}
81888189
}
@@ -8199,6 +8200,13 @@ class OpenApiColumnsBuilder
81998200
{
82008201
private $openapi;
82018202
private $reflection;
8203+
private $operations = [
8204+
'list' => 'get',
8205+
'create' => 'post',
8206+
'read' => 'get',
8207+
'update' => 'put',
8208+
'delete' => 'delete',
8209+
];
82028210

82038211
public function __construct(OpenApiDefinition $openapi, ReflectionService $reflection)
82048212
{
@@ -8208,6 +8216,85 @@ public function __construct(OpenApiDefinition $openapi, ReflectionService $refle
82088216

82098217
public function build() /*: void*/
82108218
{
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+
}
82118298
}
82128299
}
82138300
}

src/Tqdev/PhpCrudApi/OpenApi/OpenApiBuilder.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ public function build(): OpenApiDefinition
3535
$this->openapi->set("servers|0|url", $this->getServerUrl());
3636
}
3737
$this->records->build();
38+
//$this->columns->build();
3839
return $this->openapi;
3940
}
4041
}

src/Tqdev/PhpCrudApi/OpenApi/OpenApiColumnsBuilder.php

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,13 @@ class OpenApiColumnsBuilder
1010
{
1111
private $openapi;
1212
private $reflection;
13+
private $operations = [
14+
'list' => 'get',
15+
'create' => 'post',
16+
'read' => 'get',
17+
'update' => 'put',
18+
'delete' => 'delete',
19+
];
1320

1421
public function __construct(OpenApiDefinition $openapi, ReflectionService $reflection)
1522
{
@@ -19,5 +26,84 @@ public function __construct(OpenApiDefinition $openapi, ReflectionService $refle
1926

2027
public function build() /*: void*/
2128
{
29+
$tableNames = $this->reflection->getTableNames();
30+
foreach ($tableNames as $tableName) {
31+
$this->setPath($tableName);
32+
}
33+
}
34+
35+
private function isOperationOnTableAllowed(string $operation, string $tableName): bool
36+
{
37+
$tableHandler = VariableStore::get('authorization.tableHandler');
38+
if (!$tableHandler) {
39+
return true;
40+
}
41+
return (bool) call_user_func($tableHandler, $operation, $tableName);
42+
}
43+
44+
private function isOperationOnColumnAllowed(string $operation, string $tableName, string $columnName): bool
45+
{
46+
$columnHandler = VariableStore::get('authorization.columnHandler');
47+
if (!$columnHandler) {
48+
return true;
49+
}
50+
return (bool) call_user_func($columnHandler, $operation, $tableName, $columnName);
51+
}
52+
53+
private function setPath(string $tableName) /*: void*/
54+
{
55+
$table = $this->reflection->getTable($tableName);
56+
$type = $table->getType();
57+
foreach ($this->operations as $operation => $method) {
58+
if ($type != 'table' && $method != 'get') {
59+
continue;
60+
}
61+
$action = $operation == 'get' ? 'reflect' : 'remodel';
62+
if (!$this->isOperationOnTableAllowed($action, $tableName)) {
63+
continue;
64+
}
65+
$parameters = [];
66+
if (in_array($operation, ['list', 'create'])) {
67+
$path = sprintf('/records/%s', $tableName);
68+
if ($operation == 'list') {
69+
$parameters = ['filter', 'include', 'exclude', 'order', 'size', 'page', 'join'];
70+
}
71+
} else {
72+
$path = sprintf('/records/%s/{%s}', $tableName);
73+
if ($operation == 'read') {
74+
$parameters = ['pk', 'include', 'exclude', 'join'];
75+
} else {
76+
$parameters = ['pk'];
77+
}
78+
}
79+
foreach ($parameters as $p => $parameter) {
80+
$this->openapi->set("paths|$path|$method|parameters|$p|\$ref", "#/components/parameters/$parameter");
81+
}
82+
if (in_array($operation, ['create', 'update', 'increment'])) {
83+
$this->openapi->set("paths|$path|$method|requestBody|\$ref", "#/components/requestBodies/$operation-" . rawurlencode($tableName));
84+
}
85+
$this->openapi->set("paths|$path|$method|tags|0", "$tableName");
86+
$this->openapi->set("paths|$path|$method|description", "$operation $tableName");
87+
switch ($operation) {
88+
case 'list':
89+
$this->openapi->set("paths|$path|$method|responses|200|\$ref", "#/components/responses/$operation-" . rawurlencode($tableName));
90+
break;
91+
case 'create':
92+
if ($pk->getType() == 'integer') {
93+
$this->openapi->set("paths|$path|$method|responses|200|\$ref", "#/components/responses/pk_integer");
94+
} else {
95+
$this->openapi->set("paths|$path|$method|responses|200|\$ref", "#/components/responses/pk_string");
96+
}
97+
break;
98+
case 'read':
99+
$this->openapi->set("paths|$path|$method|responses|200|\$ref", "#/components/responses/$operation-" . rawurlencode($tableName));
100+
break;
101+
case 'update':
102+
case 'delete':
103+
case 'increment':
104+
$this->openapi->set("paths|$path|$method|responses|200|\$ref", "#/components/responses/rows_affected");
105+
break;
106+
}
107+
}
22108
}
23109
}

0 commit comments

Comments
 (0)