Your IP : 216.73.216.86


Current Path : /lib/fm-agent/dependencies/pg8000/__pycache__/
Upload File :
Current File : //lib/fm-agent/dependencies/pg8000/__pycache__/core.cpython-36.pyc

3

q��i�]�'@sPddlZddlmZddlmZddlZddlZddlmZddlm	Z	ddl
mZddlm
Z
mZddlmZmZdd	lmZdd
lmZmZmZmZmZmZmZddlmZddlmZdd
l m!Z!ddl"m#Z#ddlm$Z$ddl%Z%ddl&Z&dZ'yddl(m)Z)Wne*k
�rYnXed�Z+Gdd�dej,�Z-e-�Z.Gdd�de/�Z0dd�Z1e1d�\Z2Z3e1d�\Z4Z5e1d�\Z6Z7e1d�\Z8Z9e1d�\Z:Z;e1d�\Z<Z=e1d�\Z>Z?e1d�\Z@ZAe1d �\ZBZCe1d!�\ZDZEe1d"�\ZFZGe1d#�\ZHZIe1d$�\ZJZKe$d%�d�\ZLZMd�\ZNZOd�\ZPZQGd*d+�d+eR�ZSGd,d-�d-eR�ZTGd.d/�d/eT�ZUGd0d1�d1eT�ZVGd2d3�d3eV�ZWGd4d5�d5eV�ZXGd6d7�d7eV�ZYGd8d9�d9eV�ZZGd:d;�d;eV�Z[Gd<d=�d=eV�Z\Gd>d?�d?e\�Z]Gd@dA�dAe[�Z^GdBdC�dCe[�Z_GdDdE�dEe[�Z`GdFdG�dGe�ZadHdI�ZbdJdK�ZcdLdM�ZddNdO�ZedPdQ�ZfdRdS�ZgdTdU�Zhe�rDeaZinejZidZkdVZledW�ZmedX�edY�fZndZd[�Zoejd\dVdV�Zpepjqe.d]�Zre!epjs��ZtejjuZud�Zvd�evdVZwd^d_�Zxd`da�Zydbdc�Zzddde�Z{dfdg�Z|dhdi�Z}ejj~jqe.d]�Zejj�jqe.d]�Z�djdk�Z�dldm�Z�dndo�Z�dpdq�Z�drds�Z�dtdu�Z�dvdw�Z�dxdy�Z�dzd{�Z�d|d}�Z�d~d�Z�d�d��Z�e�rnd�d��Z�nd�d��Z�d�d��Z�d�d��Z�ed��Z�ed��Z�d�d��Z�e2d�Z�ed��Z�d�d��Z�d�d��Z�Gd�d��d��Z�e�r�e�j�e�_ed��Z�ed��Z�ed��Z�ed��Z�ed��Z�ed��Z�ed��Z�ed��Z�ed��Z�ed��Z�ed��Z�ed��Z�ed��Z�ed��Z�ed��Z�ed��Z�ed��Z�ed�Z�ed��Z�ed��Z�ed��Z�ed��Z�ed��Z�ed��Z�ed��Z�ed��Z�ed��Z�ed��Z�ed��Z�ed��Z�e�e2d��Z�e�e2d��Z�e�e2d��Z�e�e2d��Z�ed��Z�ed��Z�ed��Z�ed��Z�ed��Z�ed��Z�ed��Z�ed��Z�ed��Z�ed�Z�ed��Z�ed��Z�ed��Z�ed��Z�ed��Z�ed��Z�ed��Z�e�e�ee�ed���e�ed���dgd���Z�Gd�d��d�e/�Z�Gd�d��d�e/�Z�d�d�d�d�d��Z�ddd�d�d�d�d�d�d�d�d�d�d�d�d�d�d�d�d�d�d�d�d�d�d�d�d�d�d�d�d؜Z�d�dڄZ�d�d܄Z�d�dބZ�d�d�Z�d�d�Z�d�d�Z�dS)��N)�	timedelta)�warn)�pack)�md5)�Decimal)�deque�defaultdict)�count�islice)�map)�b�PY2�
integer_types�next�	text_type�u�binary_type)�UUID)�deepcopy)�timegm)�LooseVersion)�StructzMathieu Fenniak)�loadsc@s$eZdZdd�Zdd�Zdd�ZdS)�UTCcCstS)N)�ZERO)�self�dt�r�-/usr/lib/fm-agent/dependencies/pg8000/core.py�	utcoffset>sz
UTC.utcoffsetcCsdS)Nrr)rrrrr�tznameAsz
UTC.tznamecCstS)N)r)rrrrr�dstDszUTC.dstN)�__name__�
__module__�__qualname__rr r!rrrrr<src@steZdZdZddd�Zdd�Zdd�Zd	d
�Zedd�e�Z	ed
d�e�Z
edd�e�Zdd�Zdd�Z
dd�ZdS)�Intervala
An Interval represents a measurement of time.  In PostgreSQL, an
    interval is defined in the measure of months, days, and microseconds; as
    such, the pg8000 interval type represents the same information.

    Note that values of the :attr:`microseconds`, :attr:`days` and
    :attr:`months` properties are independently measured and cannot be
    converted to each other.  A month may be 28, 29, 30, or 31 days, and a day
    may occasionally be lengthened slightly by a leap second.

    .. attribute:: microseconds

        Measure of microseconds in the interval.

        The microseconds value is constrained to fit into a signed 64-bit
        integer.  Any attempt to set a value too large or too small will result
        in an OverflowError being raised.

    .. attribute:: days

        Measure of days in the interval.

        The days value is constrained to fit into a signed 32-bit integer.
        Any attempt to set a value too large or too small will result in an
        OverflowError being raised.

    .. attribute:: months

        Measure of months in the interval.

        The months value is constrained to fit into a signed 32-bit integer.
        Any attempt to set a value too large or too small will result in an
        OverflowError being raised.
    rcCs||_||_||_dS)N)�microseconds�days�months)rr&r'r(rrr�__init__mszInterval.__init__cCs@t|t�std��n(t|ko&tkns6td��n||_dS)Nz$microseconds must be an integer typez6microseconds must be representable as a 64-bit integer)�
isinstancer�	TypeError�min_int8�max_int8�
OverflowError�
_microseconds)r�valuerrr�_setMicrosecondsrs

zInterval._setMicrosecondscCs@t|t�std��n(t|ko&tkns6td��n||_dS)Nzdays must be an integer typez.days must be representable as a 32-bit integer)r*rr+�min_int4�max_int4r.�_days)rr0rrr�_setDays{s

zInterval._setDayscCs@t|t�std��n(t|ko&tkns6td��n||_dS)Nzmonths must be an integer typez0months must be representable as a 32-bit integer)r*rr+r2r3r.�_months)rr0rrr�
_setMonths�s

zInterval._setMonthscCs|jS)N)r/)rrrr�<lambda>�szInterval.<lambda>cCs|jS)N)r4)rrrrr8�scCs|jS)N)r6)rrrrr8�scCsd|j|j|jfS)Nz,<Interval %s months %s days %s microseconds>)r(r'r&)rrrr�__repr__�szInterval.__repr__cCs6|dk	o4t|t�o4|j|jko4|j|jko4|j|jkS)N)r*r%r(r'r&)r�otherrrr�__eq__�szInterval.__eq__cCs|j|�S)N)r;)rr:rrr�__neq__�szInterval.__neq__N)rrr)r"r#r$�__doc__r)r1r5r7�propertyr&r'r(r9r;r<rrrrr%Js!
			r%cCstd|�}|j|jfS)N�!)rr�unpack_from)�fmtZstrucrrr�
