On Jul 12, 2012, at 2:58 PM, Russ wrote:

> 
> I'm trying to figure out how to change this so that, rather than converting 
> in python, the conversion happens at the database layer using AT TIME ZONE 
> syntax.  I've tried using @compiles as follows:
> 
> #THIS CODE DOES NOT WORK...
> @compiles(UTCEnforcedDateTime)
> def compile_UTCEnforcedDateTime(element, compiler, **kwargs):
>     tz = UTCEnforcedDateTime.local_tz
>     if tz:
>         ret = "%s AT TIMEZONE '%s'" % (element.name, str(tz))
>     else:
>         ret = compiler.process(element) #NOT RIGHT - what is?
>     return ret
> 
> However, this is not right for a few reasons...

There is no functionality right now that allows special SQL to be automatically 
associated with a type, as associated with an enclosing column expression, at 
query time.  There's a ticket to begin making this kind of thing possible which 
is http://www.sqlalchemy.org/trac/ticket/1534.   When using @compiles for a 
type, the only time that compilation takes effect is when the type itself is 
rendered, not its containing column - that means, only CREATE TABLE and CAST 
expressions.

Your best option here is to compose that SQL expression explicitly when you 
emit a query.  There's many ways to make this happen depending on if you're 
dealing with Core only or ORM.

There might be some ways to use @compiles around Column expressions which check 
the type too, but this might be a little tricky (or not).

I'd also look into why the in-python TZ conversion is so slow, it seems a 
little suspect that it is 5x slower than the rest of the entire operation 
overall.   I'd check things like, the result processor isn't being called 
repeatedly for the same value, stuff like that.


-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to sqlalchemy@googlegroups.com.
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.

Reply via email to