~allenap/storm/blocked-references

« back to all changes in this revision

Viewing changes to storm/expr.py

  • Committer: Jamu Kakar
  • Date: 2009-11-24 18:34:34 UTC
  • mfrom: (340 storm.trunk)
  • mto: This revision was merged to the branch mainline in revision 341.
  • Revision ID: jkakar@kakar.ca-20091124183434-ambkvw9tp2byvjnc
- Merged trunk.

Show diffs side-by-side

added added

removed removed

Lines of Context:
395
395
    raise CompileError("Can't compile python expressions with %r" % type(expr))
396
396
 
397
397
 
 
398
# A translation table that can escape a unicode string for use in a
 
399
# Like() expression that uses "!" as the escape character.
 
400
like_escape = {
 
401
    ord(u"!"): u"!!",
 
402
    ord(u"_"): u"!_",
 
403
    ord(u"%"): u"!%"
 
404
    }
 
405
 
 
406
 
398
407
class Comparable(object):
399
408
    __slots__ = ()
400
409
 
498
507
    def upper(self):
499
508
        return Upper(self)
500
509
 
 
510
    def startswith(self, prefix):
 
511
        if not isinstance(prefix, unicode):
 
512
            raise ExprError("Expected unicode argument, got %r" % type(prefix))
 
513
        pattern = prefix.translate(like_escape) + u"%"
 
514
        return Like(self, pattern, u"!")
 
515
 
 
516
    def endswith(self, suffix):
 
517
        if not isinstance(suffix, unicode):
 
518
            raise ExprError("Expected unicode argument, got %r" % type(suffix))
 
519
        pattern = u"%" + suffix.translate(like_escape)
 
520
        return Like(self, pattern, u"!")
 
521
 
 
522
    def contains_string(self, substring):
 
523
        if not isinstance(substring, unicode):
 
524
            raise ExprError("Expected unicode argument, got %r" % type(substring))
 
525
        pattern = u"%" + substring.translate(like_escape) + u"%"
 
526
        return Like(self, pattern, u"!")
 
527
 
501
528
 
502
529
class ComparableExpr(Expr, Comparable):
503
530
    __slots__ = ()
1099
1126
        self.order_by = kwargs.get("order_by", Undef)
1100
1127
        self.limit = kwargs.get("limit", Undef)
1101
1128
        self.offset = kwargs.get("offset", Undef)
 
1129
        # If the first expression is of a compatible type, directly
 
1130
        # include its sub expressions.
 
1131
        if len(self.exprs) > 0:
 
1132
            first = self.exprs[0]
 
1133
            if (isinstance(first, self.__class__) and
 
1134
                first.all == self.all and
 
1135
                first.limit is Undef and
 
1136
                first.offset is Undef):
 
1137
                self.exprs = first.exprs + self.exprs[1:]
 
1138
 
1102
1139
 
1103
1140
@compile.when(SetExpr)
1104
1141
def compile_set_expr(compile, expr, state):