Commits

Olemis Lang committed ebc2f81

GViz QL : Add support for 'like' pattern matching operator ... [ok]

gvizql.py Total tests : 62 , Failures : 0 , Errors : 1

Comments (0)

Files changed (2)

trac-dev/gviz/tracgviz/gvizql.py

     lexpr = lexpr[1]
     rexpr = rexpr[1]
 
-    if op == 'like':
-      raise NotImplementedError('Try `matches` instead of `like`')
-
     f = {
         '+' : 'add', '-' : 'sub', '*' : 'mul', # '/' : 'div',
         'and' : 'and_', 'or' : 'or_',
       f = getattr(operator, f)
     else:
       from re import match
+      from fnmatch import fnmatch
 
       def div(a, b):
         try:
           return operator.div(a, b)
         except ZeroDivisionError:
           return None
+
+      def match_like(a, b):
+        pattern = b.replace('*', '[*]').replace('?', '[?]') \
+            .replace('%', '*').replace('_', '?')
+        return fnmatch(a, pattern)
+
       f = {
           '/' : div, 
           'starts with' : lambda a, b : a.startswith(b),
           'ends with' : lambda a, b : a.endswith(b),
           'matches' : lambda a, b: bool(match('^%s$' % (b,), a)),
+          'like' : match_like,
         }.get(op)
 
       if f is None:
               print "= Row ="
               for val, col in izip(row, iter_schema()):
                 print '  ', col[0], '=', val
-          except (GVizInvalidQuery, GVizRuntimeError), exc :
+          except (GVizInvalidQuery, GVizRuntimeError, NotImplementedError), exc:
             print 
             print exc.__class__.__name__, ' : ', unicode(exc)
             

trac-dev/gviz/tracgviz/testing/test_gvizql.py

       *****
       * Parsing
       *****
-      NotSupported  :(
+      [u'max(salary)']
+      *****
+      * Result
+      *****
+      = Columns =
+      max(salary) number
+      <BLANKLINE>
+      NotImplementedError  :  Feature not supported ... yet
+
       """,
   'Parsing FROM' : r"""
       >>> parse("  from employees", 'basetable') 
       *****
       * Parsing
       *****
-      NotSupported  :(
+      name like 'fre%'
+      *****
+      * Result
+      *****
+      = Columns =
+      name string
+      dept string
+      lunchTime timeofday
+      salary number
+      hireDate date
+      age number
+      isSenior boolean
+      seniorityStartTime datetime
+
 
       """,
   'Parsing GROUP BY (simple)' : r"""