
 XZ                 @   s5  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 m Z d  d l m Z d  d l m Z d  d l m Z y d  d l Z Wn e k
 rd Z Yn Xd Z Gd d   d e   Z! d S)    N)deque)contextmanager)settings)ImproperlyConfigured)DEFAULT_DB_ALIAS)utils)connection_created)TransactionManagementError)DatabaseErrorDatabaseErrorWrapper)timezone)cached_property)_threadZ	__no_db__c               @   s  e  Z d  Z d Z i  Z i  Z i  Z d Z d Z d Z	 d Z
 e d d d  Z e d d	    Z e d
 d    Z e d d    Z e d d    Z d d   Z d d   Z d d   Z d d   Z d d   Z d d   Z d d   Z d d   Z d  d!   Z d" d#   Z d$ d%   Z d& d'   Z d( d)   Z d* d+   Z  d, d-   Z! d. d/   Z" d0 d1   Z# d2 d3   Z$ d4 d5   Z% d6 d7   Z& d8 d9   Z' d: d;   Z( d< d=   Z) d> d?   Z* d@ dA   Z+ d dB dC  Z, dD dE   Z- dF dG   Z. dH dI   Z/ dJ dK   Z0 e1 dL dM    Z2 dN dO   Z3 dP dQ   Z4 d dR dS  Z5 dT dU   Z6 dV dW   Z7 dX dY   Z8 dZ d[   Z9 e d\ d]    Z: d^ d_   Z; d` da   Z< e1 db dc    Z= e dd de    Z> df dg   Z? dh di   Z@ dj dk   ZA dl dm   ZB d d dn do  ZC d S)pBaseDatabaseWrapperz+
    Represents a database connection.
    Nunknowni(#  Fc             C   s   d  |  _  | |  _ | |  _ t d |  j  |  _ d |  _ d |  _ d |  _ d |  _	 g  |  _
 d |  _ d |  _ d  |  _ d |  _ d |  _ | |  _ t j   |  _ g  |  _ d |  _ d  S)NmaxlenFr   T)
connectionsettings_dictaliasr   queries_limitqueries_logforce_debug_cursor
autocommitin_atomic_blocksavepoint_statesavepoint_idsZcommit_on_exitneeds_rollbackclose_atclosed_in_transactionerrors_occurredallow_thread_sharingthread	get_ident_thread_identrun_on_commit%run_commit_hooks_on_set_autocommit_on)selfr   r   r     r'   J/home/ubuntu/projects/ifolica/build/django/django/db/backends/base/base.py__init__*   s$    															zBaseDatabaseWrapper.__init__c             C   sO   t  j s d S|  j j r d S|  j d d k r7 t j St j |  j d  Sd S)a_  
        Time zone for datetimes stored as naive values in the database.

        Returns a tzinfo object or None.

        This is only needed when time zone support is enabled and the database
        doesn't support time zones. (When the database supports time zones,
        the adapter handles aware datetimes so Django doesn't need to.)
        N	TIME_ZONE)r   USE_TZfeaturessupports_timezonesr   r   utcpytz)r&   r'   r'   r(   r   [   s    	zBaseDatabaseWrapper.timezonec             C   s6   t  j s t  j S|  j d d k r' d S|  j d Sd S)zC
        Name of the time zone of the database connection.
        r*   NUTC)r   r+   r*   r   )r&   r'   r'   r(   timezone_namep   s
    	z!BaseDatabaseWrapper.timezone_namec             C   s   |  j  p t j S)N)r   r   DEBUG)r&   r'   r'   r(   queries_logged|   s    z"BaseDatabaseWrapper.queries_loggedc             C   sG   t  |  j  |  j j k r: t j d j |  j j   n  t |  j  S)NzLLimit for query logging exceeded, only the last {} queries will be returned.)lenr   r   warningswarnformatlist)r&   r'   r'   r(   queries   s
    zBaseDatabaseWrapper.queriesc             C   s   t  d   d S)z=Returns a dict of parameters suitable for get_new_connection.zNsubclasses of BaseDatabaseWrapper may require a get_connection_params() methodN)NotImplementedError)r&   r'   r'   r(   get_connection_params   s    z)BaseDatabaseWrapper.get_connection_paramsc             C   s   t  d   d S)z#Opens a connection to the database.zKsubclasses of BaseDatabaseWrapper may require a get_new_connection() methodN)r:   )r&   conn_paramsr'   r'   r(   get_new_connection   s    z&BaseDatabaseWrapper.get_new_connectionc             C   s   t  d   d S)z-Initializes the database connection settings.zOsubclasses of BaseDatabaseWrapper may require an init_connection_state() methodN)r:   )r&   r'   r'   r(   init_connection_state   s    z)BaseDatabaseWrapper.init_connection_statec             C   s   t  d   d S)z;Creates a cursor. Assumes that a connection is established.zFsubclasses of BaseDatabaseWrapper may require a create_cursor() methodN)r:   )r&   r'   r'   r(   create_cursor   s    z!BaseDatabaseWrapper.create_cursorc             C   s   |  j    d |  _ g  |  _ d |  _ |  j d } | d k rD d n t j   | |  _ d |  _ d |  _ |  j	   } |  j
 |  |  _ |  j |  j d  |  j   t j d |  j d |   g  |  _ d S)z@Connects to the database. Assumes that the connection is closed.FZCONN_MAX_AGEN
