public void deParse(Update update) { buffer .append("UPDATE ") .append(PlainSelect.getStringList(update.getTables(), true, false)) .append(" SET "); for (int i = 0; i < update.getColumns().size(); i++) { Column column = update.getColumns().get(i); buffer.append(column.getFullyQualifiedName()).append(" = "); Expression expression = update.getExpressions().get(i); expression.accept(expressionVisitor); if (i < update.getColumns().size() - 1) { buffer.append(", "); } } if (update.getFromItem() != null) { buffer.append(" FROM ").append(update.getFromItem()); if (update.getJoins() != null) { for (Join join : update.getJoins()) { if (join.isSimple()) { buffer.append(", ").append(join); } else { buffer.append(" ").append(join); } } } } if (update.getWhere() != null) { buffer.append(" WHERE "); update.getWhere().accept(expressionVisitor); } }
/** * 静态调用接口,获取所有表的ID * * @param sql 传入的sql语句 * @return 返回-1表示当前语句不是select语句,返回0表示当前SQL有问题,返回>0的数字表示查找出的表数目 */ public static int getOriginalTableName(String sql) { int num_Table = 0; Statement statement = null; try { statement = CCJSqlParserUtil.parse(sql); if (statement instanceof Select) { Select selectStatement = (Select) statement; System.err.println(sql); PlainSelect plainSelect = null; plainSelect = (PlainSelect) selectStatement.getSelectBody(); if (plainSelect != null) { System.out.println("\n-------------------------------------------"); if (plainSelect.getFromItem() != null) { plainSelect.getFromItem().toString(); num_Table++; } System.out.print(plainSelect.getFromItem().toString() + "\t"); if (plainSelect.getJoins() != null) { for (Join join2 : plainSelect.getJoins()) { System.out.print(join2.toString() + "\t"); num_Table++; } } System.out.println("\n-------------------------------------------"); } else { num_Table = -1; return num_Table; } } } catch (JSQLParserException ex) { ex.printStackTrace(); num_Table = 0; LogWriter.println(num_Table); return num_Table; } LogWriter.println("共统计出表个数=>" + num_Table); return num_Table; }
/** @brief The following functions override functions of the interfaces. */ public void visit(PlainSelect plainSelect) throws Exception { if (plainSelect.getFromItem() != null) { if (plainSelect.getFromItem().getAlias() != null) { this.aliasTableNameList.add(plainSelect.getFromItem().getAlias().getName()); } plainSelect.getFromItem().accept(this); } if (plainSelect.getJoins() != null) { for (Iterator joinsIt = plainSelect.getJoins().iterator(); joinsIt.hasNext(); ) { Join join = (Join) joinsIt.next(); if (join.getRightItem().getAlias() != null) { this.aliasTableNameList.add(join.getRightItem().getAlias().getName()); } if (join.getOnExpression() != null) { join.getOnExpression().accept(this); } join.getRightItem().accept(this); } } // Select selectItem From fromItem, joinItem Where whereClause. if (plainSelect.getSelectItems() != null) { for (SelectItem selectItem : plainSelect.getSelectItems()) { selectItem.accept(this); } } if (plainSelect.getWhere() != null) { plainSelect.getWhere().accept(this); } if (plainSelect.getGroupByColumnReferences() != null) { for (Iterator groupByIt = plainSelect.getGroupByColumnReferences().iterator(); groupByIt.hasNext(); ) { Expression groupBy = (Expression) groupByIt.next(); groupBy.accept(this); } } if (plainSelect.getClusterByElements() != null) { for (Iterator clusterByit = plainSelect.getClusterByElements().iterator(); clusterByit.hasNext(); ) { ClusterByElement clusterByElement = (ClusterByElement) clusterByit.next(); visit(clusterByElement); } } if (plainSelect.getDistributeByElements() != null) { for (Iterator distributeByIt = plainSelect.getDistributeByElements().iterator(); distributeByIt.hasNext(); ) { DistributeByElement distributeByElement = (DistributeByElement) distributeByIt.next(); visit(distributeByElement); } } if (plainSelect.getOrderByElements() != null) { for (Iterator orderByIt = plainSelect.getOrderByElements().iterator(); orderByIt.hasNext(); ) { OrderByElement orderByElement = (OrderByElement) orderByIt.next(); orderByElement.accept(this); } } if (plainSelect.getSortByElements() != null) { for (Iterator sortByIt = plainSelect.getSortByElements().iterator(); sortByIt.hasNext(); ) { SortByElement sortByElement = (SortByElement) sortByIt.next(); visit(sortByElement); } } if (plainSelect.getHaving() != null) { plainSelect.getHaving().accept(this); } }