Skip to content

Commit

Permalink
Merge branch 'main' into dynatrace
Browse files Browse the repository at this point in the history
  • Loading branch information
johannes-vogel authored Sep 2, 2024
2 parents 2a5fb6a + 5423cf3 commit 0beca25
Show file tree
Hide file tree
Showing 9 changed files with 102 additions and 23 deletions.
12 changes: 6 additions & 6 deletions db-service/lib/cqn2sql.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ const BINARY_TYPES = {
const { Readable } = require('stream')

const DEBUG = (() => {
let DEBUG = cds.debug('sql-json')
if (DEBUG) return DEBUG
else DEBUG = cds.debug('sql|sqlite')
if (DEBUG) {
return DEBUG
const LOG = cds.log('sql-json')
if (LOG._debug) return cds.debug('sql-json')
return cds.debug('sql|sqlite')
//if (DEBUG) {
// return DEBUG
// (sql, ...more) => DEBUG (sql.replace(/(?:SELECT[\n\r\s]+(json_group_array\()?[\n\r\s]*json_insert\((\n|\r|.)*?\)[\n\r\s]*\)?[\n\r\s]+as[\n\r\s]+_json_[\n\r\s]+FROM[\n\r\s]*\(|\)[\n\r\s]*(\)[\n\r\s]+AS )|\)$)/gim,(a,b,c,d) => d || ''), ...more)
// FIXME: looses closing ) on INSERT queries
}
//}
})()

class CQN2SQLRenderer {
Expand Down
3 changes: 2 additions & 1 deletion db-service/lib/cqn4sql.js
Original file line number Diff line number Diff line change
Expand Up @@ -530,7 +530,7 @@ function cqn4sql(originalQuery, model) {
res = getTransformedTokenStream([value], baseLink)[0]
} else if (xpr) {
res = { xpr: getTransformedTokenStream(value.xpr, baseLink) }
} else if (val) {
} else if (val !== undefined) {
res = { val }
} else if (func) {
res = { args: getTransformedFunctionArgs(value.args, baseLink), func: value.func }
Expand Down Expand Up @@ -884,6 +884,7 @@ function cqn4sql(originalQuery, model) {

if (expand.expand) {
const nested = _subqueryForGroupBy(expand, fullRef, expand.as || expand.ref.map(idOnly).join('_'))
setElementOnColumns(nested, expand.element)
elements[expand.as || expand.ref.map(idOnly).join('_')] = nested
return nested
}
Expand Down
23 changes: 19 additions & 4 deletions hana/lib/HANAService.js
Original file line number Diff line number Diff line change
Expand Up @@ -505,9 +505,18 @@ class HANAService extends SQLService {
if (col.ref?.length > 1) {
const colName = this.column_name(col)
if (col.ref[0] !== parent.as) {
// Inject foreign columns into parent select
// Inject foreign columns into parent selects (recursively)
const as = `$$${col.ref.join('.')}$$`
parent.SELECT.columns.push({ __proto__: col, ref: col.ref, as })
let curPar = parent
while (curPar) {
if (curPar.SELECT.from?.args?.some(a => a.as === col.ref[0])) {
curPar.SELECT.columns.push({ __proto__: col, ref: col.ref, as })
break
} else {
curPar.SELECT.columns.push({ __proto__: col, ref: [curPar.SELECT.parent.as, as], as })
curPar = curPar.SELECT.parent
}
}
col.as = colName
col.ref = [parent.as, as]
} else if (!parent.SELECT.columns.some(c => this.column_name(c) === colName)) {
Expand Down Expand Up @@ -557,8 +566,6 @@ class HANAService extends SQLService {
)
.filter(a => a)

if (sql.length === 0) sql = '*'

if (SELECT.expand === 'root') {
this._blobs = blobs
const blobColumns = Object.keys(blobs)
Expand Down Expand Up @@ -977,6 +984,13 @@ class HANAService extends SQLService {
this.values.push(JSON.stringify(list.list.map(l => l.list.reduce((l, c, i) => { l[`V${i}`] = c.val; return l }, {}))))
return `(SELECT * FROM JSON_TABLE(?, '$' COLUMNS(${extraction})))`
}
// If the list only contains of vals it is replaced with a json function and a placeholder
if (this.values && first.val) {
const v = first
const extraction = `"val" ${this.constructor.InsertTypeMap[typeof v.val]()} PATH '$.val'`
this.values.push(JSON.stringify(list.list))
return `(SELECT * FROM JSON_TABLE(?, '$' COLUMNS(${extraction})))`
}
// Call super for normal SQL behavior
return super.list(list)
}
Expand Down Expand Up @@ -1124,6 +1138,7 @@ class HANAService extends SQLService {

static OutputConverters = {
...super.OutputConverters,
LargeString: cds.env.features.sql_simple_queries > 0 ? e => `TO_NVARCHAR(${e})` : undefined,
// REVISIT: binaries should use BASE64_ENCODE, but this results in BASE64_ENCODE(BINTONHEX(${e}))
Binary: e => `BINTONHEX(${e})`,
Date: e => `to_char(${e}, 'YYYY-MM-DD')`,
Expand Down
24 changes: 12 additions & 12 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 30 additions & 0 deletions test/compliance/resources/db/complex/computed.cds
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
namespace complex.computed;

entity static {
value : Integer;
integer : Integer = 1;
double : Double = 0.1;
string : String = '';
}

entity dynamic {
integer : Integer;
@(Core.Computed: true,readonly)
![case] : String = (
case
when
integer = 0
then
'zero'
when
integer = 1
then
'one'
when
integer = 2
then
'two'
end
);
lambda : String = (integer = 0 ? 'none' : 'some')
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module.exports = [
{
integer: 0,
'=case': 'zero',
'=lambda': 'none',
},
{
integer: 1,
'=case': 'one',
'=lambda': 'some',
},
{
integer: 2,
'=case': 'two',
'=lambda': 'some',
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports = [
{
'=integer': 1,
'=double': 0.1,
'=string': '',
}
]
1 change: 1 addition & 0 deletions test/compliance/resources/db/complex/index.cds
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
namespace complex;

using from './computed';
using from './associations';
using from './associationsUnmanaged';
8 changes: 8 additions & 0 deletions test/scenarios/bookshop/read.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,14 @@ describe('Bookshop - Read', () => {
).to.be.true
})

test('same as above, with more depth', async () => {
const res = await GET(
'/admin/Books?$apply=filter(title%20ne%20%27bar%27)/groupby((genre/parent/name),aggregate(price with sum as totalAmount))',
admin,
)
expect(res.data.value[0].genre.parent.name).to.be.eq('Fiction')
})

test('Path expression', async () => {
const q = CQL`SELECT title, author.name as author FROM sap.capire.bookshop.Books where author.name LIKE '%a%'`
const res = await cds.run(q)
Expand Down

0 comments on commit 0beca25

Please sign in to comment.