
    h[                        S r SSKrSSKrSSKrSSKJr  SSKJr  SSKJ	r	  SSK
Jr  SSK
Jr  SSKJrJr  SS	KJr  SS
KJr  SSKJr  SSKJr  SSKJr    SSKrS r \ " 5       S:  a  \	" S\RB                   35      eS\ " 5       s=::  a  S:  a  O  O\	" S\RB                   35      eSSK"J#r#J$r$  \$(       a0  SSKJ%r%J&r&  SSK'J(r(  SSK"J)r)J*r*  \%RV                  S   RX                  r-OSSK.rSSK/r\R`                  Rc                  \\R`                  Rd                  5        \Rf                  Ri                  5         Sr5\R`                  Rm                  \54S\R`                  Rn                  5      r8\R`                  Rs                  \85        SSK:J;r;  SSK<J=r=  SSK>J?r?  SS K@JArA  SS!KBJCrC  SS"KDJErE  S# rF " S$ S%\5      rG\$(       am   " S& S'5      rH " S( S)\H\R                  5      rJ " S* S+\H\R                  5      rI " S, S-\H\R                  R                  \R                  5      rO " S. S/\5      rg\R`                  R                  rI " S0 S/\5      rg! \ a    SSKr GNf = f! \ a	    \	" S5      ef = f)1zY
PostgreSQL database backend for Django.

Requires psycopg2 >= 2.8.4 or psycopg >= 3.1.8
    N)contextmanager)settings)ImproperlyConfigured)DatabaseError)connections)NO_DB_ALIASBaseDatabaseWrapperCursorDebugWrapper)async_unsafe)cached_property)
SafeString)get_version_tuplez(Error loading psycopg2 or psycopg modulec                  ^    [         R                  R                  SS5      S   n [        U 5      $ )N    r   )Database__version__splitr   )versions    T/var/www/html/env/lib/python3.13/site-packages/django/db/backends/postgresql/base.pypsycopg_versionr       s*    ""((a03GW%%    )         z6psycopg2 version 2.8.4 or newer is required; you have )   )r   r   r   z5psycopg version 3.1.8 or newer is required; you have r   )IsolationLevelis_psycopg3)adapterssql)Format)get_adapters_templateregister_tzloadertimestamptzi  	INETARRAY)DatabaseClient)DatabaseCreation)DatabaseFeatures)DatabaseIntrospection)DatabaseOperations)DatabaseSchemaEditorc                     U S   c  gSU -  $ )N
max_lengthvarcharvarchar(%(max_length)s) )datas    r   _get_varchar_columnr3   S   s    L!$t++r   c                     ^  \ rS rSrSrSr0 SS_SS_SS	_S
S_S\_SS_SS_SS_SS_SS_SS_SS_SS_SS_SS_SS_SS _SSSS!SS!S!S"S#S$S%.
ErS&S&S&S'.rS(S(S(S).r	S*S+S,S-S.S/S0S1S2S3S,S,S-S-S4.r
S5rS6S7S8S9S:S;S<.r\r\r\r\r\r\r\rS=r0 r\S> 5       rS? rS@ rSA r \!SB 5       r"SC r#SD r$SE r%SF r&SG r'U 4SH jr(\!STSI j5       r)SJ r*\!SK 5       r+SL r,STSM jr-SN r.U 4SO jr/\0U 4SP j5       r1\2SQ 5       r3SR r4SSr5U =r6$ )UDatabaseWrapperY   
postgresql
PostgreSQL	AutoFieldintegerBigAutoFieldbigintBinaryFieldbyteaBooleanFieldboolean	CharField	DateFielddateDateTimeFieldztimestamp with time zoneDecimalFieldz+numeric(%(max_digits)s, %(decimal_places)s)DurationFieldinterval	FileFieldr0   FilePathField
FloatFieldzdouble precisionIntegerFieldBigIntegerFieldIPAddressFieldinetGenericIPAddressField	JSONFieldjsonbsmallinttexttimeuuid)
OneToOneFieldPositiveBigIntegerFieldPositiveIntegerFieldPositiveSmallIntegerField	SlugFieldSmallAutoFieldSmallIntegerField	TextField	TimeField	UUIDFieldz"%(column)s" >= 0)rW   rX   rY   z GENERATED BY DEFAULT AS IDENTITY)r9   r;   r[   z= %sz= UPPER(%s)zLIKE %szLIKE UPPER(%s)z~ %sz~* %sz> %sz>= %sz< %sz<= %s)exactiexactcontains	icontainsregexiregexgtgteltlte
startswithendswithistartswith	iendswithzKREPLACE(REPLACE(REPLACE({}, E'\\', E'\\\\'), E'%%', E'\\%%'), E'_', E'\\_')zLIKE '%%' || {} || '%%'zLIKE '%%' || UPPER({}) || '%%'zLIKE {} || '%%'zLIKE UPPER({}) || '%%'zLIKE '%%' || {}zLIKE '%%' || UPPER({}))rb   rc   rj   rl   rk   rm   r   c                 r   U R                   S   R                  S5      nU R                  [        :X  d  U(       d  g U R                  U R                  ;  a  U R                   R                  SS5      S:w  a  [        S5      eUSL a  0 n SSKJn  U R                  5       nSUS	'   U R                   S
   nU" SUSU R                  U(       a  UR                  OS S.UD6nU R                  R                  U R                  U5        U R                  U R                     $ ! [         a  n[        S5      UeS nAff = f)NOPTIONSpoolCONN_MAX_AGEr   z/Pooling doesn't support persistent connections.T)ConnectionPoolzAError loading psycopg_pool module.
Did you install psycopg[pool]?
autocommitCONN_HEALTH_CHECKSF)kwargsopen	configurecheckr1   )settings_dictgetaliasr   _connection_poolsr   psycopg_poolrr   ImportErrorget_connection_params_configure_connectioncheck_connection
setdefault)selfpool_optionsrr   errconnect_kwargsenable_checksrp   s          r   rp   DatabaseWrapper.pool   s8   )))488@::$L::T333!!%%na8A=*E  t#!7 "779N+/N<( ../CDM! %449Fn55D	
 D ""--djj$?%%djj11-  *Xs   D 
