
    hA                     ~    S r SSKJr  SSKJrJr   " S S\5      r " S S5      r " S S	5      r	 " S
 S5      r
S	S/rg)zg
An asynchronous mapping to U{DB-API
2.0<http://www.python.org/topics/database/DatabaseAPI-2.0.html>}.
    )threads)logreflectc                       \ rS rSrSrSrg)ConnectionLost   zV
This exception means that a db connection has been lost.  Client code may
try again.
 N)__name__
__module____qualname____firstlineno____doc____static_attributes__r	       `/root/1688_scrapy/alibaba-scraper/venv/lib/python3.13/site-packages/twisted/enterprise/adbapi.pyr   r      s    r   r   c                   6    \ rS rSrSrS rS rS rS rS r	Sr
g	)

Connection   z
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                 >    Xl         S U l        U R                  5         g N)_pool_connection	reconnect)selfpools     r   __init__Connection.__init__   s    
r   c                     g r   r	   r   s    r   closeConnection.close$   s     	r   c                    U R                   R                  (       d  U R                  R                  5         g  U R                  R                  5         U R                  R	                  5       nUR                  U R                   R                  5        UR                  5         U R                  R                  5         g ! [         a    [        R                  " S S5         Of = fU R                   R                  U R                  5        U R                   R                  (       a  [        R                  " S5        [        5       e)NRollback failedzConnection lost.)r   r   r   rollbackcursorexecutegood_sqlr    commitBaseExceptionr   err
disconnectnoisymsgr   )r   curss     r   r$   Connection.rollback.   s    zz##%%'	-%%'##**,DLL,,-JJL##% 	-GGD+,	- 	

d../::GG&'s   BB< <!C C c                     U R                   b%  U R                  R                  U R                   5        U R                  R                  5       U l         g r   )r   r   r+   connectr   s    r   r   Connection.reconnectD   s;    'JJ!!$"2"23::--/r   c                 .    [        U R                  U5      $ r   )getattrr   r   names     r   __getattr__Connection.__getattr__I   s    t''..r   )r   r   N)r
   r   r   r   r   r   r    r$   r   r7   r   r	   r   r   r   r      s     
,0
/r   r   c                   :    \ rS rSrSrSrS rS rS rS r	S r
S	rg)
TransactionM   a  
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                 <    Xl         X l        U R                  5         g r   )r   r   reopen)r   r   
connections      r   r   Transaction.__init__Y   s    
%r   c                 J    U R                   nS U l         UR                  5         g r   )_cursorr    )r   rA   s     r   r    Transaction.close^   s    ,,r   c                    U R                   b  U R                  5          U R                  R                  5       U l         g ! [         a6    U R
                  R                  (       d  e [        R                  " S S5         Of = fU R
                  R                  (       a  [        R                  " S5        U R                  5         U R                  R                  5       U l         g )NzCursor creation failedzConnection lost, reconnecting)rA   r    r   r%   r)   r   r   r   r*   r,   r-   r   s    r   r=   Transaction.reopenc   s    <<#JJL	8++224DL 	8::''67		8 ::GG34''..0s   ? =A?>A?c                 F    U R                   R                  5         S U l        g r   )r   r   rA   r   s    r   r   Transaction.reconnectv   s    ""$r   c                 .    [        U R                  U5      $ r   )r4   rA   r5   s     r   r7   Transaction.__getattr__z   s    t||T**r   )r   rA   r   )r
   r   r   r   r   rA   r   r    r=   r   r7   r   r	   r   r   r:   r:   M   s'     G

1&+r   r:   c                       \ rS rSrSrSR                  5       rSrSrSr	Sr
SrSrSrSr\r\rSrS	 rS
 rS rS rS rS rS rS rS rS rS rS rS r S r!S r"S r#S r$S r%Sr&g)ConnectionPool~   aV  
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                 @   Xl         [        R                  " U5      U l        [	        U R                  SS5      S:w  a  [
        R                  " S5        [	        U R                  SS5      S:  a  [
        R                  " S5        UR                  S	S5      nUc  SS
KJ	n  X@l
        X l        X0l        U R                   H  nSU 3nXc;   d  M  [        XX6   5        X6	 M!     [        U R                  U R                   5      U l        [!        U R                  U R                   5      U l        0 U l        SSKJnJn  UR*                  U l        UR/                  U R                  U R                   5      U l        U R                  R1                  U R2                  5      U l        g)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dbapir4   r   r-   poptwisted.internetrS   _reactorconnargsconnkwCP_ARGSsetattrminmaxconnectionstwisted.pythonrU   rV   getThreadIDthreadID
ThreadPoolcallWhenRunning_startstartID)	r   rW   r]   r^   rS   argcpArgrU   rV   s	            r   r   ConnectionPool.__init__   s6   F #((3
4::z40E9GG=>4::~q1A5GGAB**\40?0 <<C#KE6=1M	   txx*txx*  	:"..$//$((C}}44T[[Ar   c                 0    S U l         U R                  5       $ r   )rj   startr   s    r   ri   ConnectionPool._start   s    zz|r   c                     U R                   (       dN  U R                  R                  5         U R                  R	                  SSU R
                  5      U l        SU l         gg)zp
Start the connection pool.

If you are using the reactor normally, this function does *not*
need to be called.
duringshutdownTN)runningrV   ro   r\   addSystemEventTrigger
finalClose
shutdownIDr   s    r   ro   ConnectionPool.start   sJ     ||OO!!#"mmAA*dooDO  DL r   c                 x    [         R                  " U R                  U R                  U R                  U/UQ70 UD6$ )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   deferToThreadPoolr\   rV   _runWithConnection)r   funcargskws       r   runWithConnection ConnectionPool.runWithConnection   s>    , ((MM4??D,C,CT
LP
TV
 	
r   c                     U R                  U 5      n U" U/UQ70 UD6nUR                  5         U$ ! [         a:     UR                  5         e ! [         a    [        R
                  " S S5         e f = ff = fNr#   )connectionFactoryr(   r)   r$   r   r*   )r   r|   r}   r~   connresults         r   r{   !ConnectionPool._runWithConnection  s~    %%d+		$,,,FKKMM 	1  ! 1/01	s,   1 
A5AA5!A1.A50A11A5c                 x    [         R                  " U R                  U R                  U R                  U/UQ70 UD6$ )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   rz   r\   rV   _runInteraction)r   interactionr}   r~   s       r   runInteractionConnectionPool.runInteraction$  sE    8 ((MMOO  	

 
 
 	
r   c                 B    U R                   " U R                  /UQ70 UD6$ )a  
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}.
)r   	_runQueryr   r}   r~   s      r   runQueryConnectionPool.runQueryI  s#    $ ""4>>?D?B??r   c                 B    U R                   " U R                  /UQ70 UD6$ )a  
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}.
)r   _runOperationr   s      r   runOperationConnectionPool.runOperation]  s%    & ""4#5#5CCCCr   c                    U R                   (       a,  U R                  R                  U R                   5        SU l         U R                  (       a,  U R                  R                  U R                  5        SU l        U R	                  5         g)z3
Close all pool connections and shutdown the pool.
N)rw   r\   removeSystemEventTriggerrj   rv   r   s    r   r    ConnectionPool.closer  sV     ??MM224??C"DO<<MM224<<@DLr   c                     SU l         U R                  R                  5         SU l        U R                  R                  5        H  nU R                  U5        M     U R                  R                  5         g)z5
This should only be called by the shutdown trigger.
NF)rw   rV   stoprt   rc   values_closeclearr   r   s     r   rv   ConnectionPool.finalClose~  sY     $$++-DKK . r   c                    U R                  5       nU R                  R                  U5      nUc  U R                  (       a#  [        R
                  " SU R                   35        U R                  R                  " U R                  0 U R                  D6nU R                  b  U R                  U5        X R                  U'   U$ )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: )rf   rc   getr,   r   r-   rW   rY   r1   r]   r^   openfun)r   tidr   s      r   r1   ConnectionPool.connect  s     mmo##C(<zz-dnn-=>?::%%t}}DDD||'T"$(S!r   c                     U R                  5       nXR                  R                  U5      La  [        S5      eUb  U R	                  U5        U R                  U	 gg)z
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)rf   rc   r   	Exceptionr   )r   r   r   s      r   r+   ConnectionPool.disconnect  sW     mmo''++C009::KK  % r   c                     U R                   (       a#  [        R                  " SU R                   35         UR	                  5         g ! [
         a    [        R                  " S S5         g f = f)Nzadbapi closing: zConnection close failed)r,   r   r-   rW   r    r)   r*   r   s     r   r   ConnectionPool._close  sN    ::GG&t~~&678	5JJL 	5GGD34	5s   A !A+*A+c                 2   U R                  U 5      nU R                  X5      n U" U/UQ70 UD6nUR                  5         UR                  5         U$ ! [         a:     UR                  5         e ! [         a    [        R                  " S S5         e f = ff = fr   )r   transactionFactoryr    r(   r)   r$   r   r*   )r   r   r}   r~   r   transr   s          r   r   ConnectionPool._runInteraction  s    %%d+''3
	 444FKKMKKMM 	1  ! 1/01	s/   -A 
BA.-B.!BBBBc                 F    UR                   " U0 UD6  UR                  5       $ r   )r&   fetchallr   r   r}   r~   s       r   r   ConnectionPool._runQuery  s     t"r"~~r   c                 (    UR                   " U0 UD6  g r   )r&   r   s       r   r   ConnectionPool._runOperation  s    t"r"r   c           	          U R                   U R                  U R                  U R                  U R                  U R
                  U R                  U R                  S.$ )NrW   ra   rb   r,   r   r'   r]   r^   r   r   s    r   __getstate__ConnectionPool.__getstate__  sD    8888ZZkk	
 		
r   c                 x    Xl         U R                  " U R                  /U R                  Q70 U R                  D6  g r   )__dict__r   rW   r]   r^   )r   states     r   __setstate__ConnectionPool.__setstate__  s)    dnnDt}}DDr   )r   r\   r]   rc   r^   rY   rW   rb   ra   rt   rw   rj   rf   rV   )'r
   r   r   r   r   splitr_   r,   ra   rb   r6   r   r   r'   rt   r   r   r:   r   rw   r   ri   ro   r   r{   r   r   r   r    rv   r1   r+   r   r   r   r   r   r   r   r	   r   r   rJ   rJ   ~   s    & >CCEGE
C
CDGIHG"$ JEBN 
4#
J@(D*
	!.&5 #

Er   rJ   N)r   r[   r   rd   r   r   r   r   r   r:   rJ   __all__r	   r   r   <module>r      sP   
 % 'Y 4/ 4/n.+ .+b]E ]E@ *
+r   