Project

General

Profile

« Previous | Next » 

Revision 2997

sql.py: Added add_not_null(), add_index_col(), ensure_not_null()

View differences:

lib/sql.py
998 998
        +(', '.join(col_strs))+')', recover=True, cacheable=True, log_level=3,
999 999
        log_ignore_excs=(DuplicateException,))
1000 1000

  
1001
def add_not_null(db, col):
1002
    table = col.table
1003
    col = sql_gen.to_name_only_col(col)
1004
    run_query(db, 'ALTER TABLE '+table.to_str(db)+' ALTER COLUMN '
1005
        +col.to_str(db)+' SET NOT NULL', cacheable=True)
1006

  
1007
def add_index_col(db, col, suffix, expr, nullable=True):
1008
    if col.index_col != None: return # already has index col
1009
    
1010
    new_col = sql_gen.suffixed_col(col, suffix)
1011
    new_typed_col = sql_gen.TypedCol(new_col.name, db.col_info(col).type)
1012
    
1013
    add_col(db, col.table, new_typed_col)
1014
    update(db, col.table, [(new_col, expr)])
1015
    if not nullable: add_not_null(db, new_col)
1016
    add_index(db, new_col)
1017
    
1018
    col.index_col = new_col
1019

  
1020
def ensure_not_null(db, col):
1021
    '''For params, see sql_gen.ensure_not_null()'''
1022
    expr = sql_gen.ensure_not_null(db, col)
1023
    
1024
    # If nullable column in a temp table, add separate column instead
1025
    if sql_gen.is_temp_col(col) and isinstance(expr, sql_gen.EnsureNotNull):
1026
        add_index_col(db, col, '::NOT NULL', expr, nullable=False)
1027
        expr = col.index_col
1028
    
1029
    return expr
1030

  
1001 1031
already_indexed = object() # tells add_indexes() the pkey has already been added
1002 1032

  
1003 1033
def add_indexes(db, table, has_pkey=True):

Also available in: Unified diff