Skip to content
Open
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
17 changes: 14 additions & 3 deletions db-service/lib/cqn4sql.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,17 @@ function cqn4sql(originalQuery, model, useTechnicalAlias = true) {
const { where, having } = transformSearch(searchTerm)
if (where) inferred.SELECT.where = where
else if (having) inferred.SELECT.having = having
if (searchTerm.func) (inferred.SELECT.orderBy ??= []).unshift({ func: searchTerm.func, args: [...searchTerm.args, true], sort: 'desc' })
else if (searchTerm.xpr) {
const searchSelect = searchTerm.xpr[2]
const searchFunc = searchSelect.SELECT.where[0]
; (inferred.SELECT.orderBy ??= []).unshift({
__proto__: SELECT.from(searchSelect.SELECT.from)
.columns({ func: searchFunc.func, args: [...searchFunc.args, true] })
.where([searchTerm.xpr[0], 'in', { list: searchSelect.SELECT.columns }]), // TODO: <-- ensure that the sub select in the order by is bound to the original query result row
sort: 'desc'
})
}
}
}
// query modifiers can also be defined in from ref leaf infix filter
Expand Down Expand Up @@ -995,11 +1006,11 @@ function cqn4sql(originalQuery, model, useTechnicalAlias = true) {
const keyName = k.as || k.ref.join('_')
const fkName = `${elemName}_${keyName}` // e.g., 'head_id'
const fkFullName = `${columnAlias}_${fkName}` // e.g., 'department_head_id'

// Check if this FK is excluded
if (exclude.some(e => (e.ref?.at(-1) || e.as || e) === fkName)) continue
if (exclude.some(e => (e.ref?.at(-1) || e.as || e) === fkFullName)) continue

const flatColumn = {
ref: [joinAlias, fkName],
as: fkFullName,
Expand All @@ -1017,7 +1028,7 @@ function cqn4sql(originalQuery, model, useTechnicalAlias = true) {
calcElement.as = fullName
}
res.push(calcElement)
}
}
else {
// Scalar element
const flatColumn = {
Expand Down
4 changes: 2 additions & 2 deletions hana/lib/cql-functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ const StandardFunctions = {
* @param {string} arg - Argument object containing search values
* @returns {string} - SQL statement
*/
search: function (ref, arg) {
search: function (ref, arg, numeric) {
if (cds.env.hana.fuzzy === false) {
// Handle non-fuzzy search
arg = arg.xpr ? arg.xpr : arg
Expand Down Expand Up @@ -207,7 +207,7 @@ const StandardFunctions = {
}
}

return `(CASE WHEN SCORE(${arg} IN ${ref}) > 0 THEN TRUE ELSE FALSE END)`
return numeric ? `SCORE(${arg} IN ${ref})` : `(CASE WHEN SCORE(${arg} IN ${ref}) > 0 THEN TRUE ELSE FALSE END)`
},

// ==============================
Expand Down
Loading