2017-09-15 22:00:12 +08:00
|
|
|
import functools
|
|
|
|
|
2017-05-03 13:25:30 +08:00
|
|
|
import psycopg2
|
2014-03-15 01:34:49 +08:00
|
|
|
from psycopg2 import ProgrammingError
|
|
|
|
from psycopg2.extras import register_hstore
|
|
|
|
|
2017-09-15 22:00:12 +08:00
|
|
|
from django.db import connections
|
2017-09-19 02:29:47 +08:00
|
|
|
from django.db.backends.base.base import NO_DB_ALIAS
|
2017-09-15 22:00:12 +08:00
|
|
|
|
|
|
|
|
|
|
|
@functools.lru_cache()
|
|
|
|
def get_hstore_oids(connection_alias):
|
|
|
|
"""Return hstore and hstore array OIDs."""
|
|
|
|
with connections[connection_alias].cursor() as cursor:
|
|
|
|
cursor.execute(
|
|
|
|
"SELECT t.oid, typarray "
|
|
|
|
"FROM pg_type t "
|
|
|
|
"JOIN pg_namespace ns ON typnamespace = ns.oid "
|
|
|
|
"WHERE typname = 'hstore'"
|
|
|
|
)
|
|
|
|
oids = []
|
|
|
|
array_oids = []
|
|
|
|
for row in cursor:
|
|
|
|
oids.append(row[0])
|
|
|
|
array_oids.append(row[1])
|
|
|
|
return tuple(oids), tuple(array_oids)
|
|
|
|
|
|
|
|
|
|
|
|
@functools.lru_cache()
|
|
|
|
def get_citext_oids(connection_alias):
|
|
|
|
"""Return citext array OIDs."""
|
|
|
|
with connections[connection_alias].cursor() as cursor:
|
|
|
|
cursor.execute("SELECT typarray FROM pg_type WHERE typname = 'citext'")
|
|
|
|
return tuple(row[0] for row in cursor)
|
|
|
|
|
2014-03-15 01:34:49 +08:00
|
|
|
|
2017-05-03 13:25:30 +08:00
|
|
|
def register_type_handlers(connection, **kwargs):
|
2017-09-19 02:29:47 +08:00
|
|
|
if connection.vendor != 'postgresql' or connection.alias == NO_DB_ALIAS:
|
2014-03-15 01:34:49 +08:00
|
|
|
return
|
|
|
|
|
|
|
|
try:
|
2017-09-15 22:00:12 +08:00
|
|
|
oids, array_oids = get_hstore_oids(connection.alias)
|
|
|
|
register_hstore(connection.connection, globally=True, oid=oids, array_oid=array_oids)
|
2014-03-15 01:34:49 +08:00
|
|
|
except ProgrammingError:
|
|
|
|
# Hstore is not available on the database.
|
|
|
|
#
|
|
|
|
# If someone tries to create an hstore field it will error there.
|
|
|
|
# This is necessary as someone may be using PSQL without extensions
|
|
|
|
# installed but be using other features of contrib.postgres.
|
|
|
|
#
|
|
|
|
# This is also needed in order to create the connection in order to
|
|
|
|
# install the hstore extension.
|
|
|
|
pass
|
2017-05-03 13:25:30 +08:00
|
|
|
|
|
|
|
try:
|
2017-09-15 22:00:12 +08:00
|
|
|
citext_oids = get_citext_oids(connection.alias)
|
|
|
|
array_type = psycopg2.extensions.new_array_type(citext_oids, 'citext[]', psycopg2.STRING)
|
2017-05-03 13:25:30 +08:00
|
|
|
psycopg2.extensions.register_type(array_type, None)
|
|
|
|
except ProgrammingError:
|
|
|
|
# citext is not available on the database.
|
|
|
|
#
|
|
|
|
# The same comments in the except block of the above call to
|
|
|
|
# register_hstore() also apply here.
|
|
|
|
pass
|