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

wip: attach cardinality to join nodes #963

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
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
6 changes: 5 additions & 1 deletion db-service/lib/cqn4sql.js
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,11 @@ function cqn4sql(originalQuery, model) {
lhs = joinForBranch(lhs, c)
})
}
return lhs.args.length > 1 ? lhs : lhs.args[0]
const res = lhs.args.length > 1 ? lhs : lhs.args[0]
// default is to-one
if(nextAssoc && nextAssoc.$refLink.definition)
Object.defineProperty(res, 'element', { value: nextAssoc.$refLink.definition, /* why do we need this */writable: true })
return res
}
}

Expand Down
9 changes: 9 additions & 0 deletions db-service/test/cqn4sql/assocs2joins.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,15 @@ describe('Unfolding Association Path Expressions to Joins', () => {
`
expect(query).to.deep.equal(expected)
})
it('in select, one assoc to many', () => {
let query = cqn4sql(CQL`SELECT from bookshop.Authors { ID, name } where books.title LIKE '%Potter%'`, model)
const expected = CQL`SELECT from bookshop.Authors as Authors
left outer join bookshop.Books as books on books.author_ID = Authors.ID
{ Authors.ID, Authors.name }
where books.title LIKE '%Potter%'
`
expect(query).to.deep.equal(expected)
})

it('in select, one deep assoc, with filter', () => {
let query = cqn4sql(CQL`SELECT from bookshop.Books { ID, dedication.addressee[name = 'Hasso'].name }`, model)
Expand Down
11 changes: 11 additions & 0 deletions hana/lib/HANAService.js
Original file line number Diff line number Diff line change
Expand Up @@ -686,6 +686,17 @@ class HANAService extends SQLService {
return sql
}

from(from, q) {
const { element, ref, SELECT } = from
if (!element?.cardinality || ref || SELECT) return super.from(from, q)
const { type, cardinality: { min, max } } = element
const cardinality =
(type === 'cds.Composition' ? 'EXACT ONE' : 'MANY')
+ ' TO '
+ (max == 1 ? min == 1 ? 'EXACT ONE' : 'ONE' : 'MANY')
if (from.join) return `${this.from(from.args[0])} ${from.join} ${cardinality} JOIN ${this.from(from.args[1])}${from.on ? ` ON ${this.where(from.on)}` : ''}`
}

from_dummy() {
return ' FROM DUMMY'
}
Expand Down
Loading