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: Align all quote functions with @sap/cds-compiler #619

Merged
merged 4 commits into from
Apr 30, 2024
Merged
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
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