forked from SuperMap/iClient-JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFilterCondition.js
More file actions
349 lines (335 loc) · 10.1 KB
/
Copy pathFilterCondition.js
File metadata and controls
349 lines (335 loc) · 10.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
function getParseSpecialCharacter() {
// 特殊字符字典
const directory = ['(', ')', '(', ')', ',', ','];
const res = {};
directory.forEach((item, index) => {
res[item] = `$${index}`
});
return res;
}
function parseSpecialCharacter(str) {
const directory = getParseSpecialCharacter();
for (let key in directory) {
const replaceValue = directory[key];
const pattern = new RegExp(`\\${key}`, 'g');
// eslint-disable-next-line
while (pattern.test(str)) {
str = str.replace(pattern, replaceValue);
}
}
return str;
}
function parseCondition(filterCondition, keys) {
const str = filterCondition.replace(/&|\||>|<|=|!/g, ' ');
const arr = str.split(' ').filter((item) => item);
let result = filterCondition;
arr.forEach((item) => {
const key = keys.find((val) => val === item);
if (startsWithNumber(item) && key) {
result = result.replace(key, '$' + key);
}
if (key) {
const res = parseSpecialCharacter(key);
result = result.replace(key, res);
}
});
return result;
}
// 处理jsonsqlfeature, 加前缀
function parseConditionFeature(feature) {
let copyValue = {};
for (let key in feature) {
let copyKey = key;
if (startsWithNumber(key)) {
copyKey = '$' + key;
}
copyKey = parseSpecialCharacter(copyKey);
copyValue[copyKey] = feature[key];
}
return copyValue;
}
function startsWithNumber(str) {
return /^\d/.test(str);
}
// 要判断一下是否是二维数组的filter
function isSimpleComparison(filter) {
if (!filter || !Array.isArray(filter)) {
return false;
}
const [operator, ...operands] = filter;
const simpleOps = ['==', '!=', '>', '>=', '<', '<=', 'in', '!in', 'has', '!has'];
const logicalOps = ['all', 'any', 'none'];
if (simpleOps.includes(operator)) {
// has / !has:只要求一个字段名
if (operator === 'has' || operator === '!has') {
// 排除这种: ['has', ['get', 'name']]
return typeof operands[0] === 'string';
}
// in / !in:field + 至少一个值
if (operator === 'in' || operator === '!in') {
if (typeof operands[0] !== 'string') { return false; }
return operands.length > 1;
}
// 普通比较:field + value
return operands.length === 2 && typeof operands[0] === 'string';
}
if (logicalOps.includes(operator)) {
if (operands.length === 0) { return false; }
return operands.every((item) => isSimpleComparison(item));
}
return false;
}
// 处理类似"O'Reilly"这种字符串,在SQL语句中需要转,单引号需要处理
function formatValue(value) {
if (typeof value === 'string') {
return `'${value.replace(/'/g, "''")}'`;
}
return String(value);
}
// 判断空值
function isNullvalue(value) {
return [null, '', undefined].includes(value);
}
function mapboxFilterToCqlFilter(filter, dataType) {
if (!isSimpleComparison(filter)) {
return '';
}
const [operator, ...operands] = filter;
const field = dataType === 'STRUCTURE_DATA' ? `"${operands[0]}"` : operands[0];
let value;
switch (operator) {
case '>':
case '>=':
case '<':
case '<=': {
value = operands[1];
return `${field} ${operator} ${formatValue(value)}`;
}
case '==': {
value = operands[1];
if (isNullvalue(value)) {
return `${field} IS NULL`;
}
return `${field} = ${formatValue(value)}`;
}
case '!=': {
value = operands[1];
if (isNullvalue(value)) {
return `${field} IS NOT NULL`;
}
return `${field} <> ${formatValue(value)}`;
}
case 'in': {
const values = operands.slice(1);
return `${field} IN (${values.map((v) => formatValue(v)).join(', ')})`;
}
case '!in': {
const values = operands.slice(1);
return `${field} NOT IN (${values.map((v) => formatValue(v)).join(', ')})`;
}
// TODO
// case 'has': {
// const field = operands[0];
// return tableFieldNames?.includes(field) ? '1=1' : '1=0';
// }
// case '!has': {
// const field = operands[0];
// return tableFieldNames?.includes(field) ? '1=0' : '1=1';
// }
case 'all': {
return operands
.map((item) => mapboxFilterToCqlFilter(item, dataType))
.filter(Boolean)
.map((item) => `${item}`)
.join(' AND ');
}
case 'any': {
return operands
.map((item) => mapboxFilterToCqlFilter(item, dataType))
.filter(Boolean)
.map((item) => `(${item})`)
.join(' OR ');
}
case 'none': {
return operands
.map((item) => mapboxFilterToCqlFilter(item, dataType))
.filter(Boolean)
.map((item) => `NOT (${item})`)
.join(' AND ');
}
default:
return '';
}
}
// 暂时不考虑WFS
// none的情况下,全部用Not一个一个包裹,会报错(应该是接口问题),反转即可
function negateComparison(filter) {
const op = filter[0];
const args = filter.slice(1);
switch (op) {
case '==':
return mapboxFilterToWfsFilter(['!=', ...args]);
case '!=':
return mapboxFilterToWfsFilter(['==', ...args]);
case '>':
return mapboxFilterToWfsFilter(['<=', ...args]);
case '>=':
return mapboxFilterToWfsFilter(['<', ...args]);
case '<':
return mapboxFilterToWfsFilter(['>=', ...args]);
case '<=':
return mapboxFilterToWfsFilter(['>', ...args]);
case 'in':
return mapboxFilterToWfsFilter(['!in', ...args]);
case '!in':
return mapboxFilterToWfsFilter(['in', ...args]);
// case 'has':
// return mapboxFilterToWfsFilter(['!has', ...args], fieldNames, tableFieldNames);
// case '!has':
// return mapboxFilterToWfsFilter(['has', ...args], fieldNames, tableFieldNames);
default:
return '';
}
}
function mapboxFilterToWfsFilterBody(filter) {
if (!isSimpleComparison(filter)) {
return '';
}
const [operator, ...operands] = filter;
const fieldRef = (field) => `<fes:ValueReference>${field}</fes:ValueReference>`;
const literal = (value) => `<fes:Literal>${value}</fes:Literal>`;
switch (operator) {
case '==': {
const [field, value] = operands;
if (isNullvalue(value)) {
return `
<fes:PropertyIsNull>
${fieldRef(field)}
</fes:PropertyIsNull>`;
}
return `
<fes:PropertyIsEqualTo>
${fieldRef(field)}
${literal(value)}
</fes:PropertyIsEqualTo>`;
}
case '!=': {
const [field, value] = operands;
if (isNullvalue(value)) {
return `
<fes:PropertyIsNotNull>
${fieldRef(field)}
</fes:PropertyIsNotNull>`;
}
return `
<fes:PropertyIsNotEqualTo>
${fieldRef(field)}
${literal(value)}
</fes:PropertyIsNotEqualTo>`;
}
case '>':
return `
<fes:PropertyIsGreaterThan>
${fieldRef(operands[0])}
${literal(operands[1])}
</fes:PropertyIsGreaterThan>`;
case '>=':
return `
<fes:PropertyIsGreaterThanOrEqualTo>
${fieldRef(operands[0])}
${literal(operands[1])}
</fes:PropertyIsGreaterThanOrEqualTo>`;
case '<':
return `
<fes:PropertyIsLessThan>
${fieldRef(operands[0])}
${literal(operands[1])}
</fes:PropertyIsLessThan>`;
case '<=':
return `
<fes:PropertyIsLessThanOrEqualTo>
${fieldRef(operands[0])}
${literal(operands[1])}
</fes:PropertyIsLessThanOrEqualTo>`;
case 'in': {
const field = operands[0];
const values = operands.slice(1);
return `
<fes:Or>${values
.map(
(v) => `
<fes:PropertyIsEqualTo>
${fieldRef(field)}
${literal(v)}
</fes:PropertyIsEqualTo>`
)
.join('')}</fes:Or>`;
}
case '!in': {
const field = operands[0];
const values = operands.slice(1);
return `
<fes:Not><fes:Or>${values
.map(
(v) => `
<fes:PropertyIsEqualTo>
${fieldRef(field)}
${literal(v)}
</fes:PropertyIsEqualTo>`
)
.join('')}</fes:Or></fes:Not>`;
}
// TODO,has条件暂时是用null判断,不准确
// case 'has': { ... }
// case '!has': { ... }
case 'all': {
const children = operands
.map((item) => mapboxFilterToWfsFilterBody(item).trim())
.filter(Boolean);
return children.length
? `
<fes:And>
${children.join('')}
</fes:And>`
: '';
}
case 'any': {
const children = operands
.map((item) => mapboxFilterToWfsFilterBody(item).trim())
.filter(Boolean);
return children.length
? `
<fes:Or>
${children.join('')}
</fes:Or>`
: '';
}
case 'none': {
const children = operands.map((item) => negateComparison(item).trim()).filter(Boolean);
return children.length
? `
<fes:And>
${children.join('')}
</fes:And>`
: '';
}
default:
return '';
}
}
function mapboxFilterToWfsFilter(filter) {
const body = mapboxFilterToWfsFilterBody(filter);
if (!body) { return ''; }
return `<fes:Filter>${body}
</fes:Filter>`;
}
function mapboxFilterToQueryFilter(filter, dataType, type = 'SQL') {
if (type === 'SQL') {
return mapboxFilterToCqlFilter(filter, dataType);
}
if (type === 'XML') {
return mapboxFilterToWfsFilter(filter);
}
return '';
}
export { parseCondition, parseConditionFeature, mapboxFilterToQueryFilter };