Index: tracdiscussion/api.py =================================================================== --- tracdiscussion/api.py (revision 9009) +++ tracdiscussion/api.py (working copy) @@ -1329,9 +1329,13 @@ # Get one item functions. def _get_item(self, context, table, columns, where = '', values = ()): + if where: + where = 'WHERE ' + where + else: + where = '' sql_values = {'columns' : ', '.join(columns), 'table' : table, - 'where' : 'WHERE ' + where if where else ''} + 'where' : where } sql = ("SELECT %(columns)s " "FROM %(table)s " "%(where)s" % (sql_values)) @@ -1429,8 +1433,12 @@ # Get items count functions. def _get_items_count(self, context, table, where = '', values = ()): + if where: + where = 'WHERE ' + where + else: + where = '' sql_values = {'table' : table, - 'where' : 'WHERE ' + where if where else ''} + 'where' : where } sql = ("SELECT COUNT(id) " "FROM %(table)s " "%(where)s" % (sql_values)) @@ -1452,13 +1460,29 @@ def _get_items(self, context, table, columns, where = '', values = (), order_by = '', desc = False, limit = 0, offset = 0): + if where: + where = 'WHERE ' + where + else: + where = '' + if order_by: + order_by = 'ORDER BY ' + order_by + (' ASC', ' DESC')[bool(desc)] + else: + order_by = '' + if limit: + limit = 'LIMIT ' + to_unicode(limit) + else: + limit = '' + if offset: + offset = 'OFFSET ' + to_unicode(offset) + else: + offset = '' + sql_values = {'columns' : ', '.join(columns), 'table' : table, - 'where' : 'WHERE ' + where if where else '', - 'order_by' : 'ORDER BY ' + order_by + (' ASC', ' DESC')[bool(desc)] - if order_by else '', - 'limit' : 'LIMIT ' + to_unicode(limit) if limit else '', - 'offset' : ' OFFSET ' + to_unicode(offset) if offset else ''} + 'where' : where, + 'order_by' : order_by, + 'limit' : limit, + 'offset' : offset } sql = ("SELECT %(columns)s " "FROM %(table)s " "%(where)s " @@ -1489,9 +1513,14 @@ # Get forum groups. if order_by != 'forum': order_by = 'g.' + order_by + + if order_by: + order_by + (' ASC', ' DESC')[bool(desc)] + else: + order_by = '' + columns = ('id', 'name', 'description', 'forums') - sql_values = {'order_by' : 'ORDER BY ' + order_by + - (' ASC', ' DESC')[bool(desc)] if order_by else ''} + sql_values = {'order_by' : 'ORDER BY ' + order_by } sql = ("SELECT g.id, g.name, g.description, f.forums " "FROM forum_group g " "LEFT JOIN " @@ -1555,11 +1584,15 @@ if not order_by in ('topics', 'replies', 'lasttopic', 'lastreply'): order_by = 'f.' + order_by + if order_by: + order_by = order_by + (' ASC',' DESC')[bool(desc)] + else: + order_by = '' + columns = ('id', 'name', 'author', 'time', 'moderators', 'subscribers', 'forum_group', 'subject', 'description', 'topics', 'replies', 'lasttopic', 'lastreply') - sql_values = {'order_by' : 'ORDER BY ' + order_by + (' ASC', - ' DESC')[bool(desc)] if order_by else ''} + sql_values = {'order_by' : 'ORDER BY ' + order_by } sql = ("SELECT f.id, f.name, f.author, f.time, f.moderators, " "f.subscribers, f.forum_group, f.subject, f.description, " "ta.topics, ta.replies, ta.lasttopic, ta.lastreply " @@ -1613,18 +1646,32 @@ if not order_by in ('replies', 'lastreply',): order_by = 't.' + order_by + if order_by: + order_by = 'ORDER BY ' + order_by + (' ASC', ' DESC')[bool(desc)] + else: + order_by = '' + if limit: + limit = 'LIMIT ' + to_unicode(limit) + else: + limit = '' + if offset: + offset = 'OFFSET ' + to_unicode(offset) + else: + offset = '' + if with_body: columns = ('id', 'forum', 'time', 'subject', 'body', 'author', 'replies', 'lastreply') + with_body_column = 't.body, ' else: columns = ('id', 'forum', 'time', 'subject', 'author', 'replies', 'lastreply') - sql_values = {'with_body' : 't.body, ' if with_body else '', + with_body_column = '' + sql_values = {'with_body' : with_body_column, 'forum_id' : to_unicode(forum_id), - 'order_by' : 'ORDER BY ' + order_by + (' ASC', ' DESC')[bool(desc)] - if order_by else '', - 'limit' : 'LIMIT ' + to_unicode(limit) if limit else '', - 'offset' : 'OFFSET ' + to_unicode(offset) if offset else ''} + 'order_by' : order_by, + 'limit' : limit, + 'offset' : offset } sql = ("SELECT t.id, t.forum, t.time, t.subject, %(with_body)s" "t.author, m.replies, m.lastreply " "FROM topic t " @@ -1655,9 +1702,12 @@ def get_messages(self, context, topic_id, order_by = 'time', desc = False): order_by = 'm.' + order_by columns = ('id', 'replyto', 'time', 'author', 'body') + if order_by: + order_by = order_by + (' ASC',' DESC')[bool(desc)] + else: + order_by = '' sql_values = {'topic_id' : to_unicode(topic_id), - 'order_by' : 'ORDER BY ' + order_by + (' ASC',' DESC')[bool(desc)] - if order_by else ''} + 'order_by' : 'ORDER BY ' + order_by } sql = ("SELECT m.id, m.replyto, m.time, m.author, m.body " "FROM message m " "WHERE m.topic = %(topic_id)s " @@ -1751,8 +1801,10 @@ # Delete items functions. def _delete_item(self, context, table, where = '', values = ()): + if not where: + where = '' sql_values = {'table' : table, - 'where' : ' WHERE ' + where if where else ''} + 'where' : ' WHERE ' + where } sql = ("DELETE FROM %(table)s " "%(where)s" % (sql_values)) self.log.debug(sql % values) @@ -1794,10 +1846,12 @@ # Set item functions. def _set_item(self, context, table, column, value, where = '', values = ()): + if not where: + where = '' sql_values = {'table' : table, 'column' : column, 'value' : to_unicode(value), - 'where' : 'WHERE ' + where if where else ''} + 'where' : 'WHERE ' + where } sql = ("UPDATE %(table)s " "SET %(column)s = \"%(value)s\" " "%(where)s" % (sql_values))