diff options
author | Chris Withers <chris@simplistix.co.uk> | 2013-05-21 21:11:35 +0100 |
---|---|---|
committer | Chris Withers <chris@simplistix.co.uk> | 2013-06-10 12:09:56 +0100 |
commit | b2ea2eef5db160183cd4f812b0ce1636d8799b91 (patch) | |
tree | 88b4b352a463c278e587fa5c13749685639b18b0 /lib/sqlalchemy/dialects/postgresql/constraints.py | |
parent | f4020282b798ea510e6aafda779ab33c692c0120 (diff) | |
download | sqlalchemy-b2ea2eef5db160183cd4f812b0ce1636d8799b91.tar.gz |
Implement EXCLUDE constraints for postgres.
Diffstat (limited to 'lib/sqlalchemy/dialects/postgresql/constraints.py')
-rw-r--r-- | lib/sqlalchemy/dialects/postgresql/constraints.py | 73 |
1 files changed, 73 insertions, 0 deletions
diff --git a/lib/sqlalchemy/dialects/postgresql/constraints.py b/lib/sqlalchemy/dialects/postgresql/constraints.py new file mode 100644 index 000000000..88d688a05 --- /dev/null +++ b/lib/sqlalchemy/dialects/postgresql/constraints.py @@ -0,0 +1,73 @@ +# Copyright (C) 2013 the SQLAlchemy authors and contributors <see AUTHORS file> +# +# This module is part of SQLAlchemy and is released under +# the MIT License: http://www.opensource.org/licenses/mit-license.php +from sqlalchemy.schema import ColumnCollectionConstraint +from sqlalchemy.sql import expression + +class ExcludeConstraint(ColumnCollectionConstraint): + """A table-level UNIQUE constraint. + + Defines a single column or composite UNIQUE constraint. For a no-frills, + single column constraint, adding ``unique=True`` to the ``Column`` + definition is a shorthand equivalent for an unnamed, single column + UniqueConstraint. + """ + + __visit_name__ = 'exclude_constraint' + + where = None + + def __init__(self, *elements, **kw): + """ + :param \*elements: + A sequence of two tuples of the form ``(column, operator)`` where + column must be a column name or Column object and operator must + be a string containing the operator to use. + + :param name: + Optional, the in-database name of this constraint. + + :param deferrable: + Optional bool. If set, emit DEFERRABLE or NOT DEFERRABLE when + issuing DDL for this constraint. + + :param initially: + Optional string. If set, emit INITIALLY <value> when issuing DDL + for this constraint. + + :param using: + Optional string. If set, emit USING <index_method> when issuing DDL + for this constraint. Defaults to 'gist'. + + :param where: + Optional string. If set, emit WHERE <predicate> when issuing DDL + for this constraint. + + """ + ColumnCollectionConstraint.__init__( + self, + *[col for col, op in elements], + name=kw.get('name'), + deferrable=kw.get('deferrable'), + initially=kw.get('initially') + ) + self.operators = {} + for col_or_string, op in elements: + name = getattr(col_or_string, 'name', col_or_string) + self.operators[name] = op + self.using = kw.get('using', 'gist') + where = kw.get('where') + if where: + self.where = expression._literal_as_text(where) + + def copy(self, **kw): + elements = [(col, self.operators[col]) + for col in self.columns.keys()] + c = self.__class__(*elements, + name=self.name, + deferrable=self.deferrable, + initially=self.initially) + c.dispatch._update(self.dispatch) + return c + |