~aafshar/storm/automatic-table-schema-generation

« back to all changes in this revision

Viewing changes to storm/databases/postgres.py

  • Committer: Gustavo Niemeyer
  • Date: 2007-07-18 22:35:21 UTC
  • mfrom: (142.1.16 reserved-words)
  • Revision ID: gustavo@niemeyer.net-20070718223521-8ree9u3myy81jrvr
Merged the reserved-words branch [r=jkakar,radix] [f=126875]

This branch introduces the capability of properly escaping
words defined as reserved by SQL1992.  Backends are also
free to introduce their own reserved words if wanted.

Compatibility notes:

- compiler handlers had their arguments reordered, to support
  a unification that cleaned up the way that handlers are used.
- compile now returns only the statement.  To get parameters,
  one must pass the State explicitly.
- compile.fork() is now compile.create_child() to better
  reflect the relationship between parent and child.
- The __object_info and __class_info attributes are now
  __storm_object_info__ and __storm_class_info__.

Show diffs side-by-side

added added

removed removed

Lines of Context:
38
38
 
39
39
 
40
40
install_exceptions(psycopg2)
41
 
compile = compile.fork()
 
41
compile = compile.create_child()
42
42
 
43
43
 
44
44
class currval(FuncExpr):
49
49
        self.column = column
50
50
 
51
51
@compile.when(currval)
52
 
def compile_currval(compile, state, expr):
53
 
    return "currval('%s_%s_seq')" % (compile(state, expr.column.table),
 
52
def compile_currval(compile, expr, state):
 
53
    return "currval('%s_%s_seq')" % (compile(expr.column.table, state),
54
54
                                     expr.column.name)
55
55
 
56
56
@compile.when(ListVariable)
57
 
def compile_list_variable(compile, state, list_variable):
 
57
def compile_list_variable(compile, list_variable, state):
58
58
    elements = []
59
59
    variables = list_variable.get(to_db=True)
60
60
    if variables is None:
62
62
    if not variables:
63
63
        return "'{}'"
64
64
    for variable in variables:
65
 
        elements.append(compile(state, variable))
 
65
        elements.append(compile(variable, state))
66
66
    return "ARRAY[%s]" % ",".join(elements)
67
67
 
68
68
@compile.when(SetExpr)
69
 
def compile_set_expr_postgres(compile, state, expr):
 
69
def compile_set_expr_postgres(compile, expr, state):
70
70
    if expr.order_by is not Undef:
71
71
        # The following statement breaks in postgres:
72
72
        #     SELECT 1 AS id UNION SELECT 1 ORDER BY id+1
88
88
            state.push("aliases", {})
89
89
 
90
90
        # Build set expression, collecting aliases.
91
 
        set_stmt = SQLRaw("(%s)" % compile_set_expr(compile, state, new_expr))
 
91
        set_stmt = SQLRaw("(%s)" % compile_set_expr(compile, new_expr, state))
92
92
 
93
93
        # Build order_by statement, using aliases.
94
94
        state.push("context", COLUMN_NAME)
95
 
        order_by_stmt = SQLRaw(compile(state, expr.order_by))
 
95
        order_by_stmt = SQLRaw(compile(expr.order_by, state))
96
96
        state.pop()
97
97
 
98
98
        # Discard aliases, if they were not being collected previously.
102
102
        # Build wrapping select statement.
103
103
        select = Select(SQLRaw("*"), tables=Alias(set_stmt), limit=expr.limit,
104
104
                        offset=expr.offset, order_by=order_by_stmt)
105
 
        return compile_select(compile, state, select)
 
105
        return compile_select(compile, select, state)
106
106
    else:
107
 
        return compile_set_expr(compile, state, expr)
 
107
        return compile_set_expr(compile, expr, state)
108
108
 
109
109
 
110
110
class PostgresResult(Result):