Skip to content

Commit

Permalink
refactor(infer): consolidate two main functions (#912)
Browse files Browse the repository at this point in the history
Rework #617

when the `infer` function was developed, it grew quite fast and some
functions were tailor made for very specific use cases. While separation
of concerns is a good practice w.r.t. maintainability, the wheel must
not be re-invented for each minor deviation in behavior.

That being said, the two function `inferQueryElement` and
`attachRefLinksToArg` are doing essentially the same. They both walk an
`arg` which is - generally spoken - an expression. That might be a
simple `ref`, `func`, `val` or a (nested) `xpr`.

- `inferQueryElement` was used for the `columns` of a query. Each column
was resolved to a query element. All elements together formed the set of
query elements. The other main part of the function is the attachment of
the infamous `$refLinks` next to each `ref` array. Moreover, there was a
flag `insertIntoQueryElements` which could be set to `false`, if the
`arg` must _not_ be inserted into the queries elements. This is the case
for nested `xpr`, the `where`, `having`, `with`, `groupBy` and `orderBy`
clause of the query.
- `attachRefLinksToArg` does exactly what the name implies, it attaches
`$refLinks` to an `arg` but does not resolve the `arg` to a query
element. This function was used for the `from` clause of the query.

It is obvious that both functions have significant shared logic. This
change removes the usage of the `attachRefLinksToArg` function.

- [x] stakeholder tests are all green with this branch →
cap/stakeholder-tests/actions/runs/9482613
- [x] cds tests are all green with this branch →
/cap/cds/actions/runs/9483245
  • Loading branch information
patricebender authored Nov 28, 2024
1 parent 7d48c13 commit 0562858
Show file tree
Hide file tree
Showing 3 changed files with 624 additions and 726 deletions.
6 changes: 4 additions & 2 deletions db-service/lib/cqn4sql.js
Original file line number Diff line number Diff line change
Expand Up @@ -1404,7 +1404,8 @@ function cqn4sql(originalQuery, model) {
if (list.every(e => e.val))
// no need for transformation
transformedTokenStream.push({ list })
else transformedTokenStream.push({ list: getTransformedTokenStream(list, $baseLink) })
else
transformedTokenStream.push({ list: getTransformedTokenStream(list, $baseLink) })
}
} else if (tokenStream.length === 1 && token.val && $baseLink) {
// infix filter - OData variant w/o mentioning key --> flatten out and compare each leaf to token.val
Expand Down Expand Up @@ -1726,7 +1727,8 @@ function cqn4sql(originalQuery, model) {
transformedFrom.$refLinks.splice(0, transformedFrom.$refLinks.length - 1)

let args = from.ref.at(-1).args
const subquerySource = transformedFrom.$refLinks[0].target
const subquerySource =
getDefinition(transformedFrom.$refLinks[0].definition.target) || transformedFrom.$refLinks[0].target
if (subquerySource.params && !args) args = {}
const id = localized(subquerySource)
transformedFrom.ref = [args ? { id, args } : id]
Expand Down
Loading

0 comments on commit 0562858

Please sign in to comment.