3
(h                 @   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G d
d deZdddZdddZdddZdddZdddZdS )zImplementation 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.
    N)	ISQLQuoteQuotedString)new_typenew_array_typeregister_type)PY2r      i  i  c               @   sP   e Zd ZdZdddZdd Zdd Zd	d
 Zdd Ze	rDdd Z
ndd Z
dS )Jsona  
    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.

    Nc             C   s   || _ d | _|ptj| _d S )N)adapted_connjsondumps_dumps)selfr   r    r   ;/tmp/pip-install-q3hcpn_q/psycopg2-binary/psycopg2/_json.py__init__9   s    zJson.__init__c             C   s   |t kr| S d S )N)r   )r   protor   r   r   __conform__>   s    zJson.__conform__c             C   s
   | j |S )zSerialize *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   objr   r   r   r   B   s    z
Json.dumpsc             C   s
   || _ d S )N)r   )r   connr   r   r   prepareK   s    zJson.preparec             C   s2   | j | j}t|}| jd k	r*|j| j |j S )N)r   r   r   r   r   	getquoted)r   sqsr   r   r   r   N   s
    
zJson.getquotedc             C   s   | j  S )N)r   )r   r   r   r   __str__V   s    zJson.__str__c             C   s   | j  jddS )Nasciireplace)r   decode)r   r   r   r   r   Y   s    )N)__name__
__module____qualname____doc__r   r   r   r   r   r   r   r   r   r   r   r
   /   s   	
	
r
   Fr   c             C   sf   |dkrt | |\}}t||||j d\}}t|| r<| p>d |dk	r^t|| rX| pZd ||fS )a  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.

    N)loadsname)_get_json_oids_create_json_typecastersupperr   )conn_or_cursgloballyr$   oid	array_oidr%   JSON	JSONARRAYr   r   r   register_json^   s    r/   c             C   s   t | ||ttdS )a{  
    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/   JSON_OIDJSONARRAY_OID)r)   r*   r$   r   r   r   register_default_json   s    	r2   c             C   s   t | ||ttddS )a^  
    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()`.
    Zjsonb)r)   r*   r$   r+   r,   r%   )r/   	JSONB_OIDJSONBARRAY_OID)r)   r*   r$   r   r   r   register_default_jsonb   s    	r5   r-   c                sP    dkrt j  fdd}t| f||}|dk	rDt|fd| |}nd}||fS )z&Create typecasters for json data type.Nc                s   | d krd S  | S )Nr   )r   cur)r$   r   r   typecast_json   s    z/_create_json_typecasters.<locals>.typecast_jsonz%sARRAY)r   r$   r   r   )r+   r,   r$   r%   r7   r-   r.   r   )r$   r   r'      s    r'   c       	      C   s   ddl m} ddlm} || \}}|j}|jjdkr:dp<d}|jd| |f |j }||krp|j	 rp|j
  |s|jd| |S )	Nr   )STATUS_IN_TRANSACTION)_solve_conn_cursi9 typarrayZNULLz6SELECT t.oid, %s FROM pg_type t WHERE t.typname = %%s;z%s data type not found)Zpsycopg2.extensionsr8   Zpsycopg2.extrasr9   statusinfoZserver_versionexecuteZfetchoneZ
autocommitrollbackZProgrammingError)	r)   r%   r8   r9   r   ZcursZconn_statusr:   rr   r   r   r&      s    r&   )NFNNNr   )NFN)NFN)Nr-   )r   )r#   r   Zpsycopg2._psycopgr   r   r   r   r   Zpsycopg2.compatr   r0   r1   r3   r4   objectr
   r/   r2   r5   r'   r&   r   r   r   r   <module>   s   / 
&


