From 13ec6702959584e1a9c797370beded6955ed2426 Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Wed, 4 May 2022 16:13:54 +0100 Subject: [PATCH] Fix for #5669 comment, when using SQL tables with spaces in names, we use a raw knex function for like to achieve lower case searching, this needs to handle spaces in columns and delimiting. --- packages/server/src/integrations/base/sql.ts | 29 +++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/packages/server/src/integrations/base/sql.ts b/packages/server/src/integrations/base/sql.ts index 2e14eae870..782f61e49e 100644 --- a/packages/server/src/integrations/base/sql.ts +++ b/packages/server/src/integrations/base/sql.ts @@ -21,6 +21,31 @@ type KnexQuery = Knex.QueryBuilder | Knex const MIN_ISO_DATE = "0000-00-00T00:00:00.000Z" const MAX_ISO_DATE = "9999-00-00T00:00:00.000Z" +function likeKey(client: string, key: string): string { + if (!key.includes(" ")) { + return key + } + let start: string, end: string + switch (client) { + case SqlClients.MY_SQL: + start = end = "`" + break + case SqlClients.ORACLE: + case SqlClients.POSTGRES: + start = end = '"' + break + case SqlClients.MS_SQL: + start = "[" + end = "]" + break + default: + throw "Unknown client" + } + const parts = key.split(".") + key = parts.map(part => `${start}${part}${end}`).join(".") + return key +} + function parse(input: any) { if (Array.isArray(input)) { return JSON.stringify(input) @@ -125,7 +150,9 @@ class InternalBuilder { } else { const rawFnc = `${fnc}Raw` // @ts-ignore - query = query[rawFnc](`LOWER(${key}) LIKE ?`, [`%${value}%`]) + query = query[rawFnc](`LOWER(${likeKey(this.client, key)}) LIKE ?`, [ + `%${value}%`, + ]) } }) }