Project

General

Profile

« Previous | Next » 

Revision 3356

sql.py: add_index(): Support literal values as expressions

View differences:

lib/sql.py
1191 1191

  
1192 1192
def add_index(db, exprs, table=None, unique=False, ensure_not_null_=True):
1193 1193
    '''Adds an index on column(s) or expression(s) if it doesn't already exist.
1194
    Currently, only function calls are supported as expressions.
1194
    Currently, only function calls and literal values are supported expressions.
1195 1195
    @param ensure_not_null_ If set, translates NULL values to sentinel values.
1196 1196
        This allows indexes to be used for comparisons where NULLs are equal.
1197 1197
    '''
......
1211 1211
        
1212 1212
        # Extract col
1213 1213
        expr = copy.deepcopy(expr) # don't modify input!
1214
        if isinstance(expr, sql_gen.FunctionCall):
1215
            col = expr.args[0]
1214
        col = expr
1215
        if isinstance(expr, sql_gen.FunctionCall): col = expr.args[0]
1216
        expr = sql_gen.cast_literal(expr)
1217
        if not isinstance(expr, (sql_gen.Expr, sql_gen.Col)):
1216 1218
            expr = sql_gen.Expr(expr)
1217
        else: col = expr
1218
        assert isinstance(col, sql_gen.Col)
1219
            
1219 1220
        
1220 1221
        # Extract table
1221 1222
        if table == None:
1222 1223
            assert sql_gen.is_table_col(col)
1223 1224
            table = col.table
1224 1225
        
1225
        col.table = None
1226
        if isinstance(col, sql_gen.Col): col.table = None
1226 1227
        
1227 1228
        exprs.append(expr)
1228 1229
        cols.append(col)

Also available in: Unified diff