ó
º9—^c           @   sà   d  Z  d d l Z d d l m Z m Z d d l m Z m Z m Z d d l m	 Z	 d Z
 d Z d Z d	 Z d
 e f d „  ƒ  YZ d e d d d d d „ Z d e d d „ Z d e d d „ Z d d d „ Z d d „ Z d S(   s÷   Implementation of the JSON adaptation objects

This module exists to avoid a circular import problem: pyscopg2.extras depends
on psycopg2.extension, so I can't create the default JSON typecasters in
extensions importing register_json from extras.
iÿÿÿÿN(   t	   ISQLQuotet   QuotedString(   t   new_typet   new_array_typet   register_type(   t   PY2ir   iÇ   iÚ  iß  t   Jsonc           B   sY   e  Z d  Z d d „ Z d „  Z d „  Z d „  Z d „  Z e	 rN d „  Z
 n	 d „  Z
 RS(	   s  
    An `~psycopg2.extensions.ISQLQuote` wrapper to adapt a Python object to
    :sql:`json` data type.

    `!Json` can be used to wrap any object supported by the provided *dumps*
    function. If none is provided, the standard :py:func:`json.dumps()` is
    used.

    c         C   s(   | |  _  d  |  _ | p t j |  _ d  S(   N(   t   adaptedt   Nonet   _connt   jsont   dumpst   _dumps(   t   selfR   R   (    (    s0   /tmp/pip-unpacked-wheel-WyN9Ij/psycopg2/_json.pyt   __init__8   s    		c         C   s   | t  k r |  Sd  S(   N(   R    (   R   t   proto(    (    s0   /tmp/pip-unpacked-wheel-WyN9Ij/psycopg2/_json.pyt   __conform__=   s    c         C   s   |  j  | ƒ S(   sß   Serialize *obj* in JSON format.

        The default is to call `!json.dumps()` or the *dumps* function
        provided in the constructor. You can override this method to create a
        customized JSON wrapper.
        (   R   (   R   t   obj(    (    s0   /tmp/pip-unpacked-wheel-WyN9Ij/psycopg2/_json.pyR   A   s    c         C   s   | |  _  d  S(   N(   R	   (   R   t   conn(    (    s0   /tmp/pip-unpacked-wheel-WyN9Ij/psycopg2/_json.pyt   prepareJ   s    c         C   sJ   |  j  |  j ƒ } t | ƒ } |  j d  k	 r@ | j |  j ƒ n  | j ƒ  S(   N(   R   R   R   R	   R   R   t	   getquoted(   R   t   st   qs(    (    s0   /tmp/pip-unpacked-wheel-WyN9Ij/psycopg2/_json.pyR   M   s
    c         C   s
   |  j  ƒ  S(   N(   R   (   R   (    (    s0   /tmp/pip-unpacked-wheel-WyN9Ij/psycopg2/_json.pyt   __str__U   s    c         C   s   |  j  ƒ  j d d ƒ S(   Nt   asciit   replace(   R   t   decode(   R   (    (    s0   /tmp/pip-unpacked-wheel-WyN9Ij/psycopg2/_json.pyR   X   s    N(   t   __name__t
   __module__t   __doc__R   R   R   R   R   R   R   R   (    (    (    s0   /tmp/pip-unpacked-wheel-WyN9Ij/psycopg2/_json.pyR   .   s   						R
   c         C   s˜   | d k r$ t |  | ƒ \ } } n  t | | d | d | j ƒ  ƒ\ } } t | | r^ |  pa d ƒ | d k	 rŽ t | | r„ |  p‡ d ƒ n  | | f S(   s  Create and register typecasters converting :sql:`json` type to Python objects.

    :param conn_or_curs: a connection or cursor used to find the :sql:`json`
        and :sql:`json[]` oids; the typecasters are registered in a scope
        limited to this object, unless *globally* is set to `!True`. It can be
        `!None` if the oids are provided
    :param globally: if `!False` register the typecasters only on
        *conn_or_curs*, otherwise register them globally
    :param loads: the function used to parse the data into a Python object. If
        `!None` use `!json.loads()`, where `!json` is the module chosen
        according to the Python version (see above)
    :param oid: the OID of the :sql:`json` type if known; If not, it will be
        queried on *conn_or_curs*
    :param array_oid: the OID of the :sql:`json[]` array type if known;
        if not, it will be queried on *conn_or_curs*
    :param name: the name of the data type to look for in *conn_or_curs*

    The connection or cursor passed to the function will be used to query the
    database and look for the OID of the :sql:`json` type (or an alternative
    type if *name* if provided). No query is performed if *oid* and *array_oid*
    are provided.  Raise `~psycopg2.ProgrammingError` if the type is not found.

    t   loadst   nameN(   R   t   _get_json_oidst   _create_json_typecasterst   upperR   (   t   conn_or_curst   globallyR   t   oidt	   array_oidR   t   JSONt	   JSONARRAY(    (    s0   /tmp/pip-unpacked-wheel-WyN9Ij/psycopg2/_json.pyt   register_json]   s    $c         C   s%   t  d |  d | d | d t d t ƒ S(   s{  
    Create and register :sql:`json` typecasters for PostgreSQL 9.2 and following.

    Since PostgreSQL 9.2 :sql:`json` is a builtin type, hence its oid is known
    and fixed. This function allows specifying a customized *loads* function
    for the default :sql:`json` type without querying the database.
    All the parameters have the same meaning of `register_json()`.
    R#   R$   R   R%   R&   (   R)   t   JSON_OIDt   JSONARRAY_OID(   R#   R$   R   (    (    s0   /tmp/pip-unpacked-wheel-WyN9Ij/psycopg2/_json.pyt   register_default_json„   s    	c         C   s+   t  d |  d | d | d t d t d d ƒ S(   s^  
    Create and register :sql:`jsonb` typecasters for PostgreSQL 9.4 and following.

    As in `register_default_json()`, the function allows to register a
    customized *loads* function for the :sql:`jsonb` type at its known oid for
    PostgreSQL 9.4 and following versions.  All the parameters have the same
    meaning of `register_json()`.
    R#   R$   R   R%   R&   R   t   jsonb(   R)   t	   JSONB_OIDt   JSONBARRAY_OID(   R#   R$   R   (    (    s0   /tmp/pip-unpacked-wheel-WyN9Ij/psycopg2/_json.pyt   register_default_jsonb‘   s    	R'   c            st   ˆ  d k r t j ‰  n  ‡  f d †  } t |  f | | ƒ } | d k	 rd t | f d | | ƒ } n d } | | f S(   s&   Create typecasters for json data type.c            s   |  d  k r d  Sˆ  |  ƒ S(   N(   R   (   R   t   cur(   R   (    s0   /tmp/pip-unpacked-wheel-WyN9Ij/psycopg2/_json.pyt   typecast_json£   s    s   %sARRAYN(   R   R
   R   R   R   (   R%   R&   R   R   R2   R'   R(   (    (   R   s0   /tmp/pip-unpacked-wheel-WyN9Ij/psycopg2/_json.pyR!   ž   s    c   	      C   s¿   d d l  m } d d l m } | |  ƒ \ } } | j } | j j d k rS d pV d } | j d | | f ƒ | j ƒ  } | | k rŸ | j	 rŸ | j
 ƒ  n  | s» | j d | ƒ ‚ n  | S(	   Niÿÿÿÿ(   t   STATUS_IN_TRANSACTION(   t   _solve_conn_cursi¬9 t   typarrayt   NULLs6   SELECT t.oid, %s FROM pg_type t WHERE t.typname = %%s;s   %s data type not found(   t   psycopg2.extensionsR3   t   psycopg2.extrasR4   t   statust   infot   server_versiont   executet   fetchonet
   autocommitt   rollbackt   ProgrammingError(	   R#   R   R3   R4   R   t   curst   conn_statusR5   t   r(    (    s0   /tmp/pip-unpacked-wheel-WyN9Ij/psycopg2/_json.pyR    ±   s    	(   R   R
   t   psycopg2._psycopgR    R   R   R   R   t   psycopg2.compatR   R*   R+   R.   R/   t   objectR   R   t   FalseR)   R,   R0   R!   R    (    (    (    s0   /tmp/pip-unpacked-wheel-WyN9Ij/psycopg2/_json.pyt   <module>   s   /	&