o
    Ti'                     @   s   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 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)NotSupportedError)md5)
parse_timezdjango.db.backendsc                   @   st   e Zd Zdd Zeg 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   X/var/www/html/evchargy.com/venv/lib/python3.10/site-packages/django/db/backends/utils.py__init__   s   
zCursorWrapper.__init__)ZfetchoneZ	fetchmanyZfetchallZnextsetc                 C   s&   t | j|}|tjv r| j|S |S r   )getattrr   r   WRAP_ERROR_ATTRSr	   wrap_database_errors)r
   attrZ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	   Z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| j   | j j; |d u r,|d u r,| j|W  d    S |d u r>| j||W  d    S |pAd}| j|||W  d    S 1 sTw   Y  d S )NzKKeyword parameters for callproc are not supported on this database backend.r   )r	   featuresZsupports_callproc_kwargsr   validate_no_broken_transactionr   r   callproc)r
   ZprocnameparamsZkparamsr   r   r   r   0   s   


$zCursorWrapper.callprocc                 C      | j ||d| jdS )NFmanyexecutor)_execute_with_wrappers_executer
   sqlr   r   r   r   executeB      
zCursorWrapper.executec                 C   r    )NTr!   )r$   _executemanyr
   r'   
param_listr   r   r   executemanyG   r)   zCursorWrapper.executemanyc                 C   s8   | j | d}t| j jD ]}t||}q|||||S )N)
connectionr   )r	   reversedZexecute_wrappers	functoolspartial)r
   r'   r   r"   r#   contextwrapperr   r   r   r$   L   s   z$CursorWrapper._execute_with_wrappersc                 G   sf   | j   | j j  |d u r| j|W  d    S | j||W  d    S 1 s,w   Y  d S r   )r	   r   r   r   r(   )r
   r'   r   ignored_wrapper_argsr   r   r   r%   R   s   


$zCursorWrapper._executec                 G   sD   | j   | j j | j||W  d    S 1 sw   Y  d S r   )r	   r   r   r   r-   )r
   r'   r,   r4   r   r   r   r*   [   s   

$zCursorWrapper._executemany)NNr   )__name__
__module____qualname__r   	frozensetr   r   r   r   r   r   r(   r-   r$   r%   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(   r&   	__class__r   r   r(   d      $zCursorDebugWrapper.executec                    r:   )NT)r"   )r<   r=   r-   r+   r>   r   r   r-   h   r@   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	|rd||f n|d| d t
jd|||| jj|||| jjdd w )	N ?z%s times: %s%.3fr'   time(%.3f) %s; args=%s; alias=%s)durationr'   r   aliasextra)rE   	monotonicr	   opsZlast_executed_queryr   len	TypeErrorqueries_logappendloggerdebugrH   )	r
   r'   r   r;   r"   startstoprG   timesr   r   r   r<   l   sp   
zCursorDebugWrapper.debug_sqlr   )NNFF)r5   r6   r7   r(   r-   r   r<   __classcell__r   r   r>   r   r9   a   s    r9   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%srC   rD   rF   )rG   r'   rH   rI   )rE   rK   Zqueries_loggedrO   rP   rQ   rR   rH   )r.   r'   rS   rT   rG   r   r   r   debug_transaction   sV   

rW   c                 C   sF   dD ]}|| v r|  |d\}}|rt|r|||f  S q| ddfS )zH
    Split a time zone name into a 3-tuple of (name, sign, offset).
    )+-   N)rsplitr   )tznamesignnameoffsetr   r   r   split_tzname_delta   s   
r`   c                 C   s   | rt jtt| d S d S )NrY   )datetimedatemapintsplit)sr   r   r   typecast_date   s   rg   c              	   C   s`   | sd S |  d\}}}d|v r| d\}}nd}tt|t|t|t|d d d S )N:.0000000   )re   ra   rE   rd   )rf   hourminutessecondsmicrosecondsr   r   r   typecast_time   s   $rq   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 rY   rZ   rX   rh      ri   rj   r   rk   rl   )rg   re   ra   rd   )rf   dt_datesrU   ro   rp   r   r   r   typecast_timestamp   s0   






rx   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"."rA   ")re   
ValueErrorstrip)
identifier	namespacer^   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"."rA   )r~   rM   names_digest)r|   r   Zhash_lenr}   r^   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     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.
    NrZ   )r2   z{:f})decimal
getcontextcopyprecquantizeDecimalscalebZtrapsRoundedcreate_decimalformat)r   Z
max_digitsZdecimal_placesr2   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'.
    ry   rZ   )
startswithendswith)Z
table_nameZ
has_quotesr   r   r   strip_quotes9  s   r   )Nr   )ra   r   r0   loggingrE   
contextlibr   Z	django.dbr   Zdjango.utils.cryptor   Zdjango.utils.dateparser   	getLoggerrQ   r   r9   rW   r`   rg   rq   rx   r~   r   r   r   r   r   r   r   r   <module>   s.    
R1
$
