Project

General

Profile

« Previous | Next » 

Revision 2945

sql.py: Merged DuplicateTableException and DuplicateFunctionException into one exception DuplicateException, with a type variable for the type of duplicate item. Added ExceptionWithNameType.

View differences:

lib/sql.py
46 46
        self.name = name
47 47
        self.value = value
48 48

  
49
class ExceptionWithNameType(DbException):
50
    def __init__(self, type_, name, cause=None):
51
        DbException.__init__(self, 'for type: '+strings.as_tt(str(type_))
52
            +'; name: '+strings.as_tt(name), cause)
53
        self.type = type_
54
        self.name = name
55

  
49 56
class ConstraintException(DbException):
50 57
    def __init__(self, name, cols, cause=None):
51 58
        DbException.__init__(self, 'Violated '+strings.as_tt(name)
......
68 75

  
69 76
class FunctionValueException(ExceptionWithNameValue): pass
70 77

  
71
class DuplicateTableException(ExceptionWithName): pass
78
class DuplicateException(ExceptionWithNameType): pass
72 79

  
73
class DuplicateFunctionException(ExceptionWithName): pass
74

  
75 80
class EmptyRowException(DbException): pass
76 81

  
77 82
##### Warnings
......
450 455
                col, type_ = match.groups()
451 456
                raise MissingCastException(type_, col, e)
452 457
            
453
            match = re.search(r'relation "(.+?)" already exists', msg)
454
            if match: raise DuplicateTableException(match.group(1), e)
458
            match = re.search(r'\b(\S+) "(.+?)" already exists', msg)
459
            if match:
460
                type_, name = match.groups()
461
                raise DuplicateException(type_, name, e)
455 462
            
456
            match = re.search(r'function "(.+?)" already exists', msg)
457
            if match: raise DuplicateFunctionException(match.group(1), e)
458
            
459 463
            raise # no specific exception raised
460 464
    except log_ignore_excs:
461 465
        log_level += 2
......
487 491
    assert isinstance(into, sql_gen.Table)
488 492
    
489 493
    kw_args['recover'] = True
490
    kw_args.setdefault('log_ignore_excs', (DuplicateTableException,))
494
    kw_args.setdefault('log_ignore_excs', (DuplicateException,))
491 495
    
492 496
    temp = not db.debug_temp # tables are permanent in debug_temp mode
493 497
    # "temporary tables cannot specify a schema name", so remove schema
......
503 507
            cur = run_query(db, create_query, **kw_args)
504 508
                # CREATE TABLE AS sets rowcount to # rows in query
505 509
            break
506
        except DuplicateTableException, e:
510
        except DuplicateException, e:
507 511
            into.name = next_version(into.name)
508 512
            # try again with next version of name
509 513
    
......
648 652
$$;
649 653
'''
650 654
                run_query(db, function_query, recover=True, cacheable=True,
651
                    log_ignore_excs=(DuplicateFunctionException,))
655
                    log_ignore_excs=(DuplicateException,))
652 656
                break # this version was successful
653
            except DuplicateFunctionException, e:
657
            except DuplicateException, e:
654 658
                function_name = next_version(function_name)
655 659
                # try again with next version of name
656 660
        
......
839 843
        # Create function
840 844
        try:
841 845
            run_query(db, query, recover=True, cacheable=True,
842
                log_ignore_excs=(DuplicateFunctionException,))
846
                log_ignore_excs=(DuplicateException,))
843 847
            break # successful
844
        except DuplicateFunctionException:
848
        except DuplicateException:
845 849
            function.name = next_version(function.name)
846 850
            # try again with next version of name
847 851
    
......
975 979
        ', '.join((v.to_str(db) for v in exprs)))+')'
976 980
    
977 981
    try: run_query(db, str_, recover=True, cacheable=True, log_level=3)
978
    except DuplicateTableException: pass # index already existed
982
    except DuplicateException: pass # index already existed
979 983

  
980 984
def add_pkey(db, table, cols=None, recover=None):
981 985
    '''Adds a primary key.
......
989 993
    
990 994
    run_query(db, 'ALTER TABLE '+table.to_str(db)+' ADD PRIMARY KEY ('
991 995
        +(', '.join(col_strs))+')', recover=True, cacheable=True, log_level=3,
992
        log_ignore_excs=(DuplicateTableException,))
996
        log_ignore_excs=(DuplicateException,))
993 997

  
994 998
already_indexed = object() # tells add_indexes() the pkey has already been added
995 999

  

Also available in: Unified diff