Browse Source

Improved compatibility with Windows and MsSQL

Maurits van der Schee 8 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
 			return "'".mysqli_real_escape_string($db,$param)."'";
69
 			return "'".mysqli_real_escape_string($db,$param)."'";
70
 		}, $sql);
70
 		}, $sql);
71
 		//echo "\n$sql\n";
71
 		//echo "\n$sql\n";
72
-		return mysqli_query($db,$sql);
72
+		return mysqli_query($db,$sql);
73
 	}
73
 	}
74
 
74
 
75
 	protected function fetch_assoc($result) {
75
 	protected function fetch_assoc($result) {
404
 		if (strtoupper(substr($sql,0,6))=='INSERT') {
404
 		if (strtoupper(substr($sql,0,6))=='INSERT') {
405
 			$sql .= ';SELECT SCOPE_IDENTITY()';
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
 	protected function fetch_assoc($result) {
410
 	protected function fetch_assoc($result) {
524
 	}
519
 	}
525
 
520
 
526
 	protected function applyInputSanitizer($callback,$action,$database,$table,&$input) {
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
 		if (is_callable($callback,true)) foreach ((array)$input as $key=>$value) {
522
 		if (is_callable($callback,true)) foreach ((array)$input as $key=>$value) {
534
 			$input->$key = $callback($action,$database,$table,$key,$value);
523
 			$input->$key = $callback($action,$database,$table,$key,$value);
535
 		}
524
 		}
536
 	}
525
 	}
537
 	
526
 	
538
 	protected function applyInputValidator($callback,$action,$database,$table,&$input) {
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
 		$errors = array();
528
 		$errors = array();
546
 		if (is_callable($callback,true)) foreach ((array)$input as $key=>$value) {
529
 		if (is_callable($callback,true)) foreach ((array)$input as $key=>$value) {
547
 			$error = $callback($action,$database,$table,$key,$value);
530
 			$error = $callback($action,$database,$table,$key,$value);
603
 	protected function exitWith422($object) {
586
 	protected function exitWith422($object) {
604
 		if (isset($_SERVER['REQUEST_METHOD'])) {
587
 		if (isset($_SERVER['REQUEST_METHOD'])) {
605
 			header('Content-Type:',true,422);
588
 			header('Content-Type:',true,422);
606
-			die('Unprocessable Entity');
589
+			die(json_encode($object));
607
 		} else {
590
 		} else {
608
 			throw new \Exception(json_encode($object));
591
 			throw new \Exception(json_encode($object));
609
 		}
592
 		}
777
 		$input = (object)array();
760
 		$input = (object)array();
778
 		$data = trim(file_get_contents($post));
761
 		$data = trim(file_get_contents($post));
779
 		if (strlen($data)>0) {
762
 		if (strlen($data)>0) {
780
-			if ($data[0]=='{' || $data[0]=='[') {
763
+			if ($data[0]=='{') {
781
 				$input = json_decode($data);
764
 				$input = json_decode($data);
782
 			} else {
765
 			} else {
783
 				parse_str($data, $input);
766
 				parse_str($data, $input);
809
 	}
792
 	}
810
 	
793
 	
811
 	protected function limitInputFields($input,$fields) {
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
 		foreach (array_keys((array)$input) as $key) {
795
 		foreach (array_keys((array)$input) as $key) {
819
 			if (!isset($fields[$key])) {
796
 			if (!isset($fields[$key])) {
820
 				unset($input->$key);
797
 				unset($input->$key);
824
 	}
801
 	}
825
 
802
 
826
 	protected function convertBinary($input,$fields) {
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
 		foreach ($fields as $key=>$field) {
804
 		foreach ($fields as $key=>$field) {
834
 			if (isset($input->$key) && $input->$key && $this->is_binary_type($field)) {
805
 			if (isset($input->$key) && $input->$key && $this->is_binary_type($field)) {
835
 				$data = $input->$key;
806
 				$data = $input->$key;
1045
 		extract($parameters);
1016
 		extract($parameters);
1046
 		if (!$input) $this->exitWith404('input');
1017
 		if (!$input) $this->exitWith404('input');
1047
 		$this->startOutput($callback);
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
 		$this->endOutput($callback);
1020
 		$this->endOutput($callback);
1058
 	}
1021
 	}
1059
 
1022
 

+ 14
- 12
tests/tests.php View File

24
 		$data = 'data://text/plain;base64,'.base64_encode($data);
24
 		$data = 'data://text/plain;base64,'.base64_encode($data);
25
 
25
 
26
 		switch(MySQL_CRUD_API_Config::$dbengine) {
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
 			default:	die("DB engine not supported: $dbengine\n");
30
 			default:	die("DB engine not supported: $dbengine\n");
31
 		}
31
 		}
32
 
32
 
35
 				'username'=>MySQL_CRUD_API_Config::$username,
35
 				'username'=>MySQL_CRUD_API_Config::$username,
36
 				'password'=>MySQL_CRUD_API_Config::$password,
36
 				'password'=>MySQL_CRUD_API_Config::$password,
37
 				'database'=>MySQL_CRUD_API_Config::$database,
37
 				'database'=>MySQL_CRUD_API_Config::$database,
38
-				'charset'=>'utf8',
38
+				'charset'=>$charset,
39
 				// callbacks
39
 				// callbacks
40
 				'table_authorizer'=>function($action,$database,$table) { return true; },
40
 				'table_authorizer'=>function($action,$database,$table) { return true; },
41
 				'column_authorizer'=>function($action,$database,$table,$column) { return $column!='password'; },
41
 				'column_authorizer'=>function($action,$database,$table,$column) { return $column!='password'; },
358
 		$test->expect('{"id":"2","name":"article","icon":null}');
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
 	public function testAddPostFailure()
361
 	public function testAddPostFailure()
369
 	{
362
 	{
370
 		$test = new API($this);
363
 		$test = new API($this);
402
 		$test->expect('{"id":"2","name":"alert();","icon":null}');
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
 	public function testErrorOnDuplicatePrimaryKey()
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