Ticket #366: axiom-unique-attribute.patch

File axiom-unique-attribute.patch, 3.0 kB (added by ironfroggy, 3 years ago)

Patch to implement this request for a unique constraint. Compares values of non-reference attributes and compares identity of reference attributes.

  • axiom/store.py

    old new  
    872872            sqlarg.append("\n%s %s" % 
    873873                          (atr.columnName, atr.sqltype)) 
    874874            if atr.indexed: 
    875                 indexes.append(nam
     875                indexes.append((nam, atr)
    876876        if len(sqlarg) == 0: 
    877877            # XXX should be raised way earlier, in the class definition or something 
    878878            raise NoEmptyItems("%r did not define any attributes" % (tableClass,)) 
     
    884884            self.connection.rollback() 
    885885 
    886886        self.createSQL(''.join(sqlstr)) 
    887         for index in indexes: 
    888             self.createSQL('CREATE INDEX axiomidx_%s_%s ON %s([%s])' 
    889                            % (tableName, index, 
     887        for index, index_atr in indexes: 
     888            if index_atr.unique: 
     889                unique = "UNIQUE" 
     890            else: 
     891                unique = "" 
     892            self.createSQL('CREATE %s INDEX axiomidx_%s_%s ON %s([%s])' 
     893                           % (unique, 
     894                              tableName, index, 
    890895                              tableName, index)) 
    891896 
    892897        if not self.autocommit: 
  • axiom/errors.py

    old new  
    3636    Found 2 or more of an item which is supposed to be unique. 
    3737    """ 
    3838 
     39class DuplicateUniqueAttribute(KeyError): 
     40    """ 
     41    Found 2 or more of an attribute which is supposed to be unique. 
     42    """ 
     43 
    3944class ItemNotFound(KeyError): 
    4045    """ 
    4146    Did not find even 1 of an item which was supposed to exist. 
  • axiom/attributes.py

    old new  
    1414 
    1515from axiom.slotmachine import Attribute as inmemory 
    1616 
    17 from axiom.errors import NoCrossStoreReferences 
     17from axiom.errors import NoCrossStoreReferences, DuplicateUniqueAttribute 
    1818 
    1919from axiom.iaxiom import IComparison, IOrdering 
    2020 
     
    282282    """ 
    283283    sqltype = None 
    284284 
    285     def __init__(self, doc='', indexed=False, default=None, allowNone=True): 
     285    def __init__(self, doc='', indexed=False, default=None, allowNone=True, unique=False): 
    286286        inmemory.__init__(self, doc) 
     287        if unique: 
     288            indexed = True 
    287289        self.indexed = indexed 
     290        self.unique = unique 
    288291        self.default = default 
    289292        self.allowNone = allowNone 
    290293 
     
    692695        return Time.fromPOSIXTimestamp(dbval / MICRO) 
    693696 
    694697class reference(integer): 
    695     def __init__(self, doc='', indexed=True, allowNone=True, reftype=None): 
    696         integer.__init__(self, doc, indexed, None, allowNone
     698    def __init__(self, doc='', indexed=True, allowNone=True, reftype=None, unique=False): 
     699        integer.__init__(self, doc, indexed, None, allowNone, unique=unique
    697700        self.reftype = reftype 
    698701 
    699702 
jethro@divmod.org