Project

General

Profile

« Previous | Next » 

Revision 2760

sql.py: create_table(): Add indexes on all non-pkey columns, unless turned off or deferred using new param col_indexes

View differences:

lib/sql.py
982 982
    run_query(db, 'ALTER TABLE '+table+' ADD COLUMN '+row_num_col
983 983
        +' serial NOT NULL PRIMARY KEY', log_level=3)
984 984

  
985
def create_table(db, table, cols, has_pkey=True):
985
def create_table(db, table, cols, has_pkey=True, col_indexes=True):
986 986
    '''Creates a table.
987 987
    @param cols [sql_gen.TypedCol,...] The column names and types
988 988
    @param has_pkey If set, the first column becomes the primary key.
989
    @param col_indexes bool|[ref]
990
        * If True, indexes will be added on all non-pkey columns.
991
        * If a list reference, [0] will be set to a function to do this.
992
          This can be used to delay index creation until the table is populated.
989 993
    '''
990 994
    table = sql_gen.as_Table(table)
991 995
    
......
997 1001
    str_ += '\n, '.join(v.to_str(db) for v in cols)
998 1002
    str_ += '\n);\n'
999 1003
    run_query(db, str_, cacheable=True, log_level=2)
1004
    
1005
    # Add indexes
1006
    if has_pkey: index_cols = cols[1:]
1007
    else: index_cols = cols
1008
    def add_indexes():
1009
        for col in index_cols: add_index(db, col.to_Col(), table)
1010
    if isinstance(col_indexes, list): col_indexes[0] = add_indexes # defer
1011
    elif col_indexes: add_indexes() # add now
1000 1012

  
1001 1013
def vacuum(db, table):
1002 1014
    table = sql_gen.as_Table(table)
bin/csv2db
68 68
        
69 69
        def load_():
70 70
            log('Creating table')
71
            sql.create_table(db, table, typed_cols)
71
            sql.create_table(db, table, typed_cols, col_indexes=False)
72 72
            
73 73
            # Create COPY FROM statement
74 74
            if use_copy_from[0]:
......
130 130
            sql_gen.TypedCol('error', 'text NOT NULL'),
131 131
            ]
132 132
        sql.create_table(db, errors_table, typed_cols, has_pkey=False)
133
        for col in typed_cols: sql.add_index(db, col.to_Col(), errors_table)
134 133
        index_cols = ['column', sql_gen.EnsureNotNull('value'), 'error_code',
135 134
            'error']
136 135
        sql.add_index(db, index_cols, errors_table, unique=True)

Also available in: Unified diff