| 1 |
""" |
|---|
| 2 |
Various data structures used in query construction. |
|---|
| 3 |
|
|---|
| 4 |
Factored out from django.db.models.query so that they can also be used by other |
|---|
| 5 |
modules without getting into circular import difficulties. |
|---|
| 6 |
""" |
|---|
| 7 |
|
|---|
| 8 |
from copy import deepcopy |
|---|
| 9 |
|
|---|
| 10 |
from django.utils import tree |
|---|
| 11 |
|
|---|
| 12 |
class QueryWrapper(object): |
|---|
| 13 |
""" |
|---|
| 14 |
A type that indicates the contents are an SQL fragment and the associate |
|---|
| 15 |
parameters. Can be used to pass opaque data to a where-clause, for example. |
|---|
| 16 |
""" |
|---|
| 17 |
def __init__(self, sql, params): |
|---|
| 18 |
self.data = sql, params |
|---|
| 19 |
|
|---|
| 20 |
class Q(tree.Node): |
|---|
| 21 |
""" |
|---|
| 22 |
Encapsulates filters as objects that can then be combined logically (using |
|---|
| 23 |
& and |). |
|---|
| 24 |
""" |
|---|
| 25 |
# Connection types |
|---|
| 26 |
AND = 'AND' |
|---|
| 27 |
OR = 'OR' |
|---|
| 28 |
default = AND |
|---|
| 29 |
|
|---|
| 30 |
def __init__(self, *args, **kwargs): |
|---|
| 31 |
super(Q, self).__init__(children=list(args) + kwargs.items()) |
|---|
| 32 |
|
|---|
| 33 |
def _combine(self, other, conn): |
|---|
| 34 |
if not isinstance(other, Q): |
|---|
| 35 |
raise TypeError(other) |
|---|
| 36 |
obj = deepcopy(self) |
|---|
| 37 |
obj.add(other, conn) |
|---|
| 38 |
return obj |
|---|
| 39 |
|
|---|
| 40 |
def __or__(self, other): |
|---|
| 41 |
return self._combine(other, self.OR) |
|---|
| 42 |
|
|---|
| 43 |
def __and__(self, other): |
|---|
| 44 |
return self._combine(other, self.AND) |
|---|
| 45 |
|
|---|
| 46 |
def __invert__(self): |
|---|
| 47 |
obj = deepcopy(self) |
|---|
| 48 |
obj.negate() |
|---|
| 49 |
return obj |
|---|
| 50 |
|
|---|
| 51 |
def select_related_descend(field, restricted, requested): |
|---|
| 52 |
""" |
|---|
| 53 |
Returns True if this field should be used to descend deeper for |
|---|
| 54 |
select_related() purposes. Used by both the query construction code |
|---|
| 55 |
(sql.query.fill_related_selections()) and the model instance creation code |
|---|
| 56 |
(query.get_cached_row()). |
|---|
| 57 |
""" |
|---|
| 58 |
if not field.rel: |
|---|
| 59 |
return False |
|---|
| 60 |
if field.rel.parent_link: |
|---|
| 61 |
return False |
|---|
| 62 |
if restricted and field.name not in requested: |
|---|
| 63 |
return False |
|---|
| 64 |
if not restricted and field.null: |
|---|
| 65 |
return False |
|---|
| 66 |
return True |
|---|