Ticket #790: discussion-plugin-r1491-sqlite-2.8.16.patch

File discussion-plugin-r1491-sqlite-2.8.16.patch, 8.6 kB (added by sto, 2 years ago)

New patch, removes nested queries

  • 0.10/tracdiscussion/api.py

    old new  
    777777        if order_by != 'forum': 
    778778            order_by = 'g.' + order_by 
    779779        columns = ('id', 'name', 'description', 'forums') 
    780         sql = "SELECT g.id, g.name, g.description, (SELECT COUNT(f.id)" \ 
    781           " FROM forum f, forum_group g WHERE f.forum_group = g.id) AS" \ 
    782           " forums FROM forum_group g ORDER BY " + order_by + (" ASC", 
     780        sql = "SELECT g.id, g.name, g.description, f.forums FROM " \ 
     781          " forum_group g LEFT JOIN (SELECT COUNT(id) AS forums, " \ 
     782          " forum_group FROM forum GROUP BY forum_group) f ON g.id = " \ 
     783          " f.forum_group ORDER BY " + order_by + (" ASC", 
    783784          " DESC")[bool(desc)] 
    784785        self.log.debug(sql) 
    785786        cursor.execute(sql) 
     
    797798          'subject', 'description', 'topics', 'replies', 'lastreply', 
    798799          'lasttopic') 
    799800        sql = "SELECT f.id, f.name, f.author, f.time, f.moderators," \ 
    800           " f.forum_group, f.subject, f.description, (SELECT COUNT(t.id)" \ 
    801           " FROM topic t, forum f WHERE t.forum = f.id) AS topics, (SELECT" \ 
    802           " COUNT(m.id) FROM message m, forum f WHERE m.forum = f.id) AS" \ 
    803           " replies, (SELECT MAX(m.time) FROM message m, forum f WHERE" \ 
    804           " m.forum = f.id) AS lasttopic, (SELECT MAX(t.time) FROM topic t," \ 
    805           " forum f WHERE t.forum = f.id) AS lastreply FROM forum f ORDER BY " \ 
     801          " f.forum_group, f.subject, f.description, tm.topics, tm.replies," \ 
     802          " tm.lastreply, tm.lasttopic FROM forum f LEFT JOIN ( SELECT" \ 
     803          " tf.forum AS forum, topics, lasttopic, replies, lastreply FROM" \ 
     804          " (SELECT COUNT(id) AS topics, MAX(time) AS lasttopic, forum" \ 
     805          " FROM topic GROUP BY forum) tf, (SELECT COUNT(id) AS replies," \ 
     806          " MAX(time) AS lastreply, forum FROM message GROUP BY forum) mf" \ 
     807          " WHERE tf.forum = mf.forum) tm ON f.id = tm.forum ORDER BY " \ 
    806808          + order_by + (" ASC", " DESC")[bool(desc)] 
    807809        self.log.debug(sql) 
    808810        cursor.execute(sql) 
     
    812814            row['moderators'] = wiki_to_oneliner(row['moderators'], self.env) 
    813815            row['description'] = wiki_to_oneliner(row['description'], self.env) 
    814816            if row['lastreply']: 
    815                 row['lastreply'] = pretty_timedelta(row['lastreply']
     817                row['lastreply'] = pretty_timedelta(float(row['lastreply'])
    816818            else: 
    817819                row['lastreply'] = 'No replies' 
    818820            if row['lasttopic']: 
    819                 row['lasttopic'] = pretty_timedelta(row['lasttopic']) 
     821                self.log.debug('lasttopic: %s' % row['lasttopic']) 
     822                row['lasttopic'] = pretty_timedelta(float(row['lasttopic'])) 
    820823            else: 
    821824                row['lasttopic'] = 'No topics' 
    822825            row['time'] = format_datetime(row['time']) 
     
    829832        columns = ('id', 'forum', 'time', 'subject', 'body', 'author', 
    830833          'replies', 'lastreply') 
    831834        sql = "SELECT t.id, t.forum, t.time, t.subject, t.body, t.author," \ 
    832           " (SELECT COUNT(m.id) FROM message m, topic t WHERE m.topic = t.id)" \ 
    833           " AS replies, (SELECT MAX(m.time) FROM message m, topic t WHERE" \ 
    834           " m.topic = t.id) AS lastreply FROM topic t WHERE t.forum = %s" \ 
    835           " ORDER BY " + order_by + (" ASC", " DESC")[bool(desc)] 
     835          " m.replies, m.lastreply FROM topic t LEFT JOIN (SELECT COUNT(id)" \ 
     836          " AS replies, MAX(time) as lastreply, topic FROM message GROUP BY" \ 
     837          " topic) m ON t.id = m.topic WHERE t.forum = %s ORDER BY " \ 
     838          + order_by + (" ASC", " DESC")[bool(desc)] 
    836839        self.log.debug(sql % (forum,)) 
    837840        cursor.execute(sql, (forum,)) 
    838841        topics = [] 
     
    841844            row['author'] = wiki_to_oneliner(row['author'], self.env) 
    842845            row['body'] = wiki_to_html(row['body'], self.env, req) 
    843846            if row['lastreply']: 
    844                 row['lastreply'] = pretty_timedelta(row['lastreply']
     847                row['lastreply'] = pretty_timedelta(float(row['lastreply'])
    845848            else: 
    846849                row['lastreply'] = 'No replies' 
    847850            row['time'] = format_datetime(row['time']) 
  • 0.10/tracdiscussion/wiki.py

    old new  
    136136              id), title = label, class_ = 'missing') 
    137137        elif ns == 'topic': 
    138138            columns = ('forum', 'forum_subject', 'subject') 
    139             sql = "SELECT t.forum, (SELECT f.subject FROM forum f, topic t" \ 
    140               " WHERE f.id = t.forum), t.subject FROM topic t WHERE t.id = %s" 
     139            sql = "SELECT t.forum, f.subject, t.subject FROM topic t LEFT" \ 
     140              " JOIN (SELECT subject, id FROM forum GROUP BY id) f ON" \ 
     141              " t.forum = f.id WHERE t.id = %s" 
    141142            self.log.debug(sql % (id,)) 
    142143            cursor.execute(sql, (id,)) 
    143144            for row in cursor: 
     
    149150              id), title = label, class_ = 'missing') 
    150151        elif ns == 'message': 
    151152            columns = ('forum', 'topic', 'forum_subject', 'subject') 
    152             sql = "SELECT m.forum, m.topic, (SELECT f.subject FROM forum f," \ 
    153               " message m WHERE f.id = m.forum), (SELECT t.subject FROM" \ 
    154               " topic t, message m WHERE t.id = m.topic) FROM message m" \ 
    155               " WHERE m.id = %s" 
     153            sql = "SELECT m.forum, m.topic, f.subject, t.subject FROM" \ 
     154              " message m, (SELECT subject, id FROM forum GROUP BY id) f," \ 
     155              " (SELECT subject, id FROM topic GROUP BY id) t WHERE" \ 
     156              " m.forum = f.id AND m.topic = t.id AND m.id = %s" 
    156157            self.log.debug(sql % (id,)) 
    157158            cursor.execute(sql, (id,)) 
    158159            for row in cursor: 
  • 0.10/tracdiscussion/search.py

    old new  
    4343 
    4444        # Search in messages 
    4545        columns = ('id', 'forum', 'topic', 'time', 'author', 'body', 'subject') 
    46         sql = "SELECT id, forum, topic, time, author, body, (SELECT" \ 
    47           " subject FROM topic t WHERE t.id = message.topic) FROM message" \ 
    48           " WHERE body LIKE '%%%s%%'" % (query) 
     46        sql = "SELECT m.id, m.forum, m.topic, m.time, m.author, m.body," \ 
     47          " t.subject FROM message m LEFT JOIN (SELECT subject, id FROM" \ 
     48          " topic GROUP BY id) t ON t.id = m.topic WHERE body LIKE '%%%s%%'" \ 
     49          % (query) 
    4950        self.log.debug(sql) 
    5051        cursor.execute(sql) 
    5152        for row in cursor: 
  • 0.10/tracdiscussion/timeline.py

    old new  
    9696 
    9797    def _get_changed_topics(self, cursor, start, stop): 
    9898        columns = ('id', 'subject', 'author', 'time', 'forum', 'forum_name') 
    99         sql = "SELECT t.id, t.subject, t.author, t.time, t.forum, (SELECT" \ 
    100           " f.name FROM forum f, topic t WHERE f.id = t.forum) FROM topic t" \ 
    101           " WHERE t.time BETWEEN %s AND %s" 
     99        sql = "SELECT t.id, t.subject, t.author, t.time, t.forum, f.name" \ 
     100          " FROM topic t LEFT JOIN (SELECT name, id FROM forum GROUP BY id)" \ 
     101          " f ON t.forum = f.id WHERE time BETWEEN %s AND %s" 
    102102        self.log.debug(sql % (start, stop)) 
    103103        cursor.execute(sql, (start, stop)) 
    104104        for row in cursor: 
     
    108108    def _get_changed_messages(self, cursor, start, stop): 
    109109        columns = ('id', 'author', 'time', 'forum', 'topic', 'forum_name', 
    110110          'topic_subject') 
    111         sql = "SELECT m.id, m.author, m.time, m.forum, m.topic, (SELECT" \ 
    112           " f.name FROM forum f, message m WHERE f.id = m.forum), (SELECT" \ 
    113           " t.subject FROM topic t, message m WHERE t.id = m.topic) FROM" \ 
    114           " message m WHERE m.time BETWEEN %s AND %s" 
     111        sql = "SELECT m.id, m.author, m.time, m.forum, m.topic, f.name," \ 
     112          " t.subject FROM message m, (SELECT name, id FROM forum GROUP BY" \ 
     113          " id) f, (SELECT subject, id FROM topic GROUP BY id) t WHERE" \ 
     114          " t.id = m.topic AND f.id = m.forum AND time BETWEEN %s AND %s" 
    115115        self.log.debug(sql % (start, stop)) 
    116116        cursor.execute(sql, (start, stop)) 
    117117        for row in cursor: