o
    ô<âg+  ã                   @   sî   d dl Z d dlZd dl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 e d¡ZG dd	„ d	ƒZG d
d„ deƒZedd„ ƒZdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zd!dd„Zdd„ Zdd„ Zdd „ ZdS )"é    N)Úcontextmanager)Úmd5)Úapps)ÚNotSupportedError)Ú
parse_timezdjango.db.backendsc                   @   sx   e Zd Zdd„ Zeg d¢ƒZdZdd„ Zdd„ Zd	d
„ Z	dd„ Z
ddd„Zddd„Zdd„ Zdd„ Zdd„ Zdd„ ZdS )ÚCursorWrapperc                 C   s   || _ || _d S ©N)ÚcursorÚdb)Úselfr	   r
   © r   úK/var/www/html/venv/lib/python3.10/site-packages/django/db/backends/utils.pyÚ__init__   s   
zCursorWrapper.__init__)ÚfetchoneÚ	fetchmanyÚfetchallÚnextsetz©Accessing the database during app initialization is discouraged. To fix this warning, avoid executing queries in AppConfig.ready() or when your app modules are imported.c                 C   s&   t | j|ƒ}|tjv r| j |¡S |S r   )Úgetattrr	   r   ÚWRAP_ERROR_ATTRSr
   Úwrap_database_errors)r   ÚattrÚcursor_attrr   r   r   Ú__getattr__   s   
zCursorWrapper.__getattr__c                 c   s<    | j j | jE d H  W d   ƒ d S 1 sw   Y  d S r   )r
   r   r	   ©r   r   r   r   Ú__iter__%   s   €
"ÿzCursorWrapper.__iter__c                 C   s   | S r   r   r   r   r   r   Ú	__enter__)   s   zCursorWrapper.__enter__c                 C   s*   z|   ¡  W d S  | jjjy   Y d S w r   )Úcloser
   ÚDatabaseÚError)r   ÚtypeÚvalueÚ	tracebackr   r   r   Ú__exit__,   s
   ÿzCursorWrapper.__exit__Nc                 C   sÒ   |d ur| j jjstdƒ‚tjstjstj| j	t
d | j  ¡  | j j; |d u r:|d u r:| j |¡W  d   ƒ S |d u rL| j ||¡W  d   ƒ S |pOd}| j |||¡W  d   ƒ S 1 sbw   Y  d S )NzKKeyword parameters for callproc are not supported on this database backend.©Úcategoryr   )r
   ÚfeaturesÚsupports_callproc_kwargsr   r   ÚreadyÚstored_app_configsÚwarningsÚwarnÚAPPS_NOT_READY_WARNING_MSGÚRuntimeWarningÚvalidate_no_broken_transactionr   r	   Úcallproc)r   ÚprocnameÚparamsÚkparamsr   r   r   r.   8   s"   ÿ


þü$ùzCursorWrapper.callprocc                 C   ó   | j ||d| jdS )NF©ÚmanyÚexecutor)Ú_execute_with_wrappersÚ_execute©r   Úsqlr0   r   r   r   ÚexecuteN   ó   
ÿzCursorWrapper.executec                 C   r2   )NTr3   )r6   Ú_executemany©r   r9   Ú
param_listr   r   r   ÚexecutemanyS   r;   zCursorWrapper.executemanyc                 C   s8   | j | dœ}t| j jƒD ]}t ||¡}q|||||ƒS )N)Ú
connectionr	   )r
   ÚreversedÚexecute_wrappersÚ	functoolsÚpartial)r   r9   r0   r4   r5   ÚcontextÚwrapperr   r   r   r6   X   s   z$CursorWrapper._execute_with_wrappersc                 G   s‚   t jst jstj| jtd | j ¡  | jj	  |d u r)| j
 |¡W  d   ƒ S | j
 ||¡W  d   ƒ S 1 s:w   Y  d S ©Nr#   )r   r'   r(   r)   r*   r+   r,   r
   r-   r   r	   r:   )r   r9   r0   Úignored_wrapper_argsr   r   r   r7   ^   s   


