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: use "$$value$$" as internal column name for UPSERT #976

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
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
12 changes: 7 additions & 5 deletions db-service/lib/cqn2sql.js
Original file line number Diff line number Diff line change
Expand Up @@ -741,10 +741,10 @@ class CQN2SQLRenderer {

const extractkeys = managed
.filter(c => keys.includes(c.name))
.map(c => `${c.onInsert || c.sql} as ${this.quote(c.name)}`)
.map(c => `${(c.onInsert || c.sql).replace('"$$value$$"', 'value')} as ${this.quote(c.name)}`)

const entity = this.name(q.target?.name || UPSERT.into.ref[0], q)
sql = `SELECT ${managed.map(c => c.upsert)} FROM (SELECT value, ${extractkeys} from json_each(?)) as NEW LEFT JOIN ${this.quote(entity)} AS OLD ON ${keyCompare}`
sql = `SELECT ${managed.map(c => c.upsert)} FROM (SELECT value as "$$value$$", ${extractkeys} from json_each(?)) as NEW LEFT JOIN ${this.quote(entity)} AS OLD ON ${keyCompare}`

const updateColumns = columns.filter(c => {
if (keys.includes(c)) return false //> keys go into ON CONFLICT clause
Expand Down Expand Up @@ -1126,9 +1126,10 @@ class CQN2SQLRenderer {

managed_extract(name, element, converter) {
const { UPSERT, INSERT } = this.cqn
const value = UPSERT ? '"$$value$$"' : 'value'
const extract = !(INSERT?.entries || UPSERT?.entries) && (INSERT?.rows || UPSERT?.rows)
? `value->>'$[${this.columns.indexOf(name)}]'`
: `value->>'$."${name.replace(/"/g, '""')}"'`
? `${value}->>'$[${this.columns.indexOf(name)}]'`
: `${value}->>'$."${name.replace(/"/g, '""')}"'`
const sql = converter?.(extract) || extract
return { extract, sql }
}
Expand All @@ -1139,7 +1140,8 @@ class CQN2SQLRenderer {
}

managed_default(name, managed, src) {
return `(CASE WHEN json_type(value,${this.managed_extract(name).extract.slice(8)}) IS NULL THEN ${managed} ELSE ${src} END)`
const value = this.cqn.INSERT ? 'value' : '"$$value$$"'
return `(CASE WHEN json_type(${value},${this.managed_extract(name).extract.slice(value.length + 3)}) IS NULL THEN ${managed} ELSE ${src} END)`
}
}

Expand Down
5 changes: 5 additions & 0 deletions test/bookshop/db/schema.cds
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,9 @@ entity C : managed {
toB : Composition of many B
on toB.ID = $self.B;
};

entity Values {
key ID : Integer;
value : String;
}
entity BooksAnnotated as projection on Books;
16 changes: 16 additions & 0 deletions test/scenarios/bookshop/upsert.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const cds = require('../../cds.js')
const bookshop = cds.utils.path.resolve(__dirname, '../../bookshop')

describe('Bookshop - Upsert', () => {
const { expect } = cds.test(bookshop)

test.only('upsert data with "value" as column name', async () => {
// in our UPSERT logic we used "value" as internal column
// which led to an ambiguous column error if the entity has an element with the same name
const { Values } = cds.entities
const upsert = UPSERT({ ID: 201, value: 42 }).into(Values)
const res = await upsert;
expect(res).to.eql(1)
})

})
Loading