o 3a3 @sdZddlZddlZddlZddlmZddlmZddlm Z ddl m Z m Z ddlmZddlmZdd lmZdd lmZdd lmZdd lmZzddlZddlZddlZWneylZ ze d e dZ [ wwddZ!e!Z"e"dkre dej#ddl$m%Z%ddl&m'Z'ddl(m)Z)ddl*m+Z+ddl,m-Z-ddl.m/Z/ej01eej0j2ej34dZ5ej06e5fdej0j7Z8ej09e8GdddeZ:GdddeZdS)zW PostgreSQL database backend for Django. Requires psycopg 2: https://www.psycopg.org/ N)contextmanager)settings)ImproperlyConfigured) DatabaseError connections)BaseDatabaseWrapperCursorDebugWrapper) async_unsafe)cached_property) SafeString)get_version_tuplez!Error loading psycopg2 module: %scCstjddd}t|S)N r)psycopg2 __version__splitr )versionrD/usr/lib/python3/dist-packages/django/db/backends/postgresql/base.pypsycopg2_version sr)z8psycopg2_version 2.5.4 or newer is required; you have %sr)DatabaseClient)DatabaseCreation)DatabaseFeatures)DatabaseIntrospection)DatabaseOperations)DatabaseSchemaEditori INETARRAYcseZdZdZdZiddddddd d d d d ddddddddd dd ddddddddddd d!d dddd"d d#d"d$d%d&d' Zd(d(d(d)Zd*d+d,d-d.d/d0d1d2d3d,d,d-d-d4Zd5Zd6d7d8d9d:d;d<Z e Z e Z e ZeZeZeZeZd=Zd>d?Zed@dAZdBdCZdDdEZedYdGdHZdIdJZedKdLZdMdNZ dYdOdPZ!dQdRZ"e#fdSdTZ$e%dUdVZ&dWdXZ'Z(S)ZDatabaseWrapper postgresql PostgreSQL AutoFieldserial BigAutoField bigserial BinaryFieldbytea BooleanFieldboolean CharFieldzvarchar(%(max_length)s) DateFielddate DateTimeFieldztimestamp with time zone DecimalFieldz+numeric(%(max_digits)s, %(decimal_places)s) DurationFieldinterval FileField FilePathField FloatFieldzdouble precision IntegerFieldintegerBigIntegerFieldbigintIPAddressFieldinetGenericIPAddressField JSONFieldjsonbsmallint smallserialtexttimeuuid) NullBooleanField OneToOneFieldPositiveBigIntegerFieldPositiveIntegerFieldPositiveSmallIntegerField SlugFieldSmallAutoFieldSmallIntegerField TextField TimeField UUIDFieldz"%(column)s" >= 0)rFrGrHz= %sz = UPPER(%s)zLIKE %szLIKE UPPER(%s)z~ %sz~* %sz> %sz>= %sz< %sz<= %s)exactiexactcontains icontainsregexiregexgtgteltlte startswithendswith istartswith iendswithzKREPLACE(REPLACE(REPLACE({}, E'\\', E'\\\\'), E'%%', E'\\%%'), E'_', E'\\_')zLIKE '%%' || {} || '%%'zLIKE '%%' || UPPER({}) || '%%'zLIKE {} || '%%'zLIKE UPPER({}) || '%%'zLIKE '%%' || {}zLIKE '%%' || UPPER({}))rQrRrYr[rZr\rcCs|j}|ddkr tdt|dpd|jkr,td|dt|d|jfd|dp2di|d}|dd|d rH|d |d <|d rR|d |d <|d r\|d |d<|drf|d|d<|S)NNAMEzJsettings.DATABASES is improperly configured. Please supply the NAME value.zThe database name '%s' (%d characters) is longer than PostgreSQL's limit of %d characters. Supply a shorter NAME in settings.DATABASES.databasepostgresOPTIONSisolation_levelUSERuserPASSWORDpasswordHOSThostPORTport) settings_dictrlenopsmax_name_lengthpop)selfrk conn_paramsrrrget_connection_paramss8        z%DatabaseWrapper.get_connection_paramscCsvtjdi|}|jd}z|d|_Wn ty!|j|_Ynw|j|jkr/|j|jdtjj|ddd|S)Nrarb)rbcSs|SNr)xrrrsz4DatabaseWrapper.get_new_connection..) conn_or_cursloadsr) DatabaseconnectrkrbKeyError set_sessionrextrasregister_default_jsonb)rprq connectionoptionsrrrget_new_connections    z"DatabaseWrapper.get_new_connectioncCst|jdurdS|jd}|j}|r8||kr8|j}||j|gWddS1s1wYdSdS)NFTimeZoneT)r~get_parameter_status timezone_namecursorexecutermset_time_zone_sql)rpconn_timezone_namerrrrrensure_timezones     zDatabaseWrapper.ensure_timezonecCs6|jd|}|r|s|jdSdSdS)NUTF8)r~set_client_encodingrget_autocommitcommit)rptimezone_changedrrrinit_connection_states z%DatabaseWrapper.init_connection_stateNcCsB|r|jj|d|jjd}n|j}tjr|j|_|Sd|_|S)NF) scrollablewithhold)r~r autocommitrUSE_TZtzinfo_factory)rpnamerrrr create_cursors zDatabaseWrapper.create_cursorcCs|jSrs)timezone)rpoffsetrrrrszDatabaseWrapper.tzinfo_factorycCs||jd7_zttdrt}ntj}Wn ty#d}Ynw|r-tt|}nd}|jdt j ||jfdS)Nr current_tasksyncz_django_curs_%d_%s_%d)r) _named_cursor_idxhasattrasynciorTask RuntimeErrorstrid_cursor threadingcurrent_threadident)rpr task_identrrrchunked_cursors&    zDatabaseWrapper.chunked_cursorcCs4|j ||j_WddS1swYdSrs)wrap_database_errorsr~r)rprrrr_set_autocommits "zDatabaseWrapper._set_autocommitcCsB|}|d|dWddS1swYdS)zl Check constraints by setting them to immediate. Return them to deferred afterward. zSET CONSTRAINTS ALL IMMEDIATEzSET CONSTRAINTS ALL DEFERREDN)rr)rp table_namesrrrrcheck_constraintss   "z!DatabaseWrapper.check_constraintscCsVz|j}|dWdWdS1swYWdStjy*YdSw)NzSELECT 1FT)r~rrrxErrorrprrrr is_usable s   zDatabaseWrapper.is_usablec #szt }|VWdWdS1swYWdStjtfy{tdtt D]G}|j dkry|j ddkry|j i|j d|j di|j d}z| }|VWdn1sewYW|n|wYdSq2w)Na8Normally Django will use a connection to the 'postgres' database to avoid running initialization queries against the production database when it's not needed (for example, when running tests). Django was unable to create a connection to the 'postgres' database and will use the first PostgreSQL database instead.r"r]r`)alias)super _nodb_cursorrxrWrappedDatabaseErrorwarningswarnRuntimeWarningrallvendorrk __class__rrclose)rprr~connrrrr*s4 &  zDatabaseWrapper._nodb_cursorcCs4| |jjWdS1swYdSrs)temporary_connectionr~server_version)rprrr pg_versionGs $zDatabaseWrapper.pg_versioncCs t||Srsrrrrrmake_debug_cursorLs z!DatabaseWrapper.make_debug_cursorrs))__name__ __module__ __qualname__r display_name data_typesdata_type_check_constraints operators pattern_esc pattern_opsrxrSchemaEditorClassr client_classrcreation_classrfeatures_classrintrospection_classr ops_classrrrr rrrrrrrrrrrr rr __classcell__rrrrr!As               r!c@seZdZddZddZdS)r cGsD|||jj||g|RWdS1swYdSrs) debug_sqlr copy_expert)rpsqlfileargsrrrrQs $zCursorDebugWrapper.copy_expertcOsP|jd|d|jj||g|Ri|WdS1s!wYdS)NzCOPY %s TO STDOUT)r)rrcopy_to)rprtablerkwargsrrrrUs$zCursorDebugWrapper.copy_toN)rrrrrrrrrr Ps r );__doc__rrr contextlibr django.confrdjango.core.exceptionsr django.dbrrrdjango.db.backends.base.baserdjango.db.backends.utilsr BaseCursorDebugWrapperdjango.utils.asyncior django.utils.functionalr django.utils.safestringr django.utils.versionr rrxpsycopg2.extensionspsycopg2.extras ImportErrorerPSYCOPG2_VERSIONrclientrcreationrfeaturesr introspectionr operationsrschemar extensionsregister_adapter QuotedStringr| register_uuid INETARRAY_OIDnew_array_typeUNICODEr register_typer!rrrrsZ