o
    h&                     @   sB   d dl Z ddlmZ ddlmZ ddlmZ G dd dejZdS )    N   )connresource)cursor)
exceptionsc                       sF  e Zd ZdZdZ fddZejdefddZ	ejdefdd	Z
ejdefd
dZejdd Zejdd ZejddddejfddZejddddZejddddZejdddddZejdddd Zejddd!d"Zejddd#efd$d%Zd&d' Zd(d) Zd*d+ Z fd,d-Zd.d/ Z  ZS )0PreparedStatementz)A representation of a prepared statement.)_state_query_last_statusc                    s*   t  | || _|| _|  d | _d S N)super__init__r   r   attachr	   )self
connectionquerystate	__class__ i/var/www/html/construction_image-detection-poc/venv/lib/python3.10/site-packages/asyncpg/prepared_stmt.pyr      s
   
zPreparedStatement.__init__returnc                 C   s   | j jS )zVReturn the name of this prepared statement.

        .. versionadded:: 0.25.0
        )r   namer   r   r   r   get_name   s   zPreparedStatement.get_namec                 C   s   | j S )zReturn the text of the query for this prepared statement.

        Example::

            stmt = await connection.prepare('SELECT $1::int')
            assert stmt.get_query() == "SELECT $1::int"
        )r   r   r   r   r   	get_query#   s   	zPreparedStatement.get_queryc                 C   s   | j du r| j S | j  S )zReturn the status of the executed command.

        Example::

            stmt = await connection.prepare('CREATE TABLE mytab (a int)')
            await stmt.fetch()
            assert stmt.get_statusmsg() == "CREATE TABLE"
        N)r	   decoder   r   r   r   get_statusmsg.   s   


zPreparedStatement.get_statusmsgc                 C   
   | j  S )a  Return a description of statement parameters types.

        :return: A tuple of :class:`asyncpg.types.Type`.

        Example::

            stmt = await connection.prepare('SELECT ($1::int, $2::text)')
            print(stmt.get_parameters())

            # Will print:
            #   (Type(oid=23, name='int4', kind='scalar', schema='pg_catalog'),
            #    Type(oid=25, name='text', kind='scalar', schema='pg_catalog'))
        )r   _get_parametersr   r   r   r   get_parameters<   s   
z PreparedStatement.get_parametersc                 C   r   )a  Return a description of relation attributes (columns).

        :return: A tuple of :class:`asyncpg.types.Attribute`.

        Example::

            st = await self.con.prepare('''
                SELECT typname, typnamespace FROM pg_type
            ''')
            print(st.get_attributes())

            # Will print:
            #   (Attribute(
            #       name='typname',
            #       type=Type(oid=19, name='name', kind='scalar',
            #                 schema='pg_catalog')),
            #    Attribute(
            #       name='typnamespace',
            #       type=Type(oid=26, name='oid', kind='scalar',
            #                 schema='pg_catalog')))
        )r   _get_attributesr   r   r   r   get_attributesM   s   
z PreparedStatement.get_attributesN)prefetchtimeoutc             	   G   s    t | j| j| j|||| jjS )ab  Return a *cursor factory* for the prepared statement.

        :param args: Query arguments.
        :param int prefetch: The number of rows the *cursor iterator*
                             will prefetch (defaults to ``50``.)
        :param float timeout: Optional timeout in seconds.

        :return: A :class:`~cursor.CursorFactory` object.
        )r   CursorFactory_connectionr   r   record_class)r   r"   r#   argsr   r   r   r   f   s   zPreparedStatement.cursorF)analyzec                   s   d}|r
|d7 }n|d7 }|| j j7 }|rA| j }| I dH  z| jj|g|R  I dH }W | I dH  n| I dH  w | jj|g|R  I dH }t|S )a  Return the execution plan of the statement.

        :param args: Query arguments.
        :param analyze: If ``True``, the statement will be executed and
                        the run time statitics added to the return value.

        :return: An object representing the execution plan.  This value
                 is actually a deserialized JSON output of the SQL
                 ``EXPLAIN`` command.
        zEXPLAIN (FORMAT JSON, VERBOSEz, ANALYZE) z) N)	r   r   r%   transactionstartfetchvalrollbackjsonloads)r   r(   r'   r   trdatar   r   r   explain|   s   

 
zPreparedStatement.explain)r#   c                   s   |  |d|I dH }|S )a  Execute the statement and return a list of :class:`Record` objects.

        :param str query: Query text
        :param args: Query arguments
        :param float timeout: Optional timeout value in seconds.

        :return: A list of :class:`Record` instances.
        r   N _PreparedStatement__bind_executer   r#   r'   r0   r   r   r   fetch   s   