ý$ûzCursorWrapper._executec                 G   s`   t jst jstj| jtd | j ¡  | jj	 | j
 ||¡W  d   ƒ S 1 s)w   Y  d S rG   )r   r'   r(   r)   r*   r+   r,   r
   r-   r   r	   r?   )r   r9   r>   rH   r   r   r   r<   k   s   

$ÿzCursorWrapper._executemany)NNr   )Ú__name__Ú
__module__Ú__qualname__r   Ú	frozensetr   r+   r   r   r   r"   r.   r:   r?   r6   r7   r<   r   r   r   r   r      s    ÿ

r   c                       s:   e Zd Zd	‡ fdd„	Z‡ fdd„Ze	d
dd„ƒZ‡  ZS )ÚCursorDebugWrapperNc                    óB   | j ||dd tƒ  ||¡W  d   ƒ S 1 sw   Y  d S )NT)Úuse_last_executed_query)Ú	debug_sqlÚsuperr:   r8   ©Ú	__class__r   r   r:   x   ó   $ÿzCursorDebugWrapper.executec                    rN   )NT)r4   )rP   rQ   r?   r=   rR   r   r   r?   |   rT   zCursorDebugWrapper.executemanyFc           	      c   sb   t  ¡ }zXd V  W t  ¡ }|| }|r| jj | j||¡}z
|r%t|ƒnd}W n ty3   d}Y nw | jj 	|r@d||f n|d| dœ¡ t
jd|||| jj|||| jjdœd d S t  ¡ }|| }|rr| jj | j||¡}z
|ryt|ƒnd}W n ty‡   d}Y nw | jj 	|r”d||f n|d| dœ¡ t
jd|||| jj|||| jjdœd w )	NÚ ú?z%s times: %sú%.3f©r9   Útimeú(%.3f) %s; args=%s; alias=%s)Údurationr9   r0   Úalias©Úextra)rY   Ú	monotonicr
   ÚopsÚlast_executed_queryr	   ÚlenÚ	TypeErrorÚqueries_logÚappendÚloggerÚdebugr\   )	r   r9   r0   rO   r4   ÚstartÚstopr[   Útimesr   r   r   rP   €   sp   €þþÿü
úñþþÿüúzCursorDebugWrapper.debug_sqlr   )NNFF)rI   rJ   rK   r:   r?   r   rP   Ú__classcell__r   r   rR   r   rM   u   s    ÿrM   c                 c   sÄ    t  ¡ }z1d V  W | jr5t  ¡ }|| }| j d| d| dœ¡ tjd||d | j||| jdœd d S d S | jrat  ¡ }|| }| j d| d| dœ¡ tjd||d | j||| jdœd w w )Nz%srW   rX   rZ   )r[   r9   r\   r]   )rY   r_   Úqueries_loggedrd   re   rf   rg   r\   )r@   r9   rh   ri   r[   r   r   r   Údebug_transaction¦   sV   €þÿý
ú
÷þÿýú÷rm   c                 C   sX   dD ]$}|| v r&|   |d¡\}}|r&t|ƒr&d|vr|› d}|||f  S q| ddfS )zH
    Split a time zone name into a 3-tuple of (name, sign, offset).
    )ú+ú-é   ú:z:00N)Úrsplitr   )ÚtznameÚsignÚnameÚoffsetr   r   r   Úsplit_tzname_deltaÃ   s   
€
rw   c                 C   s   | rt jtt|  d¡ƒŽ S d S )Nro   )ÚdatetimeÚdateÚmapÚintÚsplit)Úsr   r   r   Útypecast_dateÖ   s   ÿÿr~   c              	   C   s`   | sd S |   d¡\}}}d|v r|  d¡\}}nd}t t|ƒt|ƒt|ƒt|d d d… ƒ¡S )Nrq   Ú.Ú0Ú000000é   )r|   rx   rY   r{   )r}   ÚhourÚminutesÚsecondsÚmicrosecondsr   r   r   Útypecast_timeÜ   s   $ÿr‡   c                 C   sà   | sd S d| vrt | ƒS |  ¡ \}}d|v r| dd¡\}}nd|v r+| dd¡\}}| d¡}| d¡}|d }d|v rE| d¡\}}nd}t t|d	 ƒt|d ƒt|d ƒt|d	 ƒt|d ƒt|ƒt|d
 d d… ƒ¡S )Nú ro   rp   rn   rq   é   r   r€   r   r   r‚   )r~   r|   rx   r{   )r}   ÚdÚtÚ_Údatesrj   r…   r†   r   r   r   Útypecast_timestampé   s0   






ùrŽ   c                 C   sD   z	|   d¡\}}W n ty   d| }}Y nw | d¡| d¡fS )zµ
    Split an SQL identifier into a two element tuple of (namespace, name).

    The identifier could be a table, column, or sequence name might be prefixed
    by a namespace.
    z"."rU   ú")r|   Ú
ValueErrorÚstrip)Ú
identifierÚ	namespaceru   r   r   r   Úsplit_identifier  s   ÿr”   é   c                 C   sV   t | ƒ\}}|du st|ƒ|kr| S t||d}d|rd| nd|d|| … |f S )zÎ
    Shorten an SQL identifier to a repeatable mangled version with the given
    length.

    If a quote stripped name contains a namespace, e.g. USERNAME"."TABLE,
    truncate the table portion only.
    N)Úlengthz%s%s%sz%s"."rU   )r”   rb   Únames_digest)r’   r–   Úhash_lenr“   ru   Údigestr   r   r   Útruncate_name  s   ýrš   c                 G   s2   t dd}|D ]	}| | ¡ ¡ q| ¡ d| … S )zk
    Generate a 32-bit digest of a set of arguments that can be used to shorten
    identifying names.
    F)ÚusedforsecurityN)r   ÚupdateÚencodeÚ	hexdigest)r–   ÚargsÚhÚargr   r   r   r—   0  s   
r—   c                 C   sl   | du rdS t  ¡  ¡ }|dur||_|dur&| jt  d¡ | ¡|d} nd|jt j< | 	| ¡} d 
| ¡S )zc
    Format a number into a string with the requisite number of digits and
    decimal places.
    Nrp   )rE   z{:f})ÚdecimalÚ
getcontextÚcopyÚprecÚquantizeÚDecimalÚscalebÚtrapsÚRoundedÚcreate_decimalÚformat)r    Ú
max_digitsÚdecimal_placesrE   r   r   r   Úformat_number;  s   ÿ

r¯   c                 C   s(   |   d¡o	|  d¡}|r| dd… S | S )zÆ
    Strip quotes off of quoted table names to make them safe for use in index
    names, sequence names, etc. For example '"USER"."TABLE"' (an Oracle naming
    scheme) becomes 'USER"."TABLE'.
    r   rp   éÿÿÿÿ)Ú
startswithÚendswith)Ú
table_nameÚ
has_quotesr   r   r   Ústrip_quotesO  s   rµ   )Nr•   )rx   r¢   rC   ÚloggingrY   r)   Ú
contextlibr   Úhashlibr   Údjango.appsr   Ú	django.dbr   Údjango.utils.dateparser   Ú	getLoggerrf   r   rM   rm   rw   r~   r‡   rŽ   r”   rš   r—   r¯   rµ   r   r   r   r   Ú<module>   s2    
d1
$
