
v^r!                 @   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 e j d  Z Gd d   d  Z Gd	 d
   d
 e  Z d d   Z d d   Z d d   Z d d   Z d d d d  Z d d   Z d d   Z d d   Z d S)    N)contextmanager)settings)NotSupportedError)utczdjango.db.backendsc               @   s   e  Z d  Z d d   Z e d d d d g  Z d d   Z d	 d
   Z d d   Z d d   Z	 d 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
   :/tmp/pip-build-8lau8j11/django/django/db/backends/utils.py__init__   s    	zCursorWrapper.__init__ZfetchoneZ	fetchmanyZfetchallZnextsetc             C   s9   t  |  j |  } | t j k r1 |  j j |  S| Sd  S)N)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 Ed  HWd  QRXd  S)N)r   r   r   )r	   r
   r
   r   __iter__   s    zCursorWrapper.__iter__c             C   s   |  S)Nr
   )r	   r
   r
   r   	__enter__"   s    zCursorWrapper.__enter__c             C   s0   y |  j    Wn |  j j j k
 r+ Yn Xd  S)N)closer   ZDatabaseError)r	   typevalue	tracebackr
   r
   r   __exit__%   s    zCursorWrapper.__exit__Nc             C   s   | d  k	 r( |  j  j j r( t d   |  j  j   |  j  j n | d  k rj | d  k rj |  j j |  S| d  k r |  j j | |  S| p f  } |  j j | | |  SWd  QRXd  S)NzKKeyword parameters for callproc are not supported on this database backend.)r   featuresZsupports_callproc_kwargsr   validate_no_broken_transactionr   r   callproc)r	   ZprocnameparamsZkparamsr
   r
   r   r   1   s    	zCursorWrapper.callprocc             C   s   |  j  | | d d d |  j S)NmanyFexecutor)_execute_with_wrappers_execute)r	   sqlr   r
   r
   r   executeC   s    zCursorWrapper.executec             C   s   |  j  | | d d d |  j S)Nr   Tr   )r    _executemany)r	   r"   
param_listr
   r
   r   executemanyF   s    zCursorWrapper.executemanyc             C   sW   d |  j  d |  i } x, t |  j  j  D] } t j | |  } q( W| | | | |  S)N
connectionr   )r   reversedZexecute_wrappers	functoolspartial)r	   r"   r   r   r   contextwrapperr
   r
   r   r    I   s    z$CursorWrapper._execute_with_wrappersc          
   G   sT   |  j  j   |  j  j 4 | d  k r6 |  j j |  S|  j j | |  SWd  QRXd  S)N)r   r   r   r   r#   )r	   r"   r   ignored_wrapper_argsr
   r
   r   r!   O   s
    zCursorWrapper._executec          
   G   s8   |  j  j   |  j  j  |  j j | |  SWd  QRXd  S)N)r   r   r   r   r&   )r	   r"   r%   r-   r
   r
   r   r$   X   s    zCursorWrapper._executemany)__name__
__module____qualname__r   	frozensetr   r   r   r   r   r   r#   r&   r    r!   r$   r
   r
   r
   r   r      s   	r   c                   sU   e  Z d  Z d   f d d  Z   f d d   Z e d d d d d d   Z   S)	CursorDebugWrapperNc          
      s7   |  j  | | d d  t   j | |  SWd  QRXd  S)Nuse_last_executed_queryT)	debug_sqlsuperr#   )r	   r"   r   )	__class__r
   r   r#   b   s    zCursorDebugWrapper.executec          
      s7   |  j  | | d d  t   j | |  SWd  QRXd  S)Nr   T)r4   r5   r&   )r	   r"   r%   )r6   r
   r   r&   f   s    zCursorDebugWrapper.executemanyFc       	      c   s   t  j   } z	 d  VWd  t  j   } | | } | rR |  j j j |  j | |  } y | rg t |  n d } Wn t k
 r d } Yn X|  j j j	 d | r d | | f n | d d | i  t
 j d | | | d d	 | d | d
 | i Xd  S)N ?r"   z%s times: %stimez%.3fz(%.3f) %s; args=%sextradurationr   )r9   	monotonicr   opsZlast_executed_queryr   len	TypeErrorZqueries_logappendloggerdebug)	r	   r"   r   r3   r   startstopr;   timesr
   r
   r   r4   j   s(    	
zCursorDebugWrapper.debug_sql)r.   r/   r0   r#   r&   r   r4   r
   r
   )r6   r   r2   ^   s   r2   c             C   s)   |  r% t  j t t |  j d     Sd  S)N-)datetimedatemapintsplit)sr
   r
   r   typecast_date   s    rM   c             C   s   |  s
 d  S|  j  d  \ } } } d | k rF | j  d  \ } } n d } t j t |  t |  t |  t | d d  d    S)N:.0000000   )rK   rG   r9   rJ   )rL   hourminutessecondsmicrosecondsr
   r
   r   typecast_time   s    rW   c       	      C   sR  |  s
 d  Sd |  k r  t  |   S|  j   \ } } d | k rY | j d d  \ } } n$ d | k r} | j d d  \ } } | j d  } | j d  } | d } d | k r | j d  \ } } n d } t j r t n d  } t j t | d	  t | d  t | d  t | d	  t | d  t |  t | d
 d  d   |  S)N rF      +rN      rO   rP   r   rQ   rR   )rM   rK   r   ZUSE_TZr   rG   rJ   )	rL   dt_datesrE   rU   rV   tzinfor
   r
   r   typecast_timestamp   s(    

'#ra   c             C   sW   y |  j  d  \ } } Wn t k
 r: d |  } } Yn X| j d  | j d  f S)z
    Split a 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"."r7   ")rK   
ValueErrorstrip)
identifier	namespacenamer
   r
   r   split_identifier   s
    rh      c             C   su   t  |   \ } } | d k s0 t |  | k r4 |  St | d | } d | rY d | n d | d | |  | f S)z
    Shorten a 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"."r7   )rh   r>   names_digest)re   rj   Zhash_lenrf   rg   digestr
   r
   r   truncate_name   s
    rm   c             G   sD   t  j   } x! | D] } | j | j    q W| j   d |   S)zk
    Generate a 32-bit digest of a set of arguments that can be used to shorten
    identifying names.
    N)hashlibmd5updateencode	hexdigest)rj   argshargr
   r
   r   rk      s    rk   c             C   s   |  d k r d St  j   j   } | d k	 r7 | | _ | d k	 rn |  j t  j d  j |  d | }  n d | j t  j <| j	 |   }  d j
 |   S)zc
    Format a number into a string with the requisite number of digits and
    decimal places.
    NrY   r+   z{:f})decimalZ
getcontextcopyZprecZquantizeZDecimalZscalebZtrapsZRoundedZcreate_decimalformat)r   Z
max_digitsZdecimal_placesr+   r
   r
   r   format_number   s    	+ry   c             C   s6   |  j  d  o |  j d  } | r2 |  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'.
    rb   rY   )
startswithendswith)Z
table_nameZ
has_quotesr
   r
   r   strip_quotes   s    r}   )rG   rv   r)   rn   loggingr9   
contextlibr   Zdjango.confr   Zdjango.db.utilsr   Zdjango.utils.timezoner   	getLoggerrA   r   r2   rM   rW   ra   rh   rm   rk   ry   r}   r
   r
   r
   r   <module>   s(   N, 