The TypeDecorator is not used for defaults.   You set "default=utc_timestamp()" 
on your Column.    The SQL expression is rendered directly in the INSERT when 
no value given, no bind params used.


On Feb 8, 2011, at 10:13 PM, chris e wrote:

> I now have the code below, but the _oracle_utc_timestamp function is
> never called, even when I do explicitly set a value.
> 
> class UTCTimestamp(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
> 
>    def process_bind_param(self, value, dialect):
>        # if we have a value convert it to UTC if needed
>        if value != None :
>            if value.tzinfo :
>                value = value.astimezone(UTC)
>        return value
> 
> class utc_timestamp(FunctionElement):
>    type = DateTime()
> 
> @compiles(utc_timestamp)
> def _oracle_utc_timestamp(element, compiler, **kw):
>    import pdb
>    pdb.set_trace()
> 
> 
> 
> 
> 
> 
> On Feb 8, 6:09 pm, Michael Bayer <mike...@zzzcomputing.com> wrote:
>> 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 
>>> athttp://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.
> 

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