zPreparedStatement.fetchr   )columnr#   c                   s*   |  |d|I dH }|sdS |d | S )a:  Execute the statement and return a value in the first row.

        :param args: Query arguments.
        :param int column: Numeric index within the record of the value to
                           return (defaults to 0).
        :param float timeout: Optional timeout value in seconds.
                            If not specified, defaults to the value of
                            ``command_timeout`` argument to the ``Connection``
                            instance constructor.

        :return: The value of the specified column of the first record.
        r   Nr   r2   )r   r6   r#   r'   r0   r   r   r   r+      s
   zPreparedStatement.fetchvalc                   s&   |  |d|I dH }|sdS |d S )a  Execute the statement and return the first row.

        :param str query: Query text
        :param args: Query arguments
        :param float timeout: Optional timeout value in seconds.

        :return: The first row as a :class:`Record` instance.
        r   Nr   r2   r4   r   r   r   fetchrow   s
   
zPreparedStatement.fetchrowc                        fddI dH S )a  Execute the statement and return a list of :class:`Record` objects.

        :param args: Query arguments.
        :param float timeout: Optional timeout value in seconds.

        :return: A list of :class:`Record` instances.

        .. versionadded:: 0.30.0
        c                       | j j dddS )N Tportal_namer#   return_rowsbind_execute_manyr   protocolr'   r   r#   r   r   <lambda>       z-PreparedStatement.fetchmany.<locals>.<lambda>N_PreparedStatement__do_executer   r'   r#   r   rB   r   	fetchmany   s   
zPreparedStatement.fetchmanyr#   c                   r8   )a:  Execute the statement for each sequence of arguments in *args*.

        :param args: An iterable containing sequences of arguments.
        :param float timeout: Optional timeout value in seconds.
        :return None: This method discards the results of the operations.

        .. versionadded:: 0.22.0
        c                    r9   )Nr:   Fr;   r>   r@   rB   r   r   rC      rD   z/PreparedStatement.executemany.<locals>.<lambda>NrE   rG   r   rB   r   executemany   s   

zPreparedStatement.executemanyc                    sH   | j j}z||I d H W S  tjy#   | j  I d H  | j   w r
   )r%   	_protocolr   OutdatedSchemaCacheErrorreload_schema_stater   mark_closed)r   executorrA   r   r   r   __do_execute   s   
zPreparedStatement.__do_executec                    s0     fddI d H \}}}|_|S )Nc                    s   |  j ddS )Nr:   T)bind_executer   r@   r'   limitr   r#   r   r   rC     s    z2PreparedStatement.__bind_execute.<locals>.<lambda>)rF   r	   )r   r'   rR   r#   r0   status_r   rQ   r   __bind_execute
  s   z PreparedStatement.__bind_executec                 C   s   | j jrtd|d S )Nz?cannot call PreparedStmt.{}(): the prepared statement is closed)r   closedr   InterfaceErrorformatr   	meth_namer   r   r   _check_open  s   zPreparedStatement._check_openc                    s   |  | t | d S r
   )r[   r   _check_conn_validityrY   r   r   r   r\     s   
z&PreparedStatement._check_conn_validityc                 C   s   | j   | j| j  d S r
   )r   detachr%   _maybe_gc_stmtr   r   r   r   __del__  s   
zPreparedStatement.__del__)__name__
__module____qualname____doc__	__slots__r   r   guardedstrr   r   r   r   r!   r   r$   r1   r5   r+   r7   rH   floatrI   rF   r3   r[   r\   r_   __classcell__r   r   r   r   r      sJ    


)r   )r-   r:   r   r   r   ConnectionResourcer   r   r   r   r   <module>   s
   