On Fri, Jan 30, 2009 at 7:59 PM, koenb <[email protected]> wrote: > >> As committed, the expressions framework is fairly dumb - it is really >> just a way to do two things: >> 1) Expand Django-style field references into column names (and joins >> if required) >> 2) Turn Python expression trees into SQL expression trees. >> >> This means that the operations that are allowed depend somewhat on the >> database in use. For example, on MySQL, you can perform mod operations >> on floats; under Postgres, you can't. >> >> Constants in expressions are rendered as-is, so whether dates are >> allowed depends on what your database allows. From some quick tests, >> date + integer works on SQLite and MySQL -- or, at least, doesn't >> throw an error. I haven't confirmed that the answer it gives is >> actually meaningful. Postgres complains with an error "operator does >> not exist: timestamp with time zone + integer". >> >> However, I'm certainly interested in entertaining any modification to >> support more interesting expressions - for example, converting the >> expression "F('date') + timedelta(days=3)" into meaningful SQL. >> Patches welcome. > > I will have a look at how expressions are implemented (I am aware of > the patches philosophy).
Glad to hear it :-) If you are interested in implementing this, I'm happy to give you pointers. The first pointer I can suggest is that you will be looking at the SQLEvaluator class contained in django/db/models/sql/expressions.py. This class traverses the expression tree, turning it into raw SQL. The actual expression tree is a series of tree.Node objects defined in django/db/models/expressions.py. Also - anything you do here shouldn't be date-specific. Other data types - such as strings and GIS types - will require similar capabilities. Try to keep that in mind when you are looking to potential solutions for the date problem. > It seems though that the different backends will certainly need > different handlings: Indeed. The Postgres option should be fairly easy to implement - you just need to replace leaf nodes on the expression tree that are timedeltas with matching "interval X" statements. SQLite style expressions will be a little harder. Yours, Russ Magee %-) --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~----------~----~----~----~------~----~------~--~---

