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

     
    224224    filter = filter.to_sql_expr() 
    225225    params = filter.compile().params 
    226226    assert str(filter) == '"table".text LIKE :text_1' 
    227     assert params["text_1"] == "%foo%" 
     227    assert params["text_1"] == "foo" 
    228228 
    229229def test_comparison_ilike(): 
    230230    filter = comparison.Comparison( 
     
    235235    filter = filter.to_sql_expr() 
    236236    params = filter.compile().params 
    237237    assert str(filter) == 'lower("table".text) LIKE lower(:text_1)' 
    238     assert params["text_1"] == "%foo%" 
     238    assert params["text_1"] == "foo" 
    239239 
    240240# 
    241241# Test FeatureId 
     
    284284    filter = filter.to_sql_expr() 
    285285    params = filter.compile().params 
    286286    assert str(filter) == '"table".text NOT LIKE :text_1' 
    287     assert params["text_1"] == "%foo%" 
     287    assert params["text_1"] == "foo" 
    288288 
    289289def test_logical_and(): 
    290290    filter = logical.Logical( 
     
    306306    params = filter.compile().params 
    307307    assert str(filter) == '"table".id = :id_1 AND "table".text LIKE :text_1' 
    308308    assert params["id_1"] == 1 
    309     assert params["text_1"] == "%foo%" 
     309    assert params["text_1"] == "foo" 
    310310 
    311311def test_logical_or(): 
    312312    filter = logical.Logical( 
     
    328328    params = filter.compile().params 
    329329    assert str(filter) == '"table".id = :id_1 OR "table".text LIKE :text_1' 
    330330    assert params["id_1"] == 1 
    331     assert params["text_1"] == "%foo%" 
     331    assert params["text_1"] == "foo" 
  • server/python/mapfish/lib/filters/comparison.py

     
    9292            ) 
    9393 
    9494        if self.type == Comparison.LIKE: 
    95             return self.column.like('%' + self.values['value'] + '%') 
     95            return self.column.like(self.values['value']) 
    9696 
    9797        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

     
    117117                     "protocol.read is passed a LIKE filter"); 
    118118                t.eq(options.filter.property, "bar", 
    119119                     "protocol.read is passed a filter with expected property"); 
    120                 t.eq(options.filter.value, q, 
     120                t.eq(options.filter.value, ".*" + q + ".*", 
    121121                     "protocol.read is passed a filter with expected value"); 
    122122            }; 
    123123 
  • client/mfbase/mapfish/tests/core/Protocol/MapFish.html

     
    6464    } 
    6565 
    6666    function test_MapFish_filterAdapter(t) { 
    67         t.plan(43); 
     67        t.plan(46); 
    6868 
    6969        // setup 
    7070 
     
    367367        t.eq(options.filter, undefined, 
    368368             "filterAdapter deletes filter"); 
    369369 
     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)"); 
    370387    } 
    371388 
     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 
    372450    function test_MapFish_update(t) { 
    373451        t.plan(9); 
    374452 
  • client/mfbase/mapfish/widgets/recenter/DataField.js

     
    151151        // itself passes these options to protocol.read 
    152152        store.on({ 
    153153            beforeload: function(store, options) { 
     154                var value = ".*" + store.baseParams[combo.queryParam] + ".*"; 
    154155                options.filter = new OpenLayers.Filter.Comparison({ 
    155156                    type: OpenLayers.Filter.Comparison.LIKE, 
    156157                    property: combo.queryParam, 
    157                     value: store.baseParams[combo.queryParam] 
     158                    value: value 
    158159                }); 
    159160                // remove the queryParam from the store's base 
    160161                // params not to pollute the query string 
  • client/mfbase/mapfish/core/Protocol/MapFish.js

     
    3737 
    3838mapfish.Protocol.MapFish = OpenLayers.Class(OpenLayers.Protocol.HTTP, { 
    3939    /** 
     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    /** 
    4051     * Constructor: mapfish.Protocol.MapFish 
    4152     * 
    4253     * Parameters: 
     
    203214                        'Unknown comparison filter type ' + filter.type); 
    204215                    return false; 
    205216                } 
    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; 
    207225                params["queryable"] = params["queryable"] || []; 
    208226                params["queryable"].push(filter.property); 
    209227                break; 
     
    231249    }, 
    232250 
    233251    /** 
     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    /** 
    234300     * Method: filterAdapter 
    235301     *      If params has a filter property and if that filter property 
    236302     *      is an OpenLayers.Filter that the MapFish protocol can deal