ó o©ÐOc@sºdZddlZddlmZddlmZmZddlmZddl m Z de fd„ƒYZ d e fd „ƒYZd fd „ƒYZd fd„ƒYZd d gZdS(sg An asynchronous mapping to U{DB-API 2.0}. iÿÿÿÿN(tthreads(treflecttlog(t deprecated(tVersiontConnectionLostcBseZdZRS(sb This exception means that a db connection has been lost. Client code may try again. (t__name__t __module__t__doc__(((s=/usr/lib/python2.7/dist-packages/twisted/enterprise/adbapi.pyRst ConnectioncBs;eZdZd„Zd„Zd„Zd„Zd„ZRS(s  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 Connection knows about its pool and also handle reconnecting should when the real connection dies. cCs ||_d|_|jƒdS(N(t_pooltNonet _connectiont reconnect(tselftpool((s=/usr/lib/python2.7/dist-packages/twisted/enterprise/adbapi.pyt__init__#s  cCsdS(N((R((s=/usr/lib/python2.7/dist-packages/twisted/enterprise/adbapi.pytclose(scCsÁ|jjs|jjƒdSyN|jjƒ|jjƒ}|j|jjƒ|jƒ|jjƒdSWnt j ddƒnX|jj |jƒ|jj r´t jdƒntƒ‚dS(NsRollback failedsConnection lost.(R R R trollbacktcursortexecutetgood_sqlRtcommitRterrR t disconnecttnoisytmsgR(Rtcurs((s=/usr/lib/python2.7/dist-packages/twisted/enterprise/adbapi.pyR2s       cCs;|jdk r%|jj|jƒn|jjƒ|_dS(N(R R R Rtconnect(R((s=/usr/lib/python2.7/dist-packages/twisted/enterprise/adbapi.pyR HscCst|j|ƒS(N(tgetattrR (Rtname((s=/usr/lib/python2.7/dist-packages/twisted/enterprise/adbapi.pyt __getattr__Ms(RRRRRRR R(((s=/usr/lib/python2.7/dist-packages/twisted/enterprise/adbapi.pyR s    t TransactioncBsAeZdZdZd„Zd„Zd„Zd„Zd„Z RS(sA lightweight wrapper for a DB-API 'cursor' object. Relays attribute access to the DB cursor. That is, you can call execute(), fetchall(), etc., and they will be called on the underlying DB-API cursor object. Attributes will also be retrieved from there. cCs ||_||_|jƒdS(N(R R treopen(RRt connection((s=/usr/lib/python2.7/dist-packages/twisted/enterprise/adbapi.pyR[s  cCs |j}d|_|jƒdS(N(t_cursorR R(RR#((s=/usr/lib/python2.7/dist-packages/twisted/enterprise/adbapi.pyR`s  cCsž|jdk r|jƒny|jjƒ|_dSWn)|jjsN‚qbtjddƒnX|jj r~tj dƒn|jƒ|jjƒ|_dS(NsCursor creation failedsConnection lost, reconnecting( R#R RR RR R RRRR(R((s=/usr/lib/python2.7/dist-packages/twisted/enterprise/adbapi.pyR!es    cCs|jjƒd|_dS(N(R R R R#(R((s=/usr/lib/python2.7/dist-packages/twisted/enterprise/adbapi.pyR xs cCst|j|ƒS(N(RR#(RR((s=/usr/lib/python2.7/dist-packages/twisted/enterprise/adbapi.pyR|sN( RRRR R#RRR!R R(((s=/usr/lib/python2.7/dist-packages/twisted/enterprise/adbapi.pyR Qs    tConnectionPoolcBsþeZdZdjƒZeZdZdZdZ dZ eZ dZ eZeZeZdZd„Zd„Zd„Zd„Zd „Zd „Zd „Zd „Zd „Zd„Zd„Zd„Zd„Z d„Z!d„Z"d„Z#d„Z$d„Z%RS(sŠ 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: C{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 s-min max name noisy openfun reconnect good_sqliisselect 1c Os||_tj|ƒ|_t|jdd ƒdkrFtjdƒnt|jddƒdkrqtjdƒn|jdd ƒ}|d kr¢d d l m }n||_ ||_ ||_ xE|jD]:}d |}||krÇt||||ƒ||=qÇqÇWt|j|jƒ|_t|j|jƒ|_i|_d d lm}d d l}|j|_|j|j|jƒ|_|j j|jƒ|_d S(sCCreate a new 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. 'pyPgSQL.PgSQL') @param cp_min: the minimum number of connections in pool (default 3) @param cp_max: the maximum number of connections in pool (default 5) @param cp_noisy: generate informational log messages during operation (default False) @param cp_openfun: a callback invoked after every 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. @param cp_reconnect: detect connections which have failed and reconnect (default False). Failed connections may result in ConnectionLost exceptions, which indicate the query may need to be re-sent. @param cp_good_sql: an sql query which should always succeed and change no state (default 'select 1') @param cp_reactor: use this reactor instead of the global reactor (added in Twisted 10.2). @type cp_reactor: L{IReactorCore} provider tapilevels2.0s'DB API module not DB API 2.0 compliant.t threadsafetyiis+DB API module not sufficiently thread-safe.t cp_reactoriÿÿÿÿ(treactorscp_%s(t threadpoolN(t dbapiNameRt namedModuletdbapiRR RRtpopttwisted.internetR(t_reactortconnargstconnkwtCP_ARGStsetattrtmintmaxt connectionsttwisted.pythonR)tthreadt get_identtthreadIDt ThreadPooltcallWhenRunningt_starttstartID( RR*R0R1R(targtcp_argR)R8((s=/usr/lib/python2.7/dist-packages/twisted/enterprise/adbapi.pyR¦s2$          cCsd|_|jƒS(N(R R>tstart(R((s=/usr/lib/python2.7/dist-packages/twisted/enterprise/adbapi.pyR=ïs cCsD|js@|jjƒ|jjdd|jƒ|_t|_ndS(s Start the connection pool. If you are using the reactor normally, this function does *not* need to be called. tduringtshutdownN(trunningR)RAR/taddSystemEventTriggert finalCloset shutdownIDtTrue(R((s=/usr/lib/python2.7/dist-packages/twisted/enterprise/adbapi.pyRAôs    cOs2ddlm}tj||j|j|||ŽS(s× 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 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 Deferred which will fire the return value of C{func(Transaction(...), *args, **kw)}, or a Failure. iÿÿÿÿ(R((R.R(RtdeferToThreadPoolR)t_runWithConnection(RtfunctargstkwR(((s=/usr/lib/python2.7/dist-packages/twisted/enterprise/adbapi.pytrunWithConnectionsc OsŠ|j|ƒ}y$||||Ž}|jƒ|SWnPtjƒ\}}}y|jƒWntjddƒnX|||‚nXdS(NsRollback failed(tconnectionFactoryRtsystexc_infoRRRR ( RRKRLRMtconntresulttexcTypetexcValuet excTraceback((s=/usr/lib/python2.7/dist-packages/twisted/enterprise/adbapi.pyRJs cOs2ddlm}tj||j|j|||ŽS(s± 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 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 'interaction(Transaction(...), *args, **kw)', or a Failure. iÿÿÿÿ(R((R.R(RRIR)t_runInteraction(Rt interactionRLRMR(((s=/usr/lib/python2.7/dist-packages/twisted/enterprise/adbapi.pytrunInteraction,scOs|j|j||ŽS(sExecute an SQL query and return the result. A DB-API cursor will will be invoked with 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 *args be an SQL statement. The result of a subsequent cursor.fetchall() will be fired to the Deferred which is returned. If either the 'execute' or 'fetchall' methods raise an exception, the transaction will be rolled back and a Failure returned. The *args and **kw arguments will be passed to the DB-API cursor's 'execute' method. @return: a Deferred which will fire the return value of a DB-API cursor's 'fetchall' method, or a Failure. (RYt _runQuery(RRLRM((s=/usr/lib/python2.7/dist-packages/twisted/enterprise/adbapi.pytrunQueryOscOs|j|j||ŽS(síExecute an SQL query and return None. A DB-API cursor will will be invoked with 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 *args will be an SQL statement. This method will not attempt to fetch any results from the query and is thus suitable for INSERT, 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 Failure returned. The args and kw arguments will be passed to the DB-API cursor's 'execute' method. return: a Deferred which will fire None or a Failure. (RYt _runOperation(RRLRM((s=/usr/lib/python2.7/dist-packages/twisted/enterprise/adbapi.pyt runOperationcscCs^|jr(|jj|jƒd|_n|jrP|jj|jƒd|_n|jƒdS(sC Close all pool connections and shutdown the pool. N(RGR/tremoveSystemEventTriggerR R>RF(R((s=/usr/lib/python2.7/dist-packages/twisted/enterprise/adbapi.pyRvs    cCsWd|_|jjƒt|_x$|jjƒD]}|j|ƒq/W|jj ƒdS(s3This should only be called by the shutdown trigger.N( R RGR)tstoptFalseRDR6tvaluest_closetclear(RRR((s=/usr/lib/python2.7/dist-packages/twisted/enterprise/adbapi.pyRF‚s    cCs³|jƒ}|jj|ƒ}|dkr¯|jretjd|j|jpNd|j pZdfƒn|j j |j|j Ž}|j dkrŸ|j |ƒn||j|  I     #        (RRPR.RR7RRttwisted.python.deprecateRttwisted.python.versionsRRhRtobjectR R R$t__all__(((s=/usr/lib/python2.7/dist-packages/twisted/enterprise/adbapi.pyts 7/ÿd