Skip to content

Instantly share code, notes, and snippets.

@wilig
Created October 6, 2010 17:54
Show Gist options
  • Save wilig/613778 to your computer and use it in GitHub Desktop.
Save wilig/613778 to your computer and use it in GitHub Desktop.
create or replace function update_asset_json(text, text, integer) returns text as $$
import simplejson
from datetime import datetime
now = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
updated_data = simplejson.loads(args[1])
rs = plpy.execute("select * from assets where id in (%s)" % args[0])
resp = []
for r in rs:
data = simplejson.loads(r['data'])
data.update(updated_data) # apply the update(s)
id = r['id']
r.update(data)
r['updated_on'] = now # update the timestamp in the results being returned
r.pop('data') # Get rid of the data key as data is now in root hash
resp.append(r)
plpy.execute("update assets set updated_by=%d, updated_on='%s', data='%s' where id=%d" % (args[2], now, simplejson.dumps(data), id))
return simplejson.dumps(resp)
$$ language plpythonu;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment