o
    ×ŸŽi3G  ã                   @   sœ  d Z ddlZddlZddlZddlmZ ddlmZ ddlm	Z	 ddl
mZ ddl
mZ ddlmZ dd	lmZ dd
lmZ ddlmZ ddlmZ ddlmZ zzddlZW n eyd   ddlZY nw W n eyq   e	dƒ‚w dd„ Zeƒ dk rƒe	dej › �ƒ‚deƒ   krŽdk r˜n ne	dej › �ƒ‚ddl!m"Z"m#Z# e#r¿ddlm$Z$m%Z% ddl&m'Z' ddl!m(Z(m)Z) e$j*d j+Z,n)ddl-Zddl.Zej/ 0eej/j1¡ ej2 3¡  dZ4ej/ 5e4fdej/j6¡Z7ej/ 8e7¡ ddl9m:Z: ddl;m<Z< dd l=m>Z> dd!l?m@Z@ dd"lAmBZB dd#lCmDZD d$d%„ ZEG d&d'„ d'eƒZFe#�r@G d(d)„ d)ƒZGG d*d+„ d+eGejHƒZIG d,d-„ d-eGejJƒZHG d.d/„ d/eƒZdS ej/jKZHG d0d/„ d/eƒZdS )1zY
PostgreSQL database backend for Django.

Requires psycopg2 >= 2.8.4 or psycopg >= 3.1.8
é    N)Úcontextmanager)Úsettings)ÚImproperlyConfigured)ÚDatabaseError)Úconnections)ÚBaseDatabaseWrapper©ÚCursorDebugWrapper)Úasync_unsafe)Úcached_property)Ú
SafeString)Úget_version_tuplez(Error loading psycopg2 or psycopg modulec                  C   s   t j dd¡d } t| ƒS )Nú é   r   )ÚDatabaseÚ__version__Úsplitr   )Úversion© r   úlC:\Users\psant\Downloads\Anu Raj Backend\school\venv\Lib\site-packages\django/db/backends/postgresql/base.pyÚpsycopg_version    s   r   )é   é   é   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_tzloaderZtimestamptzi  Ú	INETARRAY)ÚDatabaseClient)ÚDatabaseCreation)ÚDatabaseFeatures)ÚDatabaseIntrospection)ÚDatabaseOperations)ÚDatabaseSchemaEditorc                 C   s   | d d u rdS d|  S )NÚ
max_lengthZvarcharúvarchar(%(max_length)s)r   )Údatar   r   r   Ú_get_varchar_columnS   s   r,   c                       s¤  e Zd ZdZdZi dd“dd“dd“d	d
“de“dd“dd“dd“dd“dd“dd“dd“dd“dd“dd“dd“dd“dddd dd d d!d"d#d$œ
¥Zd%d%d%d&œZd'd'd'd(œZd)d*d+d,d-d.d/d0d1d2d+d+d,d,d3œZ	d4Z
d5d6d7d8d9d:d;œZeZeZeZeZeZeZeZd<Zd=d>„ Zd?d@„ ZedAdB„ ƒZdCdD„ ZdEdF„ Z‡ fdGdH„Z ed\dJdK„ƒZ!dLdM„ Z"edNdO„ ƒZ#dPdQ„ Z$d\dRdS„Z%dTdU„ Z&e'‡ fdVdW„ƒZ(e)dXdY„ ƒZ*dZd[„ Z+‡  Z,S )]ÚDatabaseWrapperÚ
postgresqlZ
PostgreSQLÚ	AutoFieldÚintegerÚBigAutoFieldZbigintZBinaryFieldZbyteaZBooleanFieldÚbooleanZ	CharFieldZ	DateFieldÚdateZDateTimeFieldztimestamp with time zoneZDecimalFieldz+numeric(%(max_digits)s, %(decimal_places)s)ZDurationFieldÚintervalZ	FileFieldr*   ZFilePathFieldZ
FloatFieldzdouble precisionZIntegerFieldZBigIntegerFieldZIPAddressFieldZinetZGenericIPAddressFieldZ	JSONFieldZjsonbZsmallintÚtextÚtimeÚuuid)
ZOneToOneFieldÚPositiveBigIntegerFieldÚPositiveIntegerFieldÚPositiveSmallIntegerFieldZ	SlugFieldÚSmallAutoFieldZSmallIntegerFieldZ	TextFieldZ	TimeFieldZ	UUIDFieldz"%(column)s" >= 0)r8   r9   r:   z GENERATED BY DEFAULT AS IDENTITY)r/   r1   r;   z= %sz= UPPER(%s)zLIKE %szLIKE UPPER(%s)z~ %sz~* %sz> %sz>= %sz< %sz<= %s)ÚexactZiexactÚcontainsÚ	icontainsÚregexZiregexÚgtZgteÚltZ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({}))r=   r>   rB   rD   rC   rE   r   c                 C   s   t | jdƒS )zo
        Return a tuple of the database's version.
        E.g. for pg_version 120004, return (12, 4).
        i'  N)ÚdivmodÚ
