svn keywords
[plcapi.git] / PLC / Filter.py
index 807ce00..8a7ba31 100644 (file)
@@ -1,4 +1,5 @@
 # $Id$
+# $URL$
 from types import StringTypes
 try:
     set
@@ -63,8 +64,8 @@ class Filter(Parameter, dict):
     example : filter = { '-OFFSET' : 100, '-LIMIT':25}
 
     A realistic example would read
-    GetNodes ( { 'hostname' : '*.edu' , '-SORT' : 'hostname' , '-OFFSET' : 30 , '-LIMIT' : 25 } )
-    and that would return nodes matching '*.edu' in alphabetical order from 31th to 55th
+    GetNodes ( { 'node_type' : 'regular' , 'hostname' : '*.edu' , '-SORT' : 'hostname' , '-OFFSET' : 30 , '-LIMIT' : 25 } )
+    and that would return regular (usual) nodes matching '*.edu' in alphabetical order from 31th to 55th
     """
 
     def __init__(self, fields = {}, filter = {}, doc = "Attribute filter"):
@@ -120,13 +121,18 @@ class Filter(Parameter, dict):
                     raise PLCInvalidArgument, "Invalid filter field '%s'" % field
 
                 if isinstance(value, (list, tuple, set)):
-                    # Turn empty list into (NULL) instead of invalid ()
+                    # handling filters like '~slice_id':[]
+                    # this should return true, as it's the opposite of 'slice_id':[] which is false
+                    # prior to this fix, 'slice_id':[] would have returned ``slice_id IN (NULL) '' which is unknown 
+                    # so it worked by coincidence, but the negation '~slice_ids':[] would return false too
                     if not value:
-                        value = [None]
-
-                    operator = "IN"
-                    value = map(str, map(api.db.quote, value))
-                    value = "(%s)" % ", ".join(value)
+                        field=""
+                        operator=""
+                        value = "FALSE"
+                    else:
+                        operator = "IN"
+                        value = map(str, map(api.db.quote, value))
+                        value = "(%s)" % ", ".join(value)
                 else:
                     if value is None:
                         operator = "IS"
@@ -134,7 +140,12 @@ class Filter(Parameter, dict):
                     elif isinstance(value, StringTypes) and \
                             (value.find("*") > -1 or value.find("%") > -1):
                         operator = "LIKE"
-                        value = str(api.db.quote(value.replace("*", "%")))
+                        # insert *** in pattern instead of either * or %
+                        # we dont use % as requests are likely to %-expansion later on
+                        # actual replacement to % done in PostgreSQL.py
+                        value = value.replace ('*','***')
+                        value = value.replace ('%','***')
+                        value = str(api.db.quote(value))
                     else:
                         operator = "="
                         if modifiers['<']: