-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathplpgsql-parser.test.ts
More file actions
212 lines (176 loc) Β· 5.71 KB
/
plpgsql-parser.test.ts
File metadata and controls
212 lines (176 loc) Β· 5.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
import { parse, transformSync, deparseSync, loadModule } from '../src';
beforeAll(async () => {
await loadModule();
});
const simpleFunctionSql = `
CREATE OR REPLACE FUNCTION test_func(p_id int)
RETURNS void
LANGUAGE plpgsql
AS $$
DECLARE
v_count int;
BEGIN
v_count := 0;
RAISE NOTICE 'Count: %', v_count;
END;
$$;
`;
const multiStatementSql = `
CREATE TABLE users (id int);
CREATE OR REPLACE FUNCTION get_user(p_id int)
RETURNS int
LANGUAGE plpgsql
AS $$
BEGIN
RETURN p_id;
END;
$$;
CREATE INDEX idx_users_id ON users(id);
`;
describe('plpgsql-parser', () => {
describe('parse', () => {
it('should parse a simple PL/pgSQL function', () => {
const result = parse(simpleFunctionSql);
expect(result.sql).toBeDefined();
expect(result.sql.stmts).toHaveLength(1);
expect(result.items).toHaveLength(1);
expect(result.functions).toHaveLength(1);
const fn = result.functions[0];
expect(fn.kind).toBe('plpgsql-function');
expect(fn.language).toBe('plpgsql');
expect(fn.body.raw).toContain('v_count');
expect(fn.plpgsql.hydrated).toBeDefined();
expect(fn.plpgsql.stats.totalExpressions).toBeGreaterThan(0);
});
it('should parse multi-statement SQL with mixed content', () => {
const result = parse(multiStatementSql);
expect(result.sql.stmts).toHaveLength(3);
expect(result.items).toHaveLength(3);
expect(result.functions).toHaveLength(1);
expect(result.items[0].kind).toBe('stmt');
expect(result.items[1].kind).toBe('plpgsql-function');
expect(result.items[2].kind).toBe('stmt');
});
it('should skip hydration when hydrate=false', () => {
const result = parse(simpleFunctionSql, { hydrate: false });
expect(result.functions).toHaveLength(0);
expect(result.items).toHaveLength(1);
expect(result.items[0].kind).toBe('stmt');
});
});
describe('transformSync', () => {
it('should transform function using callback', () => {
const result = transformSync(simpleFunctionSql, (ctx) => {
const fn = ctx.functions[0];
fn.stmt.funcname[0].String.sval = 'renamed_func';
});
expect(result).toContain('renamed_func');
});
it('should transform function using visitor', () => {
const result = transformSync(simpleFunctionSql, {
onFunction: (fn) => {
fn.stmt.funcname[0].String.sval = 'visitor_renamed';
}
});
expect(result).toContain('visitor_renamed');
});
});
describe('deparseSync', () => {
it('should deparse parsed script back to SQL', () => {
const parsed = parse(simpleFunctionSql);
const result = deparseSync(parsed);
expect(result).toContain('CREATE');
expect(result).toContain('FUNCTION');
expect(result).toContain('test_func');
expect(result).toContain('plpgsql');
});
it('should support pretty printing', () => {
const parsed = parse(simpleFunctionSql);
const result = deparseSync(parsed, { pretty: true });
expect(result).toContain('\n');
});
});
describe('automatic return info handling', () => {
it('should preserve bare RETURN for SETOF functions', () => {
const setofSql = `
CREATE FUNCTION get_users()
RETURNS SETOF users
LANGUAGE plpgsql AS $$
BEGIN
RETURN QUERY SELECT * FROM users;
RETURN;
END;
$$;
`;
const parsed = parse(setofSql);
const result = deparseSync(parsed);
// SETOF functions should keep bare RETURN (not RETURN NULL)
expect(result).toMatch(/RETURN\s*;/);
expect(result).not.toMatch(/RETURN\s+NULL\s*;/);
});
it('should emit RETURN NULL for scalar functions with empty return', () => {
const scalarSql = `
CREATE FUNCTION get_value()
RETURNS int
LANGUAGE plpgsql AS $$
BEGIN
RETURN;
END;
$$;
`;
const parsed = parse(scalarSql);
const result = deparseSync(parsed);
// Scalar functions with empty RETURN should become RETURN NULL
expect(result).toMatch(/RETURN\s+NULL\s*;/);
});
it('should preserve bare RETURN for void functions', () => {
const voidSql = `
CREATE FUNCTION do_something()
RETURNS void
LANGUAGE plpgsql AS $$
BEGIN
RAISE NOTICE 'done';
RETURN;
END;
$$;
`;
const parsed = parse(voidSql);
const result = deparseSync(parsed);
// Void functions should keep bare RETURN
expect(result).toMatch(/RETURN\s*;/);
expect(result).not.toMatch(/RETURN\s+NULL\s*;/);
});
it('should preserve bare RETURN for trigger functions', () => {
const triggerSql = `
CREATE FUNCTION my_trigger()
RETURNS trigger
LANGUAGE plpgsql AS $$
BEGIN
RETURN NEW;
END;
$$;
`;
const parsed = parse(triggerSql);
const result = deparseSync(parsed);
// Trigger functions should work correctly (case-insensitive check)
expect(result.toLowerCase()).toContain('return new');
});
it('should preserve bare RETURN for OUT parameter functions', () => {
const outParamSql = `
CREATE FUNCTION get_info(OUT result text)
RETURNS text
LANGUAGE plpgsql AS $$
BEGIN
result := 'hello';
RETURN;
END;
$$;
`;
const parsed = parse(outParamSql);
const result = deparseSync(parsed);
// OUT parameter functions should keep bare RETURN
expect(result).toMatch(/RETURN\s*;/);
expect(result).not.toMatch(/RETURN\s+NULL\s*;/);
});
});
});