On Feb 8, 2011, at 4:56 PM, chris e wrote:

> To simplify date handling in a project on which I am working, I am
> storing UTC dates in the database in a timestamp with timezone field,
> however, because cx_Oracle does not have any timezone functionality, I
> need to cast the UTC timestamp I'm inserting into the database as a
> timestamp in UTC so that the database does not convert it to the db
> timezone. This also needs to apply to default values.
> 
> 
> I have the following, however, it is not called for default values:
> 

The @compiles for _BindParamClause was never expected and is not covered within 
a visit_insert() right now.  Ticket #2042 is added.  However, you're better off 
using SQL level UTC functions for defaults in any case which would be the 
workaround here.  Here's one I use for PG + SQL Server.  You can add another 
for Oracle that includes your CAST expression if needed:

class utcnow(expression.FunctionElement):
    type = DateTime()

@compiles(utcnow, 'postgresql')
def _pg_utcnow(element, compiler, **kw):
    return "TIMEZONE('utc', CURRENT_TIMESTAMP)"

@compiles(utcnow, 'mssql')
def _ms_utcnow(element, compiler, **kw):
    return "GETUTCDATE()"



> from pytz import UTC
> class UTCDateTime(TypeDecorator):
>    impl = TIMESTAMP
> 
>    # add the UTC time zone info to naive timestamps
>    def process_result_value(self, value, dialect) :
>        if value != None :
>            value = UTC.localize(value)
> 
>        return value
> 
> @compiles(_BindParamClause)
> def _compile_utc_date(element, compiler, **kw):
>    if isinstance(element.type, UTCDateTime) :
>       return "from_tz(cast(%s as timestamp), 'UTC')" \
>           % compiler.visit_bindparam(element, **kw)
> 
>    return compiler.visit_bindparam(element, **kw)
> 
> -- 
> 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.
> 

-- 
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