|
| 1 | +import { loadModule, parsePlPgSQLSync } from '@libpg-query/parser'; |
| 2 | +import * as fs from 'fs'; |
| 3 | +import * as path from 'path'; |
| 4 | +import { hydratePlpgsqlAst, dehydratePlpgsqlAst, PLpgSQLParseResult, deparseSync } from '../src'; |
| 5 | + |
| 6 | +describe('hydrate demonstration with big-function.sql', () => { |
| 7 | + beforeAll(async () => { |
| 8 | + await loadModule(); |
| 9 | + }); |
| 10 | + |
| 11 | + it('should parse, hydrate, modify, and deparse big-function.sql', () => { |
| 12 | + const fixturePath = path.join(__dirname, '../../../__fixtures__/plpgsql-pretty/big-function.sql'); |
| 13 | + const sql = fs.readFileSync(fixturePath, 'utf-8'); |
| 14 | + |
| 15 | + const parsed = parsePlPgSQLSync(sql) as unknown as PLpgSQLParseResult; |
| 16 | + |
| 17 | + console.log('\n=== HYDRATION STATS ==='); |
| 18 | + const { ast: hydratedAst, errors, stats } = hydratePlpgsqlAst(parsed); |
| 19 | + console.log('Total expressions:', stats.totalExpressions); |
| 20 | + console.log('Parsed expressions:', stats.parsedExpressions); |
| 21 | + console.log('Assignment expressions:', stats.assignmentExpressions); |
| 22 | + console.log('SQL expressions:', stats.sqlExpressions); |
| 23 | + console.log('Failed expressions:', stats.failedExpressions); |
| 24 | + console.log('Raw expressions:', stats.rawExpressions); |
| 25 | + |
| 26 | + if (errors.length > 0) { |
| 27 | + console.log('\nErrors:', errors.slice(0, 5)); |
| 28 | + } |
| 29 | + |
| 30 | + console.log('\n=== SAMPLE HYDRATED EXPRESSIONS ==='); |
| 31 | + const sampleExprs = collectHydratedExprs(hydratedAst, 5); |
| 32 | + sampleExprs.forEach((expr, i) => { |
| 33 | + console.log(`\n[${i + 1}] Kind: ${expr.kind}`); |
| 34 | + console.log(` Original: "${expr.original}"`); |
| 35 | + if (expr.kind === 'assign') { |
| 36 | + console.log(` Target: "${expr.target}"`); |
| 37 | + console.log(` Value: "${expr.value}"`); |
| 38 | + console.log(` Has targetExpr: ${!!expr.targetExpr}`); |
| 39 | + console.log(` Has valueExpr: ${!!expr.valueExpr}`); |
| 40 | + } else if (expr.kind === 'sql-expr') { |
| 41 | + console.log(` Has expr AST: ${!!expr.expr}`); |
| 42 | + } |
| 43 | + }); |
| 44 | + |
| 45 | + console.log('\n=== MODIFYING AST ==='); |
| 46 | + const modifiedAst = modifyAst(JSON.parse(JSON.stringify(hydratedAst))); |
| 47 | + |
| 48 | + console.log('\n=== DEHYDRATING MODIFIED AST ==='); |
| 49 | + const dehydratedAst = dehydratePlpgsqlAst(modifiedAst); |
| 50 | + |
| 51 | + console.log('\n=== DEPARSING DEHYDRATED AST ==='); |
| 52 | + const deparsed = deparseSync(dehydratedAst); |
| 53 | + |
| 54 | + console.log('\n=== VERIFICATION: Changes Applied ==='); |
| 55 | + expect(deparsed).toContain('v_discount_MODIFIED'); |
| 56 | + expect(deparsed).toContain('v_tax_MODIFIED'); |
| 57 | + expect(deparsed).toContain('888'); |
| 58 | + |
| 59 | + console.log('Found v_discount_MODIFIED:', deparsed.includes('v_discount_MODIFIED')); |
| 60 | + console.log('Found v_tax_MODIFIED:', deparsed.includes('v_tax_MODIFIED')); |
| 61 | + console.log('Found 888 (modified default values):', deparsed.includes('888')); |
| 62 | + |
| 63 | + console.log('\n=== DEPARSED OUTPUT (first 2000 chars) ==='); |
| 64 | + console.log(deparsed.substring(0, 2000)); |
| 65 | + |
| 66 | + expect(stats.totalExpressions).toBeGreaterThan(0); |
| 67 | + expect(stats.parsedExpressions).toBeGreaterThan(0); |
| 68 | + }); |
| 69 | +}); |
| 70 | + |
| 71 | +function collectHydratedExprs(obj: any, limit: number): any[] { |
| 72 | + const results: any[] = []; |
| 73 | + |
| 74 | + function walk(node: any): void { |
| 75 | + if (results.length >= limit) return; |
| 76 | + if (node === null || node === undefined) return; |
| 77 | + |
| 78 | + if (typeof node === 'object') { |
| 79 | + if ('PLpgSQL_expr' in node) { |
| 80 | + const query = node.PLpgSQL_expr.query; |
| 81 | + if (query && typeof query === 'object' && 'kind' in query) { |
| 82 | + results.push(query); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + for (const value of Object.values(node)) { |
| 87 | + walk(value); |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + if (Array.isArray(node)) { |
| 92 | + for (const item of node) { |
| 93 | + walk(item); |
| 94 | + } |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + walk(obj); |
| 99 | + return results; |
| 100 | +} |
| 101 | + |
| 102 | +function modifyAst(ast: any): any { |
| 103 | + let modCount = 0; |
| 104 | + let assignModCount = 0; |
| 105 | + |
| 106 | + function walk(node: any): void { |
| 107 | + if (node === null || node === undefined) return; |
| 108 | + |
| 109 | + if (typeof node === 'object') { |
| 110 | + if ('PLpgSQL_expr' in node) { |
| 111 | + const query = node.PLpgSQL_expr.query; |
| 112 | + |
| 113 | + if (typeof query === 'object' && query.kind === 'assign') { |
| 114 | + if (query.target === 'v_discount' && assignModCount === 0) { |
| 115 | + console.log(` Modifying assignment target: "${query.target}" -> "v_discount_MODIFIED"`); |
| 116 | + query.target = 'v_discount_MODIFIED'; |
| 117 | + assignModCount++; |
| 118 | + modCount++; |
| 119 | + } |
| 120 | + if (query.target === 'v_tax' && assignModCount === 1) { |
| 121 | + console.log(` Modifying assignment target: "${query.target}" -> "v_tax_MODIFIED"`); |
| 122 | + query.target = 'v_tax_MODIFIED'; |
| 123 | + assignModCount++; |
| 124 | + modCount++; |
| 125 | + } |
| 126 | + if (query.value === '0' && modCount < 5) { |
| 127 | + console.log(` Modifying assignment value: "${query.value}" -> "999"`); |
| 128 | + query.value = '999'; |
| 129 | + modCount++; |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + if (typeof query === 'object' && query.kind === 'sql-expr') { |
| 134 | + if (query.original === '0' && modCount < 8) { |
| 135 | + console.log(` Modifying sql-expr value: "${query.original}" -> "888"`); |
| 136 | + query.original = '888'; |
| 137 | + modCount++; |
| 138 | + } |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + for (const value of Object.values(node)) { |
| 143 | + walk(value); |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + if (Array.isArray(node)) { |
| 148 | + for (const item of node) { |
| 149 | + walk(item); |
| 150 | + } |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + walk(ast); |
| 155 | + console.log(` Total modifications: ${modCount}`); |
| 156 | + return ast; |
| 157 | +} |
0 commit comments