Skip to content

Commit 5b7205a

Browse files
[fxi]isvj5238 jsonsql数字字段过滤 review by songym
1 parent cdb224f commit 5b7205a

File tree

5 files changed

+96
-22
lines changed

5 files changed

+96
-22
lines changed

src/common/util/FilterCondition.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
function parseCondition(filterCondition, keys) {
2+
const str = filterCondition.replace(/&|\||>|<|=|!/g, ' ');
3+
const arr = str.split(' ').filter((item) => item);
4+
let result = filterCondition;
5+
arr.forEach((item) => {
6+
const key = startsWithNumber(item) && keys.find((val) => val === item);
7+
if (key) {
8+
result = result.replace(key, '$' + key);
9+
}
10+
});
11+
return result;
12+
}
13+
14+
// 处理jsonsqlfeature, 加前缀
15+
function parseConditionFeature(feature) {
16+
let copyValue = {};
17+
for (let key in feature) {
18+
let copyKey = key;
19+
if (startsWithNumber(key)) {
20+
copyKey = '$' + key;
21+
}
22+
copyValue[copyKey] = feature[key];
23+
}
24+
return copyValue;
25+
}
26+
27+
function startsWithNumber(str) {
28+
return /^\d/.test(str);
29+
}
30+
31+
export { parseCondition, parseConditionFeature };

