18
18
from __future__ import unicode_literals
19
19
20
20
import sys
21
- import warnings
22
21
import django
23
22
24
23
try :
52
51
from mysql .connector .django .introspection import DatabaseIntrospection
53
52
from mysql .connector .django .validation import DatabaseValidation
54
53
54
+ try :
55
+ import pytz
56
+ HAVE_PYTZ = True
57
+ except ImportError :
58
+ HAVE_PYTZ = False
59
+
55
60
DatabaseError = mysql .connector .DatabaseError
56
61
IntegrityError = mysql .connector .IntegrityError
57
62
NotSupportedError = mysql .connector .NotSupportedError
@@ -163,7 +168,7 @@ def __init__(self, connection):
163
168
self .supports_microsecond_precision = self ._microseconds_precision ()
164
169
165
170
def _microseconds_precision (self ):
166
- if self .connection .get_server_version () >= (5 , 6 , 3 ):
171
+ if self .connection .server_version >= (5 , 6 , 3 ):
167
172
return True
168
173
return False
169
174
@@ -183,7 +188,7 @@ def _mysql_storage_engine(self):
183
188
cursor .execute (droptable )
184
189
cursor .execute ('CREATE TABLE {table} (X INT)' .format (table = tblname ))
185
190
186
- if self .connection .get_server_version () >= (5 , 0 , 0 ):
191
+ if self .connection .server_version >= (5 , 0 , 0 ):
187
192
cursor .execute (
188
193
"SELECT ENGINE FROM INFORMATION_SCHEMA.TABLES "
189
194
"WHERE TABLE_SCHEMA = %s AND TABLE_NAME = %s" ,
@@ -215,11 +220,12 @@ def has_zoneinfo_database(self):
215
220
MySQL cannot perform time zone conversions reliably.
216
221
"""
217
222
# Django 1.6
218
- if pytz is None :
223
+ if not HAVE_PYTZ :
219
224
return False
220
225
221
- self .connection .cmd_query ("SELECT 1 FROM mysql.time_zone LIMIT 1" )
222
- return self .connection .get_rows ()[0 ] is not []
226
+ cursor = self .connection .cursor ()
227
+ cursor .execute ("SELECT 1 FROM mysql.time_zone LIMIT 1" )
228
+ return cursor .fetchall () != []
223
229
224
230
225
231
class DatabaseOperations (BaseDatabaseOperations ):
@@ -358,7 +364,7 @@ def sequence_reset_by_name_sql(self, style, sequences):
358
364
# Truncate already resets the AUTO_INCREMENT field from
359
365
# MySQL version 5.0.13 onwards. Refs #16961.
360
366
res = []
361
- if self .connection .get_server_version () < (5 , 0 , 13 ):
367
+ if self .connection .server_version < (5 , 0 , 13 ):
362
368
fmt = "{alter} {table} {{tablename}} {auto_inc} {field};" .format (
363
369
alter = style .SQL_KEYWORD ('ALTER' ),
364
370
table = style .SQL_KEYWORD ('TABLE' ),
@@ -389,6 +395,13 @@ def value_to_db_datetime(self, value):
389
395
raise ValueError (
390
396
"MySQL backend does not support timezone-aware times."
391
397
)
398
+
399
+ try :
400
+ # Django 1.6
401
+ self .connection .ensure_connection ()
402
+ except AttributeError :
403
+ if not self .connection .connection :
404
+ self .connection ._connect ()
392
405
return self .connection .connection .converter ._datetime_to_mysql (value )
393
406
394
407
def value_to_db_time (self , value ):
@@ -400,6 +413,12 @@ def value_to_db_time(self, value):
400
413
raise ValueError ("MySQL backend does not support timezone-aware "
401
414
"times." )
402
415
416
+ try :
417
+ # Django 1.6
418
+ self .connection .ensure_connection ()
419
+ except AttributeError :
420
+ if not self .connection .connection :
421
+ self .connection ._connect ()
403
422
return self .connection .connection .converter ._time_to_mysql (value )
404
423
405
424
def year_lookup_bounds (self , value ):
@@ -413,7 +432,7 @@ def year_lookup_bounds_for_datetime_field(self, value):
413
432
# Again, no microseconds
414
433
first , second = super (DatabaseOperations ,
415
434
self ).year_lookup_bounds_for_datetime_field (value )
416
- if self .connection .get_server_version () >= (5 , 6 , 4 ):
435
+ if self .connection .server_version >= (5 , 6 , 4 ):
417
436
return [first .replace (microsecond = 0 ), second ]
418
437
else :
419
438
return [first .replace (microsecond = 0 ),
@@ -462,10 +481,14 @@ def __init__(self, *args, **kwargs):
462
481
self .server_version = None
463
482
464
483
# Since some features depend on the MySQL version, we need to connect
465
- self ._connect ()
484
+ try :
485
+ # Django 1.6
486
+ self .ensure_connection ()
487
+ except AttributeError :
488
+ self ._connect ()
466
489
467
- self .features = DatabaseFeatures (self )
468
490
self .ops = DatabaseOperations (self )
491
+ self .features = DatabaseFeatures (self )
469
492
self .client = DatabaseClient (self )
470
493
self .creation = DatabaseCreation (self )
471
494
self .introspection = DatabaseIntrospection (self )
@@ -530,7 +553,11 @@ def init_connection_state(self):
530
553
self .connection .cmd_query ("SET SQL_AUTO_IS_NULL = 0" )
531
554
532
555
if 'AUTOCOMMIT' in self .settings_dict :
533
- self .set_autocommit (self .settings_dict ['AUTOCOMMIT' ])
556
+ try :
557
+ # Django 1.6
558
+ self .set_autocommit (self .settings_dict ['AUTOCOMMIT' ])
559
+ except AttributeError :
560
+ self ._set_autocommit (self .settings_dict ['AUTOCOMMIT' ])
534
561
535
562
def create_cursor (self ):
536
563
# Django 1.6
@@ -560,7 +587,14 @@ def get_server_version(self):
560
587
561
588
Returns a tuple
562
589
"""
563
- return self .server_version
590
+ try :
591
+ # Django 1.6
592
+ self .ensure_connection ()
593
+ except AttributeError :
594
+ if not self .connection :
595
+ self ._connect ()
596
+
597
+ return self .connection .get_server_version ()
564
598
565
599
def disable_constraint_checking (self ):
566
600
"""Disables foreign key checks
0 commit comments