Ë
    ojA  ã                   ó~   — d Z ddlmZ ddlmZmZ  G d„ de«      Z G d„ d«      Z G d„ d	«      Z	 G d
„ d«      Z
d	dgZy)zg
An asynchronous mapping to U{DB-API
2.0<http://www.python.org/topics/database/DatabaseAPI-2.0.html>}.
é    )Úthreads)ÚlogÚreflectc                   ó   — e Zd ZdZy)ÚConnectionLostzb
    This exception means that a db connection has been lost.  Client code may
    try again.
    N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__© ó    ú;/usr/lib/python3/dist-packages/twisted/enterprise/adbapi.pyr   r      s   „ òr   r   c                   ó.   — e Zd ZdZd„ Zd„ Zd„ Zd„ Zd„ Zy)Ú
Connectiona  
    A wrapper for a DB-API connection instance.

    The wrapper passes almost everything to the wrapped connection and so has
    the same API. However, the L{Connection} knows about its pool and also
    handle reconnecting should when the real connection dies.
    c                 ó@   — || _         d | _        | j                  «        y ©N)Ú_poolÚ_connectionÚ	reconnect)ÚselfÚpools     r   Ú__init__zConnection.__init__   s   € ØˆŒ
ØˆÔØ�‰Õr   c                  ó   — y r   r   ©r   s    r   ÚclosezConnection.close$   s   € ð 	r   c                 ól  — | j                   j                  s| j                  j                  «        y 	 | j                  j                  «        | j                  j	                  «       }|j                  | j                   j                  «       |j                  «        | j                  j                  «        y # t        $ r t        j                  d d«       Y nw xY w| j                   j                  | j                  «       | j                   j                  rt        j                  d«       t        «       ‚)NúRollback failedzConnection lost.)r   r   r   ÚrollbackÚcursorÚexecuteÚgood_sqlr   ÚcommitÚBaseExceptionr   ÚerrÚ
disconnectÚnoisyÚmsgr   )r   Úcurss     r   r   zConnection.rollback.   sÞ   € Ø�z‰z×#Ò#Ø×Ñ×%Ñ%Ô'Øð	-Ø×Ñ×%Ñ%Ô'Ø×#Ñ#×*Ñ*Ó,ˆDØ�L‰L˜Ÿ™×,Ñ,Ô-Ø�J‰JŒLØ×Ñ×#Ñ#Ô%ØøÜò 	-Ü�G‰G�DÐ+Ö,ð	-úð 	�
‰
×Ñ˜d×.Ñ.Ô/à�:‰:×ÒÜ�G‰GÐ&Ô'äÓÐs   ³BB7 Â7CÃCc                 ó¤   — | j                   �%| j                  j                  | j                   «       | j                  j                  «       | _         y r   )r   r   r%   Úconnectr   s    r   r   zConnection.reconnectD   s;   € Ø×ÑÐ'Ø�J‰J×!Ñ! $×"2Ñ"2Ô3ØŸ:™:×-Ñ-Ó/ˆÕr   c                 ó.   — t        | j                  |«      S r   )Úgetattrr   ©r   Únames     r   Ú__getattr__zConnection.__getattr__I   s   € Ü�t×'Ñ'¨Ó.Ð.r   N)	r   r	   r
   r   r   r   r   r   r/   r   r   r   r   r      s    „ ñòò
òò,0ó
/r   r   c                   ó2   — e Zd ZdZdZd„ Zd„ Zd„ Zd„ Zd„ Z	y)ÚTransactiona  
    A lightweight wrapper for a DB-API 'cursor' object.

    Relays attribute access to the DB cursor. That is, you can call
    C{execute()}, C{fetchall()}, etc., and they will be called on the
    underlying DB-API cursor object. Attributes will also be retrieved from
    there.
    Nc                 ó@   — || _         || _        | j                  «        y r   )r   r   Úreopen)r   r   Ú