src/mapboxgl/mapping/WebMap.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { GetFeaturesBySQLService } from '@supermap/iclient-common/iServer/GetFea
1313
import { QueryBySQLParameters } from '@supermap/iclient-common/iServer/QueryBySQLParameters';
1414
import { FilterParameter } from '@supermap/iclient-common/iServer/FilterParameter';
1515
import { Lang } from '@supermap/iclient-common/lang/Lang';
16+
import { parseCondition, parseConditionFeature } from '@supermap/iclient-common/util/FilterCondition';
1617
import { Util } from '../core/Util';
1718
import { QueryService } from '../services/QueryService';
1819
import convert from 'xml-js';
@@ -1497,15 +1498,16 @@ export class WebMap extends mapboxgl.Evented {
14971498
return allFeatures;
14981499
}
14991500
let condition = this._replaceFilterCharacter(filterCondition);
1500-
let sql = 'select * from json where (' + condition + ')';
15011501
let filterFeatures = [];
15021502
for (let i = 0; i < allFeatures.length; i++) {
15031503
let feature = allFeatures[i];
15041504
let filterResult = false;
15051505
try {
1506-
filterResult = window.jsonsql.query(sql, {
1507-
properties: feature.properties
1508-
});
1506+
const properties = feature.properties;
1507+
const conditions = parseCondition(condition, Object.keys(properties));
1508+
const filterFeature = parseConditionFeature(properties);
1509+
const sql = 'select * from json where (' + conditions + ')';
1510+
filterResult = window.jsonsql.query(sql, { attr: filterFeature });
15091511
} catch (err) {
15101512
//必须把要过滤得内容封装成一个对象,主要是处理jsonsql(line : 62)中由于with语句遍历对象造成的问题
15111513
continue;

src/openlayers/mapping/WebMap.js

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { ColorsPickerUtil } from '@supermap/iclient-common/util/ColorsPickerUtil
88
import { SecurityManager } from '@supermap/iclient-common/security/SecurityManager';
99
import { Events } from '@supermap/iclient-common/commontypes/Events';
1010
import { Util as CommonUtil} from '@supermap/iclient-common/commontypes/Util';
11+
import { parseCondition, parseConditionFeature } from '@supermap/iclient-common/util/FilterCondition';
1112
import {
1213
Util
1314
} from '../core/Util';
@@ -2749,15 +2750,16 @@ export class WebMap extends Observable {
27492750
*/
27502751
getFiterFeatures(filterCondition, allFeatures) {
27512752
let condition = this.parseFilterCondition(filterCondition);
2752-
let sql = "select * from json where (" + condition + ")";
27532753
let filterFeatures = [];
27542754
for (let i = 0; i < allFeatures.length; i++) {
27552755
let feature = allFeatures[i];
27562756
let filterResult = false;
27572757
try {
2758-
filterResult = window.jsonsql.query(sql, {
2759-
attributes: feature.get('attributes')
2760-
});
2758+
const properties = feature.get('attributes');
2759+
const conditions = parseCondition(condition, Object.keys(properties));
2760+
const filterFeature = parseConditionFeature(properties);
2761+
const sql = 'select * from json where (' + conditions + ')';
2762+
filterResult = window.jsonsql.query(sql, { attributes: filterFeature });
27612763
} catch (err) {
27622764
//必须把要过滤得内容封装成一个对象,主要是处理jsonsql(line : 62)中由于with语句遍历对象造成的问题
27632765
continue;
@@ -3459,11 +3461,12 @@ export class WebMap extends Observable {
34593461
});
34603462
if (layerInfo.filterCondition) {
34613463
//过滤条件
3462-
let condition = that.parseFilterCondition(layerInfo.filterCondition);
3463-
let sql = "select * from json where (" + condition + ")";
3464-
let filterResult = window.jsonsql.query(sql, {
3465-
attributes: feature.get('attributes')
3466-
});
3464+
const condition = that.parseFilterCondition(layerInfo.filterCondition);
3465+
const properties = feature.get('attributes');
3466+
const conditions = parseCondition(condition, Object.keys(properties));
3467+
const filterFeature = parseConditionFeature(properties);
3468+
const sql = 'select * from json where (' + conditions + ')';
3469+
let filterResult = window.jsonsql.query(sql, { attributes: filterFeature });
34673470
if (filterResult && filterResult.length > 0) {
34683471
that.addDataflowFeature(feature, layerInfo.identifyField, {
34693472
dataflowSource: source,
@@ -3610,10 +3613,11 @@ export class WebMap extends Observable {
36103613
if (layerInfo.filterCondition) {
36113614
//过滤条件
36123615
let condition = that.parseFilterCondition(layerInfo.filterCondition);
3613-
let sql = "select * from json where (" + condition + ")";
3614-
let filterResult = window.jsonsql.query(sql, {
3615-
attributes: feature.get('attributes')
3616-
});
3616+
const properties = feature.get('attributes');
3617+
const conditions = parseCondition(condition, Object.keys(properties));
3618+
const filterFeature = parseConditionFeature(properties);
3619+
const sql = 'select * from json where (' + conditions + ')';
3620+
let filterResult = window.jsonsql.query(sql, { attributes: filterFeature });
36173621
if (filterResult && filterResult.length > 0) {
36183622
that.addDataflowFeature(feature, layerInfo.identifyField, {
36193623
dataflowSource: source,

test/mapboxgl/mapping/WebMapSpec.js

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,8 @@ import mapboxgl from 'mapbox-gl';
22
import { WebMap } from '../../../src/mapboxgl/mapping/WebMap';
33
import { FetchRequest } from '@supermap/iclient-common/util/FetchRequest';
44
import { ArrayStatistic } from '../../../src/common/util/ArrayStatistic';
5-
import { ColorsPickerUtil } from '../../../src/common/util/ColorsPickerUtil';
65
import '../../resources/WebMapV5.js';
7-
import img from '../../resources/img/baiduTileTest.png';
8-
import convert from 'xml-js';
9-
import jsonsql from 'jsonsql';
10-
import canvg from 'canvg';
6+
window.jsonsql = { query: () => {} };
117

128
describe('mapboxgl_WebMap', () => {
139
var originalTimeout, testDiv;
@@ -99,6 +95,23 @@ describe('mapboxgl_WebMap', () => {
9995
done();
10096
});
10197
});
98+
it('jsonsql', (done) => {
99+
let options = {
100+
server: server
101+
};
102+
spyOn(FetchRequest, 'get').and.callFake((url) => {
103+
if (url.indexOf('map.json') > -1) {
104+
var mapJson = datavizWebMap_CLOUD;
105+
return Promise.resolve(new Response(mapJson));
106+
}
107+
return Promise.resolve();
108+
});
109+
datavizWebmap = new WebMap(id, options);
110+
datavizWebmap.on('mapinitialized', () => {
111+
datavizWebmap._getFiterFeatures('2020年人口数>20', [{ properties: { '2020年人口数': 30 }}]);
112+
done();
113+
});
114+
});
102115
it('setWebMapOptions', (done) => {
103116
let options = {
104117
server: server

test/openlayers/mapping/WebMapSpec.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ import * as olControl from 'ol/control';
2121
import Feature from 'ol/Feature';
2222
import * as olProj from 'ol/proj';
2323

24+
window.jsonsql = { query: () => {} };
25+
2426
describe('openlayers_WebMap', () => {
2527
var originalTimeout, testDiv, webMap;
2628
var server = "http://127.0.0.1:8090/iportal/";
@@ -177,6 +179,28 @@ describe('openlayers_WebMap', () => {
177179
done();
178180
}
179181
});
182+
183+
it('jsonsql', (done) => {
184+
let options = {
185+
server: server,
186+
successCallback,
187+
errorCallback: function () {}
188+
};
189+
spyOn(FetchRequest, 'get').and.callFake((url) => {
190+
if (url.indexOf('map.json') > -1) {
191+
var mapJson = datavizWebMap_BAIDU;
192+
return Promise.resolve(new Response(mapJson));
193+
}
194+
return Promise.resolve();
195+
});
196+
var datavizWebmap = new WebMap(id, options);
197+
198+
function successCallback() {
199+
datavizWebmap.getFiterFeatures('2020年人口数> 20', [{ get: () => ({ '2020年人口数': 30 }) }]);
200+
datavizWebmap.createDataflowLayer({filterCondition:'2020年人口数> 20', pointStyle:{}}, [{ get: () => ({ '2020年人口数': 30 }) }]);
201+
done();
202+
}
203+
});
180204
it('initialize_OPENSTREET', (done) => {
181205
let options = {
182206
server: server,

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy