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: add placeholder for string values #733

Merged
merged 13 commits into from
Jul 15, 2024
Merged
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
7 changes: 7 additions & 0 deletions test/scenarios/bookshop/read.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,13 @@ describe('Bookshop - Read', () => {
expect(columns).to.contain('title')
})

test('Path expression with null value', async () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. the test name is incorrect
  2. there is already a test doing this as was shared with you before here

Copy link
Contributor Author

@mariayord mariayord Jul 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Please make a suggestion for the test name if you don't like it.
  2. This is an integration test (I suppose it should run with Hana, Postgres..), the other one is unit, if this is not the case please point where to add the integration test.

const q = CQL`SELECT null as title, author.name as author FROM sap.capire.bookshop.Books where author.name LIKE '%a%'`
const res = await cds.run(q)
expect(res.length).to.be.eq(4)
expect(res[0].title).to.be.eq(null)
})

test('Smart quotation', async () => {
const q = CQL`
SELECT FROM sap.capire.bookshop.Books as ![FROM]
Expand Down