Skip to content

Commit

Permalink
Issue #55 - Database Backend Support For Django 1.7
Browse files Browse the repository at this point in the history
* Got a start on refactoring the code from @kavdev's branch.
* Made a second conditional decode child method which gets defined if django.VERSION >= (1, 7). This method has @kavdev's implementation for Django 1.7 support.
* Refactored the existing code from master and put it in the else definition for the above check.
* Formatted both methods for PEP-8
  • Loading branch information
sethdenner committed Apr 15, 2015
1 parent 697d84e commit f35e663
Showing 1 changed file with 131 additions and 40 deletions.
171 changes: 131 additions & 40 deletions djangotoolbox/db/basecompiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ def get_selected_fields(query):
else:
return query.model._meta.fields


EMULATED_OPS = {
'exact': lambda x, y: y in x if isinstance(x, (list, tuple)) else x == y,
'iexact': lambda x, y: x.lower() == y.lower(),
Expand Down Expand Up @@ -188,46 +187,138 @@ def _decode_child(self, child):
Produces arguments suitable for add_filter from a WHERE tree
leaf (a tuple).
"""
if django.VERSION >= (1, 7):
def decode_child(
query,
child
):
lhs, lhs_params = child.process_lhs(
query.compiler,
query.connection
)
rhs, rhs_params = child.process_rhs(
query.compiler,
query.connection
)

lookup_type = child.lookup_name
annotation = True # No idea from where to pull this...
value = rhs_params

packed = child.lhs.get_group_by_cols()[0]
alias, column = packed
field = child.lhs.output_field
db_type = field.db_type(query.connection)

opts = query.query.model._meta
if alias and alias != opts.db_table:
raise DatabaseError(
"This database doesn't support JOINs "
"and multi-table inheritance."
)

'''
For parent.child_set queries the field held
by the constraint is the parent's primary
key, while the field the filter should
consider is the child's foreign key field.
'''
if column != field.column:
if not field.primary_key:
raise DatabaseError(
"This database doesn't support "
"filtering on non-primary key "
"ForeignKey fields."
)

field = (
f for f in opts.fields if
f.column == column
).next()
assert field.rel is not None

value = query._normalize_lookup_value(
lookup_type,
value,
field,
annotation
)

# TODO: Call get_db_prep_lookup directly, constraint.process
# doesn't do much more.
# constraint, lookup_type, annotation, value = child
# packed, value = constraint.process(lookup_type, value, self.connection)
# alias, column, db_type = packed
# field = constraint.field

lhs, lhs_params = child.process_lhs(self.compiler, self.connection)
rhs, rhs_params = child.process_rhs(self.compiler, self.connection)

lookup_type = child.lookup_name
annotation = True # No idea from where to pull this...
value = rhs_params

packed = child.lhs.get_group_by_cols()[0]
alias, column = packed
field = child.lhs.output_field
db_type = field.db_type(self.connection)

opts = self.query.model._meta
if alias and alias != opts.db_table:
raise DatabaseError("This database doesn't support JOINs "
"and multi-table inheritance.")

# For parent.child_set queries the field held by the constraint
# is the parent's primary key, while the field the filter
# should consider is the child's foreign key field.
if column != field.column:
if not field.primary_key:
raise DatabaseError("This database doesn't support filtering "
"on non-primary key ForeignKey fields.")

field = (f for f in opts.fields if f.column == column).next()
assert field.rel is not None

value = self._normalize_lookup_value(
lookup_type, value, field, annotation)

return field, lookup_type, value
return (
field,
lookup_type,
value
)

else:
def decode_child(
query,
child
):
"""
Produces arguments suitable for
add_filter from a WHERE tree
leaf (a tuple).
"""

"""
TODO: Call get_db_prep_lookup
directly, constraint.process
doesn't do much more.
"""
(
constraint,
lookup_type,
annotation,
value
)= child
packed, value = constraint.process(
lookup_type,
value,
query.connection
)
alias, column, db_type = packed
field = constraint.field
opts = query.query.model._meta
if alias and alias != opts.db_table:
raise DatabaseError(
"This database doesn't support"
"JOINs and multi-table inheritance."
)

"""
For parent.child_set queries the field held
by the constraint is the parent's primary
key, while the field the filter should
consider is the child's foreign key field.
"""
if column != field.column:
if not field.primary_key:
raise DatabaseError(
"This database doesn't support "
"filtering on non-primary key "
"ForeignKey fields."
)

field = (
f for f in opts.fields if
f.column == column
).next()
assert field.rel is not None

value = query._normalize_lookup_value(
lookup_type,
value,
field,
annotation
)

return field, lookup_type, value

return decode_child(
self,
child
)

def _normalize_lookup_value(self, lookup_type, value, field, annotation):
"""
Expand Down

0 comments on commit f35e663

Please sign in to comment.