2024-03-27 10:33:25 +08:00
|
|
|
import { MySQL } from 'src/parser/mysql';
|
2024-01-19 21:10:00 +08:00
|
|
|
import { MySqlParserVisitor } from 'src/lib/mysql/MySqlParserVisitor';
|
2020-09-11 17:39:10 +08:00
|
|
|
|
2023-11-27 15:25:40 +08:00
|
|
|
describe('MySQL Visitor Tests', () => {
|
2020-09-11 17:39:10 +08:00
|
|
|
const expectTableName = 'user1';
|
|
|
|
const sql = `select id,name,sex from ${expectTableName};`;
|
2024-03-27 10:33:25 +08:00
|
|
|
const mysql = new MySQL();
|
2020-09-11 17:39:10 +08:00
|
|
|
|
2024-03-27 10:33:25 +08:00
|
|
|
const parseTree = mysql.parse(sql, (error) => {
|
2024-02-26 20:25:09 +08:00
|
|
|
console.error('Parse error:', error);
|
2020-09-11 17:39:10 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
test('Visitor visitTableName', () => {
|
2024-03-27 19:04:16 +08:00
|
|
|
class MyVisitor extends MySqlParserVisitor<string> {
|
|
|
|
defaultResult(): string {
|
|
|
|
return '';
|
2023-05-04 10:13:05 +08:00
|
|
|
}
|
2024-03-27 19:04:16 +08:00
|
|
|
aggregateResult(aggregate: string, nextResult: string): string {
|
|
|
|
return aggregate + nextResult;
|
|
|
|
}
|
|
|
|
visitProgram = (ctx) => {
|
|
|
|
return this.visitChildren(ctx);
|
|
|
|
};
|
|
|
|
visitTableName = (ctx) => {
|
|
|
|
return ctx.getText().toLowerCase();
|
2023-10-13 11:16:36 +08:00
|
|
|
};
|
2020-09-11 17:39:10 +08:00
|
|
|
}
|
2023-05-04 10:13:05 +08:00
|
|
|
const visitor = new MyVisitor();
|
2024-03-27 19:04:16 +08:00
|
|
|
const result = visitor.visit(parseTree);
|
2020-09-11 17:39:10 +08:00
|
|
|
|
|
|
|
expect(result).toBe(expectTableName);
|
|
|
|
});
|
|
|
|
});
|