Does it make sense to modify the gluon.sql Expression __invert__
method so a double invert is possible.

Currently this produces an error:

    print db().select(db.person.name, orderby=~~db.person.name)

    ProgrammingError: (1064, "You have an error in your SQL syntax;
check the manual that corresponds to your MySQL server version for the
right syntax to use near 'DESC' at line 1")

The cause of the error is obvious when you look at the SQL query.

    SELECT person.name FROM person ORDER BY person.name DESC DESC

The second invert adds an additional 'DESC' which makes the query
invalid.

I tried this and it worked.

    def __invert__(self):
        if str(self)[-5:] == ' DESC':
            return Expression(str(self)[:-5], None, None)
        else:
            return Expression(str(self) + ' DESC', None, None)

To explain why a double invert is useful, I have a function that
returns the rows from a query and the order of the rows is determined
by parameters.

    def people(order_expr, reverse=False)
        if False:
            orderby = order_expr
        else:
            orderby = ~order_expr
        return db().select(db.person.ALL, orderby=order_by)

It is used in a report. Each field in the report has a default order
expression. The default order direction for most fields is ascending,
but for some fields, eg updated_on, the default order is more
intuitive as descending. The report will allow the user to switch the
direction with a click. The code simply toggles the reverse parameter.
I'd like the function to handle any of these combinations.

    people(db.person.name)
    people(db.person.name, reverse=True)
    people(~db.person.updated_on)
    people(~db.person.updated_on, reverse=True)

I am not familiar with the web2py code enough to be aware of the
effects of changing the __invert__ so my suggestion might have some
unwanted results. Hopefully others can provide input.

Reply via email to