Browse Source

Refactor for #191 and #192

Maurits van der Schee 8 years ago
parent
commit
83035f5aab
2 changed files with 85 additions and 41 deletions
  1. 0
    1
      README.md
  2. 85
    40
      api.php

+ 0
- 1
README.md View File

@@ -94,7 +94,6 @@ $api = new PHP_CRUD_API(array(
94 94
 // configurable options
95 95
 	'allow_origin'=>'*',
96 96
 	'auto_include'=>true,
97
-	'extensions'=>true,
98 97
 // dependencies (added for unit testing):
99 98
 	'db'=>null,
100 99
 	'method'=>$_SERVER['REQUEST_METHOD'],

+ 85
- 40
api.php View File

@@ -89,13 +89,15 @@ class MySQL implements DatabaseInterface {
89 89
 					k1."TABLE_NAME" COLLATE \'utf8_bin\' = k2."TABLE_NAME" COLLATE \'utf8_bin\' AND
90 90
 					k1."REFERENCED_TABLE_NAME" COLLATE \'utf8_bin\' = ? AND
91 91
 					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"
94 94
 				FROM 
95 95
 					"INFORMATION_SCHEMA"."COLUMNS" 
96 96
 				WHERE 
97 97
 					"TABLE_SCHEMA" = ? AND 
98
-					"TABLE_NAME" = ?'
98
+					"TABLE_NAME" = ?
99
+				ORDER BY
100
+					"ORDINAL_POSITION"'
99 101
 		);
100 102
 	}
101 103
 
@@ -243,7 +245,7 @@ class PostgreSQL implements DatabaseInterface {
243 245
 	public function __construct() {
244 246
 		$this->queries = array(
245 247
 			'list_tables'=>'select
246
-					"table_name","table_comment"
248
+					"table_name",\'\' as "table_comment"
247 249
 				from
248 250
 					"information_schema"."tables"
249 251
 				where
@@ -253,7 +255,7 @@ class PostgreSQL implements DatabaseInterface {
253 255
 				from
254 256
 					"information_schema"."tables"
255 257
 				where
256
-					"table_name" like ? and
258
+					"table_name" = ? and
257 259
 					"table_catalog" = ?',
258 260
 			'reflect_pk'=>'select
259 261
 					"column_name"
@@ -316,7 +318,16 @@ class PostgreSQL implements DatabaseInterface {
316 318
 					cub2."table_catalog" = ? and
317 319
 					cua1."table_name" = cub1."table_name" and
318 320
 					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"'
320 331
 		);
321 332
 	}
322 333
 
@@ -494,7 +505,7 @@ class SQLServer implements DatabaseInterface {
494 505
 				FROM
495 506
 					"INFORMATION_SCHEMA"."TABLES"
496 507
 				WHERE
497
-					"TABLE_NAME" LIKE ? AND
508
+					"TABLE_NAME" = ? AND
498 509
 					"TABLE_CATALOG" = ?',
499 510
 			'reflect_pk'=>'SELECT
500 511
 					"COLUMN_NAME"
@@ -557,7 +568,16 @@ class SQLServer implements DatabaseInterface {
557 568
 					cub2."TABLE_CATALOG" = ? AND
558 569
 					cua1."TABLE_NAME" = cub1."TABLE_NAME" AND
559 570
 					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"'
561 581
 		);
562 582
 	}
563 583
 
@@ -905,7 +925,15 @@ class SQLite implements DatabaseInterface {
905 925
 					? like "%" AND
906 926
 					k1."self" = k2."self" AND
907 927
 					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"'
909 937
 		);
910 938
 	}
911 939
 
@@ -2083,7 +2111,6 @@ class PHP_CRUD_API {
2083 2111
 		$tenancy_function = isset($tenancy_function)?$tenancy_function:null;
2084 2112
 		$input_sanitizer = isset($input_sanitizer)?$input_sanitizer:null;
2085 2113
 		$input_validator = isset($input_validator)?$input_validator:null;
2086
-		$extensions = isset($extensions)?$extensions:null;
2087 2114
 		$auto_include = isset($auto_include)?$auto_include:null;
2088 2115
 		$allow_origin = isset($allow_origin)?$allow_origin:null;
2089 2116
 
@@ -2129,9 +2156,6 @@ class PHP_CRUD_API {
2129 2156
 			}
2130 2157
 			$db->connect($hostname,$username,$password,$database,$port,$socket,$charset);
2131 2158
 		}
2132
-		if ($extensions===null) {
2133
-			$extensions = true;
2134
-		}
2135 2159
 		if ($auto_include===null) {
2136 2160
 			$auto_include = true;
2137 2161
 		}
@@ -2140,7 +2164,7 @@ class PHP_CRUD_API {
2140 2164
 		}
2141 2165
 
2142 2166
 		$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');
2144 2168
 	}
2145 2169
 
2146 2170
 	public static function php_crud_api_transform(&$tables) {
@@ -2211,24 +2235,25 @@ class PHP_CRUD_API {
2211 2235
 			$table_fields = $this->findFields($table_list,false,false,false,$database);
2212 2236
 			$table_names = array_map(function($v){ return $v['name'];},$tables);
2213 2237
 
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];
2232 2257
 			}
2233 2258
 
2234 2259
 			foreach (array('root_actions','id_actions') as $path) {
@@ -2353,8 +2378,13 @@ class PHP_CRUD_API {
2353 2378
 							if ($k>0) echo ',';
2354 2379
 							echo '"'.$field.'": {';
2355 2380
 							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);
2358 2388
 							}
2359 2389
 							if (isset($action['fields'][$field]->referenced)) {
2360 2390
 								echo ',"x-referenced": '.json_encode($action['fields'][$field]->referenced);
@@ -2386,8 +2416,13 @@ class PHP_CRUD_API {
2386 2416
 							if ($k>0) echo ',';
2387 2417
 							echo '"'.$field.'": {';
2388 2418
 							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);
2391 2426
 							}
2392 2427
 							if (isset($action['fields'][$field]->referenced)) {
2393 2428
 								echo ',"x-referenced": '.json_encode($action['fields'][$field]->referenced);
@@ -2445,8 +2480,13 @@ class PHP_CRUD_API {
2445 2480
 							if ($k>0) echo ',';
2446 2481
 							echo '"'.$field.'": {';
2447 2482
 							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);
2450 2490
 							}
2451 2491
 							if (isset($action['fields'][$field]->referenced)) {
2452 2492
 								echo ',"x-referenced": '.json_encode($action['fields'][$field]->referenced);
@@ -2475,8 +2515,13 @@ class PHP_CRUD_API {
2475 2515
 							if ($k>0) echo ',';
2476 2516
 							echo '"'.$field.'": {';
2477 2517
 							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);
2480 2525
 							}
2481 2526
 							if (isset($action['fields'][$field]->referenced)) {
2482 2527
 								echo ',"x-referenced": '.json_encode($action['fields'][$field]->referenced);

Loading…
Cancel
Save