Ver código fonte

Refactor database drivers

Maurits van der Schee 8 anos atrás
pai
commit
d8cb1f717a
1 arquivos alterados com 75 adições e 73 exclusões
  1. 75
    73
      api.php

+ 75
- 73
api.php Ver arquivo

@@ -550,6 +550,7 @@ class SQLServer implements DatabaseInterface {
550 550
 
551 551
 class PHP_CRUD_API {
552 552
 
553
+	protected $db;
553 554
 	protected $settings;
554 555
 
555 556
 	protected function mapMethodToAction($method,$key) {
@@ -598,9 +599,9 @@ class PHP_CRUD_API {
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 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 605
 			if ($f) {
605 606
 				if (!isset($filters[$table])) $filters[$table] = array();
606 607
 				if (!isset($filters[$table]['and'])) $filters[$table]['and'] = array();
@@ -669,15 +670,15 @@ class PHP_CRUD_API {
669 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 674
 		$blacklist = array('information_schema','mysql','sys','pg_catalog');
674 675
 		if (in_array(strtolower($database), $blacklist)) return array();
675 676
 		$table_array = explode(',',$tables);
676 677
 		$table_list = array();
677 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 682
 				if ($action!='list') break;
682 683
 			}
683 684
 		}
@@ -734,16 +735,16 @@ class PHP_CRUD_API {
734 735
 		}
735 736
 	}
736 737
 
737
-	protected function processKeyParameter($key,$tables,$database,$db) {
738
+	protected function processKeyParameter($key,$tables,$database) {
738 739
 		if (!$key) return false;
739 740
 		$count = 0;
740 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 744
 				$count++;
744 745
 				$field = $row[0];
745 746
 			}
746
-			$db->close($result);
747
+			$this->db->close($result);
747 748
 		}
748 749
 		if ($count!=1 || $field==false) $this->exitWith404('1pk');
749 750
 		return array($key,$field);
@@ -758,11 +759,11 @@ class PHP_CRUD_API {
758 759
 		return $order;
759 760
 	}
760 761
 
761
-	protected function convertFilter($db, $field, $comparator, $value) {
762
+	protected function convertFilter($field, $comparator, $value) {
762 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 767
 			case 'eq': $comparator = '='; break;
767 768
 			case 'ne': $comparator = '<>'; break;
768 769
 			case 'lt': $comparator = '<'; break;
@@ -774,21 +775,21 @@ class PHP_CRUD_API {
774 775
 		return array($field, $comparator, $value);
775 776
 	}
776 777
 
777
-	protected function convertFilters($db,$filters) {
778
+	protected function convertFilters($filters) {
778 779
 		$result = array();
779 780
 		if ($filters) {
780 781
 			for ($i=0;$i<count($filters);$i++) {
781 782
 				$filter = explode(',',$filters[$i],3);
782 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 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 793
 		if (!$result) return array();
793 794
 		$and = ($satisfy && strtolower($satisfy)=='any')?'or':'and';
794 795
 		return array($tables[0]=>array($and=>$result));
@@ -802,7 +803,7 @@ class PHP_CRUD_API {
802 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 807
 		if (!$key) return false;
807 808
 		$table = $tables[0];
808 809
 		$sql = 'SELECT ';
@@ -813,31 +814,31 @@ class PHP_CRUD_API {
813 814
 		if (!isset($filters[$table]['or'])) $filters[$table]['or'] = array();
814 815
 		$filters[$table]['or'][] = array($key[1],'=',$key[0]);
815 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 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 826
 		return $object;
826 827
 	}
827 828
 
828
-	protected function createObject($input,$tables,$db) {
829
+	protected function createObject($input,$tables) {
829 830
 		if (!$input) return false;
830 831
 		$input = (array)$input;
831 832
 		$keys = implode('","',str_split(str_repeat('!', count($input))));
832 833
 		$values = implode(',',str_split(str_repeat('?', count($input))));
833 834
 		$params = array_merge(array_keys($input),array_values($input));
834 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 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 842
 		if (!$input) return false;
842 843
 		$input = (array)$input;
843 844
 		$table = $tables[0];
@@ -854,11 +855,11 @@ class PHP_CRUD_API {
854 855
 		if (!isset($filters[$table]['or'])) $filters[$table]['or'] = array();
855 856
 		$filters[$table]['or'][] = array($key[1],'=',$key[0]);
856 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 863
 		$table = $tables[0];
863 864
 		$sql = 'DELETE FROM "!"';
864 865
 		$params = array($table);
@@ -866,11 +867,11 @@ class PHP_CRUD_API {
866 867
 		if (!isset($filters[$table]['or'])) $filters[$table]['or'] = array();
867 868
 		$filters[$table]['or'][] = array($key[1],'=',$key[0]);
868 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 875
 		$tableset = array();
875 876
 		$collect = array();
876 877
 		$select = array();
@@ -879,20 +880,20 @@ class PHP_CRUD_API {
879 880
 			$table0 = array_shift($tables);
880 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 885
 				$collect[$row[0]][$row[1]]=array();
885 886
 				$select[$row[2]][$row[3]]=array($row[0],$row[1]);
886 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 891
 				$collect[$row[2]][$row[3]]=array();
891 892
 				$select[$row[0]][$row[1]]=array($row[2],$row[3]);
892 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 897
 				$collect[$row[2]][$row[3]]=array();
897 898
 				$select[$row[0]][$row[1]]=array($row[2],$row[3]);
898 899
 				$collect[$row[4]][$row[5]]=array();
@@ -925,10 +926,10 @@ class PHP_CRUD_API {
925 926
 		return $input;
926 927
 	}
927 928
 
928
-	protected function findFields($tables,$columns,$database,$db) {
929
+	protected function findFields($tables,$columns,$database) {
929 930
 		$fields = array();
930 931
 		foreach ($tables as $i=>$table) {
931
-			$fields[$table] = $this->findTableFields($table,$database,$db);
932
+			$fields[$table] = $this->findTableFields($table,$database);
932 933
 			if ($i==0) $fields[$table] = $this->filterFieldsByColumns($fields[$table],$columns);
933 934
 		}
934 935
 		return $fields;
@@ -946,10 +947,10 @@ class PHP_CRUD_API {
946 947
 		return $fields;
947 948
 	}
948 949
 
949
-	protected function findTableFields($table,$database,$db) {
950
+	protected function findTableFields($table,$database) {
950 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 954
 			$fields[$field->name] = $field;
954 955
 		}
955 956
 		return $fields;
@@ -964,9 +965,9 @@ class PHP_CRUD_API {
964 965
 		return $input;
965 966
 	}
966 967
 
967
-	protected function convertBinary(&$input,$keys,$db) {
968
+	protected function convertBinary(&$input,$keys) {
968 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 971
 				$data = $input->$key;
971 972
 				$data = str_pad(strtr($data, '-_', '+/'), strlen($data) % 4, '=', STR_PAD_RIGHT);
972 973
 				$input->$key = (object)array('type'=>'base64','data'=>$data);
@@ -988,19 +989,19 @@ class PHP_CRUD_API {
988 989
 		$order     = $this->parseGetParameter($get, 'order', 'a-zA-Z0-9\-_,');
989 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 995
 		$page      = $this->processPageParameter($page);
995 996
 		$order     = $this->processOrderParameter($order);
996 997
 
997 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 1002
 		// permissions
1002 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 1005
 		if ($column_authorizer) $this->applyColumnAuthorizer($column_authorizer,$action,$database,$fields);
1005 1006
 		if ($tenancy_function) $this->applyTenancyFunction($tenancy_function,$action,$database,$fields,$filters);
1006 1007
 
@@ -1013,10 +1014,10 @@ class PHP_CRUD_API {
1013 1014
 			if ($input_sanitizer) $this->applyInputSanitizer($input_sanitizer,$action,$database,$tables[0],$input,$fields[$tables[0]]);
1014 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 1023
 	protected function addWhereFromFilters($filters,&$sql,&$params) {
@@ -1059,8 +1060,8 @@ class PHP_CRUD_API {
1059 1060
 			if (isset($filters[$table])) {
1060 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 1065
 					$count = $pages[0];
1065 1066
 				}
1066 1067
 			}
@@ -1079,21 +1080,21 @@ class PHP_CRUD_API {
1079 1080
 			$params[] = $order[1];
1080 1081
 		}
1081 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 1086
 			echo '"columns":';
1086 1087
 			$keys = array();
1087 1088
 			$base64 = array();
1088 1089
 			foreach ($fields[$table] as $field) {
1089
-				$base64[] = $db->is_binary_type($field);
1090
+				$base64[] = $this->db->is_binary_type($field);
1090 1091
 				$keys[] = $field->name;
1091 1092
 			}
1092 1093
 			echo json_encode($keys);
1093 1094
 			$keys = array_flip($keys);
1094 1095
 			echo ',"records":[';
1095 1096
 			$first_row = true;
1096
-			while ($row = $db->fetch_row($result)) {
1097
+			while ($row = $this->db->fetch_row($result)) {
1097 1098
 				if ($first_row) $first_row = false;
1098 1099
 				else echo ',';
1099 1100
 				if (isset($collect[$table])) {
@@ -1103,12 +1104,12 @@ class PHP_CRUD_API {
1103 1104
 				}
1104 1105
 				foreach ($base64 as $k=>$v) {
1105 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 1110
 				echo json_encode($row);
1110 1111
 			}
1111
-			$db->close($result);
1112
+			$this->db->close($result);
1112 1113
 			echo ']';
1113 1114
 			if ($count) echo ',';
1114 1115
 		}
@@ -1138,20 +1139,20 @@ class PHP_CRUD_API {
1138 1139
 				echo '}';
1139 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 1143
 				if (isset($select[$table])) echo ',';
1143 1144
 				echo '"columns":';
1144 1145
 				$keys = array();
1145 1146
 				$base64 = array();
1146 1147
 				foreach ($fields[$table] as $field) {
1147
-					$base64[] = $db->is_binary_type($field);
1148
+					$base64[] = $this->db->is_binary_type($field);
1148 1149
 					$keys[] = $field->name;
1149 1150
 				}
1150 1151
 				echo json_encode($keys);
1151 1152
 				$keys = array_flip($keys);
1152 1153
 				echo ',"records":[';
1153 1154
 				$first_row = true;
1154
-				while ($row = $db->fetch_row($result)) {
1155
+				while ($row = $this->db->fetch_row($result)) {
1155 1156
 					if ($first_row) $first_row = false;
1156 1157
 					else echo ',';
1157 1158
 					if (isset($collect[$table])) {
@@ -1161,12 +1162,12 @@ class PHP_CRUD_API {
1161 1162
 					}
1162 1163
 					foreach ($base64 as $k=>$v) {
1163 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 1168
 					echo json_encode($row);
1168 1169
 				}
1169
-				$db->close($result);
1170
+				$this->db->close($result);
1170 1171
 				echo ']';
1171 1172
 			}
1172 1173
 			echo '}';
@@ -1176,7 +1177,7 @@ class PHP_CRUD_API {
1176 1177
 
1177 1178
 	protected function readCommand($parameters) {
1178 1179
 		extract($parameters);
1179
-		$object = $this->retrieveObject($key,$fields,$filters,$tables,$db);
1180
+		$object = $this->retrieveObject($key,$fields,$filters,$tables);
1180 1181
 		if (!$object) $this->exitWith404('object');
1181 1182
 		$this->startOutput($callback);
1182 1183
 		echo json_encode($object);
@@ -1187,7 +1188,7 @@ class PHP_CRUD_API {
1187 1188
 		extract($parameters);
1188 1189
 		if (!$input) $this->exitWith404('input');
1189 1190
 		$this->startOutput($callback);
1190
-		echo json_encode($this->createObject($input,$tables,$db));
1191
+		echo json_encode($this->createObject($input,$tables));
1191 1192
 		$this->endOutput($callback);
1192 1193
 	}
1193 1194
 
@@ -1195,14 +1196,14 @@ class PHP_CRUD_API {
1195 1196
 		extract($parameters);
1196 1197
 		if (!$input) $this->exitWith404('subject');
1197 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 1200
 		$this->endOutput($callback);
1200 1201
 	}
1201 1202
 
1202 1203
 	protected function deleteCommand($parameters) {
1203 1204
 		extract($parameters);
1204 1205
 		$this->startOutput($callback);
1205
-		echo json_encode($this->deleteObject($key,$filters,$tables,$db));
1206
+		echo json_encode($this->deleteObject($key,$filters,$tables));
1206 1207
 		$this->endOutput($callback);
1207 1208
 	}
1208 1209
 
@@ -1278,7 +1279,8 @@ class PHP_CRUD_API {
1278 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 1286
 	public static function php_crud_api_transform(&$tables) {

Carregando…
Cancelar
Salvar