@@ -81,7 +81,7 @@ def connect(dbapi_connection, connection_record):
81
81
82
82
# Enable foreign key constraints
83
83
try :
84
- if type (dbapi_connection ) is sqlite3 .Connection : # If back end is sqlite
84
+ if isinstance (dbapi_connection , sqlite3 .Connection ) : # If back end is sqlite
85
85
cursor = dbapi_connection .cursor ()
86
86
cursor .execute ("PRAGMA foreign_keys=ON" )
87
87
cursor .close ()
@@ -350,11 +350,11 @@ def teardown_appcontext(exception):
350
350
351
351
# Coerce decimal.Decimal objects to float objects
352
352
# https://groups.google.com/d/msg/sqlalchemy/0qXMYJvq8SA/oqtvMD9Uw-kJ
353
- if type (row [column ]) is decimal .Decimal :
353
+ if isinstance (row [column ], decimal .Decimal ) :
354
354
row [column ] = float (row [column ])
355
355
356
356
# Coerce memoryview objects (as from PostgreSQL's bytea columns) to bytes
357
- elif type (row [column ]) is memoryview :
357
+ elif isinstance (row [column ], memoryview ) :
358
358
row [column ] = bytes (row [column ])
359
359
360
360
# Rows to be returned
@@ -432,52 +432,52 @@ def __escape(value):
432
432
import sqlalchemy
433
433
434
434
# bool
435
- if type (value ) is bool :
435
+ if isinstance (value , bool ) :
436
436
return sqlparse .sql .Token (
437
437
sqlparse .tokens .Number ,
438
438
sqlalchemy .types .Boolean ().literal_processor (self ._engine .dialect )(value ))
439
439
440
440
# bytes
441
- elif type (value ) is bytes :
441
+ elif isinstance (value , bytes ) :
442
442
if self ._engine .url .get_backend_name () in ["mysql" , "sqlite" ]:
443
443
return sqlparse .sql .Token (sqlparse .tokens .Other , f"x'{ value .hex ()} '" ) # https://dev.mysql.com/doc/refman/8.0/en/hexadecimal-literals.html
444
444
elif self ._engine .url .get_backend_name () == "postgresql" :
445
445
return sqlparse .sql .Token (sqlparse .tokens .Other , f"'\\ x{ value .hex ()} '" ) # https://dba.stackexchange.com/a/203359
446
446
else :
447
447
raise RuntimeError ("unsupported value: {}" .format (value ))
448
448
449
- # datetime.date
450
- elif type (value ) is datetime .date :
449
+ # datetime.datetime
450
+ elif isinstance (value , datetime .datetime ) :
451
451
return sqlparse .sql .Token (
452
452
sqlparse .tokens .String ,
453
- sqlalchemy .types .String ().literal_processor (self ._engine .dialect )(value .strftime ("%Y-%m-%d" )))
453
+ sqlalchemy .types .String ().literal_processor (self ._engine .dialect )(value .strftime ("%Y-%m-%d %H:%M:%S " )))
454
454
455
- # datetime.datetime
456
- elif type (value ) is datetime .datetime :
455
+ # datetime.date
456
+ elif isinstance (value , datetime .date ) :
457
457
return sqlparse .sql .Token (
458
458
sqlparse .tokens .String ,
459
- sqlalchemy .types .String ().literal_processor (self ._engine .dialect )(value .strftime ("%Y-%m-%d %H:%M:%S " )))
459
+ sqlalchemy .types .String ().literal_processor (self ._engine .dialect )(value .strftime ("%Y-%m-%d" )))
460
460
461
461
# datetime.time
462
- elif type (value ) is datetime .time :
462
+ elif isinstance (value , datetime .time ) :
463
463
return sqlparse .sql .Token (
464
464
sqlparse .tokens .String ,
465
465
sqlalchemy .types .String ().literal_processor (self ._engine .dialect )(value .strftime ("%H:%M:%S" )))
466
466
467
467
# float
468
- elif type (value ) is float :
468
+ elif isinstance (value , float ) :
469
469
return sqlparse .sql .Token (
470
470
sqlparse .tokens .Number ,
471
471
sqlalchemy .types .Float ().literal_processor (self ._engine .dialect )(value ))
472
472
473
473
# int
474
- elif type (value ) is int :
474
+ elif isinstance (value , int ) :
475
475
return sqlparse .sql .Token (
476
476
sqlparse .tokens .Number ,
477
477
sqlalchemy .types .Integer ().literal_processor (self ._engine .dialect )(value ))
478
478
479
479
# str
480
- elif type (value ) is str :
480
+ elif isinstance (value , str ) :
481
481
return sqlparse .sql .Token (
482
482
sqlparse .tokens .String ,
483
483
sqlalchemy .types .String ().literal_processor (self ._engine .dialect )(value ))
@@ -493,7 +493,7 @@ def __escape(value):
493
493
raise RuntimeError ("unsupported value: {}" .format (value ))
494
494
495
495
# Escape value(s), separating with commas as needed
496
- if type (value ) in [ list , tuple ] :
496
+ if isinstance (value , ( list , tuple )) :
497
497
return sqlparse .sql .TokenList (sqlparse .parse (", " .join ([str (__escape (v )) for v in value ])))
498
498
else :
499
499
return __escape (value )
0 commit comments