Browse Source

Improved compatibility with Windows and MsSQL

Maurits van der Schee 9 years ago
parent
commit
7f79f914df
2 changed files with 19 additions and 54 deletions
  1. 5
    42
      api.php
  2. 14
    12
      tests/tests.php

+ 5
- 42
api.php View File

@@ -69,7 +69,7 @@ class MySQL_CRUD_API extends REST_CRUD_API {
69 69
 			return "'".mysqli_real_escape_string($db,$param)."'";
70 70
 		}, $sql);
71 71
 		//echo "\n$sql\n";
72
-		return mysqli_query($db,$sql);
72
+		return mysqli_query($db,$sql);
73 73
 	}
74 74
 
75 75
 	protected function fetch_assoc($result) {
@@ -404,12 +404,7 @@ class MsSQL_CRUD_API extends REST_CRUD_API {
404 404
 		if (strtoupper(substr($sql,0,6))=='INSERT') {
405 405
 			$sql .= ';SELECT SCOPE_IDENTITY()';
406 406
 		}
407
-		$result = sqlsrv_query($db,$sql,$args);
408
-		if ($result===false) {
409
-			$errors = sqlsrv_errors();
410
-			$this->exitWith409(compact('sql','args','errors'));
411
-		}
412
-		return $result;
407
+		return sqlsrv_query($db,$sql,$args)?:null;
413 408
 	}
414 409
 
415 410
 	protected function fetch_assoc($result) {
@@ -524,24 +519,12 @@ class REST_CRUD_API {
524 519
 	}
525 520
 
526 521
 	protected function applyInputSanitizer($callback,$action,$database,$table,&$input) {
527
-		if (is_array($input)) {
528
-			foreach (array_keys($input) as $i) {
529
-				$this->applyInputSanitizer($callback,$action,$database,$table,$input[$i]);
530
-			}
531
-			return;
532
-		}		
533 522
 		if (is_callable($callback,true)) foreach ((array)$input as $key=>$value) {
534 523
 			$input->$key = $callback($action,$database,$table,$key,$value);
535 524
 		}
536 525
 	}
537 526
 	
538 527
 	protected function applyInputValidator($callback,$action,$database,$table,&$input) {
539
-		if (is_array($input)) {
540
-			foreach (array_keys($input) as $i) {
541
-				$this->applyInputValidator($callback,$action,$database,$table,$input[$i]);
542
-			}
543
-			return;
544
-		}	
545 528
 		$errors = array();
546 529
 		if (is_callable($callback,true)) foreach ((array)$input as $key=>$value) {
547 530
 			$error = $callback($action,$database,$table,$key,$value);
@@ -603,7 +586,7 @@ class REST_CRUD_API {
603 586
 	protected function exitWith422($object) {
604 587
 		if (isset($_SERVER['REQUEST_METHOD'])) {
605 588
 			header('Content-Type:',true,422);
606
-			die('Unprocessable Entity');
589
+			die(json_encode($object));
607 590
 		} else {
608 591
 			throw new \Exception(json_encode($object));
609 592
 		}
@@ -777,7 +760,7 @@ class REST_CRUD_API {
777 760
 		$input = (object)array();
778 761
 		$data = trim(file_get_contents($post));
779 762
 		if (strlen($data)>0) {
780
-			if ($data[0]=='{' || $data[0]=='[') {
763
+			if ($data[0]=='{') {
781 764
 				$input = json_decode($data);
782 765
 			} else {
783 766
 				parse_str($data, $input);
@@ -809,12 +792,6 @@ class REST_CRUD_API {
809 792
 	}
810 793
 	
811 794
 	protected function limitInputFields($input,$fields) {
812
-		if (is_array($input)) {
813
-			foreach (array_keys($input) as $i) {
814
-				$input[$i] = $this->limitInputFields($input[$i],$fields);
815
-			}
816
-			return $input;
817
-		}
818 795
 		foreach (array_keys((array)$input) as $key) {
819 796
 			if (!isset($fields[$key])) {
820 797
 				unset($input->$key);
@@ -824,12 +801,6 @@ class REST_CRUD_API {
824 801
 	}
825 802
 
826 803
 	protected function convertBinary($input,$fields) {
827
-		if (is_array($input)) {
828
-			foreach (array_keys($input) as $i) {
829
-				$input[$i] = $this->convertBinary($input[$i],$fields);
830
-			}
831
-			return $input;
832
-		}
833 804
 		foreach ($fields as $key=>$field) {
834 805
 			if (isset($input->$key) && $input->$key && $this->is_binary_type($field)) {
835 806
 				$data = $input->$key;
@@ -1045,15 +1016,7 @@ class REST_CRUD_API {
1045 1016
 		extract($parameters);
1046 1017
 		if (!$input) $this->exitWith404('input');
1047 1018
 		$this->startOutput($callback);
1048
-		if (!is_array($input)) {
1049
-			echo json_encode($this->createObject($input,$table,$db));
1050
-		} else {
1051
-			$result = array();
1052
-			foreach ($input as $i) {
1053
-				$result[] = $this->createObject($i,$table,$db);
1054
-			}
1055
-			echo json_encode($result);
1056
-		}
1019
+		echo json_encode($this->createObject($input,$table,$db));
1057 1020
 		$this->endOutput($callback);
1058 1021
 	}
1059 1022
 

+ 14
- 12
tests/tests.php View File

@@ -24,9 +24,9 @@ class API
24 24
 		$data = 'data://text/plain;base64,'.base64_encode($data);
25 25
 
26 26
 		switch(MySQL_CRUD_API_Config::$dbengine) {
27
-			case 'mssql':	$class = 'MsSQL_CRUD_API'; break;
28
-			case 'pgsql':	$class = 'PgSQL_CRUD_API'; break;
29
-			case 'mysql':	$class = 'MySQL_CRUD_API'; break;
27
+			case 'mssql':	$class = 'MsSQL_CRUD_API'; $charset = 'utf-8'; break;
28
+			case 'pgsql':	$class = 'PgSQL_CRUD_API'; $charset = 'utf8'; break;
29
+			case 'mysql':	$class = 'MySQL_CRUD_API'; $charset = 'utf8'; break;
30 30
 			default:	die("DB engine not supported: $dbengine\n");
31 31
 		}
32 32
 
@@ -35,7 +35,7 @@ class API
35 35
 				'username'=>MySQL_CRUD_API_Config::$username,
36 36
 				'password'=>MySQL_CRUD_API_Config::$password,
37 37
 				'database'=>MySQL_CRUD_API_Config::$database,
38
-				'charset'=>'utf8',
38
+				'charset'=>$charset,
39 39
 				// callbacks
40 40
 				'table_authorizer'=>function($action,$database,$table) { return true; },
41 41
 				'column_authorizer'=>function($action,$database,$table,$column) { return $column!='password'; },
@@ -358,13 +358,6 @@ class MySQL_CRUD_API_Test extends PHPUnit_Framework_TestCase
358 358
 		$test->expect('{"id":"2","name":"article","icon":null}');
359 359
 	}
360 360
 
361
-	public function testAddPosts()
362
-	{
363
-		$test = new API($this);
364
-		$test->post('/posts','[{"user_id":"1","category_id":"1","content":"tests"},{"user_id":"1","category_id":"1","content":"tests"}]');
365
-		$test->expect('[15,16]');
366
-	}
367
-
368 361
 	public function testAddPostFailure()
369 362
 	{
370 363
 		$test = new API($this);
@@ -402,8 +395,17 @@ class MySQL_CRUD_API_Test extends PHPUnit_Framework_TestCase
402 395
 		$test->expect('{"id":"2","name":"alert();","icon":null}');
403 396
 	}
404 397
 	
398
+	public function testErrorOnInvalidJson()
399
+	{
400
+		$test = new API($this);
401
+		$test->post('/posts','{"}');
402
+		$test->expect('Not found (input)');
403
+	}
404
+		
405 405
 	public function testErrorOnDuplicatePrimaryKey()
406 406
 	{
407
-		// to be implemented
407
+		$test = new API($this);
408
+		$test->post('/posts','{"id":"1","user_id":"1","category_id":"1","content":"blog started (duplicate)"}');
409
+		$test->expect('null');
408 410
 	}
409 411
 }

Loading…
Cancel
Save