connections      r   r   zTransaction.__init__Y   s   € ØˆŒ
Ø%ˆÔØ�‰�r   c                 óJ   — | j                   }d | _         |j                  «        y r   )Ú_cursorr   )r   r6   s     r   r   zTransaction.close^   s   € Ø—,‘,ˆØˆŒØ�‰�r   c                 óª  — | j                   �| j                  «        	 | j                  j                  «       | _         y # t        $ r0 | j
                  j                  s‚ t        j                  d d«       Y nw xY w| j
                  j                  rt        j                  d«       | j                  «        | j                  j                  «       | _         y )NzCursor creation failedzConnection lost, reconnecting)r6   r   r   r   r#   r   r   r   r$   r&   r'   r   s    r   r3   zTransaction.reopenc   s�   € Ø�<‰<Ð#Ø�J‰JŒLð	8Ø×+Ñ+×2Ñ2Ó4ˆDŒLØøÜò 	8Ø—:‘:×'Ò'Øä—‘˜Ð6Ö7ð		8úð �:‰:×ÒÜ�G‰GÐ3Ô4à�‰ÔØ×'Ñ'×.Ñ.Ó0ˆ�s   ž> ¾6A7Á6A7c                 óF   — | j                   j                  «        d | _        y r   )r   r   r6   r   s    r   r   zTransaction.reconnectv   s   € Ø×Ñ×"Ñ"Ô$Øˆ�r   c                 ó.   — t        | j                  |«      S r   )r,   r6   r-   s     r   r/   zTransaction.__getattr__z   s   € Ü�t—|‘| TÓ*Ð*r   )
r   r	   r
   r   r6   r   r   r3   r   r/   r   r   r   r1   r1   M   s'   „ ñð €Gòò
ò
1ò&ó+r   r1   c                   óÈ   — e Zd ZdZdj	                  «       ZdZdZdZdZ	dZ
dZdZdZeZeZdZd„ Zd	„ Zd
„ Zd„ Zd„ Zd„ Zd„ Zd„ Zd„ Zd„ Zd„ Zd„ Zd„ Zd„ Z d„ Z!d„ Z"d„ Z#d„ Z$y)ÚConnectionPoolaŠ  
    Represent a pool of connections to a DB-API 2.0 compliant database.

    @ivar connectionFactory: factory for connections, default to L{Connection}.
    @type connectionFactory: any callable.

    @ivar transactionFactory: factory for transactions, default to
        L{Transaction}.
    @type transactionFactory: any callable

    @ivar shutdownID: L{None} or a handle on the shutdown event trigger which
        will be used to stop the connection pool workers when the reactor
        stops.

    @ivar _reactor: The reactor which will be used to schedule startup and
        shutdown events.
    @type _reactor: L{IReactorCore} provider
    z-min max name noisy openfun reconnect good_sqlFé   é   Nzselect 1c                 ó:  — || _         t        j                  |«      | _        t	        | j                  dd«      dk7  rt        j                  d«       t	        | j                  dd«      dk  rt        j                  d«       |j                  d	d«      }|€dd
