/**
主程序入口
**/
@Test
public void testJsqlParserSql() throws Exception {
String sql = SqlConfig.HIGHT4;
Statement statement = CCJSqlParserUtil.parse(sql);
parseStatement(statement);
System.out.println("sql:" + statement.toString());
}
private void parseItemsList(ItemsList itemsList) {
if (itemsList instanceof SubSelect) {
parseFromItem((SubSelect) itemsList);
} else if (itemsList instanceof ExpressionList) {
((ExpressionList) itemsList).getExpressions().forEach(expression -> parseExpression(expression));
} else {
System.out.println(itemsList.getClass() + ":遇到无法解析的对象:" + Thread.currentThread().getStackTrace()[1].getMethodName());
}
}
private void parseStatement(Statement statement) {
if (statement instanceof Select) {
parseSelectBody(((Select) statement).getSelectBody());
} else if (statement instanceof SubSelect) {
parseItemsList((SubSelect) statement);
} else {
System.out.println(statement.getClass() + ":遇到无法解析的对象:" + Thread.currentThread().getStackTrace()[1].getMethodName());
}
}
private void parseSelectBody(SelectBody selectBody) {
if (selectBody instanceof PlainSelect) {
PlainSelect plainSelect = (PlainSelect) selectBody;
// 主查询
parseFromItem(plainSelect.getFromItem());
// 连接
parseJoin(plainSelect.getJoins());
// 条件
parseExpression(plainSelect.getWhere());
// 子查询
List<SelectItem> selectItems = plainSelect.getSelectItems();
if (selectItems.size() > 0) {
selectItems.forEach(selectItem -> {
parseSelectItem(selectItem);
});
}
whereAdd(plainSelect);
} else if (selectBody instanceof SetOperationList) {
List<SelectBody> selectBodyList = ((SetOperationList) selectBody).getSelects();
selectBodyList.forEach(select -> {
parseSelectBody(select);
});
return;
} else {
System.out.println(selectBody.getClass() + ":遇到无法解析的对象:" + Thread.currentThread().getStackTrace()[1].getMethodName());
}
}
private void parseJoin(List<Join> joins) {
if (null != joins && joins.size() > 0) {
joins.forEach(join -> {
parseFromItem(join.getRightItem());
});
}
}
/**
* 拼接条件
*
* @param plainSelect
*/
private void whereAdd(PlainSelect plainSelect) {
Expression expression = plainSelect.getWhere();
String alias = getAlias(plainSelect.getFromItem());
/* Expression ex = null;*/
EqualsTo equalsTo = new EqualsTo();
equalsTo.setLeftExpression(new Column(alias + "." + "is_delete"));
equalsTo.setRightExpression(new LongValue("0"));
/* try {
ex = CCJSqlParserUtil.parseCondExpression(alias + "." + "is_delete ='0'");
} catch (Exception e) {
e.printStackTrace();
}*/
AndExpression andExpression = new AndExpression(expression, equalsTo);
if (expression != null) {
plainSelect.setWhere(andExpression);
} else {
plainSelect.setWhere(equalsTo);
}
}
private void parseExpression(Expression expression) {
if (expression instanceof Column || expression instanceof LongValue || expression instanceof StringValue) {
return;
}
if (expression instanceof Function) {
return;
}
if (expression instanceof MinorThanEquals) {
return;
} if (expression instanceof JdbcParameter) {
return;
}
if (expression instanceof GreaterThanEquals) {
return;
}
if (expression instanceof Parenthesis) {
return;
}
if (expression instanceof EqualsTo) {
EqualsTo equalsTo = (EqualsTo) expression;
Expression leftExpression = equalsTo.getLeftExpression();
Expression rightExpression = equalsTo.getRightExpression();
parseExpression(leftExpression);
parseExpression(rightExpression);
} else if (expression instanceof AndExpression) {
AndExpression andExpression = (AndExpression) expression;
parseExpression(andExpression.getLeftExpression());
parseExpression(andExpression.getRightExpression());
} else if (expression instanceof InExpression) {
InExpression inExpression = (InExpression) expression;
Expression leftExpression = inExpression.getLeftExpression();
ItemsList itemsList = inExpression.getRightItemsList();
parseItemsList(itemsList);
parseExpression(leftExpression);
} else if (expression instanceof SubSelect) {
parseFromItem((SubSelect) expression);
} else {
if (expression != null) {
System.out.println(expression.getClass() + ":遇到无法解析的对象:" + Thread.currentThread().getStackTrace()[1].getMethodName());
}
}
}
private void parseSelectItem(SelectItem selectItem) {
if (selectItem instanceof AllTableColumns || selectItem instanceof AllColumns) {
return;
}
if (selectItem instanceof SelectExpressionItem) {
SelectExpressionItem selectExpressionItem = (SelectExpressionItem) selectItem;
parseExpression(selectExpressionItem.getExpression());
} else {
System.out.println(selectItem.getClass() + ":遇到无法解析的对象:" + Thread.currentThread().getStackTrace()[1].getMethodName());
}
}
/**
* 获取别名
*
* @return
*/
private String getAlias(FromItem fromItem) {
if (null != fromItem.getAlias() && null != fromItem.getAlias().getName()) {
return fromItem.getAlias().getName();
}
return fromItem.toString();
}
private void parseFromItem(FromItem fromItem) {
if (fromItem instanceof Table) {
return;
}
if (fromItem instanceof SubSelect) {
SubSelect subSelect = (SubSelect) fromItem;
parseSelectBody(subSelect.getSelectBody());
} else {
System.out.println("遇到无法解析的对象:" + Thread.currentThread().getStackTrace()[1].getMethodName());
}
}
jsqlParse分析SQL
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 一. 数据库与SQL语言简介简介 1.1 SQL:与数据库交互的语言 SQL的全称是结构化查询语言(Structu...