-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdebug_ast.js
More file actions
49 lines (38 loc) · 1.23 KB
/
debug_ast.js
File metadata and controls
49 lines (38 loc) · 1.23 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
import pkg from 'node-sql-parser';
const { Parser } = pkg;
function normalizeTableName(name) {
if (!name) return '';
return name.replace(/^["'`]|["'`]$/g, '').toLowerCase();
}
const parser = new Parser();
const opt = { database: 'mysql' };
const sql1 = `
SELECT "name", "employee_id"
FROM "employees"
ORDER BY "employee_id" ASC
`;
const sql2 = `
SELECT e.name AS name
FROM employees e
ORDER BY e.employee_id ASC
`;
function getAst(sql) {
const result = parser.astify(sql, opt);
return Array.isArray(result) ? result[0] : result;
}
console.log("--- Debugging AST ---");
try {
const ast1 = getAst(sql1);
console.log("AST 1 FROM:", JSON.stringify(ast1.from, null, 2));
console.log("AST 1 Columns:", JSON.stringify(ast1.columns, null, 2));
console.log("AST 1 OrderBy:", JSON.stringify(ast1.orderby, null, 2));
const fromTable = ast1.from[0].table;
console.log("\nTable Name Raw:", fromTable);
const normalizedTable = normalizeTableName(fromTable);
console.log("Table Name Normalized:", normalizedTable);
console.log("\n--- Comparison target ---");
const ast2 = getAst(sql2);
console.log("AST 2 Columns:", JSON.stringify(ast2.columns, null, 2));
} catch (e) {
console.error("Error:", e);
}