look for microseconds in MySQL datetimes

Issue #2534 resolved
Mike Bayer repo owner created an issue

even though the MySQLdb driver doesn't support the microseconds portion of datetimes yet:

diff -r 0dde519fe172cf3ac7241cbb1ab93b35beaf6d6a lib/sqlalchemy/dialects/mysql/base.py
--- a/lib/sqlalchemy/dialects/mysql/base.py Wed Jul 11 11:35:36 2012 -0400
+++ b/lib/sqlalchemy/dialects/mysql/base.py Fri Jul 13 19:00:09 2012 -0400
@@ -672,19 +672,49 @@
             return value
         return process

-class _MSTime(sqltypes.Time):
-    """MySQL TIME type."""
+class TIME(sqltypes.TIME):
+    """MySQL TIME type.
+
+    Recent versions of MySQL add support for
+    fractional seconds precision.   While the
+    :class:`.mysql.TIME` type now supports this,
+    note that many DBAPI drivers may not yet
+    include support.
+
+    """

     __visit_name__ = 'TIME'

+    def __init__(self, timezone=False, fsp=None):
+        """Construct a MySQL TIME type.
+
+        :param timezone: not used by the MySQL dialect.
+        :param fsp: fractional seconds precision value.
+         MySQL 5.6 supports storage of fractional seconds;
+         this parameter will be used when emitting DDL
+         for the TIME type.  Note that many DBAPI drivers
+         may not yet have support for fractional seconds,
+         however.
+
+        .. versionadded:: 0.8 The MySQL-specific TIME
+           type as well as fractional seconds support.
+
+        """
+        super(TIME, self).__init__(timezone=timezone)
+        self.fsp = fsp
+
     def result_processor(self, dialect, coltype):
         time = datetime.time
         def process(value):
             # convert from a timedelta value
             if value is not None:
+                microseconds = value.microseconds
                 seconds = value.seconds
                 minutes = seconds / 60
-                return time(minutes / 60, minutes % 60, seconds - minutes * 60)
+                return time(minutes / 60,
+                            minutes % 60,
+                            seconds - minutes * 60,
+                            microseconds=microseconds)
             else:
                 return None
         return process
@@ -1153,7 +1183,7 @@
         return process

 # old names
-MSTime = _MSTime
+MSTime = TIME
 MSSet = SET
 MSEnum = ENUM
 MSLongBlob = LONGBLOB
@@ -1187,7 +1217,7 @@
 colspecs = {
     sqltypes.Numeric: NUMERIC,
     sqltypes.Float: FLOAT,
-    sqltypes.Time: _MSTime,
+    sqltypes.Time: TIME,
     sqltypes.Enum: ENUM,
 }

@@ -1697,6 +1727,9 @@
         return "DATE"

     def visit_TIME(self, type_):
+        if type_.fsp:
+            return "TIME(%d)" % type_.fsp
+        else:
         return "TIME"

     def visit_TIMESTAMP(self, type_):

Comments (2)

  1. Log in to comment