
    :"ri7&                     b    d dl Z d dlZddlmZ ddlmZ ddlmZ  G d dej                        Zy)    N   )connresource)cursor)
exceptionsc                   r    e Zd ZdZdZ fdZej                  defd       Z	ej                  defd       Z
ej                  defd       Zej                  d        Zej                  d	        Zej                  d
d
ddej                  fd       Zej                  ddd       Zej                  d
dd       Zej                  dd
dd       Zej                  d
dd       Zej                  d
dd       Zej                  d
ddej,                  e   fd       Zd Zd Zd Z fdZd Z xZS )PreparedStatementz)A representation of a prepared statement.)_state_query_last_statusc                 n    t         |   |       || _        || _        |j	                          d | _        y N)super__init__r	   r
   attachr   )self
connectionquerystate	__class__s       ]/var/www/fortnox.pascalinesoft.com/venv/lib/python3.12/site-packages/asyncpg/prepared_stmt.pyr   zPreparedStatement.__init__   s/    $     returnc                 .    | j                   j                  S )zVReturn the name of this prepared statement.

        .. versionadded:: 0.25.0
        )r	   namer   s    r   get_namezPreparedStatement.get_name   s     {{r   c                     | 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   s    r   	get_queryzPreparedStatement.get_query$   s     {{r   c                 f    | j                   | j                   S | j                   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"
        )r   decoder   s    r   get_statusmsgzPreparedStatement.get_statusmsg/   s1     $$$$  ''))r   c                 6    | j                   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   s    r   get_parametersz PreparedStatement.get_parameters=   s     {{**,,r   c                 6    | j                   j                         S )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   s    r   get_attributesz PreparedStatement.get_attributesN   s    . {{**,,r   N)prefetchtimeoutc          	          t        j                  | 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)   argss       r   r   zPreparedStatement.cursorg   sD     ##KKKKKK$$
 	
r   F)analyzec                   K   d}|r|dz  }n|dz  }|| j                   j                  z  }|rq| j                  j                         }|j	                          d{    	  | j                  j
                  |g|  d{   }|j                          d{    n% | j                  j
                  |g|  d{   }t        j                  |      S 7 }7 Y7 C# |j                          d{  7   w xY w7 =w)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datas         r   explainzPreparedStatement.explain}   s      0]"ETME""" !!--/B((*$6T--66uDtDDkkm##2))225@4@@Dzz$ D#bkkm##@sf   AC>CC>  C  CC C>C%C>>C<?C>C C>C92C53C99C>)r)   c                H   K   | j                  |d|       d{   }|S 7 w)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.   r8   s       r   fetchzPreparedStatement.fetch   s)      ((q':: ;s   " "r   )columnr)   c                Z   K   | j                  |d|       d{   }|sy|d   |   S 7 w)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   r;   )r   r?   r)   r.   r8   s        r   r3   zPreparedStatement.fetchval   s8      ((q'::Awv ;s   +)+c                T   K   | j                  |d|       d{   }|sy|d   S 7 w)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   r;   r=   s       r   fetchrowzPreparedStatement.fetchrow   s3      ((q'::Aw ;s   (&(c                P    K    j                   fd       d{   S 7 w)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                 D    | j                  j                  dd      S )N Tportal_namer)   return_rowsbind_execute_manyr	   protocolr.   r   r)   s    r   <lambda>z-PreparedStatement.fetchmany.<locals>.<lambda>   s*    X77  8  r   N_PreparedStatement__do_executer   r.   r)   s   ```r   	fetchmanyzPreparedStatement.fetchmany   s+      &&
 
 	
 
   &$&r)   c                P    K    j                   fd       d{   S 7 w)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                 D    | j                  j                  dd      S )NrE   FrF   rI   rK   s    r   rM   z/PreparedStatement.executemany.<locals>.<lambda>   s*    X77! 8  r   NrN   rP   s   ```r   executemanyzPreparedStatement.executemany   s+      &&  	 rR   c                   K   | j                   j                  }	  ||       d {   S 7 # t        j                  $ r? | j                   j	                          d {  7   | j
                  j                           w xY wwr   )r,   	_protocolr   OutdatedSchemaCacheErrorreload_schema_stater	   mark_closed)r   executorrL   s      r   __do_executezPreparedStatement.__do_execute   sn     ##--		!(++++22 	""66888
 KK##%	s1   B, *, B, 0A>A!A>>Bc                 n    K    j                   fd       d {   \  }}}| _        |S 7 w)Nc                 D    | j                  j                  dd      S )NrE   T)bind_executer	   )rL   r.   limitr   r)   s    r   rM   z2PreparedStatement.__bind_execute.<locals>.<lambda>  s#    X22T2udG= r   )rO   r   )r   r.   r`   r)   r8   status_s   ````   r   __bind_executez PreparedStatement.__bind_execute  s;      $ 1 1=!> >fa #	>s   535c                 x    | j                   j                  r$t        j                  dj	                  |            y )Nz?cannot call PreparedStmt.{}(): the prepared statement is closed)r	   closedr   InterfaceErrorformat)r   	meth_names     r   _check_openzPreparedStatement._check_open  s8    ;;++3396)3DF F r   c                 F    | j                  |       t        | 	  |       y r   )ri   r   _check_conn_validity)r   rh   r   s     r   rk   z&PreparedStatement._check_conn_validity  s    #$Y/r   c                     | j                   j                          | j                  j                  | j                          y r   )r	   detachr,   _maybe_gc_stmtr   s    r   __del__zPreparedStatement.__del__  s*    ''4r   )__name__
__module____qualname____doc__	__slots__r   r   guardedstrr   r   r!   r$   r'   r   r+   r9   r>   r3   rB   rQ   typingOptionalfloatrU   rO   r<   ri   rk   ro   __classcell__)r   s   @r   r   r      s   34I!  #     3   *s * * - -  - -0 %)
 & 4 4
 
* +0 '  ' R )- 
 
 +,d  $ ,0   /3 
 
( IM &//%2H  $F05r   r   )r5   rw   rE   r   r   r   ConnectionResourcer    r   r   <module>r}      s*        N577 N5r   