~djfroofy/storm/oracle-support-patches

« back to all changes in this revision

Viewing changes to storm/databases/oracle.py

  • Committer: Drew Smathers
  • Date: 2009-05-16 16:16:12 UTC
  • Revision ID: drew@kieru-20090516161612-9sfgx74eoukxvwen
play nicely with statement caching by using consistent Aliases in query compilation [f=377312]

Show diffs side-by-side

added added

removed removed

Lines of Context:
55
55
install_exceptions(oracle)
56
56
compile = compile.create_child()
57
57
 
 
58
def alias_names():
 
59
    ct = 0
 
60
    while 1:
 
61
        yield '_%x' % ct
 
62
        ct += 1
 
63
 
58
64
@compile.when(type)
59
65
def compile_type(compile, expr, state):
60
66
    cls_info = get_cls_info(expr)
100
106
 
101
107
@compile.when(SetExpr)
102
108
def compile_set_expr_oracle(compile, expr, state):
 
109
 
 
110
    names = alias_names()
 
111
 
103
112
    if isinstance(expr, Minus):
104
113
        # Build new set expression without arguments (order_by, etc).
105
114
        new_expr = expr.__class__()
124
133
                    for i, column in enumerate(columns):
125
134
                        if column not in aliases:
126
135
                            if isinstance(column, Column):
127
 
                                aliases[column] = columns[i] = Alias(column)
 
136
                                aliases[column] = columns[i] = Alias(column, name=names.next())
128
137
                            elif isinstance(column, Alias):
129
138
                                aliases[column.expr] = column
130
139
                    subexpr.columns = columns
143
152
            order_by_stmt = Undef
144
153
 
145
154
        # Build wrapping select statement.
146
 
        select = Select(SQLRaw("*"), tables=Alias(set_stmt), limit=expr.limit,
 
155
        select = Select(SQLRaw("*"), tables=Alias(set_stmt, name=names.next()), limit=expr.limit,
147
156
                        offset=expr.offset, order_by=order_by_stmt)
148
157
 
149
158
        return compile_select(compile, select, state)
150
159
    return compile_set_expr(compile, expr, state)
151
160
 
 
161
 
 
162
 
152
163
@compile.when(Select)
153
164
def compile_select_oracle(compile, select, state):
154
165
    limit = select.limit
156
167
    # make sure limit is Undef'ed
157
168
    select.offset = select.limit = Undef
158
169
 
 
170
    names = alias_names()
 
171
 
159
172
    if select.default_tables is Undef:
160
173
        select.default_tables = ['DUAL']
161
174
 
170
183
        for i, column in enumerate(columns):
171
184
            if column not in aliases:
172
185
                if isinstance(column, Column):
173
 
                    aliases[column] = columns[i] = Alias(column)
 
186
                    aliases[column] = columns[i] = Alias(column, name=names.next())
174
187
                elif isinstance(column, Alias):
175
188
                    aliases[column.expr] = column
176
189
        select.columns = columns
177
190
        # /copied from expr.py's compile_set_expr
178
191
        stmt = SQLRaw("(%s)" % compile_select(compile, select, state))
179
 
        select = Select(SQLRaw('*'), tables=Alias(stmt))
 
192
        select = Select(SQLRaw('*'), tables=Alias(stmt, name=names.next()))
180
193
 
181
194
    if (limit is not Undef) and (offset is not Undef):
182
 
        rownum_alias = Alias(SQLRaw('ROWNUM'))
 
195
        rownum_alias = Alias(SQLRaw('ROWNUM'), name=names.next())
183
196
 
184
197
        # if we have an SQLRaw here that is because we are dealing
185
198
        # with a subquery
195
208
            select.where = And(select.where, where_expr)
196
209
 
197
210
        stmt = SQLRaw("(%s)" % compile_select(compile, select, state))
198
 
        select = Select(SQLRaw('*'), tables=Alias(stmt), where = Gt(rownum_alias, offset))
 
211
        select = Select(SQLRaw('*'), tables=Alias(stmt, names.next()), where = Gt(rownum_alias, offset))
199
212
    elif limit is not Undef:
200
213
        expr = Le(SQLRaw('ROWNUM'), limit)
201
214
        if select.where is Undef:
203
216
        else:
204
217
            select.where = And(select.where, expr)
205
218
    elif offset is not Undef:
206
 
        rownum_alias = Alias(SQLRaw('ROWNUM'))
 
219
        rownum_alias = Alias(SQLRaw('ROWNUM'), name=names.next())
207
220
 
208
221
        # if we have an SQLRaw here that is because we are dealing
209
222
        # with a subquery
213
226
            select.columns.append(rownum_alias)
214
227
 
215
228
        stmt = SQLRaw("(%s)" % compile_select(compile, select, state))
216
 
        select = Select(SQLRaw('*'), tables=Alias(stmt), where = Gt(rownum_alias, offset))
 
229
        select = Select(SQLRaw('*'), tables=Alias(stmt, name=names.next()), where = Gt(rownum_alias, offset))
217
230
 
218
231
    return compile_select(compile, select, state)
219
232