Project

General

Profile

1
# SQL code generation
2

    
3
import sql
4
import strings
5
import util
6

    
7
##### SQL code objects
8

    
9
class Code:
10
    def to_str(self, db): raise NotImplemented()
11
    
12
    def __str__(self): return str(self.__dict__)
13

    
14
class CustomCode:
15
    def __init__(self, str_): self.str_ = str_
16
    
17
    def to_str(self, db): return self.str_
18

    
19
class Literal(Code):
20
    def __init__(self, value): self.value = value
21
    
22
    def to_str(self, db): return db.esc_value(self.value)
23

    
24
def is_null(value): return isinstance(value, Literal) and value.value == None
25

    
26
class Table(Code):
27
    def __init__(self, name, schema=None):
28
        '''
29
        @param schema str|None (for no schema)
30
        '''
31
        self.name = name
32
        self.schema = schema
33
    
34
    def to_str(self, db): return sql.qual_name(db, self.schema, self.name)
35

    
36
def as_Table(table):
37
    if table == None or isinstance(table, Table): return table
38
    elif isinstance(table, tuple):
39
        schema, table = table
40
        return Table(table, schema)
41
    else: return Table(table)
42

    
43
class Col(Code):
44
    def __init__(self, name, table=None):
45
        '''
46
        @param table Table|None (for no table)
47
        '''
48
        if util.is_str(table): table = Table(table)
49
        assert table == None or isinstance(table, Table)
50
        
51
        self.name = name
52
        self.table = table
53
    
54
    def to_str(self, db):
55
        str_ = ''
56
        if self.table != None: str_ += self.table.to_str(db)+'.'
57
        str_ += sql.esc_name(db, self.name)
58
        return str_
59

    
60
def as_Col(col, table=None):
61
    if col == None or isinstance(col, Code): return col
62
    else: return Col(col, table)
63

    
64
class NamedCode(Code):
65
    def __init__(self, name, code):
66
        if not isinstance(code, Code): code = Literal(code)
67
        
68
        self.name = name
69
        self.code = code
70
    
71
    def to_str(self, db):
72
        return self.code.to_str(db)+' AS '+sql.esc_name(db, self.name)
73

    
74
##### Parameterized SQL code objects
75

    
76
class ValueCond:
77
    def __init__(self, value):
78
        if not isinstance(value, Code): value = Literal(value)
79
        
80
        self.value = value
81
    
82
    def to_str(self, db, left_value):
83
        '''
84
        @param left_value The Code object that the condition is being applied on
85
        '''
86
        raise NotImplemented()
87
    
88
    def __str__(self): return str(self.__dict__)
89

    
90
class CompareCond(ValueCond):
91
    def __init__(self, value, operator='='):
92
        '''
93
        @param operator By default, compares NULL values literally. Use '~=' or
94
            '~!=' to pass NULLs through.
95
        '''
96
        ValueCond.__init__(self, value)
97
        self.operator = operator
98
    
99
    def to_str(self, db, left_value):
100
        if not isinstance(left_value, Code): left_value = Col(left_value)
101
        
102
        right_value = self.value
103
        left = left_value.to_str(db)
104
        right = right_value.to_str(db)
105
        
106
        # Parse operator
107
        operator = self.operator
108
        passthru_null_ref = [False]
109
        operator = strings.remove_prefix('~', operator, passthru_null_ref)
110
        neg_ref = [False]
111
        operator = strings.remove_prefix('!', operator, neg_ref)
112
        equals = operator.endswith('=')
113
        if equals and is_null(self.value): operator = 'IS'
114
        
115
        # Create str
116
        str_ = left+' '+operator+' '+right
117
        if equals and not passthru_null_ref[0] and isinstance(right_value, Col):
118
            str_ += ' OR ('+left+' IS NULL AND '+right+' IS NULL)'
119
        if neg_ref[0]: str_ = 'NOT ('+str_+')'
120
        return str_
121

    
122
# Tells as_ValueCond() to assume a non-ValueCond is a literal value
123
assume_literal = object()
124

    
125
def as_ValueCond(value, default_table=assume_literal):
126
    if not isinstance(value, ValueCond):
