|
@@ -1,22 +1,16 @@
|
|
|
import functools
|
|
|
|
|
|
import psycopg2
|
|
|
-from psycopg2 import ProgrammingError
|
|
|
from psycopg2.extras import register_hstore
|
|
|
|
|
|
from django.db import connections
|
|
|
from django.db.backends.base.base import NO_DB_ALIAS
|
|
|
|
|
|
|
|
|
-@functools.lru_cache
|
|
|
-def get_hstore_oids(connection_alias):
|
|
|
- """Return hstore and hstore array OIDs."""
|
|
|
+def get_type_oids(connection_alias, type_name):
|
|
|
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'"
|
|
|
+ "SELECT oid, typarray FROM pg_type WHERE typname = %s", (type_name,)
|
|
|
)
|
|
|
oids = []
|
|
|
array_oids = []
|
|
@@ -26,43 +20,42 @@ def get_hstore_oids(connection_alias):
|
|
|
return tuple(oids), tuple(array_oids)
|
|
|
|
|
|
|
|
|
+@functools.lru_cache
|
|
|
+def get_hstore_oids(connection_alias):
|
|
|
+ """Return hstore and hstore array OIDs."""
|
|
|
+ return get_type_oids(connection_alias, "hstore")
|
|
|
+
|
|
|
+
|
|
|
@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)
|
|
|
+ """Return citext and citext array OIDs."""
|
|
|
+ return get_type_oids(connection_alias, "citext")
|
|
|
|
|
|
|
|
|
def register_type_handlers(connection, **kwargs):
|
|
|
if connection.vendor != "postgresql" or connection.alias == NO_DB_ALIAS:
|
|
|
return
|
|
|
|
|
|
- try:
|
|
|
- oids, array_oids = get_hstore_oids(connection.alias)
|
|
|
+ oids, array_oids = get_hstore_oids(connection.alias)
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ if oids:
|
|
|
register_hstore(
|
|
|
connection.connection, globally=True, oid=oids, array_oid=array_oids
|
|
|
)
|
|
|
- except ProgrammingError:
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- pass
|
|
|
|
|
|
- try:
|
|
|
- citext_oids = get_citext_oids(connection.alias)
|
|
|
+ oids, citext_oids = get_citext_oids(connection.alias)
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ if oids:
|
|
|
array_type = psycopg2.extensions.new_array_type(
|
|
|
citext_oids, "citext[]", psycopg2.STRING
|
|
|
)
|
|
|
psycopg2.extensions.register_type(array_type, None)
|
|
|
- except ProgrammingError:
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- pass
|