§
    ÅÉWc»9  ã                   ó  — d Z ddlZddlmZ  ej        ¦   «         Z G d„ d¦  «        Z G d„ de¦  «        Z G d„ d	e¦  «        Z	 G d
„ de¦  «        Z
 G d„ de¦  «        Z G d„ de¦  «        Z e	d¦  «        Z e	d¦  «        ZdS )zSQL composition utility module
é    N)Ú
extensionsc                   ó<   — e Zd ZdZd„ Zd„ Zd„ Zd„ Zd„ Zd„ Z	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                 ó   — || _         d S ©N©Ú_wrapped)ÚselfÚwrappeds     ú./usr/lib/python3/dist-packages/psycopg2/sql.pyÚ__init__zComposable.__init__1   s   € ØˆŒˆˆó    c                 ó0   — | j         j        › d| j        ›d�S )Nú(ú))Ú	__class__Ú__name__r	   ©r
   s    r   Ú__repr__zComposable.__repr__4   s    € Ø”.Ô)Ð>Ð>¨D¬MÐ>Ð>Ð>Ð>r   c                 ó   — 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
   Úcontexts     r   Ú	as_stringzComposable.as_string7   s
   € õ "Ð!r   c                 óÌ   — t          |t          ¦  «        rt          | g¦  «        |z   S t          |t          ¦  «        r!t          | g¦  «        t          |g¦  «        z   S t          S r   )Ú