D6%D11D6c                     U R                   (       a2  U R                   R                  5         U R                  U R                  	 g g N)rp   closer|   r{   r   s    r   
close_poolDatabaseWrapper.close_pool   s.    99IIOO&&tzz2 r   c                 .    [        U R                  S5      $ )zW
Return a tuple of the database's version.
E.g. for pg_version 120004, return (12, 4).
i'  )divmod
pg_versionr   s    r   get_database_version$DatabaseWrapper.get_database_version   s    
 doou--r   c                    U R                   nUS   S:X  a$  US   R                  S5      (       d  [        S5      e[        US   =(       d    S5      U R                  R                  5       :  a8  [        SUS   [        US   5      U R                  R                  5       4-  5      eUS   (       a  SUS   0US   EnO,US   c  US   R                  SS 5        SS0US   EnO0 US   EnS	US
'   UR                  SS 5        UR                  SS 5        UR                  SS 5      nU(       a  [        (       d  [        S5      eUR                  SS 5      nUR                  S[        (       a  USL a  [        O[        5        US   (       a  US   US'   US   (       a  US   US'   US   (       a  US   US'   US   (       a  US   US'   [        (       a<  [        [        R                  U R                  5      US'   UR                  SS 5      US'   U$ )NNAME ro   servicez`settings.DATABASES is improperly configured. Please supply the NAME or OPTIONS['service'] value.zThe database name '%s' (%d characters) is longer than PostgreSQL's limit of %d characters. Supply a shorter NAME in settings.DATABASES.dbnamepostgresUTF8client_encodingassume_roleisolation_levelrp   z&Database pooling requires psycopg >= 3server_side_bindingcursor_factoryTUSERuserPASSWORDpasswordHOSThostPORTportcontextprepare_threshold)ry   rz   r   lenopsmax_name_lengthpopr   r   ServerBindingCursorCursorr#   r   USE_TZtimezone)r   ry   conn_paramsr   r   s        r   r   %DatabaseWrapper.get_connection_params   s?   ** B&}Y/G/K/KI/V/V&F  }V$*+dhh.F.F.HH&) "&)f-.HH,,.	 	  -/	*K 6"*)$((D9#ZL=3KLK6]956K)/%&t,)40"vt4&'OPP)oo.CTJ ;#6$#> $	
  "/"7K$&3J&?K
# "/"7K "/"7K;%:&K	"
 0;#T0K+, r   c                 &   U R                   S   nSn US   n [        U5      U l        SnU R                  (       a5  U R                  R                  5         U R                  R                  5       nOU R                  R                  " S	0 UD6nU(       a  U R                  Ul        [        (       d  [        R                  R                  US S9  U$ ! [         a    [	        SU S35      ef = f! [
         a    [        R                  U l         Nf = f)
Nro   Fr   Tz$Invalid transaction isolation level z9 specified. Use one of the psycopg.IsolationLevel values.c                     U $ r   r1   )xs    r   <lambda>4DatabaseWrapper.get_new_connection.<locals>.<lambda>T  s    r   )conn_or_cursloadsr1   )ry   r   r   
ValueErrorr   KeyErrorREAD_COMMITTEDrp   rv   getconnr   connectr   psycopg2extrasregister_default_jsonb)r   r   optionsset_isolation_levelisolation_level_value
connections         r   get_new_connection"DatabaseWrapper.get_new_connection0  s    $$Y/#	$+,=$>!
'56K'L$&*# 99IINN**,J..==J)-)=)=J&{ OO22'{ 3  )  *:;P:Q RO P   	A#1#@#@D 	As   C. C C+.DDc                 t    U R                  5         U R                  c  gU R                  U R                  5      $ )NF)r   r   _configure_timezoner   s    r   ensure_timezoneDatabaseWrapper.ensure_timezoneX  s.    ??"''88r   c                    UR                   R                  S5      nU R                  nU(       aJ  X#:w  aE  UR                  5        nUR	                  U R
                  R                  5       U/5        S S S 5        gg! , (       d  f       g= f)NTimeZoneTF)infoparameter_statustimezone_namecursorexecuter   set_time_zone_sql)r   r   conn_timezone_namer   r   s        r   r   #DatabaseWrapper._configure_timezone_  sm    '__==jI**/@""$txx99;m_M % %$s   ,A::
Bc                     U R                   S   R                  S5      =n(       aH  UR                  5        nU R                  R	                  SU/5      nUR                  U5        S S S 5        gg! , (       d  f       g= f)Nro   r   zSET ROLE %sTF)ry   rz   r   r   compose_sqlr   )r   r   new_roler   r!   s        r   _configure_roleDatabaseWrapper._configure_roleh  sp    )))488GG8G""$hh**=8*Es# % 	 %$ s   /A//
A=c                 \    U R                  U5      nU R                  U5      nU=(       d    U$ r   )r   r   )r   r   	commit_tzcommit_roles       r   r   %DatabaseWrapper._configure_connectionp  s1    
 ,,Z8	 **:6'i'r   c                 D   U R                   b  U R                     U R                  (       a7  U R                   R                  R	                  U R                   5        S U l         O#U R                   R                  5       sS S S 5        $  S S S 5        g g ! , (       d  f       g = fr   )r   wrap_database_errorsrp   _poolputconnr   r   s    r   _closeDatabaseWrapper._close}  sv    ??& **99 OO))11$//B&*DO??002 +* $ +*	 ' +*s   A"B
Bc                   > [         TU ]  5         U R                  bf  U R                  (       dT  U R	                  U R                  5      nU(       a1  U R                  5       (       d  U R                  R                  5         g g g g g r   )superinit_connection_stater   rp   r   get_autocommitcommit)r   r   	__class__s     r   r   %DatabaseWrapper.init_connection_state  sb    %'??&tyy//@Fd1133&&( 4v 09&r   c                    U(       a  [         (       aL  U R                  S   R                  S5      SLa+  [        U R                  USU R                  R
                  S9nOJU R                  R                  USU R                  R
                  S9nOU R                  R                  5       n[         (       aj  U R                  R                  R                  [        [        R                  5      nU R                  UR                  :w  a  [        U R                  U5        U$ [        R                  (       a  U R                   OS Ul        U$ )Nro   r   TF)name
scrollablewithhold)r   r   )r   ry   rz   ServerSideCursorr   rs   r   r    
get_loaderTIMESTAMPTZ_OIDr"   TEXTr   r$   r   r   tzinfo_factory)r   r   r   tzloaders       r   create_cursorDatabaseWrapper.create_cursor  s    {""9-112GHPTT *OO$!__77	 //UT__5O5O 0  __++-F; //::?FKKXH}} 1 11!$--8  <D??D$7$7PTF!r   c                     U R                   $ r   )r   )r   offsets     r   r   DatabaseWrapper.tzinfo_factory  s    }}r   c                 0   U =R                   S-  sl          [        R                  " 5       nU(       a  [	        [        U5      5      nOSnU R                  S[        R                  " 5       R                  UU R                   4-  S9$ ! [         a    S n Nhf = f)Nr   syncz_django_curs_%d_%s_%d)r   )
_named_cursor_idxasynciocurrent_taskRuntimeErrorstrid_cursor	threadingcurrent_threadident)r   r   
task_idents      r   chunked_cursorDatabaseWrapper.chunked_cursor  s    !#	 "//1L R-.JJ||( ((*00&&	  
 	
  	 L	 s   B BBc                 p    U R                      XR                  l        S S S 5        g ! , (       d  f       g = fr   )r   r   rs   )r   rs   s     r   _set_autocommitDatabaseWrapper._set_autocommit  s!    &&)3OO& '&&s   '
5c                     U R                  5        nUR                  S5        UR                  S5        SSS5        g! , (       d  f       g= f)zT
Check constraints by setting them to immediate. Return them to deferred
afterward.
zSET CONSTRAINTS ALL IMMEDIATEzSET CONSTRAINTS ALL DEFERREDN)r   r   )r   table_namesr   s      r   check_constraints!DatabaseWrapper.check_constraints  s3    
 [[]fNN:;NN9: ]]s	   #=
Ac                     U R                   c  g U R                   R                  5        nUR                  S5        S S S 5        g! , (       d  f       g= f! [        R                   a     gf = f)NFzSELECT 1T)r   r   r   r   Errorr   r   s     r   	is_usableDatabaseWrapper.is_usable  sa    ??"	'')Vz* *
  *)
  ~~ 		s-   A AA 
AA A A-,A-c                 D   > U R                   (       a  g [        TU ]	  5       $ r   )rp   r   close_if_health_check_failed)r   r   s    r   r  ,DatabaseWrapper.close_if_health_check_failed  s    99w355r   c              #     >#    S n [         TU ]  5        nUv   S S S 5        g ! , (       d  f       g = f! [        R                  [        4 a    Ub  e [
        R                  " S[        5        [        R                  " 5        H  nUR                  S:X  d  M  UR                  S   S:w  d  M*  U R                  0 U R                  ESUR                  S   0EU R                  S9n UR                  5        nUv   S S S 5        O! , (       d  f       O= fUR                  5         O! UR                  5         f = f   g    e f = f7f)Na8  Normally 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.r7   r   r   )r{   )r   _nodb_cursorr   r   WrappedDatabaseErrorwarningswarnRuntimeWarningr   allvendorry   r   r{   r   r   )r   r   r   connr   s       r   r  DatabaseWrapper._nodb_cursor  s    !	%'6 (''&&(<= 	!MMF
  *oo/
