Skip to content

Commit 83035f5

Browse files
committed
Refactor for #191 and #192
1 parent 113c000 commit 83035f5

File tree

2 files changed

+85
-41
lines changed

2 files changed

+85
-41
lines changed

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,6 @@ $api = new PHP_CRUD_API(array(
9494
// configurable options
9595
'allow_origin'=>'*',
9696
'auto_include'=>true,
97-
'extensions'=>true,
9897
// dependencies (added for unit testing):
9998
'db'=>null,
10099
'method'=>$_SERVER['REQUEST_METHOD'],

api.php

Lines changed: 85 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -89,13 +89,15 @@ public function __construct() {
8989
k1."TABLE_NAME" COLLATE \'utf8_bin\' = k2."TABLE_NAME" COLLATE \'utf8_bin\' AND
9090
k1."REFERENCED_TABLE_NAME" COLLATE \'utf8_bin\' = ? AND
9191
k2."REFERENCED_TABLE_NAME" COLLATE \'utf8_bin\' IN ?',
92-
'reflect_type'=> 'SELECT
93-
"COLUMN_NAME", "COLUMN_TYPE"
92+
'reflect_columns'=> 'SELECT
93+
"COLUMN_NAME", "COLUMN_DEFAULT", "IS_NULLABLE", "DATA_TYPE", "CHARACTER_MAXIMUM_LENGTH"
9494
FROM
9595
"INFORMATION_SCHEMA"."COLUMNS"
9696
WHERE
9797
"TABLE_SCHEMA" = ? AND
98-
"TABLE_NAME" = ?'
98+
"TABLE_NAME" = ?
99+
ORDER BY
100+
"ORDINAL_POSITION"'
99101
);
100102
}
101103

@@ -243,7 +245,7 @@ class PostgreSQL implements DatabaseInterface {
243245
public function __construct() {
244246
$this->queries = array(
245247
'list_tables'=>'select
246-
"table_name","table_comment"
248+
"table_name",\'\' as "table_comment"
247249
from
248250
"information_schema"."tables"
249251
where
@@ -253,7 +255,7 @@ public function __construct() {
253255
from
254256
"information_schema"."tables"
255257
where
256-
"table_name" like ? and
258+
"table_name" = ? and
257259
"table_catalog" = ?',
258260
'reflect_pk'=>'select
259261
"column_name"
@@ -316,7 +318,16 @@ public function __construct() {
316318
cub2."table_catalog" = ? and
317319
cua1."table_name" = cub1."table_name" and
318320
cua2."table_name" = ? and
319-
cub2."table_name" in ?'
321+
cub2."table_name" in ?',
322+
'reflect_columns'=> 'select
323+
"column_name", "column_default", "is_nullable", "data_type", "character_maximum_length"
324+
from
325+
"information_schema"."columns"
326+
where
327+
"table_name" like ? and
328+
"table_catalog" = ?
329+
order by
330+
"ordinal_position"'
320331
);
321332
}
322333

@@ -494,7 +505,7 @@ public function __construct() {
494505
FROM
495506
"INFORMATION_SCHEMA"."TABLES"
496507
WHERE
497-
"TABLE_NAME" LIKE ? AND
508+
"TABLE_NAME" = ? AND
498509
"TABLE_CATALOG" = ?',
499510
'reflect_pk'=>'SELECT
500511
"COLUMN_NAME"
@@ -557,7 +568,16 @@ public function __construct() {
557568
cub2."TABLE_CATALOG" = ? AND
558569
cua1."TABLE_NAME" = cub1."TABLE_NAME" AND
559570
cua2."TABLE_NAME" = ? AND
560-
cub2."TABLE_NAME" IN ?'
571+
cub2."TABLE_NAME" IN ?',
572+
'reflect_columns'=> 'SELECT
573+
"COLUMN_NAME", "COLUMN_DEFAULT", "IS_NULLABLE", "DATA_TYPE", "CHARACTER_MAXIMUM_LENGTH"
574+
FROM
575+
"INFORMATION_SCHEMA"."COLUMNS"
576+
WHERE
577+
"TABLE_NAME" LIKE ? AND
578+
"TABLE_CATALOG" = ?
579+
ORDER BY
580+
"ORDINAL_POSITION"'
561581
);
562582
}
563583

@@ -905,7 +925,15 @@ public function __construct() {
905925
? like "%" AND
906926
k1."self" = k2."self" AND
907927
k1."table" = ? AND
908-
k2."table" IN ?'
928+
k2."table" IN ?',
929+
'reflect_columns'=> 'SELECT
930+
"name", "dflt_value", "notnull", "type", 2147483647
931+
FROM
932+
"sys/columns"
933+
WHERE
934+
"self"=?
935+
ORDER BY
936+
"cid"'
909937
);
910938
}
911939

@@ -2083,7 +2111,6 @@ public function __construct($config) {
20832111
$tenancy_function = isset($tenancy_function)?$tenancy_function:null;
20842112
$input_sanitizer = isset($input_sanitizer)?$input_sanitizer:null;
20852113
$input_validator = isset($input_validator)?$input_validator:null;
2086-
$extensions = isset($extensions)?$extensions:null;
20872114
$auto_include = isset($auto_include)?$auto_include:null;
20882115
$allow_origin = isset($allow_origin)?$allow_origin:null;
20892116

@@ -2129,9 +2156,6 @@ public function __construct($config) {
21292156
}
21302157
$db->connect($hostname,$username,$password,$database,$port,$socket,$charset);
21312158
}
2132-
if ($extensions===null) {
2133-
$extensions = true;
2134-
}
21352159
if ($auto_include===null) {
21362160
$auto_include = true;
21372161
}
@@ -2140,7 +2164,7 @@ public function __construct($config) {
21402164
}
21412165

21422166
$this->db = $db;
2143-
$this->settings = compact('method', 'request', 'get', 'post', 'origin', 'database', 'table_authorizer', 'record_filter', 'column_authorizer', 'tenancy_function', 'input_sanitizer', 'input_validator', 'extensions', 'auto_include', 'allow_origin');
2167+
$this->settings = compact('method', 'request', 'get', 'post', 'origin', 'database', 'table_authorizer', 'record_filter', 'column_authorizer', 'tenancy_function', 'input_sanitizer', 'input_validator', 'auto_include', 'allow_origin');
21442168
}
21452169

21462170
public static function php_crud_api_transform(&$tables) {
@@ -2211,24 +2235,25 @@ protected function swagger($settings) {
22112235
$table_fields = $this->findFields($table_list,false,false,false,$database);
22122236
$table_names = array_map(function($v){ return $v['name'];},$tables);
22132237

2214-
if ($extensions) {
2215-
$result = $this->db->query($this->db->getSql('reflect_belongs_to'),array($table_list[0],$table_names,$database,$database));
2216-
while ($row = $this->db->fetchRow($result)) {
2217-
$table_fields[$table['name']][$row[1]]->references=array($row[2],$row[3]);
2218-
}
2219-
$result = $this->db->query($this->db->getSql('reflect_has_many'),array($table_names,$table_list[0],$database,$database));
2220-
while ($row = $this->db->fetchRow($result)) {
2221-
$table_fields[$table['name']][$row[3]]->referenced[]=array($row[0],$row[1]);
2222-
}
2223-
$primaryKeys = $this->findPrimaryKeys($table_list[0],$database);
2224-
foreach ($primaryKeys as $primaryKey) {
2225-
$table_fields[$table['name']][$primaryKey]->primaryKey = true;
2226-
}
2227-
$result = $this->db->query($this->db->getSql('reflect_type'),array($database,$table_list[0]));
2228-
while ($row = $this->db->fetchRow($result)) {
2229-
$expl = explode('(',$row[1]);
2230-
$table_fields[$table['name']][$row[0]]->type = $expl[0];
2231-
}
2238+
// extensions
2239+
$result = $this->db->query($this->db->getSql('reflect_belongs_to'),array($table_list[0],$table_names,$database,$database));
2240+
while ($row = $this->db->fetchRow($result)) {
2241+
$table_fields[$table['name']][$row[1]]->references=array($row[2],$row[3]);
2242+
}
2243+
$result = $this->db->query($this->db->getSql('reflect_has_many'),array($table_names,$table_list[0],$database,$database));
2244+
while ($row = $this->db->fetchRow($result)) {
2245+
$table_fields[$table['name']][$row[3]]->referenced[]=array($row[0],$row[1]);
2246+
}
2247+
$primaryKeys = $this->findPrimaryKeys($table_list[0],$database);
2248+
foreach ($primaryKeys as $primaryKey) {
2249+
$table_fields[$table['name']][$primaryKey]->primaryKey = true;
2250+
}
2251+
$result = $this->db->query($this->db->getSql('reflect_columns'),array($database,$table_list[0]));
2252+
while ($row = $this->db->fetchRow($result)) {
2253+
if ($row[1]!==null) $table_fields[$table['name']][$row[0]]->default = $row[1];
2254+
$table_fields[$table['name']][$row[0]]->required = strtolower($row[2])=='no' && $row[1]===null;
2255+
$table_fields[$table['name']][$row[0]]->format = $row[3];
2256+
$table_fields[$table['name']][$row[0]]->maxLength = $row[4];
22322257
}
22332258

22342259
foreach (array('root_actions','id_actions') as $path) {
@@ -2353,8 +2378,13 @@ protected function swagger($settings) {
23532378
if ($k>0) echo ',';
23542379
echo '"'.$field.'": {';
23552380
echo '"type": "string"';
2356-
if (isset($action['fields'][$field]->type)) {
2357-
echo ',"x-dbtype": '.json_encode($action['fields'][$field]->type);
2381+
echo ',"format": '.json_encode($action['fields'][$field]->format);
2382+
if (isset($action['fields'][$field]->maxLength)) {
2383+
echo ',"maxLength": '.json_encode($action['fields'][$field]->maxLength);
2384+
}
2385+
echo ',"required": '.json_encode($action['fields'][$field]->required);
2386+
if (isset($action['fields'][$field]->default)) {
2387+
echo ',"default": '.json_encode($action['fields'][$field]->default);
23582388
}
23592389
if (isset($action['fields'][$field]->referenced)) {
23602390
echo ',"x-referenced": '.json_encode($action['fields'][$field]->referenced);
@@ -2386,8 +2416,13 @@ protected function swagger($settings) {
23862416
if ($k>0) echo ',';
23872417
echo '"'.$field.'": {';
23882418
echo '"type": "string"';
2389-
if (isset($action['fields'][$field]->type)) {
2390-
echo ',"x-dbtype": '.json_encode($action['fields'][$field]->type);
2419+
echo ',"format": '.json_encode($action['fields'][$field]->format);
2420+
if (isset($action['fields'][$field]->maxLength)) {
2421+
echo ',"maxLength": '.json_encode($action['fields'][$field]->maxLength);
2422+
}
2423+
echo ',"required": '.json_encode($action['fields'][$field]->required);
2424+
if (isset($action['fields'][$field]->default)) {
2425+
echo ',"default": '.json_encode($action['fields'][$field]->default);
23912426
}
23922427
if (isset($action['fields'][$field]->referenced)) {
23932428
echo ',"x-referenced": '.json_encode($action['fields'][$field]->referenced);
@@ -2445,8 +2480,13 @@ protected function swagger($settings) {
24452480
if ($k>0) echo ',';
24462481
echo '"'.$field.'": {';
24472482
echo '"type": "string"';
2448-
if (isset($action['fields'][$field]->type)) {
2449-
echo ',"x-dbtype": '.json_encode($action['fields'][$field]->type);
2483+
echo ',"format": '.json_encode($action['fields'][$field]->format);
2484+
if (isset($action['fields'][$field]->maxLength)) {
2485+
echo ',"maxLength": '.json_encode($action['fields'][$field]->maxLength);
2486+
}
2487+
echo ',"required": '.json_encode($action['fields'][$field]->required);
2488+
if (isset($action['fields'][$field]->default)) {
2489+
echo ',"default": '.json_encode($action['fields'][$field]->default);
24502490
}
24512491
if (isset($action['fields'][$field]->referenced)) {
24522492
echo ',"x-referenced": '.json_encode($action['fields'][$field]->referenced);
@@ -2475,8 +2515,13 @@ protected function swagger($settings) {
24752515
if ($k>0) echo ',';
24762516
echo '"'.$field.'": {';
24772517
echo '"type": "string"';
2478-
if (isset($action['fields'][$field]->type)) {
2479-
echo ',"x-dbtype": '.json_encode($action['fields'][$field]->type);
2518+
echo ',"format": '.json_encode($action['fields'][$field]->format);
2519+
if (isset($action['fields'][$field]->maxLength)) {
2520+
echo ',"maxLength": '.json_encode($action['fields'][$field]->maxLength);
2521+
}
2522+
echo ',"required": '.json_encode($action['fields'][$field]->required);
2523+
if (isset($action['fields'][$field]->default)) {
2524+
echo ',"default": '.json_encode($action['fields'][$field]->default);
24802525
}
24812526
if (isset($action['fields'][$field]->referenced)) {
24822527
echo ',"x-referenced": '.json_encode($action['fields'][$field]->referenced);

0 commit comments

Comments
 (0)