<s> """<STR_LIT>""" <EOL> import logging <EOL> import sys <EOL> from django . db import utils <EOL> from django . db . backends import * <EOL> from django . db . backends . signals import connection_created <EOL> from django . db . backends . postgresql_psycopg2 . operations import DatabaseOperations <EOL> from django . db . backends . postgresql_psycopg2 . client import DatabaseClient <EOL> from django . db . backends . postgresql_psycopg2 . creation import DatabaseCreation <EOL> from django . db . backends . postgresql_psycopg2 . version import get_version <EOL> from django . db . backends . postgresql_psycopg2 . introspection import DatabaseIntrospection <EOL> from django . utils . encoding import force_str <EOL> from django . utils . safestring import SafeText , SafeBytes <EOL> from django . utils import six <EOL> from django . utils . timezone import utc <EOL> try : <EOL> import psycopg2 as Database <EOL> import psycopg2 . extensions <EOL> except ImportError as e : <EOL> from django . core . exceptions import ImproperlyConfigured <EOL> raise ImproperlyConfigured ( "<STR_LIT>" % e ) <EOL> DatabaseError = Database . DatabaseError <EOL> IntegrityError = Database . IntegrityError <EOL> psycopg2 . extensions . register_type ( psycopg2 . extensions . UNICODE ) <EOL> psycopg2 . extensions . register_adapter ( SafeBytes , psycopg2 . extensions . QuotedString ) <EOL> psycopg2 . extensions . register_adapter ( SafeText , psycopg2 . extensions . QuotedString ) <EOL> logger = logging . getLogger ( '<STR_LIT>' ) <EOL> def utc_tzinfo_factory ( offset ) : <EOL> if offset != <NUM_LIT:0> : <EOL> raise AssertionError ( "<STR_LIT>" ) <EOL> return utc <EOL> class CursorWrapper ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , cursor ) : <EOL> self . cursor = cursor <EOL> def execute ( self , query , args = None ) : <EOL> try : <EOL> return self . cursor . execute ( query , args ) <EOL> except Database . IntegrityError as e : <EOL> six . reraise ( utils . IntegrityError , utils . IntegrityError ( * tuple ( e . args ) ) , sys . exc_info ( ) [ <NUM_LIT:2> ] ) <EOL> except Database . DatabaseError as e : <EOL> six . reraise ( utils . DatabaseError , utils . DatabaseError ( * tuple ( e . args ) ) , sys . exc_info ( ) [ <NUM_LIT:2> ] ) <EOL> def executemany ( self , query , args ) : <EOL> try : <EOL> return self . cursor . executemany ( query , args ) <EOL> except Database . IntegrityError as e : <EOL> six . reraise ( utils . IntegrityError , utils . IntegrityError ( * tuple ( e . args ) ) , sys . exc_info ( ) [ <NUM_LIT:2> ] ) <EOL> except Database . DatabaseError as e : <EOL> six . reraise ( utils . DatabaseError , utils . DatabaseError ( * tuple ( e . args ) ) , sys . exc_info ( ) [ <NUM_LIT:2> ] ) <EOL> def __getattr__ ( self , attr ) : <EOL> if attr in self . __dict__ : <EOL> return self . __dict__ [ attr ] <EOL> else : <EOL> return getattr ( self . cursor , attr ) <EOL> def __iter__ ( self ) : <EOL> return iter ( self . cursor ) <EOL> class DatabaseFeatures ( BaseDatabaseFeatures ) : <EOL> needs_datetime_string_cast = False <EOL> can_return_id_from_insert = True <EOL> requires_rollback_on_dirty_transaction = True <EOL> has_real_datatype = True <EOL> can_defer_constraint_checks = True <EOL> has_select_for_update = True <EOL> has_select_for_update_nowait = True <EOL> has_bulk_insert = True <EOL> supports_tablespaces = True <EOL> supports_transactions = True <EOL> can_distinct_on_fields = True <EOL> class DatabaseWrapper ( BaseDatabaseWrapper ) : <EOL> vendor = '<STR_LIT>' <EOL> operators = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> super ( DatabaseWrapper , self ) . __init__ ( * args , ** kwargs ) <EOL> self . features = DatabaseFeatures ( self ) <EOL> autocommit = self . settings_dict [ "<STR_LIT>" ] . get ( '<STR_LIT>' , False ) <EOL> self . features . uses_autocommit = autocommit <EOL> if autocommit : <EOL> level = psycopg2 . extensions . ISOLATION_LEVEL_AUTOCOMMIT <EOL> else : <EOL> level = psycopg2 . extensions . ISOLATION_LEVEL_READ_COMMITTED <EOL> self . _set_isolation_level ( level ) <EOL> self . ops = DatabaseOperations ( self ) <EOL> self . client = DatabaseClient ( self ) <EOL> self . creation = DatabaseCreation ( self ) <EOL> self . introspection = DatabaseIntrospection ( self ) <EOL> self . validation = BaseDatabaseValidation ( self ) <EOL> self . _pg_version = None <EOL> def check_constraints ( self , table_names = None ) : <EOL> """<STR_LIT>""" <EOL> self . cursor ( ) . execute ( '<STR_LIT>' ) <EOL> self . cursor ( ) . execute ( '<STR_LIT>' ) <EOL> def close ( self ) : <EOL> self . validate_thread_sharing ( ) <EOL> if self . connection is None : <EOL> return <EOL> try : <EOL> self . connection . close ( ) <EOL> self . connection = None <EOL> except Database . Error : <EOL> self . connection = None <EOL> logger . warning ( '<STR_LIT>' , <EOL> exc_info = sys . exc_info ( ) <EOL> ) <EOL> raise <EOL> def _get_pg_version ( self ) : <EOL> if self . _pg_version is None : <EOL> self . _pg_version = get_version ( self . connection ) <EOL> return self . _pg_version <EOL> pg_version = property ( _get_pg_version ) <EOL> def _cursor ( self ) : <EOL> settings_dict = self . settings_dict <EOL> if self . connection is None : <EOL> if not settings_dict [ '<STR_LIT>' ] : <EOL> from django . core . exceptions import ImproperlyConfigured <EOL> raise ImproperlyConfigured ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> conn_params = { <EOL> '<STR_LIT>' : settings_dict [ '<STR_LIT>' ] , <EOL> } <EOL> conn_params . update ( settings_dict [ '<STR_LIT>' ] ) <EOL> if '<STR_LIT>' in conn_params : <EOL> del conn_params [ '<STR_LIT>' ] <EOL> if settings_dict [ '<STR_LIT>' ] : <EOL> conn_params [ '<STR_LIT:user>' ] = settings_dict [ '<STR_LIT>' ] <EOL> if settings_dict [ '<STR_LIT>' ] : <EOL> conn_params [ '<STR_LIT:password>' ] = force_str ( settings_dict [ '<STR_LIT>' ] ) <EOL> if settings_dict [ '<STR_LIT>' ] : <EOL> conn_params [ '<STR_LIT:host>' ] = settings_dict [ '<STR_LIT>' ] <EOL> if settings_dict [ '<STR_LIT>' ] : <EOL> conn_params [ '<STR_LIT:port>' ] = settings_dict [ '<STR_LIT>' ] <EOL> self . connection = Database . connect ( ** conn_params ) <EOL> self . connection . set_client_encoding ( '<STR_LIT>' ) <EOL> tz = '<STR_LIT>' if settings . USE_TZ else settings_dict . get ( '<STR_LIT>' ) <EOL> if tz : <EOL> try : <EOL> get_parameter_status = self . connection . get_parameter_status <EOL> except AttributeError : <EOL> conn_tz = None <EOL> else : <EOL> conn_tz = get_parameter_status ( '<STR_LIT>' ) <EOL> if conn_tz != tz : <EOL> self . connection . set_isolation_level ( <EOL> psycopg2 . extensions . ISOLATION_LEVEL_AUTOCOMMIT ) <EOL> self . connection . cursor ( ) . execute ( <EOL> self . ops . set_time_zone_sql ( ) , [ tz ] ) <EOL> self . connection . set_isolation_level ( self . isolation_level ) <EOL> self . _get_pg_version ( ) <EOL> connection_created . send ( sender = self . __class__ , connection = self ) <EOL> cursor = self . connection . cursor ( ) <EOL> cursor . tzinfo_factory = utc_tzinfo_factory if settings . USE_TZ else None <EOL> return CursorWrapper ( cursor ) <EOL> def _enter_transaction_management ( self , managed ) : <EOL> """<STR_LIT>""" <EOL> if self . features . uses_autocommit and managed and not self . isolation_level : <EOL> self . _set_isolation_level ( psycopg2 . extensions . ISOLATION_LEVEL_READ_COMMITTED ) <EOL> def _leave_transaction_management ( self , managed ) : <EOL> """<STR_LIT>""" <EOL> if self . features . uses_autocommit and not managed and self . isolation_level : <EOL> self . _set_isolation_level ( psycopg2 . extensions . ISOLATION_LEVEL_AUTOCOMMIT ) <EOL> def _set_isolation_level ( self , level ) : <EOL> """<STR_LIT>""" <EOL> assert level in range ( <NUM_LIT:5> ) <EOL> try : <EOL> if self . connection is not None : <EOL> self . connection . set_isolation_level ( level ) <EOL> finally : <EOL> self . isolation_level = level <EOL> self . features . uses_savepoints = bool ( level ) <EOL> def _commit ( self ) : <EOL> if self . connection is not None : <EOL> try : <EOL> return self . connection . commit ( ) <EOL> except Database . IntegrityError as e : <EOL> six . reraise ( utils . IntegrityError , utils . IntegrityError ( * tuple ( e . args ) ) , sys . exc_info ( ) [ <NUM_LIT:2> ] ) </s>
