diff --git a/db-service/lib/cqn4sql.js b/db-service/lib/cqn4sql.js index 21e929ac9..f3d92fc10 100644 --- a/db-service/lib/cqn4sql.js +++ b/db-service/lib/cqn4sql.js @@ -132,15 +132,12 @@ function cqn4sql(originalQuery, model) { const queryTarget = Object.values(inferred.sources)[0].definition const keys = Object.values(queryTarget.elements).filter(e => e.key === true) const primaryKey = { list: [] } - keys - .filter(k => !k.virtual) // e.g. draft column `isActiveEntity` is virtual and key - .forEach(k => { - // cqn4sql will add the table alias to the column later, no need to add it here - subquery.SELECT.columns.push({ ref: [k.name] }) - - // add the alias of the main query to the list of primary key references - primaryKey.list.push({ ref: [transformedFrom.as, k.name] }) - }) + for (const k of keys) { + if (!k.virtual) { + subquery.SELECT.columns.push({ ref: [k.name] }) + primaryKey.list.push({ ref: [transformedFrom.as, k.name] }) + } + } const transformedSubquery = cqn4sql(subquery, model) @@ -1069,13 +1066,12 @@ function cqn4sql(originalQuery, model) { */ function getColumnsForWildcard(exclude = [], replace = [], baseName = null) { const wildcardColumns = [] - Object.keys(inferred.$combinedElements) - .filter(k => !exclude.includes(k)) - .forEach(k => { + for (const k of Object.keys(inferred.$combinedElements)) { + if (!exclude.includes(k)) { const { index, tableAlias } = inferred.$combinedElements[k][0] const element = tableAlias.elements[k] // ignore FK for odata csn / ignore blobs from wildcard expansion - if (isManagedAssocInFlatMode(element) || element.type === 'cds.LargeBinary') return + if (isManagedAssocInFlatMode(element) || element.type === 'cds.LargeBinary') continue // for wildcard on subquery in from, just reference the elements if (tableAlias.SELECT && !element.elements && !element.target) { wildcardColumns.push(index ? { ref: [index, k] } : { ref: [k] }) @@ -1091,7 +1087,8 @@ function cqn4sql(originalQuery, model) { ) wildcardColumns.push(...flatColumns) } - }) + } + } return wildcardColumns /** @@ -1421,12 +1418,12 @@ function cqn4sql(originalQuery, model) { const keys = def.keys // use key aspect on entity const keyValComparisons = [] const flatKeys = [] - Object.values(keys) - // up__ID already part of inner where exists, no need to add it explicitly here - .filter(k => k !== backlinkFor($baseLink.definition)?.[0]) - .forEach(v => { + for (const v of Object.values(keys)) { + if (v !== backlinkFor($baseLink.definition)?.[0]) { + // up__ID already part of inner where exists, no need to add it explicitly here flatKeys.push(...getFlatColumnsFor(v, { tableAlias: $baseLink.alias })) - }) + } + } if (flatKeys.length > 1) throw new Error('Filters can only be applied to managed associations which result in a single foreign key') flatKeys.forEach(c => keyValComparisons.push([...[c, '=', token]])) @@ -1586,10 +1583,7 @@ function cqn4sql(originalQuery, model) { if (!def.$refLinks) return def const leaf = def.$refLinks[def.$refLinks.length - 1] const first = def.$refLinks[0] - const tableAlias = getTableAlias( - def, - def.ref.length > 1 && first.definition.isAssociation ? first : $baseLink, - ) + const tableAlias = getTableAlias(def, def.ref.length > 1 && first.definition.isAssociation ? first : $baseLink) if (leaf.definition.parent.kind !== 'entity') // we need the base name return getFlatColumnsFor(leaf.definition, { @@ -1855,7 +1849,7 @@ function cqn4sql(originalQuery, model) { result[i] = asXpr(xpr) continue } - if(lhs.args) { + if (lhs.args) { const args = calculateOnCondition(lhs.args) result[i] = { ...lhs, args } continue diff --git a/db-service/lib/infer/index.js b/db-service/lib/infer/index.js index 704099822..c02ac1496 100644 --- a/db-service/lib/infer/index.js +++ b/db-service/lib/infer/index.js @@ -170,7 +170,8 @@ function infer(originalQuery, model) { if (list) list.forEach(arg => attachRefLinksToArg(arg, $baseLink, expandOrExists)) if (!ref) return init$refLinks(arg) - ref.forEach((step, i) => { + let i = 0 + for (const step of ref) { const id = step.id || step if (i === 0) { // infix filter never have table alias @@ -231,7 +232,8 @@ function infer(originalQuery, model) { step.where.forEach(walkTokenStream) } else throw new Error('A filter can only be provided when navigating along associations') } - }) + i += 1 + } const { definition, target } = arg.$refLinks[arg.$refLinks.length - 1] if (definition.value) { // nested calculated element @@ -1046,15 +1048,17 @@ function infer(originalQuery, model) { if (Object.keys(queryElements).length === 0 && aliases.length === 1) { const { elements } = getDefinitionFromSources(sources, aliases[0]) // only one query source and no overwritten columns - Object.keys(elements) - .filter(k => !exclude(k)) - .forEach(k => { + for (const k of Object.keys(elements)) { + if (!exclude(k)) { const element = elements[k] - if (element.type !== 'cds.LargeBinary') queryElements[k] = element + if (element.type !== 'cds.LargeBinary') { + queryElements[k] = element + } if (element.value) { linkCalculatedElement(element) } - }) + } + } return }