|
9 | 9 | import java.time.ZonedDateTime; |
10 | 10 | import java.time.format.DateTimeFormatter; |
11 | 11 | import java.util.Arrays; |
| 12 | +import java.util.HashMap; |
12 | 13 | import java.util.Map; |
13 | 14 |
|
14 | 15 | import static ir.bigz.springbootreal.dto.ValueCondition.*; |
@@ -111,69 +112,72 @@ else if (condition == IN) { |
111 | 112 | public static <T> void buildNativeQueryCondition(Map<String, String> queryString, |
112 | 113 | Map<String, String> conditionsMap, |
113 | 114 | Map<String, Object> parametersMap, |
114 | | - String fieldNameOnDataBase, |
115 | | - String parameterQueryStringMap, |
116 | | - SqlOperation operation, |
| 115 | + Map<String, String> parameterQueryWithFieldNameMap, |
| 116 | + Map<String, SqlOperation> parameterQueryWithOperationMap, |
117 | 117 | Class<T> type) { |
118 | | - if (queryString != null) { |
119 | | - String value = queryString.get(parameterQueryStringMap); |
120 | | - T parameterVal = null; |
121 | | - String queryCondition = ""; |
122 | | - if (type.equals(String.class)) { |
123 | | - parameterVal = (T) ""; |
124 | | - } |
125 | | - if (value != null && !Utils.isNull(value)) { |
126 | | - if (operation == SqlOperation.EQUAL || |
127 | | - operation == SqlOperation.NOT_EQUAL || |
128 | | - operation == SqlOperation.GREATER_THAN || |
129 | | - operation == SqlOperation.GREATER_THAN_OR_EQUAL || |
130 | | - operation == SqlOperation.LESS_THAN || |
131 | | - operation == SqlOperation.LESS_THAN_OR_EQUAL) { |
132 | | - if (type.equals(Integer.class)) { |
133 | | - parameterVal = (T) Integer.valueOf(value); |
134 | | - } |
135 | | - if (type.equals(Double.class)) { |
136 | | - parameterVal = (T) Double.valueOf(value); |
137 | | - } |
138 | | - if (type.equals(Long.class)) { |
139 | | - parameterVal = (T) Long.valueOf(value); |
140 | | - } |
141 | | - if (type.equals(Boolean.class)) { |
142 | | - parameterVal = (T) Boolean.valueOf(value); |
143 | | - } |
144 | | - if (type.equals(String.class)) { |
145 | | - parameterVal = (T) value; |
146 | | - } |
147 | | - parametersMap.put(parameterQueryStringMap, parameterVal); |
148 | | - queryCondition = "and " + fieldNameOnDataBase + " " + operation.operationSign + " " + ":" + parameterQueryStringMap; |
149 | | - } else if (operation == SqlOperation.CONTAINS || operation == SqlOperation.NOT_CONTAINS) { |
150 | | - if (type.equals(String.class)) { |
151 | | - parametersMap.put(parameterQueryStringMap, "%" + value + "%"); |
152 | | - queryCondition = "and " + fieldNameOnDataBase + " " + operation.operationSign + " :" + parameterQueryStringMap; |
| 118 | + parameterQueryWithFieldNameMap.keySet().forEach( |
| 119 | + paramQuery -> { |
| 120 | + if (queryString != null) { |
| 121 | + String value = queryString.get(paramQuery); |
| 122 | + T parameterVal = null; |
| 123 | + String queryCondition = ""; |
| 124 | + if (type.equals(String.class)) { |
| 125 | + parameterVal = (T) ""; |
| 126 | + } |
| 127 | + SqlOperation operation = parameterQueryWithOperationMap.get(paramQuery); |
| 128 | + if (value != null && !Utils.isNull(value)) { |
| 129 | + if (operation == SqlOperation.EQUAL || |
| 130 | + operation == SqlOperation.NOT_EQUAL || |
| 131 | + operation == SqlOperation.GREATER_THAN || |
| 132 | + operation == SqlOperation.GREATER_THAN_OR_EQUAL || |
| 133 | + operation == SqlOperation.LESS_THAN || |
| 134 | + operation == SqlOperation.LESS_THAN_OR_EQUAL) { |
| 135 | + if (type.equals(Integer.class)) { |
| 136 | + parameterVal = (T) Integer.valueOf(value); |
| 137 | + } |
| 138 | + if (type.equals(Double.class)) { |
| 139 | + parameterVal = (T) Double.valueOf(value); |
| 140 | + } |
| 141 | + if (type.equals(Long.class)) { |
| 142 | + parameterVal = (T) Long.valueOf(value); |
| 143 | + } |
| 144 | + if (type.equals(Boolean.class)) { |
| 145 | + parameterVal = (T) Boolean.valueOf(value); |
| 146 | + } |
| 147 | + if (type.equals(String.class)) { |
| 148 | + parameterVal = (T) value; |
| 149 | + } |
| 150 | + parametersMap.put(paramQuery, parameterVal); |
| 151 | + queryCondition = " and " + parameterQueryWithFieldNameMap.get(paramQuery) + " " + operation.operationSign + " " + ":" + paramQuery; |
| 152 | + } else if (operation == SqlOperation.CONTAINS || operation == SqlOperation.NOT_CONTAINS) { |
| 153 | + if (type.equals(String.class)) { |
| 154 | + parametersMap.put(paramQuery, "%" + value + "%"); |
| 155 | + queryCondition = " and " + parameterQueryWithFieldNameMap.get(paramQuery) + " " + operation.operationSign + " " + ":" + paramQuery; |
| 156 | + } |
| 157 | + } else if (operation == SqlOperation.STARTS_WITH) { |
| 158 | + if (type.equals(String.class)) { |
| 159 | + parametersMap.put(paramQuery, value + "%"); |
| 160 | + queryCondition = " and " + parameterQueryWithFieldNameMap.get(paramQuery) + " " + operation.operationSign + " " + ":" + paramQuery; |
| 161 | + } |
| 162 | + } else if (operation == SqlOperation.ENDS_WITH) { |
| 163 | + if (type.equals(String.class)) { |
| 164 | + parametersMap.put(paramQuery, "%" + value); |
| 165 | + queryCondition = " and " + parameterQueryWithFieldNameMap.get(paramQuery) + " " + operation.operationSign + " " + ":" + paramQuery; |
| 166 | + } |
| 167 | + } else if (operation == SqlOperation.IN) { |
| 168 | + String[] listArray = value.split(","); |
| 169 | + parametersMap.put(paramQuery, Arrays.asList(listArray)); |
| 170 | + queryCondition = " and " + parameterQueryWithFieldNameMap.get(paramQuery) + " " + operation.operationSign + " (" + ":" + paramQuery + ")"; |
| 171 | + } |
| 172 | + |
| 173 | + conditionsMap.put(paramQuery, queryCondition); |
| 174 | + } |
153 | 175 | } |
154 | | - } else if (operation == SqlOperation.STARTS_WITH) { |
155 | | - if (type.equals(String.class)) { |
156 | | - parametersMap.put(parameterQueryStringMap, value + "%"); |
157 | | - queryCondition = "and " + fieldNameOnDataBase + " " + operation.operationSign + " " + ":" + parameterQueryStringMap; |
158 | | - } |
159 | | - } else if (operation == SqlOperation.ENDS_WITH) { |
160 | | - if (type.equals(String.class)) { |
161 | | - parametersMap.put(parameterQueryStringMap, "%" + value); |
162 | | - queryCondition = "and " + fieldNameOnDataBase + " " + operation.operationSign + " " + ":" + parameterQueryStringMap; |
163 | | - } |
164 | | - } else if (operation == SqlOperation.IN) { |
165 | | - String[] listArray = value.split(","); |
166 | | - parametersMap.put(parameterQueryStringMap, Arrays.asList(listArray)); |
167 | | - queryCondition = "and " + fieldNameOnDataBase + " " + operation.operationSign + " (" + ":" + parameterQueryStringMap + ")"; |
168 | | - } |
169 | | - |
170 | | - conditionsMap.put(parameterQueryStringMap, queryCondition); |
171 | | - } |
172 | | - } |
| 176 | + }); |
173 | 177 |
|
174 | 178 | } |
175 | 179 |
|
176 | 180 | public static String getWhereSimple(){ |
177 | | - return " where 1=1 "; |
| 181 | + return " where 1=1"; |
178 | 182 | } |
179 | 183 | } |
0 commit comments