ó Tc@sdZddlZddlmZmZddlmZmZmZejd dkrfddl Z n*yddl Z Wne k rdZ nXdZ dZd Zd Zd efd „ƒYZdedddd d„Zdedd„Zdedd„Zddd„Zd d„ZdS(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(tnew_typetnew_array_typet register_typeiiiriÇiÚißtJsoncBs5eZdZdd„Zd„Zd„Zd„ZRS(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 (`!simplejson` for Python < 2.6; `~psycopg2.extensions.ISQLQuote.getquoted()` will raise `!ImportError` if the module is not available). cCsI||_|dk r!||_n$tdk r<tj|_n d|_dS(N(tadaptedtNonet_dumpstjsontdumps(tselfRR ((sA/opt/alt/python27/lib64/python2.7/site-packages/psycopg2/_json.pyt__init__Bs     cCs|tkr|SdS(N(R(R tproto((sA/opt/alt/python27/lib64/python2.7/site-packages/psycopg2/_json.pyt __conform__Ls cCs/|j}|dk r||ƒStdƒ‚dS(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. s>json module not available: you should provide a dumps functionN(RRt ImportError(R tobjR ((sA/opt/alt/python27/lib64/python2.7/site-packages/psycopg2/_json.pyR Ps    cCs"|j|jƒ}t|ƒjƒS(N(R RRt getquoted(R ts((sA/opt/alt/python27/lib64/python2.7/site-packages/psycopg2/_json.pyR_sN(t__name__t __module__t__doc__RR RR R(((sA/opt/alt/python27/lib64/python2.7/site-packages/psycopg2/_json.pyR6s    R cCs˜|dkr$t||ƒ\}}nt||d|d|jƒƒ\}}t|| r^|padƒ|dk rŽt|| r„|p‡dƒn||fS(sCreate 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. tloadstnameN(Rt_get_json_oidst_create_json_typecasterstupperR(t conn_or_curstgloballyRtoidt array_oidRtJSONt JSONARRAY((sA/opt/alt/python27/lib64/python2.7/site-packages/psycopg2/_json.pyt register_jsonds $ c Cs%td|d|d|dtdtƒ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()`. RRRRR(R!tJSON_OIDt JSONARRAY_OID(RRR((sA/opt/alt/python27/lib64/python2.7/site-packages/psycopg2/_json.pytregister_default_jsonŠs c Cs+td|d|d|dtdtddƒ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()`. RRRRRRtjsonb(R!t JSONB_OIDtJSONBARRAY_OID(RRR((sA/opt/alt/python27/lib64/python2.7/site-packages/psycopg2/_json.pytregister_default_jsonb–s Rcsˆdkr3tdkr'tdƒ‚q3tj‰n‡fd†}t|f||ƒ}|dk rt|fd||ƒ}nd}||fS(s&Create typecasters for json data type.sno json module availablecs|dkrdSˆ|ƒS(N(R(Rtcur(R(sA/opt/alt/python27/lib64/python2.7/site-packages/psycopg2/_json.pyt typecast_jsonŞs s%sARRAYN(RR RRRR(RRRRR*RR ((RsA/opt/alt/python27/lib64/python2.7/site-packages/psycopg2/_json.pyR˘s    c Csźddlm}ddlm}||ƒ\}}|j}|jdkrPdpSd}|jd||fƒ|jƒ}||krœ|j rœ|j ƒn|s¸|j d|ƒ‚n|S( Ni˙˙˙˙(tSTATUS_IN_TRANSACTION(t_solve_conn_cursiŹ9ttyparraytNULLs6SELECT t.oid, %s FROM pg_type t WHERE t.typname = %%s;s%s data type not found( tpsycopg2.extensionsR+tpsycopg2.extrasR,tstatustserver_versiontexecutetfetchonet autocommittrollbacktProgrammingError( RRR+R,tconntcurst conn_statusR-tr((sA/opt/alt/python27/lib64/python2.7/site-packages/psycopg2/_json.pyRˇs   (ii(Rtsystpsycopg2._psycopgRRRRRt version_infoR t simplejsonRRR"R#R&R'tobjectRtFalseR!R$R(RR(((sA/opt/alt/python27/lib64/python2.7/site-packages/psycopg2/_json.pyts(   . %