base.py 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. from django.db.backends.postgresql_psycopg2.base import *
  2. from django.db.backends.postgresql_psycopg2.base import DatabaseFeatures as PostgresqlDatabaseFeatures
  3. from django.db.backends.postgresql_psycopg2.base import DatabaseOperations as PostgresqlDatabaseOperations
  4. from django.db.backends.postgresql_psycopg2.base import DatabaseWrapper as PostgresqlDatabaseWrapper
  5. class DatabaseFeatures(PostgresqlDatabaseFeatures):
  6. uses_custom_query_class = True
  7. class DatabaseOperations(PostgresqlDatabaseOperations):
  8. def field_cast_sql(self, db_type):
  9. return '%s'
  10. def query_class(self, DefaultQueryClass):
  11. from django.db.models.sql.where import WhereNode
  12. from django.db.models.sql.constants import QUERY_TERMS
  13. INET_TERMS = {}
  14. INET_TERMS['inet_lt'] = '<'
  15. INET_TERMS['inet_lte'] = '<='
  16. INET_TERMS['inet_exact'] = '='
  17. INET_TERMS['inet_gte'] = '>='
  18. INET_TERMS['inet_gt'] = '>'
  19. INET_TERMS['inet_not'] = '<>'
  20. INET_TERMS['inet_is_contained'] = '<<'
  21. INET_TERMS['inet_is_contained_or_equal'] = '<<='
  22. INET_TERMS['inet_contains'] = '>>'
  23. INET_TERMS['inet_contains'] = '>>='
  24. ALL_TERMS = QUERY_TERMS.copy()
  25. ALL_TERMS.update(INET_TERMS)
  26. class InetAwareWhereNode(WhereNode):
  27. def make_atom(self, child, qn):
  28. table_alias, name, db_type, lookup_type, value_annot, params = child
  29. if db_type != 'inet':
  30. return super(InetAwareWhereNode, self).make_atom(child, qn)
  31. if lookup_type in INET_TERMS:
  32. return ('%s.%s %s inet %%s' % (table_alias, name, INET_TERMS[lookup_type]), params)
  33. return super(InetAwareWhereNode, self).make_atom(child, qn)
  34. class InetAwareQueryClass(DefaultQueryClass):
  35. query_terms = ALL_TERMS
  36. def __init__(self, model, connection, where=InetAwareWhereNode):
  37. super(InetAwareQueryClass, self).__init__(model, connection, where)
  38. return InetAwareQueryClass
  39. class DatabaseWrapper(PostgresqlDatabaseWrapper):
  40. def __init__(self, *args, **kwargs):
  41. super(DatabaseWrapper, self).__init__(*args, **kwargs)
  42. self.features = DatabaseFeatures()
  43. self.ops = DatabaseOperations()