| 1 |
import functools |
| 2 |
|
| 3 |
from django.db import connections |
| 4 |
from django.db.backends.base.base import NO_DB_ALIAS |
| 5 |
from django.db.backends.postgresql.psycopg_any import is_psycopg3 |
| 6 |
|
| 7 |
|
| 8 |
def get_type_oids(connection_alias, type_name): |
| 9 |
with connections[connection_alias].cursor() as cursor: |
| 10 |
cursor.execute( |
| 11 |
"SELECT oid, typarray FROM pg_type WHERE typname = %s", (type_name,) |
| 12 |
) |
| 13 |
oids = [] |
| 14 |
array_oids = [] |
| 15 |
for row in cursor: |
| 16 |
oids.append(row[0]) |
| 17 |
array_oids.append(row[1]) |
| 18 |
return tuple(oids), tuple(array_oids) |
| 19 |
|
| 20 |
|
| 21 |
@functools.lru_cache |
| 22 |
def get_hstore_oids(connection_alias): |
| 23 |
"""Return hstore and hstore array OIDs.""" |
| 24 |
return get_type_oids(connection_alias, "hstore") |
| 25 |
|
| 26 |
|
| 27 |
@functools.lru_cache |
| 28 |
def get_citext_oids(connection_alias): |
| 29 |
"""Return citext and citext array OIDs.""" |
| 30 |
return get_type_oids(connection_alias, "citext") |
| 31 |
|
| 32 |
|
| 33 |
if is_psycopg3: |
| 34 |
from psycopg.types import TypeInfo, hstore |
| 35 |
|
| 36 |
def register_type_handlers(connection, **kwargs): |
| 37 |
if connection.vendor != "postgresql" or connection.alias == NO_DB_ALIAS: |
| 38 |
return |
| 39 |
|
| 40 |
oids, array_oids = get_hstore_oids(connection.alias) |
| 41 |
for oid, array_oid in zip(oids, array_oids): |
| 42 |
ti = TypeInfo("hstore", oid, array_oid) |
| 43 |
hstore.register_hstore(ti, connection.connection) |
| 44 |
|
| 45 |
_, citext_oids = get_citext_oids(connection.alias) |
| 46 |
for array_oid in citext_oids: |
| 47 |
ti = TypeInfo("citext", 0, array_oid) |
| 48 |
ti.register(connection.connection) |
| 49 |
|
| 50 |
else: |
| 51 |
import psycopg2 |
| 52 |
from psycopg2.extras import register_hstore |
| 53 |
|
| 54 |
def register_type_handlers(connection, **kwargs): |
| 55 |
if connection.vendor != "postgresql" or connection.alias == NO_DB_ALIAS: |
| 56 |
return |
| 57 |
|
| 58 |
oids, array_oids = get_hstore_oids(connection.alias) |
| 59 |
# Don't register handlers when hstore is not available on the database. |
| 60 |
# |
| 61 |
# If someone tries to create an hstore field it will error there. This is |
| 62 |
# necessary as someone may be using PSQL without extensions installed but |
| 63 |
# be using other features of contrib.postgres. |
| 64 |
# |
| 65 |
# This is also needed in order to create the connection in order to install |
| 66 |
# the hstore extension. |
| 67 |
if oids: |
| 68 |
register_hstore( |
| 69 |
connection.connection, globally=True, oid=oids, array_oid=array_oids |
| 70 |
) |
| 71 |
|
| 72 |
oids, citext_oids = get_citext_oids(connection.alias) |
| 73 |
# Don't register handlers when citext is not available on the database. |
| 74 |
# |
| 75 |
# The same comments in the above call to register_hstore() also apply here. |
| 76 |
if oids: |
| 77 |
array_type = psycopg2.extensions.new_array_type( |
| 78 |
citext_oids, "citext[]", psycopg2.STRING |
| 79 |
) |
| 80 |
psycopg2.extensions.register_type(array_type, None) |