From 2bc2c405322b93450836b58f3fb1abd46d87436a Mon Sep 17 00:00:00 2001 From: Rikin Kachhia Date: Tue, 19 Mar 2019 14:23:19 +0530 Subject: [PATCH] handle capital letters and quotes in Raw SQL Track this (#1811) --- .../components/Services/Data/RawSQL/utils.js | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/console/src/components/Services/Data/RawSQL/utils.js b/console/src/components/Services/Data/RawSQL/utils.js index ae99a9e2877..10a728d192d 100644 --- a/console/src/components/Services/Data/RawSQL/utils.js +++ b/console/src/components/Services/Data/RawSQL/utils.js @@ -2,14 +2,23 @@ const createSQLRegex = /create\s*(?:|or\s*replace)\s*(view|table|function)\s*((\ const createSQLRegexNoFunction = /create\s*(?:|or\s*replace)\s*(view|table)\s*((\"?\w+\"?)\.(\"?\w+\"?)|(\"?\w+\"?))/; // eslint-disable-line +const getSQLValue = value => { + const quotedStringRegex = /^".*"$/; + + let sqlValue = value; + if (!quotedStringRegex.test(value)) { + sqlValue = value.toLowerCase(); + } + + return sqlValue.replace(/['"]+/g, ''); +}; + const parseCreateSQL = (sql, allowFunction) => { const _objects = []; - const formattedSQL = sql.toLowerCase(); - const regExp = allowFunction ? createSQLRegex : createSQLRegexNoFunction; - const matches = formattedSQL.match(new RegExp(regExp, 'gmi')); + const matches = sql.match(new RegExp(regExp, 'gmi')); if (matches) { matches.forEach(element => { const itemMatch = element.match(new RegExp(regExp, 'i')); @@ -32,8 +41,8 @@ const parseCreateSQL = (sql, allowFunction) => { } _object.type = type.toLowerCase(); - _object.name = name.replace(/['"]+/g, '').trim(); - _object.schema = schema.replace(/['"]+/g, '').trim(); + _object.name = getSQLValue(name); + _object.schema = getSQLValue(schema); _objects.push(_object); }