%%5"008JF>>"00"J$<$<V$D #jj * D%![[]f"(L +]] 



# 0& =	s{   E4 #4 E
14 E4 AE E ,7E $D$4D9	D$
DD$E $D66E :E<E  Ec                     U R                  5          U R                  R                  R                  sS S S 5        $ ! , (       d  f       g = fr   )temporary_connectionr   r   server_versionr   s    r   r   DatabaseWrapper.pg_version  s-    &&(??''66 )((s	    ;
A	c                     [        X5      $ r   r
   r  s     r   make_debug_cursor!DatabaseWrapper.make_debug_cursor"  s    !&//r   )r   r   r   )7__name__
__module____qualname____firstlineno__r!  display_namer3   
data_typesdata_type_check_constraintsdata_types_suffix	operatorspattern_escpattern_opsr   r,   SchemaEditorClassr'   client_classr(   creation_classr)   features_classr*   introspection_classr+   	ops_classr   r|   propertyrp   r   r   r   r   r   r   r   r   r   r   r   r   r   r	  r  r  r  r  r   r  r   r   r)  __static_attributes____classcell__)r   s   @r   r5   r5   Y   s   FL
Y 	w 			
 	( 	V 	3 	E 	 	. 	2 	( 		 	8 	&  	 !" 	W#$ ##+ )%/.$'7J< $7 3%8# 8:< %'%I4 	W  .5'/%-K H,!L%N%N/"I&2 &2P3
.AF % %N9(3 )    D 
 
64;
6 # #J 7 70 0r   r5   c                   "    \ rS rSrSrSS jrSrg)CursorMixini(  z5
A subclass of psycopg cursor implementing callproc.
Nc                    [        U[        R                  5      (       d  [        R                  " U5      n[        R                  " S5      U[        R                  " S5      /nU(       aV  U HM  nUR	                  [        R
                  " U5      5        UR	                  [        R                  " S5      5        MO     US	 UR	                  [        R                  " S5      5        [        R                  " U5      nU R                  U5        U$ )NzSELECT * FROM (,))
isinstancer!   
IdentifierSQLappendLiteralComposedr   )r   r   argsqpartsitemstmts         r   callprocCursorMixin.callproc-  s    dCNN33~~d+gg./swws|DF DMM#++d"34MM#''#,/ ! 2JMM#''#,'<<'DLLKr   r1   r   )r+  r,  r-  r.  __doc__rP  r=  r1   r   r   r@  r@  (  s    		r   r@  c                       \ rS rSrSrg)r   i=  r1   Nr+  r,  r-  r.  r=  r1   r   r   r   r   =      r   r   c                       \ rS rSrSrg)r   i@  r1   NrT  r1   r   r   r   r   @  rU  r   r   c                       \ rS rSrSrSrg)r   iC  a  
psycopg >= 3 forces the usage of server-side bindings when using named
cursors but the ORM doesn't yet support the systematic generation of
prepareable SQL (#20516).

ClientCursorMixin forces the usage of client-side bindings while
ServerCursor implements the logic required to declare and scroll
through named cursors.

Mixing ClientCursorMixin in wouldn't be necessary if Cursor allowed to
specify how parameters should be bound instead, which ServerCursor
would inherit, but that's not the case.
r1   N)r+  r,  r-  r.  rR  r=  r1   r   r   r   r   C  s    	r   r   c                       \ rS rSrS rSrg)r   iT  c                     U R                  U5         U R                  R                  U5      sS S S 5        $ ! , (       d  f       g = fr   )	debug_sqlr   copy)r   	statements     r   r[  CursorDebugWrapper.copyU  s,    	*{{''	2 +**s	   7
Ar1   N)r+  r,  r-  r.  r[  r=  r1   r   r   r   r   T  s    	3r   r   c                        \ rS rSrS rS rSrg)r   i\  c                     U R                  U5         U R                  R                  " X/UQ76 sS S S 5        $ ! , (       d  f       g = fr   )rZ  r   copy_expert)r   r!   filerL  s       r   r`  CursorDebugWrapper.copy_expert]  s1    ${{..s@4@ %$$s	   9
Ac                     U R                  SU-  S9   U R                  R                  " X/UQ70 UD6sS S S 5        $ ! , (       d  f       g = f)NzCOPY %s TO STDOUT)r!   )rZ  r   copy_to)r   ra  tablerL  ru   s        r   rd  CursorDebugWrapper.copy_toa  s?    $7%$?@{{**4HHH A@@s	    =
Ar1   N)r+  r,  r-  r.  r`  rd  r=  r1   r   r   r   r   \  s    	A	Ir   )QrR  r   r  r  
contextlibr   django.confr   django.core.exceptionsr   	django.dbr   r  r   django.db.backends.base.baser   r	   django.db.backends.utilsr   BaseCursorDebugWrapperdjango.utils.asyncior   django.utils.functionalr   django.utils.safestringr   django.utils.versionr   psycopgr   r~   r   r   r   psycopg_anyr   r   r    r!   
psycopg.pqr"   r#   r$   typesoidr   psycopg2.extensionspsycopg2.extras
extensionsregister_adapterQuotedStringr   register_uuidINETARRAY_OIDnew_array_typeUNICODEr&   register_typeclientr'   creationr(   featuresr)   introspectionr*   
operationsr+   schemar,   r3   r5   r@  r   r   ClientCursorclient_cursorClientCursorMixinServerCursorr   r   r1   r   r   <module>r     s/      %   7 ; ! I Q - 3 . 2K$"&
 y 

@AUAU@VW  ?(y(

?@T@T?UV 
 5%!Enn]377O ((X5H5H5U5UVOO!!# M##22	##I
 %%i0 # & & 0 * (,J0) J0Z  *k8?? h33 X++==x?T?T"33 3   ''FI3 IE  $#$ K
I
JJKs$   I 
II II I.