Skip to content

Commit 58e026d

Browse files
[update] mapboxFilterToCqlFilter转换逻辑中结构化字段名加入双引号
(review by xjj)
1 parent 108794d commit 58e026d

File tree

2 files changed

+29
-29
lines changed

2 files changed

+29
-29
lines changed

src/common/util/FilterCondition.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -99,12 +99,12 @@ function isNullvalue(value) {
9999
return [null, '', undefined].includes(value);
100100
}
101101

102-
function mapboxFilterToCqlFilter(filter) {
102+
function mapboxFilterToCqlFilter(filter, dataType) {
103103
if (!isSimpleComparison(filter)) {
104104
return '';
105105
}
106106
const [operator, ...operands] = filter;
107-
const field = operands[0];
107+
const field = dataType === 'STRUCTURE_DATA' ? `"${operands[0]}"` : operands[0];
108108
let value;
109109
switch (operator) {
110110
case '>':
@@ -147,21 +147,21 @@ function mapboxFilterToCqlFilter(filter) {
147147
// }
148148
case 'all': {
149149
return operands
150-
.map((item) => mapboxFilterToCqlFilter(item))
150+
.map((item) => mapboxFilterToCqlFilter(item, dataType))
151151
.filter(Boolean)
152152
.map((item) => `${item}`)
153153
.join(' AND ');
154154
}
155155
case 'any': {
156156
return operands
157-
.map((item) => mapboxFilterToCqlFilter(item))
157+
.map((item) => mapboxFilterToCqlFilter(item, dataType))
158158
.filter(Boolean)
159159
.map((item) => `(${item})`)
160160
.join(' OR ');
161161
}
162162
case 'none': {
163163
return operands
164-
.map((item) => mapboxFilterToCqlFilter(item))
164+
.map((item) => mapboxFilterToCqlFilter(item, dataType))
165165
.filter(Boolean)
166166
.map((item) => `NOT (${item})`)
167167
.join(' AND ');
@@ -170,7 +170,7 @@ function mapboxFilterToCqlFilter(filter) {
170170
return '';
171171
}
172172
}
173-
173+
// 暂时不考虑WFS
174174
// none的情况下,全部用Not一个一个包裹,会报错(应该是接口问题),反转即可
175175
function negateComparison(filter) {
176176
const op = filter[0];
@@ -335,9 +335,9 @@ function mapboxFilterToWfsFilter(filter) {
335335
</fes:Filter>`;
336336
}
337337

338-
function mapboxFilterToQueryFilter(filter, type = 'SQL') {
338+
function mapboxFilterToQueryFilter(filter, dataType, type = 'SQL') {
339339
if (type === 'SQL') {
340-
return mapboxFilterToCqlFilter(filter);
340+
return mapboxFilterToCqlFilter(filter, dataType);
341341
}
342342
if (type === 'XML') {
343343
return mapboxFilterToWfsFilter(filter);

test/common/util/FilterConditionSpec.js

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,45 +4,45 @@ import {
44

55
describe('FilterCondition', () => {
66
describe('mapboxFilterToQueryFilter - SQL', () => {
7-
it('== 普通等值', () => {
7+
it('== 普通等值 + 结构化', () => {
88
const filter = ['==', 'name', 'Tom'];
9-
const result = mapboxFilterToQueryFilter(filter, 'SQL');
10-
expect(result).toBe("name = 'Tom'");
9+
const result = mapboxFilterToQueryFilter(filter, 'STRUCTURE_DATA');
10+
expect(result).toBe(`"name" = 'Tom'`);
1111
});
1212

1313
it('== null 转 IS NULL', () => {
1414
const filter = ['==', 'name', null];
15-
const result = mapboxFilterToQueryFilter(filter, 'SQL');
15+
const result = mapboxFilterToQueryFilter(filter);
1616
expect(result).toBe('name IS NULL');
1717
});
1818

1919
it('!= null 转 IS NOT NULL', () => {
2020
const filter = ['!=', 'name', null];
21-
const result = mapboxFilterToQueryFilter(filter, 'SQL');
21+
const result = mapboxFilterToQueryFilter(filter);
2222
expect(result).toBe('name IS NOT NULL');
2323
});
2424

2525
it('字符串单引号转义', () => {
2626
const filter = ['==', 'publisher', "O'Reilly"];
27-
const result = mapboxFilterToQueryFilter(filter, 'SQL');
27+
const result = mapboxFilterToQueryFilter(filter);
2828
expect(result).toBe("publisher = 'O''Reilly'");
2929
});
3030

3131
it('数值比较 >', () => {
3232
const filter = ['>', 'age', 18];
33-
const result = mapboxFilterToQueryFilter(filter, 'SQL');
33+
const result = mapboxFilterToQueryFilter(filter);
3434
expect(result).toBe('age > 18');
3535
});
3636

3737
it('in 操作符', () => {
3838
const filter = ['in', 'status', 'A', 'B', 'C'];
39-
const result = mapboxFilterToQueryFilter(filter, 'SQL');
39+
const result = mapboxFilterToQueryFilter(filter);
4040
expect(result).toBe("status IN ('A', 'B', 'C')");
4141
});
4242

4343
it('!in 操作符', () => {
4444
const filter = ['!in', 'status', 'A', 'B'];
45-
const result = mapboxFilterToQueryFilter(filter, 'SQL');
45+
const result = mapboxFilterToQueryFilter(filter);
4646
expect(result).toBe("status NOT IN ('A', 'B')");
4747
});
4848

@@ -52,7 +52,7 @@ describe('FilterCondition', () => {
5252
['>', 'age', 18],
5353
['==', 'gender', 'M']
5454
];
55-
const result = mapboxFilterToQueryFilter(filter, 'SQL');
55+
const result = mapboxFilterToQueryFilter(filter);
5656
expect(result).toBe("age > 18 AND gender = 'M'");
5757
});
5858

@@ -62,7 +62,7 @@ describe('FilterCondition', () => {
6262
['==', 'type', 'A'],
6363
['==', 'type', 'B']
6464
];
65-
const result = mapboxFilterToQueryFilter(filter, 'SQL');
65+
const result = mapboxFilterToQueryFilter(filter);
6666
expect(result).toBe("(type = 'A') OR (type = 'B')");
6767
});
6868

@@ -72,47 +72,47 @@ describe('FilterCondition', () => {
7272
['==', 'status', 'disabled'],
7373
['<', 'age', 10]
7474
];
75-
const result = mapboxFilterToQueryFilter(filter, 'SQL');
75+
const result = mapboxFilterToQueryFilter(filter);
7676
expect(result).toBe("NOT (status = 'disabled') AND NOT (age < 10)");
7777
});
7878

7979
it('非法 filter 返回空字符串', () => {
80-
const result = mapboxFilterToQueryFilter(['get', 'name'], 'SQL');
80+
const result = mapboxFilterToQueryFilter(['get', 'name']);
8181
expect(result).toBe('');
8282
});
8383
});
8484

8585
describe('mapboxFilterToQueryFilter - XML', () => {
8686
it('== 普通等值', () => {
8787
const filter = ['==', 'name', 'Tom'];
88-
const result = mapboxFilterToQueryFilter(filter, 'XML');
88+
const result = mapboxFilterToQueryFilter(filter,'WFS', 'XML');
8989
expect(result).toContain('<fes:PropertyIsEqualTo>');
9090
expect(result).toContain('<fes:ValueReference>name</fes:ValueReference>');
9191
expect(result).toContain('<fes:Literal>Tom</fes:Literal>');
9292
});
9393

9494
it('== null 转 PropertyIsNull', () => {
9595
const filter = ['==', 'name', null];
96-
const result = mapboxFilterToQueryFilter(filter, 'XML');
96+
const result = mapboxFilterToQueryFilter(filter,'WFS', 'XML');
9797
expect(result).toContain('<fes:PropertyIsNull>');
9898
});
9999

100100
it('!= null 转 PropertyIsNotNull', () => {
101101
const filter = ['!=', 'name', null];
102-
const result = mapboxFilterToQueryFilter(filter, 'XML');
102+
const result = mapboxFilterToQueryFilter(filter,'WFS', 'XML');
103103
expect(result).toContain('<fes:PropertyIsNotNull>');
104104
});
105105

106106
it('in 转 Or', () => {
107107
const filter = ['in', 'type', 'A', 'B'];
108-
const result = mapboxFilterToQueryFilter(filter, 'XML');
108+
const result = mapboxFilterToQueryFilter(filter,'WFS', 'XML');
109109
expect(result).toContain('<fes:Or>');
110110
const equalCount = (result.match(/<fes:PropertyIsEqualTo>/g) || []).length;
111111
expect(equalCount).toBe(2);
112112
});
113113
it('!in 转 Not + Or', () => {
114114
const filter = ['!in', 'type', 'A', 'B'];
115-
const result = mapboxFilterToQueryFilter(filter, 'XML');
115+
const result = mapboxFilterToQueryFilter(filter,'WFS', 'XML');
116116
expect(result).toContain('<fes:Not>');
117117
expect(result).toContain('<fes:Or>');
118118
});
@@ -123,7 +123,7 @@ describe('FilterCondition', () => {
123123
['>', 'age', 18],
124124
['==', 'gender', 'M']
125125
];
126-
const result = mapboxFilterToQueryFilter(filter, 'XML');
126+
const result = mapboxFilterToQueryFilter(filter,'WFS', 'XML');
127127
expect(result).toContain('<fes:And>');
128128
});
129129

@@ -133,7 +133,7 @@ describe('FilterCondition', () => {
133133
['==', 'type', 'A'],
134134
['==', 'type', 'B']
135135
];
136-
const result = mapboxFilterToQueryFilter(filter, 'XML');
136+
const result = mapboxFilterToQueryFilter(filter,'WFS', 'XML');
137137
expect(result).toContain('<fes:Or>');
138138
});
139139

@@ -143,7 +143,7 @@ describe('FilterCondition', () => {
143143
['==', 'status', 'disabled'],
144144
['<', 'age', 10]
145145
];
146-
const result = mapboxFilterToQueryFilter(filter, 'XML');
146+
const result = mapboxFilterToQueryFilter(filter,'WFS', 'XML');
147147
// == → != , < → >=
148148
expect(result).toContain('PropertyIsNotEqualTo');
149149
expect(result).toContain('PropertyIsGreaterThanOrEqualTo');

0 commit comments

Comments
 (0)