Commits

Mike Bayer committed 50a996f

- Supplied a custom strftime() function which
handles dates before 1900. [ticket:968]
- removed a few bogus classes from util

Comments (0)

Files changed (4)

 - mysql
     - Added MSMediumInteger type [ticket:1146].
 
+- sqlite
+    - Supplied a custom strftime() function which
+      handles dates before 1900.  [ticket:968]
+      
 - oracle
     - has_sequence() now takes schema name into account
       [ticket:1155]

lib/sqlalchemy/databases/sqlite.py

             elif value is not None:
                 if self.__microsecond__ and getattr(value, 'microsecond', None) is not None:
                     if self.__legacy_microseconds__:
-                        return value.strftime(self.__format__ + '.' + str(value.microsecond))
+                        return util.strftime(value, self.__format__ + '.' + str(value.microsecond))
                     else:
-                        return value.strftime(self.__format__ + ('.%06d' % value.microsecond))
+                        return util.strftime(value, self.__format__ + ('.%06d' % value.microsecond))
                 else:
-                    return value.strftime(self.__format__)
+                    return util.strftime(value, self.__format__)
             else:
                 return None
         return process

lib/sqlalchemy/util.py

 import inspect, itertools, new, operator, sets, sys, warnings, weakref
 import __builtin__
 types = __import__('types')
+import re
 
 from sqlalchemy import exceptions
 
         exec py in env
         setattr(into_cls, method, env[method])
 
-class SimpleProperty(object):
-    """A *default* property accessor."""
-
-    def __init__(self, key):
-        self.key = key
-
-    def __set__(self, obj, value):
-        setattr(obj, self.key, value)
-
-    def __delete__(self, obj):
-        delattr(obj, self.key)
-
-    def __get__(self, obj, owner):
-        if obj is None:
-            return self
-        else:
-            return getattr(obj, self.key)
-
-
-class NotImplProperty(object):
-  """a property that raises ``NotImplementedError``."""
-
-  def __init__(self, doc):
-      self.__doc__ = doc
-
-  def __set__(self, obj, value):
-      raise NotImplementedError()
-
-  def __delete__(self, obj):
-      raise NotImplementedError()
-
-  def __get__(self, obj, owner):
-      if obj is None:
-          return self
-      else:
-          raise NotImplementedError()
-
+_strftime_conversions = {
+    "Y":lambda dt: "%4.4d" % getattr(dt, 'year', 0),
+    "m":lambda dt: "%2.2d" % getattr(dt, 'month', 0),
+    "d":lambda dt: "%2.2d" % getattr(dt, 'day', 0),
+    "H":lambda dt: "%2.2d" % getattr(dt, 'hour', 0),
+    "M":lambda dt: "%2.2d" % getattr(dt, 'minute', 0),
+    "S":lambda dt: "%2.2d" % getattr(dt, 'second', 0),
+    "c":lambda dt: '%06d' % getattr(dt, 'microsecond', 0)
+}
+_strftime_regexp = re.compile(r'%(\w)')
+    
+def strftime(dt, format):
+    """format a date, time or datetime object.
+    
+    Partially compatible with datetime.strftime().
+    
+    handles years before 1900.
+    
+    Also adds "c", representing microseconds.
+    
+    """
+    def repl(m):
+        return _strftime_conversions[m.group(1)](dt)
+    return _strftime_regexp.sub(repl, format)
+    
 class OrderedProperties(object):
     """An object that maintains the order in which attributes are set upon it.
 

test/sql/testtypes.py

         if testing.against('oracle'):
             import sqlalchemy.databases.oracle as oracle
             insert_data =  [
-                    [7, 'jack',
+                    (7, 'jack',
                      datetime.datetime(2005, 11, 10, 0, 0),
                      datetime.date(2005,11,10),
-                     datetime.datetime(2005, 11, 10, 0, 0, 0, 29384)],
-                    [8, 'roy',
+                     datetime.datetime(2005, 11, 10, 0, 0, 0, 29384)),
+                    (8, 'roy',
                      datetime.datetime(2005, 11, 10, 11, 52, 35),
                      datetime.date(2005,10,10),
-                     datetime.datetime(2006, 5, 10, 15, 32, 47, 6754)],
-                    [9, 'foo',
+                     datetime.datetime(2006, 5, 10, 15, 32, 47, 6754)),
+                    (9, 'foo',
                      datetime.datetime(2006, 11, 10, 11, 52, 35),
                      datetime.date(1970,4,1),
-                     datetime.datetime(2004, 9, 18, 4, 0, 52, 1043)],
-                    [10, 'colber', None, None, None]
+                     datetime.datetime(2004, 9, 18, 4, 0, 52, 1043)),
+                    (10, 'colber', None, None, None),
              ]
             fnames = ['user_id', 'user_name', 'user_datetime',
                       'user_date', 'user_time']
                 time_micro = 0
 
             insert_data =  [
-                [7, 'jack',
+                (7, 'jack',
                  datetime.datetime(2005, 11, 10, 0, 0),
                  datetime.date(2005, 11, 10),
-                 datetime.time(12, 20, 2)],
-                [8, 'roy',
+                 datetime.time(12, 20, 2)),
+                (8, 'roy',
                  datetime.datetime(2005, 11, 10, 11, 52, 35),
                  datetime.date(2005, 10, 10),
-                 datetime.time(0, 0, 0)],
-                [9, 'foo',
+                 datetime.time(0, 0, 0)),
+                (9, 'foo',
                  datetime.datetime(2005, 11, 10, 11, 52, 35, datetime_micro),
                  datetime.date(1970, 4, 1),
-                 datetime.time(23, 59, 59, time_micro)],
-                [10, 'colber', None, None, None]
+                 datetime.time(23, 59, 59, time_micro)),
+                (10, 'colber', None, None, None),
             ]
+            
+            
             fnames = ['user_id', 'user_name', 'user_datetime',
                       'user_date', 'user_time']
 
                        Column('user_date', Date),
                        Column('user_time', Time)]
 
+        if testing.against('sqlite', 'postgres'):
+            insert_data.append(
+                (11, 'historic',
+                datetime.datetime(1850, 11, 10, 11, 52, 35, datetime_micro),
+                datetime.date(1727,4,1),
+                None),
+            )
+
         users_with_date = Table('query_users_with_date',
                                 MetaData(testing.db), *collist)
         users_with_date.create()
     def testdate(self):
         global insert_data
 
-        l = map(list, users_with_date.select().execute().fetchall())
+        l = map(tuple, users_with_date.select().execute().fetchall())
         self.assert_(l == insert_data,
                      'DateTest mismatch: got:%s expected:%s' % (l, insert_data))