Hi!
I have a table with a json field and I woul like to perform something like:

db(db.mytable.id>0).select(db.mytable.myjsonfield.getitem('myfield').with_alias("myfield"))

the only way I thought is the patch in attachment that works just fine
but I don't like to use a patched version of the framework.
Is these a way to monkeypath the two classes PostgreSQLAdapter and
Expression (I guess before the db definition) in my application?

On the other hand... do you think it could be a usefull implementation
for web2py itself?

Thanks a lot

    Manuele

-- 
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
--- 
You received this message because you are subscribed to the Google Groups 
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.
>From 5ad13bcf556cfacd9df59f60bdb8e8b16b95894c Mon Sep 17 00:00:00 2001
From: manuele pesenti <[email protected]>
Date: Thu, 6 Aug 2015 00:00:52 +0200
Subject: [PATCH 1/1] json getitem solution

---
 gluon/dal/adapters/postgres.py | 4 ++++
 gluon/dal/objects.py           | 4 ++++
 2 files changed, 8 insertions(+)

diff --git a/gluon/dal/adapters/postgres.py b/gluon/dal/adapters/postgres.py
index 7e8c32f..0ded855 100644
--- a/gluon/dal/adapters/postgres.py
+++ b/gluon/dal/adapters/postgres.py
@@ -215,6 +215,10 @@ class PostgreSQLAdapter(BaseAdapter):
         return '(%s ~ %s)' % (self.expand(first),
                               self.expand(second,'string'))
 
+    def GETITEM(self, first, second):
+        args = (self.expand(first), self.expand(second,'string'))
+        return "%s->>%s" % args
+
     # GIS functions
 
     def ST_ASGEOJSON(self, first, second):
diff --git a/gluon/dal/objects.py b/gluon/dal/objects.py
index 9c22449..61394eb 100644
--- a/gluon/dal/objects.py
+++ b/gluon/dal/objects.py
@@ -1120,6 +1120,10 @@ class Expression(object):
         db = self.db
         return Expression(db, db._adapter.EPOCH, self, None, 'integer')
 
+    def getitem(self, what):
+        db = self.db
+        return Expression(db, db._adapter.GETITEM, self, what, 'string')
+
     def __getslice__(self, start, stop):
         db = self.db
         if start < 0:
-- 
1.8.5.1

Reply via email to