jsqlparser不仅可以解析完整的SQL语句也可以用来解析SQL语法片段:
如下示例,我们将"ORDER BY column"
这个用于定义排序的ORDER BY语句解析为
net.sf.jsqlparser.statement.select.OrderByElement
对象
@Test
public void test14ParseOrderByElements() {
try {
/** 创建解析器对象 */
CCJSqlParser parser = new CCJSqlParser(new StringProvider("ORDER BY column"));
/** 调用 OrderByElements 方法返回 net.sf.jsqlparser.statement.select.OrderByElement 对象 */
System.out.printf("elements:%s\n",parser.OrderByElements());
} catch (ParseException e) {
e.printStackTrace();
assertTrue(false);
}
}
对每个SQL部件,CCJSqlParser都有一个对应的无参解析方法,一般情况下,这个方法名是解析对象的类名。所以我们可以基于此将这个解析SQL语句部件的方法改造一下变成一个通用方法:
/**
* 调用{@link CCJSqlParser}解析SQL语句部件返回解析生成的对象,如{@code 'ORDER BY id DESC'}
* @param <T>
* @param input
* @param method 指定调用的{@link CCJSqlParser}解析方法
* @param targetType 返回的解析对象类型
* @return
* @since 3.18.3
*/
public static <T> T parseComponent(String input,String method,Class<T> targetType){
try {
CCJSqlParser parser = new CCJSqlParser(new StringProvider(input));
try {
return checkNotNull(targetType,"targetType is null").cast(parser.getClass().getMethod(method).invoke(parser));
} catch (InvocationTargetException e) {
Throwables.throwIfUnchecked(e.getTargetException());
throw new RuntimeException(e.getTargetException());
}
} catch (IllegalAccessException | NoSuchMethodException | SecurityException e) {
Throwables.throwIfUnchecked(e);
throw new RuntimeException(e);
}
}
/**
* 调用{@link CCJSqlParser}解析SQL语句部件返回解析生成的对象,
* 使用{@code targetType}的类名作为解析方法名
* @see #parseComponent(String, String, Class)
* @since 3.18.3
*/
public static <T> T parseComponent(String input,Class<T> targetType){
return parseComponent(input,
checkNotNull(targetType,"targetType is null").getSimpleName(),
targetType);
}
调用示例:
@Test
public void test15ParseOrderByElement() {
try {
OrderByElement orderByElement= ParserSupport.parseComponent("column desc", OrderByElement.class);
System.out.printf("element:%s\n",orderByElement);
} catch (ParseException e) {
e.printStackTrace();
assertTrue(false);
}
}
应用示例:
/**
*
* 将指定的字段加入SQL语句ORDER BY 排序字段,作为最优先排序字段
* @param sql
* @param column
* @return 返回加入column排序的SQL语句
*/
private String addOrderByColumn(String sql,String column) {
if(!isNullOrEmpty(sql) && !isNullOrEmpty(column)) {
Select select = ParserSupport.parseSelect(sql);
if(select.getSelectBody() instanceof PlainSelect) {
PlainSelect plainSelect = (PlainSelect) select.getSelectBody();
List<OrderByElement> orderByElements = plainSelect.getOrderByElements();
if(null == orderByElements) {
orderByElements = Lists.newArrayListWithCapacity(1);
plainSelect.setOrderByElements(orderByElements);
}
OrderByElement orderBy = parseComponent(column,OrderByElement.class);
orderByElements.add(0,orderBy);
return select.toString();
}
}
return sql;
}
完整代码参见我的码云仓库:
https://gitee.com/l0km/sql2java/blob/dev/sql2java-manager/src/main/java/gu/sql2java/parser/ParserSupport.java
测试代码
https://gitee.com/l0km/sql2java/blob/dev/sql2java-manager/src/test/java/gu/sql2java/pagehelper/parser/JsqlParserTest.java