dbservice.make_record: convert list to pgarray
authorMarko Kreen <markokr@gmail.com>
Thu, 22 Dec 2011 11:00:14 +0000 (13:00 +0200)
committerMarko Kreen <markokr@gmail.com>
Thu, 22 Dec 2011 11:00:14 +0000 (13:00 +0200)
This makes the behaviour compatible with 8.3 plpython,
with did not know arrays and returned text strings.

More general solution would be to use urlenc arrays (same key repeats),
except that not general enough - there is no transparent way
to encode lists with 0 and 1 elements.  So caller must always
be aware whether a key is list or not.  Thus no advantages
over this pgarray solution and disadvantaga is incompatibility.

python/skytools/dbservice.py

index 931d514e4b86165c02dbde2632cb5a69ed534ea6..ee06a8725cc1bf438cdfe62e4f7d706f82563d4e 100755 (executable)
@@ -103,17 +103,28 @@ def get_record_lists(tbl, field):
         dict.setdefault( id, [] ).append(rec)
     return dict
 
+def _make_record_convert(row):
+    """Converts complex values."""
+    d = row.copy()
+    for k, v in d.items():
+        if isinstance(v, list):
+            d[k] = skytools.make_pgarray(v)
+    return skytools.db_urlencode(d)
+
 def make_record(row):
     """ Takes record as dict and returns it as urlencoded string.
         Used to send data out of db service layer.or to fake incoming calls
     """
+    for v in row.values():
+        if isinstance(v, list):
+            return _make_record_convert(row)
     return skytools.db_urlencode(row)
 
 def make_record_array(rowlist):
     """ Takes list of records got from plpy execute and turns it into postgers aray string.
         Used to send data out of db service layer.
     """
-    return '{' + ','.join( map(skytools.db_urlencode, rowlist) ) +  '}'
+    return '{' + ','.join( map(make_record, rowlist) ) +  '}'
 
 def get_result_items(list, name):
     """ Get return values from result