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: rewrite to path expression, if alias for relative is already used in subselect to avoid sql error #125

Merged
merged 7 commits into from
Nov 27, 2024
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
12 changes: 11 additions & 1 deletion lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,17 @@ const _buildSubSelect = (model, { entity, relative, element, next }, row, previo
const targetAlias = _alias(element._target)
const relativeAlias = _alias(relative)

childCqn.where(relative._relations[element.name].join(targetAlias, relativeAlias))
let w = relative._relations[element.name].join(targetAlias, relativeAlias)

// REVISIT: rewrite to path expression, if alias for relative is already used in subselect to avoid sql error
if (previousCqn?._aliases.has(relativeAlias)) {
let t
for (const a in entity.associations) if (entity.associations[a].target === relative.name) t = entity.associations[a]
if (t && w[0]?.xpr) for (const ele of w[0].xpr) if (ele.ref?.[0] === relativeAlias) ele.ref.splice(0, 1, as, t.name)
}
childCqn._aliases = new Set(previousCqn ? [...previousCqn._aliases.values(), as] : [as])

childCqn.where(w)

if (previousCqn) childCqn.where('exists', previousCqn)
else childCqn.where(_addKeysToWhere(keys, row, as))
Expand Down
9 changes: 5 additions & 4 deletions test/personal-data/crud.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ const { POST: _POST, PATCH: _PATCH, GET: _GET, DELETE: _DELETE, data } = cds.tes

// the persistent outbox adds a delay
const wait = require('util').promisify(setTimeout)
const POST = (...args) => _POST(...args).then(async res => (await wait(7), res))
const PATCH = (...args) => _PATCH(...args).then(async res => (await wait(7), res))
const GET = (...args) => _GET(...args).then(async res => (await wait(7), res))
const DELETE = (...args) => _DELETE(...args).then(async res => (await wait(7), res))
const DELAY = process.env.CI ? 42 : 7
const POST = (...args) => _POST(...args).then(async res => (await wait(DELAY), res))
const PATCH = (...args) => _PATCH(...args).then(async res => (await wait(DELAY), res))
const GET = (...args) => _GET(...args).then(async res => (await wait(DELAY), res))
const DELETE = (...args) => _DELETE(...args).then(async res => (await wait(DELAY), res))

// TODO: @cap-js/sqlite doesn't support structured properties
// // needed for testing structured properties
Expand Down