|
|
@ -155,6 +155,103 @@ public class SqlParser { |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public List<Map<String,String>> parse(String procSql, String dbType, Integer defaultSystem, String defaultModel) throws Exception { |
|
|
|
|
|
try { |
|
|
|
|
|
String sqlText = ""; |
|
|
|
|
|
List<Map<String,String>> resultList = new ArrayList<>(); |
|
|
|
|
|
List<String> targetSqlList = new ArrayList<>(); |
|
|
|
|
|
String sql = new GenericLogNormalizer().normalizer(procSql,dbType); |
|
|
|
|
|
sql = optDeclare(sql,null); |
|
|
|
|
|
sql = optDeclare2(sql); |
|
|
|
|
|
if(sql.trim().equals("")){ |
|
|
|
|
|
throw new BusinessException("errorSQLparse"); |
|
|
|
|
|
} |
|
|
|
|
|
targetSqlList.add(sql); |
|
|
|
|
|
this.parserContext.setDefaultDb(defaultSystem); |
|
|
|
|
|
this.parserContext.setDefaultSchema(defaultModel); |
|
|
|
|
|
for (String s : targetSqlList) { |
|
|
|
|
|
sqlText = s; |
|
|
|
|
|
if ("ORACLE".equals(dbType)) { |
|
|
|
|
|
if (Objects.equals(dbType, Oracle.toString())) { |
|
|
|
|
|
if (sqlText != null) { |
|
|
|
|
|
sqlText = sqlText.toUpperCase(); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
sqlParser = new TGSqlParser(EDbVendor.dbvoracle); |
|
|
|
|
|
} |
|
|
|
|
|
if ("MYSQL".equals(dbType)) { |
|
|
|
|
|
sqlParser = new TGSqlParser(EDbVendor.dbvmysql); |
|
|
|
|
|
} |
|
|
|
|
|
if ("SQLSERVER".equals(dbType)) { |
|
|
|
|
|
sqlParser = new TGSqlParser(EDbVendor.dbvmssql); |
|
|
|
|
|
} |
|
|
|
|
|
if ("TERADATA".equals(dbType)) { |
|
|
|
|
|
sqlParser = new TGSqlParser(EDbVendor.dbvteradata); |
|
|
|
|
|
} |
|
|
|
|
|
if ("POSTGRESQL".equals(dbType)) { |
|
|
|
|
|
sqlParser = new TGSqlParser(EDbVendor.dbvpostgresql); |
|
|
|
|
|
} |
|
|
|
|
|
if ("DB2".equals(dbType)) { |
|
|
|
|
|
sqlParser = new TGSqlParser(EDbVendor.dbvdb2); |
|
|
|
|
|
} |
|
|
|
|
|
sqlParser.sqltext = sqlText; |
|
|
|
|
|
int ret = sqlParser.parse(); |
|
|
|
|
|
List<KColumn> result = new ArrayList<>(); |
|
|
|
|
|
if (ret == 0) { |
|
|
|
|
|
TStatementList statementList = sqlParser.getSqlstatements(); |
|
|
|
|
|
while (statementList.hasNext()) { |
|
|
|
|
|
TCustomSqlStatement stmt = statementList.next(); |
|
|
|
|
|
parserContext.getTableInCurrentStatement().clear(); |
|
|
|
|
|
switch (stmt.sqlstatementtype) { |
|
|
|
|
|
case sstselect: |
|
|
|
|
|
result = parseSelect(stmt); |
|
|
|
|
|
break; |
|
|
|
|
|
case sstdelete: |
|
|
|
|
|
result = parseDelete(stmt); |
|
|
|
|
|
break; |
|
|
|
|
|
case sstupdate: |
|
|
|
|
|
result = parseUpdate(stmt); |
|
|
|
|
|
break; |
|
|
|
|
|
case sstinsert: |
|
|
|
|
|
result = parseInsert(stmt); |
|
|
|
|
|
break; |
|
|
|
|
|
case sstcreatetable: |
|
|
|
|
|
result = parseCreateTable(stmt); |
|
|
|
|
|
break; |
|
|
|
|
|
case sstcreateview: |
|
|
|
|
|
break; |
|
|
|
|
|
case sstoraclealtertablespace: |
|
|
|
|
|
// parseAlterTable(stmt);
|
|
|
|
|
|
break; |
|
|
|
|
|
case sstdroptable: |
|
|
|
|
|
// parseDropTable(stmt);
|
|
|
|
|
|
break; |
|
|
|
|
|
case sstmerge: |
|
|
|
|
|
log.error("sstmerge[" |
|
|
|
|
|
+ stmt.sqlstatementtype |
|
|
|
|
|
+ "][unknow sql type]sqltext:" |
|
|
|
|
|
); |
|
|
|
|
|
default: |
|
|
|
|
|
log.error("[" + stmt.sqlstatementtype |
|
|
|
|
|
+ "][unknow sql type]sqltext:" |
|
|
|
|
|
); |
|
|
|
|
|
break; |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
} else { |
|
|
|
|
|
log.error(sqlParser.sqltext + sqlParser.getErrormessage()); |
|
|
|
|
|
} |
|
|
|
|
|
if (result.size() > 0) { |
|
|
|
|
|
resultList = exportParseResultUtil.expResult(result); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
return resultList; |
|
|
|
|
|
} catch (Exception e) { |
|
|
|
|
|
e.printStackTrace(); |
|
|
|
|
|
} |
|
|
|
|
|
return null; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
private String optDeclare(String sql,String procName) { |
|
|
private String optDeclare(String sql,String procName) { |
|
|
String returnSql=sql; |
|
|
String returnSql=sql; |
|
|
if(StringUtils.isNotEmpty(sql)){ |
|
|
if(StringUtils.isNotEmpty(sql)){ |
|
|
@ -194,21 +291,36 @@ public class SqlParser { |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
//找出脚本结束的位置
|
|
|
//找出脚本结束的位置
|
|
|
Pattern lastPattern = Pattern.compile("(END)(\\s)+"+("".equals(procName)?";":procName)); |
|
|
if (procName != null && procName.length()>0){ |
|
|
Matcher lastMatcher = lastPattern.matcher(sql); |
|
|
Pattern lastPattern = Pattern.compile("(END)(\\s)+"+("".equals(procName)?";":procName)); |
|
|
if(lastMatcher.find()){ |
|
|
Matcher lastMatcher = lastPattern.matcher(sql); |
|
|
int lastIndex = lastMatcher.start(); |
|
|
if(lastMatcher.find()){ |
|
|
if(sql.contains("BEGIN")){ |
|
|
int lastIndex = lastMatcher.start(); |
|
|
returnSql = sql.substring(sql.indexOf("BEGIN")+5, lastIndex); |
|
|
if(sql.contains("BEGIN")){ |
|
|
|
|
|
returnSql = sql.substring(sql.indexOf("BEGIN")+5, lastIndex); |
|
|
|
|
|
}else{ |
|
|
|
|
|
returnSql = sql.substring(0, lastIndex); |
|
|
|
|
|
} |
|
|
}else{ |
|
|
}else{ |
|
|
returnSql = sql.substring(0, lastIndex); |
|
|
Pattern tempPattern = Pattern.compile("(END)(\\s)*;"); |
|
|
|
|
|
Matcher tempMatcher = tempPattern.matcher(sql); |
|
|
|
|
|
Pattern tempPatternBegin = Pattern.compile("(\\s)(BEGIN)(\\s)"); |
|
|
|
|
|
Matcher tempMatcherBegin = tempPatternBegin.matcher(sql); |
|
|
|
|
|
|
|
|
|
|
|
if(tempMatcher.find()){ |
|
|
|
|
|
int tempIndex = tempMatcher.start(); |
|
|
|
|
|
int tempIndexBegin = 0; |
|
|
|
|
|
if(tempMatcherBegin.find()){ |
|
|
|
|
|
tempIndexBegin = tempMatcherBegin.start(); |
|
|
|
|
|
} |
|
|
|
|
|
returnSql = sql.substring(tempIndexBegin+6, tempIndex); |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
}else{ |
|
|
}else { |
|
|
Pattern tempPattern = Pattern.compile("(END)(\\s)*;"); |
|
|
Pattern tempPattern = Pattern.compile("(END)(\\s)*;"); |
|
|
Matcher tempMatcher = tempPattern.matcher(sql); |
|
|
Matcher tempMatcher = tempPattern.matcher(sql); |
|
|
Pattern tempPatternBegin = Pattern.compile("(\\s)(BEGIN)(\\s)"); |
|
|
Pattern tempPatternBegin = Pattern.compile("(\\s)(BEGIN)(\\s)"); |
|
|
Matcher tempMatcherBegin = tempPatternBegin.matcher(sql); |
|
|
Matcher tempMatcherBegin = tempPatternBegin.matcher(sql); |
|
|
|
|
|
|
|
|
if(tempMatcher.find()){ |
|
|
if(tempMatcher.find()){ |
|
|
int tempIndex = tempMatcher.start(); |
|
|
int tempIndex = tempMatcher.start(); |
|
|
int tempIndexBegin = 0; |
|
|
int tempIndexBegin = 0; |
|
|
@ -218,7 +330,6 @@ public class SqlParser { |
|
|
returnSql = sql.substring(tempIndexBegin+6, tempIndex); |
|
|
returnSql = sql.substring(tempIndexBegin+6, tempIndex); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
} |
|
|
} |
|
|
return returnSql; |
|
|
return returnSql; |
|
|
} |
|
|
} |
|
|
@ -250,7 +361,6 @@ public class SqlParser { |
|
|
return returnVal.toString(); |
|
|
return returnVal.toString(); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private List<KColumn> parseAlterTable(TCustomSqlStatement stmt) { |
|
|
private List<KColumn> parseAlterTable(TCustomSqlStatement stmt) { |
|
|
TAlterTableStatement alterTableStatement=(TAlterTableStatement)stmt; |
|
|
TAlterTableStatement alterTableStatement=(TAlterTableStatement)stmt; |
|
|
AlterParser parser=new AlterParser(alterTableStatement,parserContext); |
|
|
AlterParser parser=new AlterParser(alterTableStatement,parserContext); |
|
|
|