o
    ÒèFh»9  ã                   @   s’   d Z ddlZddlmZ e ¡ ZG dd„ dƒZG dd„ deƒZG dd	„ d	eƒZ	G d
d„ deƒZ
G dd„ deƒZG dd„ deƒZe	dƒZe	dƒZdS )zSQL composition utility module
é    N)Ú
extensionsc                   @   sH   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ Zdd„ Z	dd„ Z
dS )Ú
Composablea6  
    Abstract base class for objects that can be used to compose an SQL string.

    `!Composable` objects can be passed directly to `~cursor.execute()`,
    `~cursor.executemany()`, `~cursor.copy_expert()` in place of the query
    string.

    `!Composable` objects can be joined using the ``+`` operator: the result
    will be a `Composed` instance containing the objects joined. The operator
    ``*`` is also supported with an integer argument: the result is a
    `!Composed` instance containing the left argument repeated as many times as
    requested.
    c                 C   s
   || _ d S ©N©Ú_wrapped)ÚselfÚwrapped© r	   úE/var/www/html/loop/nvenv/lib/python3.10/site-packages/psycopg2/sql.pyÚ__init__1   ó   
zComposable.__init__c                 C   s   | j j› d| j›d�S )Nú(ú))Ú	__class__Ú__name__r   ©r   r	   r	   r
   Ú__repr__4   s   zComposable.__repr__c                 C   s   t ‚)aj  
        Return the string value of the object.

        :param context: the context to evaluate the string into.
        :type context: `connection` or `cursor`

        The method is automatically invoked by `~cursor.execute()`,
        `~cursor.executemany()`, `~cursor.copy_expert()` if a `!Composable` is
        passed instead of the query string.
        )ÚNotImplementedError©r   Úcontextr	   r	   r
   Ú	as_string7   s   zComposable.as_stringc                 C   s:   t |tƒrt| gƒ| S t |tƒrt| gƒt|gƒ S tS r   )Ú
isinstanceÚComposedr   ÚNotImplemented©r   Úotherr	   r	   r
   Ú__add__D   s
   

zComposable.__add__c                 C   s   t | g| ƒS r   )r   )r   Únr	   r	   r
   Ú__mul__L   s   zComposable.__mul__c                 C   s   t | ƒt |ƒu o| j|jkS r   )Útyper   r   r	   r	   r
   Ú__eq__O   s   zComposable.__eq__c                 C   s   |   |¡ S r   )r    r   r	   r	   r
   Ú__ne__R   s   zComposable.__ne__N)r   Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r   r    r!   r	   r	   r	   r
   r   #   s    r   c                       sL   e Zd ZdZ‡ fdd„Zedd„ ƒZdd„ Zdd	„ Zd
d„ Z	dd„ Z
‡  ZS )r   a  
    A `Composable` object made of a sequence of `!Composable`.

    The object is usually created using `!Composable` operators and methods.
    However it is possible to create a `!Composed` directly specifying a
    sequence of `!Composable` as arguments.

    Example::

        >>> comp = sql.Composed(
        ...     [sql.SQL("insert into "), sql.Identifier("table")])
        >>> print(comp.as_string(conn))
        insert into "table"

    `!Composed` objects are iterable (so they can be used in `SQL.join` for
    instance).
    c                    sB   g }|D ]}t |tƒstd|›d�ƒ‚| |¡ qtƒ  |¡ d S )Nz*Composed elements must be Composable, got z instead)r   r   Ú	TypeErrorÚappendÚsuperr   )r   Úseqr   Úi©r   r	   r
   r   h   s   

ÿzComposed.__init__c                 C   ó
   t | jƒS )z+The list of the content of the `!Composed`.)Úlistr   r   r	   r	   r
   r(   r   s   
zComposed.seqc                 C   s*   g }| j D ]
}| | |¡¡ qd |¡S )NÚ )r   r&   r   Újoin)r   r   Úrvr)   r	   r	   r
   r   w   s   