pack_funcs�srB�i�h�q�d�fZiii�iiZqiiZdiiZihihihZciZbhZccccz!i����?c@seZdZdZdS)�Warningz�Generic exception raised for important database warnings like data
    truncations.  This exception is not currently used by pg8000.

    This exception is part of the `DBAPI 2.0 specification
    <http://www.python.org/dev/peps/pep-0249/>`_.
    N)r"r#r$r=rrrrrM�srMc@seZdZdZdS)�Errorz�Generic exception that is the base exception of all other error
    exceptions.

    This exception is part of the `DBAPI 2.0 specification
    <http://www.python.org/dev/peps/pep-0249/>`_.
    N)r"r#r$r=rrrrrN�srNc@seZdZdZdS)�InterfaceErroraiGeneric exception raised for errors that are related to the database
    interface rather than the database itself.  For example, if the interface
    attempts to use an SSL connection but the server refuses, an InterfaceError
    will be raised.

    This exception is part of the `DBAPI 2.0 specification
    <http://www.python.org/dev/peps/pep-0249/>`_.
    N)r"r#r$r=rrrrrO�srOc@seZdZdZdS)�
DatabaseErrorz�Generic exception raised for errors that are related to the database.
    This exception is currently never raised by pg8000.

    This exception is part of the `DBAPI 2.0 specification
    <http://www.python.org/dev/peps/pep-0249/>`_.
    N)r"r#r$r=rrrrrP�srPc@seZdZdZdS)�	DataErrorz�Generic exception raised for errors that are due to problems with the
    processed data.  This exception is not currently raised by pg8000.

    This exception is part of the `DBAPI 2.0 specification
    <http://www.python.org/dev/peps/pep-0249/>`_.
    N)r"r#r$r=rrrrrQ�srQc@seZdZdZdS)�OperationalErrora=
    Generic exception raised for errors that are related to the database's
    operation and not necessarily under the control of the programmer. This
    exception is currently never raised by pg8000.

    This exception is part of the `DBAPI 2.0 specification
    <http://www.python.org/dev/peps/pep-0249/>`_.
    N)r"r#r$r=rrrrrR�srRc@seZdZdZdS)�IntegrityErrora
    Generic exception raised when the relational integrity of the database is
    affected.  This exception is not currently raised by pg8000.

    This exception is part of the `DBAPI 2.0 specification
    <http://www.python.org/dev/peps/pep-0249/>`_.
    N)r"r#r$r=rrrrrS�srSc@seZdZdZdS)�
InternalErroraNGeneric exception raised when the database encounters an internal error.
    This is currently only raised when unexpected state occurs in the pg8000
    interface itself, and is typically the result of a interface bug.

    This exception is part of the `DBAPI 2.0 specification
    <http://www.python.org/dev/peps/pep-0249/>`_.
    N)r"r#r$r=rrrrrTsrTc@seZdZdZdS)�ProgrammingErrora&Generic exception raised for programming errors.  For example, this
    exception is raised if more parameter fields are in a query string than
    there are available parameters.

    This exception is part of the `DBAPI 2.0 specification
    <http://www.python.org/dev/peps/pep-0249/>`_.
    N)r"r#r$r=rrrrrUsrUc@seZdZdZdS)�NotSupportedErrorz�Generic exception raised in case a method or database API was used which
    is not supported by the database.

    This exception is part of the `DBAPI 2.0 specification
    <http://www.python.org/dev/peps/pep-0249/>`_.
    N)r"r#r$r=rrrrrVsrVc@seZdZdZdS)�ArrayContentNotSupportedErrorz�
    Raised when attempting to transmit an array where the base type is not
    supported for binary data transfer by the interface.
    N)r"r#r$r=rrrrrW$srWc@seZdZdZdS)�ArrayContentNotHomogenousErrorzl
    Raised when attempting to transmit an array that doesn't contain only a
    single type of object.
    N)r"r#r$r=rrrrrX,srXc@seZdZdZdS)�ArrayContentEmptyErrorz�Raised when attempting to transmit an empty array. The type oid of an
    empty array cannot be determined, and so sending them is not permitted.
    N)r"r#r$r=rrrrrY4srYc@seZdZdZdS)�!ArrayDimensionsNotConsistentErrorzf
    Raised when attempting to transmit an array that has inconsistent
    multi-dimension sizes.
    N)r"r#r$r=rrrrrZ;srZc@seZdZdZdS)�Byteaz�Bytea is a str-derived class that is mapped to a PostgreSQL byte array.
    This class is only used in Python 2, the built-in ``bytes`` type is used in
    Python 3.
    N)r"r#r$r=rrrrr[Csr[cCstj|||�S)z�Constuct an object holding a date value.

    This function is part of the `DBAPI 2.0 specification
    <http://www.python.org/dev/peps/pep-0249/>`_.

    :rtype: :class:`datetime.date`
    )�datetime�date)�year�month�dayrrr�DateKsracCstj|||�S)z�Construct an object holding a time value.

    This function is part of the `DBAPI 2.0 specification
    <http://www.python.org/dev/peps/pep-0249/>`_.

    :rtype: :class:`datetime.time`
    )r\�time)�hour�minute�secondrrr�TimeVsrfcCstj||||||�S)z�Construct an object holding a timestamp value.

    This function is part of the `DBAPI 2.0 specification
    <http://www.python.org/dev/peps/pep-0249/>`_.

    :rtype: :class:`datetime.datetime`
    )r\)r^r_r`rcrdrerrr�	TimestampasrgcCsttj|�dd��S)aConstruct an object holding a date value from the given ticks value
    (number of seconds since the epoch).

    This function is part of the `DBAPI 2.0 specification
    <http://www.python.org/dev/peps/pep-0249/>`_.

    :rtype: :class:`datetime.date`
    N�)rarb�	localtime)�ticksrrr�
DateFromTicksls	rkcCsttj|�dd��S)aConstruct an objet holding a time value from the given ticks value
    (number of seconds since the epoch).

    This function is part of the `DBAPI 2.0 specification
    <http://www.python.org/dev/peps/pep-0249/>`_.

    :rtype: :class:`datetime.time`
    rh�)rfrbri)rjrrr�
TimeFromTicksxs	rmcCsttj|�dd��S)aConstruct an object holding a timestamp value from the given ticks value
    (number of seconds since the epoch).

    This function is part of the `DBAPI 2.0 specification
    <http://www.python.org/dev/peps/pep-0249/>`_.

    :rtype: :class:`datetime.datetime`
    Nrl)rgrbri)rjrrr�TimestampFromTicks�s	rncCstrt|�S|SdS)z�Construct an object holding binary data.

    This function is part of the `DBAPI 2.0 specification
    <http://www.python.org/dev/peps/pep-0249/>`_.

    :rtype: :class:`pg8000.types.Bytea` for Python 2, otherwise :class:`bytes`
    N)r
