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

62 lines
1.4 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-28 11:35:00 +08:00
let sql1=sql;
let sql2='';
if(sql instanceof Array){
sql1=sql[0];
sql2=sql[1];
}
return sqlSyntaxParser.parser.parseSyntax(sql1, sql2, type, false)
2018-08-16 18:02:31 +08:00
}
2018-07-02 18:01:01 +08:00
2018-10-08 17:24:17 +08:00
/**
* 自动补全提示
* @param {(string | array)} sql
* @param {string=} type
*/
2018-08-27 11:03:13 +08:00
function parserSql(sql, type) {
if (typeof type == "undefined") {
type = "hive"
}
2018-08-28 11:35:00 +08:00
let sql1=sql;
let sql2='';
if(sql instanceof Array){
sql1=sql[0];
sql2=sql[1];
}
return sqlAutoCompleteParser.parser.parseSql(sql1, sql2, type, false)
2018-08-27 11:03:13 +08:00
}
exports.parse = parse;
exports.parseSyntax = parseSyntax;
exports.parserSql = parserSql;