lm	} || _
        || _        || _        | j                  D ]  }d|› �}||v sŒt        | |||   «       ||= Œ! t        | j                  | j                   «      | _        t!        | j                  | j                   «      | _        i | _        ddlm}m} |j*                  | _        |j/                  | j                  | j                   «      | _        | j                  j1                  | j2                  «      | _        y)aÈ  
        Create a new L{ConnectionPool}.

        Any positional or keyword arguments other than those documented here
        are passed to the DB-API object when connecting. Use these arguments to
        pass database names, usernames, passwords, etc.

        @param dbapiName: an import string to use to obtain a DB-API compatible
            module (e.g. C{'pyPgSQL.PgSQL'})

        @keyword cp_min: the minimum number of connections in pool (default 3)

        @keyword cp_max: the maximum number of connections in pool (default 5)

        @keyword cp_noisy: generate informational log messages during operation
            (default C{False})

        @keyword cp_openfun: a callback invoked after every C{connect()} on the
            underlying DB-API object. The callback is passed a new DB-API
            connection object. This callback can setup per-connection state
            such as charset, timezone, etc.

        @keyword cp_reconnect: detect connections which have failed and reconnect
            (default C{False}). Failed connections may result in
            L{ConnectionLost} exceptions, which indicate the query may need to
            be re-sent.

        @keyword cp_good_sql: an sql query which should always succeed and change
            no state (default C{'select 1'})

        @keyword cp_reactor: use this reactor instead of the global reactor
            (added in Twisted 10.2).
        @type cp_reactor: L{IReactorCore} provider
        ÚapilevelNz2.0z'DB API module not DB API 2.0 compliant.Úthreadsafetyr   é   z+DB API module not sufficiently thread-safe.Ú
cp_reactor)ÚreactorÚcp_)Ú
threadableÚ
threadpool)Ú	dbapiNamer   ÚnamedModuleÚdbapir,   r   r'   ÚpopÚtwisted.internetrC   Ú_reactorÚconnargsÚconnkwÚCP_ARGSÚsetattrÚminÚmaxÚconnectionsÚtwisted.pythonrE   rF   ÚgetThreadIDÚthreadIDÚ
ThreadPoolÚcallWhenRunningÚ_startÚstartID)	r   rG   rM   rN   rC   ÚargÚcpArgrE   rF   s	            r   r   zConnectionPool.__init__¤   sK  € ðF #ˆŒÜ×(Ñ(¨Ó3ˆŒ
ä�4—:‘:˜z¨4Ó0°EÒ9Ü�G‰GÐ=Ô>ä�4—:‘:˜~¨qÓ1°AÒ5Ü�G‰GÐAÔBà—*‘*˜\¨4Ó0ˆØˆ?Ý0ØˆŒà ˆŒØˆŒà—<‘<ò 	"ˆCØ˜#˜�KˆEØ˜ŠÜ˜˜c 6¨%¡=Ô1Ø˜5‘Mð		"ô �t—x‘x §¡Ó*ˆŒÜ�t—x‘x §¡Ó*ˆŒð ˆÔ÷ 	:à"×.Ñ.ˆŒØ$×/Ñ/°·±¸$¿(¹(ÓCˆŒØ—}‘}×4Ñ4°T·[±[ÓAˆ�r   c                 ó0   — d | _         | j                  «       S r   )rZ   Ústartr   s    r   rY   zConnectionPool._startë   s   € ØˆŒØ�z‰z‹|Ðr   c                 ó¸   — | j                   sN| j                  j                  «        | j                  j	                  dd| j
                  «      | _        d| _         yy)z�
        Start the connection pool.

        If you are using the reactor normally, this function does *not*
        need to be called.
        ÚduringÚshutdownTN)ÚrunningrF   r^   rL   ÚaddSystemEventTriggerÚ