r[)r0rrr�Binary�sro�� ZALTERZCREATEcs�d}d}d}d}d}d}d}g�g}	tdd�td��}
|}d}�x�t|�D�]v\}
}|
dt|�krr||
d}nd}||k�r�|d	kr�|	j|�|d
kr�|}n|}�q�|dkr�|	j|�|}�q�|dkr�|d
kr�|	jt|
��n�|dkr�|dkr�|	jd�n�|dk�r"|dk�r"|}�jd�n�|dk�rP|dk�rP|dk�rP|}�jd�np|d$k�r�|dk�r�d}|�r~d}|	j|�n6|dk�r�d}n&|dk�r�|}|	jt|
��ntd��n
|	j|��n�||k�r^|d	k�r|	j|�|�r�d}n|d	k�r�d}n|}nV|d%k�rP|dk�rP|�r.d}|	j|�n |dk�r>d}ntd|d��n
|	j|��n`||k�r�|dk�r�|}|	j|�nV|d&k�r�|dk�r�|�r�d}|	j|�n |dk�r�d}ntd|d��n
|	j|��n�||k�rd|d	k�r
|dk�r
|	j|�|}nV|d'k�rV|dk�rV|�r4d}|	j|�n |dk�rDd}ntd|d��n
|	j|��nZ||k�r�|dk�r�d(|7<|dk�s�|j��r�|dk�r�|}y2�j�d)dd*�}|	jdt	|d���d+=Wn,t
k
�r
|	jdt	t����YnXn�|dk�r�|dk�r�|dk�r�|}y2�j�d,dd-�}|	jdt	|d���d.=Wn,t
k
�r�|	jdt	t����YnXn|d k�r�n�d/|7<n|dk�r�|}|}qJW|d0k�r�d!d"�}n�fd#d"�}dj|	�|fS)1NrrprIrh�FcSsdt|�S)N�$)�str)�xrrrr8�sz$convert_paramstyle.<locals>.<lambda>�'�E�"�qmark�?�numeric�:rs�named��pyformat�%�(�formatT�sz*Only %s and %% are supported in the query.z'%z:' not supported in a quoted string within the query string�\z;' not supported in a quoted string within the query string.�_�)z()cSs|S)Nr)�valsrrr�	make_argsHsz%convert_paramstyle.<locals>.make_argscst�fdd��D��S)Nc3s|]}�|VqdS)Nr)�.0�p)r�rr�	<genexpr>Lsz8convert_paramstyle.<locals>.make_args.<locals>.<genexpr>)�tuple)r�)�placeholders)r�rr�Ks)r�r)rr�)rr�)rr����r�r�r�r�r�r�r�)r{ryr�)rr	�	enumerate�len�appendrrO�isalnum�indexrt�
ValueError�join)�style�queryZOUTSIDEZ	INSIDE_SQZ	INSIDE_QIZ	INSIDE_ESZ	INSIDE_PNZin_quote_escapeZin_param_escapeZoutput_queryZ	param_idx�state�prev_crC�cZnext_cZpidxr�r)r�r�convert_paramstyle�s�	

















 






r�i�)�tzinfocCsht||�d}ytt|d�Stk
rb}z*|tkr>tjjS|tkrNtjjS|�WYdd}~XnXdS)Nr)r&)	�q_unpack�EPOCHrr.�INFINITY_MICROSECONDSr\�max�MINUS_INFINITY_MICROSECONDS�min)�data�offset�length�micros�errr�timestamp_recv_integer[sr�cCsttt||�d�S)Nr)�utcfromtimestamp�
EPOCH_SECONDS�d_unpack)r�r�r�rrr�timestamp_recv_floatisr�cCsJ|tjjkrt}n0|tjjkr$t}ntt|j��td�|j	}t
|�S)Ng��.A)r\r�r�r�r��intr�	timetupler��microsecond�q_pack)�vr�rrr�timestamp_send_integernsr�cCstt|j��|jdt�S)Ng��.A)�d_packrr�r�r�)r�rrr�timestamp_send_floatzsr�cCst|jt�jdd��S)N)r�)r��
astimezone�utc�replace)r�rrr�timestamptz_send_integer~sr�cCst|jt�jdd��S)N)r�)r�r�r�r�)r�rrr�timestamptz_send_float�sr�cCs`t||�d}ytt|d�Stk
rZ}z"|tkr:tS|tkrFtS|�WYdd}~XnXdS)Nr)r&)r��EPOCH_TZrr.r��DATETIME_MAX_TZr��DATETIME_MIN_TZ)r�r�r�r�r�rrr�timestamptz_recv_integer�sr�cCst|||�jtd�S)N)r�)r�r�r�)r�r�r�rrr�timestamptz_recv_float�sr�cCsd|j}y|t|jd�7}Wntk
r0YnXy
|j}Wntk
rTd}YnXt||j|�S)Ng��.Ar)r&r��seconds�AttributeErrorr(�qii_packr')r�r&r(rrr�interval_send_integer�s

r�cCsd|jdd}y||j7}Wntk
r0YnXy
|j}Wntk
rTd}YnXt||j|�S)Ng@�@r)r&r�r�r(�dii_packr')r�r�r(rrr�interval_send_float�s

r�cCsDt||�\}}}|dkr4t|d�\}}tj|||�St|||�SdS)Nrg��.A)�
qii_unpack�divmodr\rr%)r�r�r�r&r'r(r�r�rrr�interval_recv_integer�s
r�cCsPt||�\}}}|dkr4t|d�\}}tj|||�Stt|dd�||�SdS)Nrg��.Ai�)�
dii_unpackr�r\rr%r�)r�r�r�r�r'r(Zsecsr&rrr�interval_recv_float�s
r�cCst||�dS)Nr)r�)r�r�r�rrr�	int8_recv�sr�cCst||�dS)Nr)�h_unpack)r�r�r�rrr�	int2_recv�sr�cCst||�dS)Nr)�i_unpack)r�r�r�rrr�	int4_recv�sr�cCst||�dS)Nr)�f_unpack)r�r�r�rrr�float4_recv�sr�cCst||�dS)Nr)r�)r�r�r�rrr�float8_recv�sr�cCs|S)Nr)r�rrr�
bytea_send�sr�cCst||||��S)N)r[)r�r�r�rrr�
bytea_recv�sr�cCs||||�S)Nr)r�r�r�rrrr��scCs|jS)N)�bytes)r�rrr�	uuid_send�sr�cCst||||�d�S)N)r�)r)r�r�r�rrr�	uuid_recv�sr���cCs|rtStS)N)�TRUE�FALSE)r�rrr�	bool_send�sr�cCstS)N)�NULL)r�rrr�	null_sendsr�cCst||||��S)N)r�)r�r�r�rrr�int_insr�c@s�eZdZdZdd�Zedd��Zedd��Zedd	��Zd
d�Z	d!d
d�Z
dd�Zdd�Zd"dd�Z
dd�Zdd�Zdd�Zdd�Zd#dd�Zdd �ZdS)$�Cursora<A cursor object is returned by the :meth:`~Connection.cursor` method of
    a connection. It has the following attributes and methods:

    .. attribute:: arraysize

        This read/write attribute specifies the number of rows to fetch at a
        time with :meth:`fetchmany`.  It defaults to 1.

    .. attribute:: connection

        This read-only attribute contains a reference to the connection object
        (an instance of :class:`Connection`) on which the cursor was
        created.

        This attribute is part of a DBAPI 2.0 extension.  Accessing this
        attribute will generate the following warning: ``DB-API extension
        cursor.connection used``.

    .. attribute:: rowcount

        This read-only attribute contains the number of rows that the last
        ``execute()`` or ``executemany()`` method produced (for query
        statements like ``SELECT``) or affected (for modification statements
        like ``UPDATE``).

        The value is -1 if:

        - No ``execute()`` or ``executemany()`` method has been performed yet
          on the cursor.
        - There was no rowcount associated with the last ``execute()``.
        - At least one of the statements executed as part of an
          ``executemany()`` had no row count associated with it.
        - Using a ``SELECT`` query statement on PostgreSQL server older than
          version 9.
        - Using a ``COPY`` query statement on PostgreSQL server version 8.1 or
          older.

        This attribute is part of the `DBAPI 2.0 specification
        <http://www.python.org/dev/peps/pep-0249/>`_.

    .. attribute:: description

        This read-only attribute is a sequence of 7-item sequences.  Each value
        contains information describing one result column.  The 7 items
        returned for each column are (name, type_code, display_size,
        internal_size, precision, scale, null_ok).  Only the first two values
        are provided by the current implementation.

        This attribute is part of the `DBAPI 2.0 specification
        <http://www.python.org/dev/peps/pep-0249/>`_.
    cCs0||_d|_d|_d|_t�|_d|_d|_dS)NrpFr�)�_c�	arraysize�ps�
