2020-01-21 06:35:58 +00:00
|
|
|
from copy import copy
|
|
|
|
|
|
|
|
from django.db import connections
|
|
|
|
from django.db.models import Field
|
|
|
|
from django.db.models.expressions import RawSQL
|
|
|
|
from django.db.models.sql.constants import INNER, LOUTER
|
|
|
|
from django.db.models.sql.datastructures import Join
|
|
|
|
from django.utils import six
|
|
|
|
|
|
|
|
from judge.utils.cachedict import CacheDict
|
|
|
|
|
|
|
|
|
2022-05-14 17:57:27 +00:00
|
|
|
def unique_together_left_join(
|
|
|
|
queryset, model, link_field_name, filter_field_name, filter_value, parent_model=None
|
|
|
|
):
|
2020-01-21 06:35:58 +00:00
|
|
|
link_field = copy(model._meta.get_field(link_field_name).remote_field)
|
|
|
|
filter_field = model._meta.get_field(filter_field_name)
|
|
|
|
|
|
|
|
def restrictions(where_class, alias, related_alias):
|
|
|
|
cond = where_class()
|
2022-05-14 17:57:27 +00:00
|
|
|
cond.add(
|
|
|
|
filter_field.get_lookup("exact")(filter_field.get_col(alias), filter_value),
|
|
|
|
"AND",
|
|
|
|
)
|
2020-01-21 06:35:58 +00:00
|
|
|
return cond
|
|
|
|
|
|
|
|
link_field.get_extra_restriction = restrictions
|
|
|
|
|
|
|
|
if parent_model is not None:
|
|
|
|
parent_alias = parent_model._meta.db_table
|
|
|
|
else:
|
|
|
|
parent_alias = queryset.query.get_initial_alias()
|
2022-05-14 17:57:27 +00:00
|
|
|
return queryset.query.join(
|
|
|
|
Join(model._meta.db_table, parent_alias, None, LOUTER, link_field, True)
|
|
|
|
)
|
2020-01-21 06:35:58 +00:00
|
|
|
|
|
|
|
|
|
|
|
class RawSQLJoin(Join):
|
2022-05-14 17:57:27 +00:00
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
subquery,
|
|
|
|
subquery_params,
|
|
|
|
parent_alias,
|
|
|
|
table_alias,
|
|
|
|
join_type,
|
|
|
|
join_field,
|
|
|
|
nullable,
|
|
|
|
filtered_relation=None,
|
|
|
|
):
|
2020-01-21 06:35:58 +00:00
|
|
|
self.subquery_params = subquery_params
|
2022-05-14 17:57:27 +00:00
|
|
|
super().__init__(
|
|
|
|
subquery,
|
|
|
|
parent_alias,
|
|
|
|
table_alias,
|
|
|
|
join_type,
|
|
|
|
join_field,
|
|
|
|
nullable,
|
|
|
|
filtered_relation,
|
|
|
|
)
|
2020-01-21 06:35:58 +00:00
|
|
|
|
|
|
|
def as_sql(self, compiler, connection):
|
2022-05-14 17:57:27 +00:00
|
|
|
compiler.quote_cache[self.table_name] = "(%s)" % self.table_name
|
2020-01-21 06:35:58 +00:00
|
|
|
sql, params = super().as_sql(compiler, connection)
|
|
|
|
return sql, self.subquery_params + params
|
|
|
|
|
|
|
|
|
|
|
|
class FakeJoinField:
|
|
|
|
def __init__(self, joining_columns):
|
|
|
|
self.joining_columns = joining_columns
|
|
|
|
|
|
|
|
def get_joining_columns(self):
|
|
|
|
return self.joining_columns
|
|
|
|
|
|
|
|
def get_extra_restriction(self, where_class, alias, remote_alias):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2022-05-14 17:57:27 +00:00
|
|
|
def join_sql_subquery(
|
|
|
|
queryset, subquery, params, join_fields, alias, join_type=INNER, parent_model=None
|
|
|
|
):
|
2020-01-21 06:35:58 +00:00
|
|
|
if parent_model is not None:
|
|
|
|
parent_alias = parent_model._meta.db_table
|
|
|
|
else:
|
|
|
|
parent_alias = queryset.query.get_initial_alias()
|
|
|
|
queryset.query.external_aliases.add(alias)
|
2022-05-14 17:57:27 +00:00
|
|
|
join = RawSQLJoin(
|
|
|
|
subquery,
|
|
|
|
params,
|
|
|
|
parent_alias,
|
|
|
|
alias,
|
|
|
|
join_type,
|
|
|
|
FakeJoinField(join_fields),
|
|
|
|
join_type == LOUTER,
|
|
|
|
)
|
2020-01-21 06:35:58 +00:00
|
|
|
queryset.query.join(join)
|
|
|
|
join.table_alias = alias
|
|
|
|
|
|
|
|
|
|
|
|
def RawSQLColumn(model, field=None):
|
|
|
|
if isinstance(model, Field):
|
|
|
|
field = model
|
|
|
|
model = field.model
|
|
|
|
if isinstance(field, six.string_types):
|
|
|
|
field = model._meta.get_field(field)
|
2022-05-14 17:57:27 +00:00
|
|
|
return RawSQL("%s.%s" % (model._meta.db_table, field.get_attname_column()[1]), ())
|
2020-01-21 06:35:58 +00:00
|
|
|
|
|
|
|
|
|
|
|
def make_straight_join_query(QueryType):
|
|
|
|
class Query(QueryType):
|
|
|
|
def join(self, join, *args, **kwargs):
|
|
|
|
alias = super().join(join, *args, **kwargs)
|
|
|
|
join = self.alias_map[alias]
|
|
|
|
if join.join_type == INNER:
|
2022-05-14 17:57:27 +00:00
|
|
|
join.join_type = "STRAIGHT_JOIN"
|
2020-01-21 06:35:58 +00:00
|
|
|
return alias
|
|
|
|
|
|
|
|
return Query
|
|
|
|
|
|
|
|
|
|
|
|
straight_join_cache = CacheDict(make_straight_join_query)
|
|
|
|
|
|
|
|
|
|
|
|
def use_straight_join(queryset):
|
2022-05-14 17:57:27 +00:00
|
|
|
if connections[queryset.db].vendor != "mysql":
|
2020-01-21 06:35:58 +00:00
|
|
|
return
|
|
|
|
try:
|
|
|
|
cloner = queryset.query.chain
|
|
|
|
except AttributeError:
|
|
|
|
cloner = queryset.query.clone
|
|
|
|
queryset.query = cloner(straight_join_cache[type(queryset.query)])
|