Skip to content

Commit 769a30d

Browse files
committed
refactor(plpgsql-deparser): use pretty print and creative lowercase names
- Enable pretty print option for SQL deparser - Rename function: big_kitchen_sink -> order_rollup_calculator - Rename variables: v_discount -> v_rebate, v_tax -> v_levy - Change default values: 0 -> 42
1 parent c1698f9 commit 769a30d

2 files changed

Lines changed: 21 additions & 21 deletions

File tree

packages/plpgsql-deparser/__tests__/__snapshots__/hydrate-demo.test.ts.snap

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
// Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing
22

33
exports[`hydrate demonstration with big-function.sql should parse, hydrate, modify, and deparse big-function.sql with full CREATE FUNCTION 1`] = `
4-
"CREATE OR REPLACE FUNCTION app_public."big_kitchen_sink_MODIFIED"(p_org_id uuid, p_user_id uuid, p_from_ts timestamptz DEFAULT now() - '30 days'::interval, p_to_ts timestamptz DEFAULT now(), p_min_total numeric DEFAULT 0, p_max_rows int DEFAULT 250, p_currency text DEFAULT 'USD', p_apply_discount boolean DEFAULT true, p_discount_rate numeric DEFAULT 0.05, p_tax_rate numeric DEFAULT 0.0875, p_round_to int DEFAULT 2, p_note text DEFAULT NULL, p_lock boolean DEFAULT false, p_debug boolean DEFAULT false) RETURNS TABLE ( org_id uuid, user_id uuid, period_from timestamptz, period_to timestamptz, orders_scanned int, orders_upserted int, gross_total numeric, discount_total numeric, tax_total numeric, net_total numeric, avg_order_total numeric, top_sku text, top_sku_qty bigint, message text ) LANGUAGE plpgsql AS $$DECLARE
5-
v_orders_scanned int := 888;
6-
v_orders_upserted int := 888;
7-
v_gross numeric := 888;
8-
v_discount numeric := 888;
9-
v_tax numeric := 888;
10-
v_net numeric := 888;
11-
v_avg numeric := 888;
4+
"CREATE OR REPLACE FUNCTION app_public.order_rollup_calculator(p_org_id uuid, p_user_id uuid, p_from_ts timestamptz DEFAULT now() - '30 days'::interval, p_to_ts timestamptz DEFAULT now(), p_min_total numeric DEFAULT 0, p_max_rows int DEFAULT 250, p_currency text DEFAULT 'USD', p_apply_discount boolean DEFAULT true, p_discount_rate numeric DEFAULT 0.05, p_tax_rate numeric DEFAULT 0.0875, p_round_to int DEFAULT 2, p_note text DEFAULT NULL, p_lock boolean DEFAULT false, p_debug boolean DEFAULT false) RETURNS TABLE ( org_id uuid, user_id uuid, period_from timestamptz, period_to timestamptz, orders_scanned int, orders_upserted int, gross_total numeric, discount_total numeric, tax_total numeric, net_total numeric, avg_order_total numeric, top_sku text, top_sku_qty bigint, message text ) LANGUAGE plpgsql AS $$DECLARE
5+
v_orders_scanned int := 42;
6+
v_orders_upserted int := 42;
7+
v_gross numeric := 42;
8+
v_discount numeric := 42;
9+
v_tax numeric := 42;
10+
v_net numeric := 42;
11+
v_avg numeric := 42;
1212
v_top_sku text := NULL;
13-
v_top_sku_qty bigint := 888;
13+
v_top_sku_qty bigint := 42;
1414
v_now timestamptz := clock_timestamp();
1515
v_jitter numeric := (random() - 0.5) * 0.02;
1616
v_discount_rate numeric := GREATEST(LEAST(p_discount_rate, 0.50), 0);
@@ -70,11 +70,11 @@ END IF;
7070
t.avg_total
7171
FROM totals t;
7272
IF p_apply_discount THEN
73-
v_discount_MODIFIED := round(v_gross * GREATEST(LEAST(v_discount_rate + v_jitter, 0.50), 0), p_round_to);
73+
v_rebate := round(v_gross * GREATEST(LEAST(v_discount_rate + v_jitter, 0.50), 0), p_round_to);
7474
ELSE
7575
v_discount := 0;
7676
END IF;
77-
v_tax_MODIFIED := round(GREATEST(v_gross - v_discount, 0) * v_tax_rate, p_round_to);
77+
v_levy := round(GREATEST(v_gross - v_discount, 0) * v_tax_rate, p_round_to);
7878
v_net := round((v_gross - v_discount + v_tax) * power(10::numeric, 0), p_round_to);
7979
SELECT
8080
oi.sku,

packages/plpgsql-deparser/__tests__/hydrate-demo.test.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ describe('hydrate demonstration with big-function.sql', () => {
3434
expect(stats.failedExpressions).toBe(0);
3535
expect(stats.rawExpressions).toBe(0);
3636

37-
createFunctionStmt.funcname[1].String.sval = 'big_kitchen_sink_MODIFIED';
37+
createFunctionStmt.funcname[1].String.sval = 'order_rollup_calculator';
3838

3939
const modifiedPlpgsqlAst = modifyAst(JSON.parse(JSON.stringify(hydratedAst)));
4040

@@ -46,14 +46,14 @@ describe('hydrate demonstration with big-function.sql', () => {
4646
asOption.DefElem.arg.List.items[0].String.sval = modifiedBody;
4747
}
4848

49-
const fullDeparsed = await deparse(sqlParsed.stmts[0].stmt);
49+
const fullDeparsed = await deparse(sqlParsed.stmts[0].stmt, { pretty: true });
5050

51-
expect(fullDeparsed).toContain('big_kitchen_sink_MODIFIED');
51+
expect(fullDeparsed).toContain('order_rollup_calculator');
5252
expect(fullDeparsed).toContain('RETURNS TABLE');
5353
expect(fullDeparsed).toContain('CREATE OR REPLACE FUNCTION');
54-
expect(fullDeparsed).toContain('v_discount_MODIFIED');
55-
expect(fullDeparsed).toContain('v_tax_MODIFIED');
56-
expect(fullDeparsed).toContain('888');
54+
expect(fullDeparsed).toContain('v_rebate');
55+
expect(fullDeparsed).toContain('v_levy');
56+
expect(fullDeparsed).toContain('42');
5757

5858
expect(fullDeparsed).toMatchSnapshot();
5959
});
@@ -103,24 +103,24 @@ function modifyAst(ast: any): any {
103103

104104
if (typeof query === 'object' && query.kind === 'assign') {
105105
if (query.target === 'v_discount' && assignModCount === 0) {
106-
query.target = 'v_discount_MODIFIED';
106+
query.target = 'v_rebate';
107107
assignModCount++;
108108
modCount++;
109109
}
110110
if (query.target === 'v_tax' && assignModCount === 1) {
111-
query.target = 'v_tax_MODIFIED';
111+
query.target = 'v_levy';
112112
assignModCount++;
113113
modCount++;
114114
}
115115
if (query.value === '0' && modCount < 5) {
116-
query.value = '999';
116+
query.value = '42';
117117
modCount++;
118118
}
119119
}
120120

121121
if (typeof query === 'object' && query.kind === 'sql-expr') {
122122
if (query.original === '0' && modCount < 8) {
123-
query.original = '888';
123+
query.original = '42';
124124
modCount++;
125125
}
126126
}

0 commit comments

Comments
 (0)