pg_version©Úselfr   r   r   Úget_database_version·   s   z$DatabaseWrapper.get_database_versionc                 C   sl  | j }|d dkr| di ¡ d¡stdƒ‚t|d pdƒ| j ¡ kr5td|d t|d ƒ| j ¡ f ƒ‚ddi}|d rHd	|d i|d ¥}n|d d u ra| di ¡ dd ¡ d	d
i|d ¥}ni |d ¥}| dd ¡ | dd ¡ | dd ¡ |d rƒ|d |d< |d r�|d |d< |d r—|d |d< |d r¡|d |d< tr´tt	j
| jƒ|d< | dd ¡|d< |S )NÚNAMEÚ ÚOPTIONSZservicez`settings.DATABASES is improperly configured. Please supply the NAME or OPTIONS['service'] value.z‡The database name '%s' (%d characters) is longer than PostgreSQL's limit of %d characters. Supply a shorter NAME in settings.DATABASES.Zclient_encodingÚUTF8ZdbnameÚpostgresÚassume_roleÚisolation_levelÚserver_side_bindingÚUSERÚuserZPASSWORDÚpasswordZHOSTÚhostZPORTÚportÚcontextZprepare_threshold)Úsettings_dictÚgetr   ÚlenÚopsZmax_name_lengthÚpopr   r    r   ÚUSE_TZÚtimezone)rI   rY   Úconn_paramsr   r   r   Úget_connection_params¾   sZ   ÿÿ
ýýÿ
ÿþÿÿz%DatabaseWrapper.get_connection_paramsc                 C   sÊ   | j d }d}z|d }W n ty   tj| _Y nw z	t|ƒ| _d}W n ty3   td|› d�ƒ‚w | jjdi |¤Ž}|rC| j|_t	rV| 
d¡du rQt|_|S t|_|S tjj|dd	„ d
� t|_|S )NrM   FrQ   Tz$Invalid transaction isolation level z9 specified. Use one of the psycopg.IsolationLevel values.rR   c                 S   s   | S ©Nr   )Úxr   r   r   Ú<lambda>  s    z4DatabaseWrapper.get_new_connection.<locals>.<lambda>)Zconn_or_cursÚloadsr   )rY   ÚKeyErrorr   ZREAD_COMMITTEDrQ   Ú
ValueErrorr   r   Úconnectr   rZ   ÚServerBindingCursorÚCursorZcursor_factoryÚpsycopg2ÚextrasZregister_default_jsonb)rI   r`   ÚoptionsZset_isolation_levelZisolation_level_valueÚ
connectionr   r   r   Úget_new_connectionö   s>   
ÿ

ÿÿÿÿöýüÿz"DatabaseWrapper.get_new_connectionc                 C   sv   | j d u rdS | j j d¡}| j}|r9||kr9| j  ¡ �}| | j ¡ |g¡ W d   ƒ dS 1 s2w   Y  dS dS )NFZTimeZoneT)rn   ÚinfoZparameter_statusÚtimezone_nameÚcursorÚexecuter\   Zset_time_zone_sql)rI   Zconn_timezone_namerq   rr   r   r   r   Úensure_timezone   s   

