@@ -192,7 +192,7 @@ def __init__(self, connection):
192
192
self .supports_microsecond_precision = self ._microseconds_precision ()
193
193
194
194
def _microseconds_precision (self ):
195
- if self .connection .server_version >= (5 , 6 , 3 ):
195
+ if self .connection .mysql_version >= (5 , 6 , 3 ):
196
196
return True
197
197
return False
198
198
@@ -212,7 +212,7 @@ def _mysql_storage_engine(self):
212
212
cursor .execute (droptable )
213
213
cursor .execute ('CREATE TABLE {table} (X INT)' .format (table = tblname ))
214
214
215
- if self .connection .server_version >= (5 , 0 , 0 ):
215
+ if self .connection .mysql_version >= (5 , 0 , 0 ):
216
216
cursor .execute (
217
217
"SELECT ENGINE FROM INFORMATION_SCHEMA.TABLES "
218
218
"WHERE TABLE_SCHEMA = %s AND TABLE_NAME = %s" ,
@@ -399,7 +399,7 @@ def sequence_reset_by_name_sql(self, style, sequences):
399
399
# Truncate already resets the AUTO_INCREMENT field from
400
400
# MySQL version 5.0.13 onwards. Refs #16961.
401
401
res = []
402
- if self .connection .server_version < (5 , 0 , 13 ):
402
+ if self .connection .mysql_version < (5 , 0 , 13 ):
403
403
fmt = "{alter} {table} {{tablename}} {auto_inc} {field};" .format (
404
404
alter = style .SQL_KEYWORD ('ALTER' ),
405
405
table = style .SQL_KEYWORD ('TABLE' ),
@@ -431,13 +431,7 @@ def value_to_db_datetime(self, value):
431
431
"MySQL backend does not support timezone-aware times."
432
432
)
433
433
434
- try :
435
- # Django 1.6
436
- self .connection .ensure_connection ()
437
- except AttributeError :
438
- if not self .connection .connection :
439
- self .connection ._connect ()
440
- return self .connection .connection .converter ._datetime_to_mysql (value )
434
+ return self .connection .converter .to_mysql (value )
441
435
442
436
def value_to_db_time (self , value ):
443
437
if value is None :
@@ -448,13 +442,7 @@ def value_to_db_time(self, value):
448
442
raise ValueError ("MySQL backend does not support timezone-aware "
449
443
"times." )
450
444
451
- try :
452
- # Django 1.6
453
- self .connection .ensure_connection ()
454
- except AttributeError :
455
- if not self .connection .connection :
456
- self .connection ._connect ()
457
- return self .connection .connection .converter ._time_to_mysql (value )
445
+ return self .connection .converter .to_mysql (value )
458
446
459
447
def year_lookup_bounds (self , value ):
460
448
# Again, no microseconds
@@ -467,7 +455,7 @@ def year_lookup_bounds_for_datetime_field(self, value):
467
455
# Again, no microseconds
468
456
first , second = super (DatabaseOperations ,
469
457
self ).year_lookup_bounds_for_datetime_field (value )
470
- if self .connection .server_version >= (5 , 6 , 4 ):
458
+ if self .connection .mysql_version >= (5 , 6 , 4 ):
471
459
return [first .replace (microsecond = 0 ), second ]
472
460
else :
473
461
return [first .replace (microsecond = 0 ),
@@ -522,15 +510,8 @@ class DatabaseWrapper(BaseDatabaseWrapper):
522
510
523
511
def __init__ (self , * args , ** kwargs ):
524
512
super (DatabaseWrapper , self ).__init__ (* args , ** kwargs )
525
- self .server_version = None
526
-
527
- # Since some features depend on the MySQL version, we need to connect
528
- try :
529
- # Django 1.6
530
- self .ensure_connection ()
531
- except AttributeError :
532
- self ._connect ()
533
513
514
+ self .converter = DjangoMySQLConverter ()
534
515
self .ops = DatabaseOperations (self )
535
516
self .features = DatabaseFeatures (self )
536
517
self .client = DatabaseClient (self )
@@ -584,14 +565,13 @@ def get_connection_params(self):
584
565
def get_new_connection (self , conn_params ):
585
566
# Django 1.6
586
567
cnx = mysql .connector .connect (** conn_params )
587
- self .server_version = cnx .get_server_version ()
588
568
cnx .set_converter_class (DjangoMySQLConverter )
589
569
590
570
return cnx
591
571
592
572
def init_connection_state (self ):
593
573
# Django 1.6
594
- if self .server_version < (5 , 5 , 3 ):
574
+ if self .mysql_version < (5 , 5 , 3 ):
595
575
# See sysvar_sql_auto_is_null in MySQL Reference manual
596
576
self .connection .cmd_query ("SET SQL_AUTO_IS_NULL = 0" )
597
577
@@ -737,6 +717,11 @@ def is_usable(self):
737
717
# Django 1.6
738
718
return self .connection .is_connected ()
739
719
740
- @property
720
+ @cached_property
741
721
def mysql_version (self ):
742
- return self .server_version
722
+ config = self .get_connection_params ()
723
+ temp_conn = mysql .connector .connect (** config )
724
+ server_version = temp_conn .get_server_version ()
725
+ temp_conn .close ()
726
+
727
+ return server_version
0 commit comments