Skip to content

Commit

Permalink
Fallback to escaping multiple identifiers if no builder found - fixes #…
Browse files Browse the repository at this point in the history
  • Loading branch information
porsager committed Feb 1, 2023
1 parent 11aec84 commit 71d085e
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions src/types.js
Expand Up @@ -66,10 +66,9 @@ export class Builder extends NotTagged {

build(before, parameters, types, options) {
const keyword = builders.map(([x, fn]) => ({ fn, i: before.search(x) })).sort((a, b) => a.i - b.i).pop()
if (keyword.i === -1)
throw new Error('Could not infer helper mode')

return keyword.fn(this.first, this.rest, parameters, types, options)
return keyword.i === -1
? escapeIdentifiers(this.first, options)
: keyword.fn(this.first, this.rest, parameters, types, options)
}
}

Expand Down Expand Up @@ -137,7 +136,7 @@ function values(first, rest, parameters, types, options) {
function select(first, rest, parameters, types, options) {
typeof first === 'string' && (first = [first].concat(rest))
if (Array.isArray(first))
return first.map(x => escapeIdentifier(options.transform.column.to ? options.transform.column.to(x) : x)).join(',')
return escapeIdentifiers(first, options)

let value
const columns = rest.length ? rest.flat() : Object.keys(first)
Expand Down Expand Up @@ -170,9 +169,7 @@ const builders = Object.entries({

insert(first, rest, parameters, types, options) {
const columns = rest.length ? rest.flat() : Object.keys(Array.isArray(first) ? first[0] : first)
return '(' + columns.map(x =>
escapeIdentifier(options.transform.column.to ? options.transform.column.to(x) : x)
).join(',') + ')values' +
return '(' + escapeIdentifiers(columns, options) + ')values' +
valuesBuilder(Array.isArray(first) ? first : [first], parameters, types, columns, options)
}
}).map(([x, fn]) => ([new RegExp('((?:^|[\\s(])' + x + '(?:$|[\\s(]))(?![\\s\\S]*\\1)', 'i'), fn]))
Expand Down Expand Up @@ -209,6 +206,10 @@ function typeHandlers(types) {
}, { parsers: {}, serializers: {} })
}

function escapeIdentifiers(xs, { transform: { column } }) {
return xs.map(x => escapeIdentifier(column.to ? column.to(x) : x)).join(',')
}

export const escapeIdentifier = function escape(str) {
return '"' + str.replace(/"/g, '""').replace(/\./g, '"."') + '"'
}
Expand Down

0 comments on commit 71d085e

Please sign in to comment.