use of com.dtstack.taier.develop.sql.Table in project Taier by DTStack.
the class InsertNodeParser method parseSqlTable.
@Override
public void parseSqlTable(Node node, Set<Table> tables) {
if (node instanceof InsertNode) {
Table table = new Table();
table.setName(((InsertNode) node).getTargetTable().getTable());
table.setDb(((InsertNode) node).getTargetTable().getDb());
table.setOperate(TableOperateEnum.INSERT);
tables.add(table);
SelectNodeParser selectNodeParser = new SelectNodeParser();
selectNodeParser.parseSqlTable(((InsertNode) node).getSource(), tables);
}
}
use of com.dtstack.taier.develop.sql.Table in project Taier by DTStack.
the class LibraUglySqlHandler method parserUglySql.
public ParseResult parserUglySql() {
ParseResult parseResult = new ParseResult();
String parsedSql = parseUglySql();
parseResult.setStandardSql(parsedSql);
// 针对comment_on特殊处理
if ((sqlMode & COMMENT_ON_TABLE) == COMMENT_ON_TABLE) {
parseResult.setSqlType(SqlType.COMMENT_ON);
Matcher matcher = COMMENT_ON_TABLE_PATTERN.matcher(sql);
if (matcher.find()) {
String db = matcher.group("db");
if (StringUtils.isNotEmpty(db)) {
parseResult.setCurrentDb(db);
}
String table = matcher.group("table");
if (StringUtils.isNotEmpty(table)) {
Table table1 = new Table();
table1.setName(table);
parseResult.setMainTable(table1);
}
}
} else if ((sqlMode & COMMENT_ON_COLUMN) == COMMENT_ON_COLUMN) {
parseResult.setSqlType(SqlType.COMMENT_ON);
Matcher matcher = COMMENT_ON_COLUMN_PATTERN.matcher(sql);
if (matcher.find()) {
String columnStr = matcher.group("column");
String[] split = columnStr.split("\\.");
if (split.length == 3) {
parseResult.setCurrentDb(split[0]);
Table table = new Table();
table.setName(split[1]);
parseResult.setMainTable(table);
} else if (split.length == 2) {
Table table = new Table();
table.setName(split[0]);
parseResult.setMainTable(table);
}
}
}
return parseResult;
}
use of com.dtstack.taier.develop.sql.Table in project Taier by DTStack.
the class AstNodeParser method parseInvalidateTable.
/**
* 从invalidate 语句中刷新表信息
*/
private ParseResult parseInvalidateTable(ParseResult result) {
Map<String, String> dbTable = SqlRegexUtil.parseDbTableFromInvalidateSql(result.getStandardSql());
Table table = result.getMainTable();
if (Objects.isNull(table)) {
table = new Table();
}
table.setDb(dbTable.get(SqlRegexUtil.KEY_DB) == null ? result.getCurrentDb() : dbTable.get(SqlRegexUtil.KEY_DB));
table.setName(dbTable.get(SqlRegexUtil.KEY_TABLE));
table.setOperate(TableOperateEnum.INVALIDATE_TABLE);
if (Objects.isNull(result.getTables())) {
result.setTables(new ArrayList<>());
}
result.getTables().add(table);
result.setMainTable(table);
result.setSqlType(SqlType.INVALIDATE_TABLE);
return result;
}
use of com.dtstack.taier.develop.sql.Table in project Taier by DTStack.
the class AstNodeParser method getParseResult.
/**
* 进行语法树转换 和 血缘解析的具体类
*
* @param originSql
* @param currentDb
* @param tableColumnsMap
* @param parseResult
* @param root
* @return
*/
private ParseResult getParseResult(String originSql, String currentDb, Map<String, List<Column>> tableColumnsMap, ParseResult parseResult, ASTNode root) {
NodeParser nodeParser = ASTNodeLineageParser.ParserProxy.getParser((ASTNode) root.getChild(0), originSql);
if (Objects.isNull(nodeParser)) {
return parseResult;
}
if (nodeParser instanceof OtherNodeParser) {
((OtherNodeParser) nodeParser).getSqlType((ASTNode) root.getChild(0), parseResult);
return parseResult;
}
com.dtstack.taier.develop.sql.node.Node node = nodeParser.parseSql((ASTNode) root.getChild(0), currentDb, tableColumnsMap, new HashMap<>());
if (Objects.isNull(node)) {
return parseResult;
}
LineageParser lineageParser = LineageParser.HiveParserProxy.getParser(node, currentDb, tableColumnsMap);
if (lineageParser != null) {
List<ColumnLineage> columnLineages = getColumnLineages(node, lineageParser);
parseResult.setColumnLineages(columnLineages);
}
getMainTable(node, parseResult, root);
try {
Set<Table> tables = new HashSet<>();
nodeParser.parseSqlTable(node, tables);
parseResult.setTables(new ArrayList<>(tables));
} catch (Exception e) {
e.printStackTrace();
}
return parseResult;
}
use of com.dtstack.taier.develop.sql.Table in project Taier by DTStack.
the class AstNodeParser method parseSql.
/**
* 解析sql
*/
@Override
public ParseResult parseSql(String originSql, String currentDb, Map<String, List<Column>> tableColumnsMap) throws Exception {
ParseResult parseResult = new ParseResult();
parseResult.setCurrentDb(currentDb);
parseResult.setOriginSql(originSql);
parseResult.setMainTable(new Table());
ASTNode root;
originSql = SqlFormatUtil.formatSql(originSql);
// 解析和去掉sql中的生命周期和类目信息
parseLifecycleAndCatalogue(parseResult);
SqlFormatUtil.checkSql(parseResult.getStandardSql());
if (isNotParse(originSql, parseResult)) {
return parseResult;
}
if (SqlRegexUtil.isCreateTemp(parseResult.getStandardSql())) {
parseResult.setStandardSql(SqlRegexUtil.removeTempKey(parseResult.getStandardSql()));
parseResult.setExtraSqlType(SqlType.CREATE_TEMP);
}
String formattedSql = this.uglySqlHandler.parseUglySql(originSql);
if (StringUtils.isEmpty(formattedSql)) {
return parseResult;
}
try {
root = parseDriver.parse(formattedSql);
} catch (ParseException e) {
LOG.error("sql解析异常========>>>>:{}", e.getMessage());
// 用正则匹配一下
ParseResult p = SqlTypeRegexUtil.getParseResultByRegexSql(parseResult);
return p;
}
return getParseResult(originSql, currentDb, tableColumnsMap, parseResult, root);
}
Aggregations