Revision 95d47e1a pithos/backends/lib/sqlite/node.py

b/pithos/backends/lib/sqlite/node.py
31 31
# interpreted as representing official policies, either expressed
32 32
# or implied, of GRNET S.A.
33 33

  
34
import re
35

  
34 36
from time import time
35 37

  
36 38
from dbworker import DBWorker
......
78 80
    return s
79 81

  
80 82

  
83
_regexfilter = re.compile('(!?)\s*([\w-]+)\s*(==|!=|<=|>=|<|>)?\s*(.*)$', re.UNICODE)
84

  
81 85
_propnames = {
82 86
    'serial'    : 0,
83 87
    'node'      : 1,
......
589 593
             "where serial = ?")
590 594
        self.execute(q, (dest, source))
591 595
    
596
    def _parse_filters(self, filterq):
597
        preterms = filterq.split(',')
598
        included = []
599
        excluded = []
600
        opers = []
601
        match = _regexfilter.match
602
        for term in preterms:
603
            m = match(term)
604
            if m is None:
605
                continue
606
            neg, key, op, value = m.groups()
607
            if neg:
608
                excluded.append(key)
609
            elif not value:
610
                included.append(key)
611
            elif op:
612
                opers.append((key, op, value))
613
        
614
        return included, excluded, opers
615
    
592 616
    def _construct_filters(self, filterq):
593 617
        if not filterq:
594 618
            return None, None
595 619
        
596
        args = filterq.split(',')
597
        subq = " and a.key in ("
598
        subq += ','.join(('?' for x in args))
599
        subq += ")"
620
        subqlist = []
621
        append = subqlist.append
622
        included, excluded, opers = self._parse_filters(filterq)
623
        args = []
624
        
625
        if included:
626
            subq = "a.key in ("
627
            subq += ','.join(('?' for x in included)) + ")"
628
            args += included
629
            append(subq)
630
        
631
        if excluded:
632
            subq = "a.key not in ("
633
            subq += ','.join(('?' for x in excluded)) + ")"
634
            args += excluded
635
            append(subq)
636
        
637
        if opers:
638
            t = (("(key = ? and value %s ?)" % (o,)) for k, o, v in opers)
639
            subq = "(" + ' or '.join(t) + ")"
640
            for k, o, v in opers:
641
                args += (k, v)
642
            append(subq)
643
        
644
        if not subqlist:
645
            return None, None
646
        
647
        subq = ' ' + ' and '.join(subqlist)
600 648
        
601 649
        return subq, args
602 650
    

Also available in: Unified diff