isinstanceÚComposedr   ÚNotImplemented©r
   Úothers     r   Ú__add__zComposable.__add__D   s`   € Ý�e�XÑ&Ô&ð 	,Ý˜T˜FÑ#Ô# eÑ+Ð+Ý�e�ZÑ(Ô(ð 	"Ý˜T˜FÑ#Ô#¥h°¨wÑ&7Ô&7Ñ7Ð7å!Ð!r   c                 ó(   — t          | g|z  ¦  «        S r   )r   )r
   Úns     r   Ú__mul__zComposable.__mul__L   s   € Ý˜˜ ™
Ñ#Ô#Ð#r   c                 ó^   — t          | ¦  «        t          |¦  «        u o| j        |j        k    S r   )Útyper	   r   s     r   Ú__eq__zComposable.__eq__O   s'   € Ý�D‰zŒz�T %™[œ[Ð(ÐL¨T¬]¸e¼nÒ-LÐLr   c                 ó.   — |                       |¦  «         S r   )r'   r   s     r   Ú__ne__zComposable.__ne__R   s   € Ø—;’;˜uÑ%Ô%Ð%Ð%r   N)r   Ú
__module__Ú__qualname__Ú__doc__r   r   r   r!   r$   r'   r)   © r   r   r   r   #   sŠ   € € € € € ðð ð ð  ð  ð?ð ?ð ?ð"ð "ð "ð"ð "ð "ð$ð $ð $ðMð Mð Mð&ð &ð &ð &ð &r   r   c                   óP   ‡ — e Zd ZdZˆ fd„Zed„ ¦   «         Zd„ Zd„ Zd„ Z	d„ Z
ˆ x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                 óÒ   •— g }|D ]?}t          |t          ¦  «        st          d|›d�¦  «        ‚|                     |¦  «         Œ@t	          ¦   «                              |¦  «         d S )Nz*Composed elements must be Composable, got z instead)r   r   Ú	TypeErrorÚappendÚsuperr   )r
   Úseqr   Úir   s       €r   r   zComposed.__init__h   s€   ø€ ØˆØð 	ð 	ˆAÝ˜a¥Ñ,Ô,ð PÝØNÀÐNÐNÐNñPô Pð Pà�NŠN˜1ÑÔÐÐå‰Œ×Ò˜Ñ!Ô!Ð!Ð!Ð!r   c                 ó*   — t          | j        ¦  «        S )z+The list of the content of the `!Composed`.)Úlistr	   r   s    r   r3   zComposed.seqr   s   € õ �D”MÑ"Ô"Ð"r   c                 ó”   — g }| j         D ]*}|                     |                     |¦  «        ¦  «         Œ+d                     |¦  «        S )NÚ )r	   r1   r   Újoin)r
   r   Úrvr4   s       r   r   zComposed.as_stringw   sI   € ØˆØ”ð 	,ð 	,ˆAØ�IŠI�a—k’k 'Ñ*Ô*Ñ+Ô+Ð+Ð+Ø�wŠw�r‰{Œ{Ðr   c                 ó*   — t          | j        ¦  «        S r   )Úiterr	   r   s    r   Ú__iter__zComposed.__iter__}   s   € Ý�D”MÑ"Ô"Ð"r   c                 óÌ   — t          |t          ¦  «        rt          | j        |j        z   ¦  «        S t          |t          ¦  «        rt          | j        |gz   ¦  «        S t          S r   )r   r   r	   r   r   r   s     r   r!   zComposed.__add__€   sZ   € Ý�e�XÑ&Ô&ð 	<Ý˜DœM¨E¬NÑ:Ñ;Ô;Ð;Ý�e�ZÑ(Ô(ð 	"Ý˜DœM¨U¨GÑ3Ñ4Ô4Ð4å!Ð!r   c                 ó¾   — 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ÚSQLr0   r9   )r
   Újoiners     r   r9   zComposed.joinˆ   sc   € õ �f�cÑ"Ô"ð 	GÝ˜‘[”[ˆFˆFÝ˜F¥CÑ(Ô(ð 	GÝØEñGô Gð Gð �{Š{˜4Ñ Ô Ð r   )r   r*   r+   r,   r   Úpropertyr3   r   r=   r!   r9   Ú__classcell__©r   s   @r   r   r   V   s™   ø€ € € € € ðð ð""ð "ð "ð "ð "ð ð#ð #ñ „Xð#ðð ð ð#ð #ð #ð"ð "ð "ð!ð !ð !ð !ð !ð !ð !r   r   c                   óJ   ‡ — e Zd ZdZˆ fd„Zed„ ¦   «         Zd„ Zd„ Zd„ Z	ˆ xZ
S )rA   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                 ó’   •— t          |t          ¦  «        st          d¦  «        ‚t          ¦   «                              |¦  «         d S )NzSQL values must be strings)r   r@   r0   r2   r   )r
   Ústringr   s     €r   r   zSQL.__init__µ   sB   ø€ Ý˜&¥#Ñ&Ô&ð 	:ÝÐ8Ñ9Ô9Ð9Ý‰Œ×Ò˜Ñ Ô Ð Ð Ð r   c                 ó   — | j         S )z(The string wrapped by the `!SQL` object.r   r   s    r   rH   z
SQL.stringº   ó   € ð Œ}Ðr   c                 ó   — | j         S r   r   r   s     r   r   zSQL.as_string¿   s
   € ØŒ}Ðr   c                 óF  — g }d}t                                | j        ¦  «        D ]ï\  }}}}|rt          d¦  «        ‚|rt          d¦  «        ‚|r"|                     t          |¦  «        ¦  «         |€ŒP|                     ¦   «         r<|rt          d¦  «        ‚|                     |t          |¦  «                 ¦  «         d}Œ |s2|€t          d¦  «        ‚|                     ||         ¦  «         |dz  }ŒÔ|                     ||         ¦  «         Œð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	   Ú
ValueErrorr1   rA   ÚisdigitÚintr   )	r
   ÚargsÚkwargsr:   ÚautonumÚpreÚnameÚspecÚconvs	            r   Úformatz
SQL.formatÂ   sP  € ð@ ˆØˆÝ%/×%5Ò%5°d´mÑ%DÔ%Dð 	(ð 	(Ñ!ˆC��t˜TØð MÝ Ð!KÑLÔLÐLØð JÝ Ð!HÑIÔIÐIØð $Ø—	’	�#˜c™(œ(Ñ#Ô#Ð#àˆ|Øà�|Š|‰~Œ~ð (Øð RÝ$ØPñRô Rð Rà—	’	˜$�s 4™yœyœ/Ñ*Ô*Ð*Ø��àð (Ø�?Ý$ØPñRô Rð Rà—	’	˜$˜wœ-Ñ(Ô(Ð(Ø˜1‘��ð —	’	˜& œ,Ñ'Ô'Ð'Ð'å˜‰|Œ|Ðr   c                 ó  — g }t          |¦  «        }	 |                     t          |¦  «        ¦  «         |D ],}|                     | ¦  «         |                     |¦  «         Œ-n# t          $ r Y nw xY w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"
        )r<   r1   ÚnextÚStopIterationr   )r
   r3   r:   Úitr4   s        r   r9   zSQL.join  s—   € ð$ ˆÝ�#‰YŒYˆð	Ø�IŠI•d˜2‘h”hÑÔÐð ð ð �Ø—	’	˜$‘”�Ø—	’	˜!‘”��ðøõ ð 	ð 	ð 	ØˆDð	øøøõ ˜‰|Œ|Ðs   “"A% Á%
A2Á1A2)r   r*   r+   r,   r   rC   rH   r   rZ   r9   rD   rE   s   @r   rA   rA   Ÿ   s‹   ø€ € € € € ðð ð*!ð !ð !ð !ð !ð
 ðð ñ „Xððð ð ð>ð >ð >ð@ð ð ð ð ð ð r   rA   c                   óZ   ‡ — e Zd ZdZˆ fd„Zed„ ¦   «         Zed„ ¦   «         Zd„ Zd„ Z	ˆ x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                 ó¾   •— |st          d¦  «        ‚|D ]&}t          |t          ¦  «        st          d¦  «        ‚Œ't          ¦   «                              |¦  «         d S )NzIdentifier cannot be emptyz$SQL identifier parts must be strings)r0   r   r@   r2   r   )r
   ÚstringsÚsr   s      €r   r   zIdentifier.__init__A  ss   ø€ Øð 	:ÝÐ8Ñ9Ô9Ð9àð 	Hð 	HˆAÝ˜a¥Ñ%Ô%ð HÝÐ FÑGÔGÐGðHõ 	‰Œ×Ò˜Ñ!Ô!Ð!Ð!Ð!r   c                 ó   — | j         S )z5A tuple with the strings wrapped by the `Identifier`.r   r   s    r   rb   zIdentifier.stringsK  rJ   r   c                 ój   — t          | j        ¦  «        dk    r| j        d         S t          d¦  «        ‚)z0The string wrapped by the `Identifier`.
        rM   r   z2the Identifier wraps more than one than one string)Úlenr	   ÚAttributeErrorr   s    r   rH   zIdentifier.stringP  s>   € õ ˆtŒ}ÑÔ Ò"Ð"Ø”= Ô#Ð#å ØDñFô Fð Fr   c                 ó|   — | j         j        › dd                     t          t          | j        ¦  «        ¦  «        › d�S )Nr   z, r   )r   r   r9   ÚmapÚreprr	   r   s    r   r   zIdentifier.__repr__Z  s6   € Ø”.Ô)ÐRÐR¨D¯IªIµc½$ÀÄÑ6NÔ6NÑ,OÔ,OÐRÐRÐRÐRr   c                 óP   ‡— d                      ˆfd„| j        D ¦   «         ¦  «        S )Nú.c              3   óB   •K  — | ]}t          j        |‰¦  «        V — Œd S r   )ÚextÚquote_ident)Ú.0rc   r   s     €r   ú	<genexpr>z'Identifier.as_string.<locals>.<genexpr>^  s/   øè è € ÐKÐK¸�œ¨¨7Ñ3Ô3ÐKÐKÐKÐKÐKÐKr   )r9   r	   r   s    `r   r   zIdentifier.as_string]  s,   ø€ Ø�xŠxÐKÐKÐKÐK¸T¼]ÐKÑKÔKÑKÔKÐKr   )r   r*   r+   r,   r   rC   rb   rH   r   r   rD   rE   s   @r   r`   r`   "  s£   ø€ € € € € ðð ð<"ð "ð "ð "ð "ð ðð ñ „Xðð ðFð Fñ „XðFðSð Sð SðLð Lð Lð Lð Lð Lð Lr   r`   c                   ó.   — e Zd ZdZed„ ¦   «         Z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                 ó   — | j         S )z%The object wrapped by the `!Literal`.r   r   s    r   r   zLiteral.wrappedu  rJ   r   c                 óÄ  — t          |t          j        ¦  «        r|}n1t          |t          j        ¦  «        r|j        }nt	          d¦  «        ‚t          j        | j        ¦  «        }t          |d¦  «        r|                     |¦  «         | 	                    ¦   «         }t          |t          ¦  «        r*|                     t          j        |j                 ¦  «        }|S )Nz(context must be a connection or a cursorÚprepare)r   rn   Ú
connectionÚcursorr0   Úadaptr	   Úhasattrrv   Ú	getquotedÚbytesÚdecodeÚ	encodingsÚencoding)r
   r   ÚconnÚar:   s        r   r   zLiteral.as_stringz  s¼   € å�g�sœ~Ñ.Ô.ð 	HØˆDˆDÝ˜¥¤Ñ,Ô,ð 	HØÔ%ˆDˆDåÐFÑGÔGÐGåŒI�d”mÑ$Ô$ˆÝ�1�iÑ Ô ð 	Ø�IŠI�d‰OŒOˆOà�[Š[‰]Œ]ˆÝ�b�%Ñ Ô ð 	9Ø—’�3œ=¨¬Ô7Ñ8Ô8ˆBàˆ	r   N)r   r*   r+   r,   rC   r   r   r-   r   r   rs   rs   a  sH   € € € € € ðð ð& ðð ñ „Xððð ð ð ð r   rs   c                   óF   ‡ — e Zd ZdZdˆ fd„	Zed„ ¦   «         Zd„ Zd„ Zˆ x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                 óÊ   •— t          |t          ¦  «        rd|v rt          d|›�¦  «        ‚n|�t          d|›�¦  «        ‚t	          ¦   «                              |¦  «         d S )Nr   zinvalid name: z%expected string or None as name, got )r   r@   rP   r0   r2   r   )r
   rW   r   s     €r   r   zPlaceholder.__init__©  sx   ø€ Ý�d�CÑ Ô ð 	NØ�dˆ{ˆ{Ý Ð!:°$Ð!:Ð!:Ñ;Ô;Ð;ð ð ÐÝÐLÀDÐLÐLÑMÔMÐMå‰Œ×Ò˜ÑÔÐÐÐr   c                 ó   — | j         S )zThe name of the `!Placeholder`.r   r   s    r   rW   zPlaceholder.name³  rJ   r   c                 ó\   — | j         €| j        j        › d�S | j        j        › d| j         ›d�S )Nz()r   r   )r	   r   r   r   s    r   r   zPlaceholder.__repr__¸  s<   € ØŒ=Ð Ø”nÔ-Ð1Ð1Ð1Ð1à”nÔ-ÐBÐB°´ÐBÐBÐBÐBr   c                 ó*   — | j         �d| j         › d�S dS )Nz%(z)sz%sr   r   s     r   r   zPlaceholder.as_string¾  s"   € ØŒ=Ð$Ø)˜œÐ)Ð)Ð)Ð)à�4r   r   )
r   r*   r+   r,   r   rC   rW   r   r   rD   rE   s   @r   rƒ   rƒ   Ž  sƒ   ø€ € € € € ðð ð4ð ð ð ð ð ð ðð ñ „XððCð Cð Cðð ð ð ð ð ð r   rƒ   ÚNULLÚDEFAULT)r,   rH   Úpsycopg2r   rn   Ú	FormatterrN   r   r   rA   r`   rs   rƒ   rˆ   r‰   r-   r   r   ú<module>rŒ      sv  ððð ð4 €€€à &Ð &Ð &Ð &Ð &Ð &ð ˆVÔÑÔ€
ð0&ð 0&ð 0&ð 0&ð 0&ñ 0&ô 0&ð 0&ðfF!ð F!ð F!ð F!ð F!ˆzñ F!ô F!ð F!ðR@ð @ð @ð @ð @ˆ*ñ @ô @ð @ðF<Lð <Lð <Lð <Lð <L�ñ <Lô <Lð <Lð~*ð *ð *ð *ð *ˆjñ *ô *ð *ðZ4ð 4ð 4ð 4ð 4�*ñ 4ô 4ð 4ðp €sˆ6�{„{€Ø
ˆ#ˆi‰.Œ.€€€r   