U Dx`W_<@sddlmZddlmZddlZddlmZmZmZm Z m Z m Z m Z m Z mZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZmZm Z m!Z!m"Z"m#Z#m$Z$m%Z%m&Z&m'Z'm(Z(m)Z)m*Z*m+Z+m,Z,m-Z-m.Z.m/Z/m0Z0m1Z1m2Z2m3Z3ddl4m5Z5ddl6m7Z7m8Z8m9Z9m:Z:m;Z;mZ>ddl?m@Z@mAZAmBZBdd lCmDZDeDd ZE[Dd ZFe"ZGdddZHdZIdZJdZKGdddZLGddde5ZMddZNGdddZOe=e7e@eHeBe)_c arraysizepg8000 paramstyle_context _row_iter _input_oids)self connectionrXrPrPrQ__init__\s zCursor.__init__cCs|SNrPr\rPrPrQ __enter__iszCursor.__enter__cCs |dSr_)close)r\exc_type exc_value tracebackrPrPrQ__exit__lszCursor.__exit__cCstddd|jS)Nz'DB-API extension cursor.connection used stacklevel)rrUr`rPrPrQr]os zCursor.connectioncCs|j}|dkrdS|jS)N)rY row_count)r\contextrPrPrQrowcounttszCursor.rowcountcCs|Sr_)_getDescriptionr`rPrPrQ|zCursor.c Csf|j}|dkrdS|j}|dkr$dSt|dkr4dSg}|D]$}||d|ddddddfq<|S)NrnameZtype_oid)rYcolumnslenappend)r\rlrow_descrrcolrPrPrQrn~s zCursor._getDescriptionrPc Cs,z^|jjs|jjs|jdt|j||\}}|jj|||j|d|_t|jj |_ d|_Wnt k r}z2|jdkrt dn|jj dkrt dn|W5d}~XYnttk r }zT|jd}t|tr|d}|dkrt } n|d krt} nt} | |nt|W5d}~XYnXg|_|S) aeExecutes a database operation. Parameters may be provided as a sequence, or as a mapping, depending upon the value of :data:`pg8000.paramstyle`. This method is part of the `DBAPI 2.0 specification `_. :param operation: The SQL statement to execute. :param args: If :data:`paramstyle` is ``qmark``, ``numeric``, or ``format``, this argument should be an array of parameters to bind into the statement. If :data:`paramstyle` is ``named``, the argument should be a dict mapping of parameters. If the :data:`paramstyle` is ``pyformat``, the argument value may be either an array or a mapping. :param stream: This is a pg8000 extension for use with the PostgreSQL `COPY `_ command. For a COPY FROM the parameter must be a readable file-like object, and for COPY TO it must be writable. .. versionadded:: 1.9.11 begin transaction)valsZ input_oidsstreamN Cursor closedconnection is closedrC2800023505)rUin_transaction autocommitexecute_unnamedr:rXr[rYiterrowsrZAttributeErrorr=_sockr;args isinstancedictr4r8Z input_types) r\ operationrry statementrxemsg response_codeclsrPrPrQexecutesF         zCursor.executecCsDg}|D]}|||||jjqd|kr4dnt||j_|S)a1Prepare a database operation, and then execute it against all parameter sequences or mappings provided. This method is part of the `DBAPI 2.0 specification `_. :param operation: The SQL statement to execute :param parameter_sets: A sequence of parameters to execute the statement with. The values in the sequence should be sequences or mappings of parameters, the same as the args argument of the :meth:`execute` method. rj)rrtrYrksum)r\rZ param_setsZ rowcounts parametersrPrPrQ executemanys  zCursor.executemanycCsZz t|WStk r YdStk r:tdYntk rTtdYnXdS)a Fetch the next row of a query result set. This method is part of the `DBAPI 2.0 specification `_. :returns: A row as a sequence of field values, or ``None`` if no more rows are available. N#attempting to use unexecuted cursor)next StopIteration TypeErrorr8rr`rPrPrQfetchones   zCursor.fetchonecCs@ztt||dkr|jn|WStk r:tdYnXdS)aFetches the next set of rows of a query result. This method is part of the `DBAPI 2.0 specification `_. :param size: The number of rows to fetch when called. If not provided, the :attr:`arraysize` attribute value is used instead. :returns: A sequence, each entry of which is a sequence of field values making up a row. If no more rows are available, an empty sequence will be returned. Nr)tuplerrVrr8)r\numrPrPrQ fetchmanys zCursor.fetchmanycCs,z t|WStk r&tdYnXdS)a$Fetches all remaining rows of a query result. This method is part of the `DBAPI 2.0 specification `_. :returns: A sequence, each entry of which is a sequence of field values making up a row. rN)rrr8r`rPrPrQfetchalls  zCursor.fetchallcCs d|_dS)zCloses the cursor. This method is part of the `DBAPI 2.0 specification `_. N)rUr`rPrPrQrb$sz Cursor.closecCs|S)zoA cursor object is iterable to retrieve the rows from a query. This is a DBAPI 2.0 extension. rPr`rPrPrQ__iter__,szCursor.__iter__c Gsdg}|D]P}t|tr|}n2z|jj|\}}Wntk rLtjj}YnX||q||_ dS)z`_, however, it is not implemented by pg8000. NrP)r\rcolumnrPrPrQ setoutputsizeCszCursor.setoutputsizec Csz t|jWStk r6|jdkr0tdnYnTtk r}z6|jdkrZtdnt|jjdkrttdn|W5d}~XYnXdS)NzA query hasn't been issued.rz no result set)rrZrrYr8rrsrr)r\rrPrPrQ__next__Js      zCursor.__next__)N)rPN)N)N)__name__ __module__ __qualname__r^rarfpropertyr]rm descriptionrnrrrrrrbrrrrrPrPrPrQrT[s&     B  rTcseZdZeddZeddZeddZeddZeddZeddZ eddZ ed dZ ed dZ fd d Z d dZddZeddZddZddZd(ddZddZddZddZd d!Zd)d"d#Zd*d$d%Zd&d'ZZS)+rNcCs |tSr_) _getErrorr9r`rPrPrQro^rpzConnection.cCs |tSr_)rr<r`rPrPrQro_rpcCs |tSr_)rr=r`rPrPrQro`rpcCs |tSr_)rr;r`rPrPrQroarpcCs |tSr_)rr7r`rPrPrQrobrpcCs |tSr_)rr4r`rPrPrQrocrpcCs |tSr_)rr5r`rPrPrQrodrpcCs |tSr_)rr8r`rPrPrQroerpcCs |tSr_)rr6r`rPrPrQrogrpc sztj||Wnptk r}zR|jd}t|trj|d}|dkrNt}n|dkr\t}nt}||nt|W5d}~XYnXt |dd|_ dS)Nrr|r}r~named)rX) superr^r;rrrr=r4r8rT _run_cursor)r\rkwargsrrrr __class__rPrQr^is   zConnection.__init__cCstd|jdd|S)Nz#DB-API extension connection.%s usedrgrh)rr)r\errorrPrPrQr~szConnection._getErrorcCst|S)zCreates a :class:`Cursor` object bound to this connection. This function is part of the `DBAPI 2.0 specification `_. )rTr`rPrPrQcursorszConnection.cursorcCs |jSr_)rrnr`rPrPrQrszConnection.descriptioncCs|ddS)zCommits the current database transaction. This function is part of the `DBAPI 2.0 specification `_. commitN)rr`rPrPrQrszConnection.commitcCs|js dS|ddS)zRolls back the current database transaction. This function is part of the `DBAPI 2.0 specification `_. Nrollback)rrr`rPrPrQrszConnection.rollbackNcKs |jj|||dt|jjjS)N)ry)rrrrYr)r\ZsqlryparamsrPrPrQrunszConnection.runcCs t||Sr_)PreparedStatement)r\rrPrPrQprepareszConnection.preparecCs |||fS)a)Create a Transaction IDs (only global_transaction_id is used in pg) format_id and branch_qualifier are not used in postgres global_transaction_id may be any string identifier supported by postgres returns a tuple (format_id, global_transaction_id, branch_qualifier)rP)r\Z format_idZglobal_transaction_idZbranch_qualifierrPrPrQxidszConnection.xidcCs||_|jr|ddS)a.Begins a TPC transaction with the given transaction ID xid. This method should be called outside of a transaction (i.e. nothing may have executed since the last .commit() or .rollback()). Furthermore, it is an error to call .commit() or .rollback() within the TPC transaction. A ProgrammingError is raised, if the application calls .commit() or .rollback() during an active TPC transaction. This function is part of the `DBAPI 2.0 specification `_. rwN)_xidrr)r\rrPrPrQ tpc_begins zConnection.tpc_begincCsd|jdf}||dS)aPerforms the first phase of a transaction started with .tpc_begin(). A ProgrammingError is be raised if this method is called outside of a TPC transaction. After calling .tpc_prepare(), no statements can be executed until .tpc_commit() or .tpc_rollback() have been called. This function is part of the `DBAPI 2.0 specification `_. zPREPARE TRANSACTION '%s';r>N)rr)r\qrPrPrQ tpc_prepares zConnection.tpc_preparecCsl|dkr|j}|dkrtdz:|j}d|_||krN|d|dfn|W5||_Xd|_dS)a%When called with no arguments, .tpc_commit() commits a TPC transaction previously prepared with .tpc_prepare(). If .tpc_commit() is called prior to .tpc_prepare(), a single phase commit is performed. A transaction manager may choose to do this if only a single resource is participating in the global transaction. When called with a transaction ID xid, the database commits the given transaction. If an invalid transaction ID is provided, a ProgrammingError will be raised. This form should be called outside of a transaction, and is intended for use in recovery. On return, the TPC transaction is ended. This function is part of the `DBAPI 2.0 specification `_. Nz.Cannot tpc_commit() without a TPC transaction!TzCOMMIT PREPARED '%s';r>)rr8r tpc_recoverrrr\rprevious_autocommit_moderPrPrQ tpc_commits  zConnection.tpc_commitcCsl|dkr|j}|dkrtdz:|j}d|_||krN|d|dfn|W5||_Xd|_dS)aDWhen called with no arguments, .tpc_rollback() rolls back a TPC transaction. It may be called before or after .tpc_prepare(). When called with a transaction ID xid, it rolls back the given transaction. If an invalid transaction ID is provided, a ProgrammingError is raised. This form should be called outside of a transaction, and is intended for use in recovery. On return, the TPC transaction is ended. This function is part of the `DBAPI 2.0 specification `_. Nz9Cannot tpc_rollback() without a TPC prepared transaction!TzROLLBACK PREPARED '%s';r>)rr8rrrrrrPrPrQ tpc_rollbacks  zConnection.tpc_rollbackcsBz4j}d_}|dfdd|DWS|_XdS)zReturns a list of pending transaction IDs suitable for use with .tpc_commit(xid) or .tpc_rollback(xid). This function is part of the `DBAPI 2.0 specification `_. Tz!select gid FROM pg_prepared_xactscsg|]}d|ddqS)r)r).0rowr`rPrQ $sz*Connection.tpc_recover..N)rrr)r\rZcursrPr`rQrs zConnection.tpc_recover)N)N)N)rrrrr9r<r=r;r7r4r5r8r6r^rrrrrrrrrrrrr __classcell__rPrPrrQrN[s4             % "rNc spd}d}d}d}d}d}d}gg}|} d} t|D]\} } | dt|kr\|| d} nd} | |kr| dkr|| | d kr|} q|} nj| d kr|| |} nR| d kr|| | d kr|} n2| d kr| d kr| d kr|} dn || nT| |kr>| dkr0|rd}n| dkr,d}n|} || n| |krb| d krV|} || n| |kr| dkr| dkr|} || n| |kr.d| 7<| dks| sP| dkrP|} z2ddd}|dt|dd=Wn,tk r*|dttYnXn"| |krP|| | dkrP|} | } q4fdd}d||fS)Nrr>rgF'E"-:z:=rT\rjr$ cstfddDS)Nc3s|]}|VqdSr_rP)rprrPrQ ysz2to_statement..make_vals..)rrZ placeholdersrrQ make_valsxszto_statement..make_vals) enumeratersrtisalnumindexstr ValueErrorjoin)queryZOUTSIDEZ INSIDE_SQZ INSIDE_QIZ INSIDE_ESZ INSIDE_PNZ INSIDE_COZin_quote_escapeZ output_querystateZprev_cicZnext_cZpidxrrPrrQ to_statement)s                  rc@s$eZdZddZddZddZdS)rcCs&||_||_t|\|_|_i|_dSr_)conrrr make_argsname_map)r\rrrPrPrQr^szPreparedStatement.__init__c Ks|j||\}}z|j|\}}}Wn4tk r^|j|j|\}}}|j|<YnXz4|jjs~|jjs~|j d|j |||||_ WnPt k r}z2|jdkrt dn|jjdkrt dn|W5d}~XYnXt|j jS)Nrwrzr{)rZ make_paramsrrrZprepare_statementrrrrZ execute_namedrYrr=rrr)r\rxrrZname_binruZ input_funcsrrPrPrQrs,      zPreparedStatement.runcCs,|jD]\}}}|j|q d|_dSr_)rvaluesrZclose_prepared_statement)r\Zstatement_name_binrrPrPrQrbszPreparedStatement.closeN)rrrr^rrbrPrPrPrQr~sr) rANrBNNNNNTNN)Q itertoolsrwarningsrrWZpg8000.convertersrrrrrr r r r r rrrrrrrrrrrrrrrrrrr r!r"r#r$r%r&r'r(r)r*r+r,r-r.r/r0r1Z pg8000.corer2Z pg8000.dbapir3r4r5r6r7r8r9r:Zpg8000.exceptionsr;r<r=_versionr? __version__ __author__ZROWIDrRZapilevelZ threadsafetyrXrTrNrr__all__rPrPrPrQs   (    OU'