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: joins without columns are rejected #535

Merged
merged 8 commits into from
Mar 20, 2024
Merged
Show file tree
Hide file tree
Changes from 5 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
5 changes: 5 additions & 0 deletions db-service/lib/cqn2sql.js
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,11 @@ class CQN2SQLRenderer {
SELECT(q) {
let { from, expand, where, groupBy, having, orderBy, limit, one, distinct, localized, forUpdate, forShareLock } =
q.SELECT

if (from?.join && !q.SELECT.columns) {
throw new Error('CQN query using joins must specify the selected columns.')
}

// REVISIT: When selecting from an entity that is not in the model the from.where are not normalized (as cqn4sql is skipped)
if (!where && from?.ref?.length === 1 && from.ref[0]?.where) where = from.ref[0]?.where
let columns = this.SELECT_columns(q)
Expand Down
4 changes: 4 additions & 0 deletions hana/lib/HANAService.js
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,10 @@ class HANAService extends SQLService {
this.temporary = this.temporary || []
this.temporaryValues = this.temporaryValues || []

if (q.SELECT.from?.join && !q.SELECT.columns) {
throw new Error('CQN query using joins must specify the selected columns.')
}

const { limit, one, orderBy, expand, columns = ['*'], localized, count, parent } = q.SELECT

const walkAlias = q => {
Expand Down
34 changes: 34 additions & 0 deletions test/scenarios/bookshop/read.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,40 @@ describe('Bookshop - Read', () => {
expect((await cds.db.run(query)).count).to.be.eq(2)
})

it('joins without columns are rejected because of conflicts', async () => {
const query = {
SELECT: {
from: {
join: 'inner',
args: [
{ ref: ['sap.capire.bookshop.Books'], as: 'b' },
{ ref: ['sap.capire.bookshop.Authors'], as: 'a' },
],
on: [{ ref: ['a', 'ID'] }, '=', { ref: ['b', 'author_ID'] }],
},
},
}

expect(cds.db.run(query)).to.be.rejectedWith(/Ambiguous wildcard elements/)
})

it('joins without columns are rejected in general', async () => {
const query = {
SELECT: {
from: {
join: 'inner',
args: [
{ ref: ['AdminService.RenameKeys'], as: 'rk' },
{ ref: ['DraftService.DraftEnabledBooks'], as: 'deb' },
],
on: [{ ref: ['deb', 'ID'] }, '=', { ref: ['rk', 'foo'] }],
},
},
}

expect(cds.db.run(query)).to.be.rejectedWith(/joins must specify the selected columns/)
})

test('Delete Book', async () => {
const res = await DELETE('/admin/Books(271)', admin)
expect(res.status).to.be.eq(204)
Expand Down