Przeglądaj źródła

Mutiple filter support

Maurits van der Schee 10 lat temu
rodzic
commit
5de7978ed0
1 zmienionych plików z 51 dodań i 30 usunięć
  1. 51
    30
      api.php

+ 51
- 30
api.php Wyświetl plik

@@ -98,6 +98,7 @@ class MySQL_CRUD_API extends REST_CRUD_API {
98 98
 	}
99 99
 	
100 100
 	protected function is_binary_type($field) {
101
+		//echo "$field->name: $field->type ($field->flags)\n";
101 102
 		return ($field->flags & 128);
102 103
 	}
103 104
 
@@ -247,6 +248,17 @@ class REST_CRUD_API {
247 248
 		return $characters?preg_replace("/[^$characters]/",'',$value):$value;
248 249
 	}
249 250
 
251
+	protected function parseGetParameterArray($get,$name,$characters,$default) {
252
+		$values = isset($get[$name])?$get[$name]:$default;
253
+		if (!is_array($values)) $values = array($values);
254
+		if ($characters) {
255
+			foreach ($values as &$value) {
256
+				$value = preg_replace("/[^$characters]/",'',$value);
257
+			}
258
+		}
259
+		return $values;
260
+	}
261
+	
250 262
 	protected function applyPermissions($database, $tables, $action, $permissions, $multidb) {
251 263
 		if (in_array(strtolower($database), array('information_schema','mysql','sys'))) return array();
252 264
 		$results = array();
@@ -330,22 +342,24 @@ class REST_CRUD_API {
330 342
 		return $order;
331 343
 	}
332 344
 
333
-	protected function processFilterParameter($filter,$match,$db) {
345
+	protected function processFilterParameter($filter,$db) {
334 346
 		if ($filter) {
335
-			$filter = explode(':',$filter,2);
336
-			if (count($filter)==2) {
337
-				$filter[2] = 'LIKE';
338
-				if ($match=='contain') $filter[1] = '%'.addcslashes($filter[1], '%_').'%';
339
-				if ($match=='start') $filter[1] = addcslashes($filter[1], '%_').'%';
340
-				if ($match=='end') $filter[1] = '%'.addcslashes($filter[1], '%_');
341
-				if ($match=='exact') $filter[2] = '=';
342
-				if ($match=='lower') $filter[2] = '<';
343
-				if ($match=='upto') $filter[2] = '<=';
344
-				if ($match=='from') $filter[2] = '>=';
345
-				if ($match=='higher') $filter[2] = '>';
347
+			$filter = explode(',',$filter,3);
348
+			if (count($filter)==3) {
349
+				$match = $filter[1];
350
+				$filter[1] = 'LIKE';
351
+				if ($match=='cs') $filter[2] = '%'.addcslashes($filter[2], '%_').'%';
352
+				if ($match=='sw') $filter[2] = addcslashes($filter[2], '%_').'%';
353
+				if ($match=='ew') $filter[2] = '%'.addcslashes($filter[2], '%_');
354
+				if ($match=='eq') $filter[1] = '=';
355
+				if ($match=='ne') $filter[1] = '!=';
356
+				if ($match=='lt') $filter[1] = '<';
357
+				if ($match=='le') $filter[1] = '<=';
358
+				if ($match=='ge') $filter[1] = '>=';
359
+				if ($match=='gt') $filter[1] = '>';
346 360
 				if ($match=='in') {
347
-					$filter[2] = 'IN';
348
-					$filter[1] = explode(',',$filter[1]);
361
+					$filter[1] = 'IN';
362
+					$filter[2] = explode(',',$filter[2]);
349 363
 
350 364
 				}
351 365
 			} else {
@@ -442,14 +456,15 @@ class REST_CRUD_API {
442 456
 		$action    = $this->mapMethodToAction($method,$key);
443 457
 		$callback  = $this->parseGetParameter($get, 'callback', 'a-zA-Z0-9\-_', false);
444 458
 		$page      = $this->parseGetParameter($get, 'page', '0-9,', false);
445
-		$filter    = $this->parseGetParameter($get, 'filter', false, false);
446
-		$match     = $this->parseGetParameter($get, 'match', 'a-z', 'exact');
459
+		$filters   = $this->parseGetParameterArray($get, 'filter', false, false);
447 460
 		$order     = $this->parseGetParameter($get, 'order', 'a-zA-Z0-9\-_*,', false);
448 461
 		$transform = $this->parseGetParameter($get, 'transform', '1', false);
449 462
 
450 463
 		$table    = $this->processTableParameter($database,$table,$db);
451 464
 		$key      = $this->processKeyParameter($key,$table,$database,$db);
452
-		$filter   = $this->processFilterParameter($filter,$match,$db);
465
+		foreach ($filters as &$filter) {
466
+			$filter   = $this->processFilterParameter($filter,$match,$db);
467
+		}
453 468
 		$page     = $this->processPageParameter($page);
454 469
 		$order    = $this->processOrderParameter($order,$table,$database,$db);
455 470
 
@@ -461,7 +476,7 @@ class REST_CRUD_API {
461 476
 
462 477
 		list($collect,$select) = $this->findRelations($table,$database,$db);
463 478
 
464
-		return compact('action','database','table','key','callback','page','filter','match','order','transform','db','object','input','collect','select');
479
+		return compact('action','database','table','key','callback','page','filters','match','order','transform','db','object','input','collect','select');
465 480
 	}
466 481
 
467 482
 	protected function listCommand($parameters) {
@@ -477,11 +492,14 @@ class REST_CRUD_API {
477 492
 			$params = array();
478 493
 			$sql = 'SELECT COUNT(*) FROM "!"';
479 494
 			$params[] = $table;
480
-			if (is_array($filter)) {
481
-				$sql .= ' WHERE "!" ! ?';
482
-				$params[] = $filter[0];
483
-				$params[] = $filter[2];
484
-				$params[] = $filter[1];
495
+			foreach ($filters as $i=>$filter) {
496
+				if (is_array($filter)) {
497
+					$sql .= $i==0?' WHERE ':' AND ';
498
+					$sql .= '"!" ! ?';
499
+					$params[] = $filter[0];
500
+					$params[] = $filter[1];
501
+					$params[] = $filter[2];
502
+				}
485 503
 			}
486 504
 			if ($result = $this->query($db,$sql,$params)) {
487 505
 				while ($pages = $this->fetch_row($result)) {
@@ -492,11 +510,14 @@ class REST_CRUD_API {
492 510
 		$params = array();
493 511
 		$sql = 'SELECT * FROM "!"';
494 512
 		$params[] = $table;
495
-		if (is_array($filter)) {
496
-			$sql .= ' WHERE "!" ! ?';
497
-			$params[] = $filter[0];
498
-			$params[] = $filter[2];
499
-			$params[] = $filter[1];
513
+		foreach ($filters as $i=>$filter) {
514
+			if (is_array($filter)) {
515
+				$sql .= $i==0?' WHERE ':' AND ';
516
+				$sql .= '"!" ! ?';
517
+				$params[] = $filter[0];
518
+				$params[] = $filter[1];
519
+				$params[] = $filter[2];
520
+			}
500 521
 		}
501 522
 		if (is_array($order)) {
502 523
 			$sql .= ' ORDER BY "!" !';
@@ -564,10 +585,10 @@ class REST_CRUD_API {
564 585
 					else echo ',';
565 586
 					echo '"'.$field.'":"'.implode('.',$path).'"';
566 587
 				}
567
-				echo '},';
588
+				echo '}';
568 589
 			}
569 590
 			if ($result = $this->query($db,$sql,$params)) {
570
-				echo '"columns":';
591
+				echo ',"columns":';
571 592
 				$fields = array();
572 593
 				$base64 = array();
573 594
 				foreach ($this->fetch_fields($result) as $field) {

Loading…
Anuluj
Zapisz