diff options
Diffstat (limited to 'django/db/models/sql')
| -rw-r--r-- | django/db/models/sql/aggregates.py | 7 | ||||
| -rw-r--r-- | django/db/models/sql/compiler.py | 7 | ||||
| -rw-r--r-- | django/db/models/sql/datastructures.py | 7 | ||||
| -rw-r--r-- | django/db/models/sql/expressions.py | 4 | ||||
| -rw-r--r-- | django/db/models/sql/query.py | 10 | ||||
| -rw-r--r-- | django/db/models/sql/subqueries.py | 11 | ||||
| -rw-r--r-- | django/db/models/sql/where.py | 9 |
7 files changed, 41 insertions, 14 deletions
diff --git a/django/db/models/sql/aggregates.py b/django/db/models/sql/aggregates.py index 2bd2b2f76f..9fc5fe8a5b 100644 --- a/django/db/models/sql/aggregates.py +++ b/django/db/models/sql/aggregates.py @@ -9,6 +9,7 @@ from django.db.models.fields import IntegerField, FloatField ordinal_aggregate_field = IntegerField() computed_aggregate_field = FloatField() + class Aggregate(object): """ Default SQL Aggregate. @@ -93,6 +94,7 @@ class Avg(Aggregate): is_computed = True sql_function = 'AVG' + class Count(Aggregate): is_ordinal = True sql_function = 'COUNT' @@ -101,12 +103,15 @@ class Count(Aggregate): def __init__(self, col, distinct=False, **extra): super(Count, self).__init__(col, distinct='DISTINCT ' if distinct else '', **extra) + class Max(Aggregate): sql_function = 'MAX' + class Min(Aggregate): sql_function = 'MIN' + class StdDev(Aggregate): is_computed = True @@ -114,9 +119,11 @@ class StdDev(Aggregate): super(StdDev, self).__init__(col, **extra) self.sql_function = 'STDDEV_SAMP' if sample else 'STDDEV_POP' + class Sum(Aggregate): sql_function = 'SUM' + class Variance(Aggregate): is_computed = True diff --git a/django/db/models/sql/compiler.py b/django/db/models/sql/compiler.py index f70750abed..0b12bd1552 100644 --- a/django/db/models/sql/compiler.py +++ b/django/db/models/sql/compiler.py @@ -688,7 +688,7 @@ class SQLCompiler(object): # the relation, which is always nullable. new_nullable = True table = model._meta.db_table - self.fill_related_selections(model._meta, table, cur_depth+1, + self.fill_related_selections(model._meta, table, cur_depth + 1, next, restricted, new_nullable) def deferred_to_columns(self): @@ -915,6 +915,7 @@ class SQLDeleteCompiler(SQLCompiler): result.append('WHERE %s' % where) return ' '.join(result), tuple(params) + class SQLUpdateCompiler(SQLCompiler): def as_sql(self): """ @@ -1029,6 +1030,7 @@ class SQLUpdateCompiler(SQLCompiler): for alias in self.query.tables[1:]: self.query.alias_refcount[alias] = 0 + class SQLAggregateCompiler(SQLCompiler): def as_sql(self, qn=None): """ @@ -1050,6 +1052,7 @@ class SQLAggregateCompiler(SQLCompiler): params = params + self.query.sub_params return sql, params + class SQLDateCompiler(SQLCompiler): def results_iter(self): """ @@ -1075,6 +1078,7 @@ class SQLDateCompiler(SQLCompiler): date = date.date() yield date + class SQLDateTimeCompiler(SQLCompiler): def results_iter(self): """ @@ -1107,6 +1111,7 @@ class SQLDateTimeCompiler(SQLCompiler): datetime = timezone.make_aware(datetime, self.query.tzinfo) yield datetime + def order_modified_iter(cursor, trim, sentinel): """ Yields blocks of rows from a cursor. We use this iterator in the special diff --git a/django/db/models/sql/datastructures.py b/django/db/models/sql/datastructures.py index daaabbe6da..76b3db5b35 100644 --- a/django/db/models/sql/datastructures.py +++ b/django/db/models/sql/datastructures.py @@ -3,9 +3,11 @@ Useful auxilliary data structures for query construction. Not useful outside the SQL domain. """ + class EmptyResultSet(Exception): pass + class MultiJoin(Exception): """ Used by join construction code to indicate the point at which a @@ -17,12 +19,10 @@ class MultiJoin(Exception): # The path travelled, this includes the path to the multijoin. self.names_with_path = path_with_names + class Empty(object): pass -class RawValue(object): - def __init__(self, value): - self.value = value class Date(object): """ @@ -42,6 +42,7 @@ class Date(object): col = self.col return connection.ops.date_trunc_sql(self.lookup_type, col), [] + class DateTime(object): """ Add a datetime selection column. diff --git a/django/db/models/sql/expressions.py b/django/db/models/sql/expressions.py index 62adf79d87..f9a8929974 100644 --- a/django/db/models/sql/expressions.py +++ b/django/db/models/sql/expressions.py @@ -1,7 +1,9 @@ +import copy + from django.core.exceptions import FieldError from django.db.models.constants import LOOKUP_SEP from django.db.models.fields import FieldDoesNotExist -import copy + class SQLEvaluator(object): def __init__(self, expression, query, allow_joins=True, reuse=None): diff --git a/django/db/models/sql/query.py b/django/db/models/sql/query.py index 75e8e7540d..110925114c 100644 --- a/django/db/models/sql/query.py +++ b/django/db/models/sql/query.py @@ -615,7 +615,6 @@ class Query(object): for model, values in six.iteritems(seen): callback(target, model, values) - def deferred_to_columns_cb(self, target, model, fields): """ Callback used by deferred_to_columns(). The "target" parameter should @@ -627,7 +626,6 @@ class Query(object): for field in fields: target[table].add(field.column) - def table_alias(self, table_name, create=False): """ Returns a table alias for the given table_name and whether this is a @@ -955,7 +953,6 @@ class Query(object): self.unref_alias(alias) self.included_inherited_models = {} - def add_aggregate(self, aggregate, model, alias, is_summary): """ Adds a single aggregate expression to the Query @@ -1780,7 +1777,7 @@ class Query(object): return self._aggregate_select_cache elif self.aggregate_select_mask is not None: self._aggregate_select_cache = SortedDict([ - (k,v) for k,v in self.aggregates.items() + (k, v) for k, v in self.aggregates.items() if k in self.aggregate_select_mask ]) return self._aggregate_select_cache @@ -1793,7 +1790,7 @@ class Query(object): return self._extra_select_cache elif self.extra_select_mask is not None: self._extra_select_cache = SortedDict([ - (k,v) for k,v in self.extra.items() + (k, v) for k, v in self.extra.items() if k in self.extra_select_mask ]) return self._extra_select_cache @@ -1876,6 +1873,7 @@ class Query(object): else: return field.null + def get_order_dir(field, default='ASC'): """ Returns the field name and direction for an order specification. For @@ -1900,6 +1898,7 @@ def add_to_dict(data, key, value): else: data[key] = set([value]) + def is_reverse_o2o(field): """ A little helper to check if the given field is reverse-o2o. The field is @@ -1907,6 +1906,7 @@ def is_reverse_o2o(field): """ return not hasattr(field, 'rel') and field.field.unique + def alias_diff(refcounts_before, refcounts_after): """ Given the before and after copies of refcounts works out which aliases diff --git a/django/db/models/sql/subqueries.py b/django/db/models/sql/subqueries.py index 20770162c9..6aab02bd9a 100644 --- a/django/db/models/sql/subqueries.py +++ b/django/db/models/sql/subqueries.py @@ -7,7 +7,7 @@ from django.core.exceptions import FieldError from django.db import connections from django.db.models.constants import LOOKUP_SEP from django.db.models.fields import DateField, DateTimeField, FieldDoesNotExist -from django.db.models.sql.constants import * +from django.db.models.sql.constants import GET_ITERATOR_CHUNK_SIZE, SelectInfo from django.db.models.sql.datastructures import Date, DateTime from django.db.models.sql.query import Query from django.db.models.sql.where import AND, Constraint @@ -20,6 +20,7 @@ from django.utils import timezone __all__ = ['DeleteQuery', 'UpdateQuery', 'InsertQuery', 'DateQuery', 'DateTimeQuery', 'AggregateQuery'] + class DeleteQuery(Query): """ Delete queries are done through this class, since they are more constrained @@ -77,7 +78,9 @@ class DeleteQuery(Query): return else: innerq.clear_select_clause() - innerq.select = [SelectInfo((self.get_initial_alias(), pk.column), None)] + innerq.select = [ + SelectInfo((self.get_initial_alias(), pk.column), None) + ] values = innerq where = self.where_class() where.add((Constraint(None, pk.column, pk), 'in', values), AND) @@ -178,6 +181,7 @@ class UpdateQuery(Query): result.append(query) return result + class InsertQuery(Query): compiler = 'SQLInsertCompiler' @@ -215,6 +219,7 @@ class InsertQuery(Query): self.objs = objs self.raw = raw + class DateQuery(Query): """ A DateQuery is a normal query, except that it specifically selects a single @@ -260,6 +265,7 @@ class DateQuery(Query): def _get_select(self, col, lookup_type): return Date(col, lookup_type) + class DateTimeQuery(DateQuery): """ A DateTimeQuery is like a DateQuery but for a datetime field. If time zone @@ -280,6 +286,7 @@ class DateTimeQuery(DateQuery): tzname = timezone._get_timezone_name(self.tzinfo) return DateTime(col, lookup_type, tzname) + class AggregateQuery(Query): """ An AggregateQuery takes another query as a parameter to the FROM diff --git a/django/db/models/sql/where.py b/django/db/models/sql/where.py index 2a342d417a..024b995c99 100644 --- a/django/db/models/sql/where.py +++ b/django/db/models/sql/where.py @@ -16,10 +16,12 @@ from django.utils.six.moves import xrange from django.utils import timezone from django.utils import tree + # Connection types AND = 'AND' OR = 'OR' + class EmptyShortCircuit(Exception): """ Internal exception used to indicate that a "matches nothing" node should be @@ -27,6 +29,7 @@ class EmptyShortCircuit(Exception): """ pass + class WhereNode(tree.Node): """ Used to represent the SQL where-clause. @@ -175,7 +178,7 @@ class WhereNode(tree.Node): """ lvalue, lookup_type, value_annotation, params_or_value = child field_internal_type = lvalue.field.get_internal_type() if lvalue.field else None - + if isinstance(lvalue, Constraint): try: lvalue, params = lvalue.process(lookup_type, params_or_value, connection) @@ -304,14 +307,15 @@ class WhereNode(tree.Node): clone.children.append(child) return clone -class EmptyWhere(WhereNode): +class EmptyWhere(WhereNode): def add(self, data, connector): return def as_sql(self, qn=None, connection=None): raise EmptyResultSet + class EverythingNode(object): """ A node that matches everything. @@ -385,6 +389,7 @@ class Constraint(object): new.alias, new.col, new.field = change_map[self.alias], self.col, self.field return new + class SubqueryConstraint(object): def __init__(self, alias, columns, targets, query_object): self.alias = alias |
