ó
º9—^c           @   sä   d  Z  d d l Z d d l m Z d d l m Z m Z e j ƒ  Z	 d e
 f d „  ƒ  YZ d e f d „  ƒ  YZ d	 e f d
 „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ e d ƒ Z e d ƒ Z d S(   s   SQL composition utility module
iÿÿÿÿN(   t
   extensions(   t   PY3t   string_typest
   Composablec           B   sM   e  Z d  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z	 RS(   s6  
    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(   t   _wrapped(   t   selft   wrapped(    (    s.   /tmp/pip-unpacked-wheel-WyN9Ij/psycopg2/sql.pyt   __init__1   s    c         C   s   d |  j  j |  j f S(   Ns   %s(%r)(   t	   __class__t   __name__R   (   R   (    (    s.   /tmp/pip-unpacked-wheel-WyN9Ij/psycopg2/sql.pyt   __repr__4   s    c         C   s
   t  ‚ d S(   sj  
        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.
        N(   t   NotImplementedError(   R   t   context(    (    s.   /tmp/pip-unpacked-wheel-WyN9Ij/psycopg2/sql.pyt	   as_string7   s    c         C   sQ   t  | t ƒ r  t |  g ƒ | St  | t ƒ rI t |  g ƒ t | g ƒ St Sd  S(   N(   t
   isinstancet   ComposedR   t   NotImplemented(   R   t   other(    (    s.   /tmp/pip-unpacked-wheel-WyN9Ij/psycopg2/sql.pyt   __add__D   s
    c         C   s   t  |  g | ƒ S(   N(   R   (   R   t   n(    (    s.   /tmp/pip-unpacked-wheel-WyN9Ij/psycopg2/sql.pyt   __mul__L   s    c         C   s(   t  |  ƒ t  | ƒ k o' |  j | j k S(   N(   t   typeR   (   R   R   (    (    s.   /tmp/pip-unpacked-wheel-WyN9Ij/psycopg2/sql.pyt   __eq__O   s    c         C   s   |  j  | ƒ S(   N(   R   (   R   R   (    (    s.   /tmp/pip-unpacked-wheel-WyN9Ij/psycopg2/sql.pyt   __ne__R   s    (
   R	   t
   __module__t   __doc__R   R
   R   R   R   R   R   (    (    (    s.   /tmp/pip-unpacked-wheel-WyN9Ij/psycopg2/sql.pyR   #   s   						R   c           B   sJ   e  Z d  Z d „  Z e d „  ƒ Z d „  Z d „  Z d „  Z d „  Z	 RS(   s  
    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         C   s`   g  } x= | D]5 } t  | t ƒ s5 t d | ƒ ‚ n  | j | ƒ q Wt t |  ƒ j | ƒ d  S(   Ns4   Composed elements must be Composable, got %r instead(   R   R   t	   TypeErrort   appendt   superR   R   (   R   t   seqR   t   i(    (    s.   /tmp/pip-unpacked-wheel-WyN9Ij/psycopg2/sql.pyR   h   s    c         C   s   t  |  j ƒ S(   s+   The list of the content of the `!Composed`.(   t   listR   (   R   (    (    s.   /tmp/pip-unpacked-wheel-WyN9Ij/psycopg2/sql.pyR   r   s    c         C   s=   g  } x' |  j  D] } | j | j | ƒ ƒ q Wd j | ƒ S(   Nt    (   R   R   R   t   join(   R   R   t   rvR   (    (    s.   /tmp/pip-unpacked-wheel-WyN9Ij/psycopg2/sql.pyR   w   s    c         C   s   t  |  j ƒ S(   N(   t   iterR   (   R   (    (    s.   /tmp/pip-unpacked-wheel-WyN9Ij/psycopg2/sql.pyt   __iter__}   s    c         C   sN   t  | t ƒ r# t |  j | j ƒ St  | t ƒ rF t |  j | g ƒ St Sd  S(   N(   R   R   R   R   R   (   R   R   (    (    s.   /tmp/pip-unpacked-wheel-WyN9Ij/psycopg2/sql.pyR   €   s
    c         C   sI   t  | t ƒ r t | ƒ } n t  | t ƒ s< t d ƒ ‚ n  | j |  ƒ S(   s|  
        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"

        s3   Composed.join() argument must be a string or an SQL(   R   R   t   SQLR   R!   (   R   t   joiner(    (    s.   /tmp/pip-unpacked-wheel-WyN9Ij/psycopg2/sql.pyR!   ˆ   s    (
   R	   R   R   R   t   propertyR   R   R$   R   R!   (    (    (    s.   /tmp/pip-unpacked-wheel-WyN9Ij/psycopg2/sql.pyR   V   s   	
			R%   c           B   sA   e  Z d  Z d „  Z e d „  ƒ Z d „  Z d „  Z d „  Z RS(   sA  
    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         C   s8   t  | t ƒ s t d ƒ ‚ n  t t |  ƒ j | ƒ d  S(   Ns   SQL values must be strings(   R   R   R   R   R%   R   (   R   t   string(    (    s.   /tmp/pip-unpacked-wheel-WyN9Ij/psycopg2/sql.pyR   µ   s    c         C   s   |  j  S(   s(   The string wrapped by the `!SQL` object.(   R   (   R   (    (    s.   /tmp/pip-unpacked-wheel-WyN9Ij/psycopg2/sql.pyR(   º   s    c         C   s   |  j  S(   N(   R   (   R   R   (    (    s.   /tmp/pip-unpacked-wheel-WyN9Ij/psycopg2/sql.pyR   ¿   s    c   	      O   s(  g  } d } xt  j |  j ƒ D]û \ } } } } | rF t d ƒ ‚ n  | r[ t d ƒ ‚ n  | rw | j t | ƒ ƒ n  | d k r‰ q n  | j ƒ  rÊ | rª t d ƒ ‚ n  | j | t | ƒ ƒ d } q | s	| d k rë t d ƒ ‚ n  | j | | ƒ | d 7} q | j | | ƒ q Wt	 | ƒ S(   s^  
        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

        i    s(   no format specification supported by SQLs%   no format conversion supported by SQLs6   cannot switch from automatic field numbering to manuals6   cannot switch from manual field numbering to automatici   N(
   t
   _formattert   parseR   t
   ValueErrorR   R%   t   Nonet   isdigitt   intR   (	   R   t   argst   kwargsR"   t   autonumt   pret   namet   spect   conv(    (    s.   /tmp/pip-unpacked-wheel-WyN9Ij/psycopg2/sql.pyt   formatÂ   s2     %	c         C   sr   g  } t  | ƒ } y | j t | ƒ ƒ Wn t k
 r< n, Xx( | D]  } | j |  ƒ | j | ƒ qD Wt | ƒ S(   s  
        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#   R   t   nextt   StopIterationR   (   R   R   R"   t   itR   (    (    s.   /tmp/pip-unpacked-wheel-WyN9Ij/psycopg2/sql.pyR!     s    (	   R	   R   R   R   R'   R(   R   R6   R!   (    (    (    s.   /tmp/pip-unpacked-wheel-WyN9Ij/psycopg2/sql.pyR%   Ÿ   s   			@t
   Identifierc           B   sG   e  Z d  Z d „  Z e d „  ƒ Z e d „  ƒ Z d „  Z d „  Z RS(   s*  
    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         G   s^   | s t  d ƒ ‚ n  x, | D]$ } t | t ƒ s t  d ƒ ‚ q q Wt t |  ƒ j | ƒ d  S(   Ns   Identifier cannot be emptys$   SQL identifier parts must be strings(   R   R   R   R   R:   R   (   R   t   stringst   s(    (    s.   /tmp/pip-unpacked-wheel-WyN9Ij/psycopg2/sql.pyR   A  s    c         C   s   |  j  S(   s5   A tuple with the strings wrapped by the `Identifier`.(   R   (   R   (    (    s.   /tmp/pip-unpacked-wheel-WyN9Ij/psycopg2/sql.pyR;   K  s    c         C   s0   t  |  j ƒ d k r  |  j d St d ƒ ‚ d S(   s0   The string wrapped by the `Identifier`.
        i   i    s2   the Identifier wraps more than one than one stringN(   t   lenR   t   AttributeError(   R   (    (    s.   /tmp/pip-unpacked-wheel-WyN9Ij/psycopg2/sql.pyR(   P  s    c         C   s)   d |  j  j d j t t |  j ƒ ƒ f S(   Ns   %s(%s)s   , (   R   R	   R!   t   mapt   reprR   (   R   (    (    s.   /tmp/pip-unpacked-wheel-WyN9Ij/psycopg2/sql.pyR
   Z  s    	c            s    d j  ‡  f d †  |  j Dƒ ƒ S(   Nt   .c         3   s!   |  ] } t  j | ˆ  ƒ Vq d  S(   N(   t   extt   quote_ident(   t   .0R<   (   R   (    s.   /tmp/pip-unpacked-wheel-WyN9Ij/psycopg2/sql.pys	   <genexpr>`  s    (   R!   R   (   R   R   (    (   R   s.   /tmp/pip-unpacked-wheel-WyN9Ij/psycopg2/sql.pyR   _  s    (	   R	   R   R   R   R'   R;   R(   R
   R   (    (    (    s.   /tmp/pip-unpacked-wheel-WyN9Ij/psycopg2/sql.pyR:   "  s   	

	t   Literalc           B   s&   e  Z d  Z e d „  ƒ Z d „  Z RS(   sˆ  
    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   s   |  j  S(   s%   The object wrapped by the `!Literal`.(   R   (   R   (    (    s.   /tmp/pip-unpacked-wheel-WyN9Ij/psycopg2/sql.pyR   w  s    c         C   s·   t  | t j ƒ r | } n* t  | t j ƒ r9 | j } n t d ƒ ‚ t j |  j ƒ } t | d ƒ rv | j | ƒ n  | j	 ƒ  } t
 r³ t  | t ƒ r³ | j t j | j ƒ } n  | S(   Ns(   context must be a connection or a cursort   prepare(   R   RB   t
   connectiont   cursorR   t   adaptR   t   hasattrRF   t	   getquotedR   t   bytest   decodet	   encodingst   encoding(   R   R   t   connt   aR"   (    (    s.   /tmp/pip-unpacked-wheel-WyN9Ij/psycopg2/sql.pyR   |  s    	(   R	   R   R   R'   R   R   (    (    (    s.   /tmp/pip-unpacked-wheel-WyN9Ij/psycopg2/sql.pyRE   c  s   t   Placeholderc           B   s;   e  Z d  Z d d „ Z e d „  ƒ Z d „  Z d „  Z RS(   sã  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)

    c         C   sj   t  | t ƒ r1 d | k rP t d | ƒ ‚ qP n | d  k	 rP t d | ƒ ‚ n  t t |  ƒ j | ƒ d  S(   Nt   )s   invalid name: %rs'   expected string or None as name, got %r(   R   R   R+   R,   R   R   RR   R   (   R   R3   (    (    s.   /tmp/pip-unpacked-wheel-WyN9Ij/psycopg2/sql.pyR   «  s    c         C   s   |  j  S(   s   The name of the `!Placeholder`.(   R   (   R   (    (    s.   /tmp/pip-unpacked-wheel-WyN9Ij/psycopg2/sql.pyR3   µ  s    c         C   s#   d |  j  d  k	 r |  j  n d f S(   Ns   Placeholder(%r)R    (   R   R,   (   R   (    (    s.   /tmp/pip-unpacked-wheel-WyN9Ij/psycopg2/sql.pyR
   º  s    c         C   s"   |  j  d  k	 r d |  j  Sd Sd  S(   Ns   %%(%s)ss   %s(   R   R,   (   R   R   (    (    s.   /tmp/pip-unpacked-wheel-WyN9Ij/psycopg2/sql.pyR   ¾  s    N(	   R	   R   R   R,   R   R'   R3   R
   R   (    (    (    s.   /tmp/pip-unpacked-wheel-WyN9Ij/psycopg2/sql.pyRR     s
   
	t   NULLt   DEFAULT(   R   R(   t   psycopg2R    RB   t   psycopg2.compatR   R   t	   FormatterR)   t   objectR   R   R%   R:   RE   RR   RT   RU   (    (    (    s.   /tmp/pip-unpacked-wheel-WyN9Ij/psycopg2/sql.pyt   <module>   s   3IƒA-6