2
0

operations.py 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. import re
  2. from django.db.backends import BaseDatabaseOperations
  3. class DatabaseOperations(BaseDatabaseOperations):
  4. def __init__(self, connection):
  5. super(DatabaseOperations, self).__init__(connection)
  6. self._postgres_version = None
  7. def _get_postgres_version(self):
  8. if self._postgres_version is None:
  9. from django.db.backends.postgresql_psycopg2.version import get_version
  10. cursor = self.connection.cursor()
  11. self._postgres_version = get_version(cursor)
  12. return self._postgres_version
  13. postgres_version = property(_get_postgres_version)
  14. def date_extract_sql(self, lookup_type, field_name):
  15. # http://www.postgresql.org/docs/8.0/static/functions-datetime.html#FUNCTIONS-DATETIME-EXTRACT
  16. if lookup_type == 'week_day':
  17. # For consistency across backends, we return Sunday=1, Saturday=7.
  18. return "EXTRACT('dow' FROM %s) + 1" % field_name
  19. else:
  20. return "EXTRACT('%s' FROM %s)" % (lookup_type, field_name)
  21. def date_interval_sql(self, sql, connector, timedelta):
  22. """
  23. implements the interval functionality for expressions
  24. format for Postgres:
  25. (datefield + interval '3 days 200 seconds 5 microseconds')
  26. """
  27. modifiers = []
  28. if timedelta.days:
  29. modifiers.append(u'%s days' % timedelta.days)
  30. if timedelta.seconds:
  31. modifiers.append(u'%s seconds' % timedelta.seconds)
  32. if timedelta.microseconds:
  33. modifiers.append(u'%s microseconds' % timedelta.microseconds)
  34. mods = u' '.join(modifiers)
  35. conn = u' %s ' % connector
  36. return u'(%s)' % conn.join([sql, u'interval \'%s\'' % mods])
  37. def date_trunc_sql(self, lookup_type, field_name):
  38. # http://www.postgresql.org/docs/8.0/static/functions-datetime.html#FUNCTIONS-DATETIME-TRUNC
  39. return "DATE_TRUNC('%s', %s)" % (lookup_type, field_name)
  40. def deferrable_sql(self):
  41. return " DEFERRABLE INITIALLY DEFERRED"
  42. def lookup_cast(self, lookup_type):
  43. lookup = '%s'
  44. # Cast text lookups to text to allow things like filter(x__contains=4)
  45. if lookup_type in ('iexact', 'contains', 'icontains', 'startswith',
  46. 'istartswith', 'endswith', 'iendswith'):
  47. lookup = "%s::text"
  48. # Use UPPER(x) for case-insensitive lookups; it's faster.
  49. if lookup_type in ('iexact', 'icontains', 'istartswith', 'iendswith'):
  50. lookup = 'UPPER(%s)' % lookup
  51. return lookup
  52. def field_cast_sql(self, db_type):
  53. if db_type == 'inet':
  54. return 'HOST(%s)'
  55. return '%s'
  56. def last_insert_id(self, cursor, table_name, pk_name):
  57. # Use pg_get_serial_sequence to get the underlying sequence name
  58. # from the table name and column name (available since PostgreSQL 8)
  59. cursor.execute("SELECT CURRVAL(pg_get_serial_sequence('%s','%s'))" % (
  60. self.quote_name(table_name), pk_name))
  61. return cursor.fetchone()[0]
  62. def no_limit_value(self):
  63. return None
  64. def quote_name(self, name):
  65. if name.startswith('"') and name.endswith('"'):
  66. return name # Quoting once is enough.
  67. return '"%s"' % name
  68. def sql_flush(self, style, tables, sequences):
  69. if tables:
  70. if self.postgres_version[0:2] >= (8,1):
  71. # Postgres 8.1+ can do 'TRUNCATE x, y, z...;'. In fact, it *has to*
  72. # in order to be able to truncate tables referenced by a foreign
  73. # key in any other table. The result is a single SQL TRUNCATE
  74. # statement.
  75. sql = ['%s %s;' % \
  76. (style.SQL_KEYWORD('TRUNCATE'),
  77. style.SQL_FIELD(', '.join([self.quote_name(table) for table in tables]))
  78. )]
  79. else:
  80. # Older versions of Postgres can't do TRUNCATE in a single call, so
  81. # they must use a simple delete.
  82. sql = ['%s %s %s;' % \
  83. (style.SQL_KEYWORD('DELETE'),
  84. style.SQL_KEYWORD('FROM'),
  85. style.SQL_FIELD(self.quote_name(table))
  86. ) for table in tables]
  87. # 'ALTER SEQUENCE sequence_name RESTART WITH 1;'... style SQL statements
  88. # to reset sequence indices
  89. for sequence_info in sequences:
  90. table_name = sequence_info['table']
  91. column_name = sequence_info['column']
  92. if not (column_name and len(column_name) > 0):
  93. # This will be the case if it's an m2m using an autogenerated
  94. # intermediate table (see BaseDatabaseIntrospection.sequence_list)
  95. column_name = 'id'
  96. sql.append("%s setval(pg_get_serial_sequence('%s','%s'), 1, false);" % \
  97. (style.SQL_KEYWORD('SELECT'),
  98. style.SQL_TABLE(self.quote_name(table_name)),
  99. style.SQL_FIELD(column_name))
  100. )
  101. return sql
  102. else:
  103. return []
  104. def sequence_reset_sql(self, style, model_list):
  105. from django.db import models
  106. output = []
  107. qn = self.quote_name
  108. for model in model_list:
  109. # Use `coalesce` to set the sequence for each model to the max pk value if there are records,
  110. # or 1 if there are none. Set the `is_called` property (the third argument to `setval`) to true
  111. # if there are records (as the max pk value is already in use), otherwise set it to false.
  112. # Use pg_get_serial_sequence to get the underlying sequence name from the table name
  113. # and column name (available since PostgreSQL 8)
  114. for f in model._meta.local_fields:
  115. if isinstance(f, models.AutoField):
  116. output.append("%s setval(pg_get_serial_sequence('%s','%s'), coalesce(max(%s), 1), max(%s) %s null) %s %s;" % \
  117. (style.SQL_KEYWORD('SELECT'),
  118. style.SQL_TABLE(qn(model._meta.db_table)),
  119. style.SQL_FIELD(f.column),
  120. style.SQL_FIELD(qn(f.column)),
  121. style.SQL_FIELD(qn(f.column)),
  122. style.SQL_KEYWORD('IS NOT'),
  123. style.SQL_KEYWORD('FROM'),
  124. style.SQL_TABLE(qn(model._meta.db_table))))
  125. break # Only one AutoField is allowed per model, so don't bother continuing.
  126. for f in model._meta.many_to_many:
  127. if not f.rel.through:
  128. output.append("%s setval(pg_get_serial_sequence('%s','%s'), coalesce(max(%s), 1), max(%s) %s null) %s %s;" % \
  129. (style.SQL_KEYWORD('SELECT'),
  130. style.SQL_TABLE(qn(f.m2m_db_table())),
  131. style.SQL_FIELD('id'),
  132. style.SQL_FIELD(qn('id')),
  133. style.SQL_FIELD(qn('id')),
  134. style.SQL_KEYWORD('IS NOT'),
  135. style.SQL_KEYWORD('FROM'),
  136. style.SQL_TABLE(qn(f.m2m_db_table()))))
  137. return output
  138. def savepoint_create_sql(self, sid):
  139. return "SAVEPOINT %s" % sid
  140. def savepoint_commit_sql(self, sid):
  141. return "RELEASE SAVEPOINT %s" % sid
  142. def savepoint_rollback_sql(self, sid):
  143. return "ROLLBACK TO SAVEPOINT %s" % sid
  144. def prep_for_iexact_query(self, x):
  145. return x
  146. def check_aggregate_support(self, aggregate):
  147. """Check that the backend fully supports the provided aggregate.
  148. The population and sample statistics (STDDEV_POP, STDDEV_SAMP,
  149. VAR_POP, VAR_SAMP) were first implemented in Postgres 8.2.
  150. The implementation of population statistics (STDDEV_POP and VAR_POP)
  151. under Postgres 8.2 - 8.2.4 is known to be faulty. Raise
  152. NotImplementedError if this is the database in use.
  153. """
  154. if aggregate.sql_function in ('STDDEV_POP', 'STDDEV_SAMP', 'VAR_POP', 'VAR_SAMP'):
  155. if self.postgres_version[0:2] < (8,2):
  156. raise NotImplementedError('PostgreSQL does not support %s prior to version 8.2. Please upgrade your version of PostgreSQL.' % aggregate.sql_function)
  157. if aggregate.sql_function in ('STDDEV_POP', 'VAR_POP'):
  158. if self.postgres_version[0:2] == (8,2):
  159. if self.postgres_version[2] is None or self.postgres_version[2] <= 4:
  160. raise NotImplementedError('PostgreSQL 8.2 to 8.2.4 is known to have a faulty implementation of %s. Please upgrade your version of PostgreSQL.' % aggregate.sql_function)
  161. def max_name_length(self):
  162. """
  163. Returns the maximum length of an identifier.
  164. Note that the maximum length of an identifier is 63 by default, but can
  165. be changed by recompiling PostgreSQL after editing the NAMEDATALEN
  166. macro in src/include/pg_config_manual.h .
  167. This implementation simply returns 63, but can easily be overridden by a
  168. custom database backend that inherits most of its behavior from this one.
  169. """
  170. return 63
  171. def last_executed_query(self, cursor, sql, params):
  172. # http://initd.org/psycopg/docs/cursor.html#cursor.query
  173. # The query attribute is a Psycopg extension to the DB API 2.0.
  174. return cursor.query
  175. def return_insert_id(self):
  176. return "RETURNING %s", ()