@Test public void testVisit() { ConditionAddVisitor visitor = createConditionAddVisitor(); String sql = getSql(); SQLStatementParser parser = SQLParserUtils.createSQLStatementParser(sql, JdbcConstants.MYSQL); SQLSelectStatement stmt = (SQLSelectStatement) parser.parseStatement(); stmt.accept(visitor); Map<String, List<MySqlSelectQueryBlock>> aliasMap = visitor.getBlockMap(); List<MySqlSelectQueryBlock> lsBlocks = aliasMap.get("ls"); List<MySqlSelectQueryBlock> esBlocks = aliasMap.get("es"); if(lsBlocks != null && !lsBlocks.isEmpty()) { for (MySqlSelectQueryBlock block : lsBlocks) { block.addCondition("ls.id = 0"); } } if(esBlocks != null && !esBlocks.isEmpty()) { for (MySqlSelectQueryBlock block : esBlocks) { block.addCondition("es.id = 0"); } } System.out.println(stmt.toString()); }
@Override public boolean visit(MySqlSelectQueryBlock block) { String from = block.getFrom().toString(); for (String tableName : targetTableNames) { StringBuilder sb = new StringBuilder(tableName); sb.append("."); String tableNamePoint = sb.toString(); if(!from.contains(tableNamePoint)) { continue; } TableAddNumberEnum tableAddNumberEnum = targetTableFirstContainsMap.get(tableName); List<MySqlSelectQueryBlock> sqlSelectQueryBlocks = blockMap.computeIfAbsent(tableName, k -> new LinkedList<>()); if(tableAddNumberEnum.equals(TableAddNumberEnum.ZERO)) { sqlSelectQueryBlocks.add(block); targetTableFirstContainsMap.put(tableName, TableAddNumberEnum.ONE); continue; } if(tableAddNumberEnum.equals(TableAddNumberEnum.ONE)) { sqlSelectQueryBlocks.remove(0); sqlSelectQueryBlocks.add(block); targetTableFirstContainsMap.put(tableName, TableAddNumberEnum.TWO); continue; } if(tableAddNumberEnum.equals(TableAddNumberEnum.TWO)) { sqlSelectQueryBlocks.add(block); } } return true; }
ConditionAddVisitor extends MySqlASTVisitorAdapter { @Override public boolean visit(MySqlSelectQueryBlock block) { String from = block.getFrom().toString(); for (String tableName : targetTableNames) { StringBuilder sb = new StringBuilder(tableName); sb.append("."); String tableNamePoint = sb.toString(); if(!from.contains(tableNamePoint)) { continue; } TableAddNumberEnum tableAddNumberEnum = targetTableFirstContainsMap.get(tableName); List<MySqlSelectQueryBlock> sqlSelectQueryBlocks = blockMap.computeIfAbsent(tableName, k -> new LinkedList<>()); if(tableAddNumberEnum.equals(TableAddNumberEnum.ZERO)) { sqlSelectQueryBlocks.add(block); targetTableFirstContainsMap.put(tableName, TableAddNumberEnum.ONE); continue; } if(tableAddNumberEnum.equals(TableAddNumberEnum.ONE)) { sqlSelectQueryBlocks.remove(0); sqlSelectQueryBlocks.add(block); targetTableFirstContainsMap.put(tableName, TableAddNumberEnum.TWO); continue; } if(tableAddNumberEnum.equals(TableAddNumberEnum.TWO)) { sqlSelectQueryBlocks.add(block); } } return true; } }
ConditionAddVisitor extends MySqlASTVisitorAdapter { @Override public boolean visit(MySqlSelectQueryBlock block) { String from = block.getFrom().toString(); for (String tableName : targetTableNames) { StringBuilder sb = new StringBuilder(tableName); sb.append("."); String tableNamePoint = sb.toString(); if(!from.contains(tableNamePoint)) { continue; } TableAddNumberEnum tableAddNumberEnum = targetTableFirstContainsMap.get(tableName); List<MySqlSelectQueryBlock> sqlSelectQueryBlocks = blockMap.computeIfAbsent(tableName, k -> new LinkedList<>()); if(tableAddNumberEnum.equals(TableAddNumberEnum.ZERO)) { sqlSelectQueryBlocks.add(block); targetTableFirstContainsMap.put(tableName, TableAddNumberEnum.ONE); continue; } if(tableAddNumberEnum.equals(TableAddNumberEnum.ONE)) { sqlSelectQueryBlocks.remove(0); sqlSelectQueryBlocks.add(block); targetTableFirstContainsMap.put(tableName, TableAddNumberEnum.TWO); continue; } if(tableAddNumberEnum.equals(TableAddNumberEnum.TWO)) { sqlSelectQueryBlocks.add(block); } } return true; } ConditionAddVisitor(List<String> targetTableNames); }
ConditionAddVisitor extends MySqlASTVisitorAdapter { @Override public boolean visit(MySqlSelectQueryBlock block) { String from = block.getFrom().toString(); for (String tableName : targetTableNames) { StringBuilder sb = new StringBuilder(tableName); sb.append("."); String tableNamePoint = sb.toString(); if(!from.contains(tableNamePoint)) { continue; } TableAddNumberEnum tableAddNumberEnum = targetTableFirstContainsMap.get(tableName); List<MySqlSelectQueryBlock> sqlSelectQueryBlocks = blockMap.computeIfAbsent(tableName, k -> new LinkedList<>()); if(tableAddNumberEnum.equals(TableAddNumberEnum.ZERO)) { sqlSelectQueryBlocks.add(block); targetTableFirstContainsMap.put(tableName, TableAddNumberEnum.ONE); continue; } if(tableAddNumberEnum.equals(TableAddNumberEnum.ONE)) { sqlSelectQueryBlocks.remove(0); sqlSelectQueryBlocks.add(block); targetTableFirstContainsMap.put(tableName, TableAddNumberEnum.TWO); continue; } if(tableAddNumberEnum.equals(TableAddNumberEnum.TWO)) { sqlSelectQueryBlocks.add(block); } } return true; } ConditionAddVisitor(List<String> targetTableNames); @Override boolean visit(MySqlSelectQueryBlock block); Map<String, List<MySqlSelectQueryBlock>> getBlockMap(); }
ConditionAddVisitor extends MySqlASTVisitorAdapter { @Override public boolean visit(MySqlSelectQueryBlock block) { String from = block.getFrom().toString(); for (String tableName : targetTableNames) { StringBuilder sb = new StringBuilder(tableName); sb.append("."); String tableNamePoint = sb.toString(); if(!from.contains(tableNamePoint)) { continue; } TableAddNumberEnum tableAddNumberEnum = targetTableFirstContainsMap.get(tableName); List<MySqlSelectQueryBlock> sqlSelectQueryBlocks = blockMap.computeIfAbsent(tableName, k -> new LinkedList<>()); if(tableAddNumberEnum.equals(TableAddNumberEnum.ZERO)) { sqlSelectQueryBlocks.add(block); targetTableFirstContainsMap.put(tableName, TableAddNumberEnum.ONE); continue; } if(tableAddNumberEnum.equals(TableAddNumberEnum.ONE)) { sqlSelectQueryBlocks.remove(0); sqlSelectQueryBlocks.add(block); targetTableFirstContainsMap.put(tableName, TableAddNumberEnum.TWO); continue; } if(tableAddNumberEnum.equals(TableAddNumberEnum.TWO)) { sqlSelectQueryBlocks.add(block); } } return true; } ConditionAddVisitor(List<String> targetTableNames); @Override boolean visit(MySqlSelectQueryBlock block); Map<String, List<MySqlSelectQueryBlock>> getBlockMap();  }