Project

General

Profile

« Previous | Next » 

Revision 2353

sql_gen.py: Join: Added join_same_not_null. to_str(): Refactored to switch order of left and right tables and cols because left_table is on the right in the comparison, and using the sides of the comparison instead of the sides of the join makes the code clearer.

View differences:

lib/sql_gen.py
174 174

  
175 175
join_same = object() # tells Join the left and right columns have the same name
176 176

  
177
# Tells Join the left and right columns have the same name and are never NULL
178
join_same_not_null = object()
179

  
177 180
filter_out = object() # tells Join to filter out rows that match the join
178 181

  
179 182
class Join(Code):
......
181 184
        '''
182 185
        @param mapping dict(right_table_col=left_table_col, ...)
183 186
            * if left_table_col is join_same: left_table_col = right_table_col
187
              * Note that right_table_col must be a string
188
            * if left_table_col is join_same_not_null:
189
              left_table_col = right_table_col and both have NOT NULL constraint
190
              * Note that right_table_col must be a string
184 191
        @param type_ None (for plain join)|str (e.g. 'LEFT')|filter_out
185 192
            * filter_out: equivalent to 'LEFT' with the query filtered by
186 193
              `table_pkey IS NULL` (indicating no match)
......
193 200
        self.type_ = type_
194 201
    
195 202
    def to_str(self, db, left_table):
203
        # Switch order (left_table is on the right in the comparison)
204
        right_table = left_table
205
        left_table = self.table # note left_table is reassigned
206
        
196 207
        def join(entry):
197 208
            '''Parses non-USING joins'''
198 209
            right_table_col, left_table_col = entry
199
            # Note that right_table_col is on the left in the comparison
200 210
            
211
            # Switch order (right_table_col is on the left in the comparison)
212
            left = right_table_col
213
            right = left_table_col
214
            
201 215
            # Parse special values
202
            if left_table_col is join_same: left_table_col = right_table_col
216
            if right is join_same: right = left
217
            elif right is join_same_not_null:
218
                right = CompareCond(as_Col(left, right_table), '~=')
203 219
            
204
            cond = as_ValueCond(left_table_col, left_table)
205
            return cond.to_str(db, as_Col(right_table_col, self.table))
220
            right = as_ValueCond(right, right_table)
221
            return right.to_str(db, as_Col(left, left_table))
206 222
        
207 223
        # Create join condition
208 224
        type_ = self.type_
......
218 234
        if type_ is filter_out: type_ = 'LEFT'
219 235
        str_ = ''
220 236
        if type_ != None: str_ += type_+' '
221
        str_ += 'JOIN '+self.table.to_str(db)+' '+join_cond
237
        str_ += 'JOIN '+left_table.to_str(db)+' '+join_cond
222 238
        return str_
223 239
    
224 240
    def __str__(self): return self.to_str(mockDb, '')

Also available in: Unified diff