2020-09-11 17:39:10 +08:00
|
|
|
import SQLParser, { HiveSqlVisitor } from '../../../src/parser/hive';
|
|
|
|
|
|
|
|
describe('Generic SQL Visitor Tests', () => {
|
2020-11-25 17:04:55 +08:00
|
|
|
const expectTableName = 'dm_gis.dlv_addr_tc_count';
|
|
|
|
const sql = `select citycode,tc,inc_day from ${expectTableName} where inc_day='20190501' limit 100;`;
|
2020-09-11 17:39:10 +08:00
|
|
|
const parser = new SQLParser();
|
|
|
|
|
|
|
|
const parserTree = parser.parse(sql, (error) => {
|
|
|
|
console.log('Parse error:', error);
|
|
|
|
});
|
|
|
|
|
2020-11-25 17:04:55 +08:00
|
|
|
// console.log('Parser tree string:', parser.toString(parserTree));
|
2020-09-11 17:39:10 +08:00
|
|
|
|
|
|
|
test('Visitor visitTableName', () => {
|
|
|
|
let result = '';
|
|
|
|
class MyVisitor extends HiveSqlVisitor {
|
|
|
|
visitTable_name(ctx): void {
|
|
|
|
result = ctx.getText().toLowerCase();
|
|
|
|
super.visitTable_name(ctx);
|
|
|
|
}
|
|
|
|
}
|
2020-11-25 17:04:55 +08:00
|
|
|
|
2020-09-11 17:39:10 +08:00
|
|
|
const visitor: any = new MyVisitor();
|
|
|
|
visitor.visit(parserTree);
|
|
|
|
|
|
|
|
expect(result).toBe(expectTableName);
|
|
|
|
});
|
|
|
|
});
|