Funktionierender Prototyp des Serious Games zur Vermittlung von Wissen zu Software-Engineering-Arbeitsmodellen.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

signals.py 2.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. import functools
  2. import psycopg2
  3. from psycopg2 import ProgrammingError
  4. from psycopg2.extras import register_hstore
  5. from django.db import connections
  6. from django.db.backends.base.base import NO_DB_ALIAS
  7. @functools.lru_cache
  8. def get_hstore_oids(connection_alias):
  9. """Return hstore and hstore array OIDs."""
  10. with connections[connection_alias].cursor() as cursor:
  11. cursor.execute(
  12. "SELECT t.oid, typarray "
  13. "FROM pg_type t "
  14. "JOIN pg_namespace ns ON typnamespace = ns.oid "
  15. "WHERE typname = 'hstore'"
  16. )
  17. oids = []
  18. array_oids = []
  19. for row in cursor:
  20. oids.append(row[0])
  21. array_oids.append(row[1])
  22. return tuple(oids), tuple(array_oids)
  23. @functools.lru_cache
  24. def get_citext_oids(connection_alias):
  25. """Return citext array OIDs."""
  26. with connections[connection_alias].cursor() as cursor:
  27. cursor.execute("SELECT typarray FROM pg_type WHERE typname = 'citext'")
  28. return tuple(row[0] for row in cursor)
  29. def register_type_handlers(connection, **kwargs):
  30. if connection.vendor != "postgresql" or connection.alias == NO_DB_ALIAS:
  31. return
  32. try:
  33. oids, array_oids = get_hstore_oids(connection.alias)
  34. register_hstore(
  35. connection.connection, globally=True, oid=oids, array_oid=array_oids
  36. )
  37. except ProgrammingError:
  38. # Hstore is not available on the database.
  39. #
  40. # If someone tries to create an hstore field it will error there.
  41. # This is necessary as someone may be using PSQL without extensions
  42. # installed but be using other features of contrib.postgres.
  43. #
  44. # This is also needed in order to create the connection in order to
  45. # install the hstore extension.
  46. pass
  47. try:
  48. citext_oids = get_citext_oids(connection.alias)
  49. array_type = psycopg2.extensions.new_array_type(
  50. citext_oids, "citext[]", psycopg2.STRING
  51. )
  52. psycopg2.extensions.register_type(array_type, None)
  53. except ProgrammingError:
  54. # citext is not available on the database.
  55. #
  56. # The same comments in the except block of the above call to
  57. # register_hstore() also apply here.
  58. pass