_row_countr�_cached_rows�portal_name�portal_suspended)r�
connectionrrrr)DszCursor.__init__cCstddd�|jS)Nz'DB-API extension cursor.connection usedrh)�
stacklevel)rr�)rrrrr�MszCursor.connectioncCs|jS)N)r�)rrrr�rowcountRszCursor.rowcountcCs|j�S)N)�_getDescription)rrrrr8VszCursor.<lambda>c	Cs^|jdkrdS|jd}t|�dkr(dSg}x,|D]$}|j|d|ddddddf�q2W|S)N�row_descr�name�type_oid)r�r�r�)rr��columns�colrrrr�Xs


 zCursor._getDescriptionNcCs�yR|jj�@||_|jjr6|jjr6|jj|dd�|jj|||�WdQRXWnPtk
r�}z4|jdkrxtd��n|jjdkr�td��n|�WYdd}~XnXdS)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
        <http://www.python.org/dev/peps/pep-0249/>`_.

        :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
            <http://www.postgresql.org/docs/current/static/sql-copy.html>`_
            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
        zbegin transactionNz
Cursor closedzconnection is closed)	r��_lock�stream�in_transaction�
autocommit�executer�rO�_sock)r�	operation�argsr�r�rrrr�is



zCursor.executecCsDg}x$|D]}|j||�|j|j�q
Wd|kr6dnt|�|_dS)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
        <http://www.python.org/dev/peps/pep-0249/>`_.

        :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.
        rpNr�r�)r�r�r��sum)rr�Z
param_setsZ	rowcountsZ
parametersrrr�executemany�s

zCursor.executemanycCsVyt|�Stk
rdStk
r6td��Yntk
rPtd��YnXdS)a Fetch the next row of a query result set.

        This method is part of the `DBAPI 2.0 specification
        <http://www.python.org/dev/peps/pep-0249/>`_.

        :returns:
            A row as a sequence of field values, or ``None`` if no more rows
            are available.
        Nz#attempting to use unexecuted cursor)r�
StopIterationr+rUr�)rrrr�fetchone�s
zCursor.fetchonecCs>ytt||dkr|jn|��Stk
r8td��YnXdS)aFetches the next set of rows of a query result.

        This method is part of the `DBAPI 2.0 specification
        <http://www.python.org/dev/peps/pep-0249/>`_.

        :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.
        Nz#attempting to use unexecuted cursor)r�r
r�r+rU)r�numrrr�	fetchmany�s
zCursor.fetchmanycCs*yt|�Stk
r$td��YnXdS)a$Fetches all remaining rows of a query result.

        This method is part of the `DBAPI 2.0 specification
        <http://www.python.org/dev/peps/pep-0249/>`_.

        :returns:

            A sequence, each entry of which is a sequence of field values
            making up a row.
        z#attempting to use unexecuted cursorN)r�r+rU)rrrr�fetchall�szCursor.fetchallcCs
d|_dS)z�Closes the cursor.

        This method is part of the `DBAPI 2.0 specification
        <http://www.python.org/dev/peps/pep-0249/>`_.
        N)r�)rrrr�close�szCursor.closecCs|S)zoA cursor object is iterable to retrieve the rows from a query.

        This is a DBAPI 2.0 extension.
        r)rrrr�__iter__�szCursor.__iter__cCsdS)z�This method is part of the `DBAPI 2.0 specification
        <http://www.python.org/dev/peps/pep-0249/>`_, however, it is not
        implemented by pg8000.
        Nr)rZsizesrrr�
setinputsizes�szCursor.setinputsizescCsdS)z�This method is part of the `DBAPI 2.0 specification
        <http://www.python.org/dev/peps/pep-0249/>`_, however, it is not
        implemented by pg8000.
        Nr)r�size�columnrrr�
setoutputsize�szCursor.setoutputsizecCs�|jj��y
|jj�Stk
r�|jrj|jj|�|jjt�|jj	�|jj
|�|jsj|jj|�y
|jj�Stk
r�|jdkr�t
d��n"t|jd�dkr�t
d��nt��YnXYnXWdQRXdS)NzA query hasn't been issued.r�rz
no result set)r�r�r��popleft�
IndexErrorr��send_EXECUTE�_write�SYNC_MSG�_flush�handle_messages�close_portalr�rUr�r�)rrrr�__next__�s&






zCursor.__next__)NN)N)N)r"r#r$r=r)r>r�r��descriptionr�r�r�r�r�r�r�rrrr
rrrrr�s 3	
*

r��N�R�S�K�Z�Trw�D�C�1�2�3r��n�t�Ar��G�H�I�B�Pr��Xrr�M�W�F�Lz[] 'uz{}rhc@s4eZdZdd�Zdd�Zdd�Zdd�Zd	d
�ZdS)�MulticastDelegatecCs
g|_dS)N)�	delegates)rrrrr)YszMulticastDelegate.__init__cCs|j|�|S)N)�add)r�delegaterrr�__iadd__\s
zMulticastDelegate.__iadd__cCs|jj|�dS)N)r(r�)rr*rrrr)`szMulticastDelegate.addcCs|jj|�|S)N)r(�remove)rr*rrr�__isub__cszMulticastDelegate.__isub__cOsx|jD]}|||�qWdS)N)r()rr��kwargsrFrrr�__call__gszMulticastDelegate.__call__N)r"r#r$r)r+r)r-r/rrrrr'Xs
r'c@s�eZdZdZedd��Zedd��Zedd��Zedd��Zedd��Z	edd��Z
ed	d��Zed
d��Zedd��Z
dZee�Zd
d�Zdd�Zdd�Zdd�Zdd�Zdd�Zdd�Zdd�Zdd�Zdd �Zd!d"�Zd#d$�Zd%d&�Zd'd(�Zd)d*�Zd+d,�Z d-d.�Z!d/d0�Z"d1d2�Z#d3d4�Z$d5d6�Z%d7d8�Z&d9d:�Z'd;d<�Z(d=d>�Z)d?d@�Z*dAdB�Z+dCdD�Z,dEdF�Z-dGdH�Z.dIdJ�Z/dKdL�Z0dMdN�Z1dOdP�Z2dQdR�Z3dSdT�Z4dUdV�Z5dWdX�Z6dYdZ�Z7dbd\d]�Z8dcd^d_�Z9d`da�Z:d[S)d�
Connectiona2	A connection object is returned by the :func:`pg8000.connect` function.
    It represents a single physical connection to a PostgreSQL database.

    .. attribute:: Connection.notifies

        A list of server-side notifications received by this database
        connection (via the LISTEN/NOTIFY PostgreSQL commands).  Each list
        element is a two-element tuple containing the PostgreSQL backend PID
        that issued the notify, and the notification name.

        PostgreSQL will only send notifications to a client between
        transactions.  The contents of this property are generally only
        populated after a commit or rollback of the current transaction.

        This list can be modified by a client application to clean out
        notifications as they are handled.  However, inspecting or modifying
        this collection should only be done while holding the
        :attr:`notifies_lock` lock in order to guarantee thread-safety.

        This attribute is not part of the DBAPI standard; it is a pg8000
        extension.

        .. versionadded:: 1.07

    .. attribute:: Connection.notifies_lock

        A :class:`threading.Lock` object that should be held to read or
        modify the contents of the :attr:`notifies` list.

        This attribute is not part of the DBAPI standard; it is a pg8000
        extension.

        .. versionadded:: 1.07

    .. attribute:: Connection.autocommit

        Following the DB-API specification, autocommit is off by default.
        It can be turned on by setting this boolean pg8000-specific autocommit
        property to True.

        .. versionadded:: 1.9

    .. exception:: Connection.Error
                   Connection.Warning
                   Connection.InterfaceError
                   Connection.DatabaseError
                   Connection.InternalError
                   Connection.OperationalError
                   Connection.ProgrammingError
                   Connection.IntegrityError
                   Connection.DataError
                   Connection.NotSupportedError

        All of the standard database exception types are accessible via
        connection instances.

        This is a DBAPI 2.0 extension.  Accessing any of these attributes will
        generate the warning ``DB-API extension connection.DatabaseError
        used``.
    cCs
