σ
Ώ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 d d l	 m
 Z
 d Z e
 j e
 j Z e
 j d d e d e d e Z e
 j d d e d e d e Z e
 j d d e d e d e Z e
 j d d e d e d e Z e
 j d d e d e d e Z e
 j d d e d e d e Z e
 j d d e d e d e Z d e j j f d     YZ e e e j j <d	 e j f d     YZ e e e j <e e d <d
 e f d     YZ e e d <d S(   i    (   t   absolute_importi   (   t   colspecs(   t   ischema_namesi   (   t   types(   t   util(   t	   operatorst   JSONt   JSONBs   ->>t
   precedencet   natural_self_precedentt   eager_groupings   #>>t   ?s   ?&s   ?|s   @>s   <@t   JSONPathTypec           B@  s   e  Z d    Z d   Z RS(   c         @  s"   |  j  |      f d   } | S(   Nc         @  si   t  |  t j j  s t  g  |  D] } t j |  ^ q" } d d j |  }    re   |   }  n  |  S(   Ns   {%s}s   , (   t
   isinstanceR   t   collections_abct   Sequencet   AssertionErrort	   text_typet   join(   t   valuet   elemt   tokens(   t
   super_proc(    sB   lib/python2.7/site-packages/sqlalchemy/dialects/postgresql/json.pyt   processK   s    "(   t   string_bind_processor(   t   selft   dialectR   (    (   R   sB   lib/python2.7/site-packages/sqlalchemy/dialects/postgresql/json.pyt   bind_processorH   s    c         @  s"   |  j  |      f d   } | S(   Nc         @  si   t  |  t j j  s t  g  |  D] } t j |  ^ q" } d d j |  }    re   |   }  n  |  S(   Ns   {%s}s   , (   R   R   R   R   R   R   R   (   R   R   R   (   R   (    sB   lib/python2.7/site-packages/sqlalchemy/dialects/postgresql/json.pyR   X   s    "(   t   string_literal_processor(   R   R   R   (    (   R   sB   lib/python2.7/site-packages/sqlalchemy/dialects/postgresql/json.pyt   literal_processorU   s    (   t   __name__t
   __module__R   R   (    (    (    sB   lib/python2.7/site-packages/sqlalchemy/dialects/postgresql/json.pyR   G   s   	c           B@  sK   e  Z d  Z e j   Z e d d  Z d e j	 j
 f d     YZ
 e
 Z RS(   s 	  Represent the PostgreSQL JSON type.

    This type is a specialization of the Core-level :class:`.types.JSON`
    type.   Be sure to read the documentation for :class:`.types.JSON` for
    important tips regarding treatment of NULL values and ORM use.

    .. versionchanged:: 1.1 :class:`.postgresql.JSON` is now a PostgreSQL-
       specific specialization of the new :class:`.types.JSON` type.

    The operators provided by the PostgreSQL version of :class:`.JSON`
    include:

    * Index operations (the ``->`` operator)::

        data_table.c.data['some key']

        data_table.c.data[5]


    * Index operations returning text (the ``->>`` operator)::

        data_table.c.data['some key'].astext == 'some value'

    * Index operations with CAST
      (equivalent to ``CAST(col ->> ['some key'] AS <type>)``)::

        data_table.c.data['some key'].astext.cast(Integer) == 5

    * Path index operations (the ``#>`` operator)::

        data_table.c.data[('key_1', 'key_2', 5, ..., 'key_n')]

    * Path index operations returning text (the ``#>>`` operator)::

        data_table.c.data[('key_1', 'key_2', 5, ..., 'key_n')].astext == 'some value'

    .. versionchanged:: 1.1  The :meth:`.ColumnElement.cast` operator on
       JSON objects now requires that the :attr:`.JSON.Comparator.astext`
       modifier be called explicitly, if the cast works only from a textual
       string.

    Index operations return an expression object whose type defaults to
    :class:`.JSON` by default, so that further JSON-oriented instructions
    may be called upon the result type.

    Custom serializers and deserializers are specified at the dialect level,
    that is using :func:`.create_engine`.  The reason for this is that when
    using psycopg2, the DBAPI only allows serializers at the per-cursor
    or per-connection level.   E.g.::

        engine = create_engine("postgresql://scott:tiger@localhost/test",
                                json_serializer=my_serialize_fn,
                                json_deserializer=my_deserialize_fn
                        )

    When using the psycopg2 dialect, the json_deserializer is registered
    against the database using ``psycopg2.extras.register_default_json``.

    .. seealso::

        :class:`.types.JSON` - Core level JSON type

        :class:`.JSONB`

    c         C@  s5   t  t |   j d |  | d k	 r1 | |  _ n  d S(   s   Construct a :class:`.JSON` type.

        :param none_as_null: if True, persist the value ``None`` as a
         SQL NULL value, not the JSON encoding of ``null``.   Note that
         when this flag is False, the :func:`.null` construct can still
         be used to persist a NULL value::

             from sqlalchemy import null
             conn.execute(table.insert(), data=null())

         .. versionchanged:: 0.9.8 - Added ``none_as_null``, and :func:`.null`
            is now supported in order to persist a NULL value.

         .. seealso::

              :attr:`.JSON.NULL`

        :param astext_type: the type to use for the
         :attr:`.JSON.Comparator.astext`
         accessor on indexed attributes.  Defaults to :class:`.types.Text`.

         .. versionadded:: 1.1

         t   none_as_nullN(   t   superR   t   __init__t   Nonet   astext_type(   R   R    R$   (    (    sB   lib/python2.7/site-packages/sqlalchemy/dialects/postgresql/json.pyR"   «   s    t
   Comparatorc           B@  s   e  Z d  Z e d    Z RS(   s0   Define comparison operations for :class:`.JSON`.c         C@  sr   t  |  j j j t j j  rF |  j j j t	 |  j j d |  j j
 S|  j j j t |  j j d |  j j
 Sd S(   s  On an indexed expression, use the "astext" (e.g. "->>")
            conversion when rendered in SQL.

            E.g.::

                select([data_table.c.data['some key'].astext])

            .. seealso::

                :meth:`.ColumnElement.cast`

            t   result_typeN(   R   t   exprt   rightt   typet   sqltypesR   R   t   leftt   operatet   JSONPATH_ASTEXTR$   t   ASTEXT(   R   (    (    sB   lib/python2.7/site-packages/sqlalchemy/dialects/postgresql/json.pyt   astextΛ   s    (   R   R   t   __doc__t   propertyR/   (    (    (    sB   lib/python2.7/site-packages/sqlalchemy/dialects/postgresql/json.pyR%   Θ   s   N(   R   R   R0   R*   t   TextR$   t   FalseR#   R"   R   R%   t   comparator_factory(    (    (    sB   lib/python2.7/site-packages/sqlalchemy/dialects/postgresql/json.pyR   f   s
   At   jsonc           B@  s3   e  Z d  Z d Z d e j f d     YZ e Z RS(   s_  Represent the PostgreSQL JSONB type.

    The :class:`.JSONB` type stores arbitrary JSONB format data, e.g.::

        data_table = Table('data_table', metadata,
            Column('id', Integer, primary_key=True),
            Column('data', JSONB)
        )

        with engine.connect() as conn:
            conn.execute(
                data_table.insert(),
                data = {"key1": "value1", "key2": "value2"}
            )

    The :class:`.JSONB` type includes all operations provided by
    :class:`.JSON`, including the same behaviors for indexing operations.
    It also adds additional operators specific to JSONB, including
    :meth:`.JSONB.Comparator.has_key`, :meth:`.JSONB.Comparator.has_all`,
    :meth:`.JSONB.Comparator.has_any`, :meth:`.JSONB.Comparator.contains`,
    and :meth:`.JSONB.Comparator.contained_by`.

    Like the :class:`.JSON` type, the :class:`.JSONB` type does not detect
    in-place changes when used with the ORM, unless the
    :mod:`sqlalchemy.ext.mutable` extension is used.

    Custom serializers and deserializers
    are shared with the :class:`.JSON` class, using the ``json_serializer``
    and ``json_deserializer`` keyword arguments.  These must be specified
    at the dialect level using :func:`.create_engine`.  When using
    psycopg2, the serializers are associated with the jsonb type using
    ``psycopg2.extras.register_default_jsonb`` on a per-connection basis,
    in the same way that ``psycopg2.extras.register_default_json`` is used
    to register these handlers with the json type.

    .. versionadded:: 0.9.7

    .. seealso::

        :class:`.JSON`

    R   R%   c           B@  s;   e  Z d  Z d   Z d   Z d   Z d   Z d   Z RS(   s0   Define comparison operations for :class:`.JSON`.c         C@  s   |  j  t | d t j S(   sv   Boolean expression.  Test for presence of a key.  Note that the
            key may be a SQLA expression.
            R&   (   R,   t   HAS_KEYR*   t   Boolean(   R   t   other(    (    sB   lib/python2.7/site-packages/sqlalchemy/dialects/postgresql/json.pyt   has_key  s    c         C@  s   |  j  t | d t j S(   sH   Boolean expression.  Test for presence of all keys in jsonb
            R&   (   R,   t   HAS_ALLR*   R7   (   R   R8   (    (    sB   lib/python2.7/site-packages/sqlalchemy/dialects/postgresql/json.pyt   has_all#  s    c         C@  s   |  j  t | d t j S(   sG   Boolean expression.  Test for presence of any key in jsonb
            R&   (   R,   t   HAS_ANYR*   R7   (   R   R8   (    (    sB   lib/python2.7/site-packages/sqlalchemy/dialects/postgresql/json.pyt   has_any(  s    c         K@  s   |  j  t | d t j S(   s   Boolean expression.  Test if keys (or array) are a superset
            of/contained the keys of the argument jsonb expression.
            R&   (   R,   t   CONTAINSR*   R7   (   R   R8   t   kwargs(    (    sB   lib/python2.7/site-packages/sqlalchemy/dialects/postgresql/json.pyt   contains-  s    c         C@  s   |  j  t | d t j S(   s|   Boolean expression.  Test if keys are a proper subset of the
            keys of the argument jsonb expression.
            R&   (   R,   t   CONTAINED_BYR*   R7   (   R   R8   (    (    sB   lib/python2.7/site-packages/sqlalchemy/dialects/postgresql/json.pyt   contained_by3  s    (   R   R   R0   R9   R;   R=   R@   RB   (    (    (    sB   lib/python2.7/site-packages/sqlalchemy/dialects/postgresql/json.pyR%     s   				(   R   R   R0   t   __visit_name__R   R%   R4   (    (    (    sB   lib/python2.7/site-packages/sqlalchemy/dialects/postgresql/json.pyR   μ   s   *!t   jsonbN(   R   R   (   t
   __future__R    t   baseR   R   t    R   R*   R   t   sqlR   t   __all__t   _PRECEDENCEt   json_getitem_opt   idx_precedencet	   custom_opt   TrueR.   R-   R6   R:   R<   R>   RA   R   R   R   (    (    (    sB   lib/python2.7/site-packages/sqlalchemy/dialects/postgresql/json.pyt   <module>   sb   							
R