Skip to content

Commit

Permalink
fix(cqn2sql): smart quoting also for update statements (#475)
Browse files Browse the repository at this point in the history
this was missing, smart quotation is already in place for other
statements
  • Loading branch information
patricebender authored Feb 26, 2024
1 parent 30e1e7b commit 1688f77
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 5 deletions.
10 changes: 5 additions & 5 deletions db-service/lib/cqn2sql.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,8 @@ class CQN2SQLRenderer {
delete this.values
this.sql =
!query || target['@cds.persistence.table']
? `CREATE TABLE ${name} ( ${this.CREATE_elements(target.elements)} )`
: `CREATE VIEW ${name} AS ${this.SELECT(this.cqn4sql(query))}`
? `CREATE TABLE ${this.quote(name)} ( ${this.CREATE_elements(target.elements)} )`
: `CREATE VIEW ${this.quote(name)} AS ${this.SELECT(this.cqn4sql(query))}`
this.values = []
return
}
Expand Down Expand Up @@ -617,7 +617,7 @@ class CQN2SQLRenderer {
const columns = (this.columns = (INSERT.columns || ObjectKeys(elements)).filter(
c => c in elements && !elements[c].virtual && !elements[c].isAssociation,
))
this.sql = `INSERT INTO ${entity}${alias ? ' as ' + this.quote(alias) : ''} (${columns}) ${this.SELECT(
this.sql = `INSERT INTO ${this.quote(entity)}${alias ? ' as ' + this.quote(alias) : ''} (${columns}) ${this.SELECT(
this.cqn4sql(INSERT.as),
)}`
this.entries = [this.values]
Expand Down Expand Up @@ -689,8 +689,8 @@ class CQN2SQLRenderer {
UPDATE(q) {
const { entity, with: _with, data, where } = q.UPDATE
const elements = q.target?.elements
let sql = `UPDATE ${this.name(entity.ref?.[0] || entity)}`
if (entity.as) sql += ` AS ${entity.as}`
let sql = `UPDATE ${this.quote(this.name(entity.ref?.[0] || entity))}`
if (entity.as) sql += ` AS ${this.quote(entity.as)}`

let columns = []
if (data) _add(data, val => this.val({ val }))
Expand Down
48 changes: 48 additions & 0 deletions test/compliance/keywords.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
'use strict'
const cds = require('../../test/cds.js')
const { expect } = cds.test(__dirname + '/resources')

describe('keywords', () => {
test('insert, update, select', async () => {
const { Order } = cds.entities
const data = {
ID: 1,
alter: [
{
ID: 42,
number: null,
order_ID: 1,
},
{
ID: 43,
number: null,
order_ID: 1,
},
],
}
await INSERT(data).into(Order)
const select = await cds.run(CQL`SELECT from Order { ID, alter { * } } where exists alter`)
expect(select[0]).to.deep.eql(data)

data.alter.forEach(e => (e.number = 99)) // change data
await UPDATE.entity(Order).with(data).where('exists alter')

const selectAfterChange = await cds.run(CQL`SELECT from Order { ID, alter { * } } where exists alter`)
expect(selectAfterChange[0]).to.deep.eql(data)
})

test('insert as select', async () => {
const { Alter, ASC } = cds.entities
// fill other table first
await cds.run(INSERT({ ID: 1, alias: 42 }).into(ASC))
await INSERT.into(Alter)
.columns(['ID', 'number'])
.as(
SELECT.from(ASC)
.columns(['ID', 'alias'])
.where({ ref: ['alias'] }, '=', { val: 42 }),
)
const select = await SELECT.from(Alter).where('number = 42')
expect(select[0]).to.eql({ ID: 1, number: 42, order_ID: null })
})
})
1 change: 1 addition & 0 deletions test/compliance/resources/db/index.cds
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ using from './complex';
using from './edge';
using from './hana';
using from './timestamps';
using from './keywords';
15 changes: 15 additions & 0 deletions test/compliance/resources/db/keywords/index.cds
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// ORDER / ALTER / ASC / NUMBER are reserved words in ANSI SQL standard
entity Order {
key ID : Integer;
alter: composition of many Alter on alter.order = $self;
}

entity Alter {
key ID : Integer;
number: Integer;
order: Association to Order;
}
entity ASC {
key ID : Integer;
alias: Integer;
}

0 comments on commit 1688f77

Please sign in to comment.