ó
¿b›]c           @   s(  d  d l  m Z d  d l  m Z d d l m Z d d l m Z d d l m Z y d d l	 m
 Z Wn e k
 r} d Z n Xe j d	 „ Z e j d
 „ Z d e j f d „  ƒ  YZ e j d d d ƒZ e j d d d ƒZ e j d d d ƒZ d e j f d „  ƒ  YZ e e e j <e e d <d S(   i   (   t   colspecs(   t   ischema_namesi   (   t   types(   t
   expression(   t	   operatorsiÿÿÿÿ(   t   UUIDc         C   s   | j  |  | ƒ S(   sµ   A synonym for the :meth:`.ARRAY.Comparator.any` method.

    This method is legacy and is here for backwards-compatibility.

    .. seealso::

        :func:`.expression.any_`

    (   t   any(   t   othert   arrexprt   operator(    (    sC   lib/python2.7/site-packages/sqlalchemy/dialects/postgresql/array.pyt   Any   s    c         C   s   | j  |  | ƒ S(   sµ   A synonym for the :meth:`.ARRAY.Comparator.all` method.

    This method is legacy and is here for backwards-compatibility.

    .. seealso::

        :func:`.expression.all_`

    (   t   all(   R   R   R	   (    (    sC   lib/python2.7/site-packages/sqlalchemy/dialects/postgresql/array.pyt   All#   s    t   arrayc           B   s8   e  Z d  Z d Z d „  Z e d d „ Z d d „ Z RS(   sï  A PostgreSQL ARRAY literal.

    This is used to produce ARRAY literals in SQL expressions, e.g.::

        from sqlalchemy.dialects.postgresql import array
        from sqlalchemy.dialects import postgresql
        from sqlalchemy import select, func

        stmt = select([
                        array([1,2]) + array([3,4,5])
                    ])

        print(stmt.compile(dialect=postgresql.dialect()))

    Produces the SQL::

        SELECT ARRAY[%(param_1)s, %(param_2)s] ||
            ARRAY[%(param_3)s, %(param_4)s, %(param_5)s]) AS anon_1

    An instance of :class:`.array` will always have the datatype
    :class:`.ARRAY`.  The "inner" type of the array is inferred from
    the values present, unless the ``type_`` keyword argument is passed::

        array(['foo', 'bar'], type_=CHAR)

    Multidimensional arrays are produced by nesting :class:`.array` constructs.
    The dimensionality of the final :class:`.ARRAY` type is calculated by
    recursively adding the dimensions of the inner :class:`.ARRAY` type::

        stmt = select([
            array([
                array([1, 2]), array([3, 4]), array([column('q'), column('x')])
            ])
        ])
        print(stmt.compile(dialect=postgresql.dialect()))

    Produces::

        SELECT ARRAY[ARRAY[%(param_1)s, %(param_2)s],
        ARRAY[%(param_3)s, %(param_4)s], ARRAY[q, x]] AS anon_1

    .. versionadded:: 1.3.6 added support for multidimensional array literals

    .. seealso::

        :class:`.postgresql.ARRAY`

    R   c         K   s   t  t |  ƒ j | | Ž  t |  j t ƒ rk t |  j j d |  j j d  k	 r\ |  j j d n d ƒ|  _ n t |  j ƒ |  _ d  S(   Nt
   dimensionsi   i   (	   t   superR   t   __init__t
   isinstancet   typet   ARRAYt	   item_typeR   t   None(   t   selft   clausest   kw(    (    sC   lib/python2.7/site-packages/sqlalchemy/dialects/postgresql/array.pyR   f   s    "c         C   sy   | s | t  j k r@ t j d  | d | d | d |  j d t ƒSt g  | D]$ } |  j | | d t d | ƒ^ qJ ƒ Sd  S(   Nt   _compared_to_operatort   type_t   _compared_to_typet   uniquet   _assume_scalar(	   R   t   getitemR   t   BindParameterR   R   t   TrueR   t   _bind_param(   R   R	   t   objR   R   t   o(    (    sC   lib/python2.7/site-packages/sqlalchemy/dialects/postgresql/array.pyR!   r   s    	c         C   s3   | t  j t  j t  j f k r+ t j |  ƒ S|  Sd  S(   N(   R   t   any_opt   all_opR   R   t   Grouping(   R   t   against(    (    sC   lib/python2.7/site-packages/sqlalchemy/dialects/postgresql/array.pyt
   self_group‡   s    N(	   t   __name__t
   __module__t   __doc__t   __visit_name__R   t   FalseR   R!   R(   (    (    (    sC   lib/python2.7/site-packages/sqlalchemy/dialects/postgresql/array.pyR   1   s
   1	s   @>t
   precedencei   s   <@s   &&R   c           B   s„   e  Z d  Z d e j j f d „  ƒ  YZ e Z e d
 e d „ Z	 e
 d „  ƒ Z e
 d „  ƒ Z d „  Z d „  Z d „  Z d	 „  Z RS(   s–  PostgreSQL ARRAY type.

    .. versionchanged:: 1.1 The :class:`.postgresql.ARRAY` type is now
       a subclass of the core :class:`.types.ARRAY` type.

    The :class:`.postgresql.ARRAY` type is constructed in the same way
    as the core :class:`.types.ARRAY` type; a member type is required, and a
    number of dimensions is recommended if the type is to be used for more
    than one dimension::

        from sqlalchemy.dialects import postgresql

        mytable = Table("mytable", metadata,
                Column("data", postgresql.ARRAY(Integer, dimensions=2))
            )

    The :class:`.postgresql.ARRAY` type provides all operations defined on the
    core :class:`.types.ARRAY` type, including support for "dimensions",
    indexed access, and simple matching such as
    :meth:`.types.ARRAY.Comparator.any` and
    :meth:`.types.ARRAY.Comparator.all`.  :class:`.postgresql.ARRAY` class also
    provides PostgreSQL-specific methods for containment operations, including
    :meth:`.postgresql.ARRAY.Comparator.contains`
    :meth:`.postgresql.ARRAY.Comparator.contained_by`, and
    :meth:`.postgresql.ARRAY.Comparator.overlap`, e.g.::

        mytable.c.data.contains([1, 2])

    The :class:`.postgresql.ARRAY` type may not be supported on all
    PostgreSQL DBAPIs; it is currently known to work on psycopg2 only.

    Additionally, the :class:`.postgresql.ARRAY` type does not work directly in
    conjunction with the :class:`.ENUM` type.  For a workaround, see the
    special type at :ref:`postgresql_array_of_enum`.

    .. seealso::

        :class:`.types.ARRAY` - base array type

        :class:`.postgresql.array` - produces a literal array value.

    t
   Comparatorc           B   s)   e  Z d  Z d „  Z d „  Z d „  Z RS(   s$  Define comparison operations for :class:`.ARRAY`.

        Note that these operations are in addition to those provided
        by the base :class:`.types.ARRAY.Comparator` class, including
        :meth:`.types.ARRAY.Comparator.any` and
        :meth:`.types.ARRAY.Comparator.all`.

        c         K   s   |  j  t | d t j ƒS(   s   Boolean expression.  Test if elements are a superset of the
            elements of the argument array expression.
            t   result_type(   t   operatet   CONTAINSt   sqltypest   Boolean(   R   R   t   kwargs(    (    sC   lib/python2.7/site-packages/sqlalchemy/dialects/postgresql/array.pyt   containsÍ   s    c         C   s   |  j  t | d t j ƒS(   s„   Boolean expression.  Test if elements are a proper subset of the
            elements of the argument array expression.
            R0   (   R1   t   CONTAINED_BYR3   R4   (   R   R   (    (    sC   lib/python2.7/site-packages/sqlalchemy/dialects/postgresql/array.pyt   contained_byÓ   s    c         C   s   |  j  t | d t j ƒS(   su   Boolean expression.  Test if array has elements in common with
            an argument array expression.
            R0   (   R1   t   OVERLAPR3   R4   (   R   R   (    (    sC   lib/python2.7/site-packages/sqlalchemy/dialects/postgresql/array.pyt   overlapÛ   s    (   R)   R*   R+   R6   R8   R:   (    (    (    sC   lib/python2.7/site-packages/sqlalchemy/dialects/postgresql/array.pyR/   Â   s   			c         C   sa   t  | t ƒ r t d ƒ ‚ n  t  | t ƒ r9 | ƒ  } n  | |  _ | |  _ | |  _ | |  _ d S(   sP  Construct an ARRAY.

        E.g.::

          Column('myarray', ARRAY(Integer))

        Arguments are:

        :param item_type: The data type of items of this array. Note that
          dimensionality is irrelevant here, so multi-dimensional arrays like
          ``INTEGER[][]``, are constructed as ``ARRAY(Integer)``, not as
          ``ARRAY(ARRAY(Integer))`` or such.

        :param as_tuple=False: Specify whether return results
          should be converted to tuples from lists. DBAPIs such
          as psycopg2 return lists by default. When tuples are
          returned, the results are hashable.

        :param dimensions: if non-None, the ARRAY will assume a fixed
         number of dimensions.  This will cause the DDL emitted for this
         ARRAY to include the exact number of bracket clauses ``[]``,
         and will also optimize the performance of the type overall.
         Note that PG arrays are always implicitly "non-dimensioned",
         meaning they can store any number of dimensions no matter how
         they were declared.

        :param zero_indexes=False: when True, index values will be converted
         between Python zero-based and PostgreSQL one-based indexes, e.g.
         a value of one will be added to all index values before passing
         to the database.

         .. versionadded:: 0.9.5


        sU   Do not nest ARRAY types; ARRAY(basetype) handles multi-dimensional arrays of basetypeN(   R   R   t
   ValueErrorR   R   t   as_tupleR   t   zero_indexes(   R   R   R<   R   R=   (    (    sC   lib/python2.7/site-packages/sqlalchemy/dialects/postgresql/array.pyR   ã   s    &			c         C   s   |  j  S(   N(   R<   (   R   (    (    sC   lib/python2.7/site-packages/sqlalchemy/dialects/postgresql/array.pyt   hashable  s    c         C   s   t  S(   N(   t   list(   R   (    (    sC   lib/python2.7/site-packages/sqlalchemy/dialects/postgresql/array.pyt   python_type  s    c         C   s
   | | k S(   N(    (   R   t   xt   y(    (    sC   lib/python2.7/site-packages/sqlalchemy/dialects/postgresql/array.pyt   compare_values  s    c            s¨   ˆ d  k r t | ƒ } n  ˆ d k sT ˆ d  k r | sT t | d t t f ƒ r ˆ rt ˆ  ‡ f d †  | Dƒ ƒ Sˆ  | ƒ Sn# ˆ  ‡  ‡ ‡ ‡ f d †  | Dƒ ƒ Sd  S(   Ni   i    c         3   s   |  ] } ˆ  | ƒ Vq d  S(   N(    (   t   .0RA   (   t   itemproc(    sC   lib/python2.7/site-packages/sqlalchemy/dialects/postgresql/array.pys	   <genexpr>/  s    c         3   s=   |  ]3 } ˆ j  | ˆ ˆ d k	 r+ ˆ d  n d ˆ  ƒ Vq d S(   i   N(   t   _proc_arrayR   (   RD   RA   (   t
   collectiont   dimRE   R   (    sC   lib/python2.7/site-packages/sqlalchemy/dialects/postgresql/array.pys	   <genexpr>4  s   (   R   R?   R   t   tuple(   R   t   arrRE   RH   RG   (    (   RG   RH   RE   R   sC   lib/python2.7/site-packages/sqlalchemy/dialects/postgresql/array.pyRF      s    c            s1   ˆ j  j | ƒ j | ƒ ‰  ‡  ‡ f d †  } | S(   Nc            s-   |  d  k r |  Sˆ j |  ˆ  ˆ j t ƒ Sd  S(   N(   R   RF   R   R?   (   t   value(   t	   item_procR   (    sC   lib/python2.7/site-packages/sqlalchemy/dialects/postgresql/array.pyt   processB  s    (   R   t   dialect_implt   bind_processor(   R   t   dialectRM   (    (   RL   R   sC   lib/python2.7/site-packages/sqlalchemy/dialects/postgresql/array.pyRO   =  s    	c            s4   ˆ j  j | ƒ j | | ƒ ‰  ‡  ‡ f d †  } | S(   Nc            s<   |  d  k r |  Sˆ j |  ˆ  ˆ j ˆ j r1 t n t ƒ Sd  S(   N(   R   RF   R   R<   RI   R?   (   RK   (   RL   R   (    sC   lib/python2.7/site-packages/sqlalchemy/dialects/postgresql/array.pyRM   Q  s    (   R   RN   t   result_processor(   R   RP   t   coltypeRM   (    (   RL   R   sC   lib/python2.7/site-packages/sqlalchemy/dialects/postgresql/array.pyRQ   L  s    N(   R)   R*   R+   R3   R   R/   t   comparator_factoryR-   R   R   t   propertyR>   R@   RC   RF   RO   RQ   (    (    (    sC   lib/python2.7/site-packages/sqlalchemy/dialects/postgresql/array.pyR   •   s   +1			t   _arrayN(   t   baseR    R   t    R   R3   t   sqlR   R   t   uuidR   t   _python_UUIDt   ImportErrorR   t   eqR
   R   t   TupleR   t	   custom_opR2   R7   R9   R   (    (    (    sC   lib/python2.7/site-packages/sqlalchemy/dialects/postgresql/array.pyt   <module>   s"   
]Ê