Skip to content

Commit

Permalink
for #2084, remove param sql from filler
Browse files Browse the repository at this point in the history
  • Loading branch information
terrymanu committed Apr 7, 2019
1 parent 336a58c commit c594265
Show file tree
Hide file tree
Showing 39 changed files with 113 additions and 123 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import org.apache.shardingsphere.core.metadata.table.ShardingTableMetaData;
import org.apache.shardingsphere.core.parse.antlr.sql.statement.SQLStatement;
import org.apache.shardingsphere.core.parse.cache.ParsingResultCache;
import org.apache.shardingsphere.core.parse.parser.sql.AbstractSQLStatement;
import org.apache.shardingsphere.core.parse.parser.sql.SQLParserFactory;
import org.apache.shardingsphere.core.rule.EncryptRule;

Expand Down Expand Up @@ -61,9 +60,6 @@ public SQLStatement parse(final boolean useCache, final String sql) {
return cachedSQLStatement.get();
}
SQLStatement result = SQLParserFactory.newInstance(dbType, encryptRule, shardingTableMetaData, sql).parse();
if (result instanceof AbstractSQLStatement) {
((AbstractSQLStatement) result).setLogicSQL(sql);
}
if (useCache) {
parsingResultCache.put(sql, result);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import org.apache.shardingsphere.core.parse.cache.ParsingResultCache;
import org.apache.shardingsphere.core.parse.lexer.LexerEngine;
import org.apache.shardingsphere.core.parse.lexer.LexerEngineFactory;
import org.apache.shardingsphere.core.parse.parser.sql.AbstractSQLStatement;
import org.apache.shardingsphere.core.parse.parser.sql.SQLParserFactory;
import org.apache.shardingsphere.core.rule.ShardingRule;

Expand Down Expand Up @@ -60,9 +59,6 @@ public SQLStatement parse(final boolean useCache) {
}
LexerEngine lexerEngine = LexerEngineFactory.newInstance(dbType, sql);
SQLStatement result = SQLParserFactory.newInstance(dbType, shardingRule, lexerEngine, shardingTableMetaData, sql).parse();
if (result instanceof AbstractSQLStatement) {
((AbstractSQLStatement) result).setLogicSQL(sql);
}
if (useCache) {
parsingResultCache.put(sql, result);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,8 @@ public interface SQLSegmentFiller<T extends SQLSegment, R extends BaseRule> {
*
* @param sqlSegment SQL segment
* @param sqlStatement SQL statement
* @param sql SQL
* @param rule rule
* @param shardingTableMetaData sharding table meta data
*/
void fill(T sqlSegment, SQLStatement sqlStatement, String sql, R rule, ShardingTableMetaData shardingTableMetaData);
void fill(T sqlSegment, SQLStatement sqlStatement, R rule, ShardingTableMetaData shardingTableMetaData);
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,11 @@ public SQLStatementFillerEngine(final ParsingRuleRegistry parsingRuleRegistry,
@SneakyThrows
public SQLStatement fill(final Collection<SQLSegment> sqlSegments, final SQLStatementRule rule) {
SQLStatement result = rule.getSqlStatementClass().newInstance();
result.setLogicSQL(sql);
for (SQLSegment each : sqlSegments) {
Optional<SQLSegmentFiller> filler = parsingRuleRegistry.findSQLSegmentFiller(databaseType, each.getClass());
if (filler.isPresent()) {
(filler.get()).fill(each, result, sql, this.rule, shardingTableMetaData);
(filler.get()).fill(each, result, this.rule, shardingTableMetaData);
}
}
return result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
public final class ShowParamFiller implements SQLSegmentFiller<ShowParamSegment, BaseRule> {

@Override
public void fill(final ShowParamSegment sqlSegment, final SQLStatement sqlStatement, final String sql, final BaseRule rule, final ShardingTableMetaData shardingTableMetaData) {
public void fill(final ShowParamSegment sqlSegment, final SQLStatement sqlStatement, final BaseRule rule, final ShardingTableMetaData shardingTableMetaData) {
((ShowStatement) sqlStatement).setName(sqlSegment.getName());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
public final class ColumnDefinitionFiller implements SQLSegmentFiller<ColumnDefinitionSegment, BaseRule> {

@Override
public void fill(final ColumnDefinitionSegment sqlSegment, final SQLStatement sqlStatement, final String sql, final BaseRule rule, final ShardingTableMetaData shardingTableMetaData) {
public void fill(final ColumnDefinitionSegment sqlSegment, final SQLStatement sqlStatement, final BaseRule rule, final ShardingTableMetaData shardingTableMetaData) {
((CreateTableStatement) sqlStatement).getColumnDefinitions().add(sqlSegment);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
public class CommonTableFiller implements SQLSegmentFiller<TableSegment, BaseRule> {

@Override
public void fill(final TableSegment sqlSegment, final SQLStatement sqlStatement, final String sql, final BaseRule rule, final ShardingTableMetaData shardingTableMetaData) {
public void fill(final TableSegment sqlSegment, final SQLStatement sqlStatement, final BaseRule rule, final ShardingTableMetaData shardingTableMetaData) {
sqlStatement.getTables().add(new Table(sqlSegment.getName(), sqlSegment.getAlias()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
public final class ConstraintDefinitionFiller implements SQLSegmentFiller<ConstraintDefinitionSegment, BaseRule> {

@Override
public void fill(final ConstraintDefinitionSegment sqlSegment, final SQLStatement sqlStatement, final String sql, final BaseRule rule, final ShardingTableMetaData shardingTableMetaData) {
public void fill(final ConstraintDefinitionSegment sqlSegment, final SQLStatement sqlStatement, final BaseRule rule, final ShardingTableMetaData shardingTableMetaData) {
if (sqlStatement instanceof CreateTableStatement) {
fill(sqlSegment, (CreateTableStatement) sqlStatement);
} else if (sqlStatement instanceof AlterTableStatement) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
public final class IndexFiller implements SQLSegmentFiller<IndexSegment, BaseRule> {

@Override
public void fill(final IndexSegment sqlSegment, final SQLStatement sqlStatement, final String sql, final BaseRule rule, final ShardingTableMetaData shardingTableMetaData) {
public void fill(final IndexSegment sqlSegment, final SQLStatement sqlStatement, final BaseRule rule, final ShardingTableMetaData shardingTableMetaData) {
IndexToken indexToken = sqlSegment.getToken();
if (!sqlStatement.getTables().isEmpty() && null == indexToken.getTableName()) {
indexToken.setTableName(sqlStatement.getTables().getSingleTableName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@
public final class AddColumnDefinitionFiller implements SQLSegmentFiller<AddColumnDefinitionSegment, BaseRule> {

@Override
public void fill(final AddColumnDefinitionSegment sqlSegment,
final SQLStatement sqlStatement, final String sql, final BaseRule rule, final ShardingTableMetaData shardingTableMetaData) {
public void fill(final AddColumnDefinitionSegment sqlSegment, final SQLStatement sqlStatement, final BaseRule rule, final ShardingTableMetaData shardingTableMetaData) {
AlterTableStatement alterTableStatement = (AlterTableStatement) sqlStatement;
if (!alterTableStatement.findColumnDefinitionFromMetaData(sqlSegment.getColumnDefinition().getColumnName(), shardingTableMetaData).isPresent()) {
alterTableStatement.getAddedColumnDefinitions().add(sqlSegment.getColumnDefinition());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@
public final class DropColumnDefinitionFiller implements SQLSegmentFiller<DropColumnDefinitionSegment, BaseRule> {

@Override
public void fill(final DropColumnDefinitionSegment sqlSegment,
final SQLStatement sqlStatement, final String sql, final BaseRule rule, final ShardingTableMetaData shardingTableMetaData) {
public void fill(final DropColumnDefinitionSegment sqlSegment, final SQLStatement sqlStatement, final BaseRule rule, final ShardingTableMetaData shardingTableMetaData) {
((AlterTableStatement) sqlStatement).getDroppedColumnNames().add(sqlSegment.getColumnName());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
public final class DropPrimaryKeyFiller implements SQLSegmentFiller<DropPrimaryKeySegment, BaseRule> {

@Override
public void fill(final DropPrimaryKeySegment sqlSegment, final SQLStatement sqlStatement, final String sql, final BaseRule rule, final ShardingTableMetaData shardingTableMetaData) {
public void fill(final DropPrimaryKeySegment sqlSegment, final SQLStatement sqlStatement, final BaseRule rule, final ShardingTableMetaData shardingTableMetaData) {
((AlterTableStatement) sqlStatement).setDropPrimaryKey(true);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@
public final class ModifyColumnDefinitionFiller implements SQLSegmentFiller<ModifyColumnDefinitionSegment, BaseRule> {

@Override
public void fill(final ModifyColumnDefinitionSegment sqlSegment,
final SQLStatement sqlStatement, final String sql, final BaseRule rule, final ShardingTableMetaData shardingTableMetaData) {
public void fill(final ModifyColumnDefinitionSegment sqlSegment, final SQLStatement sqlStatement, final BaseRule rule, final ShardingTableMetaData shardingTableMetaData) {
AlterTableStatement alterTableStatement = (AlterTableStatement) sqlStatement;
Optional<String> oldColumnName = sqlSegment.getOldColumnName();
if (oldColumnName.isPresent()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
public final class RenameColumnDefinitionFiller implements SQLSegmentFiller<RenameColumnSegment, BaseRule> {

@Override
public void fill(final RenameColumnSegment sqlSegment, final SQLStatement sqlStatement, final String sql, final BaseRule rule, final ShardingTableMetaData shardingTableMetaData) {
public void fill(final RenameColumnSegment sqlSegment, final SQLStatement sqlStatement, final BaseRule rule, final ShardingTableMetaData shardingTableMetaData) {
AlterTableStatement alterTableStatement = (AlterTableStatement) sqlStatement;
Optional<ColumnDefinitionSegment> oldColumnDefinition = alterTableStatement.findColumnDefinition(sqlSegment.getOldColumnName(), shardingTableMetaData);
if (!oldColumnDefinition.isPresent()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
public final class RenameTableFiller implements SQLSegmentFiller<RenameTableSegment, BaseRule> {

@Override
public void fill(final RenameTableSegment sqlSegment, final SQLStatement sqlStatement, final String sql, final BaseRule rule, final ShardingTableMetaData shardingTableMetaData) {
public void fill(final RenameTableSegment sqlSegment, final SQLStatement sqlStatement, final BaseRule rule, final ShardingTableMetaData shardingTableMetaData) {
((AlterTableStatement) sqlStatement).setNewTableName(sqlSegment.getNewTableName());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
public final class InsertColumnsFiller implements SQLSegmentFiller<InsertColumnsSegment, BaseRule> {

@Override
public void fill(final InsertColumnsSegment sqlSegment, final SQLStatement sqlStatement, final String sql, final BaseRule rule, final ShardingTableMetaData shardingTableMetaData) {
public void fill(final InsertColumnsSegment sqlSegment, final SQLStatement sqlStatement, final BaseRule rule, final ShardingTableMetaData shardingTableMetaData) {
if (sqlStatement instanceof InsertStatement) {
InsertStatement insertStatement = (InsertStatement) sqlStatement;
if (sqlSegment.getColumns().isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
public final class GroupByFiller implements SQLSegmentFiller<GroupBySegment, BaseRule> {

@Override
public void fill(final GroupBySegment sqlSegment, final SQLStatement sqlStatement, final String sql, final BaseRule rule, final ShardingTableMetaData shardingTableMetaData) {
public void fill(final GroupBySegment sqlSegment, final SQLStatement sqlStatement, final BaseRule rule, final ShardingTableMetaData shardingTableMetaData) {
SelectStatement selectStatement = (SelectStatement) sqlStatement;
selectStatement.setGroupByLastIndex(sqlSegment.getGroupByStopIndex());
for (OrderByItemSegment each : sqlSegment.getGroupByItems()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
public final class LimitFiller implements SQLSegmentFiller<LimitSegment, BaseRule> {

@Override
public void fill(final LimitSegment sqlSegment, final SQLStatement sqlStatement, final String sql, final BaseRule rule, final ShardingTableMetaData shardingTableMetaData) {
public void fill(final LimitSegment sqlSegment, final SQLStatement sqlStatement, final BaseRule rule, final ShardingTableMetaData shardingTableMetaData) {
SelectStatement selectStatement = (SelectStatement) sqlStatement;
selectStatement.setLimit(new Limit());
if (sqlSegment.getOffset().isPresent()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
public final class OrderByFiller implements SQLSegmentFiller<OrderBySegment, BaseRule> {

@Override
public void fill(final OrderBySegment sqlSegment, final SQLStatement sqlStatement, final String sql, final BaseRule rule, final ShardingTableMetaData shardingTableMetaData) {
public void fill(final OrderBySegment sqlSegment, final SQLStatement sqlStatement, final BaseRule rule, final ShardingTableMetaData shardingTableMetaData) {
SelectStatement selectStatement = (SelectStatement) sqlStatement;
for (OrderByItemSegment each : sqlSegment.getOrderByItems()) {
selectStatement.getOrderByItems().add(new OrderItemBuilder(selectStatement, each).createOrderItem());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,45 +47,45 @@ public final class SelectClauseFiller implements SQLSegmentFiller<SelectClauseSe
private SelectItemFiller selectItemFiller = new SelectItemFiller();

@Override
public void fill(final SelectClauseSegment sqlSegment, final SQLStatement sqlStatement, final String sql, final BaseRule rule, final ShardingTableMetaData shardingTableMetaData) {
public void fill(final SelectClauseSegment sqlSegment, final SQLStatement sqlStatement, final BaseRule rule, final ShardingTableMetaData shardingTableMetaData) {
SelectStatement selectStatement = (SelectStatement) sqlStatement;
selectStatement.setFirstSelectItemStartIndex(sqlSegment.getFirstSelectItemStartIndex());
selectStatement.setSelectListStopIndex(sqlSegment.getSelectItemsStopIndex());
if (sqlSegment.isHasDistinct()) {
fillDistinct(sqlSegment, selectStatement, sql, rule, shardingTableMetaData);
fillDistinct(sqlSegment, selectStatement, rule, shardingTableMetaData);
return;
}
int offset = 0;
for (SelectItemSegment each : sqlSegment.getSelectItems()) {
offset = setDistinctFunctionAlias(each, offset);
selectItemFiller.fill(each, sqlStatement, sql, rule, shardingTableMetaData);
selectItemFiller.fill(each, sqlStatement, rule, shardingTableMetaData);
}
}

private void fillDistinct(final SelectClauseSegment selectClauseSegment,
final SelectStatement selectStatement, final String sql, final BaseRule rule, final ShardingTableMetaData shardingTableMetaData) {
final SelectStatement selectStatement, final BaseRule rule, final ShardingTableMetaData shardingTableMetaData) {
Iterator<SelectItemSegment> selectItemSegmentIterator = selectClauseSegment.getSelectItems().iterator();
SelectItemSegment firstSelectItemSegment = selectItemSegmentIterator.next();
Set<String> distinctColumnNames = new LinkedHashSet<>();
DistinctSelectItem distinctSelectItem = null;
int offset = 0;
if (firstSelectItemSegment instanceof StarSelectItemSegment) {
selectItemFiller.fill(firstSelectItemSegment, selectStatement, sql, rule, shardingTableMetaData);
selectItemFiller.fill(firstSelectItemSegment, selectStatement, rule, shardingTableMetaData);
selectStatement.getItems().add(new DistinctSelectItem(distinctColumnNames, Optional.<String>absent()));
} else if (firstSelectItemSegment instanceof ColumnSelectItemSegment) {
ColumnSelectItemSegment columnSelectItemSegment = (ColumnSelectItemSegment) firstSelectItemSegment;
distinctSelectItem = new DistinctSelectItem(distinctColumnNames, columnSelectItemSegment.getAlias());
selectStatement.getItems().add(distinctSelectItem);
distinctColumnNames.add(columnSelectItemSegment.getName());
} else if (firstSelectItemSegment instanceof ExpressionSelectItemSegment) {
distinctSelectItem = createDistinctExpressionItem(selectStatement, sql, distinctColumnNames, (ExpressionSelectItemSegment) firstSelectItemSegment);
distinctSelectItem = createDistinctExpressionItem(selectStatement, distinctColumnNames, (ExpressionSelectItemSegment) firstSelectItemSegment);
} else {
offset = setDistinctFunctionAlias(firstSelectItemSegment, offset);
selectItemFiller.fill(firstSelectItemSegment, selectStatement, sql, rule, shardingTableMetaData);
selectItemFiller.fill(firstSelectItemSegment, selectStatement, rule, shardingTableMetaData);
}
while (selectItemSegmentIterator.hasNext()) {
SelectItemSegment nextSelectItemSegment = selectItemSegmentIterator.next();
selectItemFiller.fill(nextSelectItemSegment, selectStatement, sql, rule, shardingTableMetaData);
selectItemFiller.fill(nextSelectItemSegment, selectStatement, rule, shardingTableMetaData);
if (nextSelectItemSegment instanceof ColumnSelectItemSegment) {
offset = setDistinctFunctionAlias(nextSelectItemSegment, offset);
distinctColumnNames.add(((ColumnSelectItemSegment) nextSelectItemSegment).getName());
Expand All @@ -108,10 +108,10 @@ private int setDistinctFunctionAlias(final SelectItemSegment selectItemSegment,
return offset;
}

private DistinctSelectItem createDistinctExpressionItem(final SelectStatement selectStatement, final String sql, final Set<String> distinctColumnNames,
private DistinctSelectItem createDistinctExpressionItem(final SelectStatement selectStatement, final Set<String> distinctColumnNames,
final ExpressionSelectItemSegment expressionSelectItemSegment) {
DistinctSelectItem distinctSelectItem = new DistinctSelectItem(distinctColumnNames, expressionSelectItemSegment.getAlias());
String commonExpression = sql.substring(expressionSelectItemSegment.getStartIndex(), expressionSelectItemSegment.getStopIndex() + 1);
String commonExpression = selectStatement.getLogicSQL().substring(expressionSelectItemSegment.getStartIndex(), expressionSelectItemSegment.getStopIndex() + 1);
int leftParenPosition = commonExpression.indexOf("(");
if (0 <= leftParenPosition) {
int rightParenPosition = commonExpression.lastIndexOf(")");
Expand Down
Loading

0 comments on commit c594265

Please sign in to comment.