comparison src/pgsql_storage.py @ 375:9a787881b824

implemented Order-By ProtoXEP (MAM + PubSub)
author Goffi <goffi@goffi.org>
date Sun, 06 Jan 2019 17:29:50 +0100
parents 40e5edd7ea11
children 22832c1d2827
comparison
equal deleted inserted replaced
374:40e5edd7ea11 375:9a787881b824
831 class LeafNode(Node): 831 class LeafNode(Node):
832 832
833 implements(iidavoll.ILeafNode) 833 implements(iidavoll.ILeafNode)
834 834
835 nodeType = 'leaf' 835 nodeType = 'leaf'
836
837 def getOrderBy(self, ext_data, direction='DESC'):
838 """Return ORDER BY clause corresponding to Order By key in ext_data
839
840 @param ext_data (dict): extra data as used in getItems
841 @param direction (unicode): ORDER BY direction (ASC or DESC)
842 @return (unicode): ORDER BY clause to use
843 """
844 keys = ext_data.get('order_by')
845 if not keys:
846 return u'ORDER BY items.updated ' + direction
847 cols_statmnt = []
848 for key in keys:
849 if key == 'creation':
850 column = 'items.item_id' # could work with items.created too
851 elif key == 'modification':
852 column = 'items.updated'
853 else:
854 log.msg(u"WARNING: Unknown order by key: {key}".format(key=key))
855 column = 'items.updated'
856 cols_statmnt.append(column + u' ' + direction)
857
858 return u"ORDER BY " + u",".join([col for col in cols_statmnt])
836 859
837 def storeItems(self, item_data, publisher): 860 def storeItems(self, item_data, publisher):
838 return self.dbpool.runInteraction(self._storeItems, item_data, publisher) 861 return self.dbpool.runInteraction(self._storeItems, item_data, publisher)
839 862
840 def _storeItems(self, cursor, items_data, publisher): 863 def _storeItems(self, cursor, items_data, publisher):
1029 else: 1052 else:
1030 log.msg("WARNING: unknown filter: {}".format(filter_.encode('utf-8'))) 1053 log.msg("WARNING: unknown filter: {}".format(filter_.encode('utf-8')))
1031 1054
1032 query.extend(query_filters) 1055 query.extend(query_filters)
1033 1056
1034 return "ORDER BY items.updated DESC" 1057 return self.getOrderBy(ext_data)
1035 1058
1036 def _getItems(self, cursor, authorized_groups, unrestricted, maxItems, ext_data, ids_only): 1059 def _getItems(self, cursor, authorized_groups, unrestricted, maxItems, ext_data, ids_only):
1037 self._checkNodeExists(cursor) 1060 self._checkNodeExists(cursor)
1038 1061
1039 if maxItems == 0: 1062 if maxItems == 0:
1073 args.append(rsm.before) 1096 args.append(rsm.before)
1074 if maxItems is not None: 1097 if maxItems is not None:
1075 # if we have maxItems (i.e. a limit), we need to reverse order 1098 # if we have maxItems (i.e. a limit), we need to reverse order
1076 # in a first query to get the right items 1099 # in a first query to get the right items
1077 query.insert(0,"SELECT * from (") 1100 query.insert(0,"SELECT * from (")
1078 query.append("ORDER BY updated ASC LIMIT %s) as x") 1101 query.append(self.getOrderBy(ext_data, direction='ASC'))
1102 query.append("LIMIT %s) as x")
1079 args.append(maxItems) 1103 args.append(maxItems)
1080 elif rsm.after: 1104 elif rsm.after:
1081 query.append("AND item_id<(SELECT item_id FROM items WHERE item=%s LIMIT 1)") 1105 query.append("AND item_id<(SELECT item_id FROM items WHERE item=%s LIMIT 1)")
1082 args.append(rsm.after) 1106 args.append(rsm.after)
1083 1107