zComposed.as_stringc                 C   r+   r   )Úiterr   r   r	   r	   r
   Ú__iter__}   r   zComposed.__iter__c                 C   s8   t |tƒrt| j|j ƒS t |tƒrt| j|g ƒS tS r   )r   r   r   r   r   r   r	   r	   r
   r   €   s
   

zComposed.__add__c                 C   s0   t |tƒr
t|ƒ}n	t |tƒstdƒ‚| | ¡S )a|  
        Return a new `!Composed` interposing the *joiner* with the `!Composed` items.

        The *joiner* must be a `SQL` or a string which will be interpreted as
        an `SQL`.

        Example::

            >>> fields = sql.Identifier('foo') + sql.Identifier('bar')  # a Composed
            >>> print(fields.join(', ').as_string(conn))
            "foo", "bar"

        z3Composed.join() argument must be a string or an SQL)r   ÚstrÚSQLr%   r.   )r   Újoinerr	   r	   r
   r.   ˆ   s   


ÿ
zComposed.join)r   r"   r#   r$   r   Úpropertyr(   r   r1   r   r.   Ú__classcell__r	   r	   r*   r
   r   V   s    

r   c                       sD   e Zd ZdZ‡ fdd„Zedd„ ƒZdd„ Zdd	„ Zd
d„ Z	‡  Z
S )r3   aA  
    A `Composable` representing a snippet of SQL statement.

    `!SQL` exposes `join()` and `format()` methods useful to create a template
    where to merge variable parts of a query (for instance field or table
    names).

    The *string* doesn't undergo any form of escaping, so it is not suitable to
    represent variable identifiers or values: you should only use it to pass
    constant strings representing templates or snippets of SQL statements; use
    other objects such as `Identifier` or `Literal` to represent variable
    parts.

    Example::

        >>> query = sql.SQL("select {0} from {1}").format(
        ...    sql.SQL(', ').join([sql.Identifier('foo'), sql.Identifier('bar')]),
        ...    sql.Identifier('table'))
        >>> print(query.as_string(conn))
        select "foo", "bar" from "table"
    c                    s"   t |tƒs	tdƒ‚tƒ  |¡ d S )NzSQL values must be strings)r   r2   r%   r'   r   )r   Ústringr*   r	   r
   r   µ   s   
zSQL.__init__c                 C   ó   | j S )z(The string wrapped by the `!SQL` object.r   r   r	   r	   r
   r7   º   ó   z
SQL.stringc                 C   r8   r   r   r   r	   r	   r
   r   ¿   s   zSQL.as_stringc           	      O   sÄ   g }d}t  | j¡D ]S\}}}}|rtdƒ‚|rtdƒ‚|r%| t|ƒ¡ |du r*q
| ¡ r@|r4tdƒ‚| |t|ƒ ¡ d}q
|sV|du rJtdƒ‚| || ¡ |d7 }q
| || ¡ q
t|ƒS )a^  
        Merge `Composable` objects into a template.

        :param `Composable` args: parameters to replace to numbered
            (``{0}``, ``{1}``) or auto-numbered (``{}``) placeholders
        :param `Composable` kwargs: parameters to replace to named (``{name}``)
            placeholders
        :return: the union of the `!SQL` string with placeholders replaced
        :rtype: `Composed`

        The method is similar to the Python `str.format()` method: the string
        template supports auto-numbered (``{}``), numbered (``{0}``,
        ``{1}``...), and named placeholders (``{name}``), with positional
        arguments replacing the numbered placeholders and keywords replacing
        the named ones. However placeholder modifiers (``{0!r}``, ``{0:<10}``)
        are not supported. Only `!Composable` objects can be passed to the
        template.

        Example::

            >>> print(sql.SQL("select * from {} where {} = %s")
            ...     .format(sql.Identifier('people'), sql.Identifier('id'))
            ...     .as_string(conn))
            select * from "people" where "id" = %s

            >>> print(sql.SQL("select * from {tbl} where {pkey} = %s")
            ...     .format(tbl=sql.Identifier('people'), pkey=sql.Identifier('id'))
            ...     .as_string(conn))
            select * from "people" where "id" = %s

        r   z(no format specification supported by SQLz%no format conversion supported by SQLNz6cannot switch from automatic field numbering to manualz6cannot switch from manual field numbering to automaticé   )	Ú
