Commits

Michael Manfre  committed 613d75c

Rework SQL server version detection to auto-introspect if a connection has not been established

  • Participants
  • Parent commits 9542cc0
  • Tags release/1.0.1

Comments (0)

Files changed (2)

File sqlserver_ado/base.py

         except ValueError:   
             self.command_timeout = 30
         
+        self.ops.is_sql2005 = self.is_sql2005
+        self.ops.is_sql2008 = self.is_sql2008
+
+    def __connect(self):
+        """Connect to the database"""
+        self.connection = Database.connect(
+            make_connection_string(self.settings_dict),
+            self.command_timeout
+        )
+        connection_created.send(sender=self.__class__)
+        return self.connection
+
+    def is_sql2005(self):
+        """
+        Returns True if the current connection is SQL2005. Establishes a
+        connection if needed.
+        """
+        if not self.connection:
+            self.__connect()
+        return self.connection.is_sql2005
+
+    def is_sql2008(self):
+        """
+        Returns True if the current connection is SQL2008. Establishes a
+        connection if needed.
+        """
+        if not self.connection:
+            self.__connect()
+        return self.connection.is_sql2008        
+
     def _cursor(self):
         if self.connection is None:
-            self.connection = Database.connect(
-                                make_connection_string(self.settings_dict),
-                                self.command_timeout
-                              )
-
-            # Provide DatabaseOperations with SQL Version
-            setattr(self.ops, 'is_sql2005', self.connection.is_sql2005)
-            setattr(self.ops, 'is_sql2008', self.connection.is_sql2008)
-            
-            connection_created.send(sender=self.__class__)
-
+            self.__connect()
         return Database.Cursor(self.connection)

File sqlserver_ado/operations.py

             raise ValueError("SQL Server 2005 does not support timezone-aware datetimes.")
 
         # SQL Server 2005 doesn't support microseconds
-        if self.is_sql2005:
+        if self.is_sql2005():
            value = value.replace(microsecond=0)
-
         return value
     
     def value_to_db_time(self, value):
-        if not self.is_sql2005:
+        if not self.is_sql2005():
             return value
 
         # MS SQL 2005 doesn't support microseconds
             return None
         
         return value.replace(microsecond=0)
-	        
+
     def value_to_db_decimal(self, value, max_digits, decimal_places):
         if value is None or value == '':
             return None