ÿþzDatabaseWrapper.ensure_timezonec                 C   st   | j d u rdS | j di ¡ d¡ }r8| j  ¡ �}| j d|g¡}| |¡ W d   ƒ dS 1 s1w   Y  dS dS )NFrM   rP   zSET ROLE %sT)rn   rY   rZ   rr   r\   Zcompose_sqlrs   )rI   Znew_rolerr   r   r   r   r   Úensure_role+  s   

þýzDatabaseWrapper.ensure_rolec                    s@   t ƒ  ¡  |  ¡ }|  ¡ }|s|r|  ¡ s| j ¡  d S d S d S rb   )ÚsuperÚinit_connection_statert   ru   Zget_autocommitrn   Úcommit)rI   Z	commit_tzZcommit_role©Ú	__class__r   r   rw   5  s   
ÿz%DatabaseWrapper.init_connection_stateNc                 C   sn   |r| j j|d| j jd�}n| j  ¡ }tr,| j j ttj¡}| j	|j	kr*t
| j	|ƒ |S tjr2| jnd |_|S )NF)Z
scrollableZwithhold)rn   rr   Ú
autocommitr   r   Ú
get_loaderÚTIMESTAMPTZ_OIDr   ZTEXTr_   r!   r   r^   Útzinfo_factory)rI   Únamerr   Ztzloaderr   r   r   Úcreate_cursorB  s   
ÿ
ÿzDatabaseWrapper.create_cursorc                 C   s   | j S rb   )r_   )rI   Úoffsetr   r   r   r~   W  s   zDatabaseWrapper.tzinfo_factoryc                 C   sf   |  j d7  _ zt ¡ }W n ty   d }Y nw |r"tt|ƒƒ}nd}| jdt ¡ j	|| j f d�S )Nr   Zsyncz_django_curs_%d_%s_%d)r   )
Ú_named_cursor_idxÚasyncioÚcurrent_taskÚRuntimeErrorÚstrÚidZ_cursorÚ	threadingÚcurrent_threadÚident)rI   r„   Z
task_identr   r   r   Úchunked_cursorZ  s"   ÿüÿÿzDatabaseWrapper.chunked_cursorc                 C   s4   | j � || j_W d   ƒ d S 1 sw   Y  d S rb   )Zwrap_database_errorsrn   r{   )rI   r{   r   r   r   Ú_set_autocommitv  s   
"ÿzDatabaseWrapper._set_autocommitc                 C   sB   |   ¡ �}| d¡ | d¡ W d  ƒ dS 1 sw   Y  dS )zl
        Check constraints by setting them to immediate. Return them to deferred
        afterward.
        zSET CONSTRAINTS ALL IMMEDIATEzSET CONSTRAINTS ALL DEFERREDN)rr   rs   )rI   Ztable_namesrr   r   r   r   Úcheck_constraintsz  s   

"þz!DatabaseWrapper.check_constraintsc                 C   sV   z| j  ¡ �}| d¡ W d   ƒ W dS 1 sw   Y  W dS  tjy*   Y dS w )NzSELECT 1FT)rn   rr   rs   r   ÚError©rI   rr   r   r   r   Ú	is_usableƒ  s   ÿûýÿzDatabaseWrapper.is_usablec                 #   s  � d }zt ƒ  ¡ �}|V  W d   ƒ W d S 1 sw   Y  W d S  tjtfy‚   |d ur/‚ t dt¡ t 	¡ D ]G}|j
dkr€|jd dkr€| ji | j¥d|jd i¥| jd�}z| ¡ �}|V  W d   ƒ n1 slw   Y  W | ¡  n| ¡  w  Y d S q9‚ w )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.r.   rK   rO   )Úalias)rv   Ú_nodb_cursorr   r   ÚWrappedDatabaseErrorÚwarningsÚwarnÚRuntimeWarningr   ÚallÚvendorrY   rz   r‘   rr   Úclose)rI   rr   rn   Úconnry   r   r   r’   �  sH   €&ÿúÿþÿ
þû
ÿ€€âzDatabaseWrapper._nodb_cursorc                 C   s6   |   ¡ � | jjjW  d   ƒ S 1 sw   Y  d S rb   )Ztemporary_connectionrn   rp   Zserver_versionrH   r   r   r   rG   ³  s   
$ÿzDatabaseWrapper.pg_versionc                 C   s
   t || ƒS rb   r   r�   r   r   r   Úmake_debug_cursor¸  s   