_formatterÚparser   Ú
ValueErrorr&   r3   ÚisdigitÚintr   )	r   ÚargsÚkwargsr/   ÚautonumÚpreÚnameÚspecÚconvr	   r	   r
   ÚformatÂ   s6    ÿÿ
z
SQL.formatc                 C   s^   g }t |ƒ}z	| t|ƒ¡ W n ty   Y t|ƒS w |D ]}| | ¡ | |¡ qt|ƒS )a  
        Join a sequence of `Composable`.

        :param seq: the elements to join.
        :type seq: iterable of `!Composable`

        Use the `!SQL` object's *string* to separate the elements in *seq*.
        Note that `Composed` objects are iterable too, so they can be used as
        argument for this method.

        Example::

            >>> snip = sql.SQL(', ').join(
            ...     sql.Identifier(n) for n in ['foo', 'bar', 'baz'])
            >>> print(snip.as_string(conn))
            "foo", "bar", "baz"
        )r0   r&   ÚnextÚStopIterationr   )r   r(   r/   Úitr)   r	   r	   r
   r.     s   ù
zSQL.join)r   r"   r#   r$   r   r5   r7   r   rG   r.   r6   r	   r	   r*   r
   r3   Ÿ   s    
@r3   c                       sH   e Zd ZdZ‡ fdd„Zedd„ ƒZedd„ ƒZdd	„ Zd
d„ Z	‡  Z
S )Ú
Identifiera*  
    A `Composable` representing an SQL identifier or a dot-separated sequence.

    Identifiers usually represent names of database objects, such as tables or
    fields. PostgreSQL identifiers follow `different rules`__ than SQL string
    literals for escaping (e.g. they use double quotes instead of single).

    .. __: https://www.postgresql.org/docs/current/static/sql-syntax-lexical.html#         SQL-SYNTAX-IDENTIFIERS

    Example::

        >>> t1 = sql.Identifier("foo")
        >>> t2 = sql.Identifier("ba'r")
        >>> t3 = sql.Identifier('ba"z')
        >>> print(sql.SQL(', ').join([t1, t2, t3]).as_string(conn))
        "foo", "ba'r", "ba""z"

    Multiple strings can be passed to the object to represent a qualified name,
    i.e. a dot-separated sequence of identifiers.

    Example::

        >>> query = sql.SQL("select {} from {}").format(
        ...     sql.Identifier("table", "field"),
        ...     sql.Identifier("schema", "table"))
        >>> print(query.as_string(conn))
        select "table"."field" from "schema"."table"

    c                    s8   |st dƒ‚|D ]}t|tƒst dƒ‚qtƒ  |¡ d S )NzIdentifier cannot be emptyz$SQL identifier parts must be strings)r%   r   r2   r'   r   )r   ÚstringsÚsr*   r	   r
   r   A  s   
ÿzIdentifier.__init__c                 C   r8   )z5A tuple with the strings wrapped by the `Identifier`.r   r   r	   r	   r
   rL   K  r9   zIdentifier.stringsc                 C   s    t | jƒdkr| jd S tdƒ‚)z0The string wrapped by the `Identifier`.
        r:   r   z2the Identifier wraps more than one than one string)Úlenr   ÚAttributeErrorr   r	   r	   r
   r7   P  s
   