|jt�S)N)�	_getErrorrM)rrrrr8�szConnection.<lambda>cCs
|jt�S)N)r1rN)rrrrr8�scCs
|jt�S)N)r1rO)rrrrr8�scCs
|jt�S)N)r1rP)rrrrr8�scCs
|jt�S)N)r1rR)rrrrr8�scCs
|jt�S)N)r1rS)rrrrr8�scCs
|jt�S)N)r1rT)rrrrr8�scCs
|jt�S)N)r1rU)rrrrr8�scCs
|jt�S)N)r1rV)rrrrr8�s�dcCstd|jdd�|S)Nz#DB-API extension connection.%s usedrh)r�)rr")r�errorrrrr1�szConnection._getErrorc	#Gs d�_td�td�td�td�td�td�td�f�_tj��_|dkrPtd	��t|t�rh|j	d��_
n|�_
t|t�r�|j	d��_n|�_d
�_d�_
tdd���_d
�_d
�_�yR|dkr�|dk	r�tjtjtj��_n8|dk	�r
ttd�s�td��tjtjtj��_ntd��t�r0|dk	�r0�jj|�|dk�rV|dk	�rV�jj||f�n|dk	�rl�jj|�|�r�j�vyPd
dl}	�jjtdd���jjd�}
|
td�k�r�|	j �j��_ntd��Wnt!k
�r�td��YnXWdQRX�jj"dd��_#Wn:tj$k
�r@}z�jj%�td|��WYdd}~XnX�j#j&�_'�j#j(�_)�j#j*�_+d�_,t-��_.t-��_/t-��_0�j/�j17_/�fdd�}�fdd�}
�fdd�}�fd d!�}t2t3t4t5t6d"��t6d#����d$t7i����fd%d&�}�fd'd(�}�fd)d*�}t�r2�fd+d,��d-d.�}�fd/d0�}n �fd1d,��d2d.�}�fd3d0�}�fd4d5�}�fd6d7�}�fd8d9�}�fd:d;�}t�fd<d�t8|ft8t9ft8�ft8t:ft8t;ft<|ft8t=ft8�ft<t>ft<t>ft<|ft8t?ft8t@ft8�ft<�ft8|ft8|ft8|ft8|ft8|ft8|ft8|ft8|ft8|ft8|ft8�ft8�ft<|ft<|ft8tAft8tBft8tCft<|ft8|ft<|ft8�ft8tDft<|fd=�&��_EtFd�dTt8tGftHd>t8tIftJd?t<|ftKd@t8tLftMjNdAt<|ftMjOdBt<|
fdCdCt8tPfdDdDt8tQftMjRdEt8tSftTdEt8tSft7dFt<|ftUdGt8tVfi�_WtMjM�jXtY�jZt[�jZi�_\t�rfdHt8t]f�jWt^<d?t<|f�jWt<d?t<t]f�jWt_<d?t<|f�jWt`<n dHt8t]f�jWta<d?t<|f�jWt_<y�d
dIlbmc�md}me}mf�mg}mh}�fdJdK�}���fdLdM�}dNt<|f�jW|<dNt<|f�jW|<dNt<|f�jW|<dNt<|f�jW|<t<|f�jEdN<Wnt!k
�r*YnXti�jjtk�jltm�j1tn�jotp�jqtr�jstt�jutv�jwtx�jytz�j{t|�j}t~�jt��j�t��j�t��j�t��j�t��j�t��j�t��j�t��j�t��j�i�_�dO}t�t�|�tdP��j
t��} |dk	�rt|t��r�|j	d�}| j�tdQ�|t��| j�d
��j+t�t�| �dR���j+| ��j'��j���_��j��ybd}!�_$x@|!tpttfk�r�t��j)dS��\}!}"�j�|!�j)|"dR�d��qZW�j$dk	�r��j$�WnLt�k
�r�}z.y�j��Wnt�k
�r�YnX|�WYdd}~XnXWdQRXd
�_�g�_�tj��_�dS)UN�utf8�INSERT�DELETE�UPDATE�MOVE�FETCH�COPYZSELECTz.The 'user' connection parameter cannot be NoneFcSstt�S)N)r�dictrrrrr8�sz%Connection.__init__.<locals>.<lambda>r�AF_UNIXz9attempt to connect to unix socket on unsupported platformz)one of host or unix_sock must be provided�i/�rprzServer refuses SSLzESSL required but ssl module not available in this python installation�rwb)�modezcommunication errorcs|j�j�S)N)�encode�_client_encoding)r�)rrr�text_out6sz%Connection.__init__.<locals>.text_outcs|j�j�j�S)N)�	isoformatr@rA)r�)rrr�time_out9sz%Connection.__init__.<locals>.time_outcsD|tjjkrdj�j�S|tjjkr0dj�j�S|j�j�j�SdS)NZinfinityz	-infinity)r\r]r�r@rAr�rC)r�)rrr�date_out<s
z%Connection.__init__.<locals>.date_outcst|�j�j�S)N)rtr@rA)r�)rrr�unknown_outDsz(Connection.__init__.<locals>.unknown_outz{}z[]rcs�g}d}xz||||�j�j�j��jtd�td��D]H}|dkrX|dkrX|jd�n|d
krr|dkrr|jd	�|j|�|}q8Wtd
j|���S)Nr��None�[�]�,rz	Decimal('r�z')r~)rHrIrJr)rHrJ)rIrJ)rHrIrJr�)	�decoderA�	translater�r�extendr��evalr�)r��idxr��arrr�r�)�glblsr�	trans_tabrr�array_inJs

z%Connection.__init__.<locals>.array_incs�||}t||�\}}}|d7}�j|d}g}x,t|�D] }	|jt||�d�|d7}q<Wg}
xN||kr�t||�\}|d7}|dkr�|
jd�qf|
j||||��||7}qfWx4t|dd��D] }tttt	t
|
�g|���}
q�W|
S)N�rprr=rrr�)�
iii_unpack�pg_types�ranger��	ii_unpackr��reversed�listr�zip�iter)r�rOr�Z	final_idxZdimZhasnullZtypeoid�
conversion�dim_lengthsrC�valuesZelement_len)rrr�
array_recvYs&
 z'Connection.__init__.<locals>.array_recvcs,td||||�j�j�jdd�d�S)NrHrqrJrI)rNrKrAr�)r�rOr�)rrr�	vector_inysz&Connection.__init__.<locals>.vector_incst||||��j�S)N)�unicoderA)r�r�r�)rrr�	text_recv~sz&Connection.__init__.<locals>.text_recvcSs||dkS)Nr�r)rF�o�lrrr�	bool_recv�sz&Connection.__init__.<locals>.bool_recvcstt||||��j��S)N)rrbrA)r�r�r�)rrr�json_in�sz$Connection.__init__.<locals>.json_incst||||��j�S)N)rtrA)r�r�r�)rrrrc�scSs||dkS)Nrpr)r�r�r�rrrrf�scstt||||��j��S)N)rrtrA)r�r�r�)rrrrg�scspt|||d��}t||d|d��}t||d||�j�j��}tj||t|�t|t|�d��S)NrIrh�rli@B)r�rrKrAr\rb)r�r�r�rcrdZsec)rrr�time_in�sz$Connection.__init__.<locals>.time_incsv|||d�j�j�}|dkr(tjjS|dkr8tjjStjt|�t||d|d��t||d|d���SdS)NrrZinfiz-infrh�r=�
)rKrAr\r]r�r�r�)r�r�r�Zyear_str)rrr�date_in�sz$Connection.__init__.<locals>.date_incst||||�j�j��S)N)rrKrA)r�r�r�)rrr�
numeric_in�sz'Connection.__init__.<locals>.numeric_incst|�j�j�S)N)rtr@rA)rF)rrr�numeric_out�sz(Connection.__init__.<locals>.numeric_outcst�fS)N)�FC_TEXTr)rcrrr8�s)&�����������ri�i�i�i=i�i�i�i�i�i�i�i�i�i�iii:i;iZi�i�i�i�i�i�i�i�rpi�i�i:i;iZi�i�i�i�rq)�
ip_address�IPv4Address�IPv6Address�
ip_network�IPv4Network�IPv6Networkcst|�j�j�S)N)rtr@rA)r�)rrr�inet_out�sz%Connection.__init__.<locals>.inet_outcs6||||�j�j�}d|kr*�|d�S�|�SdS)N�/F)rKrA)r�r�r�Zinet_str)r{r~rrr�inet_ins

z$Connection.__init__.<locals>.inet_inieizuserz	databaserrrhr�)�rAr�_commands_with_count�	threading�Lockr�rOr*rr@�user�passwordr��_xidr�_caches�statement_number�
portal_number�socket�AF_INET�SOCK_STREAM�_usock�hasattrr<rUr
�
settimeout�connect�ssl�sendall�ii_pack�recv�wrap_socket�ImportError�makefiler�r3r��flushr
�read�_read�writer�_backend_key_datar'�NoticeReceivedZParameterStatusReceived�NotificationReceived�handle_PARAMETER_STATUSr;r[r�ordrr�	FC_BINARYr�r�r�ror�r�r�r�r�r�r�r�rV�typer��boolr�r��floatr�r\r]rbr�r�rr�r%rr��py_types�inspect_datetimerZ�
array_inspectr��
inspect_funcsr�r[rt�longr��	ipaddressr{r|r}r~rr��NOTICE_RESPONSE�handle_NOTICE_RESPONSE�AUTHENTICATION_REQUEST�handle_AUTHENTICATION_REQUEST�PARAMETER_STATUS�BACKEND_KEY_DATA�handle_BACKEND_KEY_DATA�READY_FOR_QUERY�handle_READY_FOR_QUERY�ROW_DESCRIPTION�handle_ROW_DESCRIPTION�ERROR_RESPONSE�handle_ERROR_RESPONSE�EMPTY_QUERY_RESPONSE�handle_EMPTY_QUERY_RESPONSE�DATA_ROW�handle_DATA_ROW�COMMAND_COMPLETE�handle_COMMAND_COMPLETE�PARSE_COMPLETE�handle_PARSE_COMPLETE�
BIND_COMPLETE�handle_BIND_COMPLETE�CLOSE_COMPLETE�handle_CLOSE_COMPLETE�PORTAL_SUSPENDED�handle_PORTAL_SUSPENDED�NO_DATA�handle_NO_DATA�PARAMETER_DESCRIPTION�handle_PARAMETER_DESCRIPTION�NOTIFICATION_RESPONSE�handle_NOTIFICATION_RESPONSE�	COPY_DONE�handle_COPY_DONE�	COPY_DATA�handle_COPY_DATA�COPY_IN_RESPONSE�handle_COPY_IN_RESPONSE�COPY_OUT_RESPONSE�handle_COPY_OUT_RESPONSE�
message_types�	bytearray�i_pack�	NULL_BYTErMr�r��cursor�_cursor�	ci_unpack�	Exception�_closer��notifies�
notifies_lock)#rr��host�	unix_sock�port�databaser�r��timeoutZ	sslmodule�respr�rBrDrErFrSr`rarfrgrirlrmrnr|r}rr�r�r��protocol�val�code�data_lenr)rQr{r~rrcrRrr)�s�










 







 





 
 zConnection.__init__csXt�fdd�|jt�D��}t|�}|tdkr<td��_nttdd�|D����_dS)Nc3s.|]&}|dd�|dd�j�j�fVqdS)rrpN)rKrA)r�r�)rrrr�Osz3Connection.handle_ERROR_RESPONSE.<locals>.<genexpr>Z28000z"md5 password authentication failedcss|]\}}|VqdS)Nr)r��kr�rrrr�Us)r��splitr�r;�
