]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
add has_key & contains operators for jsonb (ported over from hstore)
authorDamian Dimmich <damian@tauri-tec.com>
Sat, 28 Jun 2014 18:47:20 +0000 (22:47 +0400)
committerDamian Dimmich <damian@tauri-tec.com>
Sat, 28 Jun 2014 18:47:20 +0000 (22:47 +0400)
lib/sqlalchemy/dialects/postgresql/json.py

index 37196dfb18c65f5d417415135f435da161cd7763..262ec20bd3104c1377b63d201767ff73f9b6175f 100644 (file)
@@ -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