Revision 2538
Added by Aaron Marcuse-Kubitza over 12 years ago
lib/sql.py | ||
---|---|---|
793 | 793 |
|
794 | 794 |
row_num_col = '_row_num' |
795 | 795 |
|
796 |
def add_index(db, col): |
|
797 |
'''Adds an index on a column if it doesn't already exist.''' |
|
796 |
def add_index(db, expr): |
|
797 |
'''Adds an index on a column or expression if it doesn't already exist. |
|
798 |
Currently, only function calls are supported as expressions. |
|
799 |
''' |
|
800 |
expr = copy.copy(expr) # don't modify input! |
|
801 |
|
|
802 |
# Extract col |
|
803 |
if isinstance(expr, sql_gen.FunctionCall): col = expr.args[0] |
|
804 |
else: col = expr |
|
798 | 805 |
assert sql_gen.is_table_col(col) |
799 | 806 |
|
807 |
index = sql_gen.as_Table(str(expr)) |
|
800 | 808 |
table = col.table |
801 |
index = sql_gen.as_Table(str(col)) |
|
802 |
col = sql_gen.to_name_only_col(col) |
|
809 |
col.table = None |
|
803 | 810 |
try: run_query(db, 'CREATE INDEX '+index.to_str(db)+' ON '+table.to_str(db) |
804 |
+' ('+col.to_str(db)+')', recover=True, cacheable=True, log_level=3)
|
|
811 |
+' ('+expr.to_str(db)+')', recover=True, cacheable=True, log_level=3)
|
|
805 | 812 |
except DuplicateTableException: pass # index already existed |
806 | 813 |
|
807 | 814 |
def index_pkey(db, table, recover=None): |
Also available in: Unified diff
sql.py: add_index(): Support simple expressions as well as columns