2012-09-19 07:26:35 +04:00
|
|
|
"""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.
|
|
|
|
"""
|
|
|
|
|
2012-09-20 06:44:50 +04:00
|
|
|
# psycopg/_json.py - Implementation of the JSON adaptation objects
|
2012-09-19 07:26:35 +04:00
|
|
|
#
|
|
|
|
# Copyright (C) 2012 Daniele Varrazzo <daniele.varrazzo@gmail.com>
|
|
|
|
#
|
|
|
|
# psycopg2 is free software: you can redistribute it and/or modify it
|
|
|
|
# under the terms of the GNU Lesser General Public License as published
|
|
|
|
# by the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# In addition, as a special exception, the copyright holders give
|
|
|
|
# permission to link this program with the OpenSSL library (or with
|
|
|
|
# modified versions of OpenSSL that use the same license as OpenSSL),
|
|
|
|
# and distribute linked combinations including the two.
|
|
|
|
#
|
|
|
|
# You must obey the GNU Lesser General Public License in all respects for
|
|
|
|
# all of the code used other than OpenSSL.
|
|
|
|
#
|
|
|
|
# psycopg2 is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
|
|
|
|
# License for more details.
|
|
|
|
|
|
|
|
import sys
|
|
|
|
|
|
|
|
from psycopg2._psycopg import ISQLQuote, QuotedString
|
|
|
|
from psycopg2._psycopg import new_type, new_array_type, register_type
|
|
|
|
|
|
|
|
|
|
|
|
# import the best json implementation available
|
|
|
|
if sys.version_info[:2] >= (2,6):
|
|
|
|
import json
|
|
|
|
else:
|
|
|
|
try:
|
|
|
|
import simplejson as json
|
|
|
|
except ImportError:
|
|
|
|
json = None
|
|
|
|
|
|
|
|
|
2012-09-19 18:49:00 +04:00
|
|
|
# oids from PostgreSQL 9.2
|
|
|
|
JSON_OID = 114
|
|
|
|
JSONARRAY_OID = 199
|
|
|
|
|
2014-08-13 03:54:49 +04:00
|
|
|
# oids from PostgreSQL 9.4
|
|
|
|
JSONB_OID = 3802
|
|
|
|
JSONBARRAY_OID = 3807
|
|
|
|
|
2012-09-19 07:26:35 +04:00
|
|
|
class Json(object):
|
2012-09-19 19:32:57 +04:00
|
|
|
"""
|
|
|
|
An `~psycopg2.extensions.ISQLQuote` wrapper to adapt a Python object to
|
|
|
|
:sql:`json` data type.
|
2012-09-19 07:26:35 +04:00
|
|
|
|
2012-09-24 14:51:35 +04:00
|
|
|
`!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
|
2012-09-25 03:12:57 +04:00
|
|
|
the module is not available).
|
2012-09-19 07:26:35 +04:00
|
|
|
|
|
|
|
"""
|
2012-09-19 19:32:57 +04:00
|
|
|
def __init__(self, adapted, dumps=None):
|
2012-09-19 07:26:35 +04:00
|
|
|
self.adapted = adapted
|
2012-09-19 19:32:57 +04:00
|
|
|
|
|
|
|
if dumps is not None:
|
|
|
|
self._dumps = dumps
|
|
|
|
elif json is not None:
|
|
|
|
self._dumps = json.dumps
|
|
|
|
else:
|
|
|
|
self._dumps = None
|
2012-09-19 07:26:35 +04:00
|
|
|
|
|
|
|
def __conform__(self, proto):
|
|
|
|
if proto is ISQLQuote:
|
|
|
|
return self
|
|
|
|
|
2012-09-19 19:32:57 +04:00
|
|
|
def dumps(self, obj):
|
|
|
|
"""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.
|
|
|
|
"""
|
|
|
|
dumps = self._dumps
|
|
|
|
if dumps is not None:
|
|
|
|
return dumps(obj)
|
|
|
|
else:
|
|
|
|
raise ImportError(
|
|
|
|
"json module not available: "
|
|
|
|
"you should provide a dumps function")
|
|
|
|
|
2012-09-19 07:26:35 +04:00
|
|
|
def getquoted(self):
|
2012-09-19 19:32:57 +04:00
|
|
|
s = self.dumps(self.adapted)
|
2012-09-19 07:26:35 +04:00
|
|
|
return QuotedString(s).getquoted()
|
|
|
|
|
|
|
|
|
2012-09-19 18:31:28 +04:00
|
|
|
def register_json(conn_or_curs=None, globally=False, loads=None,
|
2014-08-13 03:43:33 +04:00
|
|
|
oid=None, array_oid=None, name='json'):
|
2012-09-19 07:26:35 +04:00
|
|
|
"""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*
|
2014-08-13 03:43:33 +04:00
|
|
|
:param name: the name of the data type to look for in *conn_or_curs*
|
2012-09-19 07:26:35 +04:00
|
|
|
|
|
|
|
The connection or cursor passed to the function will be used to query the
|
2014-08-13 03:43:33 +04:00
|
|
|
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.
|
2012-09-19 07:26:35 +04:00
|
|
|
|
|
|
|
"""
|
|
|
|
if oid is None:
|
2014-08-13 03:43:33 +04:00
|
|
|
oid, array_oid = _get_json_oids(conn_or_curs, name)
|
2012-09-19 07:26:35 +04:00
|
|
|
|
2014-08-13 03:43:33 +04:00
|
|
|
JSON, JSONARRAY = _create_json_typecasters(
|
|
|
|
oid, array_oid, loads=loads, name=name.upper())
|
2012-09-19 07:26:35 +04:00
|
|
|
|
|
|
|
register_type(JSON, not globally and conn_or_curs or None)
|
|
|
|
|
|
|
|
if JSONARRAY is not None:
|
|
|
|
register_type(JSONARRAY, not globally and conn_or_curs or None)
|
|
|
|
|
|
|
|
return JSON, JSONARRAY
|
|
|
|
|
2012-09-19 18:49:00 +04:00
|
|
|
def register_default_json(conn_or_curs=None, globally=False, loads=None):
|
|
|
|
"""
|
|
|
|
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()`.
|
|
|
|
"""
|
|
|
|
return register_json(conn_or_curs=conn_or_curs, globally=globally,
|
|
|
|
loads=loads, oid=JSON_OID, array_oid=JSONARRAY_OID)
|
|
|
|
|
2014-08-13 03:54:49 +04:00
|
|
|
def register_default_jsonb(conn_or_curs=None, globally=False, loads=None):
|
|
|
|
"""
|
|
|
|
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()`.
|
|
|
|
"""
|
|
|
|
return register_json(conn_or_curs=conn_or_curs, globally=globally,
|
|
|
|
loads=loads, oid=JSONB_OID, array_oid=JSONBARRAY_OID, name='jsonb')
|
|
|
|
|
2014-08-13 03:43:33 +04:00
|
|
|
def _create_json_typecasters(oid, array_oid, loads=None, name='JSON'):
|
2012-09-19 07:26:35 +04:00
|
|
|
"""Create typecasters for json data type."""
|
|
|
|
if loads is None:
|
|
|
|
if json is None:
|
|
|
|
raise ImportError("no json module available")
|
|
|
|
else:
|
|
|
|
loads = json.loads
|
|
|
|
|
|
|
|
def typecast_json(s, cur):
|
2012-09-20 03:36:53 +04:00
|
|
|
if s is None:
|
|
|
|
return None
|
2012-09-19 07:26:35 +04:00
|
|
|
return loads(s)
|
|
|
|
|
2014-08-13 03:43:33 +04:00
|
|
|
JSON = new_type((oid, ), name, typecast_json)
|
2012-09-24 14:23:09 +04:00
|
|
|
if array_oid is not None:
|
2014-08-13 03:43:33 +04:00
|
|
|
JSONARRAY = new_array_type((array_oid, ), "%sARRAY" % name, JSON)
|
2012-09-24 14:23:09 +04:00
|
|
|
else:
|
|
|
|
JSONARRAY = None
|
2012-09-19 07:26:35 +04:00
|
|
|
|
|
|
|
return JSON, JSONARRAY
|
|
|
|
|
2014-08-13 03:43:33 +04:00
|
|
|
def _get_json_oids(conn_or_curs, name='json'):
|
2012-09-19 07:26:35 +04:00
|
|
|
# lazy imports
|
|
|
|
from psycopg2.extensions import STATUS_IN_TRANSACTION
|
|
|
|
from psycopg2.extras import _solve_conn_curs
|
|
|
|
|
|
|
|
conn, curs = _solve_conn_curs(conn_or_curs)
|
|
|
|
|
|
|
|
# Store the transaction status of the connection to revert it after use
|
|
|
|
conn_status = conn.status
|
|
|
|
|
|
|
|
# column typarray not available before PG 8.3
|
|
|
|
typarray = conn.server_version >= 80300 and "typarray" or "NULL"
|
|
|
|
|
|
|
|
# get the oid for the hstore
|
|
|
|
curs.execute(
|
2014-08-13 03:43:33 +04:00
|
|
|
"SELECT t.oid, %s FROM pg_type t WHERE t.typname = %%s;"
|
|
|
|
% typarray, (name,))
|
2012-09-19 07:26:35 +04:00
|
|
|
r = curs.fetchone()
|
|
|
|
|
|
|
|
# revert the status of the connection as before the command
|
|
|
|
if (conn_status != STATUS_IN_TRANSACTION and not conn.autocommit):
|
|
|
|
conn.rollback()
|
|
|
|
|
|
|
|
if not r:
|
2014-08-13 03:43:33 +04:00
|
|
|
raise conn.ProgrammingError("%s data type not found" % name)
|
2012-09-19 07:26:35 +04:00
|
|
|
|
|
|
|
return r
|
|
|
|
|
|
|
|
|
|
|
|
|