def join_to_table(
        table,
        table2,
        table_field,
        table2_field,
        queryset,
        alias,
        extra_restriction_func=lambda where_class, alias, related_alias: None):
    """
    Add a join on `table2` to `queryset` (having table `table`).
    """
    foreign_object = ForeignObject(to=table2,
                                   from_fields=[None],
                                   to_fields=[None],
                                   rel=None)
    foreign_object.opts = Options(table._meta)
    foreign_object.opts.model = table
    foreign_object.get_joining_columns = lambda: (
        (table_field, table2_field), )
    foreign_object.get_extra_restriction = extra_restriction_func

    join = Join(table2._meta.db_table, table._meta.db_table, alias, LOUTER,
                foreign_object, True)
    queryset.query.join(join)

    # hook for set alias
    join.table_alias = alias
    queryset.query.external_aliases.add(alias)

    return queryset
示例#2
0
def join_to(queryset, subquery, table_field, subquery_field, alias, join_type,
            nullable):
    """
    Add a join on `subquery` to `queryset` (having table `table`).
    """

    # here you can set complex clause for join
    def extra_join_cond(where_class, alias, related_alias):
        if (alias, related_alias) == ('[sys].[columns]',
                                      '[sys].[database_permissions]'):
            where = '[sys].[columns].[column_id] = ' \
                    '[sys].[database_permissions].[minor_id]'
            children = [ExtraWhere([where], ())]
            return where_class(children)
        return None

    table = queryset.model

    foreign_object = ForeignObject(to=subquery,
                                   on_delete=DO_NOTHING,
                                   from_fields=[None],
                                   to_fields=[None],
                                   rel=None)
    foreign_object.opts = Options(table._meta)
    foreign_object.opts.model = table
    foreign_object.get_joining_columns = lambda: (
        (table_field, subquery_field), )
    foreign_object.get_extra_restriction = extra_join_cond

    if isinstance(subquery.query, RawQuery):
        subquery_sql, subquery_params = subquery.query.sql, subquery.query.params
    else:
        subquery_sql, subquery_params = subquery.query.sql_with_params()

    join = CustomJoin(subquery_sql, subquery_params, table._meta.db_table,
                      alias, join_type, foreign_object, nullable)

    # init first alias for this query
    queryset.query.get_initial_alias()

    # join subquery
    queryset.query.join(join)

    # hook for set alias
    join.table_alias = alias

    return queryset
示例#3
0
def join_to(table, subquery, table_field, subquery_field, queryset, alias):
    """
    Add a join on `subquery` to `queryset` (having table `table`).
    """

    # here you can set complex clause for join
    def extra_join_cond(where_class, alias, related_alias):
        if (alias, related_alias) == ('[sys].[columns]',
                                      '[sys].[database_permissions]'):
            where = '[sys].[columns].[column_id] = ' \
                    '[sys].[database_permissions].[minor_id]'
            children = [ExtraWhere([where], ())]
            return where_class(children)
        return None

    foreign_object = ForeignObject(to=subquery,
                                   from_fields=[None],
                                   to_fields=[None],
                                   rel=None,
                                   on_delete=models.DO_NOTHING)
    foreign_object.opts = Options(table._meta)
    foreign_object.opts.model = table
    foreign_object.get_joining_columns = lambda: (
        (table_field, subquery_field), )
    foreign_object.get_extra_restriction = extra_join_cond
    subquery_sql, subquery_params = subquery.query.sql_with_params()
    join = CustomJoin(subquery_sql, subquery_params, table._meta.db_table,
                      alias, "LEFT JOIN", foreign_object, True)

    queryset.query.join(join)

    # hook for set alias
    join.table_alias = alias
    queryset.query.external_aliases.add(alias)

    return queryset
def join_to_queryset(
        table,
        subquery,
        table_field,
        subquery_field,
        queryset,
        alias,
        is_raw=False,
        extra_restriction_func=lambda where_class, alias, related_alias: None):
    """
    Add a join on `subquery` to `queryset` (having table `table`).
    """
    foreign_object = ForeignObject(to=subquery,
                                   from_fields=[None],
                                   to_fields=[None],
                                   rel=None,
                                   on_delete=models.CASCADE)
    foreign_object.opts = Options(table._meta)
    foreign_object.opts.model = table
    foreign_object.get_joining_columns = lambda: (
        (table_field, subquery_field), )
    foreign_object.get_extra_restriction = extra_restriction_func

    subquery_sql, subquery_params = (
        subquery.query, []) if is_raw else subquery.query.sql_with_params()

    join = CustomJoin(subquery_sql, subquery_params, table._meta.db_table,
                      alias, LOUTER, foreign_object, True)

    queryset.query.join(join)

    # hook for set alias
    join.table_alias = alias
    queryset.query.external_aliases.add(alias)

    return queryset