lava-oushudb-dt-sql-parser/lib/parser.js

45 lines
1.1 KiB
JavaScript
Raw Normal View History

2018-07-02 18:01:01 +08:00
const Parser = require('../core/astParser');
const Cache = require("../core/cache");
2018-08-27 11:03:13 +08:00
const sqlSyntaxParser = require("../core/sqlSyntaxParser");
const sqlAutoCompleteParser = require("../core/sqlAutoCompleteParser");
2018-07-02 18:01:01 +08:00
const filter = require("./filter");
2018-08-27 11:03:13 +08:00
const astCache = new Cache();
2018-07-02 18:01:01 +08:00
2018-08-27 11:03:13 +08:00
function parse(sql) {
const cleanSql = filter.cleanSql(sql);
2018-07-02 18:01:01 +08:00
console.log(cleanSql)
2018-08-27 11:03:13 +08:00
let ast = astCache.get(cleanSql);
if (ast) {
2018-07-02 18:01:01 +08:00
return ast
2018-08-27 11:03:13 +08:00
} else {
ast = Parser.parse(cleanSql).ast;
astCache.set(cleanSql, ast);
2018-07-02 18:01:01 +08:00
return ast;
}
}
2018-08-16 18:02:31 +08:00
/**
* sql sql
* type 语法类型默认hive
* return 返回解析对象
*/
2018-08-27 11:03:13 +08:00
function parseSyntax(sql, type) {
if (typeof type == "undefined") {
type = "hive"
2018-08-16 18:02:31 +08:00
}
2018-08-27 11:03:13 +08:00
return sqlSyntaxParser.parser.parseSyntax(sql, '', type, false)
2018-08-16 18:02:31 +08:00
}
2018-07-02 18:01:01 +08:00
2018-08-27 11:03:13 +08:00
function parserSql(sql, type) {
if (typeof type == "undefined") {
type = "hive"
}
return sqlAutoCompleteParser.parser.parseSql(sql, '', type, false)
}
exports.parse = parse;
exports.parseSyntax = parseSyntax;
exports.parserSql = parserSql;