sql解析工具:sqlParser ,GitHub地址 https://github.com/JSQLParser/JSqlParser
本次演示版本:http://search.maven.org/remotecontent?filepath=com/github/jsqlparser/jsqlparser/0.9.1/jsqlparser-0.9.1.jar
转换工具类(先导入下载的jar包):sqlServerParse.java
package com.netIDe.plugin;import java.util.List;import net.sf.jsqlparser.JsqlParserException;import net.sf.jsqlparser.parser.CCJsqlParserUtil;import net.sf.jsqlparser.statement.Statement;import net.sf.jsqlparser.statement.select.FromItem;import net.sf.jsqlparser.statement.select.Join;import net.sf.jsqlparser.statement.select.LateralSubSelect;import net.sf.jsqlparser.statement.select.OrderByElement;import net.sf.jsqlparser.statement.select.PlainSelect;import net.sf.jsqlparser.statement.select.Select;import net.sf.jsqlparser.statement.select.Selectbody;import net.sf.jsqlparser.statement.select.SetoperationList;import net.sf.jsqlparser.statement.select.SubJoin;import net.sf.jsqlparser.statement.select.SubSelect;import net.sf.jsqlparser.statement.select.ValuesList;import net.sf.jsqlparser.statement.select.WithItem;public class sqlServerParse { public String removeOrderBy(String sql) throws JsqlParserException { Statement stmt = CCJsqlParserUtil.parse(sql); Select select = (Select) stmt; Selectbody selectbody = select.getSelectbody(); processSelectbody(selectbody); return select.toString(); } public voID processSelectbody(Selectbody selectbody) { if (selectbody instanceof PlainSelect) { processplainSelect((PlainSelect) selectbody); } else if (selectbody instanceof WithItem) { WithItem withItem = (WithItem) selectbody; if (withItem.getSelectbody() != null) { processSelectbody(withItem.getSelectbody()); } } else { SetoperationList operationList = (SetoperationList) selectbody; if (operationList.getPlainSelects() != null && operationList.getPlainSelects().size() > 0) { List<PlainSelect> plainSelects = operationList.getPlainSelects(); for (PlainSelect plainSelect : plainSelects) { processplainSelect(plainSelect); } } if (!orderByHashParameters(operationList.getorderByElements())) { operationList.setorderByElements(null); } } } public voID processplainSelect(PlainSelect plainSelect) { if (!orderByHashParameters(plainSelect.getorderByElements())) { plainSelect.setorderByElements(null); } if (plainSelect.getFromItem() != null) { processFromItem(plainSelect.getFromItem()); } if (plainSelect.getJoins() != null && plainSelect.getJoins().size() > 0) { List<Join> joins = plainSelect.getJoins(); for (Join join : joins) { if (join.getRightItem() != null) { processFromItem(join.getRightItem()); } } } } public voID processFromItem(FromItem fromItem) { if (fromItem instanceof SubJoin) { SubJoin subJoin = (SubJoin) fromItem; if (subJoin.getJoin() != null) { if (subJoin.getJoin().getRightItem() != null) { processFromItem(subJoin.getJoin().getRightItem()); } } if (subJoin.getleft() != null) { processFromItem(subJoin.getleft()); } } else if (fromItem instanceof SubSelect) { SubSelect subSelect = (SubSelect) fromItem; if (subSelect.getSelectbody() != null) { processSelectbody(subSelect.getSelectbody()); } } else if (fromItem instanceof ValuesList) { } else if (fromItem instanceof LateralSubSelect) { LateralSubSelect lateralSubSelect = (LateralSubSelect) fromItem; if (lateralSubSelect.getSubSelect() != null) { SubSelect subSelect = (SubSelect) (lateralSubSelect.getSubSelect()); if (subSelect.getSelectbody() != null) { processSelectbody(subSelect.getSelectbody()); } } } } public boolean orderByHashParameters(List<OrderByElement> orderByElements) { if (orderByElements == null) { return false; } for (OrderByElement orderByElement : orderByElements) { if (orderByElement.toString().toupperCase().contains("?")) { return true; } } return false; }}
调用很简单,一句话搞定:
String newsql = sqlServerParse.class.newInstance().removeOrderBy(oldsql); //去除ORDER BY总结
以上是内存溢出为你收集整理的SQLServer去除子查询中的order by全部内容,希望文章能够帮你解决SQLServer去除子查询中的order by所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)