The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:
TypeScript analyticsbibusiness-intelligencebusinessintelligenceclojuredashboarddatadata-analysisdata-visualizationdatabasemetabasemysqlpostgrespostgresqlreportingslacksql-editorvisualization
Don't add newline to commented out clauses in native query preview (#66700)
Riley Thompsonaf53e3fa1d16
diff --git a/frontend/src/metabase/lib/engine.ts b/frontend/src/metabase/lib/engine.tsindex 83cedc52e95..7e4009791ce 100644--- a/frontend/src/metabase/lib/engine.ts+++ b/frontend/src/metabase/lib/engine.ts@@ -20,20 +20,7 @@ function formatJsonQuery(query: JSONQuery) {return JSON.stringify(query, null, 2);}-export function formatNativeQuery(- query: string | JSONQuery,- engine: string,-): string {- const engineType = getEngineNativeType(engine);-- if (typeof query === "string" && engineType === "sql") {- return formatSQL(query);- }-- if (engineType === "json") {- return typeof query === "object" ? formatJsonQuery(query) : query;- }-+export function formatNativeQuery(query: string | JSONQuery): string {return typeof query === "string" ? query : formatJsonQuery(query);}@@ -43,15 +30,3 @@ export function isDeprecatedEngine() {return engines[engine] != null && engines[engine]["superseded-by"] != null;}--function formatSQL(sql: string) {- sql = sql.replace(/\sFROM/, "\nFROM");- sql = sql.replace(/\sLEFT JOIN/, "\nLEFT JOIN");- sql = sql.replace(/\sWHERE/, "\nWHERE");- sql = sql.replace(/\sGROUP BY/, "\nGROUP BY");- sql = sql.replace(/\sORDER BY/, "\nORDER BY");- sql = sql.replace(/\sLIMIT/, "\nLIMIT");- sql = sql.replace(/\sAND\s/, "\n AND ");- sql = sql.replace(/\sOR\s/, "\n OR ");- return sql;-}diff --git a/frontend/src/metabase/lib/engine.unit.spec.ts b/frontend/src/metabase/lib/engine.unit.spec.tsindex d34d9087bb4..1c448d9cdcd 100644--- a/frontend/src/metabase/lib/engine.unit.spec.ts+++ b/frontend/src/metabase/lib/engine.unit.spec.ts@@ -38,27 +38,35 @@ describe("getNativeQueryLanguage", () => {describe("formatNativeQuery", () => {it("should return formatted SQL", () => {- expect(formatNativeQuery("select 1", "postgres")).toEqual("select 1");- expect(- formatNativeQuery("SELECT * FROM PUBLIC.ORDERS", "postgres"),- ).toEqual("SELECT *\nFROM PUBLIC.ORDERS");+ expect(formatNativeQuery("select 1")).toEqual("select 1");+ expect(formatNativeQuery("SELECT * FROM PUBLIC.ORDERS")).toEqual(+ "SELECT * FROM PUBLIC.ORDERS",+ );});it("should return any valid string if the engine type is sql", () => {- expect(formatNativeQuery("foo", "postgres")).toEqual("foo");- expect(formatNativeQuery("FOO BAR baz", "postgres")).toEqual("FOO BAR baz");- expect(formatNativeQuery("FOO: BAR, baz.", "postgres")).toEqual(- "FOO: BAR, baz.",+ expect(formatNativeQuery("foo")).toEqual("foo");+ expect(formatNativeQuery("FOO BAR baz")).toEqual("FOO BAR baz");+ expect(formatNativeQuery("FOO: BAR, baz.")).toEqual("FOO: BAR, baz.");+ expect(formatNativeQuery("-- foo")).toEqual("-- foo");+ });++ it("should not format SQL keywords inside comment lines", () => {+ expect(+ formatNativeQuery(+ "-- SELECT * FROM products WHERE category = 'Widget'\nSELECT * FROM products WHERE category = 'Widget'",+ ),+ ).toEqual(+ "-- SELECT * FROM products WHERE category = 'Widget'\nSELECT * FROM products WHERE category = 'Widget'",);- expect(formatNativeQuery("-- foo", "postgres")).toEqual("-- foo");});it("should return formatted JSON", () => {- expect(formatNativeQuery({}, "mongo")).toEqual("{}");- expect(formatNativeQuery([], "mongo")).toEqual("[]");- expect(formatNativeQuery(["foo"], "mongo")).toEqual('[\n "foo"\n]');- expect(formatNativeQuery({ a: 1 }, "mongo")).toEqual('{\n "a": 1\n}');- expect(formatNativeQuery('["foo"]', "mongo")).toEqual('["foo"]');+ expect(formatNativeQuery({})).toEqual("{}");+ expect(formatNativeQuery([])).toEqual("[]");+ expect(formatNativeQuery(["foo"])).toEqual('[\n "foo"\n]');+ expect(formatNativeQuery({ a: 1 })).toEqual('{\n "a": 1\n}');+ expect(formatNativeQuery('["foo"]')).toEqual('["foo"]');});});diff --git a/frontend/src/metabase/querying/notebook/components/NativeQueryPreview/NativeQueryPreview.tsx b/frontend/src/metabase/querying/notebook/components/NativeQueryPreview/NativeQueryPreview.tsxindex 3451c8f6734..69b0c1bef39 100644--- a/frontend/src/metabase/querying/notebook/components/NativeQueryPreview/NativeQueryPreview.tsx+++ b/frontend/src/metabase/querying/notebook/components/NativeQueryPreview/NativeQueryPreview.tsx@@ -82,7 +82,7 @@ function getFormattedQuery(if (engine === null) {return data.query;}- return formatNativeQuery(data.query, engine);+ return formatNativeQuery(data.query);}function NativeCodePanel({diff --git a/frontend/src/metabase/querying/notebook/components/NotebookNativePreview/utils.ts b/frontend/src/metabase/querying/notebook/components/NotebookNativePreview/utils.tsindex c52f42419c7..1119d29cdd0 100644--- a/frontend/src/metabase/querying/notebook/components/NotebookNativePreview/utils.ts+++ b/frontend/src/metabase/querying/notebook/components/NotebookNativePreview/utils.ts@@ -16,7 +16,7 @@ export function createNativeQuestion(database.id,question.metadata(),);- const rawQuery = formatNativeQuery(response.query, database.engine);+ const rawQuery = formatNativeQuery(response.query);const newQuery = Lib.nativeQuery(database.id, metadataProvider, rawQuery);const newQueryWithCollection =response.collection != null