feat(grid): improve production watermark and sql formatting
This commit is contained in:
10 files changed
+525
-335
No files matched your search
+12
-6
@@ -146,14 +146,17 @@ describe("sqlFormatter", () => {
|
||||
expect(formatted).toBe('CREATE VIEW "TEMP_TEST_VIEW" AS\nSELECT trunc(sysdate) AS dates,\n -- 测试\n (SELECT sysdate FROM dual t) AS nows\nFROM dual;');
|
||||
});
|
||||
|
||||
it("collapses a short OceanBase Oracle view DDL onto one line (issue #7540)", async () => {
|
||||
// The default style joins a statement that fits the line width onto one
|
||||
// line, so a short view body comes back with its keywords cased rather
|
||||
// than spread over several.
|
||||
it("keeps a short OceanBase Oracle view projection readable (issue #7540)", async () => {
|
||||
// A view body still keeps each projected field on its own line while its
|
||||
// surrounding clauses retain the default layout.
|
||||
const singleLine = `create or replace view "APP"."ACTIVE_USERS" as select ID, NAME from USERS where STATUS = 'ACTIVE'`;
|
||||
const formatted = await formatSqlForDisplay(singleLine, sqlFormatDialectForDbType("oceanbase-oracle"));
|
||||
|
||||
expect(formatted).toBe(`CREATE OR REPLACE VIEW "APP"."ACTIVE_USERS" AS SELECT ID, NAME FROM USERS WHERE STATUS = 'ACTIVE'`);
|
||||
expect(formatted).toBe(`CREATE OR REPLACE VIEW "APP"."ACTIVE_USERS" AS
|
||||
SELECT ID,
|
||||
NAME
|
||||
FROM USERS
|
||||
WHERE STATUS = 'ACTIVE'`);
|
||||
expect(formatted).toContain("'ACTIVE'");
|
||||
expect(formatted).toContain('"ACTIVE_USERS"');
|
||||
});
|
||||
@@ -339,7 +342,10 @@ OR inside$$ as note
|
||||
|
||||
// Alias casing is the tokenizer's business, not the keyword casing's: the
|
||||
// date part stays an identifier, so `keywordCase: "upper"` leaves it alone.
|
||||
expect(formatted).toBe(`SELECT ${identifier}, t.${identifier} FROM material ${identifier} LIMIT 100;`);
|
||||
expect(formatted).toBe(`SELECT ${identifier},
|
||||
t.${identifier}
|
||||
FROM material ${identifier}
|
||||
LIMIT 100;`);
|
||||
expect(formatted).toContain("SELECT");
|
||||
expect(formatted).toContain("FROM");
|
||||
expect(formatted).toContain("LIMIT");
|
||||
|
||||
+47
-16
@@ -31,10 +31,8 @@ describe("sql layout", () => {
|
||||
// The subquery's clauses align under the `(` that opens it, and the closing
|
||||
// `)` stays on the last clause's line.
|
||||
//
|
||||
// The subquery's two select elements could be split; the screenshots in
|
||||
// #827 and #4850 disagree on that point (both are two-element lists, one
|
||||
// split, one not), so the keeping rule here is the one #4850's rendering
|
||||
// matches.
|
||||
// Expanded subqueries use the same one-field-per-line rule as the outer
|
||||
// SELECT, while their clauses remain aligned under the opening parenthesis.
|
||||
expect(await format(sql)).toBe(
|
||||
lines(
|
||||
"SELECT loc_id,",
|
||||
@@ -42,7 +40,8 @@ describe("sql layout", () => {
|
||||
" loc_type,",
|
||||
" delivery_emp_num,",
|
||||
" update_time",
|
||||
"FROM (SELECT *, ROW_NUMBER() OVER (PARTITION BY loc_id ORDER BY update_time DESC) AS rn",
|
||||
"FROM (SELECT *,",
|
||||
" ROW_NUMBER() OVER (PARTITION BY loc_id ORDER BY update_time DESC) AS rn",
|
||||
" FROM responsible_area_info",
|
||||
" WHERE DATE_FORMAT(update_time, '%Y-%m') = DATE_FORMAT(NOW(), '%Y-%m')) t",
|
||||
"WHERE rn = 1;",
|
||||
@@ -62,9 +61,12 @@ describe("sql layout", () => {
|
||||
// subquery expands rather than pushing `ON` onto a line of its own.
|
||||
expect(await format(sql)).toBe(
|
||||
lines(
|
||||
"SELECT HIMMS_XDSBDJ.HIMMS_XDSBDJ_ID, HIMMS_XDSBDJ.YNKE, himms_zwxsysj_sysj(V_HIMMS_XDSBDJ.HIMMS_XDSBDJ_ID) AS SJSYSJ",
|
||||
"SELECT HIMMS_XDSBDJ.HIMMS_XDSBDJ_ID,",
|
||||
" HIMMS_XDSBDJ.YNKE,",
|
||||
" himms_zwxsysj_sysj(V_HIMMS_XDSBDJ.HIMMS_XDSBDJ_ID) AS SJSYSJ",
|
||||
"FROM HIMMS_XDSBDJ",
|
||||
" LEFT JOIN (SELECT himms_xdsbdj_id, MAX(SJ) SJ",
|
||||
" LEFT JOIN (SELECT himms_xdsbdj_id,",
|
||||
" MAX(SJ) SJ",
|
||||
" FROM himms_zwxqdjc",
|
||||
" GROUP BY himms_xdsbdj_id) SUB_QDJC ON HIMMS_XDSBDJ.HIMMS_XDSBDJ_ID = SUB_QDJC.himms_xdsbdj_id",
|
||||
"WHERE HIMMS_XDSBDJ.tybz IS NULL;",
|
||||
@@ -121,7 +123,7 @@ describe("sql layout", () => {
|
||||
// `logicalOperatorNewline: "before"` starts each continuation line with the
|
||||
// operator, aligned under the condition's first operand like any other
|
||||
// clause item.
|
||||
expect(await format(sql)).toBe(lines("SELECT a, b", "FROM t", "WHERE alpha = 1", " AND beta = 2", " AND gamma = 3", " AND delta = 4", " AND epsilon = 5", " AND zeta = 6", " AND eta = 7", " AND theta = 8;"));
|
||||
expect(await format(sql)).toBe(lines("SELECT a,", " b", "FROM t", "WHERE alpha = 1", " AND beta = 2", " AND gamma = 3", " AND delta = 4", " AND epsilon = 5", " AND zeta = 6", " AND eta = 7", " AND theta = 8;"));
|
||||
});
|
||||
|
||||
it("puts a wrapped condition's operators where the setting asks", async () => {
|
||||
@@ -130,7 +132,7 @@ describe("sql layout", () => {
|
||||
// `after` ends the previous line with the operator instead.
|
||||
expect(await format(sql, { logicalOperatorNewline: "after" })).toContain("WHERE alpha = 1 AND\n beta = 2 AND");
|
||||
// `none` keeps the condition on the clause's line, however long it gets.
|
||||
expect(await format(sql, { logicalOperatorNewline: "none" })).toBe(lines("SELECT a, b", "FROM t", "WHERE alpha = 1 AND beta = 2 AND gamma = 3 AND delta = 4 AND epsilon = 5 AND zeta = 6 AND eta = 7 AND theta = 8;"));
|
||||
expect(await format(sql, { logicalOperatorNewline: "none" })).toBe(lines("SELECT a,", " b", "FROM t", "WHERE alpha = 1 AND beta = 2 AND gamma = 3 AND delta = 4 AND epsilon = 5 AND zeta = 6 AND eta = 7 AND theta = 8;"));
|
||||
});
|
||||
|
||||
it("expands a parenthesized list that does not fit, aligned under its first item", async () => {
|
||||
@@ -167,13 +169,40 @@ describe("sql layout", () => {
|
||||
it("collapses each clause independently", async () => {
|
||||
const sql = "SELECT (SELECT count(*) FROM orders o WHERE o.customer_id = c.id AND o.status = 'paid') AS order_count, c.name FROM customers c;";
|
||||
|
||||
// The scalar subquery fits, the select list does not: only the second one
|
||||
// breaks.
|
||||
expect(await format(sql)).toBe(lines("SELECT (SELECT count(*) FROM orders o WHERE o.customer_id = c.id AND o.status = 'paid') AS order_count, c.name", "FROM customers c;"));
|
||||
// The scalar subquery stays compact within its own SELECT field.
|
||||
expect(await format(sql)).toBe(lines("SELECT (SELECT count(*) FROM orders o WHERE o.customer_id = c.id AND o.status = 'paid') AS order_count,", " c.name", "FROM customers c;"));
|
||||
});
|
||||
|
||||
it("never joins a CASE onto one line", async () => {
|
||||
expect(await format("SELECT CASE WHEN a = 1 THEN 'one' WHEN a = 2 THEN 'two' ELSE 'other' END AS label FROM t;")).toBe(lines("SELECT CASE", " WHEN a = 1 THEN 'one'", " WHEN a = 2 THEN 'two'", " ELSE 'other'", " END AS label", "FROM t;"));
|
||||
it("keeps a CASE expression compact (#10783)", async () => {
|
||||
expect(await format("SELECT CASE WHEN a = 1 THEN 'one' WHEN a = 2 THEN 'two' ELSE 'other' END AS label FROM t;")).toBe("SELECT CASE WHEN a = 1 THEN 'one' WHEN a = 2 THEN 'two' ELSE 'other' END AS label FROM t;");
|
||||
});
|
||||
|
||||
it("puts each SELECT field on its own line even in a short query (#10783)", async () => {
|
||||
expect(await format("SELECT id, CASE WHEN a = 1 THEN 'one' ELSE 'other' END AS label, coalesce(a, b) FROM t;")).toBe(lines("SELECT id,", " CASE WHEN a = 1 THEN 'one' ELSE 'other' END AS label,", " coalesce(a, b)", "FROM t;"));
|
||||
});
|
||||
|
||||
it("puts fields on separate lines when SELECT has a modifier (#10783)", async () => {
|
||||
expect(await format("SELECT DISTINCT a, b FROM t;")).toBe(lines("SELECT DISTINCT a,", " b", "FROM t;"));
|
||||
expect(await format("SELECT DISTINCT ON (key) a, b FROM t;")).toBe(lines("SELECT DISTINCT ON (KEY) a,", " b", "FROM t;"));
|
||||
});
|
||||
|
||||
it("keeps nested and simple CASE expressions compact", async () => {
|
||||
const sql = "SELECT SUM(CASE WHEN a = 1 AND b = 2 THEN 1 ELSE 0 END) AS total, CASE a WHEN 1 THEN CASE b WHEN 2 THEN 'x y' END ELSE 'other' END AS label FROM t;";
|
||||
const expected = lines("SELECT SUM(CASE WHEN a = 1 AND b = 2 THEN 1 ELSE 0 END) AS total,", " CASE a WHEN 1 THEN CASE b WHEN 2 THEN 'x y' END ELSE 'other' END AS label", "FROM t;");
|
||||
expect(await format(sql)).toBe(expected);
|
||||
expect(await format(expected)).toBe(expected);
|
||||
});
|
||||
|
||||
it("preserves comments and width wrapping inside CASE expressions", async () => {
|
||||
const commented = await format(lines("SELECT id, CASE WHEN a = 1 THEN 'one' -- retain this", "ELSE 'other' END AS label FROM t;"));
|
||||
expect(commented).toContain("-- retain this\n");
|
||||
expect(commented).toContain("ELSE 'other'");
|
||||
expect(await format(commented)).toBe(commented);
|
||||
|
||||
const longCase = await format(`SELECT id, CASE WHEN a = 1 THEN '${"x".repeat(100)}' ELSE 'other' END AS label FROM t;`);
|
||||
expect(longCase).toContain("CASE\n");
|
||||
expect(longCase).toContain("END AS label");
|
||||
expect(await format(longCase)).toBe(longCase);
|
||||
});
|
||||
|
||||
it("gives a set operation its own line", async () => {
|
||||
@@ -187,7 +216,7 @@ describe("sql layout", () => {
|
||||
});
|
||||
|
||||
it("keeps PostgreSQL casts tight", async () => {
|
||||
expect(await format("select coalesce(a, b)::text, (x + y)::int from t;", {}, "postgres")).toBe("SELECT coalesce(a, b)::text, (x + y)::int FROM t;");
|
||||
expect(await format("select coalesce(a, b)::text, (x + y)::int from t;", {}, "postgres")).toBe(lines("SELECT coalesce(a, b)::text,", " (x + y)::int", "FROM t;"));
|
||||
});
|
||||
|
||||
it("keeps the comma of a MySQL LIMIT offset", async () => {
|
||||
@@ -243,7 +272,9 @@ describe("sql layout", () => {
|
||||
// keyword alone deleted every branch after the first one.
|
||||
expect(await format(sql)).toBe(
|
||||
lines(
|
||||
"SELECT a.id, a.starttime, t.name AS taskName",
|
||||
"SELECT a.id,",
|
||||
" a.starttime,",
|
||||
" t.name AS taskName",
|
||||
"FROM ((SELECT l.id, l.starttime FROM `log_1_4` l WHERE l.taskid = 'abc' ORDER BY l.starttime DESC LIMIT 0, 10)",
|
||||
" UNION ALL",
|
||||
" (SELECT l.id, l.starttime FROM `log_2_4` l WHERE l.taskid = 'abc' ORDER BY l.starttime DESC LIMIT 0, 10)",
|
||||
|
||||
Reference in new issue
Block a user