ÿzIdentifier.stringc                 C   s"   | j j› dd tt| jƒ¡› d�S )Nr   z, r   )r   r   r.   ÚmapÚreprr   r   r	   r	   r
   r   Z  s   "zIdentifier.__repr__c                    s   d  ‡ fdd„| jD ƒ¡S )NÚ.c                 3   s   � | ]	}t  |ˆ ¡V  qd S r   )ÚextÚquote_ident)Ú.0rM   ©r   r	   r
   Ú	<genexpr>^  s   € z'Identifier.as_string.<locals>.<genexpr>)r.   r   r   r	   rV   r
   r   ]  s   zIdentifier.as_string)r   r"   r#   r$   r   r5   rL   r7   r   r   r6   r	   r	   r*   r
   rK   "  s    


	rK   c                   @   s$   e Zd ZdZedd„ ƒZdd„ ZdS )ÚLiteralaˆ  
    A `Composable` representing an SQL value to include in a query.

    Usually you will want to include placeholders in the query and pass values
    as `~cursor.execute()` arguments. If however you really really need to
    include a literal value in the query you can use this object.

    The string returned by `!as_string()` follows the normal :ref:`adaptation
    rules <python-types-adaptation>` for Python objects.

    Example::

        >>> s1 = sql.Literal("foo")
        >>> s2 = sql.Literal("ba'r")
        >>> s3 = sql.Literal(42)
        >>> print(sql.SQL(', ').join([s1, s2, s3]).as_string(conn))
        'foo', 'ba''r', 42

    c                 C   r8   )z%The object wrapped by the `!Literal`.r   r   r	   r	   r
   r   u  r9   zLiteral.wrappedc                 C   sv   t |tjƒr	|}nt |tjƒr|j}ntdƒ‚t | j¡}t|dƒr'| |¡ | 	¡ }t |t
ƒr9| tj|j ¡}|S )Nz(context must be a connection or a cursorÚprepare)r   rS   Ú
connectionÚcursorr%   Úadaptr   ÚhasattrrY   Ú	getquotedÚbytesÚdecodeÚ	encodingsÚencoding)r   r   ÚconnÚar/   r	   r	   r
   r   z  s   


zLiteral.as_stringN)r   r"   r#   r$   r5   r   r   r	   r	   r	   r
   rX   a  s
    
rX   c                       s>   e Zd ZdZd‡ fdd„	Zedd„ ƒZdd„ Zd	d
„ Z‡  Z	S )ÚPlaceholderaã  A `Composable` representing a placeholder for query parameters.

    If the name is specified, generate a named placeholder (e.g. ``%(name)s``),
    otherwise generate a positional placeholder (e.g. ``%s``).

    The object is useful to generate SQL queries with a variable number of
    arguments.

    Examples::

        >>> names = ['foo', 'bar', 'baz']

        >>> q1 = sql.SQL("insert into table ({}) values ({})").format(
        ...     sql.SQL(', ').join(map(sql.Identifier, names)),
        ...     sql.SQL(', ').join(sql.Placeholder() * len(names)))
        >>> print(q1.as_string(conn))
        insert into table ("foo", "bar", "baz") values (%s, %s, %s)

        >>> q2 = sql.SQL("insert into table ({}) values ({})").format(
        ...     sql.SQL(', ').join(map(sql.Identifier, names)),
        ...     sql.SQL(', ').join(map(sql.Placeholder, names)))
        >>> print(q2.as_string(conn))
        insert into table ("foo", "bar", "baz") values (%(foo)s, %(bar)s, %(baz)s)

    Nc                    sH   t |tƒrd|v rtd|›�ƒ‚n|d urtd|›�ƒ‚tƒ  |¡ d S )Nr   zinvalid name: z%expected string or None as name, got )r   r2   r=   r%   r'   r   )r   rD   r*   r	   r
   r   ©  s   
ÿzPlaceholder.__init__c                 C   r8   )zThe name of the `!Placeholder`.r   r   r	   r	   r
   rD   ³  r9   zPlaceholder.namec                 C   s.   | j d u r| jj› d�S | jj› d| j ›d�S )Nz()r   r   )r   r   r   r   r	   r	   r
   r   ¸  s   
zPlaceholder.__repr__c                 C   s   | j d urd| j › d�S dS )Nz%(z)sz%sr   r   r	   r	   r
   r   ¾  s   
zPlaceholder.as_stringr   )
r   r"   r#   r$   r   r5   rD   r   r   r6   r	   r	   r*   r
   re   Ž  s    

re   ÚNULLÚDEFAULT)r$   r7   Úpsycopg2r   rS   Ú	Formatterr;   r   r   r3   rK   rX   re   rf   rg   r	   r	   r	   r
   Ú<module>   s    3I ?-8