Skip to content

Commit

Permalink
fix(localized): refs in subqueries in from are translated (#366)
Browse files Browse the repository at this point in the history
if the main queries target is `localized` and this is not overwritten by
`cds.localized: false`, `ref`erences in subqueries will also be
translated. This was already the case for subqueries in columns or
expands. The responsible code has been moved to the central entry point
for all subqueries, hence this now also covers subqueries in `from`.
  • Loading branch information
patricebender authored Dec 1, 2023
1 parent 22f0aea commit cfe4897
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
3 changes: 1 addition & 2 deletions db-service/lib/cqn4sql.js
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,6 @@ function cqn4sql(originalQuery, model = cds.context?.model || cds.model) {
return transformedColumns

function handleSubquery(col) {
if (isLocalized(inferred.target)) col.SELECT.localized = true
if (!col.SELECT.from.as) {
const uniqueSubqueryAlias = inferred.joinTree.addNextAvailableTableAlias(
getLastStringSegment(col.SELECT.from.ref[col.SELECT.from.ref.length - 1]),
Expand Down Expand Up @@ -830,7 +829,6 @@ function cqn4sql(originalQuery, model = cds.context?.model || cds.model) {
one: column.$refLinks[column.$refLinks.length - 1].definition.is2one,
},
}
if (isLocalized(inferred.target)) subquery.SELECT.localized = true
const expanded = transformSubquery(subquery)
const correlated = _correlate({ ...expanded, as: columnAlias }, outerAlias)
Object.defineProperty(correlated, 'elements', { value: subquery.elements, writable: true })
Expand Down Expand Up @@ -946,6 +944,7 @@ function cqn4sql(originalQuery, model = cds.context?.model || cds.model) {
outerQueries.push(inferred)
Object.defineProperty(q, 'outerQueries', { value: outerQueries })
}
if (isLocalized(inferred.target)) q.SELECT.localized = true
return cqn4sql(q, model)
}

Expand Down
14 changes: 13 additions & 1 deletion db-service/test/cqn4sql/localized.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@ describe('localized', () => {
BPLocalized.title,
}`)
})
// TODO dont shadow query alias
it('performs simple replacement of ref within subquery', () => {
const q = CQL`SELECT from bookshop.Books {ID, title, (SELECT title from bookshop.Books) as foo}`
q.SELECT.localized = true
Expand All @@ -105,6 +104,19 @@ describe('localized', () => {
(SELECT Books2.title from localized.bookshop.Books as Books2) as foo
}`)
})
it('performs simple replacement of ref within subquery in from', () => {
const q = CQL`SELECT from (SELECT Books.title from bookshop.Books) as foo { foo.title }`
q.SELECT.localized = true
let query = cqn4sql(q, model)
expect(JSON.parse(JSON.stringify(query))).to.deep.equal(CQL`
SELECT from (
SELECT Books.title from localized.bookshop.Books as Books
) as foo
{
foo.title,
}`
)
})
it('performs no replacement of ref within subquery if main query has ”@cds.localized: false”', () => {
const q = CQL`SELECT from bookshop.BP {ID, title, (SELECT title from bookshop.Books) as foo}`
q.SELECT.localized = true
Expand Down

0 comments on commit cfe4897

Please sign in to comment.