finalCloseÚ
shutdownIDr   s    r   r^   zConnectionPool.startï   sJ   € ð �|Š|Ø�O‰O×!Ñ!Ô#Ø"Ÿm™m×AÑAØ˜* d§o¡oóˆDŒOð  ˆD�Lð r   c                 óv   — t        j                  | j                  | j                  | j                  |g|¢­i |¤ŽS )aý  
        Execute a function with a database connection and return the result.

        @param func: A callable object of one argument which will be executed
            in a thread with a connection from the pool. It will be passed as
            its first argument a L{Connection} instance (whose interface is
            mostly identical to that of a connection object for your DB-API
            module of choice), and its results will be returned as a
            L{Deferred}. If the method raises an exception the transaction will
            be rolled back. Otherwise, the transaction will be committed.
            B{Note} that this function is B{not} run in the main thread: it
            must be threadsafe.

        @param args: positional arguments to be passed to func

        @param kw: keyword arguments to be passed to func

        @return: a L{Deferred} which will fire the return value of
            C{func(Transaction(...), *args, **kw)}, or a
            L{twisted.python.failure.Failure}.
        )r   ÚdeferToThreadPoolrL   rF   Ú_runWithConnection)r   ÚfuncÚargsÚkws       r   ÚrunWithConnectionz ConnectionPool.runWithConnectioný   s>   € ô, ×(Ñ(Ø�M‰M˜4Ÿ?™?¨D×,CÑ,CÀTð
ØLPò
ØTVñ
ð 	
r   c                 óê   — | j                  | «      }	  ||g|¢­i |¤Ž}|j                  «        |S # t        $ r8 	 |j                  «        ‚ # t        $ r t	        j
                  d d«       Y ‚ w xY ww xY w©Nr   )ÚconnectionFactoryr"   r#   r   r   r$   )r   ri   rj   rk   ÚconnÚresults         r   rh   z!ConnectionPool._runWithConnection  s~   € Ø×%Ñ% dÓ+ˆð		Ù˜$Ð, Ò,¨Ñ,ˆFØ�K‰KŒMØˆMøÜò 	ð1Ø—‘”ð øô !ò 1Ü—‘˜Ð/Õ0Øð1úð	ús,   “1 ±	A2»AÁA2ÁA.Á+A2Á-A.Á.A2c                 óv   — t        j                  | j                  | j                  | j                  |g|¢­i |¤ŽS )aÈ  
        Interact with the database and return the result.

        The 'interaction' is a callable object which will be executed in a
        thread using a pooled connection. It will be passed an L{Transaction}
        object as an argument (whose interface is identical to that of the
        database cursor for your DB-API module of choice), and its results will
        be returned as a L{Deferred}. If running the method raises an
        exception, the transaction will be rolled back. If the method returns a
        value, the transaction will be committed.

        NOTE that the function you pass is *not* run in the main thread: you
        may have to worry about thread-safety in the function you pass to this
        if it tries to use non-local objects.

        @param interaction: a callable object whose first argument is an
            L{adbapi.Transaction}.

        @param args: additional positional arguments to be passed to
            interaction

        @param kw: keyword arguments to be passed to interaction

        @return: a Deferred which will fire the return value of
            C{interaction(Transaction(...), *args, **kw)}, or a
            L{twisted.python.failure.Failure}.
        )r   rg   rL   rF   Ú_runInteraction)r   Úinteractionrj   rk   s       r   ÚrunInteractionzConnectionPool.runInteraction$  sE   € ô8 ×(Ñ(Ø�M‰MØ�O‰OØ× Ñ Øð	
ð
 ò
ð ñ
ð 	
r   c                 óB   —  | j                   | j                  g|¢­i |¤ŽS )aY  
        Execute an SQL query and return the result.

        A DB-API cursor which will be invoked with C{cursor.execute(*args,
        **kw)}. The exact nature of the arguments will depend on the specific
        flavor of DB-API being used, but the first argument in C{*args} be an
        SQL statement. The result of a subsequent C{cursor.fetchall()} will be
        fired to the L{Deferred} which is returned. If either the 'execute' or
        'fetchall' methods raise an exception, the transaction will be rolled
        back and a L{twisted.python.failure.Failure} returned.

        The C{*args} and C{**kw} arguments will be passed to the DB-API
        cursor's 'execute' method.

        @return: a L{Deferred} which will fire the return value of a DB-API
            cursor's 'fetchall' method, or a L{twisted.python.failure.Failure}.
        )ru   Ú	_runQuery©r   rj   rk   s      r   ÚrunQueryzConnectionPool.runQueryI  s%   € ð$ #ˆt×"Ñ" 4§>¡>Ð?°DÒ?¸BÑ?Ð?r   c                 óB   —  | j                   | j                  g|¢­i |¤ŽS )aK  
        Execute an SQL query and return L{None}.

        A DB-API cursor which will be invoked with C{cursor.execute(*args,
        **kw)}. The exact nature of the arguments will depend on the specific
        flavor of DB-API being used, but the first argument in C{*args} will be
        an SQL statement. This method will not attempt to fetch any results
        from the query and is thus suitable for C{INSERT}, C{DELETE}, and other
        SQL statements which do not return values. If the 'execute' method
        raises an exception, the transaction will be rolled back and a
        L{Failure} returned.

        The C{*args} and C{*kw} arguments will be passed to the DB-API cursor's
        'execute' method.

        @return: a L{Deferred} which will fire with L{None} or a
            L{twisted.python.failure.Failure}.
        )ru   Ú_runOperationrx   s      r   ÚrunOperationzConnectionPool.runOperation]  s'   € ð& #ˆt×"Ñ" 4×#5Ñ#5ÐC¸ÒCÀÑCÐCr   c                 ó  — | j                   r,| j                  j                  | j                   «       d| _         | j                  r,| j                  j                  | j                  «       d| _        | j	                  «        y)zC
        Close all pool connections and shutdown the pool.
        N)re   rL   ÚremoveSystemEventTriggerrZ   rd   r   s    r   r   zConnectionPool.closer  sV   € ð �?Š?Ø�M‰M×2Ñ2°4·?±?ÔCØ"ˆDŒOØ�<Š<Ø�M‰M×2Ñ2°4·<±<Ô@ØˆDŒLØ�‰Õr   c                 óè   — d| _         | j                  j                  «        d| _        | j                  j                  «       D ]  }| j                  |«       Œ | j                  j                  «        y)zE
        This should only be called by the shutdown trigger.
        NF)re   rF   Ústoprb   rS   ÚvaluesÚ_closeÚclear©r   rp   s     r   rd   zConnectionPool.finalClose~  s^   € ð ˆŒØ�‰×ÑÔØˆŒØ×$Ñ$×+Ñ+Ó-ò 	ˆDØ�K‰K˜Õð	à×Ñ×ÑÕ r   c                 ót  — | j                  «       }| j                  j                  |«      }|€Š| j                  r"t	        j
                  d| j                  › �«        | j                  j                  | j                  i | j                  ¤Ž}| j                  �| j                  |«       || j                  |<   |S )a•  
        Return a database connection when one becomes available.

        This method blocks and should be run in a thread from the internal
        threadpool. Don't call this method directly from non-threaded code.
        Using this method outside the external threadpool may exceed the
        maximum number of connections in the pool.

        @return: a database connection from the pool.
        zadbapi connecting: )rV   rS   Úgetr&   r   r'   rG   rI   r*   rM   rN   Úopenfun)r   Útidrp   s      r   r*   zConnectionPool.connect‰  s˜   € ð �m‰m‹oˆØ×Ñ×#Ñ# CÓ(ˆØˆ<Ø�zŠzÜ—‘Ð-¨d¯n©nÐ-=Ð>Ô?Ø%�4—:‘:×%Ñ% t§}¡}ÐD¸¿¹ÑDˆDØ�|‰|Ð'Ø—‘˜TÔ"Ø$(ˆD×Ñ˜SÑ!Øˆr   c                 ó¶   — | j                  «       }|| j                  j                  |«      urt        d«      ‚|�| j	                  |«       | j                  |= yy)a  
        Disconnect a database connection associated with this pool.

        Note: This function should only be used by the same thread which called
        L{ConnectionPool.connect}. As with C{connect}, this function is not
        used in normal non-threaded Twisted code.
        zwrong connection for threadN)rV   rS   r†   Ú	Exceptionr‚   )r   rp   rˆ   s      r   r%   zConnectionPool.disconnect   sY   € ð �m‰m‹oˆØ�t×'Ñ'×+Ñ+¨CÓ0Ñ0ÜÐ9Ó:Ð:ØÐØ�K‰K˜ÔØ× Ñ  Ñ%ð r   c                 óÌ   — | j                   r"t        j                  d| j                  › �«       	 |j	                  «        y # t
        $ r t        j                  d d«       Y y w xY w)Nzadbapi closing: zConnection close failed)r&   r   r'   rG   r   r#   r$   r„   s     r   r‚   zConnectionPool._close¯  sN   € Ø�:Š:Ü�G‰GÐ& t§~¡~Ð&6Ð7Ô8ð	5Ø�J‰J�LøÜò 	5Ü�G‰G�DÐ3Ö4ð	5ús   °A ÁA#Á"A#c                 ó.  — | j                  | «      }| j                  | |«      }	  ||g|¢­i |¤Ž}|j                  «        |j                  «        |S # t        $ r8 	 |j                  «        ‚ # t        $ r t        j                  d d«       Y ‚ w xY ww xY wrn   )ro   ÚtransactionFactoryr   r"   r#   r   r   r$   )r   rt   rj   rk   rp   Útransrq   s          r   rs   zConnectionPool._runInteraction·  s™   € Ø×%Ñ% dÓ+ˆØ×'Ñ'¨¨dÓ3ˆð
	Ù  Ð4¨Ò4°Ñ4ˆFØ�K‰KŒMØ�K‰KŒMØˆMøÜò 	ð1Ø—‘”ð øô !ò 1Ü—‘˜Ð/Õ0Øð1úð	ús/   ¥-A Á	BÁA.Á-BÁ.BÂBÂBÂBc                 óF   —  |j                   |i |¤Ž |j                  «       S r   )r    Úfetchall©r   rŽ   rj   rk   s       r   rw   zConnectionPool._runQueryÆ  s"   € Øˆ�‰�tÐ"˜rÒ"Ø�~‰~ÓÐr   c                 ó(   —  |j                   |i |¤Ž y r   )r    r‘   s       r   r{   zConnectionPool._runOperationÊ  s   € Øˆ�‰�tÐ"˜rÓ"r   c           	      ó¸   — | j                   | j                  | j                  | j                  | j                  | j
                  | j                  | j                  dœS )N©rG   rQ   rR   r&   r   r!   rM   rN   r”   r   s    r   Ú__getstate__zConnectionPool.__getstate__Í  sD   € àŸ™Ø—8‘8Ø—8‘8Ø—Z‘ZØŸ™ØŸ™ØŸ™Ø—k‘kñ	
