~jbaker/storm/nose_plugin

« back to all changes in this revision

Viewing changes to storm/expr.py

  • Committer: Gustavo Niemeyer
  • Date: 2008-06-26 11:57:22 UTC
  • mfrom: (235.2.24 need-for-speed-revenge)
  • Revision ID: gustavo@niemeyer.net-20080626115722-ekl4af6sx2pn08d0
Merging need-for-speed-revenge branch [a=niemeyer,jamesh,radix]
[r=jamesh,therve]

This branch introduces a number of fixes, optimizations, and extensions
on the cextensions module, with speedup purposes.  The module is now
built by default, but still disabled unless the STORM_CEXTENSIONS=1
environment variable is defined.

Besides these, Chris also provided a mechanism to cache ClassAliases,
to prevent the cost of rebuilding the ClassInfo for each alias.

Show diffs side-by-side

added added

removed removed

Lines of Context:
29
29
    Variable, RawStrVariable, UnicodeVariable, LazyValue,
30
30
    DateTimeVariable, DateVariable, TimeVariable, TimeDeltaVariable,
31
31
    BoolVariable, IntVariable, FloatVariable, DecimalVariable)
32
 
from storm import Undef
 
32
from storm import Undef, has_cextensions
33
33
 
34
34
 
35
35
# --------------------------------------------------------------------
36
36
# Basic compiler infrastructure
37
37
 
 
38
def _when(self, types):
 
39
    """Check Compile.when.  Defined here to ease the work of cextensions."""
 
40
    def decorator(method):
 
41
        for type in types:
 
42
            self._local_dispatch_table[type] = method
 
43
        self._update_cache()
 
44
        return method
 
45
    return decorator
 
46
 
 
47
 
38
48
class Compile(object):
39
49
    """Compiler based on the concept of generic functions."""
40
50
 
45
55
        self._dispatch_table = {}
46
56
        self._precedence = {}
47
57
        self._reserved_words = {}
48
 
        self._hash = None
49
58
        self._children = WeakKeyDictionary()
50
59
        self._parents = []
51
60
        if parent:
65
74
        for child in self._children:
66
75
            child._update_cache()
67
76
 
 
77
    def when(self, *types):
 
78
        """Decorator to include a type handler in this compiler.
 
79
 
 
80
        Use this as:
 
81
 
 
82
        @compile.when(TypeA, TypeB)
 
83
        def compile_type_a_or_b(compile, expr, state):
 
84
            ...
 
85
            return "THE COMPILED SQL STATEMENT"
 
86
        """
 
87
        return _when(self, types)
 
88
 
68
89
    def add_reserved_words(self, words):
69
90
        """Include words to be considered reserved and thus escaped.
70
91
 
91
112
        """
92
113
        return self.__class__(self)
93
114
 
94
 
    def when(self, *types):
95
 
        def decorator(method):
96
 
            for type in types:
97
 
                self._local_dispatch_table[type] = method
98
 
            self._update_cache()
99
 
            return method
100
 
        return decorator
101
 
 
102
115
    def get_precedence(self, type):
103
116
        return self._precedence.get(type, MAX_PRECEDENCE)
104
117
 
132
145
            return "(%s)" % statement
133
146
        return statement
134
147
 
135
 
    def __call__(self, expr, state=None, join=", ", raw=False, token=False):
 
148
    def __call__(self, expr, state=None, join=u", ", raw=False, token=False):
136
149
        """Compile the given expression into a SQL statement.
137
150
 
138
151
        @param expr: The expression to compile.
170
183
                    statement = subexpr
171
184
                elif subexpr_type is tuple or subexpr_type is list:
172
185
                    state.precedence = outer_precedence
173
 
                    statement = self(subexpr, state, join, raw)
 
186
                    statement = self(subexpr, state, join, raw, token)
174
187
                else:
175
188
                    if token and (subexpr_type is unicode or
176
189
                                  subexpr_type is str):
186
199
        return statement
187
200
 
188
201
 
 
202
if has_cextensions:
 
203
    from storm.cextensions import Compile
 
204
 
 
205
 
189
206
class CompilePython(Compile):
190
207
 
191
208
    def get_matcher(self, expr):