Skip to content

Commit d92b300

Browse files
committed
Merge branch 'azure-1.11' of https://github.com/justinsg/django-pyodbc-azure into justinsg-azure-1.11
2 parents e85b10e + e13d614 commit d92b300

File tree

1 file changed

+32
-12
lines changed

1 file changed

+32
-12
lines changed

sql_server/pyodbc/base.py

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -395,20 +395,40 @@ def schema_editor(self, *args, **kwargs):
395395
return DatabaseSchemaEditor(self, *args, **kwargs)
396396

397397
@cached_property
398-
def sql_server_version(self):
399-
with self.temporary_connection() as cursor:
400-
cursor.execute("SELECT CAST(SERVERPROPERTY('ProductVersion') AS varchar)")
401-
ver = cursor.fetchone()[0]
402-
ver = int(ver.split('.')[0])
403-
if not ver in self._sql_server_versions:
404-
raise NotImplementedError('SQL Server v%d is not supported.' % ver)
405-
return self._sql_server_versions[ver]
398+
def sql_server_version(self, _known_versions={}):
399+
"""
400+
Get the SQL server version
401+
402+
The _known_versions default dictionary is created on the class. This is
403+
intentional - it allows us to cache this property's value across instances.
404+
Therefore, when Django creates a new database connection using the same
405+
alias, we won't need query the server again.
406+
"""
407+
if self.alias not in _known_versions:
408+
with self.temporary_connection() as cursor:
409+
cursor.execute("SELECT CAST(SERVERPROPERTY('ProductVersion') AS varchar)")
410+
ver = cursor.fetchone()[0]
411+
ver = int(ver.split('.')[0])
412+
if not ver in self._sql_server_versions:
413+
raise NotImplementedError('SQL Server v%d is not supported.' % ver)
414+
_known_versions[self.alias] = self._sql_server_versions[ver]
415+
return _known_versions[self.alias]
406416

407417
@cached_property
408-
def to_azure_sql_db(self):
409-
with self.temporary_connection() as cursor:
410-
cursor.execute("SELECT CAST(SERVERPROPERTY('EngineEdition') AS integer)")
411-
return cursor.fetchone()[0] == EDITION_AZURE_SQL_DB
418+
def to_azure_sql_db(self, _known_azures={}):
419+
"""
420+
Whether this connection is to a Microsoft Azure database server
421+
422+
The _known_azures default dictionary is created on the class. This is
423+
intentional - it allows us to cache this property's value across instances.
424+
Therefore, when Django creates a new database connection using the same
425+
alias, we won't need query the server again.
426+
"""
427+
if self.alias not in _known_azures:
428+
with self.temporary_connection() as cursor:
429+
cursor.execute("SELECT CAST(SERVERPROPERTY('EngineEdition') AS integer)")
430+
_known_azures[self.alias] = cursor.fetchone()[0] == EDITION_AZURE_SQL_DB
431+
return _known_azures[self.alias]
412432

413433
def _execute_foreach(self, sql, table_names=None):
414434
cursor = self.cursor()

0 commit comments

Comments
 (0)