Skip to content

Commit

Permalink
fix: add placeholder for string values (#733)
Browse files Browse the repository at this point in the history
Fixes : cap/issues/issues/16220
  • Loading branch information
mariayord authored Jul 15, 2024
1 parent 8b19be3 commit 8136a45
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 6 deletions.
4 changes: 2 additions & 2 deletions db-service/lib/cqn2sql.js
Original file line number Diff line number Diff line change
Expand Up @@ -311,8 +311,8 @@ class CQN2SQLRenderer {
*/
column_expr(x, q) {
if (x === '*') return '*'
let sql = this.expr({ param: false, __proto__: x })

let sql = x.param !== true && typeof x.val === 'number' ? this.expr({ param: false, __proto__: x }): this.expr(x)
let alias = this.column_alias4(x, q)
if (alias) sql += ' as ' + this.quote(alias)
return sql
Expand Down
22 changes: 20 additions & 2 deletions db-service/test/cqn2sql/__snapshots__/select.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,9 @@ exports[`cqn2sql quoted column aliases select with subselect in exists and colum

exports[`cqn2sql quoted column aliases select with subselect with in and column aliases 1`] = `
{
"sql": "SELECT Foo.a as A,'abc' as ABC,Foo.x + ? as Xpr1 FROM Foo as Foo WHERE ( Foo.x + ? ) < ? AND Foo.x IN (SELECT Foo2.a as B,Foo2.x - ? as Xpr2 FROM Foo as Foo2 WHERE Foo2.x < ?)",
"sql": "SELECT Foo.a as A,? as ABC,Foo.x + ? as Xpr1 FROM Foo as Foo WHERE ( Foo.x + ? ) < ? AND Foo.x IN (SELECT Foo2.a as B,Foo2.x - ? as Xpr2 FROM Foo as Foo2 WHERE Foo2.x < ?)",
"values": [
"abc",
1,
1,
9,
Expand All @@ -167,12 +168,29 @@ exports[`cqn2sql quoted column aliases select with subselect with in and column

exports[`cqn2sql quoted column aliases simple select with column aliases 1`] = `
{
"sql": "SELECT T.a as A,true as True,false as False,NULL as Null,count(*) as CountFunc FROM Foo as T",
"sql": "SELECT T.a as A,true as True,false as False,? as Null,count(*) as CountFunc FROM Foo as T",
"values": [
null,
],
}
`;

exports[`cqn2sql selection of columns of one table select distinct 1`] = `"SELECT DISTINCT Foo.a,Foo.b,Foo.c FROM Foo as Foo"`;

exports[`cqn2sql selection of columns of one table select with static values 1`] = `
{
"sql": "SELECT 5 as ID,? as a,3.14 as pi,cast(3.1415 as DECIMAL) as pid,cast(? as NCLOB) as stringl,cast(true as BOOLEAN) as boolt,cast(? as DATE) as date,cast(? as TIME) as time,cast(? as DATETIME) as datetime,cast(? as TIMESTAMP) as timestamp FROM Foo as Foo",
"values": [
"simple string",
"large string",
"1970-01-01",
"00:00:00",
"1970-01-01 00:00:00",
"1970-01-01 00:00:00.000",
],
}
`;

exports[`cqn2sql selection of columns of one table with select from non existent entity with star wildcard 1`] = `"SELECT * FROM "¿HoWdIdYoUmAnAgeToCaLaNeNtItyThIsNaMe?""`;

exports[`cqn2sql selection of columns of one table with select specific elements with from ref 1`] = `"SELECT Foo.a,Foo.b,Foo.c FROM Foo as Foo"`;
Expand Down
21 changes: 19 additions & 2 deletions db-service/test/cqn2sql/select.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,23 @@ describe('cqn2sql', () => {
`Query was not inferred and includes '*' in the columns. For which there is no column name available.`,
)
})

test('select with static values', () => {
let query = CQL(`SELECT from Foo {
5 as ID,
'simple string' as a,
3.14 as pi,
3.1415 as pid : cds.Decimal(5,4),
'large string' as stringl : cds.LargeString,
true as boolt : Boolean,
'1970-01-01' as date : cds.Date,
'00:00:00' as time : cds.Time,
'1970-01-01 00:00:00' as datetime : cds.DateTime,
'1970-01-01 00:00:00.000' as timestamp : cds.Timestamp
}`)
const { sql, values } = cqn2sql(query)
expect({ sql, values }).toMatchSnapshot()
})
})

describe('WHERE', () => {
Expand Down Expand Up @@ -465,8 +482,8 @@ describe('cqn2sql', () => {
],
},
}
const { sql } = cqn2sql(cqn)
expect({ sql }).toMatchSnapshot()
const { sql, values } = cqn2sql(cqn)
expect({ sql, values }).toMatchSnapshot()
})

// aliases should be quoted only for HANA
Expand Down

0 comments on commit 8136a45

Please sign in to comment.