summaryrefslogtreecommitdiff
path: root/lib/sqlalchemy/dialects/postgresql/pgjson.py
diff options
context:
space:
mode:
authornathan <nathan.alexander.rice@gmail.com>2013-12-11 10:21:08 -0500
committernathan <nathan.alexander.rice@gmail.com>2013-12-11 10:21:08 -0500
commit015c73c83a1bd915cd34cc61370a6d89228c40d6 (patch)
treed5cee53717c9ca895eebc4353b14efe217eca805 /lib/sqlalchemy/dialects/postgresql/pgjson.py
parent059039e0f7eaeeaf7ab49181a99a5edeb67d9f28 (diff)
downloadsqlalchemy-015c73c83a1bd915cd34cc61370a6d89228c40d6.tar.gz
sqlalchemy/dialects/postgresql/pgjson:
- Added support for additional operators - Made return as json default (rather than text)
Diffstat (limited to 'lib/sqlalchemy/dialects/postgresql/pgjson.py')
-rw-r--r--lib/sqlalchemy/dialects/postgresql/pgjson.py16
1 files changed, 14 insertions, 2 deletions
diff --git a/lib/sqlalchemy/dialects/postgresql/pgjson.py b/lib/sqlalchemy/dialects/postgresql/pgjson.py
index 161fe83fa..b41446dae 100644
--- a/lib/sqlalchemy/dialects/postgresql/pgjson.py
+++ b/lib/sqlalchemy/dialects/postgresql/pgjson.py
@@ -39,7 +39,7 @@ class JSON(sqltypes.TypeEngine):
* Path Index operations::
- data_table.c.data.get_path('{key_1, key_2, ..., key_n}']
+ data_table.c.data.get_path("'{key_1, key_2, ..., key_n}'"]
Please be aware that when used with the SQLAlchemy ORM, you will need to
replace the JSON object present on an attribute with a new object in order
@@ -71,11 +71,23 @@ class JSON(sqltypes.TypeEngine):
# The only downside to this is that you cannot dereference more
# than one level deep in json structures, though comparator
# support for multi-level dereference is lacking anyhow.
+ return self.expr.op('->', precedence=5)(other)
+
+ def get_item_as_text(self, other):
+ """Text expression. Get the value at the given key as text. Use
+ this when you need to cast the type of the returned value."""
return self.expr.op('->>', precedence=5)(other)
def get_path(self, other):
- """Text expression. Get the value at a given path. Paths are of
+ """Text expression. Get the value at a given path. Paths are of
the form {key_1, key_2, ..., key_n}."""
+ return self.expr.op('#>', precedence=5)(other)
+
+ def get_path_as_text(self, other):
+ """Text expression. Get the value at a given path, as text.
+ Paths are of the form '{key_1, key_2, ..., key_n}' (quotes are
+ required). Use this when you need to cast the type of the
+ returned value."""
return self.expr.op('#>>', precedence=5)(other)
def _adapt_expression(self, op, other_comparator):