Skip to content

Commit

Permalink
fix: Align all quote functions with @sap/cds-compiler (#619)
Browse files Browse the repository at this point in the history
  • Loading branch information
BobdenOs authored Apr 30, 2024
1 parent c578e9f commit 42e9828
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 13 deletions.
3 changes: 1 addition & 2 deletions db-service/lib/cqn2sql.js
Original file line number Diff line number Diff line change
Expand Up @@ -968,8 +968,7 @@ class CQN2SQLRenderer {
quote(s) {
if (typeof s !== 'string') return '"' + s + '"'
if (s.includes('"')) return '"' + s.replace(/"/g, '""') + '"'
// Column names like "Order" clash with "ORDER" keyword so toUpperCase is required
if (s in this.class.ReservedWords || /^\d|[$' ?@./\\]/.test(s)) return '"' + s + '"'
if (s in this.class.ReservedWords || !/^[A-Za-z_][A-Za-z_$0-9]*$/.test(s)) return '"' + s + '"'
return s
}

Expand Down
2 changes: 1 addition & 1 deletion hana/lib/HANAService.js
Original file line number Diff line number Diff line change
Expand Up @@ -879,7 +879,7 @@ class HANAService extends SQLService {
// cds-compiler effectiveName uses toUpperCase for hana dialect, but not for hdbcds
if (typeof s !== 'string') return '"' + s + '"'
if (s.includes('"')) return '"' + s.replace(/"/g, '""').toUpperCase() + '"'
if (s.toUpperCase() in this.class.ReservedWords || /^\d|[$' @./\\]/.test(s)) return '"' + s.toUpperCase() + '"'
if (s in this.class.ReservedWords || !/^[A-Za-z_][A-Za-z_$#0-9]*$/.test(s)) return '"' + s.toUpperCase() + '"'
return s
}

Expand Down
24 changes: 14 additions & 10 deletions postgres/lib/PostgresService.js
Original file line number Diff line number Diff line change
Expand Up @@ -298,21 +298,17 @@ GROUP BY k
if (query.SELECT?.columns?.find(col => col.as === '$mediaContentType')) {
const columns = query.SELECT.columns
const index = columns.findIndex(col => query.elements[col.ref?.[col.ref.length - 1]].type === 'cds.LargeBinary')
const binary = columns[index]
const binary = columns.splice(index, 1)
// SELECT without binary column
columns.splice(index, 1)
const { sql, values } = this.cqn2sql(query, data)
let ps = this.prepare(sql)
let res = await ps.all(values)
if (res.length === 0) return
res = res.map(r => (typeof r._json_ === 'string' ? JSON.parse(r._json_) : r._json_ || r))[0]
let res = await super.onSELECT({ query, data })
if (!res) return res
// SELECT only binary column
query.SELECT.columns = [binary]
query.SELECT.columns = binary
const { sql: streamSql, values: valuesStream } = this.cqn2sql(query, data)
ps = this.prepare(streamSql)
const ps = this.prepare(streamSql)
const stream = await ps.stream(valuesStream, true)
// merge results
res[binary.as || binary.ref[binary.ref.length - 1]] = stream
res[this.class.CQN2SQL.prototype.column_name(binary[0])] = stream
return res
}
return super.onSELECT({ query, data })
Expand Down Expand Up @@ -441,6 +437,14 @@ GROUP BY k
return 'FOR SHARE'
}

// Postgres requires own quote function, becuase the effective name is lower case
quote(s) {
if (typeof s !== 'string') return '"' + s + '"'
if (s.includes('"')) return '"' + s.replace(/"/g, '""').toLowerCase() + '"'
if (s in this.class.ReservedWords || !/^[A-Za-z_][A-Za-z_$0-9]*$/.test(s)) return '"' + s.toLowerCase() + '"'
return s
}

defaultValue(defaultValue = this.context.timestamp.toISOString()) {
return this.string(`${defaultValue}`)
}
Expand Down

0 comments on commit 42e9828

Please sign in to comment.