浏览代码

查询过程拆解,准备折腾集合

menglu 3 年之前
父节点
当前提交
fd7aba7a2e
共有 1 个文件被更改,包括 141 次插入131 次删除
  1. 141 131
      ibms-data-sdk/src/main/java/com/persagy/ibms/data/sdk/util/QueryUtil.java

+ 141 - 131
ibms-data-sdk/src/main/java/com/persagy/ibms/data/sdk/util/QueryUtil.java

@@ -95,6 +95,7 @@ public class QueryUtil {
 	 * 
 	 * @return SceneDataObject List<SceneDataValue> Object
 	 */
+	@SuppressWarnings("unchecked")
 	public static Object QueryInner(Repository Repository, SceneDataValue sv, JSONObject sql_json, List<SceneDataValue> targetSet) throws Exception {
 		String QueryType = (String) sql_json.get("QueryType");
 		// 构造查询条件
@@ -210,7 +211,13 @@ public class QueryUtil {
 			if (sql_json.get("Limit") != null) {
 				Limit = (JSONObject) sql_json.get("Limit");
 			}
-			result = select(targetSet, criteria, OrderBy, Limit, ReturnColumns, UniqueReturnColumn, Aggregation, GroupBy);
+			result = query_select(targetSet, criteria);
+			if (Aggregation != null) {
+				result = query_aggregation((List<SceneDataValue>) result, Aggregation, GroupBy);
+			}
+			if (OrderBy != null || Limit != null || ReturnColumns != null || UniqueReturnColumn != null) {
+				result = query_after((List<SceneDataValue>) result, OrderBy, Limit, ReturnColumns, UniqueReturnColumn);
+			}
 		}
 
 		return result;
@@ -589,169 +596,130 @@ public class QueryUtil {
 	 * 
 	 * @return SceneDataObject List<SceneDataValue> Object
 	 */
-	private static Object select(List<SceneDataValue> set, CriteriaBase criteria, JSONArray OrderBy, JSONObject Limit, List<String> ReturnColumns,
-			String UniqueReturnColumn, Object Aggregation, JSONArray GroupBy) {
+	private static List<SceneDataValue> query_select(List<SceneDataValue> set, CriteriaBase criteria) {
+		List<SceneDataValue> resultArray = new ArrayList<SceneDataValue>();
+		for (int i = 0; i < set.size(); i++) {
+			SceneDataValue setValue = set.get(i);
+			if (criteria.match(setValue.value_object)) {
+				resultArray.add(setValue);
+			}
+		}
+		return resultArray;
+	}
+
+	private static Object query_aggregation(List<SceneDataValue> set, Object Aggregation, JSONArray GroupBy) {
 		JSONArray agg_array = null;
 		JSONObject AggregationObject = null;
-		if (Aggregation != null && Aggregation instanceof JSONObject) {
+		JSONArray AggregationArray = null;
+		if (Aggregation instanceof JSONObject) {
 			AggregationObject = (JSONObject) Aggregation;
 			JSONObject cloneObject = (JSONObject) FastJsonUtil.Clone_JSON(AggregationObject);
 			cloneObject.put("Name", cloneObject.get("Function"));
 			agg_array = new JSONArray();
 
 			agg_array.add(AggregationObject);
-		}
-		JSONArray AggregationArray = null;
-		if (Aggregation != null && Aggregation instanceof JSONArray) {
+		} else if (Aggregation instanceof JSONArray) {
 			AggregationArray = (JSONArray) Aggregation;
 			agg_array = (JSONArray) FastJsonUtil.Clone_JSON(AggregationArray);
 		}
 		Map<String, Boolean> columnDic = new HashMap<String, Boolean>();
-		if (agg_array != null) {
-			for (Object aggItemObject : agg_array) {
-				JSONObject aggItem = (JSONObject) aggItemObject;
-				String Function = (aggItem.get("Function")).toString();
-				if (!Function.equals("count")) {
-					String Column = (aggItem.get("Column")).toString();
-					columnDic.put(Column, true);
-				}
+		for (Object aggItemObject : agg_array) {
+			JSONObject aggItem = (JSONObject) aggItemObject;
+			String Function = (aggItem.get("Function")).toString();
+			if (!Function.equals("count")) {
+				String Column = (aggItem.get("Column")).toString();
+				columnDic.put(Column, true);
 			}
 		}
 		Map<String, SceneDataObject> agg_count = new HashMap<String, SceneDataObject>();
 		Map<String, Map<String, List<Object>>> agg_items = new HashMap<String, Map<String, List<Object>>>();
 
-		List<SceneDataValue> resultArray = new ArrayList<SceneDataValue>();
 		for (int i = 0; i < set.size(); i++) {
 			SceneDataValue setValue = set.get(i);
-			if (criteria.match(setValue.value_object)) {
-				if (agg_array != null) {
-					String key;
-					if (GroupBy == null) {
-						key = "default";
-					} else {
-						JSONObject keyObject = new JSONObject();
-						for (int ii = 0; ii < GroupBy.size(); ii++) {
-							String GroupByColumn = (GroupBy.get(ii)).toString();
-							keyObject.put(GroupByColumn, setValue.value_object.get(GroupByColumn).value_primitive);
-						}
-						key = keyObject.toString();
-					}
-					if (!agg_items.containsKey(key)) {
-						agg_items.put(key, new HashMap<String, List<Object>>());
-					}
-					if (!agg_count.containsKey(key)) {
-						SceneDataObject countObject = new SceneDataObject(null, null, null, null, null, null, null);
-						SceneDataValue sdvvvv = new SceneDataValue(null, countObject, "count", null, false);
-						sdvvvv.value_primitive = 0;
-						countObject.put("count", sdvvvv);
-						agg_count.put(key, countObject);
-					}
-					{
-						SceneDataObject countObject = agg_count.get(key);
-						SceneDataValue countValue = countObject.get("count");
-						countValue.value_primitive = (int) countValue.value_primitive + 1;
-					}
-					Map<String, List<Object>> itemListDic = agg_items.get(key);
-					for (String dicKey : columnDic.keySet()) {
-						if (!itemListDic.containsKey(dicKey)) {
-							itemListDic.put(dicKey, new ArrayList<Object>());
-						}
-						List<Object> items = itemListDic.get(dicKey);
-						if (setValue.value_object.containsKey(dicKey)) {
-							items.add(setValue.value_object.get(dicKey).value_primitive);
-						}
-					}
-				} else {
-					resultArray.add(setValue);
-				}
-			}
-		}
-
-		if (agg_array != null) {
+			String key;
 			if (GroupBy == null) {
-				if (AggregationObject != null) {
-					return aggregationProcess(agg_count, agg_items, AggregationObject, "default");
-				} else {
-					SceneDataObject result = new SceneDataObject(null, null, null, null, null, null, null);
-					for (Object agg_oneObject : AggregationArray) {
-						JSONObject agg_one = (JSONObject) agg_oneObject;
-						String Name = (agg_one.get("Name")).toString();
-						SceneDataValue sdvvv = new SceneDataValue(null, null, null, null, false);
-						sdvvv.finish = true;
-						sdvvv.value_primitive = aggregationProcess(agg_count, agg_items, agg_one, "default");
-						result.put(Name, sdvvv);
-					}
-					return result;
-				}
+				key = "default";
 			} else {
-				for (String key : agg_count.keySet()) {
-					JSONObject keyObject = JSON.parseObject(key);
-					SceneDataObject resultItem = new SceneDataObject(null, null, null, null, null, null, null);
-					for (String keyObjectOneKey : keyObject.keySet()) {
-						Object keyObjectOneValue = keyObject.get(keyObjectOneKey);
-						SceneDataValue sdvvv = new SceneDataValue(null, resultItem, keyObjectOneKey, null, false);
-						sdvvv.value_primitive = keyObjectOneValue;
-						sdvvv.finish = true;
-						resultItem.put(keyObjectOneKey, sdvvv);
-					}
-					for (Object agg_oneObject : AggregationArray) {
-						JSONObject agg_one = (JSONObject) agg_oneObject;
-						String Name = (agg_one.get("Name")).toString();
-						SceneDataValue sdvvv = new SceneDataValue(null, resultItem, Name, null, false);
-						sdvvv.finish = true;
-						sdvvv.value_primitive = aggregationProcess(agg_count, agg_items, agg_one, key);
-						resultItem.put(Name, sdvvv);
-					}
-					{
-						SceneDataValue sdvvv = new SceneDataValue(null, null, null, null, false);
-						sdvvv.value_object = resultItem;
-						resultArray.add(sdvvv);
-					}
+				JSONObject keyObject = new JSONObject();
+				for (int ii = 0; ii < GroupBy.size(); ii++) {
+					String GroupByColumn = (GroupBy.get(ii)).toString();
+					keyObject.put(GroupByColumn, setValue.value_object.get(GroupByColumn).value_primitive);
 				}
-				return resultArray;
+				key = keyObject.toString();
 			}
-		} else {
-			if (OrderBy != null && OrderBy.size() > 0) {
-				Collections.sort(resultArray, new SceneDataValueComparator(OrderBy));
+			if (!agg_items.containsKey(key)) {
+				agg_items.put(key, new HashMap<String, List<Object>>());
+			}
+			if (!agg_count.containsKey(key)) {
+				SceneDataObject countObject = new SceneDataObject(null, null, null, null, null, null, null);
+				SceneDataValue sdvvvv = new SceneDataValue(null, countObject, "count", null, false);
+				sdvvvv.value_primitive = 0;
+				countObject.put("count", sdvvvv);
+				agg_count.put(key, countObject);
 			}
-			if (Limit != null) {
-				int Limit_Skip = Limit.getIntValue("Skip");
-				int Limit_Count = Limit.getIntValue("Count");
-				List<SceneDataValue> contentList = new ArrayList<SceneDataValue>();
-				for (int index_ = (int) Limit_Skip; index_ < resultArray.size() && index_ < Limit_Skip + Limit_Count; index_++) {
-					contentList.add(resultArray.get(index_));
+			{
+				SceneDataObject countObject = agg_count.get(key);
+				SceneDataValue countValue = countObject.get("count");
+				countValue.value_primitive = (int) countValue.value_primitive + 1;
+			}
+			Map<String, List<Object>> itemListDic = agg_items.get(key);
+			for (String dicKey : columnDic.keySet()) {
+				if (!itemListDic.containsKey(dicKey)) {
+					itemListDic.put(dicKey, new ArrayList<Object>());
+				}
+				List<Object> items = itemListDic.get(dicKey);
+				if (setValue.value_object.containsKey(dicKey)) {
+					items.add(setValue.value_object.get(dicKey).value_primitive);
 				}
-				resultArray = contentList;
 			}
+		}
 
-			if (UniqueReturnColumn != null) {
-				List<SceneDataValue> resultArray_new = new ArrayList<SceneDataValue>();
-				for (SceneDataValue setValue : resultArray) {
-					resultArray_new.add(setValue.value_object.get(UniqueReturnColumn));
-				}
-				return resultArray_new;
+		if (GroupBy == null) {
+			if (AggregationObject != null) {
+				return query_aggregationProcess(agg_count, agg_items, AggregationObject, "default");
 			} else {
-				if (ReturnColumns != null) {
-					List<SceneDataValue> resultArray_new = new ArrayList<SceneDataValue>();
-					for (SceneDataValue setValue : resultArray) {
-						SceneDataObject resultItem = new SceneDataObject(null, null, null, null, null, null, null);
-						for (Object jtoken : ReturnColumns) {
-							String column = (jtoken).toString();
-							resultItem.put(column, setValue.value_object.get(column));
-						}
-						SceneDataValue sdvvvv = new SceneDataValue(null, null, null, null, false);
-						sdvvvv.value_object = resultItem;
-						resultArray_new.add(sdvvvv);
-					}
-					return resultArray_new;
-				} else {
-					return resultArray;
+				SceneDataObject result = new SceneDataObject(null, null, null, null, null, null, null);
+				for (Object agg_oneObject : AggregationArray) {
+					JSONObject agg_one = (JSONObject) agg_oneObject;
+					String Name = (agg_one.get("Name")).toString();
+					SceneDataValue sdvvv = new SceneDataValue(null, null, null, null, false);
+					sdvvv.finish = true;
+					sdvvv.value_primitive = query_aggregationProcess(agg_count, agg_items, agg_one, "default");
+					result.put(Name, sdvvv);
+				}
+				return result;
+			}
+		} else {
+			List<SceneDataValue> resultArray = new ArrayList<SceneDataValue>();
+			for (String key : agg_count.keySet()) {
+				JSONObject keyObject = JSON.parseObject(key);
+				SceneDataObject resultItem = new SceneDataObject(null, null, null, null, null, null, null);
+				for (String keyObjectOneKey : keyObject.keySet()) {
+					Object keyObjectOneValue = keyObject.get(keyObjectOneKey);
+					SceneDataValue sdvvv = new SceneDataValue(null, resultItem, keyObjectOneKey, null, false);
+					sdvvv.value_primitive = keyObjectOneValue;
+					sdvvv.finish = true;
+					resultItem.put(keyObjectOneKey, sdvvv);
+				}
+				for (Object agg_oneObject : AggregationArray) {
+					JSONObject agg_one = (JSONObject) agg_oneObject;
+					String Name = (agg_one.get("Name")).toString();
+					SceneDataValue sdvvv = new SceneDataValue(null, resultItem, Name, null, false);
+					sdvvv.finish = true;
+					sdvvv.value_primitive = query_aggregationProcess(agg_count, agg_items, agg_one, key);
+					resultItem.put(Name, sdvvv);
+				}
+				{
+					SceneDataValue sdvvv = new SceneDataValue(null, null, null, null, false);
+					sdvvv.value_object = resultItem;
+					resultArray.add(sdvvv);
 				}
 			}
+			return resultArray;
 		}
 	}
 
-	private static Object aggregationProcess(Map<String, SceneDataObject> agg_count, Map<String, Map<String, List<Object>>> agg_items,
+	private static Object query_aggregationProcess(Map<String, SceneDataObject> agg_count, Map<String, Map<String, List<Object>>> agg_items,
 			JSONObject agg_obj, String key) {
 		Object result = null;
 		String Function = (agg_obj.get("Function")).toString();
@@ -841,4 +809,46 @@ public class QueryUtil {
 		}
 		return result;
 	}
+
+	private static Object query_after(List<SceneDataValue> set, JSONArray OrderBy, JSONObject Limit, List<String> ReturnColumns,
+			String UniqueReturnColumn) {
+		List<SceneDataValue> resultArray = set;
+		if (OrderBy != null && OrderBy.size() > 0) {
+			Collections.sort(resultArray, new SceneDataValueComparator(OrderBy));
+		}
+		if (Limit != null) {
+			int Limit_Skip = Limit.getIntValue("Skip");
+			int Limit_Count = Limit.getIntValue("Count");
+			List<SceneDataValue> contentList = new ArrayList<SceneDataValue>();
+			for (int index_ = (int) Limit_Skip; index_ < resultArray.size() && index_ < Limit_Skip + Limit_Count; index_++) {
+				contentList.add(resultArray.get(index_));
+			}
+			resultArray = contentList;
+		}
+
+		if (UniqueReturnColumn != null) {
+			List<SceneDataValue> resultArray_new = new ArrayList<SceneDataValue>();
+			for (SceneDataValue setValue : resultArray) {
+				resultArray_new.add(setValue.value_object.get(UniqueReturnColumn));
+			}
+			return resultArray_new;
+		} else {
+			if (ReturnColumns != null) {
+				List<SceneDataValue> resultArray_new = new ArrayList<SceneDataValue>();
+				for (SceneDataValue setValue : resultArray) {
+					SceneDataObject resultItem = new SceneDataObject(null, null, null, null, null, null, null);
+					for (Object jtoken : ReturnColumns) {
+						String column = (jtoken).toString();
+						resultItem.put(column, setValue.value_object.get(column));
+					}
+					SceneDataValue sdvvvv = new SceneDataValue(null, null, null, null, false);
+					sdvvvv.value_object = resultItem;
+					resultArray_new.add(sdvvvv);
+				}
+				return resultArray_new;
+			} else {
+				return resultArray;
+			}
+		}
+	}
 }