RESPONSE_CODErOr3rU)rr�r��	responsesZmsg_dictr)rrr�Ms
z Connection.handle_ERROR_RESPONSEcCstd�|_dS)Nzquery was empty)rUr3)rr�r�rrrr�Wsz&Connection.handle_EMPTY_QUERY_RESPONSEcCsdS)Nr)rr�r�rrrr�Zsz Connection.handle_CLOSE_COMPLETEcCsdS)Nr)rr�r�rrrr�]sz Connection.handle_PARSE_COMPLETEcCsdS)Nr)rr�r�rrrr�bszConnection.handle_BIND_COMPLETEcCs
d|_dS)NT)r�)rr�r�rrrr�esz"Connection.handle_PORTAL_SUSPENDEDcCsdS)Nr)rr�r�rrrr�hsz'Connection.handle_PARAMETER_DESCRIPTIONcCs
d|_dS)NT)Z
_copy_done)rr�r�rrrr�pszConnection.handle_COPY_DONEcCs"t|�\}}|jdkrtd��dS)Nz7An output stream is required for the COPY OUT response.)�	bh_unpackr�rO)rr�r��	is_binary�num_colsrrrr�ss
z#Connection.handle_COPY_OUT_RESPONSEcCs|jj|�dS)N)r�r�)rr�r�rrrr�~szConnection.handle_COPY_DATAcCs�t|�\}}|jj�st�|jdkr,td��trvx�|jjd�}|sDP|jt	t
t|�d��|j|�|j�q2WnTt
d�}xJ|jj|�}|dkr�P|jt	t
|d��|j|d|��|j�q�W|jt�|jt�|j�dS)Nz5An input stream is required for the COPY IN response.i rrr)r�r��locked�AssertionErrorr�rOr
r�rr�r�r�r
r��readinto�
COPY_DONE_MSGr	)rr�r�r�r�ZbffrZ
bytes_readrrrr��s0



z"Connection.handle_COPY_IN_RESPONSEc
Cs�|j|�t|�d}d}|jt|�|}||||�jd�}||d7}|jt|�|}|j�|jj||f�WdQRXdS)Nrrr�asciirp)r�r��findr�rKr�r�r�)rr�r�Zbackend_pidrO�null�	conditionrrrr��s
z'Connection.handle_NOTIFICATION_RESPONSEcCst|�S)z�Creates a :class:`Cursor` object bound to this
        connection.

        This function is part of the `DBAPI 2.0 specification
        <http://www.python.org/dev/peps/pep-0249/>`_.
        )r�)rrrrr��szConnection.cursorcCs&|j�|j|jdd�WdQRXdS)z�Commits the current database transaction.

        This function is part of the `DBAPI 2.0 specification
        <http://www.python.org/dev/peps/pep-0249/>`_.
        �commitN)r�r�r�)rrrrr��szConnection.commitcCs&|j�|j|jdd�WdQRXdS)z�Rolls back the current database transaction.

        This function is part of the `DBAPI 2.0 specification
        <http://www.python.org/dev/peps/pep-0249/>`_.
        �rollbackN)r�r�r�)rrrrr��szConnection.rollbackcCs�z�y |jt�|j�|jj�Wndtk