AUTOCOMMITZsenderr   )check_settingsr   r   r   r   timer   r   r   r;   r=   r   set_autocommitr>   r   send	__class__r$   )r&   Zmax_ager<   r'   r'   r(   connect   s    
			%		
zBaseDatabaseWrapper.connectc             C   s}   |  j  d d  k	 ry t j s2 t d |  j   qy |  j j rT t d |  j   qy t d  k ry t d |  j   qy n  d  S)Nr*   z=Connection '%s' cannot set TIME_ZONE because USE_TZ is False.z`Connection '%s' cannot set TIME_ZONE because its engine handles time zones conversions natively.zBConnection '%s' cannot set TIME_ZONE because pytz isn't installed.)r   r   r+   r   r   r,   r-   r/   )r&   r'   r'   r(   rA      s    	z"BaseDatabaseWrapper.check_settingsc             C   s0   |  j  d k r, |  j  |  j   Wd QXn  d S)zN
        Guarantees that a connection to the database is established.
        N)r   wrap_database_errorsrF   )r&   r'   r'   r(   ensure_connection   s    
z%BaseDatabaseWrapper.ensure_connectionc             C   s(   |  j    |  j  |  j   SWd  QXd  S)N)rH   rG   r?   )r&   r'   r'   r(   _cursor   s    

zBaseDatabaseWrapper._cursorc             C   s3   |  j  d  k	 r/ |  j  |  j  j   SWd  QXn  d  S)N)r   rG   commit)r&   r'   r'   r(   _commit   s    
zBaseDatabaseWrapper._commitc             C   s3   |  j  d  k	 r/ |  j  |  j  j   SWd  QXn  d  S)N)r   rG   rollback)r&   r'   r'   r(   	_rollback   s    
zBaseDatabaseWrapper._rollbackc             C   s3   |  j  d  k	 r/ |  j  |  j  j   SWd  QXn  d  S)N)r   rG   close)r&   r'   r'   r(   _close   s    
zBaseDatabaseWrapper._closec             C   sD   |  j    |  j r+ |  j |  j    } n |  j |  j    } | S)zF
        Creates a cursor, opening a connection if necessary.
        )validate_thread_sharingr3   make_debug_cursorrI   make_cursor)r&   cursorr'   r'   r(   rS      s
    
	zBaseDatabaseWrapper.cursorc             C   s4   |  j    |  j   |  j   d |  _ d |  _ d S)zB
        Commits a transaction and resets the dirty flag.
        FTN)rP   validate_no_atomic_blockrK   r   r%   )r&   r'   r'   r(   rJ      s
    


	zBaseDatabaseWrapper.commitc             C   s4   |  j    |  j   |  j   d |  _ g  |  _ d S)zE
        Rolls back a transaction and resets the dirty flag.
        FN)rP   rT   rM   r   r$   )r&   r'   r'   r(   rL      s
    


	zBaseDatabaseWrapper.rollbackc             C   sl   |  j    g  |  _ |  j s+ |  j d k r/ d Sz |  j   Wd |  j r^ d |  _ d |  _ n	 d |  _ Xd S)z8
        Closes the connection to the database.
        NT)rP   r$   r   r   rO   r   r   )r&   r'   r'   r(   rN     s    
			zBaseDatabaseWrapper.closec          
   C   s2   |  j      } | j |  j j |   Wd  QXd  S)N)rS   executeopsZsavepoint_create_sql)r&   sidrS   r'   r'   r(   
_savepoint  s    zBaseDatabaseWrapper._savepointc          
   C   s2   |  j      } | j |  j j |   Wd  QXd  S)N)rS   rU   rV   Zsavepoint_rollback_sql)r&   rW   rS   r'   r'   r(   _savepoint_rollback  s    z'BaseDatabaseWrapper._savepoint_rollbackc          
   C   s2   |  j      } | j |  j j |   Wd  QXd  S)N)rS   rU   rV   Zsavepoint_commit_sql)r&   rW   rS   r'   r'   r(   _savepoint_commit"  s    z%BaseDatabaseWrapper._savepoint_commitc             C   s   |  j  j o |  j   S)N)r,   Zuses_savepointsget_autocommit)r&   r'   r'   r(   _savepoint_allowed&  s    z&BaseDatabaseWrapper._savepoint_allowedc             C   sq   |  j    s d St j   } t |  j d d  } |  j d 7_ d | |  j f } |  j   |  j |  | S)z
        Creates a savepoint inside the current transaction. Returns an
        identifier for the savepoint that will be used for the subsequent
        rollback or commit. Does nothing if savepoints are not supported.
        N-    zs%s_x%d)r\   r!   r"   strreplacer   rP   rX   )r&   Zthread_identtidrW   r'   r'   r(   	savepoint,  s    
zBaseDatabaseWrapper.savepointc                sJ   |  j    s d S|  j   |  j      f d d   |  j D |  _ d S)zZ
        Rolls back to a savepoint. Does nothing if savepoints are not supported.
        Nc                s.   g  |  ]$ \ } }   | k r | | f  q Sr'   r'   ).0sidsfunc)rW   r'   r(   
<listcomp>L  s   	 z:BaseDatabaseWrapper.savepoint_rollback.<locals>.<listcomp>)r\   rP   rY   r$   )r&   rW   r'   )rW   r(   savepoint_rollback@  s
    
z&BaseDatabaseWrapper.savepoint_rollbackc             C   s+   |  j    s d S|  j   |  j |  d S)zU
        Releases a savepoint. Does nothing if savepoints are not supported.
        N)r\   rP   rZ   )r&   rW   r'   r'   r(   savepoint_commitO  s    
z$BaseDatabaseWrapper.savepoint_commitc             C   s   d |  _  d S)zZ
        Resets the counter used to generate unique savepoint ids in this thread.
        r   N)r   )r&   r'   r'   r(   clean_savepointsY  s    z$BaseDatabaseWrapper.clean_savepointsc             C   s   t  d   d S)zR
        Backend-specific implementation to enable or disable autocommit.
        zHsubclasses of BaseDatabaseWrapper may require a _set_autocommit() methodN)r:   )r&   r   r'   r'   r(   _set_autocommita  s    z#BaseDatabaseWrapper._set_autocommitc             C   s   |  j    |  j S)z-
        Check the autocommit state.
        )rH   r   )r&   r'   r'   r(   r[   i  s    
z"BaseDatabaseWrapper.get_autocommitc             C   s   |  j    |  j   | o* | o* |  j j } | r@ |  j   n |  j |  | |  _ | r{ |  j r{ |  j   d |  _ n  d S)a  
        Enable or disable autocommit.

        The usual way to start a transaction is to turn autocommit off.
        SQLite does not properly start a transaction when disabling
        autocommit. To avoid this buggy behavior and to actually enter a new
        transaction, an explcit BEGIN is required. Using
        force_begin_transaction_with_broken_autocommit=True will issue an
        explicit BEGIN with SQLite. This option will be ignored for other
        backends.
        FN)	rT   rH   r,   Z"autocommits_when_autocommit_is_off#_start_transaction_under_autocommitrk   r   r%   run_and_clear_commit_hooks)r&   r   Z.force_begin_transaction_with_broken_autocommitZ"start_transaction_under_autocommitr'   r'   r(   rC   p  s    

	
z"BaseDatabaseWrapper.set_autocommitc             C   s   |  j  s t d   n  |  j S)zK
        Get the "needs rollback" flag -- for *advanced use* only.
        z<The rollback flag doesn't work outside of an 'atomic' block.)r   r	   r   )r&   r'   r'   r(   get_rollback  s    	z BaseDatabaseWrapper.get_rollbackc             C   s%   |  j  s t d   n  | |  _ d S)zT
        Set or unset the "needs rollback" flag -- for *advanced use* only.
        z<The rollback flag doesn't work outside of an 'atomic' block.N)r   r	   r   )r&   rL   r'   r'   r(   set_rollback  s    	z BaseDatabaseWrapper.set_rollbackc             C   s   |  j  r t d   n  d S)z>
        Raise an error if an atomic block is active.
        z3This is forbidden when an 'atomic' block is active.N)r   r	   )r&   r'   r'   r(   rT     s    	z,BaseDatabaseWrapper.validate_no_atomic_blockc             C   s   |  j  r t d   n  d  S)NzlAn error occurred in the current transaction. You can't execute queries until the end of the 'atomic' block.)r   r	   )r&   r'   r'   r(   validate_no_broken_transaction  s    	z2BaseDatabaseWrapper.validate_no_broken_transactionc          
   c   s0   |  j    } z	 d VWd | r+ |  j   n  Xd S)zP
        Context manager that disables foreign key constraint checking.
        N)disable_constraint_checkingenable_constraint_checking)r&   disabledr'   r'   r(   constraint_checks_disabled  s
    	z.BaseDatabaseWrapper.constraint_checks_disabledc             C   s   d S)z
        Backends can implement as needed to temporarily disable foreign key
        constraint checking. Should return True if the constraints were
        disabled and will need to be reenabled.
        Fr'   )r&   r'   r'   r(   rq     s    z/BaseDatabaseWrapper.disable_constraint_checkingc             C   s   d S)zh
        Backends can implement as needed to re-enable foreign key constraint
        checking.
        Nr'   )r&   r'   r'   r(   rr     s    z.BaseDatabaseWrapper.enable_constraint_checkingc             C   s   d S)z
        Backends can override this method if they can apply constraint
        checking (e.g. via "SET CONSTRAINTS ALL IMMEDIATE"). Should raise an
        IntegrityError if any invalid foreign key references are encountered.
        Nr'   )r&   Ztable_namesr'   r'   r(   check_constraints  s    z%BaseDatabaseWrapper.check_constraintsc             C   s   t  d   d S)a  
        Tests if the database connection is usable.

        This function may assume that self.connection is not None.

        Actual implementations should take care not to raise exceptions
        as that may prevent Django from recycling unusable connections.
        zCsubclasses of BaseDatabaseWrapper may require an is_usable() methodN)r:   )r&   r'   r'   r(   	is_usable  s    	zBaseDatabaseWrapper.is_usablec             C   s   |  j  d k	 r |  j   |  j d k r6 |  j   d S|  j rh |  j   rW d |  _ qh |  j   d Sn  |  j d k	 r t j   |  j k r |  j   d Sn  d S)z
        Closes the current connection if unrecoverable errors have occurred,
        or if it outlived its maximum age.
        Nr@   F)r   r[   r   rN   r   rv   r   rB   )r&   r'   r'   r(   close_if_unusable_or_obsolete  s    
	
$
z1BaseDatabaseWrapper.close_if_unusable_or_obsoletec             C   sJ   |  j  p |  j t j   k sF t d |  j |  j t j   f   n  d S)a7  
        Validates that the connection isn't accessed by another thread than the
        one which originally created it, unless the connection was explicitly
        authorized to be shared between threads (via the `allow_thread_sharing`
        property). Raises an exception if the validation fails.
        zDatabaseWrapper objects created in a thread can only be used in that same thread. The object with alias '%s' was created in thread id %s and this is thread id %s.N)r    r#   r!   r"   r
   r   )r&   r'   r'   r(   rP     s    z+BaseDatabaseWrapper.validate_thread_sharingc             C   s   d S)z~
        Hook to do any database check or preparation, generally called before
        migrating a project or an app.
        Nr'   )r&   r'   r'   r(   prepare_database  s    z$BaseDatabaseWrapper.prepare_databasec             C   s
   t  |   S)z
        Context manager and decorator that re-throws backend-specific database
        exceptions using Django's common wrappers.
        )r   )r&   r'   r'   r(   rG     s    z(BaseDatabaseWrapper.wrap_database_errorsc             C   s   t  j | |   S)zM
        Creates a cursor that logs all queries in self.queries_log.
        )r   ZCursorDebugWrapper)r&   rS   r'   r'   r(   rQ     s    z%BaseDatabaseWrapper.make_debug_cursorc             C   s   t  j | |   S)z9
        Creates a cursor without debug logging.
        )r   ZCursorWrapper)r&   rS   r'   r'   r(   rR   $  s    zBaseDatabaseWrapper.make_cursorc          
   c   sI   |  j  d k } |  j   } z	 | VWd | j   | rD |  j   n  Xd S)a9  
        Context manager that ensures that a connection is established, and
        if it opened one, closes it to avoid leaving a dangling connection.
        This is useful for operations outside of the request-response cycle.

        Provides a cursor: with self.temporary_connection() as cursor: ...
        N)r   rS   rN   )r&   Z
must_closerS   r'   r'   r(   temporary_connection*  s    		
z(BaseDatabaseWrapper.temporary_connectionc             C   s8   |  j  j   } d | d <|  j | d t d d } | S)ac  
        Return an alternative connection to be used when there is no need to access
        the main database, specifically for test db creation/deletion.
        This also prevents the production database from being exposed to
        potential child threads while (or after) the test database is destroyed.
        Refs #10868, #17786, #16969.
        NNAMEr   r    F)r   copyrE   NO_DB_ALIAS)r&   r   Znodb_connectionr'   r'   r(   _nodb_connection<  s    	
	z$BaseDatabaseWrapper._nodb_connectionc             C   s   t  d   d S)zO
        Only required when autocommits_when_autocommit_is_off = True.
        z\subclasses of BaseDatabaseWrapper may require a _start_transaction_under_autocommit() methodN)r:   )r&   r'   r'   r(   rl   M  s    z7BaseDatabaseWrapper._start_transaction_under_autocommitc             O   s1   |  j  d k r t d   n  |  j  |  | |  S)zH
        Returns a new instance of this backend's SchemaEditor.
        NzFThe SchemaEditorClass attribute of this database wrapper is still None)SchemaEditorClassr:   )r&   argskwargsr'   r'   r(   schema_editorV  s    z!BaseDatabaseWrapper.schema_editorc             C   sQ   |  j  r+ |  j j t |  j  | f  n" |  j   sF t d   n |   d  S)Nz;on_commit() cannot be used in manual transaction management)r   r$   appendsetr   r[   r	   )r&   rf   r'   r'   r(   	on_commit_  s
    	"zBaseDatabaseWrapper.on_commitc             C   sI   |  j    |  j } g  |  _ x& | rD | j d  \ } } |   q Wd  S)Nr   )rT   r$   pop)r&   Zcurrent_run_on_commitre   rf   r'   r'   r(   rm   j  s    
			z.BaseDatabaseWrapper.run_and_clear_commit_hooksc             C   sX   t  j |  j  } | d k r* |  j } n  | d k rB |  j } n  t |   | | |  S)zy
        Return a copy of this connection.

        For tests that require two connections to the same database.
        N)r{   deepcopyr   r   r    type)r&   r   r    r   r'   r'   r(   r{   r  s    zBaseDatabaseWrapper.copy)D__name__
__module____qualname____doc__Z
data_typesZdata_types_suffixZdata_type_check_constraintsrV   vendorr~   r   r   r)   r   r   r1   propertyr3   r9   r;   r=   r>   r?   rF   rA   rH   rI   rK   rM   rO   rS   rJ   rL   rN   rX   rY   rZ   r\   rc   rh   ri   rj   rk   r[   rC   rn   ro   rT   rp   r   rt   rq   rr   ru   rv   rw   rP   rx   rG   rQ   rR   ry   r}   rl   r   r   rm   r{   r'   r'   r'   r(   r      s|   0


		
		r   )"r{   rB   r5   collectionsr   
contextlibr   Zdjango.confr   Zdjango.core.exceptionsr   Z	django.dbr   Zdjango.db.backendsr   Zdjango.db.backends.signalsr   Zdjango.db.transactionr	   Zdjango.db.utilsr
   r   Zdjango.utilsr   Zdjango.utils.functionalr   Zdjango.utils.six.movesr   r!   r/   ImportErrorr|   objectr   r'   r'   r'   r(   <module>   s(   