MCPcopy
hub / github.com/sequelize/sequelize / generateJoin

Method generateJoin

lib/dialects/abstract/query-generator.js:1696–1791  ·  view source on GitHub ↗
(include, topLevelInfo)

Source from the content-addressed store, hash-verified

1694 }
1695
1696 generateJoin(include, topLevelInfo) {
1697 const association = include.association;
1698 const parent = include.parent;
1699 const parentIsTop = !!parent && !include.parent.association && include.parent.model.name === topLevelInfo.options.model.name;
1700 let $parent;
1701 let joinWhere;
1702 /* Attributes for the left side */
1703 const left = association.source;
1704 const attrLeft = association instanceof BelongsTo ?
1705 association.identifier :
1706 association.sourceKeyAttribute || left.primaryKeyAttribute;
1707 const fieldLeft = association instanceof BelongsTo ?
1708 association.identifierField :
1709 left.rawAttributes[association.sourceKeyAttribute || left.primaryKeyAttribute].field;
1710 let asLeft;
1711 /* Attributes for the right side */
1712 const right = include.model;
1713 const tableRight = right.getTableName();
1714 const fieldRight = association instanceof BelongsTo ?
1715 right.rawAttributes[association.targetIdentifier || right.primaryKeyAttribute].field :
1716 association.identifierField;
1717 let asRight = include.as;
1718
1719 while (($parent = $parent && $parent.parent || include.parent) && $parent.association) {
1720 if (asLeft) {
1721 asLeft = `${$parent.as}->${asLeft}`;
1722 } else {
1723 asLeft = $parent.as;
1724 }
1725 }
1726
1727 if (!asLeft) asLeft = parent.as || parent.model.name;
1728 else asRight = `${asLeft}->${asRight}`;
1729
1730 let joinOn = `${this.quoteTable(asLeft)}.${this.quoteIdentifier(fieldLeft)}`;
1731 const subqueryAttributes = [];
1732
1733 if (topLevelInfo.options.groupedLimit && parentIsTop || topLevelInfo.subQuery && include.parent.subQuery && !include.subQuery) {
1734 if (parentIsTop) {
1735 // The main model attributes is not aliased to a prefix
1736 const tableName = this.quoteTable(parent.as || parent.model.name);
1737
1738 // Check for potential aliased JOIN condition
1739 joinOn = this._getAliasForField(tableName, attrLeft, topLevelInfo.options) || `${tableName}.${this.quoteIdentifier(attrLeft)}`;
1740
1741 if (topLevelInfo.subQuery) {
1742 const dbIdentifier = `${tableName}.${this.quoteIdentifier(fieldLeft)}`;
1743 subqueryAttributes.push(dbIdentifier !== joinOn ? `${dbIdentifier} AS ${this.quoteIdentifier(attrLeft)}` : dbIdentifier);
1744 }
1745 } else {
1746 const joinSource = `${asLeft.replace(/->/g, '.')}.${attrLeft}`;
1747
1748 // Check for potential aliased JOIN condition
1749 joinOn = this._getAliasForField(asLeft, joinSource, topLevelInfo.options) || this.quoteIdentifier(joinSource);
1750 }
1751 }
1752
1753 joinOn += ` = ${this.quoteIdentifier(asRight)}.${this.quoteIdentifier(fieldRight)}`;

Callers 2

generateIncludeMethod · 0.95
testsqlFunction · 0.80

Calls 7

quoteTableMethod · 0.95
quoteIdentifierMethod · 0.95
_getAliasForFieldMethod · 0.95
whereItemsQueryMethod · 0.95
getTableNameMethod · 0.80
literalMethod · 0.80
setMethod · 0.65

Tested by 1

testsqlFunction · 0.64