z!DatabaseWrapper.make_debug_cursorrb   )-Ú__name__Ú
__module__Ú__qualname__r˜   Zdisplay_namer,   Ú
data_typesZdata_type_check_constraintsZdata_types_suffixÚ	operatorsZpattern_escZpattern_opsr   r(   ZSchemaEditorClassr#   Zclient_classr$   Zcreation_classr%   Zfeatures_classr&   Zintrospection_classr'   Z	ops_classr‚   rJ   ra   r
   ro   rt   ru   rw   r€   r~   r‹   rŒ   r�   r�   r   r’   r   rG   r›   Ú__classcell__r   r   ry   r   r-   Y   sÚ    ÿþýüûúùø	÷
öõôóòñðïåýýòÿú	8
)


	
%
r-   c                   @   s   e Zd ZdZddd„ZdS )ÚCursorMixinzE
        A subclass of psycopg cursor implementing callproc.
        Nc                 C   sˆ   t |tjƒst |¡}t d¡|t d¡g}|r0|D ]}| t |¡¡ | t d¡¡ q|d= | t d¡¡ t |¡}|  |¡ |S )NzSELECT * FROM ú(ú,éÿÿÿÿú))Ú
isinstancer   Z
IdentifierZSQLÚappendÚLiteralZComposedrs   )rI   r   ÚargsZqpartsÚitemÚstmtr   r   r   ÚcallprocÃ  s   


zCursorMixin.callprocrb   )rœ   r�   rž   Ú__doc__r­   r   r   r   r   r¢   ¾  s    r¢   c                   @   ó   e Zd ZdS )ri   N©rœ   r�   rž   r   r   r   r   ri   Ó  ó    ri   c                   @   r¯   )rj   Nr°   r   r   r   r   rj   Ö  r±   rj   c                   @   s   e Zd Zdd„ ZdS )r	   c                 C   s:   |   |¡� | j |¡W  d   ƒ S 1 sw   Y  d S rb   )Ú	debug_sqlrr   Úcopy)rI   Z	statementr   r   r   r³   Ú  s   
$ÿzCursorDebugWrapper.copyN)rœ   r�   rž   r³   r   r   r   r   r	   Ù  s    r	   c                   @   s   e Zd Zdd„ Zdd„ ZdS )r	   c                 G   sD   |   |¡� | jj||g|¢R Ž W  d   ƒ S 1 sw   Y  d S rb   )r²   rr   Úcopy_expert)rI   r   Úfilerª   r   r   r   r´   â  s   $ÿzCursorDebugWrapper.copy_expertc                 O   sP   | j d| d�� | jj||g|¢R i |¤ŽW  d   ƒ S 1 s!w   Y  d S )NzCOPY %s TO STDOUT)r   )r²   rr   Úcopy_to)rI   rµ   Útablerª   Úkwargsr   r   r   r¶   æ  s   $ÿzCursorDebugWrapper.copy_toN)rœ   r�   rž   r´   r¶   r   r   r   r   r	   á  s    )Lr®   rƒ   rˆ   r”   Ú
contextlibr   Zdjango.confr   Zdjango.core.exceptionsr   Z	django.dbr   r“   r   Zdjango.db.backends.base.baser   Zdjango.db.backends.utilsr	   ZBaseCursorDebugWrapperZdjango.utils.asyncior
   Zdjango.utils.functionalr   Zdjango.utils.safestringr   Zdjango.utils.versionr   Zpsycopgr   ÚImportErrorrk   r   r   Zpsycopg_anyr   r   r   r   Z
psycopg.pqr   r    r!   ÚtypesÚoidr}   Zpsycopg2.extensionsZpsycopg2.extrasÚ
extensionsZregister_adapterÚQuotedStringrl   Zregister_uuidZINETARRAY_OIDZnew_array_typeÚUNICODEr"   Zregister_typeÚclientr#   Zcreationr$   Úfeaturesr%   Zintrospectionr&   Ú
operationsr'   Zschemar(   r,   r-   r¢   rj   ri   ZClientCursorrr   r   r   r   r   Ú<module>   sˆ    ÿ€ÿ

ÿ
ÿ
ý  e