Skip to content

Commit

Permalink
fix: consider list in from.where (#429)
Browse files Browse the repository at this point in the history
reported in #428
  • Loading branch information
patricebender authored Jan 31, 2024
1 parent 6509ee5 commit 3288e94
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
6 changes: 3 additions & 3 deletions db-service/lib/infer/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,9 +153,10 @@ function infer(originalQuery, model = cds.context?.model || cds.model) {
* @returns {void} This function does not return a value; it mutates the 'arg' object directly.
*/
function attachRefLinksToArg(arg, $baseLink = null, expandOrExists = false) {
const { ref, xpr, args } = arg
const { ref, xpr, args, list } = arg
if (xpr) xpr.forEach(t => attachRefLinksToArg(t, $baseLink, expandOrExists))
if (args) args.forEach(arg => attachRefLinksToArg(arg, $baseLink, expandOrExists))
if (list) list.forEach(arg => attachRefLinksToArg(arg, $baseLink, expandOrExists))
if (!ref) return
init$refLinks(arg)
ref.forEach((step, i) => {
Expand Down Expand Up @@ -459,12 +460,11 @@ function infer(originalQuery, model = cds.context?.model || cds.model) {

function inferQueryElement(column, insertIntoQueryElements = true, $baseLink = null, context) {
const { inExists, inExpr, inNestedProjection, inCalcElement, baseColumn } = context || {}
if (column.param) return // parameter references are only resolved into values on execution e.g. :val, :1 or ?
if (column.param || column.SELECT) return // parameter references are only resolved into values on execution e.g. :val, :1 or ?
if (column.args) column.args.forEach(arg => inferQueryElement(arg, false, $baseLink, context)) // e.g. function in expression
if (column.list) column.list.forEach(arg => inferQueryElement(arg, false, $baseLink, context))
if (column.xpr)
column.xpr.forEach(token => inferQueryElement(token, false, $baseLink, { ...context, inExpr: true })) // e.g. function in expression
if (column.SELECT) return

if (!column.ref) {
if (column.expand) queryElements[column.as] = resolveExpand(column)
Expand Down
28 changes: 28 additions & 0 deletions db-service/test/cqn4sql/table-alias.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -922,6 +922,34 @@ describe('table alias access', () => {
).to.throw(/"title" not found in "bookshop.Genres"/)
})

it('handles ref in list', () => {
const query = SELECT.from({
ref: [
{
id: 'bookshop.Books',
where: [
{ list: [{ ref: ['dedication', 'addressee', 'ID'] }] },
'in',
CQL`SELECT Books2.ID from bookshop.Books as Books2 where Books2.ID = 5`,
],
},
],
}).columns('ID')

const expected = SELECT.from('bookshop.Books as Books')
.columns('Books.ID')
.where([
{
list: [{ ref: ['Books', 'dedication_addressee_ID'] }],
},
'in',
CQL`SELECT Books2.ID from bookshop.Books as Books2 where Books2.ID = 5`,
])

const res = cqn4sql(query, model)
expect(res).to.deep.equal(expected)
})

it('handles value subquery in WHERE', () => {
let query = cqn4sql(
CQL`SELECT from bookshop.Books { ID }
Expand Down

0 comments on commit 3288e94

Please sign in to comment.