Re: [sqlalchemy] Re: Custom UTC DateTime Type with Custom insert functionality

2011-02-09 Thread Michael Bayer
OK that ticket is complete if you feel like trying the default tip or 0.6 
branch:

0.6:
http://hg.sqlalchemy.org/sqlalchemy/archive/rel_0_6.tar.gz

tip:
http://hg.sqlalchemy.org/sqlalchemy/archive/default.tar.gz


On Feb 9, 2011, at 12:52 AM, Michael Bayer wrote:

 Yeah, thats because the compiler extension, wrapped directly around 
 _BindParamClause, is not called on an insert, when the bind is generated 
 within the compile.  That is why I made ticket #2042.  Not going to work 
 until I get around to that.
 
 It would work if you generated the bindparam() yourself, as occurs with 
 literal(), i.e. table.insert().values(date=literal(some_date, 
 type=UTCNowColumn()), but that's not the general INSERT form that gets used 
 with the ORM and such.
 
 
 
 
 On Feb 8, 2011, at 11:25 PM, chris e wrote:
 
 I understand that's the case for defaults, but to test the code I
 actually set a value for one of the columns that is of type
 UTCTimestamp, and the compiler extension was never called. I'm
 stumped.
 
 That said, I can keep moving, I've decided to just use oracle
 timestamps without timezones, and always convert to UTC since
 cx_Oracle doesn't handle timezones properly.
 
 On Feb 8, 7:18 pm, Michael Bayer mike...@zzzcomputing.com wrote:
 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 
 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 

[sqlalchemy] Re: Custom UTC DateTime Type with Custom insert functionality

2011-02-08 Thread chris e
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.



Re: [sqlalchemy] Re: Custom UTC DateTime Type with Custom insert functionality

2011-02-08 Thread Michael Bayer


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.



[sqlalchemy] Re: Custom UTC DateTime Type with Custom insert functionality

2011-02-08 Thread chris e
I understand that's the case for defaults, but to test the code I
actually set a value for one of the columns that is of type
UTCTimestamp, and the compiler extension was never called. I'm
stumped.

That said, I can keep moving, I've decided to just use oracle
timestamps without timezones, and always convert to UTC since
cx_Oracle doesn't handle timezones properly.

On Feb 8, 7:18 pm, Michael Bayer mike...@zzzcomputing.com wrote:
 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 
  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.



Re: [sqlalchemy] Re: Custom UTC DateTime Type with Custom insert functionality

2011-02-08 Thread Michael Bayer
Yeah, thats because the compiler extension, wrapped directly around 
_BindParamClause, is not called on an insert, when the bind is generated within 
the compile.  That is why I made ticket #2042.  Not going to work until I get 
around to that.

It would work if you generated the bindparam() yourself, as occurs with 
literal(), i.e. table.insert().values(date=literal(some_date, 
type=UTCNowColumn()), but that's not the general INSERT form that gets used 
with the ORM and such.




On Feb 8, 2011, at 11:25 PM, chris e wrote:

 I understand that's the case for defaults, but to test the code I
 actually set a value for one of the columns that is of type
 UTCTimestamp, and the compiler extension was never called. I'm
 stumped.
 
 That said, I can keep moving, I've decided to just use oracle
 timestamps without timezones, and always convert to UTC since
 cx_Oracle doesn't handle timezones properly.
 
 On Feb 8, 7:18 pm, Michael Bayer mike...@zzzcomputing.com wrote:
 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 
 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