r>td��YnJtk
rXtd��Yn0tj	k
r�}zt
t|���WYdd}~XnXWd|jj�d|_XdS)Nzconnection is closed)
r�
TERMINATE_MSGr
r�r�r�rOr�r�r3rRrtr�)rr�rrrr��s
"
zConnection._closec	Cs|j�|j�WdQRXdS)z�Closes the database connection.

        This function is part of the `DBAPI 2.0 specification
        <http://www.python.org/dev/peps/pep-0249/>`_.
        N)r�r�)rrrrr��szConnection.closecCs|jj�st�t|�d}|dkr$n�|dkrZ|jdkr>td��|jt|jt�|j	�n�|dkr�t
d�jt|d��}|jdkr�td��t
d�t
t
|j|j�j�jd	�|�j�jd	�}|jt|t�|j	�n4|dk�r�tdt|�d��ntdt|�d��dS)NrrhzGserver requesting password authentication, but no password was providedrhr~rrzKserver requesting MD5 password authentication, but no password was providedrr�rIrlrjr=�	zAuthentication method z not supported by pg8000.z not recognized by pg8000.)rIrrrlrjr=r�)r�r�r�r�r�rO�
_send_message�PASSWORDr�r
rr��cccc_unpackrr��	hexdigestr@rt)rr�r�Z	auth_codeZsalt�pwdrrrr��s6







z(Connection.handle_AUTHENTICATION_REQUESTcCs|tk|_dS)N)�IDLEr�)rr�r�rrrr�'sz!Connection.handle_READY_FOR_QUERYcCs
||_dS)N)r�)rr�r�rrrr�+sz"Connection.handle_BACKEND_KEY_DATAcCs"|jdkr|jdS|jdSdS)NiZi�)r�r�)rr0rrrr�.s

zConnection.inspect_datetimecCs�g}x�|D]�}t|�}y|j|j|�Wq
tk
r�y|j|j||��Wn6tk
r�}ztdt|�d��WYdd}~XnXYq
Xq
Wt|�S)Nztype znot mapped to pg type)r�r�r��KeyErrorr�rVrtr�)rr_�paramsr0�typr�rrr�make_params4s
.zConnection.make_paramsc	Cs�t|�d}d}x�t|�D]v}|||jt|��}|t|�d7}ttdt||���}||d
<|d7}|jdj	|�|j
|d\|d
<|d<qWdS)NrrIrp�	table_oid�column_attrnumr��	type_size�
type_modifierr�r��r�Z	pg8000_fc�func)r	r
r�rrr�)r�rWr�r�r�r;r[�
ihihih_unpackr�r�rV)rr�r�r	rOrCr��fieldrrrr�Bsz!Connection.handle_ROW_DESCRIPTIONc"s�|dkrf}tj}�j|}y|d|\}}Wn2tk
rbt||�\}}||f|d|<YnX||�}�j|�}	||	f}
y|d|
}||_W�n"tk
�r�dt�j�}�jd7_|j	d�t
}
gtdd�|	D��d�}||_td	d�|	D��}t|
�}|j
|j	�j�t
�|j
tt|	���x0|	D](\}}}|j
t|dk�rVd
n|���q8W�jt|��jtt|
��jt�y�j�Wnntk
�r�}z �jdk�r�td��n|�WYdd}~Xn2tjk
�r}ztt|���WYdd}~XnX�j|�t�fdd�|d
D��}tdd�|d
D��|d<|
tt|	��t ddt|�f|��tt|	��|d<tt|��t ddt|�f|��|d<||d|
<YnX|j!j"�d|_#dt�j$�|_%�j$d7_$|j%j	d�t
|_&|j&t'j(|_)t|j&|d�}xPt*||d�D]>\}}|dk�rBt+}n||�}|j
tt|���|j
|��q*W|j
|d��jt,|��j-|��jt��j��j|�|j.�rƈj/�r�td��n
�j0|�dS)N�	statementr�Zpg8000_statement_rpr�css|]}|dVqdS)rINr)r�rurrrr�ksz%Connection.execute.<locals>.<genexpr>)r��param_funcscss|]}|dVqdS)rpNr)r�rurrrr�osi�zconnection is closedc3s |]}�j|ddVqdS)r�rN)rV)r�rG)rrrr��sr�css|]}|dVqdS)rNr)r�rGrrrr��s�input_funcsr?rDZbind_1Zbind_2Zpg8000_portal_rz�With autocommit on, it's not possible to retrieve more rows than the pg8000 cache size, as the portal is closed when the transaction is closed.r�r�)1�pg8000�
paramstyler�rr�rr�rtr�r@r�r�r�rMrA�h_packr�r�r��PARSE�DESCRIBE�	STATEMENTrr	r
r�r�rOr�r3rRrrr��clearr�r�r��portal_name_binr0�_row_cache_size_bin�execute_msgr[r��BINDrr�r�r)rr�r�r�r�cacherr�r�r�keyr�Zstatement_nameZstatement_name_binZ	param_fcsr��oid�fc�	send_funcr�Z	output_fc�retvalr0r)rrr�Rs�


"


(
 





zConnection.executecCs�y8|j|�|jtt|�d��|j|�|jt�WnVtk
rt}z t|�dkr`td��n|�WYdd}~Xntk
r�td��YnXdS)Nrrzwrite to closed filezconnection is closed)rr�r��	FLUSH_MSGr�rtrOr�)rr�r�r�rrrr��s


zConnection._send_messagecCsd|_|jt|j�dS)NF)r�r��EXECUTEr)rr�rrrr�szConnection.send_EXECUTEcCsdS)Nr)r�msgr�rrrr��szConnection.handle_NO_DATAcCs~|dd�jt�}|d}||jkrPt|d�}|jdkrB||_n|j|7_|tkrzx |jD]}|j|dj�q`WdS)Nrprr�r�r�r�)r��BINARY_SPACEr�r�r��DDL_COMMANDSr�r)rr�r�r_�commandZ	row_countr�rrrr�s

z"Connection.handle_COMMAND_COMPLETEcCspd}g}xV|jdD]H}t||�d}|d7}|dkrB|jd�q|j||||��||7}qW|jj|�dS)NrIrrrrrpr�)r�r�r�r�)rr�r�Zdata_idx�rowr�vlenrrrr�szConnection.handle_DATA_ROWc
Cstd}|_y>x8|tkrDt|jd��\}}|j||j|d�|�qWWn|j��YnX|jdk	rp|j�dS)Nrhrr)r3r�r�r�r�r�)rr�r�r�rrrrs

