diff options
| author | Mike Bayer <mike_mp@zzzcomputing.com> | 2012-11-24 10:49:14 -0500 |
|---|---|---|
| committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2012-11-24 10:49:14 -0500 |
| commit | 252bce8b02b1587a26b899c644ba28e78f5db4bc (patch) | |
| tree | dd4c3702efb55cca296bad87fcc8bcff48fa7b59 /lib/sqlalchemy | |
| parent | d1bf4d2b863ef71cc936977a9fee1a5b056aff67 (diff) | |
| parent | 64505dcd2871bfe67f682d67df937b713c4213a6 (diff) | |
| download | sqlalchemy-252bce8b02b1587a26b899c644ba28e78f5db4bc.tar.gz | |
merge Audrius HSTORE commits from bitbucket
Diffstat (limited to 'lib/sqlalchemy')
| -rw-r--r-- | lib/sqlalchemy/dialects/postgresql/base.py | 42 | ||||
| -rw-r--r-- | lib/sqlalchemy/dialects/postgresql/hstore.py | 13 |
2 files changed, 43 insertions, 12 deletions
diff --git a/lib/sqlalchemy/dialects/postgresql/base.py b/lib/sqlalchemy/dialects/postgresql/base.py index c7e84751d..ed24bc1fe 100644 --- a/lib/sqlalchemy/dialects/postgresql/base.py +++ b/lib/sqlalchemy/dialects/postgresql/base.py @@ -464,12 +464,21 @@ class ARRAY(sqltypes.Concatenable, sqltypes.TypeEngine): as well as UPDATE statements when the :meth:`.Update.values` method is used:: - mytable.update().values({mytable.c.data[5]:7, - mytable.c.data[2:7]:[1,2,3]}) + mytable.update().values({ + mytable.c.data[5]: 7, + mytable.c.data[2:7]: [1, 2, 3] + }) + + :class:`.ARRAY` provides special methods for containment operations, + e.g.:: + + mytable.c.data.contains([1, 2]) + + For a full list of special methods see :class:`.ARRAY.Comparator`. .. versionadded:: 0.8 Added support for index and slice operations to the :class:`.ARRAY` type, including support for UPDATE - statements. + statements, and special array containment operations. The :class:`.ARRAY` type may not be supported on all DBAPIs. It is known to work on psycopg2 and not pg8000. @@ -482,6 +491,8 @@ class ARRAY(sqltypes.Concatenable, sqltypes.TypeEngine): __visit_name__ = 'ARRAY' class Comparator(sqltypes.Concatenable.Comparator): + """Define comparison operations for :class:`.ARRAY`.""" + def __getitem__(self, index): if isinstance(index, slice): index = _Slice(index, self) @@ -491,6 +502,31 @@ class ARRAY(sqltypes.Concatenable, sqltypes.TypeEngine): return self._binary_operate(self.expr, operators.getitem, index, result_type=return_type) + def contains(self, other, **kwargs): + """Boolean expression. Test if elements are a superset of the + elements of the argument array expression. + """ + return self.expr.op('@>')(other) + + def contained_by(self, other): + """Boolean expression. Test if elements are a proper subset of the + elements of the argument array expression. + """ + return self.expr.op('<@')(other) + + def overlap(self, other): + """Boolean expression. Test if array has elements in common with + an argument array expression. + """ + return self.expr.op('&&')(other) + + def _adapt_expression(self, op, other_comparator): + if isinstance(op, operators.custom_op): + if op.opstring in ['@>', '<@', '&&']: + return op, sqltypes.Boolean + return sqltypes.Concatenable.Comparator.\ + _adapt_expression(self, op, other_comparator) + comparator_factory = Comparator def __init__(self, item_type, as_tuple=False, dimensions=None): diff --git a/lib/sqlalchemy/dialects/postgresql/hstore.py b/lib/sqlalchemy/dialects/postgresql/hstore.py index 8ac65b912..a6093d9f7 100644 --- a/lib/sqlalchemy/dialects/postgresql/hstore.py +++ b/lib/sqlalchemy/dialects/postgresql/hstore.py @@ -47,7 +47,7 @@ def _parse_error(hstore_str, pos): residual = residual[:-1] + '[...]' return "After %r, could not parse residual at position %d: %r" % ( - parsed_tail, pos, residual) + parsed_tail, pos, residual) def _parse_hstore(hstore_str): @@ -173,7 +173,7 @@ class HSTORE(sqltypes.Concatenable, sqltypes.TypeEngine): __visit_name__ = 'HSTORE' - class comparator_factory(sqltypes.TypeEngine.Comparator): + class comparator_factory(sqltypes.Concatenable.Comparator): """Define comparison operations for :class:`.HSTORE`.""" def has_key(self, other): @@ -218,12 +218,6 @@ class HSTORE(sqltypes.Concatenable, sqltypes.TypeEngine): """ return self.expr.op('->', precedence=5)(other) - def __add__(self, other): - """HStore expression. Merge the left and right hstore expressions, - with duplicate keys taking the value from the right expression. - """ - return self.expr.concat(other) - def delete(self, key): """HStore expression. Returns the contents of this hstore with the given key deleted. Note that the key may be a SQLA expression. @@ -262,7 +256,8 @@ class HSTORE(sqltypes.Concatenable, sqltypes.TypeEngine): return op, sqltypes.Boolean elif op.opstring == '->': return op, sqltypes.Text - return op, other_comparator.type + return sqltypes.Concatenable.Comparator.\ + _adapt_expression(self, op, other_comparator) def bind_processor(self, dialect): def process(value): |
