Commits

Michael Manfre committed 60c999b

Refactor SQL server version detection. Moving logic from Connection to DatabaseWrapper to reduce customizations of adodbapi.

Comments (0)

Files changed (2)

sqlserver_ado/base.py

 
 def connection_string_from_settings():
     from django.conf import settings
-    return make_connection_string(settings)
+    db_settings = getattr(settings, 'DATABASES', {}).get('default', None) or settings
+    return make_connection_string(db_settings)
 
 def make_connection_string(settings):
     class wrap(object):
                 result = getattr(d, 'DATABASE_' + name)
             else:
                 result = getattr(d, name, None)
-            return result    
-            
+            return result
+
     settings = wrap(settings) 
     
     db_name = settings.NAME.strip()
     
     return ";".join(parts)
 
+
+VERSION_SQL2000 = 8
+VERSION_SQL2005 = 9
+VERSION_SQL2008 = 10
+VERSION_SQL2012 = 11
+
 class DatabaseWrapper(BaseDatabaseWrapper):
     vendor = 'microsoft'
     
             self.command_timeout,
             use_transactions=self.use_transactions,
         )
-        
-        if self.connection.is_sql2000:
+
+        # cache the properties on the connection
+        self.connection.adoConnProperties = dict([(x.Name, x.Value) for x in self.connection.adoConn.Properties])
+
+        if self.is_sql2000(make_connection=False):
             # SQL 2000 doesn't support the OUTPUT clause
             self.features.can_return_id_from_insert = False
         
         connection_created.send(sender=self.__class__, connection=self)
         return self.connection
 
-    def is_sql2000(self):
+    def __get_dbms_version(self, make_connection=True):
+        """
+        Returns the 'DBMS Version' string, or ''. If a connection to the database has not already
+        been established, a connection will be made when `make_connection` is True.
+        """
+        if not self.connection and make_connection:
+            self.__connect()
+        return self.connection.adoConnProperties.get('DBMS Version', '') if self.connection else ''
+
+    def is_sql2000(self, make_connection=True):
         """
         Returns True if the current connection is SQL2000. Establishes a
-        connection if needed.
+        connection if needed when make_connection is True.
         """
-        if not self.connection:
-            self.__connect()
-        return self.connection.is_sql2000
+        return self.__get_dbms_version(make_connection).startswith(unicode(VERSION_SQL2000))
 
-    def is_sql2005(self):
+    def is_sql2005(self, make_connection=True):
         """
         Returns True if the current connection is SQL2005. Establishes a
-        connection if needed.
+        connection if needed when make_connection is True.
         """
-        if not self.connection:
-            self.__connect()
-        return self.connection.is_sql2005
+        return self.__get_dbms_version(make_connection).startswith(unicode(VERSION_SQL2005))
 
-    def is_sql2008(self):
+    def is_sql2008(self, make_connection=True):
         """
         Returns True if the current connection is SQL2008. Establishes a
-        connection if needed.
+        connection if needed when make_connection is True.
         """
-        if not self.connection:
-            self.__connect()
-        return self.connection.is_sql2008        
+        return self.__get_dbms_version(make_connection).startswith(unicode(VERSION_SQL2008))
 
     def _cursor(self):
         if self.connection is None:

sqlserver_ado/dbapi.py

     if p.Size == 0:
         p.Size = -1
 
-VERSION_SQL2000 = 8
-VERSION_SQL2005 = 9
-VERSION_SQL2008 = 10
-
 class Connection(object):
     def __init__(self, adoConn, useTransactions=False):
         self.adoConn = adoConn
         self.adoConn.CursorLocation = defaultCursorLocation
         self.supportsTransactions = useTransactions
 
-        self.adoConnProperties = dict([(x.Name, x.Value) for x in self.adoConn.Properties])
-
         if self.supportsTransactions:
             self.adoConn.IsolationLevel = defaultIsolationLevel
             self.adoConn.BeginTrans() # Disables autocommit per DBPAI
 
-    @property
-    def is_sql2000(self):
-        v = self.adoConnProperties.get('DBMS Version', '')
-        return v.startswith(unicode(VERSION_SQL2000))
-
-    @property
-    def is_sql2005(self):
-        v = self.adoConnProperties.get('DBMS Version', '')
-        return v.startswith(unicode(VERSION_SQL2005))
-    
-    @property
-    def is_sql2008(self):
-        v = self.adoConnProperties.get('DBMS Version', '')
-        return v.startswith(unicode(VERSION_SQL2008))
-
     def _raiseConnectionError(self, errorclass, errorvalue):
         eh = self.errorhandler
         if eh is None: