Ticket #495: patch-MapFish-495-r2707-A2.diff
| File patch-MapFish-495-r2707-A2.diff, 10.6 kB (added by elemoine, 3 years ago) |
|---|
-
server/python/mapfish/tests/test_filter.py
224 224 filter = filter.to_sql_expr() 225 225 params = filter.compile().params 226 226 assert str(filter) == '"table".text LIKE :text_1' 227 assert params["text_1"] == " %foo%"227 assert params["text_1"] == "foo" 228 228 229 229 def test_comparison_ilike(): 230 230 filter = comparison.Comparison( … … 235 235 filter = filter.to_sql_expr() 236 236 params = filter.compile().params 237 237 assert str(filter) == 'lower("table".text) LIKE lower(:text_1)' 238 assert params["text_1"] == " %foo%"238 assert params["text_1"] == "foo" 239 239 240 240 # 241 241 # Test FeatureId … … 284 284 filter = filter.to_sql_expr() 285 285 params = filter.compile().params 286 286 assert str(filter) == '"table".text NOT LIKE :text_1' 287 assert params["text_1"] == " %foo%"287 assert params["text_1"] == "foo" 288 288 289 289 def test_logical_and(): 290 290 filter = logical.Logical( … … 306 306 params = filter.compile().params 307 307 assert str(filter) == '"table".id = :id_1 AND "table".text LIKE :text_1' 308 308 assert params["id_1"] == 1 309 assert params["text_1"] == " %foo%"309 assert params["text_1"] == "foo" 310 310 311 311 def test_logical_or(): 312 312 filter = logical.Logical( … … 328 328 params = filter.compile().params 329 329 assert str(filter) == '"table".id = :id_1 OR "table".text LIKE :text_1' 330 330 assert params["id_1"] == 1 331 assert params["text_1"] == " %foo%"331 assert params["text_1"] == "foo" -
server/python/mapfish/lib/filters/comparison.py
92 92 ) 93 93 94 94 if self.type == Comparison.LIKE: 95 return self.column.like( '%' + self.values['value'] + '%')95 return self.column.like(self.values['value']) 96 96 97 97 if self.type == Comparison.ILIKE: 98 return self.column.ilike( '%' + self.values['value'] + '%')98 return self.column.ilike(self.values['value']) -
client/mfbase/mapfish/tests/widgets/recenter/DataField.html
117 117 "protocol.read is passed a LIKE filter"); 118 118 t.eq(options.filter.property, "bar", 119 119 "protocol.read is passed a filter with expected property"); 120 t.eq(options.filter.value, q,120 t.eq(options.filter.value, ".*" + q + ".*", 121 121 "protocol.read is passed a filter with expected value"); 122 122 }; 123 123 -
client/mfbase/mapfish/tests/core/Protocol/MapFish.html
64 64 } 65 65 66 66 function test_MapFish_filterAdapter(t) { 67 t.plan(4 3);67 t.plan(46); 68 68 69 69 // setup 70 70 … … 367 367 t.eq(options.filter, undefined, 368 368 "filterAdapter deletes filter"); 369 369 370 // 3 tests 371 protocol = new mapfish.Protocol.MapFish({wildcarded: true}); 372 options = { 373 filter: new OpenLayers.Filter.Comparison({ 374 type: OpenLayers.Filter.Comparison.LIKE, 375 property: "foo", 376 value: "bar" 377 }) 378 }; 379 380 protocol.filterAdapter(options); 381 t.eq(options.params["queryable"][0], "foo", 382 "filterAdapter sets correct queryable param if passed a LIKE filter (wildcarded true)"); 383 t.eq(options.params["foo__ilike"], "%bar%", 384 "filterAdapter sets correct param key and value if passed an LIKE filter (wildcarded true)"); 385 t.eq(options.filter, undefined, 386 "filterAdapter deletes filter (wildcarded true)"); 370 387 } 371 388 389 function test_MapFish_regex2value(t) { 390 t.plan(16); 391 392 // setup 393 394 var protocol = new mapfish.Protocol.MapFish(); 395 var value; 396 397 // test 398 399 value = protocol.regex2value("foo"); 400 t.eq(value, "foo", 'regex2value converts "foo" to "foo"'); 401 402 value = protocol.regex2value("foo%"); 403 t.eq(value, "foo\\%", 'regex2value converts "foo%" to "foo\\%"'); 404 405 value = protocol.regex2value("foo.*"); 406 t.eq(value, "foo%", 'regex2value converts "foo.*" to "foo%"'); 407 408 value = protocol.regex2value("f.*oo.*"); 409 t.eq(value, "f%oo%", 'regex2value converts "f.*oo.*" to "f%oo%"'); 410 411 value = protocol.regex2value("foo."); 412 t.eq(value, "foo_", 'regex2value converts "foo." to "foo_"'); 413 414 value = protocol.regex2value("f.oo."); 415 t.eq(value, "f_oo_", 'regex2value converts "f.oo." to "f_oo_"'); 416 417 value = protocol.regex2value("f.oo.*"); 418 t.eq(value, "f_oo%", 'regex2value converts "f.oo.*" to "f_oo%"'); 419 420 value = protocol.regex2value("foo\\\\"); 421 t.eq(value, "foo\\\\", 'regex2value converts "foo\\\\" to "foo\\\\"'); 422 423 value = protocol.regex2value("foo\\."); 424 t.eq(value, "foo.", 'regex2value converts "foo\\." to "foo."'); 425 426 value = protocol.regex2value("foo\\\\."); 427 t.eq(value, "foo\\\\_", 'regex2value converts "foo\\\\." to "foo\\\\_"'); 428 429 value = protocol.regex2value("foo\\*"); 430 t.eq(value, "foo*", 'regex2value converts "foo\\*" to "foo*"'); 431 432 value = protocol.regex2value("foo\\\\*"); 433 t.eq(value, "foo\\\\*", 'regex2value converts "foo\\\\*" to "foo\\\\*"'); 434 435 value = protocol.regex2value("foo\\\\.*"); 436 t.eq(value, "foo\\\\%", 'regex2value converts "foo\\\\.*" to "foo\\\\%"'); 437 438 value = protocol.regex2value("fo\\.o.*"); 439 t.eq(value, "fo.o%", 'regex2value converts from "fo\\.o.*" to "fo.o%"'); 440 441 value = protocol.regex2value("fo.*o\\."); 442 t.eq(value, "fo%o.", 'regex2value converts from "fo.*o\\." to "to%o."'); 443 444 value = protocol.regex2value("\\*\\..*.\\\\.*\\\\.%"); 445 t.eq(value, "*.%_\\\\%\\\\_\\%", 446 'regex2value converts from "\\*\\..*.\\\\.*\\\\.%" ' + 447 'to "*.%_\\\\%\\\\_\\%"'); 448 } 449 372 450 function test_MapFish_update(t) { 373 451 t.plan(9); 374 452 -
client/mfbase/mapfish/widgets/recenter/DataField.js
151 151 // itself passes these options to protocol.read 152 152 store.on({ 153 153 beforeload: function(store, options) { 154 var value = ".*" + store.baseParams[combo.queryParam] + ".*"; 154 155 options.filter = new OpenLayers.Filter.Comparison({ 155 156 type: OpenLayers.Filter.Comparison.LIKE, 156 157 property: combo.queryParam, 157 value: store.baseParams[combo.queryParam]158 value: value 158 159 }); 159 160 // remove the queryParam from the store's base 160 161 // params not to pollute the query string -
client/mfbase/mapfish/core/Protocol/MapFish.js
37 37 38 38 mapfish.Protocol.MapFish = OpenLayers.Class(OpenLayers.Protocol.HTTP, { 39 39 /** 40 * APIProperty: wildcarded. 41 * {Boolean} If true percent signs are added around values 42 * read from LIKE filters, for example if the protocol 43 * read method is passed a LIKE filter whose property 44 * is "foo" and whose value is "bar" the string 45 * "foo__ilike=%bar%" will be sent in the query string; 46 * defaults to false. 47 */ 48 wildcarded: false, 49 50 /** 40 51 * Constructor: mapfish.Protocol.MapFish 41 52 * 42 53 * Parameters: … … 203 214 'Unknown comparison filter type ' + filter.type); 204 215 return false; 205 216 } 206 params[filter.property + "__" + op] = filter.value; 217 var value = filter.value; 218 if (filter.type == OpenLayers.Filter.Comparison.LIKE) { 219 value = this.regex2value(value); 220 if (this.wildcarded) { 221 value = "%" + value + "%"; 222 } 223 } 224 params[filter.property + "__" + op] = value; 207 225 params["queryable"] = params["queryable"] || []; 208 226 params["queryable"].push(filter.property); 209 227 break; … … 231 249 }, 232 250 233 251 /** 252 * Method: regex2value 253 * Convert the value from a regular expression string to a LIKE/ILIKE 254 * string known to the MapFish web service. 255 * 256 * Parameters: 257 * value - {String} The regex string. 258 * 259 * Returns: 260 * {String} The converted string. 261 */ 262 regex2value: function(value) { 263 264 // highly sensitive!! Do not change this without running the 265 // Protocol/MapFish.html unit tests 266 267 // convert % to \% 268 value = value.replace(/%/g, "\\%"); 269 270 // convert \\. to \\_ (\\.* occurences converted later) 271 value = value.replace(/\\\\\.(\*)?/g, function($0, $1) { 272 return $1 ? $0 : "\\\\_"; 273 }); 274 275 // convert \\.* to \\% 276 value = value.replace(/\\\\\.\*/g, "\\\\%"); 277 278 // convert . to _ (\. and .* occurences converted later) 279 value = value.replace(/(\\)?\.(\*)?/g, function($0, $1, $2) { 280 return $1 || $2 ? $0 : "_"; 281 }); 282 283 // convert .* to % (\.* occurnces converted later) 284 value = value.replace(/(\\)?\.\*/g, function($0, $1) { 285 return $1 ? $0 : "%"; 286 }); 287 288 // convert \. to . 289 value = value.replace(/\\\./g, "."); 290 291 // replace \* with * (watching out for \\*) 292 value = value.replace(/(\\)?\\\*/g, function($0, $1) { 293 return $1 ? $0 : "*"; 294 }); 295 296 return value; 297 }, 298 299 /** 234 300 * Method: filterAdapter 235 301 * If params has a filter property and if that filter property 236 302 * is an OpenLayers.Filter that the MapFish protocol can deal
