From 71d085e331a8d30e30c0431e7fb6e01e33056ac3 Mon Sep 17 00:00:00 2001 From: Rasmus Porsager Date: Wed, 1 Feb 2023 20:45:02 +0100 Subject: [PATCH] Fallback to escaping multiple identifiers if no builder found - fixes #532 --- src/types.js | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/src/types.js b/src/types.js index b37f93d9..1beb60f0 100644 --- a/src/types.js +++ b/src/types.js @@ -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) } } @@ -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) @@ -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])) @@ -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, '"."') + '"' }