127
        if default_table is not assume_literal:
128
            value = as_Col(value, default_table)
129
        return CompareCond(value)
130
    else: return value
131

    
132
join_using = object() # tells Join to join the column with USING
133

    
134
filter_out = object() # tells Join to filter out rows that match the join
135

    
136
class Join(Code):
137
    def __init__(self, table, mapping, type_=None):
138
        '''
139
        @param mapping dict(right_table_col=left_table_col, ...)
140
            * if left_table_col is join_using: left_table_col = right_table_col
141
        @param type_ None (for plain join)|str (e.g. 'LEFT')|filter_out
142
            * filter_out: equivalent to 'LEFT' with the query filtered by
143
              `table_pkey IS NULL` (indicating no match)
144
        '''
145
        if util.is_str(table): table = Table(table)
146
        assert type_ == None or util.is_str(type_) or type_ is filter_out
147
        
148
        self.table = table
149
        self.mapping = mapping
150
        self.type_ = type_
151
    
152
    def to_str(self, db, left_table):
153
        def join(entry):
154
            '''Parses non-USING joins'''
155
            right_table_col, left_table_col = entry
156
            
157
            # Parse special values
158
            if left_table_col is join_using: left_table_col = right_table_col
159
            
160
            cond = as_ValueCond(right_table_col, self.table)
161
            return cond.to_str(db, as_Col(left_table_col, left_table))
162
        
163
        # Create join condition and determine join type
164
        if reduce(operator.and_, (v is join_using for v in joins.itervalues())):
165
            # all cols w/ USING, so can use simpler USING syntax
166
            join_cond = 'USING ('+(', '.join(joins.iterkeys()))+')'
167
        else: join_cond = 'ON '+(' AND '.join(map(join, joins.iteritems())))
168
        
169
        # Create join
170
        type_ = self.type_
171
        if type_ is filter_out: type_ = 'LEFT'
172
        return type_+' JOIN '+table+' '+join_cond
173

    
174
##### Old-style format support
175

    
176
def unescape_table(table):
177
    '''Currently only works with PostgreSQL.'''
178
    if table == None: return table
179
    
180
    assert table.count('.') <= 1
181
    parts = tuple((v.replace('"', '') for v in table.split('"."', 2)))
182
    if len(parts) == 1: parts, = parts
183
    return parts
184

    
185
def col2sql_gen(col, default_table=None, table_is_esc=False):
186
    '''Converts old-style (tuple-based) columns to sql_gen-compatible columns.
187
    @param table_is_esc If False, assumes any table name is not escaped or that
188
        re-escaping it will produce the same value.
189
    '''
190
    if isinstance(col, Col): return col # already in sql_gen form
191
    
192
    table = default_table
193
    if isinstance(col, tuple): table, col = col
194
    if table_is_esc: table = unescape_table(table)
195
    return Col(col, as_Table(table))
196

    
197
def value2sql_gen(value, default_table=None, table_is_esc=False,
198
    assume_col=False):
199
    '''Converts old-style (tuple-based) values to sql_gen-compatible values.
200
    @param table_is_esc If False, assumes any table name is not escaped or that
201
        re-escaping it will produce the same value.
202
    '''
203
    if isinstance(value, Code): return value # already in sql_gen form
204
    
205
    is_tuple = isinstance(value, tuple)
206
    if is_tuple and len(value) == 1: return Literal(value[0])
207
    if is_tuple or (assume_col and util.is_str(value)):
208
        return col2sql_gen(value, default_table, table_is_esc)
209
    else: return Literal(value)
210

    
211
def cond2sql_gen(value, default_table=None, table_is_esc=False,
212
    assume_col=False):
213
    '''Converts old-style (tuple-based) conditions to sql_gen-compatible values.
214
    @param table_is_esc If False, assumes any table name is not escaped or that
215
        re-escaping it will produce the same value.
216
    '''
217
    if isinstance(value, ValueCond): return value # already in sql_gen form
218
    
219
    return as_ValueCond(value2sql_gen(value, default_table, table_is_esc,
220
        assume_col))
(23-23/34)