Browse Source

Refactor database drivers

Maurits van der Schee 8 years ago
parent
commit
d8cb1f717a
1 changed files with 75 additions and 73 deletions
  1. 75
    73
      api.php

+ 75
- 73
api.php View File

550
 
550
 
551
 class PHP_CRUD_API {
551
 class PHP_CRUD_API {
552
 
552
 
553
+	protected $db;
553
 	protected $settings;
554
 	protected $settings;
554
 
555
 
555
 	protected function mapMethodToAction($method,$key) {
556
 	protected function mapMethodToAction($method,$key) {
598
 		}
599
 		}
599
 	}
600
 	}
600
 
601
 
601
-	protected function applyRecordFilter($callback,$action,$database,$tables,&$filters,$db) {
602
+	protected function applyRecordFilter($callback,$action,$database,$tables,&$filters) {
602
 		if (is_callable($callback,true)) foreach ($tables as $i=>$table) {
603
 		if (is_callable($callback,true)) foreach ($tables as $i=>$table) {
603
-			$f = $this->convertFilters($db,$callback($action,$database,$table));
604
+			$f = $this->convertFilters($callback($action,$database,$table));
604
 			if ($f) {
605
 			if ($f) {
605
 				if (!isset($filters[$table])) $filters[$table] = array();
606
 				if (!isset($filters[$table])) $filters[$table] = array();
606
 				if (!isset($filters[$table]['and'])) $filters[$table]['and'] = array();
607
 				if (!isset($filters[$table]['and'])) $filters[$table]['and'] = array();
669
 		if (!empty($errors)) $this->exitWith422($errors);
670
 		if (!empty($errors)) $this->exitWith422($errors);
670
 	}
671
 	}
671
 
672
 
672
-	protected function processTablesParameter($database,$tables,$action,$db) {
673
+	protected function processTablesParameter($database,$tables,$action) {
673
 		$blacklist = array('information_schema','mysql','sys','pg_catalog');
674
 		$blacklist = array('information_schema','mysql','sys','pg_catalog');
674
 		if (in_array(strtolower($database), $blacklist)) return array();
675
 		if (in_array(strtolower($database), $blacklist)) return array();
675
 		$table_array = explode(',',$tables);
676
 		$table_array = explode(',',$tables);
676
 		$table_list = array();
677
 		$table_list = array();
677
 		foreach ($table_array as $table) {
678
 		foreach ($table_array as $table) {
678
-			if ($result = $db->query($db->get_sql('reflect_table'),array($table,$database))) {
679
-				while ($row = $db->fetch_row($result)) $table_list[] = $row[0];
680
-				$db->close($result);
679
+			if ($result = $this->db->query($this->db->get_sql('reflect_table'),array($table,$database))) {
680
+				while ($row = $this->db->fetch_row($result)) $table_list[] = $row[0];
681
+				$this->db->close($result);
681
 				if ($action!='list') break;
682
 				if ($action!='list') break;
682
 			}
683
 			}
683
 		}
684
 		}
734
 		}
735
 		}
735
 	}
736
 	}
736
 
737
 
737
-	protected function processKeyParameter($key,$tables,$database,$db) {
738
+	protected function processKeyParameter($key,$tables,$database) {
738
 		if (!$key) return false;
739
 		if (!$key) return false;
739
 		$count = 0;
740
 		$count = 0;
740
 		$field = false;
741
 		$field = false;
741
-		if ($result = $db->query($db->get_sql('reflect_pk'),array($tables[0],$database))) {
742
-			while ($row = $db->fetch_row($result)) {
742
+		if ($result = $this->db->query($this->db->get_sql('reflect_pk'),array($tables[0],$database))) {
743
+			while ($row = $this->db->fetch_row($result)) {
743
 				$count++;
744
 				$count++;
744
 				$field = $row[0];
745
 				$field = $row[0];
745
 			}
746
 			}
746
-			$db->close($result);
747
+			$this->db->close($result);
747
 		}
748
 		}
748
 		if ($count!=1 || $field==false) $this->exitWith404('1pk');
749
 		if ($count!=1 || $field==false) $this->exitWith404('1pk');
749
 		return array($key,$field);
750
 		return array($key,$field);
758
 		return $order;
759
 		return $order;
759
 	}
760
 	}
760
 
761
 
761
-	protected function convertFilter($db, $field, $comparator, $value) {
762
+	protected function convertFilter($field, $comparator, $value) {
762
 		switch (strtolower($comparator)) {
763
 		switch (strtolower($comparator)) {
763
-			case 'cs': $comparator = 'LIKE'; $value = '%'.$db->likeEscape($value).'%'; break;
764
-			case 'sw': $comparator = 'LIKE'; $value = $db->likeEscape($value).'%'; break;
765
-			case 'ew': $comparator = 'LIKE'; $value = '%'.$db->likeEscape($value); break;
764
+			case 'cs': $comparator = 'LIKE'; $value = '%'.$this->db->likeEscape($value).'%'; break;
765
+			case 'sw': $comparator = 'LIKE'; $value = $this->db->likeEscape($value).'%'; break;
766
+			case 'ew': $comparator = 'LIKE'; $value = '%'.$this->db->likeEscape($value); break;
766
 			case 'eq': $comparator = '='; break;
767
 			case 'eq': $comparator = '='; break;
767
 			case 'ne': $comparator = '<>'; break;
768
 			case 'ne': $comparator = '<>'; break;
768
 			case 'lt': $comparator = '<'; break;
769
 			case 'lt': $comparator = '<'; break;
774
 		return array($field, $comparator, $value);
775
 		return array($field, $comparator, $value);
775
 	}
776
 	}
776
 
777
 
777
-	protected function convertFilters($db,$filters) {
778
+	protected function convertFilters($filters) {
778
 		$result = array();
779
 		$result = array();
779
 		if ($filters) {
780
 		if ($filters) {
780
 			for ($i=0;$i<count($filters);$i++) {
781
 			for ($i=0;$i<count($filters);$i++) {
781
 				$filter = explode(',',$filters[$i],3);
782
 				$filter = explode(',',$filters[$i],3);
782
 				if (count($filter)==3) {
783
 				if (count($filter)==3) {
783
-					$result[] = $this->convertFilter($db,$filter[0],$filter[1],$filter[2]);
784
+					$result[] = $this->convertFilter($filter[0],$filter[1],$filter[2]);
784
 				}
785
 				}
785
 			}
786
 			}
786
 		}
787
 		}
787
 		return $result;
788
 		return $result;
788
 	}
789
 	}
789
 
790
 
790
-	protected function processFiltersParameter($tables,$satisfy,$filters,$db) {
791
-		$result = $this->convertFilters($db, $filters);
791
+	protected function processFiltersParameter($tables,$satisfy,$filters) {
792
+		$result = $this->convertFilters($filters);
792
 		if (!$result) return array();
793
 		if (!$result) return array();
793
 		$and = ($satisfy && strtolower($satisfy)=='any')?'or':'and';
794
 		$and = ($satisfy && strtolower($satisfy)=='any')?'or':'and';
794
 		return array($tables[0]=>array($and=>$result));
795
 		return array($tables[0]=>array($and=>$result));
802
 		return $page;
803
 		return $page;
803
 	}
804
 	}
804
 
805
 
805
-	protected function retrieveObject($key,$fields,$filters,$tables,$db) {
806
+	protected function retrieveObject($key,$fields,$filters,$tables) {
806
 		if (!$key) return false;
807
 		if (!$key) return false;
807
 		$table = $tables[0];
808
 		$table = $tables[0];
808
 		$sql = 'SELECT ';
809
 		$sql = 'SELECT ';
813
 		if (!isset($filters[$table]['or'])) $filters[$table]['or'] = array();
814
 		if (!isset($filters[$table]['or'])) $filters[$table]['or'] = array();
814
 		$filters[$table]['or'][] = array($key[1],'=',$key[0]);
815
 		$filters[$table]['or'][] = array($key[1],'=',$key[0]);
815
 		$this->addWhereFromFilters($filters[$table],$sql,$params);
816
 		$this->addWhereFromFilters($filters[$table],$sql,$params);
816
-		if ($result = $db->query($sql,$params)) {
817
-			$object = $db->fetch_assoc($result);
817
+		if ($result = $this->db->query($sql,$params)) {
818
+			$object = $this->db->fetch_assoc($result);
818
 			foreach ($fields[$table] as $field) {
819
 			foreach ($fields[$table] as $field) {
819
-				if ($db->is_binary_type($field) && $object[$field->name]) {
820
-					$object[$field->name] = $db->base64_encode($object[$field->name]);
820
+				if ($this->db->is_binary_type($field) && $object[$field->name]) {
821
+					$object[$field->name] = $this->db->base64_encode($object[$field->name]);
821
 				}
822
 				}
822
 			}
823
 			}
823
-			$db->close($result);
824
+			$this->db->close($result);
824
 		}
825
 		}
825
 		return $object;
826
 		return $object;
826
 	}
827
 	}
827
 
828
 
828
-	protected function createObject($input,$tables,$db) {
829
+	protected function createObject($input,$tables) {
829
 		if (!$input) return false;
830
 		if (!$input) return false;
830
 		$input = (array)$input;
831
 		$input = (array)$input;
831
 		$keys = implode('","',str_split(str_repeat('!', count($input))));
832
 		$keys = implode('","',str_split(str_repeat('!', count($input))));
832
 		$values = implode(',',str_split(str_repeat('?', count($input))));
833
 		$values = implode(',',str_split(str_repeat('?', count($input))));
833
 		$params = array_merge(array_keys($input),array_values($input));
834
 		$params = array_merge(array_keys($input),array_values($input));
834
 		array_unshift($params, $tables[0]);
835
 		array_unshift($params, $tables[0]);
835
-		$result = $db->query('INSERT INTO "!" ("'.$keys.'") VALUES ('.$values.')',$params);
836
+		$result = $this->db->query('INSERT INTO "!" ("'.$keys.'") VALUES ('.$values.')',$params);
836
 		if (!$result) return null;
837
 		if (!$result) return null;
837
-		return $db->insert_id($result);
838
+		return $this->db->insert_id($result);
838
 	}
839
 	}
839
 
840
 
840
-	protected function updateObject($key,$input,$filters,$tables,$db) {
841
+	protected function updateObject($key,$input,$filters,$tables) {
841
 		if (!$input) return false;
842
 		if (!$input) return false;
842
 		$input = (array)$input;
843
 		$input = (array)$input;
843
 		$table = $tables[0];
844
 		$table = $tables[0];
854
 		if (!isset($filters[$table]['or'])) $filters[$table]['or'] = array();
855
 		if (!isset($filters[$table]['or'])) $filters[$table]['or'] = array();
855
 		$filters[$table]['or'][] = array($key[1],'=',$key[0]);
856
 		$filters[$table]['or'][] = array($key[1],'=',$key[0]);
856
 		$this->addWhereFromFilters($filters[$table],$sql,$params);
857
 		$this->addWhereFromFilters($filters[$table],$sql,$params);
857
-		$result = $db->query($sql,$params);
858
-		return $db->affected_rows($result);
858
+		$result = $this->db->query($sql,$params);
859
+		return $this->db->affected_rows($result);
859
 	}
860
 	}
860
 
861
 
861
-	protected function deleteObject($key,$filters,$tables,$db) {
862
+	protected function deleteObject($key,$filters,$tables) {
862
 		$table = $tables[0];
863
 		$table = $tables[0];
863
 		$sql = 'DELETE FROM "!"';
864
 		$sql = 'DELETE FROM "!"';
864
 		$params = array($table);
865
 		$params = array($table);
866
 		if (!isset($filters[$table]['or'])) $filters[$table]['or'] = array();
867
 		if (!isset($filters[$table]['or'])) $filters[$table]['or'] = array();
867
 		$filters[$table]['or'][] = array($key[1],'=',$key[0]);
868
 		$filters[$table]['or'][] = array($key[1],'=',$key[0]);
868
 		$this->addWhereFromFilters($filters[$table],$sql,$params);
869
 		$this->addWhereFromFilters($filters[$table],$sql,$params);
869
-		$result = $db->query($sql,$params);
870
-		return $db->affected_rows($result);
870
+		$result = $this->db->query($sql,$params);
871
+		return $this->db->affected_rows($result);
871
 	}
872
 	}
872
 
873
 
873
-	protected function findRelations($tables,$database,$db) {
874
+	protected function findRelations($tables,$database) {
874
 		$tableset = array();
875
 		$tableset = array();
875
 		$collect = array();
876
 		$collect = array();
876
 		$select = array();
877
 		$select = array();
879
 			$table0 = array_shift($tables);
880
 			$table0 = array_shift($tables);
880
 			$tableset[] = $table0;
881
 			$tableset[] = $table0;
881
 
882
 
882
-			$result = $db->query($db->get_sql('reflect_belongs_to'),array($table0,$tables,$database,$database));
883
-			while ($row = $db->fetch_row($result)) {
883
+			$result = $this->db->query($this->db->get_sql('reflect_belongs_to'),array($table0,$tables,$database,$database));
884
+			while ($row = $this->db->fetch_row($result)) {
884
 				$collect[$row[0]][$row[1]]=array();
885
 				$collect[$row[0]][$row[1]]=array();
885
 				$select[$row[2]][$row[3]]=array($row[0],$row[1]);
886
 				$select[$row[2]][$row[3]]=array($row[0],$row[1]);
886
 				if (!in_array($row[0],$tableset)) $tableset[] = $row[0];
887
 				if (!in_array($row[0],$tableset)) $tableset[] = $row[0];
887
 			}
888
 			}
888
-			$result = $db->query($db->get_sql('reflect_has_many'),array($tables,$table0,$database,$database));
889
-			while ($row = $db->fetch_row($result)) {
889
+			$result = $this->db->query($this->db->get_sql('reflect_has_many'),array($tables,$table0,$database,$database));
890
+			while ($row = $this->db->fetch_row($result)) {
890
 				$collect[$row[2]][$row[3]]=array();
891
 				$collect[$row[2]][$row[3]]=array();
891
 				$select[$row[0]][$row[1]]=array($row[2],$row[3]);
892
 				$select[$row[0]][$row[1]]=array($row[2],$row[3]);
892
 				if (!in_array($row[2],$tableset)) $tableset[] = $row[2];
893
 				if (!in_array($row[2],$tableset)) $tableset[] = $row[2];
893
 			}
894
 			}
894
-			$result = $db->query($db->get_sql('reflect_habtm'),array($database,$database,$database,$database,$table0,$tables));
895
-			while ($row = $db->fetch_row($result)) {
895
+			$result = $this->db->query($this->db->get_sql('reflect_habtm'),array($database,$database,$database,$database,$table0,$tables));
896
+			while ($row = $this->db->fetch_row($result)) {
896
 				$collect[$row[2]][$row[3]]=array();
897
 				$collect[$row[2]][$row[3]]=array();
897
 				$select[$row[0]][$row[1]]=array($row[2],$row[3]);
898
 				$select[$row[0]][$row[1]]=array($row[2],$row[3]);
898
 				$collect[$row[4]][$row[5]]=array();
899
 				$collect[$row[4]][$row[5]]=array();
925
 		return $input;
926
 		return $input;
926
 	}
927
 	}
927
 
928
 
928
-	protected function findFields($tables,$columns,$database,$db) {
929
+	protected function findFields($tables,$columns,$database) {
929
 		$fields = array();
930
 		$fields = array();
930
 		foreach ($tables as $i=>$table) {
931
 		foreach ($tables as $i=>$table) {
931
-			$fields[$table] = $this->findTableFields($table,$database,$db);
932
+			$fields[$table] = $this->findTableFields($table,$database);
932
 			if ($i==0) $fields[$table] = $this->filterFieldsByColumns($fields[$table],$columns);
933
 			if ($i==0) $fields[$table] = $this->filterFieldsByColumns($fields[$table],$columns);
933
 		}
934
 		}
934
 		return $fields;
935
 		return $fields;
946
 		return $fields;
947
 		return $fields;
947
 	}
948
 	}
948
 
949
 
949
-	protected function findTableFields($table,$database,$db) {
950
+	protected function findTableFields($table,$database) {
950
 		$fields = array();
951
 		$fields = array();
951
-		$result = $db->query('SELECT * FROM "!" WHERE 1=2;',array($table));
952
-		foreach ($db->fetch_fields($result) as $field) {
952
+		$result = $this->db->query('SELECT * FROM "!" WHERE 1=2;',array($table));
953
+		foreach ($this->db->fetch_fields($result) as $field) {
953
 			$fields[$field->name] = $field;
954
 			$fields[$field->name] = $field;
954
 		}
955
 		}
955
 		return $fields;
956
 		return $fields;
964
 		return $input;
965
 		return $input;
965
 	}
966
 	}
966
 
967
 
967
-	protected function convertBinary(&$input,$keys,$db) {
968
+	protected function convertBinary(&$input,$keys) {
968
 		foreach ($keys as $key=>$field) {
969
 		foreach ($keys as $key=>$field) {
969
-			if (isset($input->$key) && $input->$key && $db->is_binary_type($field)) {
970
+			if (isset($input->$key) && $input->$key && $this->db->is_binary_type($field)) {
970
 				$data = $input->$key;
971
 				$data = $input->$key;
971
 				$data = str_pad(strtr($data, '-_', '+/'), strlen($data) % 4, '=', STR_PAD_RIGHT);
972
 				$data = str_pad(strtr($data, '-_', '+/'), strlen($data) % 4, '=', STR_PAD_RIGHT);
972
 				$input->$key = (object)array('type'=>'base64','data'=>$data);
973
 				$input->$key = (object)array('type'=>'base64','data'=>$data);
988
 		$order     = $this->parseGetParameter($get, 'order', 'a-zA-Z0-9\-_,');
989
 		$order     = $this->parseGetParameter($get, 'order', 'a-zA-Z0-9\-_,');
989
 		$transform = $this->parseGetParameter($get, 'transform', '1');
990
 		$transform = $this->parseGetParameter($get, 'transform', '1');
990
 
991
 
991
-		$tables    = $this->processTablesParameter($database,$tables,$action,$db);
992
-		$key       = $this->processKeyParameter($key,$tables,$database,$db);
993
-		$filters   = $this->processFiltersParameter($tables,$satisfy,$filters, $db);
992
+		$tables    = $this->processTablesParameter($database,$tables,$action);
993
+		$key       = $this->processKeyParameter($key,$tables,$database);
994
+		$filters   = $this->processFiltersParameter($tables,$satisfy,$filters);
994
 		$page      = $this->processPageParameter($page);
995
 		$page      = $this->processPageParameter($page);
995
 		$order     = $this->processOrderParameter($order);
996
 		$order     = $this->processOrderParameter($order);
996
 
997
 
997
 		// reflection
998
 		// reflection
998
-		list($tables,$collect,$select) = $this->findRelations($tables,$database,$db);
999
-		$fields = $this->findFields($tables,$columns,$database,$db);
999
+		list($tables,$collect,$select) = $this->findRelations($tables,$database);
1000
+		$fields = $this->findFields($tables,$columns,$database);
1000
 
1001
 
1001
 		// permissions
1002
 		// permissions
1002
 		if ($table_authorizer) $this->applyTableAuthorizer($table_authorizer,$action,$database,$tables);
1003
 		if ($table_authorizer) $this->applyTableAuthorizer($table_authorizer,$action,$database,$tables);
1003
-		if ($record_filter) $this->applyRecordFilter($record_filter,$action,$database,$tables,$filters,$db);
1004
+		if ($record_filter) $this->applyRecordFilter($record_filter,$action,$database,$tables,$filters);
1004
 		if ($column_authorizer) $this->applyColumnAuthorizer($column_authorizer,$action,$database,$fields);
1005
 		if ($column_authorizer) $this->applyColumnAuthorizer($column_authorizer,$action,$database,$fields);
1005
 		if ($tenancy_function) $this->applyTenancyFunction($tenancy_function,$action,$database,$fields,$filters);
1006
 		if ($tenancy_function) $this->applyTenancyFunction($tenancy_function,$action,$database,$fields,$filters);
1006
 
1007
 
1013
 			if ($input_sanitizer) $this->applyInputSanitizer($input_sanitizer,$action,$database,$tables[0],$input,$fields[$tables[0]]);
1014
 			if ($input_sanitizer) $this->applyInputSanitizer($input_sanitizer,$action,$database,$tables[0],$input,$fields[$tables[0]]);
1014
 			if ($input_validator) $this->applyInputValidator($input_validator,$action,$database,$tables[0],$input,$fields[$tables[0]],$context);
1015
 			if ($input_validator) $this->applyInputValidator($input_validator,$action,$database,$tables[0],$input,$fields[$tables[0]],$context);
1015
 
1016
 
1016
-			$this->convertBinary($input,$fields[$tables[0]],$db);
1017
+			$this->convertBinary($input,$fields[$tables[0]]);
1017
 		}
1018
 		}
1018
 
1019
 
1019
-		return compact('action','database','tables','key','callback','page','filters','fields','order','transform','db','input','collect','select');
1020
+		return compact('action','database','tables','key','callback','page','filters','fields','order','transform','input','collect','select');
1020
 	}
1021
 	}
1021
 
1022
 
1022
 	protected function addWhereFromFilters($filters,&$sql,&$params) {
1023
 	protected function addWhereFromFilters($filters,&$sql,&$params) {
1059
 			if (isset($filters[$table])) {
1060
 			if (isset($filters[$table])) {
1060
 					$this->addWhereFromFilters($filters[$table],$sql,$params);
1061
 					$this->addWhereFromFilters($filters[$table],$sql,$params);
1061
 			}
1062
 			}
1062
-			if ($result = $db->query($sql,$params)) {
1063
-				while ($pages = $db->fetch_row($result)) {
1063
+			if ($result = $this->db->query($sql,$params)) {
1064
+				while ($pages = $this->db->fetch_row($result)) {
1064
 					$count = $pages[0];
1065
 					$count = $pages[0];
1065
 				}
1066
 				}
1066
 			}
1067
 			}
1079
 			$params[] = $order[1];
1080
 			$params[] = $order[1];
1080
 		}
1081
 		}
1081
 		if (is_array($order) && is_array($page)) {
1082
 		if (is_array($order) && is_array($page)) {
1082
-			$sql = $db->add_limit_to_sql($sql,$page[1],$page[0]);
1083
+			$sql = $this->db->add_limit_to_sql($sql,$page[1],$page[0]);
1083
 		}
1084
 		}
1084
-		if ($result = $db->query($sql,$params)) {
1085
+		if ($result = $this->db->query($sql,$params)) {
1085
 			echo '"columns":';
1086
 			echo '"columns":';
1086
 			$keys = array();
1087
 			$keys = array();
1087
 			$base64 = array();
1088
 			$base64 = array();
1088
 			foreach ($fields[$table] as $field) {
1089
 			foreach ($fields[$table] as $field) {
1089
-				$base64[] = $db->is_binary_type($field);
1090
+				$base64[] = $this->db->is_binary_type($field);
1090
 				$keys[] = $field->name;
1091
 				$keys[] = $field->name;
1091
 			}
1092
 			}
1092
 			echo json_encode($keys);
1093
 			echo json_encode($keys);
1093
 			$keys = array_flip($keys);
1094
 			$keys = array_flip($keys);
1094
 			echo ',"records":[';
1095
 			echo ',"records":[';
1095
 			$first_row = true;
1096
 			$first_row = true;
1096
-			while ($row = $db->fetch_row($result)) {
1097
+			while ($row = $this->db->fetch_row($result)) {
1097
 				if ($first_row) $first_row = false;
1098
 				if ($first_row) $first_row = false;
1098
 				else echo ',';
1099
 				else echo ',';
1099
 				if (isset($collect[$table])) {
1100
 				if (isset($collect[$table])) {
1103
 				}
1104
 				}
1104
 				foreach ($base64 as $k=>$v) {
1105
 				foreach ($base64 as $k=>$v) {
1105
 					if ($v && $row[$k]) {
1106
 					if ($v && $row[$k]) {
1106
-						$row[$k] = $db->base64_encode($row[$k]);
1107
+						$row[$k] = $this->db->base64_encode($row[$k]);
1107
 					}
1108
 					}
1108
 				}
1109
 				}
1109
 				echo json_encode($row);
1110
 				echo json_encode($row);
1110
 			}
1111
 			}
1111
-			$db->close($result);
1112
+			$this->db->close($result);
1112
 			echo ']';
1113
 			echo ']';
1113
 			if ($count) echo ',';
1114
 			if ($count) echo ',';
1114
 		}
1115
 		}
1138
 				echo '}';
1139
 				echo '}';
1139
 				$this->addWhereFromFilters($filters[$table],$sql,$params);
1140
 				$this->addWhereFromFilters($filters[$table],$sql,$params);
1140
 			}
1141
 			}
1141
-			if ($result = $db->query($sql,$params)) {
1142
+			if ($result = $this->db->query($sql,$params)) {
1142
 				if (isset($select[$table])) echo ',';
1143
 				if (isset($select[$table])) echo ',';
1143
 				echo '"columns":';
1144
 				echo '"columns":';
1144
 				$keys = array();
1145
 				$keys = array();
1145
 				$base64 = array();
1146
 				$base64 = array();
1146
 				foreach ($fields[$table] as $field) {
1147
 				foreach ($fields[$table] as $field) {
1147
-					$base64[] = $db->is_binary_type($field);
1148
+					$base64[] = $this->db->is_binary_type($field);
1148
 					$keys[] = $field->name;
1149
 					$keys[] = $field->name;
1149
 				}
1150
 				}
1150
 				echo json_encode($keys);
1151
 				echo json_encode($keys);
1151
 				$keys = array_flip($keys);
1152
 				$keys = array_flip($keys);
1152
 				echo ',"records":[';
1153
 				echo ',"records":[';
1153
 				$first_row = true;
1154
 				$first_row = true;
1154
-				while ($row = $db->fetch_row($result)) {
1155
+				while ($row = $this->db->fetch_row($result)) {
1155
 					if ($first_row) $first_row = false;
1156
 					if ($first_row) $first_row = false;
1156
 					else echo ',';
1157
 					else echo ',';
1157
 					if (isset($collect[$table])) {
1158
 					if (isset($collect[$table])) {
1161
 					}
1162
 					}
1162
 					foreach ($base64 as $k=>$v) {
1163
 					foreach ($base64 as $k=>$v) {
1163
 						if ($v && $row[$k]) {
1164
 						if ($v && $row[$k]) {
1164
-							$row[$k] = $db->base64_encode($row[$k]);
1165
+							$row[$k] = $this->db->base64_encode($row[$k]);
1165
 						}
1166
 						}
1166
 					}
1167
 					}
1167
 					echo json_encode($row);
1168
 					echo json_encode($row);
1168
 				}
1169
 				}
1169
-				$db->close($result);
1170
+				$this->db->close($result);
1170
 				echo ']';
1171
 				echo ']';
1171
 			}
1172
 			}
1172
 			echo '}';
1173
 			echo '}';
1176
 
1177
 
1177
 	protected function readCommand($parameters) {
1178
 	protected function readCommand($parameters) {
1178
 		extract($parameters);
1179
 		extract($parameters);
1179
-		$object = $this->retrieveObject($key,$fields,$filters,$tables,$db);
1180
+		$object = $this->retrieveObject($key,$fields,$filters,$tables);
1180
 		if (!$object) $this->exitWith404('object');
1181
 		if (!$object) $this->exitWith404('object');
1181
 		$this->startOutput($callback);
1182
 		$this->startOutput($callback);
1182
 		echo json_encode($object);
1183
 		echo json_encode($object);
1187
 		extract($parameters);
1188
 		extract($parameters);
1188
 		if (!$input) $this->exitWith404('input');
1189
 		if (!$input) $this->exitWith404('input');
1189
 		$this->startOutput($callback);
1190
 		$this->startOutput($callback);
1190
-		echo json_encode($this->createObject($input,$tables,$db));
1191
+		echo json_encode($this->createObject($input,$tables));
1191
 		$this->endOutput($callback);
1192
 		$this->endOutput($callback);
1192
 	}
1193
 	}
1193
 
1194
 
1195
 		extract($parameters);
1196
 		extract($parameters);
1196
 		if (!$input) $this->exitWith404('subject');
1197
 		if (!$input) $this->exitWith404('subject');
1197
 		$this->startOutput($callback);
1198
 		$this->startOutput($callback);
1198
-		echo json_encode($this->updateObject($key,$input,$filters,$tables,$db));
1199
+		echo json_encode($this->updateObject($key,$input,$filters,$tables));
1199
 		$this->endOutput($callback);
1200
 		$this->endOutput($callback);
1200
 	}
1201
 	}
1201
 
1202
 
1202
 	protected function deleteCommand($parameters) {
1203
 	protected function deleteCommand($parameters) {
1203
 		extract($parameters);
1204
 		extract($parameters);
1204
 		$this->startOutput($callback);
1205
 		$this->startOutput($callback);
1205
-		echo json_encode($this->deleteObject($key,$filters,$tables,$db));
1206
+		echo json_encode($this->deleteObject($key,$filters,$tables));
1206
 		$this->endOutput($callback);
1207
 		$this->endOutput($callback);
1207
 	}
1208
 	}
1208
 
1209
 
1278
 			$db->connectDatabase($hostname,$username,$password,$database,$port,$socket,$charset);
1279
 			$db->connectDatabase($hostname,$username,$password,$database,$port,$socket,$charset);
1279
 		}
1280
 		}
1280
 
1281
 
1281
-		$this->settings = compact('method', 'request', 'get', 'post', 'database', 'table_authorizer', 'record_filter', 'column_authorizer', 'tenancy_function', 'input_sanitizer', 'input_validator', 'db');
1282
+		$this->db = $db;
1283
+		$this->settings = compact('method', 'request', 'get', 'post', 'database', 'table_authorizer', 'record_filter', 'column_authorizer', 'tenancy_function', 'input_sanitizer', 'input_validator');
1282
 	}
1284
 	}
1283
 
1285
 
1284
 	public static function php_crud_api_transform(&$tables) {
1286
 	public static function php_crud_api_transform(&$tables) {

Loading…
Cancel
Save