diff options
author | Damian Dimmich <damian@tauri-tec.com> | 2014-06-28 22:47:20 +0400 |
---|---|---|
committer | Damian Dimmich <damian@tauri-tec.com> | 2014-06-28 22:47:20 +0400 |
commit | 4e5fcee975040d6d82baceb8e0535a548411faa6 (patch) | |
tree | 2c539d853d802770641b20b4e78f8ef1a871d0b0 /lib/sqlalchemy/dialects/postgresql/json.py | |
parent | f5092602028386c567cba62bbc857d61eb88133e (diff) | |
download | sqlalchemy-4e5fcee975040d6d82baceb8e0535a548411faa6.tar.gz |
add has_key & contains operators for jsonb (ported over from hstore)
Diffstat (limited to 'lib/sqlalchemy/dialects/postgresql/json.py')
-rw-r--r-- | lib/sqlalchemy/dialects/postgresql/json.py | 38 |
1 files changed, 17 insertions, 21 deletions
diff --git a/lib/sqlalchemy/dialects/postgresql/json.py b/lib/sqlalchemy/dialects/postgresql/json.py index 37196dfb1..262ec20bd 100644 --- a/lib/sqlalchemy/dialects/postgresql/json.py +++ b/lib/sqlalchemy/dialects/postgresql/json.py @@ -200,7 +200,7 @@ ischema_names['json'] = JSON -class JSONB(sqltypes.TypeEngine): +class JSONB(JSON): """Represent the Postgresql JSONB type. The :class:`.JSONB` type stores arbitrary JSONB format data, e.g.:: @@ -268,6 +268,7 @@ class JSONB(sqltypes.TypeEngine): """ __visit_name__ = 'JSONB' + hashable = False class comparator_factory(sqltypes.Concatenable.Comparator): """Define comparison operations for :class:`.JSON`.""" @@ -279,32 +280,27 @@ class JSONB(sqltypes.TypeEngine): def _adapt_expression(self, op, other_comparator): if isinstance(op, custom_op): + if op.opstring in ['?', '?&', '?|', '@>', '<@']: + return op, sqltypes.Boolean if op.opstring == '->': return op, sqltypes.Text return sqltypes.Concatenable.Comparator.\ _adapt_expression(self, op, other_comparator) - def bind_processor(self, dialect): - json_serializer = dialect._json_serializer or json.dumps - if util.py2k: - encoding = dialect.encoding - def process(value): - return json_serializer(value).encode(encoding) - else: - def process(value): - return json_serializer(value) - return process + def has_key(self, other): + """Boolean expression. Test for presence of a key. Note that the + key may be a SQLA expression. + """ + return self.expr.op('?')(other) + + def contains(self, other, **kwargs): + """Boolean expression. Test if keys (or array) are a superset of/contained + the keys of the argument jsonb expression. + """ + return self.expr.op('@>')(other) + + - def result_processor(self, dialect, coltype): - json_deserializer = dialect._json_deserializer or json.loads - if util.py2k: - encoding = dialect.encoding - def process(value): - return json_deserializer(value.decode(encoding)) - else: - def process(value): - return json_deserializer(value) - return process ischema_names['jsonb'] = JSONB
\ No newline at end of file |