"
zConnection.handle_messagescCs2|jtt|j�|jt�|j�|j|�dS)N)r��CLOSE�PORTALrrr	r
r)rr�rrrr.s
zConnection.close_portalcCs&tdd�|jt�D��}|j|�dS)Ncss&|]}|dd�|dd�fVqdS)rrpNr)r�r�rrrr�:sz4Connection.handle_NOTICE_RESPONSE.<locals>.<genexpr>)r;r�r�r�)rr�r�r�rrrr�9sz!Connection.handle_NOTICE_RESPONSEcCs�|jt�}|d|�||dd�}}|td�krT|jd�j�}tj||�|_�n�|td�k�rJ|td�kr�dtt	f|j
d<ttf|jd<dtt
f|j
d<ttf|jd<dttf|j
t<dttf|j
tj<ttf|jd<nldttf|j
d<ttf|jd<dttf|j
d<ttf|jd<dttf|j
t<dttf|j
tj<ttf|jd<n�|td	�k�r�t|jd��|_|jtd
�k�r�td�td�td
�td�td�f|_n:|jtd�k�r�td�td�td
�td�td�td�f|_dS)NrpZclient_encodingr�Zinteger_datetimes�oniZi�i�Zserver_versionz8.2.0r5r6r7r8r9z9.0.0r:r�)r�r�rrK�lower�pg_to_py_encodings�getrAr�r�r�r�rVr�r�r�r%r\rr�r�r�r�r�r�r�rZ_server_versionr�)rr�r��posr r0�encodingrrrr�=s:
z"Connection.handle_PARAMETER_STATUSc
s�t|�}|dkrtd��t|��t�t��r
t�d\}}}xtt|�D]h}|dkrRqDt|kodtknrlqDd}t|ko�t	knr�qDd}t
|ko�tknr�qDd}qDW|r�d}dtt
�}�n>|r�d}dtt�}�n$|�rd}d	tt�}�ntd
��n�y4|j|f�d\�}��dk�r4d
�t}t�}WnRtk
�rhtdt��d��Yn*tk
�r�tdt��d��YnX|tk�r����fdd�}	n��fdd�}	|||	fS)Nzarray has no valuesTFi�rti�rvi�rsz'numeric not supported as array contentsri�rwzoid z  not supported as array contentsztype cs�x8t|�D],\}}}t|�td�f�s
tdt����q
Wt|�t|�}t|�}tt	t
|�|���}x|D]}|jt|d��qlWxFt
|�D]:}|dkr�|td�7}q��|�}|tt
|��7}||7}q�W|S)Nz#not all array elements are of type rpr�)�
walk_arrayr*r�rXrt�array_check_dimensions�array_has_null�array_dim_lengthsr��iii_packr�rMr��
array_flattenr�)rP�arCr�Zhas_nullr^r�Z
inner_data)r!r#rrr�
send_array�s"
z,Connection.array_inspect.<locals>.send_arraycs�x8t|�D],\}}}t|�td�f�s
tdt����q
Wt|�t|�}x:t|�D].\}}}|dkrpd||<qT�|�jd�||<qTWtt|��j	t
�jd�S)Nz#not all array elements are of type r�r�)r5r*r�rXrtr6rrKrrL�	arr_transr@)rPr;rCr��ar)r#rrrr<�s
)TTT)�array_find_first_elementrYr��
issubclassrr:�min_int2�max_int2r2r3r,r-r�rr�r�rWr�pg_array_typesrrtrV)
rr0Z
first_elementZint2_okZint4_okZint8_okr�Z	array_oidr"r<r)r!r#rrr�js\


zConnection.array_inspectcCs
|||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)r)rZ	format_idZglobal_transaction_idZbranch_qualifierrrr�xid�szConnection.xidcCs ||_|jr|j|jdd�dS)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
        <http://www.python.org/dev/peps/pep-0249/>`_.
        zbegin transactionN)r�r�r�r�)rrDrrr�	tpc_begin�s
zConnection.tpc_begincCs$d|jdf}|j|j|d�dS)a�Performs 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
        <http://www.python.org/dev/peps/pep-0249/>`_.
        zPREPARE TRANSACTION '%s';rpN)r�r�r�)rrErrr�tpc_prepare�szConnection.tpc_prepareNcCsr|dkr|j}|dkrtd��z@|j}d|_||j�krT|j|jd|dfd�n|j�Wd||_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
        <http://www.python.org/dev/peps/pep-0249/>`_.
        Nz.Cannot tpc_commit() without a TPC transaction!TzCOMMIT PREPARED '%s';rp)r�rUr��tpc_recoverr�r�r�)rrD�previous_autocommit_moderrr�
tpc_commit�szConnection.tpc_commitcCsr|dkr|j}|dkrtd��z@|j}d|_||j�krT|j|jd|dfd�n|j�Wd||_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
        <http://www.python.org/dev/peps/pep-0249/>`_.
        Nz9Cannot tpc_rollback() without a TPC prepared transaction!TzROLLBACK PREPARED '%s';rp)r�rUr�rGr�r�r�)rrDrHrrr�tpc_rollback	szConnection.tpc_rollbackcs>z0�j}d�_�j�}|jd��fdd�|D�S|�_XdS)z�Returns 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
        <http://www.python.org/dev/peps/pep-0249/>`_.
        Tz!select gid FROM pg_prepared_xactscsg|]}�jd|dd��qS)rr~)rD)r�r+)rrr�
<listcomp>I	sz*Connection.tpc_recover.<locals>.<listcomp>N)r�r�r�)rrHZcursr)rrrG=	s
zConnection.tpc_recover)N)N);r"r#r$r=r>rMrNrOrPrRrSrTrUrVZ_row_cache_sizer�rr1r)r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�rr�r�r�rr�r�r�rrr�r�r�rDrErFrIrJrGrrrrr0lsv<

#				9

-a
'
$r0i�i�i�i�)rprwi�i��gb2312Zis8859_5Zis8859_6Zis8859_7Zis8859_8�koi8_rz	iso8859-1�	iso8859_2�	iso8859_3�	iso8859_4�	iso8859_9�
iso8859_10�
iso8859_13�
iso8859_14�
iso8859_15r�Zcp886Zcp874�cp1250�cp1251�cp1252�cp1253�cp1254�cp1255�cp1256�cp1257�cp1258zutf-8)Z
mule_internalZeuc_tw�euc_cn�
iso_8859_5�
iso_8859_6�
iso_8859_7�
iso_8859_8Zkoi8�latin1�latin2�latin3�latin4�latin5�latin6�latin7�latin8�latin9Z	sql_asciiZwin866Zwin874Zwin1250Zwin1251Zwin1252Zwin1253Zwin1254Zwin1255Zwin1256Zwin1257Zwin1258rbccsVxPt|�D]D\}}t|t�rBx0t|�D]\}}}|||fVq&Wq
|||fVq
WdS)N)r�r*rZr5)rPrCr�r;�i2�v2rrrr5�	s

r5cCs"xt|�D]}|dk	r
|Sq
WdS)N)r:)rPr�rrrr?�	sr?ccs<x6|D].}t|t�r.xt|�D]
}|VqWq|VqWdS)N)r*rZr:)rPr�rnrrrr:�	s


r:cCs�|d}t|t�rht|�}t|�}x0|D](}t|�}t|�|ksH||kr(td��q(W|g}|j|�|Sx|D]}t|t�rntd��qnWgSdS)Nrzarray dimensions not consistent)r*rZr�r6rZrM)rP�v0Zreq_lenZreq_inner_lengthsr�Z
inner_lengthsr$rrrr6�	s"






r6cCs"xt|�D]}|dkr
dSq
WdS)NTF)r:)rPr�rrrr7�	sr7cCs:|d}t|t�r,t|�g}|jt|��n
t|�gS|S)Nr)r*rZr�rMr8)rPror$rrrr8�	s


r8i�������)rprql���)rrrsl����)rtrull����r�r�)�r\r�warningsrr�r��structr�hashlibrZdecimalr�collectionsrr�	itertoolsr	r
Z	six.movesr�sixrr
rrrrr�uuidr�copyr�calendarrZdistutils.versionrrrbr�
__author__�jsonrr�rr�rr��objectr%rBr�r�rr�r�r�r�r�Zf_packr�r9rUr�rXr�r�r�r�Zihihih_packrZci_packr�Zbh_packr�Z	cccc_packrrArBr2r3r,r-r�rMrNrOrPrQrRrSrTrUrVrWrXrYrZr[rarfrgrkrmrnro�BINARYr�ror�r(r)r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r
r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�r�rrr&ZFLUSHZSYNCrrZ	TERMINATEr-r%r	r�r�rr.ZRESPONSE_SEVERITYr�ZRESPONSE_MSGZRESPONSE_DETAILZ
RESPONSE_HINTZRESPONSE_POSITIONZRESPONSE__POSITIONZRESPONSE__QUERYZRESPONSE_WHEREZ
RESPONSE_FILEZ
RESPONSE_LINEZRESPONSE_ROUTINErZIDLE_IN_TRANSACTIONZIDLE_IN_FAILED_TRANSACTIONr;r[r�rZr=r'r0rCr1r5r?r:r6r7r8rrrr�<module>s�$T





)
		
*l