基于jsqlparser向sql语句中插⼊where条件
import ollection.CollUtil;
import util.StrUtil;
import net.sf.jsqlparser.JSQLParserException;
import net.pression.LongValue;
import net.ditional.AndExpression;
import net.sf.jsqlparser.parser.CCJSqlParserManager;
import net.sf.jsqlparser.parser.CCJSqlParserUtil;
import net.sf.jsqlparser.schema.Table;
import net.sf.jsqlparser.statement.Statement;
import net.sf.jsqlparser.statement.alter.Alter;
import net.sf.ate.index.CreateIndex;
import net.sf.ate.table.CreateTable;
import net.sf.ate.view.CreateView;
import net.sf.jsqlparser.statement.delete.Delete;
import net.sf.jsqlparser.statement.drop.Drop;
import net.sf.ute.Execute;
import net.sf.jsqlparser.statement.insert.Insert;
import net.sf.Merge;
import net.sf.place.Replace;
import net.sf.jsqlparser.statement.select.*;
import net.sf.uncate.Truncate;
import net.sf.jsqlparser.statement.update.Update;
import net.sf.jsqlparser.util.TablesNamesFinder;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;
public class InsertWhereCondition {
public static void main(String[] args) throws JSQLParserException {
String sql =
// " de,COUNT(B.id) as value,A.value as name FROM sys_dict A\n" +
// " LEFT JOIN v_t_project B ON B.de\n" +
// " pe='projectType' de IN (1,2)\n" +
/
/ " GROUP de\n" +
// " UNION ALL\n" +
// " SELECT 3 AS code,COUNT(B.id) as value,'其他' as name FROM sys_dict A\n" +
// " LEFT JOIN v_t_project B ON B.de\n" +
// " pe='projectType' de NOT IN (1,2)\n" +
// " GROUP de";
" SELECT a.A FROM (" +
"SELECT b.B FROM " +
"(SELECT c.C FROM cc c) as b Left JOIN " +
"(SELECT * FROM F left join (select * from v) as x on x.id = F.id)d ON b.C = d.d" +                        ") as a";
CCJSqlParserManager parserManager = new CCJSqlParserManager();
StringReader stringReader = new StringReader(sql);
Select select = (Select) parserManager.parse(stringReader);
SelectBody selectBody = SelectBody();
String tableName = "v";
if (StrUtil.isEmpty(tableName) && selectBody instanceof PlainSelect) {
tableName = getTableName(selectBody);
}
processSelectBody(selectBody, tableName);
processSelectBody(selectBody, tableName);
System.out.String());
// FromItem fromItem = FromItem();
/
/有别名⽤别名,⽆别名⽤表名,防⽌字段冲突报错
String fromItemName = "";
}
public static void processSelectBody(SelectBody selectBody, String tableName) throws JSQLParserException {        if (selectBody instanceof PlainSelect) {
parsPlainSelect(selectBody, tableName);
} else if (selectBody instanceof WithItem) {
WithItem withItem = (WithItem) selectBody;
if (SelectBody() != null) {
SelectBody(), tableName);
}
} else {
SetOperationList operationList = (SetOperationList) selectBody;
List <SelectBody> selects = Selects();
for (SelectBody select : selects) {
processSelectBody(select, tableName);
}
}
}
public static void parsPlainSelect(SelectBody selectBody, String tableName) throws JSQLParserException {
PlainSelect plain = (PlainSelect) selectBody;
FromItem fromItem = FromItem();
if (getTableName(fromItem, tableName)) {
buildSubquery(plain, tableName);
return;
}
List <Join> joinList = Joins();
if (CollUtil.isNotEmpty(joinList)) {
for (Join join : joinList) {
if (RightItem(), tableName)) {
buildSubquery(plain, tableName);
return;
}
}
}
}
public static boolean getTableName(FromItem fromItem, String tableName) throws JSQLParserException {
String fromItemName = "";
if (fromItem instanceof Table) {
fromItemName = ((Table) fromItem).getName();
}
if (fromItem instanceof SubSelect) {
SelectBody subSelectBody = ((SubSelect) fromItem).getSelectBody();
processSelectBody(subSelectBody, tableName);
}
fromItemName = Alias() == null ? fromItemName : Alias().getName();
if (fromItemName.equals(tableName)) {
System.out.println("fromItemName -- " + tableName);
return true;
}
return false;
}
public static String getTableName(SelectBody selectBody) {
PlainSelect pain = (PlainSelect) selectBody;
FromItem fromItem = FromItem();
String fromItemName = "";
if (fromItem instanceof Table) {
fromItemName = ((Table) fromItem).getName();
}
}
Alias() == null ? fromItemName : Alias().getName();
}
public static void buildSubquery(PlainSelect plain, String tableName) throws JSQLParserException {
String dataAuthSql = " " + tableName + ".id in ('212','333')";
if (StrUtil.isNotEmpty(dataAuthSql)) {
import语句
if (Where() == null) {
plain.setWhere(CCJSqlParserUtil.parseCondExpression(dataAuthSql));
} else {
plain.setWhere(new Where(), CCJSqlParserUtil.parseCondExpression(dataAuthSql)));            }
}
}
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。