天天看點

JAVA - Sql解析工具jsqlparser簡單使用

  • SqlParser - github位址:​​https://github.com/JSQLParser/JSqlParser​​
  • SqlParser最新版jar下載下傳:​​http://search.maven.org/remotecontent?filepath=com/github/jsqlparser/jsqlparser/0.9.1/jsqlparser-0.9.1.jar​​

前面有一篇部落格介紹了另一個fdb-sql-parser的簡單使用。由于fdb-sql-parser針對性比較強,對不同資料庫的通用性不好,是以嘗試了另一個sql解析工具jsqlparser。

在jsqlparser的wiki頁面有簡單的使用方法:​​https://github.com/JSQLParser/JSqlParser/wiki​​

這裡仍然用去除order by做例子。  

jsqlparser的資料結構比較簡單,更清晰,因而我選擇了一種正向強制設定order by屬性為null的方式來去除order by  

下面是需要用到的幾個方法:  

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());
            }
        }
    }
    //Table時不用處理
}

public boolean orderByHashParameters(List<OrderByElement> orderByElements) {
    if (orderByElements == null) {
        return false;
    }
    for (OrderByElement orderByElement : orderByElements) {
        if (orderByElement.toString().toUpperCase().contains("?")) {
            return true;
        }
    }
    return false;
}      

由于jsqlparser結構簡單,繼承也不複雜,并且setter和getter方法很全面,是以處理起來很容易。