diff --git a/sat/memory/sqlite.py b/sat/memory/sqlite.py index 94cb504f..90c1e9ce 100644 --- a/sat/memory/sqlite.py +++ b/sat/memory/sqlite.py @@ -592,9 +592,6 @@ class SqliteStorage(object): q.append(test_jid('dest', to_jid)) query_parts.append(u"AND " + u" AND ".join(q)) - # set to True if "ORDER BY" is already added - order = False - if filters: if u'timestamp_start' in filters: query_parts.append(u"AND timestamp>= ?") @@ -618,20 +615,17 @@ class SqliteStorage(object): # this request get the last message with a "stanza_id" that we # have in history. This is mainly used to retrieve messages sent # while we were offline, using MAM (XEP-0313). - # It must be set after all other filters, because it contains an ORDER BY if (filters[u'last_stanza_id'] is not True or limit != 1): raise ValueError(u"Unexpected values for last_stanza_id filter") - query_parts.append(u"AND stanza_id IS NOT NULL ORDER BY history.received_timestamp DESC") - order = True + query_parts.append(u"AND stanza_id IS NOT NULL") - if not order: - # timestamp may be identical for 2 close messages (specially when delay is - # used) that's why we order ties by received_timestamp - # We'll reverse the order in sqliteHistoryToList - query_parts.append(u"ORDER BY timestamp DESC, history.received_timestamp DESC") - # we use DESC here so LIMIT keep the last messages + # timestamp may be identical for 2 close messages (specially when delay is + # used) that's why we order ties by received_timestamp + # We'll reverse the order in sqliteHistoryToList + # we use DESC here so LIMIT keep the last messages + query_parts.append(u"ORDER BY timestamp DESC, history.received_timestamp DESC") if limit is not None: query_parts.append(u"LIMIT ?") values.append(limit)