Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: reject virtual elements in all expressions #972

Merged
merged 2 commits into from
Jan 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions db-service/lib/cqn4sql.js
Original file line number Diff line number Diff line change
Expand Up @@ -1450,7 +1450,7 @@ function cqn4sql(originalQuery, model) {
transformedTokenStream.push({ ...token })
} else {
// expand `struct = null | struct2`
const { definition } = token.$refLinks?.[token.$refLinks.length - 1] || {}
const definition = token.$refLinks?.at(-1).definition
const next = tokenStream[i + 1]
if (allOps.some(([firstOp]) => firstOp === next) && (definition?.elements || definition?.keys)) {
const ops = [next]
Expand All @@ -1477,6 +1477,9 @@ function cqn4sql(originalQuery, model) {
} else {
// reject associations in expression, except if we are in an infix filter -> $baseLink is set
assertNoStructInXpr(token, $baseLink)
// reject virtual elements in expressions as they will lead to a sql error down the line
if(definition?.virtual)
throw new Error(`Virtual elements are not allowed in expressions`)

let result = is_regexp(token?.val) ? token : copy(token) // REVISIT: too expensive! //
if (token.ref) {
Expand Down Expand Up @@ -1620,10 +1623,10 @@ function cqn4sql(originalQuery, model) {
rejectStructInExpression()

function rejectAssocInExpression() {
throw new Error("An association can't be used as a value in an expression")
throw new Error(`An association can't be used as a value in an expression`)
}
function rejectStructInExpression() {
throw new Error("A structured element can't be used as a value in an expression")
throw new Error(`A structured element can't be used as a value in an expression`)
}
}

Expand Down
61 changes: 18 additions & 43 deletions db-service/test/cqn4sql/not-persisted.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,20 +86,6 @@ describe('not persisted', () => {
order by ID`)
})

it('dont remove in xpr in ORDER BY', () => {
let query = cqn4sql(
CQL`SELECT from bookshop.Foo { ID, virtualField as x }
order by ID, x, (Foo.toFoo.virtualField * 42)`,
model,
)
expect(query).to.deep.equal(
CQL`SELECT from bookshop.Foo as Foo left join bookshop.Foo as toFoo on toFoo.ID = Foo.toFoo_ID
{
Foo.ID
}
order by ID, (toFoo.virtualField * 42)`,
)
})

it('Navigation to virtual field does not cause join', () => {
let query = cqn4sql(
Expand All @@ -117,41 +103,30 @@ describe('not persisted', () => {
// Virtual fields in expressions are left untouched and will cause an error in the DB.
// The idea to replace conditions involving virtual fields by "1=1" doesn't work, as we
// are not able to detect where the conditions start/end (-> we don't understand xpr)
it('leave untouched in expressions', () => {
let query = cqn4sql(
CQL`SELECT from bookshop.Foo {
ID,
virtualField - 2 * stru.v + stru.nested.nv as c
} where virtualField = 2 * stru.v + stru.nested.nv and virtualField`,
model,
)
expect(query).to.deep.equal(CQL`SELECT from bookshop.Foo as Foo {
Foo.ID,
Foo.virtualField - 2 * Foo.stru_v + Foo.stru_nested_nv as c
} where Foo.virtualField = 2 * Foo.stru_v + Foo.stru_nested_nv and Foo.virtualField`)
it('reject virtual elements in expressions', () => {
let query = CQL`SELECT from bookshop.Foo {
ID
} where virtualField = 2 * stru.v + stru.nested.nv and virtualField`;
expect(() => cqn4sql(query, model)).to.throw('Virtual elements are not allowed in expressions')
})

it('Navigation to virtual field does cause join in expression', () => {
let query = cqn4sql(
CQL`SELECT from bookshop.Foo {
it('reject virtual elements in column expressions', () => {
let query = CQL`SELECT from bookshop.Foo {
ID,
toFoo.virtualField + 42 / 20 as virtualField,
}`,
model,
)
expect(query).to.deep.equal(CQL`SELECT from bookshop.Foo as Foo
left join bookshop.Foo as toFoo on toFoo.ID = Foo.toFoo_ID
{
Foo.ID,
toFoo.virtualField + 42 / 20 as virtualField
}`)
}`
expect(() => cqn4sql(query, model)).to.throw('Virtual elements are not allowed in expressions')
})

it('leave untouched also in simple conditions', () => {
let query = cqn4sql(CQL`SELECT from bookshop.Foo { ID } where ID = 5 and virtualField = 6`, model)
expect(query).to.deep.equal(
CQL`SELECT from bookshop.Foo as Foo { Foo.ID } where Foo.ID = 5 and Foo.virtualField = 6`,
)
it('reject virtual elements in simple conditions', () => {
let query = CQL`SELECT from bookshop.Foo { ID } where ID = 5 and virtualField = 6`
expect(() => cqn4sql(query, model)).to.throw('Virtual elements are not allowed in expressions')
})

it('reject virtual elements in order by', () => {
let query = CQL`SELECT from bookshop.Foo { ID, virtualField as x }
order by ID, x, (Foo.toFoo.virtualField * 42)`
expect(() => cqn4sql(query, model)).to.throw('Virtual elements are not allowed in expressions')
})
})

Expand Down
Loading