Skip to content

Commit

Permalink
feat: Add quoted mode support (#681)
Browse files Browse the repository at this point in the history
Co-authored-by: Johannes Vogel <31311694+johannes-vogel@users.noreply.github.com>
Co-authored-by: Daniel Hutzel <daniel.hutzel@sap.com>
  • Loading branch information
3 people authored Oct 1, 2024
1 parent b84797a commit 43c7a6c
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
13 changes: 12 additions & 1 deletion db-service/lib/cqn2sql.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ class CQN2SQLRenderer {
this.class = new.target // for IntelliSense
this.class._init() // is a noop for subsequent calls
this.model = srv?.model

// Overwrite smart quoting
if (cds.env.sql.names === 'quoted') {
this.class.prototype.name = (name) => name.id || name
this.class.prototype.quote = (s) => `"${String(s).replace(/"/g, '""')}"`
}
}

static _add_mixins(aspect, mixins) {
Expand Down Expand Up @@ -336,7 +342,12 @@ class CQN2SQLRenderer {
const { ref, as } = from
const _aliased = as ? s => s + ` as ${this.quote(as)}` : s => s
if (ref) {
const z = ref[0]
let z = ref[0]
if (cds.env.sql.names === 'quoted') {
// use SELECT.from to infer query, cds.infer also expects a query
const { target } = SELECT.from(from)
z = target?.['@cds.persistence.name'] || ref[0]
}
if (z.args) {
return _aliased(`${this.quote(this.name(z))}${this.from_args(z.args)}`)
}
Expand Down
4 changes: 2 additions & 2 deletions hana/lib/HANAService.js
Original file line number Diff line number Diff line change
Expand Up @@ -607,11 +607,11 @@ class HANAService extends SQLService {

// Calculate final output columns once
let outputColumns = ''
outputColumns = `_path_ as "_path_",${blobs} as "_blobs_",${expands} as "_expands_",${jsonColumn} as "_json_"`
outputColumns = `${this.quote('_path_')} as "_path_",${blobs} as "_blobs_",${expands} as "_expands_",${jsonColumn} as "_json_"`
if (blobColumns.length)
outputColumns = `${outputColumns},${blobColumns.map(b => `${this.quote(b)} as "${b.replace(/"/g, '""')}"`)}`
this._outputColumns = outputColumns
sql = `*,${path} as _path_`
sql = `*,${path} as ${this.quote('_path_')}`
}
return sql
}
Expand Down

0 comments on commit 43c7a6c

Please sign in to comment.