ð 		
r   c                 óz   — || _          | j                  | j                  g| j                  ¢­i | j                  ¤Ž y r   )Ú__dict__r   rG   rM   rN   )r   Ústates     r   Ú__setstate__zConnectionPool.__setstate__Ù  s-   € ØˆŒØˆ�‰�d—n‘nÐD t§}¡}ÒD¸¿¹ÓDr   )%r   r	   r
   r   ÚsplitrO   r&   rQ   rR   r.   r‡   r   r!   rb   r   ro   r1   r�   re   r   rY   r^   rl   rh   ru   ry   r|   r   rd   r*   r%   r‚   rs   rw   r{   r•   r™   r   r   r   r;   r;   ~   s³   „ ñð& >×CÑCÓE€Gà€EØ
€CØ
€CØ€DØ€GØ€IØ€Hà€GØ"ÐØ$Ðð €JòEBòNò ò
ò4ò#
òJ@ò(Dò*
ò	!òò.&ò5òò ò#ò

óEr   r;   N)r   rK   r   rT   r   r   rŠ   r   r   r1   r;   Ú__all__r   r   r   ú<module>rœ      sP   ðñ
õ %ß 'ô�Yô ÷4/ñ 4/÷n.+ñ .+÷b]Eñ ]Eð@ Ð*Ð
+�r   