Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 | 131x 131x 679x 131x 348x 157x 174x 140x 140x 157x 157x 157x 140x 140x 17x 17x 17x 17x 157x 40x 40x 157x 157x 157x 157x 157x 157x 174x 139x 139x | import type { ModelDefinition } from '../model/model-definition.ts';
import { AttributeColumns } from '../model/attribute-columns.ts';
import { SqlAst, type ExprNode, type JoinNode } from '../sql/ast.ts';
import type { IncludeNode } from './include.ts';
const ast = new SqlAst();
const attributeColumns = new AttributeColumns();
type Through = NonNullable<IncludeNode['through']>;
const defined = (part: ExprNode | undefined): part is ExprNode => part !== undefined;
/** Column name of an attribute (the attribute name when it has no explicit field). */
const field = (definition: ModelDefinition, name: string): string =>
definition.attributes.get(name)?.field ?? name;
/** Builds the JOIN nodes of an include and its nested includes. */
export class IncludeJoins {
/**
* Joins of an include: the target table, or junction then target for belongsToMany, followed by
* the joins of the nested (non-separate) includes.
*
* @param parent - Definition of the parent model.
* @param parentAlias - Table alias of the parent.
* @param node - Include node.
* @returns Join nodes.
*/
joins(parent: ModelDefinition, parentAlias: string, node: IncludeNode): readonly JoinNode[] {
return node.through === undefined
? this.#direct(parent, parentAlias, node)
: this.#viaJunction(parent, parentAlias, node, node.through);
}
#type(node: IncludeNode): JoinNode['type'] {
return node.right ? 'RIGHT OUTER' : node.required ? 'INNER' : 'LEFT OUTER';
}
#direct(parent: ModelDefinition, parentAlias: string, node: IncludeNode): readonly JoinNode[] {
const join: JoinNode = {
type: this.#type(node),
table: attributeColumns.table(node.definition, node.alias),
on: ast.and([this.#joinKeys(parent, parentAlias, node), ...this.#extra(node)]),
};
return this.#withChildren(node, join, []);
}
// v6 generateInclude: a required child of an optional parent goes inside the parent's parenthesised
// join, so that rows without the parent are kept instead of being dropped by a top-level INNER JOIN.
#withChildren(
node: IncludeNode,
join: JoinNode,
before: readonly JoinNode[],
): readonly JoinNode[] {
const children = this.#childJoins(node);
const nest = !node.required && node.children.some((child) => !child.separate && child.required);
return nest ? [{ ...join, nested: [...before, ...children] }] : [join, ...before, ...children];
}
#joinKeys(parent: ModelDefinition, parentAlias: string, node: IncludeNode): ExprNode {
const custom = attributeColumns.where(
node.on,
attributeColumns.whereCompiler(node.definition, node.alias),
);
return custom ?? this.#onClause(parent, parentAlias, node);
}
#viaJunction(
parent: ModelDefinition,
parentAlias: string,
node: IncludeNode,
through: Through,
): readonly JoinNode[] {
const junction: JoinNode = {
type: this.#type(node),
table: attributeColumns.table(through.definition, through.alias),
on: ast.and(this.#junctionCondition(parent, parentAlias, node, through)),
};
return this.#withChildren(node, junction, [this.#targetJoin(node, through)]);
}
#targetJoin(node: IncludeNode, through: Through): JoinNode {
return {
type: this.#type(node),
table: attributeColumns.table(node.definition, node.alias),
on: ast.and([
ast.binary(
'=',
ast.ident(node.alias, field(node.definition, node.association.targetKey)),
ast.ident(through.alias, field(through.definition, node.association.otherKey ?? '')),
),
...this.#extra(node),
]),
};
}
#junctionCondition(
parent: ModelDefinition,
parentAlias: string,
node: IncludeNode,
through: Through,
): readonly ExprNode[] {
return [
ast.binary(
'=',
ast.ident(parentAlias, field(parent, node.association.sourceKey)),
ast.ident(through.alias, field(through.definition, node.association.foreignKey)),
),
attributeColumns.where(
through.where,
attributeColumns.whereCompiler(through.definition, through.alias),
),
this.#scopeCondition(through.definition, through.alias, node.association.through?.scope),
].filter(defined);
}
#childJoins(node: IncludeNode): readonly JoinNode[] {
return node.children
.filter((child) => !child.separate)
.flatMap((child) => this.joins(node.definition, node.alias, child));
}
#extra(node: IncludeNode): readonly ExprNode[] {
return [
this.#condition(node.definition, node.alias, node.where, node.paranoid),
this.#scopeCondition(node.definition, node.alias, node.association.scope),
].filter(defined);
}
#condition(
definition: ModelDefinition,
alias: string,
where: unknown,
paranoid: boolean,
): ExprNode | undefined {
const condition = attributeColumns.where(
where,
attributeColumns.whereCompiler(definition, alias),
);
const notDeleted = paranoid ? this.#notDeleted(definition, alias) : undefined;
const parts = [condition, notDeleted].filter(defined);
return parts.length === 0 ? undefined : ast.and(parts);
}
#notDeleted(definition: ModelDefinition, alias: string): ExprNode | undefined {
return definition.deletedAt === undefined
? undefined
: ({
kind: 'postfix',
op: 'IS NULL',
expr: ast.ident(alias, field(definition, definition.deletedAt)),
} as const);
}
#scopeCondition(
definition: ModelDefinition,
alias: string,
scope: Readonly<Record<string, unknown>> | undefined,
): ExprNode | undefined {
return scope === undefined
? undefined
: attributeColumns.where(scope, attributeColumns.whereCompiler(definition, alias));
}
#onClause(parent: ModelDefinition, parentAlias: string, node: IncludeNode): ExprNode {
const { association } = node;
return association.associationType === 'BelongsTo'
? ast.binary(
'=',
ast.ident(parentAlias, field(parent, association.foreignKey)),
ast.ident(node.alias, field(node.definition, association.targetKey)),
)
: ast.binary(
'=',
ast.ident(parentAlias, field(parent, association.sourceKey)),
ast.ident(node.alias, field(node.definition, association.foreignKey)),
);
}
}
|