Skip to content

Commit 4738b91

Browse files
authored
db-type in metadata
First of all, thanks for building this great php api framework and sharing it with the world! I am a SAP Developer mainly developping JS apps with SAPui5 / OpenUit (https://open.sap.com/) And for Sapui5 to work with your API, i made a SAPui5 model implementation of your api With this model I can retrieve, edit and send data to your api and bind it to my views. Other SAPui5 Models (like Odata) auto convert api response data to usable js json objects. for example. Any date/time fields in the api will in js be converted to a new Date() object. I wanted to do this too with my model implementation of your api.. but therefore I needed to know what db column and types my db tables have. I found that in your metadata under paths/TABLENAME/post/parameters[0]/schema/properties all db columns could be found.. and that all types are set to "string" In order to get the db-types aswel I edited your class and added "db-type" to paths/TABLENAME/get/responses/200/schema/items/properties/PROPERTYNAME So now I can what what the column types are in the db, and so for my model I could convert a db column date to a js Date() object and viseversa. note: I only added the query reflect_type to the MySQL class, I don't have the experience to write it also in PostgreSQL, SQLServer, SQLite. hit me back what you think of it, and if maybe you are willing to accept the pull request or have a better solution, please let me know. Barry
1 parent 701eb54 commit 4738b91

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

api.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,15 @@ public function __construct() {
8888
k2."REFERENCED_TABLE_SCHEMA" = ? AND
8989
k1."TABLE_NAME" COLLATE \'utf8_bin\' = k2."TABLE_NAME" COLLATE \'utf8_bin\' AND
9090
k1."REFERENCED_TABLE_NAME" COLLATE \'utf8_bin\' = ? AND
91-
k2."REFERENCED_TABLE_NAME" COLLATE \'utf8_bin\' IN ?'
91+
k2."REFERENCED_TABLE_NAME" COLLATE \'utf8_bin\' IN ?',
92+
'reflect_type'=> 'SELECT
93+
"COLUMN_NAME", "COLUMN_TYPE"
94+
FROM
95+
"INFORMATION_SCHEMA"."COLUMNS"
96+
WHERE
97+
"TABLE_SCHEMA" = ? AND
98+
"TABLE_NAME" = ?
99+
'
92100
);
93101
}
94102

@@ -2217,6 +2225,11 @@ protected function swagger($settings) {
22172225
foreach ($primaryKeys as $primaryKey) {
22182226
$table_fields[$table['name']][$primaryKey]->primaryKey = true;
22192227
}
2228+
$result = $this->db->query($this->db->getSql('reflect_type'),array($database,$table_list[0]));
2229+
while ($row = $this->db->fetchRow($result)) {
2230+
$expl = explode('(',$row[1]);
2231+
$table_fields[$table['name']][$row[0]]->type = $expl[0];
2232+
}
22202233
}
22212234

22222235
foreach (array('root_actions','id_actions') as $path) {
@@ -2341,6 +2354,9 @@ protected function swagger($settings) {
23412354
if ($k>0) echo ',';
23422355
echo '"'.$field.'": {';
23432356
echo '"type": "string"';
2357+
if (isset($action['fields'][$field]->type)) {
2358+
echo ',"db-type": '.json_encode($action['fields'][$field]->type);
2359+
}
23442360
if (isset($action['fields'][$field]->referenced)) {
23452361
echo ',"x-referenced": '.json_encode($action['fields'][$field]->referenced);
23462362
}

0 commit comments

Comments
 (0)