From ba366bdcf441851a6e46d5d3c5f82015b22cc436 Mon Sep 17 00:00:00 2001 From: Liang Zhang Date: Thu, 26 Sep 2019 11:46:06 +0800 Subject: [PATCH] Rename OptimizedStatement to SQLStatementContext (#3114) * rename OptimizedStatement to SQLStatementContext * rename Tables to TablesContext * move tablecontext to table package * rename InsertValue to InsertValueContext * rename GroupBy to GroupByContext * refactor PaginationTest * rename OrderBy to OrderByContext * rename Pagination to PaginationContext * rename SelectItems to SelectItemsContext --- .../core/BaseShardingEngine.java | 5 +- .../core/BaseShardingEngineTest.java | 6 +- .../core/merge/MergeEngineFactory.java | 8 +- .../core/merge/dal/DALMergeEngine.java | 8 +- .../dal/desc/DescribeTableMergedResult.java | 10 +- .../core/merge/dql/DQLMergeEngine.java | 37 +- .../groupby/GroupByMemoryMergedResult.java | 19 +- .../dql/groupby/GroupByRowComparator.java | 10 +- .../groupby/GroupByStreamMergedResult.java | 18 +- .../LimitDecoratorMergedResult.java | 6 +- .../RowNumberDecoratorMergedResult.java | 6 +- .../TopAndRowNumberDecoratorMergedResult.java | 6 +- .../core/merge/MergeEngineFactoryTest.java | 30 +- .../core/merge/dal/DALMergeEngineTest.java | 30 +- .../desc/DescribeTableMergedResultTest.java | 24 +- .../core/merge/dql/DQLMergeEngineTest.java | 230 ++++++------- .../GroupByMemoryMergedResultTest.java | 26 +- .../dql/groupby/GroupByRowComparatorTest.java | 70 ++-- .../GroupByStreamMergedResultTest.java | 26 +- .../IteratorStreamMergedResultTest.java | 20 +- .../OrderByStreamMergedResultTest.java | 24 +- .../LimitDecoratorMergedResultTest.java | 44 +-- .../RowNumberDecoratorMergedResultTest.java | 52 +-- ...AndRowNumberDecoratorMergedResultTest.java | 64 ++-- ...y.java => SQLStatementContextFactory.java} | 24 +- ...sertValue.java => InsertValueContext.java} | 6 +- .../{GroupBy.java => GroupByContext.java} | 4 +- ...yEngine.java => GroupByContextEngine.java} | 16 +- ...lectItems.java => SelectItemsContext.java} | 6 +- ...ine.java => SelectItemsContextEngine.java} | 71 ++-- .../{OrderBy.java => OrderByContext.java} | 4 +- ...yEngine.java => OrderByContextEngine.java} | 20 +- ...Pagination.java => PaginationContext.java} | 16 +- ...java => LimitPaginationContextEngine.java} | 14 +- ...gine.java => PaginationContextEngine.java} | 23 +- ... => RowNumberPaginationContextEngine.java} | 24 +- ...e.java => TopPaginationContextEngine.java} | 14 +- .../optimize/segment/{ => table}/Table.java | 2 +- .../{Tables.java => table/TablesContext.java} | 8 +- ...tatement.java => SQLStatementContext.java} | 12 +- ...nt.java => CommonSQLStatementContext.java} | 14 +- ...nt.java => InsertSQLStatementContext.java} | 24 +- ...nt.java => SelectSQLStatementContext.java} | 65 ++-- ...sTest.java => SelectItemsContextTest.java} | 63 ++-- ...onTest.java => PaginationContextTest.java} | 84 ++--- .../TablesContextTest.java} | 48 +-- ...ava => InsertSQLStatementContextTest.java} | 33 +- ...ava => SelectSQLStatementContextTest.java} | 60 ++-- .../core/rewrite/SQLRewriteEngine.java | 20 +- .../standard/StandardParameterBuilder.java | 16 +- .../WhereClauseEncryptConditionEngine.java | 16 +- .../statement/InsertRewriteStatement.java | 38 +-- .../rewrite/statement/RewriteStatement.java | 4 +- .../statement/RewriteStatementFactory.java | 28 +- .../AggregationDistinctTokenGenerator.java | 6 +- .../token/generator/IndexTokenGenerator.java | 2 +- .../InsertCipherNameTokenGenerator.java | 20 +- .../InsertGeneratedKeyNameTokenGenerator.java | 8 +- ...nsertQueryAndPlainNamesTokenGenerator.java | 20 +- .../InsertRegularNamesTokenGenerator.java | 34 +- .../InsertSetCipherColumnTokenGenerator.java | 31 +- ...rtSetGeneratedKeyColumnTokenGenerator.java | 27 +- ...SetQueryAndPlainColumnsTokenGenerator.java | 28 +- .../generator/InsertValuesTokenGenerator.java | 18 +- .../token/generator/OffsetTokenGenerator.java | 8 +- .../generator/OrderByTokenGenerator.java | 14 +- .../token/generator/RemoveTokenGenerator.java | 2 +- .../generator/RowCountTokenGenerator.java | 10 +- .../SelectEncryptItemTokenGenerator.java | 22 +- .../SelectItemPrefixTokenGenerator.java | 8 +- .../generator/SelectItemsTokenGenerator.java | 12 +- .../token/generator/TableTokenGenerator.java | 24 +- .../UpdateEncryptColumnTokenGenerator.java | 12 +- .../StandardParameterBuilderTest.java | 18 +- .../rewriter/EncryptSQLRewriteEngineTest.java | 8 +- .../ShardingSQLRewriteEngineTest.java | 320 +++++++++--------- .../token/SQLTokenGenerateEngineTest.java | 19 +- .../shardingsphere/core/route/SQLLogger.java | 8 +- .../core/route/SQLRouteResult.java | 10 +- .../ShardingMasterSlaveRouter.java | 2 +- .../router/sharding/RoutingEngineFactory.java | 34 +- .../route/router/sharding/ShardingRouter.java | 46 +-- .../InsertClauseShardingConditionEngine.java | 24 +- .../WhereClauseShardingConditionEngine.java | 16 +- .../ShardingUpdateStatementValidator.java | 4 +- .../TableBroadcastRoutingEngine.java | 8 +- .../type/complex/ComplexRoutingEngine.java | 6 +- .../type/standard/StandardRoutingEngine.java | 8 +- .../core/route/DatabaseTest.java | 18 +- .../ShardingMasterSlaveRouterTest.java | 8 +- .../sharding/RoutingEngineFactoryTest.java | 74 ++-- .../TableBroadcastRoutingEngineTest.java | 20 +- .../complex/ComplexRoutingEngineTest.java | 30 +- .../standard/StandardRoutingEngineTest.java | 34 +- .../executor/AbstractStatementExecutor.java | 58 ++-- .../BatchPreparedStatementExecutor.java | 2 +- .../executor/PreparedStatementExecutor.java | 2 +- .../executor/StatementExecutor.java | 2 +- .../adapter/AbstractResultSetAdapter.java | 6 +- .../jdbc/core/resultset/EncryptResultSet.java | 16 +- .../resultset/EncryptResultSetMetaData.java | 12 +- .../resultset/ShardingResultSetMetaData.java | 12 +- .../statement/EncryptPreparedStatement.java | 15 +- .../jdbc/core/statement/EncryptStatement.java | 15 +- .../statement/ShardingPreparedStatement.java | 8 +- .../core/statement/ShardingStatement.java | 8 +- .../core/resultset/ShardingResultSetTest.java | 14 +- .../jdbc/JDBCDatabaseCommunicationEngine.java | 19 +- .../jdbc/execute/JDBCExecuteEngine.java | 2 +- .../PreparedStatementExecutorWrapper.java | 18 +- .../wrapper/StatementExecutorWrapper.java | 20 +- .../backend/schema/LogicSchema.java | 6 +- .../backend/schema/impl/ShardingSchema.java | 54 +-- .../prepare/MySQLComStmtPrepareExecutor.java | 6 +- 114 files changed, 1426 insertions(+), 1446 deletions(-) rename sharding-core/sharding-core-optimize/src/main/java/org/apache/shardingsphere/core/optimize/{OptimizedStatementFactory.java => SQLStatementContextFactory.java} (71%) rename sharding-core/sharding-core-optimize/src/main/java/org/apache/shardingsphere/core/optimize/segment/insert/{InsertValue.java => InsertValueContext.java} (95%) rename sharding-core/sharding-core-optimize/src/main/java/org/apache/shardingsphere/core/optimize/segment/select/groupby/{GroupBy.java => GroupByContext.java} (95%) rename sharding-core/sharding-core-optimize/src/main/java/org/apache/shardingsphere/core/optimize/segment/select/groupby/engine/{GroupByEngine.java => GroupByContextEngine.java} (82%) rename sharding-core/sharding-core-optimize/src/main/java/org/apache/shardingsphere/core/optimize/segment/select/item/{SelectItems.java => SelectItemsContext.java} (98%) rename sharding-core/sharding-core-optimize/src/main/java/org/apache/shardingsphere/core/optimize/segment/select/item/engine/{SelectItemsEngine.java => SelectItemsContextEngine.java} (69%) rename sharding-core/sharding-core-optimize/src/main/java/org/apache/shardingsphere/core/optimize/segment/select/orderby/{OrderBy.java => OrderByContext.java} (95%) rename sharding-core/sharding-core-optimize/src/main/java/org/apache/shardingsphere/core/optimize/segment/select/orderby/engine/{OrderByEngine.java => OrderByContextEngine.java} (80%) rename sharding-core/sharding-core-optimize/src/main/java/org/apache/shardingsphere/core/optimize/segment/select/pagination/{Pagination.java => PaginationContext.java} (88%) rename sharding-core/sharding-core-optimize/src/main/java/org/apache/shardingsphere/core/optimize/segment/select/pagination/engine/{LimitPaginationEngine.java => LimitPaginationContextEngine.java} (74%) rename sharding-core/sharding-core-optimize/src/main/java/org/apache/shardingsphere/core/optimize/segment/select/pagination/engine/{PaginationEngine.java => PaginationContextEngine.java} (70%) rename sharding-core/sharding-core-optimize/src/main/java/org/apache/shardingsphere/core/optimize/segment/select/pagination/engine/{RowNumberPaginationEngine.java => RowNumberPaginationContextEngine.java} (86%) rename sharding-core/sharding-core-optimize/src/main/java/org/apache/shardingsphere/core/optimize/segment/select/pagination/engine/{TopPaginationEngine.java => TopPaginationContextEngine.java} (92%) rename sharding-core/sharding-core-optimize/src/main/java/org/apache/shardingsphere/core/optimize/segment/{ => table}/Table.java (95%) rename sharding-core/sharding-core-optimize/src/main/java/org/apache/shardingsphere/core/optimize/segment/{Tables.java => table/TablesContext.java} (97%) rename sharding-core/sharding-core-optimize/src/main/java/org/apache/shardingsphere/core/optimize/statement/{OptimizedStatement.java => SQLStatementContext.java} (82%) rename sharding-core/sharding-core-optimize/src/main/java/org/apache/shardingsphere/core/optimize/statement/impl/{CommonOptimizedStatement.java => CommonSQLStatementContext.java} (72%) rename sharding-core/sharding-core-optimize/src/main/java/org/apache/shardingsphere/core/optimize/statement/impl/{InsertOptimizedStatement.java => InsertSQLStatementContext.java} (66%) rename sharding-core/sharding-core-optimize/src/main/java/org/apache/shardingsphere/core/optimize/statement/impl/{SelectOptimizedStatement.java => SelectSQLStatementContext.java} (73%) rename sharding-core/sharding-core-optimize/src/test/java/org/apache/shardingsphere/core/optimize/segment/select/item/{SelectItemsTest.java => SelectItemsContextTest.java} (66%) rename sharding-core/sharding-core-optimize/src/test/java/org/apache/shardingsphere/core/optimize/segment/select/pagination/{PaginationTest.java => PaginationContextTest.java} (50%) rename sharding-core/sharding-core-optimize/src/test/java/org/apache/shardingsphere/core/optimize/segment/{TablesTest.java => table/TablesContextTest.java} (74%) rename sharding-core/sharding-core-optimize/src/test/java/org/apache/shardingsphere/core/optimize/statement/impl/{InsertOptimizeStatementTest.java => InsertSQLStatementContextTest.java} (69%) rename sharding-core/sharding-core-optimize/src/test/java/org/apache/shardingsphere/core/optimize/statement/impl/{SelectOptimizedStatementTest.java => SelectSQLStatementContextTest.java} (69%) diff --git a/sharding-core/sharding-core-entry/src/main/java/org/apache/shardingsphere/core/BaseShardingEngine.java b/sharding-core/sharding-core-entry/src/main/java/org/apache/shardingsphere/core/BaseShardingEngine.java index 6793ca1a4b..a4187c52d1 100644 --- a/sharding-core/sharding-core-entry/src/main/java/org/apache/shardingsphere/core/BaseShardingEngine.java +++ b/sharding-core/sharding-core-entry/src/main/java/org/apache/shardingsphere/core/BaseShardingEngine.java @@ -72,7 +72,7 @@ public abstract class BaseShardingEngine { boolean showSQL = shardingProperties.getValue(ShardingPropertiesConstant.SQL_SHOW); if (showSQL) { boolean showSimple = shardingProperties.getValue(ShardingPropertiesConstant.SQL_SIMPLE); - SQLLogger.logSQL(sql, showSimple, result.getShardingStatement(), result.getRouteUnits()); + SQLLogger.logSQL(sql, showSimple, result.getShardingStatementContext(), result.getRouteUnits()); } return result; } @@ -108,7 +108,8 @@ public abstract class BaseShardingEngine { sqlRouteResult, sql, parameters, sqlRouteResult.getRoutingResult().isSingleRouting(), shardingProperties.getValue(ShardingPropertiesConstant.QUERY_WITH_CIPHER_COLUMN)); Collection result = new LinkedHashSet<>(); for (RoutingUnit each : sqlRouteResult.getRoutingResult().getRoutingUnits()) { - result.add(new RouteUnit(each.getDataSourceName(), rewriteEngine.generateSQL(each, getLogicAndActualTables(each, sqlRouteResult.getShardingStatement().getTables().getTableNames())))); + result.add(new RouteUnit( + each.getDataSourceName(), rewriteEngine.generateSQL(each, getLogicAndActualTables(each, sqlRouteResult.getShardingStatementContext().getTablesContext().getTableNames())))); } return result; } diff --git a/sharding-core/sharding-core-entry/src/test/java/org/apache/shardingsphere/core/BaseShardingEngineTest.java b/sharding-core/sharding-core-entry/src/test/java/org/apache/shardingsphere/core/BaseShardingEngineTest.java index 63163b6d88..1d0343dee8 100644 --- a/sharding-core/sharding-core-entry/src/test/java/org/apache/shardingsphere/core/BaseShardingEngineTest.java +++ b/sharding-core/sharding-core-entry/src/test/java/org/apache/shardingsphere/core/BaseShardingEngineTest.java @@ -22,7 +22,7 @@ import lombok.RequiredArgsConstructor; import org.apache.shardingsphere.api.hint.HintManager; import org.apache.shardingsphere.core.constant.properties.ShardingProperties; import org.apache.shardingsphere.core.constant.properties.ShardingPropertiesConstant; -import org.apache.shardingsphere.core.optimize.statement.impl.CommonOptimizedStatement; +import org.apache.shardingsphere.core.optimize.statement.impl.CommonSQLStatementContext; import org.apache.shardingsphere.core.parse.sql.statement.dal.DALStatement; import org.apache.shardingsphere.core.route.RouteUnit; import org.apache.shardingsphere.core.route.SQLRouteResult; @@ -54,8 +54,8 @@ public abstract class BaseShardingEngineTest { } protected final SQLRouteResult createSQLRouteResult() { - SQLRouteResult result = new SQLRouteResult(new CommonOptimizedStatement(new DALStatement()), - new CommonOptimizedStatement(new DALStatement()), new ShardingConditions(Collections.emptyList())); + SQLRouteResult result = new SQLRouteResult(new CommonSQLStatementContext(new DALStatement()), + new CommonSQLStatementContext(new DALStatement()), new ShardingConditions(Collections.emptyList())); RoutingResult routingResult = new RoutingResult(); routingResult.getRoutingUnits().add(new RoutingUnit("ds")); result.setRoutingResult(routingResult); diff --git a/sharding-core/sharding-core-merge/src/main/java/org/apache/shardingsphere/core/merge/MergeEngineFactory.java b/sharding-core/sharding-core-merge/src/main/java/org/apache/shardingsphere/core/merge/MergeEngineFactory.java index 1fe67ceed0..7d1f217dfd 100644 --- a/sharding-core/sharding-core-merge/src/main/java/org/apache/shardingsphere/core/merge/MergeEngineFactory.java +++ b/sharding-core/sharding-core-merge/src/main/java/org/apache/shardingsphere/core/merge/MergeEngineFactory.java @@ -23,7 +23,7 @@ import org.apache.shardingsphere.core.execute.sql.execute.result.QueryResult; import org.apache.shardingsphere.core.merge.dal.DALMergeEngine; import org.apache.shardingsphere.core.merge.dql.DQLMergeEngine; import org.apache.shardingsphere.core.metadata.table.TableMetas; -import org.apache.shardingsphere.core.optimize.statement.impl.SelectOptimizedStatement; +import org.apache.shardingsphere.core.optimize.statement.impl.SelectSQLStatementContext; import org.apache.shardingsphere.core.parse.sql.statement.dal.DALStatement; import org.apache.shardingsphere.core.route.SQLRouteResult; import org.apache.shardingsphere.core.rule.ShardingRule; @@ -54,11 +54,11 @@ public final class MergeEngineFactory { */ public static MergeEngine newInstance(final DatabaseType databaseType, final ShardingRule shardingRule, final SQLRouteResult routeResult, final TableMetas tableMetas, final List queryResults) throws SQLException { - if (routeResult.getShardingStatement() instanceof SelectOptimizedStatement) { + if (routeResult.getShardingStatementContext() instanceof SelectSQLStatementContext) { return new DQLMergeEngine(databaseType, tableMetas, routeResult, queryResults); } - if (routeResult.getShardingStatement().getSqlStatement() instanceof DALStatement) { - return new DALMergeEngine(shardingRule, queryResults, routeResult.getShardingStatement(), tableMetas); + if (routeResult.getShardingStatementContext().getSqlStatement() instanceof DALStatement) { + return new DALMergeEngine(shardingRule, queryResults, routeResult.getShardingStatementContext(), tableMetas); } return new TransparentMergeEngine(queryResults); } diff --git a/sharding-core/sharding-core-merge/src/main/java/org/apache/shardingsphere/core/merge/dal/DALMergeEngine.java b/sharding-core/sharding-core-merge/src/main/java/org/apache/shardingsphere/core/merge/dal/DALMergeEngine.java index 18590b3898..205102b794 100644 --- a/sharding-core/sharding-core-merge/src/main/java/org/apache/shardingsphere/core/merge/dal/DALMergeEngine.java +++ b/sharding-core/sharding-core-merge/src/main/java/org/apache/shardingsphere/core/merge/dal/DALMergeEngine.java @@ -29,7 +29,7 @@ import org.apache.shardingsphere.core.merge.dal.show.ShowOtherMergedResult; import org.apache.shardingsphere.core.merge.dal.show.ShowTableStatusMergedResult; import org.apache.shardingsphere.core.merge.dal.show.ShowTablesMergedResult; import org.apache.shardingsphere.core.metadata.table.TableMetas; -import org.apache.shardingsphere.core.optimize.statement.OptimizedStatement; +import org.apache.shardingsphere.core.optimize.statement.SQLStatementContext; import org.apache.shardingsphere.core.parse.sql.statement.SQLStatement; import org.apache.shardingsphere.core.parse.sql.statement.dal.dialect.mysql.DescribeStatement; import org.apache.shardingsphere.core.parse.sql.statement.dal.dialect.mysql.ShowCreateTableStatement; @@ -55,13 +55,13 @@ public final class DALMergeEngine implements MergeEngine { private final List queryResults; - private final OptimizedStatement optimizedStatement; + private final SQLStatementContext sqlStatementContext; private final TableMetas tableMetas; @Override public MergedResult merge() throws SQLException { - SQLStatement dalStatement = optimizedStatement.getSqlStatement(); + SQLStatement dalStatement = sqlStatementContext.getSqlStatement(); if (dalStatement instanceof ShowDatabasesStatement) { return new ShowDatabasesMergedResult(); } @@ -78,7 +78,7 @@ public final class DALMergeEngine implements MergeEngine { return new ShowIndexMergedResult(shardingRule, queryResults, tableMetas); } if (dalStatement instanceof DescribeStatement) { - return new DescribeTableMergedResult(shardingRule, queryResults, optimizedStatement); + return new DescribeTableMergedResult(shardingRule, queryResults, sqlStatementContext); } return new ShowOtherMergedResult(queryResults.get(0)); } diff --git a/sharding-core/sharding-core-merge/src/main/java/org/apache/shardingsphere/core/merge/dal/desc/DescribeTableMergedResult.java b/sharding-core/sharding-core-merge/src/main/java/org/apache/shardingsphere/core/merge/dal/desc/DescribeTableMergedResult.java index fa59a1f111..00d7d08056 100644 --- a/sharding-core/sharding-core-merge/src/main/java/org/apache/shardingsphere/core/merge/dal/desc/DescribeTableMergedResult.java +++ b/sharding-core/sharding-core-merge/src/main/java/org/apache/shardingsphere/core/merge/dal/desc/DescribeTableMergedResult.java @@ -21,7 +21,7 @@ import com.google.common.base.Optional; import org.apache.shardingsphere.core.execute.sql.execute.result.QueryResult; import org.apache.shardingsphere.core.merge.dql.common.MemoryMergedResult; import org.apache.shardingsphere.core.merge.dql.common.MemoryQueryResultRow; -import org.apache.shardingsphere.core.optimize.statement.OptimizedStatement; +import org.apache.shardingsphere.core.optimize.statement.SQLStatementContext; import org.apache.shardingsphere.core.rule.ShardingRule; import org.apache.shardingsphere.core.strategy.encrypt.EncryptTable; @@ -52,14 +52,14 @@ public final class DescribeTableMergedResult extends MemoryMergedResult { private final ShardingRule shardingRule; - private final OptimizedStatement optimizedStatement; + private final SQLStatementContext sqlStatementContext; private final Iterator memoryResultSetRows; - public DescribeTableMergedResult(final ShardingRule shardingRule, final List queryResults, final OptimizedStatement optimizedStatement) throws SQLException { + public DescribeTableMergedResult(final ShardingRule shardingRule, final List queryResults, final SQLStatementContext sqlStatementContext) throws SQLException { super(LABEL_AND_INDEX_MAP); this.shardingRule = shardingRule; - this.optimizedStatement = optimizedStatement; + this.sqlStatementContext = sqlStatementContext; this.memoryResultSetRows = init(queryResults); } @@ -81,7 +81,7 @@ public final class DescribeTableMergedResult extends MemoryMergedResult { private Optional optimize(final QueryResult queryResult) throws SQLException { MemoryQueryResultRow memoryQueryResultRow = new MemoryQueryResultRow(queryResult); - String logicTableName = optimizedStatement.getTables().getSingleTableName(); + String logicTableName = sqlStatementContext.getTablesContext().getSingleTableName(); Optional encryptTable = shardingRule.getEncryptRule().findEncryptTable(logicTableName); if (encryptTable.isPresent()) { String columnName = memoryQueryResultRow.getCell(1).toString(); diff --git a/sharding-core/sharding-core-merge/src/main/java/org/apache/shardingsphere/core/merge/dql/DQLMergeEngine.java b/sharding-core/sharding-core-merge/src/main/java/org/apache/shardingsphere/core/merge/dql/DQLMergeEngine.java index 2f91a5e896..583282eb62 100644 --- a/sharding-core/sharding-core-merge/src/main/java/org/apache/shardingsphere/core/merge/dql/DQLMergeEngine.java +++ b/sharding-core/sharding-core-merge/src/main/java/org/apache/shardingsphere/core/merge/dql/DQLMergeEngine.java @@ -35,8 +35,8 @@ import org.apache.shardingsphere.core.merge.dql.pagination.RowNumberDecoratorMer import org.apache.shardingsphere.core.merge.dql.pagination.TopAndRowNumberDecoratorMergedResult; import org.apache.shardingsphere.core.metadata.table.TableMetas; import org.apache.shardingsphere.core.optimize.segment.select.item.impl.AggregationDistinctSelectItem; -import org.apache.shardingsphere.core.optimize.segment.select.pagination.Pagination; -import org.apache.shardingsphere.core.optimize.statement.impl.SelectOptimizedStatement; +import org.apache.shardingsphere.core.optimize.segment.select.pagination.PaginationContext; +import org.apache.shardingsphere.core.optimize.statement.impl.SelectSQLStatementContext; import org.apache.shardingsphere.core.parse.util.SQLUtil; import org.apache.shardingsphere.core.route.SQLRouteResult; import org.apache.shardingsphere.spi.database.DatabaseType; @@ -58,7 +58,7 @@ public final class DQLMergeEngine implements MergeEngine { private final SQLRouteResult routeResult; - private final SelectOptimizedStatement shardingStatement; + private final SelectSQLStatementContext selectSQLStatementContext; private final List queryResults; @@ -68,7 +68,7 @@ public final class DQLMergeEngine implements MergeEngine { public DQLMergeEngine(final DatabaseType databaseType, final TableMetas tableMetas, final SQLRouteResult routeResult, final List queryResults) throws SQLException { this.databaseType = databaseType; this.routeResult = routeResult; - this.shardingStatement = (SelectOptimizedStatement) routeResult.getShardingStatement(); + this.selectSQLStatementContext = (SelectSQLStatementContext) routeResult.getShardingStatementContext(); this.queryResults = getRealQueryResults(tableMetas, queryResults); columnLabelIndexMap = getColumnLabelIndexMap(this.queryResults.get(0)); } @@ -78,12 +78,12 @@ public final class DQLMergeEngine implements MergeEngine { if (1 == result.size()) { return result; } - List aggregationDistinctSelectItems = shardingStatement.getSelectItems().getAggregationDistinctSelectItems(); + List aggregationDistinctSelectItems = selectSQLStatementContext.getSelectItemsContext().getAggregationDistinctSelectItems(); if (!aggregationDistinctSelectItems.isEmpty()) { result = getDividedQueryResults(new AggregationDistinctQueryResult(queryResults, aggregationDistinctSelectItems)); } if (isDistinctRowSelectItems()) { - result = getDividedQueryResults(new DistinctQueryResult(queryResults, shardingStatement.getColumnLabels(tableMetas))); + result = getDividedQueryResults(new DistinctQueryResult(queryResults, selectSQLStatementContext.getColumnLabels(tableMetas))); } return result.isEmpty() ? queryResults : result; } @@ -99,7 +99,7 @@ public final class DQLMergeEngine implements MergeEngine { } private boolean isDistinctRowSelectItems() { - return shardingStatement.getSelectItems().isDistinctRow() && shardingStatement.getGroupBy().getItems().isEmpty(); + return selectSQLStatementContext.getSelectItemsContext().isDistinctRow() && selectSQLStatementContext.getGroupByContext().getItems().isEmpty(); } private Map getColumnLabelIndexMap(final QueryResult queryResult) throws SQLException { @@ -115,39 +115,40 @@ public final class DQLMergeEngine implements MergeEngine { if (1 == queryResults.size()) { return new IteratorStreamMergedResult(queryResults); } - shardingStatement.setIndexForItems(columnLabelIndexMap); + selectSQLStatementContext.setIndexForItems(columnLabelIndexMap); return decorate(build()); } private MergedResult build() throws SQLException { - if (!shardingStatement.getGroupBy().getItems().isEmpty() || !shardingStatement.getSelectItems().getAggregationSelectItems().isEmpty()) { + if (!selectSQLStatementContext.getGroupByContext().getItems().isEmpty() || !selectSQLStatementContext.getSelectItemsContext().getAggregationSelectItems().isEmpty()) { return getGroupByMergedResult(); } - if (!shardingStatement.getOrderBy().getItems().isEmpty()) { - return new OrderByStreamMergedResult(queryResults, shardingStatement.getOrderBy().getItems()); + if (!selectSQLStatementContext.getOrderByContext().getItems().isEmpty()) { + return new OrderByStreamMergedResult(queryResults, selectSQLStatementContext.getOrderByContext().getItems()); } return new IteratorStreamMergedResult(queryResults); } private MergedResult getGroupByMergedResult() throws SQLException { - return shardingStatement.isSameGroupByAndOrderByItems() - ? new GroupByStreamMergedResult(columnLabelIndexMap, queryResults, shardingStatement) : new GroupByMemoryMergedResult(columnLabelIndexMap, queryResults, shardingStatement); + return selectSQLStatementContext.isSameGroupByAndOrderByItems() + ? new GroupByStreamMergedResult(columnLabelIndexMap, queryResults, selectSQLStatementContext) + : new GroupByMemoryMergedResult(columnLabelIndexMap, queryResults, selectSQLStatementContext); } private MergedResult decorate(final MergedResult mergedResult) throws SQLException { - Pagination pagination = ((SelectOptimizedStatement) routeResult.getShardingStatement()).getPagination(); - if (!pagination.isHasPagination() || 1 == queryResults.size()) { + PaginationContext paginationContext = ((SelectSQLStatementContext) routeResult.getShardingStatementContext()).getPaginationContext(); + if (!paginationContext.isHasPagination() || 1 == queryResults.size()) { return mergedResult; } String trunkDatabaseName = DatabaseTypes.getTrunkDatabaseType(databaseType.getName()).getName(); if ("MySQL".equals(trunkDatabaseName) || "PostgreSQL".equals(trunkDatabaseName)) { - return new LimitDecoratorMergedResult(mergedResult, pagination); + return new LimitDecoratorMergedResult(mergedResult, paginationContext); } if ("Oracle".equals(trunkDatabaseName)) { - return new RowNumberDecoratorMergedResult(mergedResult, pagination); + return new RowNumberDecoratorMergedResult(mergedResult, paginationContext); } if ("SQLServer".equals(trunkDatabaseName)) { - return new TopAndRowNumberDecoratorMergedResult(mergedResult, pagination); + return new TopAndRowNumberDecoratorMergedResult(mergedResult, paginationContext); } return mergedResult; } diff --git a/sharding-core/sharding-core-merge/src/main/java/org/apache/shardingsphere/core/merge/dql/groupby/GroupByMemoryMergedResult.java b/sharding-core/sharding-core-merge/src/main/java/org/apache/shardingsphere/core/merge/dql/groupby/GroupByMemoryMergedResult.java index 7255e0227e..a47d7c64c2 100644 --- a/sharding-core/sharding-core-merge/src/main/java/org/apache/shardingsphere/core/merge/dql/groupby/GroupByMemoryMergedResult.java +++ b/sharding-core/sharding-core-merge/src/main/java/org/apache/shardingsphere/core/merge/dql/groupby/GroupByMemoryMergedResult.java @@ -27,7 +27,7 @@ import org.apache.shardingsphere.core.merge.dql.common.MemoryQueryResultRow; import org.apache.shardingsphere.core.merge.dql.groupby.aggregation.AggregationUnit; import org.apache.shardingsphere.core.merge.dql.groupby.aggregation.AggregationUnitFactory; import org.apache.shardingsphere.core.optimize.segment.select.item.impl.AggregationSelectItem; -import org.apache.shardingsphere.core.optimize.statement.impl.SelectOptimizedStatement; +import org.apache.shardingsphere.core.optimize.statement.impl.SelectSQLStatementContext; import java.sql.SQLException; import java.util.ArrayList; @@ -46,14 +46,14 @@ import java.util.Map.Entry; */ public final class GroupByMemoryMergedResult extends MemoryMergedResult { - private final SelectOptimizedStatement optimizedStatement; + private final SelectSQLStatementContext selectSQLStatementContext; private final Iterator memoryResultSetRows; public GroupByMemoryMergedResult( - final Map labelAndIndexMap, final List queryResults, final SelectOptimizedStatement optimizedStatement) throws SQLException { + final Map labelAndIndexMap, final List queryResults, final SelectSQLStatementContext selectSQLStatementContext) throws SQLException { super(labelAndIndexMap); - this.optimizedStatement = optimizedStatement; + this.selectSQLStatementContext = selectSQLStatementContext; memoryResultSetRows = init(queryResults); } @@ -62,7 +62,7 @@ public final class GroupByMemoryMergedResult extends MemoryMergedResult { Map> aggregationMap = new HashMap<>(1024); for (QueryResult each : queryResults) { while (each.next()) { - GroupByValue groupByValue = new GroupByValue(each, optimizedStatement.getGroupBy().getItems()); + GroupByValue groupByValue = new GroupByValue(each, selectSQLStatementContext.getGroupByContext().getItems()); initForFirstGroupByValue(each, groupByValue, dataMap, aggregationMap); aggregate(each, groupByValue, aggregationMap); } @@ -82,7 +82,8 @@ public final class GroupByMemoryMergedResult extends MemoryMergedResult { dataMap.put(groupByValue, new MemoryQueryResultRow(queryResult)); } if (!aggregationMap.containsKey(groupByValue)) { - Map map = Maps.toMap(optimizedStatement.getSelectItems().getAggregationSelectItems(), new Function() { + Map map = Maps.toMap( + selectSQLStatementContext.getSelectItemsContext().getAggregationSelectItems(), new Function() { @Override public AggregationUnit apply(final AggregationSelectItem input) { @@ -94,7 +95,7 @@ public final class GroupByMemoryMergedResult extends MemoryMergedResult { } private void aggregate(final QueryResult queryResult, final GroupByValue groupByValue, final Map> aggregationMap) throws SQLException { - for (AggregationSelectItem each : optimizedStatement.getSelectItems().getAggregationSelectItems()) { + for (AggregationSelectItem each : selectSQLStatementContext.getSelectItemsContext().getAggregationSelectItems()) { List> values = new ArrayList<>(2); if (each.getDerivedAggregationItems().isEmpty()) { values.add(getAggregationValue(queryResult, each)); @@ -115,7 +116,7 @@ public final class GroupByMemoryMergedResult extends MemoryMergedResult { private void setAggregationValueToMemoryRow(final Map dataMap, final Map> aggregationMap) { for (Entry entry : dataMap.entrySet()) { - for (AggregationSelectItem each : optimizedStatement.getSelectItems().getAggregationSelectItems()) { + for (AggregationSelectItem each : selectSQLStatementContext.getSelectItemsContext().getAggregationSelectItems()) { entry.getValue().setCell(each.getIndex(), aggregationMap.get(entry.getKey()).get(each).getResult()); } } @@ -131,7 +132,7 @@ public final class GroupByMemoryMergedResult extends MemoryMergedResult { private List getMemoryResultSetRows(final Map dataMap, final List valueCaseSensitive) { List result = new ArrayList<>(dataMap.values()); - Collections.sort(result, new GroupByRowComparator(optimizedStatement, valueCaseSensitive)); + Collections.sort(result, new GroupByRowComparator(selectSQLStatementContext, valueCaseSensitive)); return result; } diff --git a/sharding-core/sharding-core-merge/src/main/java/org/apache/shardingsphere/core/merge/dql/groupby/GroupByRowComparator.java b/sharding-core/sharding-core-merge/src/main/java/org/apache/shardingsphere/core/merge/dql/groupby/GroupByRowComparator.java index 4290ae3a5b..c9c07c8811 100644 --- a/sharding-core/sharding-core-merge/src/main/java/org/apache/shardingsphere/core/merge/dql/groupby/GroupByRowComparator.java +++ b/sharding-core/sharding-core-merge/src/main/java/org/apache/shardingsphere/core/merge/dql/groupby/GroupByRowComparator.java @@ -22,7 +22,7 @@ import lombok.RequiredArgsConstructor; import org.apache.shardingsphere.core.merge.dql.common.MemoryQueryResultRow; import org.apache.shardingsphere.core.merge.dql.orderby.CompareUtil; import org.apache.shardingsphere.core.optimize.segment.select.orderby.OrderByItem; -import org.apache.shardingsphere.core.optimize.statement.impl.SelectOptimizedStatement; +import org.apache.shardingsphere.core.optimize.statement.impl.SelectSQLStatementContext; import java.util.Collection; import java.util.Comparator; @@ -37,16 +37,16 @@ import java.util.List; @RequiredArgsConstructor public final class GroupByRowComparator implements Comparator { - private final SelectOptimizedStatement optimizedStatement; + private final SelectSQLStatementContext selectSQLStatementContext; private final List valueCaseSensitive; @Override public int compare(final MemoryQueryResultRow o1, final MemoryQueryResultRow o2) { - if (!optimizedStatement.getOrderBy().getItems().isEmpty()) { - return compare(o1, o2, optimizedStatement.getOrderBy().getItems()); + if (!selectSQLStatementContext.getOrderByContext().getItems().isEmpty()) { + return compare(o1, o2, selectSQLStatementContext.getOrderByContext().getItems()); } - return compare(o1, o2, optimizedStatement.getGroupBy().getItems()); + return compare(o1, o2, selectSQLStatementContext.getGroupByContext().getItems()); } private int compare(final MemoryQueryResultRow o1, final MemoryQueryResultRow o2, final Collection orderByItems) { diff --git a/sharding-core/sharding-core-merge/src/main/java/org/apache/shardingsphere/core/merge/dql/groupby/GroupByStreamMergedResult.java b/sharding-core/sharding-core-merge/src/main/java/org/apache/shardingsphere/core/merge/dql/groupby/GroupByStreamMergedResult.java index db32c628d0..123cde634a 100644 --- a/sharding-core/sharding-core-merge/src/main/java/org/apache/shardingsphere/core/merge/dql/groupby/GroupByStreamMergedResult.java +++ b/sharding-core/sharding-core-merge/src/main/java/org/apache/shardingsphere/core/merge/dql/groupby/GroupByStreamMergedResult.java @@ -25,7 +25,7 @@ import org.apache.shardingsphere.core.merge.dql.groupby.aggregation.AggregationU import org.apache.shardingsphere.core.merge.dql.groupby.aggregation.AggregationUnitFactory; import org.apache.shardingsphere.core.merge.dql.orderby.OrderByStreamMergedResult; import org.apache.shardingsphere.core.optimize.segment.select.item.impl.AggregationSelectItem; -import org.apache.shardingsphere.core.optimize.statement.impl.SelectOptimizedStatement; +import org.apache.shardingsphere.core.optimize.statement.impl.SelectSQLStatementContext; import java.sql.SQLException; import java.util.ArrayList; @@ -44,20 +44,20 @@ public final class GroupByStreamMergedResult extends OrderByStreamMergedResult { private final Map labelAndIndexMap; - private final SelectOptimizedStatement optimizedStatement; + private final SelectSQLStatementContext selectSQLStatementContext; private final List currentRow; private List currentGroupByValues; public GroupByStreamMergedResult( - final Map labelAndIndexMap, final List queryResults, final SelectOptimizedStatement optimizedStatement) throws SQLException { - super(queryResults, optimizedStatement.getOrderBy().getItems()); + final Map labelAndIndexMap, final List queryResults, final SelectSQLStatementContext selectSQLStatementContext) throws SQLException { + super(queryResults, selectSQLStatementContext.getOrderByContext().getItems()); this.labelAndIndexMap = labelAndIndexMap; - this.optimizedStatement = optimizedStatement; + this.selectSQLStatementContext = selectSQLStatementContext; currentRow = new ArrayList<>(labelAndIndexMap.size()); currentGroupByValues = getOrderByValuesQueue().isEmpty() - ? Collections.emptyList() : new GroupByValue(getCurrentQueryResult(), optimizedStatement.getGroupBy().getItems()).getGroupValues(); + ? Collections.emptyList() : new GroupByValue(getCurrentQueryResult(), selectSQLStatementContext.getGroupByContext().getItems()).getGroupValues(); } @Override @@ -70,7 +70,7 @@ public final class GroupByStreamMergedResult extends OrderByStreamMergedResult { super.next(); } if (aggregateCurrentGroupByRowAndNext()) { - currentGroupByValues = new GroupByValue(getCurrentQueryResult(), optimizedStatement.getGroupBy().getItems()).getGroupValues(); + currentGroupByValues = new GroupByValue(getCurrentQueryResult(), selectSQLStatementContext.getGroupByContext().getItems()).getGroupValues(); } return true; } @@ -78,14 +78,14 @@ public final class GroupByStreamMergedResult extends OrderByStreamMergedResult { private boolean aggregateCurrentGroupByRowAndNext() throws SQLException { boolean result = false; Map aggregationUnitMap = Maps.toMap( - optimizedStatement.getSelectItems().getAggregationSelectItems(), new Function() { + selectSQLStatementContext.getSelectItemsContext().getAggregationSelectItems(), new Function() { @Override public AggregationUnit apply(final AggregationSelectItem input) { return AggregationUnitFactory.create(input.getType()); } }); - while (currentGroupByValues.equals(new GroupByValue(getCurrentQueryResult(), optimizedStatement.getGroupBy().getItems()).getGroupValues())) { + while (currentGroupByValues.equals(new GroupByValue(getCurrentQueryResult(), selectSQLStatementContext.getGroupByContext().getItems()).getGroupValues())) { aggregate(aggregationUnitMap); cacheCurrentRow(); result = super.next(); diff --git a/sharding-core/sharding-core-merge/src/main/java/org/apache/shardingsphere/core/merge/dql/pagination/LimitDecoratorMergedResult.java b/sharding-core/sharding-core-merge/src/main/java/org/apache/shardingsphere/core/merge/dql/pagination/LimitDecoratorMergedResult.java index f7b683f8e0..6baadb65ae 100644 --- a/sharding-core/sharding-core-merge/src/main/java/org/apache/shardingsphere/core/merge/dql/pagination/LimitDecoratorMergedResult.java +++ b/sharding-core/sharding-core-merge/src/main/java/org/apache/shardingsphere/core/merge/dql/pagination/LimitDecoratorMergedResult.java @@ -19,7 +19,7 @@ package org.apache.shardingsphere.core.merge.dql.pagination; import org.apache.shardingsphere.core.merge.MergedResult; import org.apache.shardingsphere.core.merge.dql.common.DecoratorMergedResult; -import org.apache.shardingsphere.core.optimize.segment.select.pagination.Pagination; +import org.apache.shardingsphere.core.optimize.segment.select.pagination.PaginationContext; import java.sql.SQLException; @@ -30,13 +30,13 @@ import java.sql.SQLException; */ public final class LimitDecoratorMergedResult extends DecoratorMergedResult { - private final Pagination pagination; + private final PaginationContext pagination; private final boolean skipAll; private int rowNumber; - public LimitDecoratorMergedResult(final MergedResult mergedResult, final Pagination pagination) throws SQLException { + public LimitDecoratorMergedResult(final MergedResult mergedResult, final PaginationContext pagination) throws SQLException { super(mergedResult); this.pagination = pagination; skipAll = skipOffset(); diff --git a/sharding-core/sharding-core-merge/src/main/java/org/apache/shardingsphere/core/merge/dql/pagination/RowNumberDecoratorMergedResult.java b/sharding-core/sharding-core-merge/src/main/java/org/apache/shardingsphere/core/merge/dql/pagination/RowNumberDecoratorMergedResult.java index d2b614e304..8addffbb26 100644 --- a/sharding-core/sharding-core-merge/src/main/java/org/apache/shardingsphere/core/merge/dql/pagination/RowNumberDecoratorMergedResult.java +++ b/sharding-core/sharding-core-merge/src/main/java/org/apache/shardingsphere/core/merge/dql/pagination/RowNumberDecoratorMergedResult.java @@ -19,7 +19,7 @@ package org.apache.shardingsphere.core.merge.dql.pagination; import org.apache.shardingsphere.core.merge.MergedResult; import org.apache.shardingsphere.core.merge.dql.common.DecoratorMergedResult; -import org.apache.shardingsphere.core.optimize.segment.select.pagination.Pagination; +import org.apache.shardingsphere.core.optimize.segment.select.pagination.PaginationContext; import java.sql.SQLException; @@ -30,13 +30,13 @@ import java.sql.SQLException; */ public final class RowNumberDecoratorMergedResult extends DecoratorMergedResult { - private final Pagination pagination; + private final PaginationContext pagination; private final boolean skipAll; private long rowNumber; - public RowNumberDecoratorMergedResult(final MergedResult mergedResult, final Pagination pagination) throws SQLException { + public RowNumberDecoratorMergedResult(final MergedResult mergedResult, final PaginationContext pagination) throws SQLException { super(mergedResult); this.pagination = pagination; skipAll = skipOffset(); diff --git a/sharding-core/sharding-core-merge/src/main/java/org/apache/shardingsphere/core/merge/dql/pagination/TopAndRowNumberDecoratorMergedResult.java b/sharding-core/sharding-core-merge/src/main/java/org/apache/shardingsphere/core/merge/dql/pagination/TopAndRowNumberDecoratorMergedResult.java index 1ab8a30418..94f3ce7905 100644 --- a/sharding-core/sharding-core-merge/src/main/java/org/apache/shardingsphere/core/merge/dql/pagination/TopAndRowNumberDecoratorMergedResult.java +++ b/sharding-core/sharding-core-merge/src/main/java/org/apache/shardingsphere/core/merge/dql/pagination/TopAndRowNumberDecoratorMergedResult.java @@ -19,7 +19,7 @@ package org.apache.shardingsphere.core.merge.dql.pagination; import org.apache.shardingsphere.core.merge.MergedResult; import org.apache.shardingsphere.core.merge.dql.common.DecoratorMergedResult; -import org.apache.shardingsphere.core.optimize.segment.select.pagination.Pagination; +import org.apache.shardingsphere.core.optimize.segment.select.pagination.PaginationContext; import java.sql.SQLException; @@ -30,13 +30,13 @@ import java.sql.SQLException; */ public final class TopAndRowNumberDecoratorMergedResult extends DecoratorMergedResult { - private final Pagination pagination; + private final PaginationContext pagination; private final boolean skipAll; private long rowNumber; - public TopAndRowNumberDecoratorMergedResult(final MergedResult mergedResult, final Pagination pagination) throws SQLException { + public TopAndRowNumberDecoratorMergedResult(final MergedResult mergedResult, final PaginationContext pagination) throws SQLException { super(mergedResult); this.pagination = pagination; skipAll = skipOffset(); diff --git a/sharding-core/sharding-core-merge/src/test/java/org/apache/shardingsphere/core/merge/MergeEngineFactoryTest.java b/sharding-core/sharding-core-merge/src/test/java/org/apache/shardingsphere/core/merge/MergeEngineFactoryTest.java index 460bc549b4..b622d754ae 100644 --- a/sharding-core/sharding-core-merge/src/test/java/org/apache/shardingsphere/core/merge/MergeEngineFactoryTest.java +++ b/sharding-core/sharding-core-merge/src/test/java/org/apache/shardingsphere/core/merge/MergeEngineFactoryTest.java @@ -23,15 +23,15 @@ import org.apache.shardingsphere.core.execute.sql.execute.result.QueryResult; import org.apache.shardingsphere.core.merge.dal.DALMergeEngine; import org.apache.shardingsphere.core.merge.dql.DQLMergeEngine; import org.apache.shardingsphere.core.merge.fixture.TestQueryResult; -import org.apache.shardingsphere.core.optimize.statement.impl.CommonOptimizedStatement; -import org.apache.shardingsphere.core.optimize.statement.impl.InsertOptimizedStatement; -import org.apache.shardingsphere.core.optimize.segment.select.groupby.GroupBy; +import org.apache.shardingsphere.core.optimize.statement.impl.CommonSQLStatementContext; +import org.apache.shardingsphere.core.optimize.statement.impl.InsertSQLStatementContext; +import org.apache.shardingsphere.core.optimize.segment.select.groupby.GroupByContext; import org.apache.shardingsphere.core.optimize.segment.select.item.SelectItem; -import org.apache.shardingsphere.core.optimize.segment.select.item.SelectItems; -import org.apache.shardingsphere.core.optimize.segment.select.orderby.OrderBy; +import org.apache.shardingsphere.core.optimize.segment.select.item.SelectItemsContext; +import org.apache.shardingsphere.core.optimize.segment.select.orderby.OrderByContext; import org.apache.shardingsphere.core.optimize.segment.select.orderby.OrderByItem; -import org.apache.shardingsphere.core.optimize.segment.select.pagination.Pagination; -import org.apache.shardingsphere.core.optimize.statement.impl.SelectOptimizedStatement; +import org.apache.shardingsphere.core.optimize.segment.select.pagination.PaginationContext; +import org.apache.shardingsphere.core.optimize.statement.impl.SelectSQLStatementContext; import org.apache.shardingsphere.core.parse.sql.segment.dml.column.ColumnSegment; import org.apache.shardingsphere.core.parse.sql.segment.generic.TableSegment; import org.apache.shardingsphere.core.parse.sql.statement.dal.DALStatement; @@ -74,17 +74,17 @@ public final class MergeEngineFactoryTest { @Test public void assertNewInstanceWithSelectStatement() throws SQLException { SQLRouteResult routeResult = new SQLRouteResult( - new SelectOptimizedStatement(new SelectStatement(), - new GroupBy(Collections.emptyList(), 0), new OrderBy(Collections.emptyList(), false), - new SelectItems(0, 0, false, Collections.emptyList()), new Pagination(null, null, Collections.emptyList())), - new CommonOptimizedStatement(new SelectStatement()), new ShardingConditions(Collections.emptyList())); + new SelectSQLStatementContext(new SelectStatement(), + new GroupByContext(Collections.emptyList(), 0), new OrderByContext(Collections.emptyList(), false), + new SelectItemsContext(0, 0, false, Collections.emptyList()), new PaginationContext(null, null, Collections.emptyList())), + new CommonSQLStatementContext(new SelectStatement()), new ShardingConditions(Collections.emptyList())); assertThat(MergeEngineFactory.newInstance(DatabaseTypes.getActualDatabaseType("MySQL"), null, routeResult, null, queryResults), instanceOf(DQLMergeEngine.class)); } @Test public void assertNewInstanceWithDALStatement() throws SQLException { - SQLRouteResult routeResult = new SQLRouteResult(new CommonOptimizedStatement(new DALStatement()), - new CommonOptimizedStatement(new DALStatement()), new ShardingConditions(Collections.emptyList())); + SQLRouteResult routeResult = new SQLRouteResult(new CommonSQLStatementContext(new DALStatement()), + new CommonSQLStatementContext(new DALStatement()), new ShardingConditions(Collections.emptyList())); assertThat(MergeEngineFactory.newInstance(DatabaseTypes.getActualDatabaseType("MySQL"), null, routeResult, null, queryResults), instanceOf(DALMergeEngine.class)); } @@ -94,8 +94,8 @@ public final class MergeEngineFactoryTest { insertStatement.getAllSQLSegments().add(new TableSegment(0, 0, "tbl")); insertStatement.getColumns().add(new ColumnSegment(0, 0, "col")); SQLRouteResult routeResult = new SQLRouteResult( - new InsertOptimizedStatement(null, Collections.emptyList(), insertStatement), - new CommonOptimizedStatement(insertStatement), new ShardingConditions(Collections.emptyList())); + new InsertSQLStatementContext(null, Collections.emptyList(), insertStatement), + new CommonSQLStatementContext(insertStatement), new ShardingConditions(Collections.emptyList())); assertThat(MergeEngineFactory.newInstance(DatabaseTypes.getActualDatabaseType("MySQL"), null, routeResult, null, queryResults), instanceOf(TransparentMergeEngine.class)); } } diff --git a/sharding-core/sharding-core-merge/src/test/java/org/apache/shardingsphere/core/merge/dal/DALMergeEngineTest.java b/sharding-core/sharding-core-merge/src/test/java/org/apache/shardingsphere/core/merge/dal/DALMergeEngineTest.java index 0e56c8ad3e..2833d42113 100644 --- a/sharding-core/sharding-core-merge/src/test/java/org/apache/shardingsphere/core/merge/dal/DALMergeEngineTest.java +++ b/sharding-core/sharding-core-merge/src/test/java/org/apache/shardingsphere/core/merge/dal/DALMergeEngineTest.java @@ -24,7 +24,7 @@ import org.apache.shardingsphere.core.merge.dal.show.ShowDatabasesMergedResult; import org.apache.shardingsphere.core.merge.dal.show.ShowOtherMergedResult; import org.apache.shardingsphere.core.merge.dal.show.ShowTablesMergedResult; import org.apache.shardingsphere.core.merge.fixture.TestQueryResult; -import org.apache.shardingsphere.core.optimize.statement.OptimizedStatement; +import org.apache.shardingsphere.core.optimize.statement.SQLStatementContext; import org.apache.shardingsphere.core.parse.sql.statement.dal.DALStatement; import org.apache.shardingsphere.core.parse.sql.statement.dal.dialect.mysql.DescribeStatement; import org.apache.shardingsphere.core.parse.sql.statement.dal.dialect.mysql.ShowCreateTableStatement; @@ -57,46 +57,46 @@ public final class DALMergeEngineTest { @Test public void assertMergeForShowDatabasesStatement() throws SQLException { DALStatement dalStatement = new ShowDatabasesStatement(); - OptimizedStatement optimizedStatement = mockOptimizedStatement(dalStatement); - DALMergeEngine dalMergeEngine = new DALMergeEngine(null, queryResults, optimizedStatement, null); + SQLStatementContext sqlStatementContext = mockSQLStatementContext(dalStatement); + DALMergeEngine dalMergeEngine = new DALMergeEngine(null, queryResults, sqlStatementContext, null); assertThat(dalMergeEngine.merge(), instanceOf(ShowDatabasesMergedResult.class)); } @Test public void assertMergeForShowShowTablesStatement() throws SQLException { DALStatement dalStatement = new ShowTablesStatement(); - OptimizedStatement optimizedStatement = mockOptimizedStatement(dalStatement); - DALMergeEngine dalMergeEngine = new DALMergeEngine(null, queryResults, optimizedStatement, null); + SQLStatementContext sqlStatementContext = mockSQLStatementContext(dalStatement); + DALMergeEngine dalMergeEngine = new DALMergeEngine(null, queryResults, sqlStatementContext, null); assertThat(dalMergeEngine.merge(), instanceOf(ShowTablesMergedResult.class)); } @Test public void assertMergeForShowCreateTableStatement() throws SQLException { DALStatement dalStatement = new ShowCreateTableStatement(); - OptimizedStatement optimizedStatement = mockOptimizedStatement(dalStatement); - DALMergeEngine dalMergeEngine = new DALMergeEngine(null, queryResults, optimizedStatement, null); + SQLStatementContext sqlStatementContext = mockSQLStatementContext(dalStatement); + DALMergeEngine dalMergeEngine = new DALMergeEngine(null, queryResults, sqlStatementContext, null); assertThat(dalMergeEngine.merge(), instanceOf(ShowCreateTableMergedResult.class)); } @Test public void assertMergeForShowOtherStatement() throws SQLException { DALStatement dalStatement = new ShowOtherStatement(); - OptimizedStatement optimizedStatement = mockOptimizedStatement(dalStatement); - DALMergeEngine dalMergeEngine = new DALMergeEngine(null, queryResults, optimizedStatement, null); + SQLStatementContext sqlStatementContext = mockSQLStatementContext(dalStatement); + DALMergeEngine dalMergeEngine = new DALMergeEngine(null, queryResults, sqlStatementContext, null); assertThat(dalMergeEngine.merge(), instanceOf(ShowOtherMergedResult.class)); } @Test public void assertMergeForDescribeStatement() throws SQLException { DALStatement dalStatement = new DescribeStatement(); - OptimizedStatement optimizedStatement = mockOptimizedStatement(dalStatement); - DALMergeEngine dalMergeEngine = new DALMergeEngine(null, queryResults, optimizedStatement, null); + SQLStatementContext sqlStatementContext = mockSQLStatementContext(dalStatement); + DALMergeEngine dalMergeEngine = new DALMergeEngine(null, queryResults, sqlStatementContext, null); assertThat(dalMergeEngine.merge(), instanceOf(DescribeTableMergedResult.class)); } - private OptimizedStatement mockOptimizedStatement(final DALStatement dalStatement) { - OptimizedStatement optimizedStatement = mock(OptimizedStatement.class); - when(optimizedStatement.getSqlStatement()).thenReturn(dalStatement); - return optimizedStatement; + private SQLStatementContext mockSQLStatementContext(final DALStatement dalStatement) { + SQLStatementContext result = mock(SQLStatementContext.class); + when(result.getSqlStatement()).thenReturn(dalStatement); + return result; } } diff --git a/sharding-core/sharding-core-merge/src/test/java/org/apache/shardingsphere/core/merge/dal/desc/DescribeTableMergedResultTest.java b/sharding-core/sharding-core-merge/src/test/java/org/apache/shardingsphere/core/merge/dal/desc/DescribeTableMergedResultTest.java index cef13dfbdd..ee9bdecd37 100644 --- a/sharding-core/sharding-core-merge/src/test/java/org/apache/shardingsphere/core/merge/dal/desc/DescribeTableMergedResultTest.java +++ b/sharding-core/sharding-core-merge/src/test/java/org/apache/shardingsphere/core/merge/dal/desc/DescribeTableMergedResultTest.java @@ -21,8 +21,8 @@ import com.google.common.base.Optional; import com.google.common.collect.Lists; import org.apache.shardingsphere.core.execute.sql.execute.result.QueryResult; import org.apache.shardingsphere.core.merge.fixture.DescribeQueryResultFixture; -import org.apache.shardingsphere.core.optimize.segment.Tables; -import org.apache.shardingsphere.core.optimize.statement.OptimizedStatement; +import org.apache.shardingsphere.core.optimize.segment.table.TablesContext; +import org.apache.shardingsphere.core.optimize.statement.SQLStatementContext; import org.apache.shardingsphere.core.rule.EncryptRule; import org.apache.shardingsphere.core.rule.ShardingRule; import org.apache.shardingsphere.core.strategy.encrypt.EncryptTable; @@ -44,14 +44,14 @@ public final class DescribeTableMergedResultTest { private List queryResults; - private OptimizedStatement optimizedStatement; + private SQLStatementContext sqlStatementContext; @Before public void setUp() throws SQLException { - optimizedStatement = mock(OptimizedStatement.class); - Tables tables = mock(Tables.class); - when(tables.getSingleTableName()).thenReturn("user"); - when(optimizedStatement.getTables()).thenReturn(tables); + sqlStatementContext = mock(SQLStatementContext.class); + TablesContext tablesContext = mock(TablesContext.class); + when(tablesContext.getSingleTableName()).thenReturn("user"); + when(sqlStatementContext.getTablesContext()).thenReturn(tablesContext); List mockQueryResults = mockQueryResults(); QueryResult queryResult = new DescribeQueryResultFixture(mockQueryResults.iterator()); queryResults = Lists.newArrayList(queryResult); @@ -101,14 +101,14 @@ public final class DescribeTableMergedResultTest { public void assertNextForEmptyQueryResult() throws SQLException { ShardingRule shardingRule = mock(ShardingRule.class); List queryResults = Collections.emptyList(); - DescribeTableMergedResult describeTableMergedResult = new DescribeTableMergedResult(shardingRule, queryResults, optimizedStatement); + DescribeTableMergedResult describeTableMergedResult = new DescribeTableMergedResult(shardingRule, queryResults, sqlStatementContext); assertFalse(describeTableMergedResult.next()); } @Test public void assertFieldWithEncryptRule() throws SQLException { ShardingRule shardingRule = mockShardingRuleWithEncryptRule(); - DescribeTableMergedResult describeTableMergedResult = new DescribeTableMergedResult(shardingRule, queryResults, optimizedStatement); + DescribeTableMergedResult describeTableMergedResult = new DescribeTableMergedResult(shardingRule, queryResults, sqlStatementContext); assertTrue(describeTableMergedResult.next()); assertEquals(describeTableMergedResult.getValue(1, String.class), "id"); assertEquals(describeTableMergedResult.getValue("Field", String.class), "id"); @@ -124,7 +124,7 @@ public final class DescribeTableMergedResultTest { @Test public void assertFieldWithoutEncryptRule() throws SQLException { ShardingRule shardingRule = mockShardingRuleWithoutEncryptRule(); - DescribeTableMergedResult describeTableMergedResult = new DescribeTableMergedResult(shardingRule, queryResults, optimizedStatement); + DescribeTableMergedResult describeTableMergedResult = new DescribeTableMergedResult(shardingRule, queryResults, sqlStatementContext); assertTrue(describeTableMergedResult.next()); assertEquals(describeTableMergedResult.getValue(1, String.class), "id"); assertEquals(describeTableMergedResult.getValue("Field", String.class), "id"); @@ -142,7 +142,7 @@ public final class DescribeTableMergedResultTest { @Test public void assertAllWithoutEncryptRule() throws SQLException { ShardingRule shardingRule = mockShardingRuleWithoutEncryptRule(); - DescribeTableMergedResult describeTableMergedResult = new DescribeTableMergedResult(shardingRule, queryResults, optimizedStatement); + DescribeTableMergedResult describeTableMergedResult = new DescribeTableMergedResult(shardingRule, queryResults, sqlStatementContext); assertTrue(describeTableMergedResult.next()); assertEquals(describeTableMergedResult.getValue(1, String.class), "id"); assertEquals(describeTableMergedResult.getValue("Field", String.class), "id"); @@ -161,7 +161,7 @@ public final class DescribeTableMergedResultTest { @Test public void assertAllWithEncryptRule() throws SQLException { ShardingRule shardingRule = mockShardingRuleWithEncryptRule(); - DescribeTableMergedResult describeTableMergedResult = new DescribeTableMergedResult(shardingRule, queryResults, optimizedStatement); + DescribeTableMergedResult describeTableMergedResult = new DescribeTableMergedResult(shardingRule, queryResults, sqlStatementContext); assertTrue(describeTableMergedResult.next()); assertEquals(describeTableMergedResult.getValue(1, String.class), "id"); assertEquals(describeTableMergedResult.getValue("Field", String.class), "id"); diff --git a/sharding-core/sharding-core-merge/src/test/java/org/apache/shardingsphere/core/merge/dql/DQLMergeEngineTest.java b/sharding-core/sharding-core-merge/src/test/java/org/apache/shardingsphere/core/merge/dql/DQLMergeEngineTest.java index 09e0c9f354..9a7b905358 100644 --- a/sharding-core/sharding-core-merge/src/test/java/org/apache/shardingsphere/core/merge/dql/DQLMergeEngineTest.java +++ b/sharding-core/sharding-core-merge/src/test/java/org/apache/shardingsphere/core/merge/dql/DQLMergeEngineTest.java @@ -29,16 +29,16 @@ import org.apache.shardingsphere.core.merge.dql.pagination.LimitDecoratorMergedR import org.apache.shardingsphere.core.merge.dql.pagination.RowNumberDecoratorMergedResult; import org.apache.shardingsphere.core.merge.dql.pagination.TopAndRowNumberDecoratorMergedResult; import org.apache.shardingsphere.core.merge.fixture.TestQueryResult; -import org.apache.shardingsphere.core.optimize.segment.select.groupby.GroupBy; +import org.apache.shardingsphere.core.optimize.segment.select.groupby.GroupByContext; import org.apache.shardingsphere.core.optimize.segment.select.item.impl.AggregationSelectItem; import org.apache.shardingsphere.core.optimize.segment.select.item.SelectItem; -import org.apache.shardingsphere.core.optimize.segment.select.item.SelectItems; -import org.apache.shardingsphere.core.optimize.segment.select.orderby.OrderBy; +import org.apache.shardingsphere.core.optimize.segment.select.item.SelectItemsContext; +import org.apache.shardingsphere.core.optimize.segment.select.orderby.OrderByContext; import org.apache.shardingsphere.core.optimize.segment.select.orderby.OrderByItem; -import org.apache.shardingsphere.core.optimize.segment.select.pagination.Pagination; -import org.apache.shardingsphere.core.optimize.statement.OptimizedStatement; -import org.apache.shardingsphere.core.optimize.statement.impl.CommonOptimizedStatement; -import org.apache.shardingsphere.core.optimize.statement.impl.SelectOptimizedStatement; +import org.apache.shardingsphere.core.optimize.segment.select.pagination.PaginationContext; +import org.apache.shardingsphere.core.optimize.statement.SQLStatementContext; +import org.apache.shardingsphere.core.optimize.statement.impl.CommonSQLStatementContext; +import org.apache.shardingsphere.core.optimize.statement.impl.SelectSQLStatementContext; import org.apache.shardingsphere.core.parse.core.constant.AggregationType; import org.apache.shardingsphere.core.parse.core.constant.OrderDirection; import org.apache.shardingsphere.core.parse.sql.segment.dml.order.item.IndexOrderByItemSegment; @@ -97,35 +97,35 @@ public final class DQLMergeEngineTest { @Test public void assertBuildIteratorStreamMergedResult() throws SQLException { - OptimizedStatement shardingStatement = new SelectOptimizedStatement(new SelectStatement(), - new GroupBy(Collections.emptyList(), 0), new OrderBy(Collections.emptyList(), false), - new SelectItems(0, 0, false, Collections.emptyList()), new Pagination(null, null, Collections.emptyList())); + SQLStatementContext selectSQLStatementContext = new SelectSQLStatementContext(new SelectStatement(), + new GroupByContext(Collections.emptyList(), 0), new OrderByContext(Collections.emptyList(), false), + new SelectItemsContext(0, 0, false, Collections.emptyList()), new PaginationContext(null, null, Collections.emptyList())); SQLRouteResult routeResult = new SQLRouteResult( - shardingStatement, new CommonOptimizedStatement(new SelectStatement()), new ShardingConditions(Collections.emptyList())); + selectSQLStatementContext, new CommonSQLStatementContext(new SelectStatement()), new ShardingConditions(Collections.emptyList())); mergeEngine = new DQLMergeEngine(DatabaseTypes.getActualDatabaseType("MySQL"), null, routeResult, queryResults); assertThat(mergeEngine.merge(), instanceOf(IteratorStreamMergedResult.class)); } @Test public void assertBuildIteratorStreamMergedResultWithLimit() throws SQLException { - OptimizedStatement shardingStatement = new SelectOptimizedStatement(new SelectStatement(), - new GroupBy(Collections.emptyList(), 0), new OrderBy(Collections.emptyList(), false), - new SelectItems(0, 0, false, Collections.emptyList()), - new Pagination(new NumberLiteralLimitValueSegment(0, 0, 1), null, Collections.emptyList())); + SQLStatementContext selectSQLStatementContext = new SelectSQLStatementContext(new SelectStatement(), + new GroupByContext(Collections.emptyList(), 0), new OrderByContext(Collections.emptyList(), false), + new SelectItemsContext(0, 0, false, Collections.emptyList()), + new PaginationContext(new NumberLiteralLimitValueSegment(0, 0, 1), null, Collections.emptyList())); SQLRouteResult routeResult = new SQLRouteResult( - shardingStatement, new CommonOptimizedStatement(new SelectStatement()), new ShardingConditions(Collections.emptyList())); + selectSQLStatementContext, new CommonSQLStatementContext(new SelectStatement()), new ShardingConditions(Collections.emptyList())); mergeEngine = new DQLMergeEngine(DatabaseTypes.getActualDatabaseType("MySQL"), null, routeResult, singleQueryResult); assertThat(mergeEngine.merge(), instanceOf(IteratorStreamMergedResult.class)); } @Test public void assertBuildIteratorStreamMergedResultWithMySQLLimit() throws SQLException { - OptimizedStatement shardingStatement = new SelectOptimizedStatement(new SelectStatement(), - new GroupBy(Collections.emptyList(), 0), new OrderBy(Collections.emptyList(), false), - new SelectItems(0, 0, false, Collections.emptyList()), - new Pagination(new NumberLiteralLimitValueSegment(0, 0, 1), null, Collections.emptyList())); + SQLStatementContext selectSQLStatementContext = new SelectSQLStatementContext(new SelectStatement(), + new GroupByContext(Collections.emptyList(), 0), new OrderByContext(Collections.emptyList(), false), + new SelectItemsContext(0, 0, false, Collections.emptyList()), + new PaginationContext(new NumberLiteralLimitValueSegment(0, 0, 1), null, Collections.emptyList())); SQLRouteResult routeResult = new SQLRouteResult( - shardingStatement, new CommonOptimizedStatement(new SelectStatement()), new ShardingConditions(Collections.emptyList())); + selectSQLStatementContext, new CommonSQLStatementContext(new SelectStatement()), new ShardingConditions(Collections.emptyList())); mergeEngine = new DQLMergeEngine(DatabaseTypes.getActualDatabaseType("MySQL"), null, routeResult, queryResults); MergedResult actual = mergeEngine.merge(); assertThat(actual, instanceOf(LimitDecoratorMergedResult.class)); @@ -134,12 +134,12 @@ public final class DQLMergeEngineTest { @Test public void assertBuildIteratorStreamMergedResultWithOracleLimit() throws SQLException { - OptimizedStatement shardingStatement = new SelectOptimizedStatement(new SelectStatement(), - new GroupBy(Collections.emptyList(), 0), new OrderBy(Collections.emptyList(), false), - new SelectItems(0, 0, false, Collections.emptyList()), - new Pagination(new NumberLiteralRowNumberValueSegment(0, 0, 1, true), null, Collections.emptyList())); + SQLStatementContext selectSQLStatementContext = new SelectSQLStatementContext(new SelectStatement(), + new GroupByContext(Collections.emptyList(), 0), new OrderByContext(Collections.emptyList(), false), + new SelectItemsContext(0, 0, false, Collections.emptyList()), + new PaginationContext(new NumberLiteralRowNumberValueSegment(0, 0, 1, true), null, Collections.emptyList())); SQLRouteResult routeResult = new SQLRouteResult( - shardingStatement, new CommonOptimizedStatement(new SelectStatement()), new ShardingConditions(Collections.emptyList())); + selectSQLStatementContext, new CommonSQLStatementContext(new SelectStatement()), new ShardingConditions(Collections.emptyList())); mergeEngine = new DQLMergeEngine(DatabaseTypes.getActualDatabaseType("Oracle"), null, routeResult, queryResults); MergedResult actual = mergeEngine.merge(); assertThat(actual, instanceOf(RowNumberDecoratorMergedResult.class)); @@ -148,12 +148,12 @@ public final class DQLMergeEngineTest { @Test public void assertBuildIteratorStreamMergedResultWithSQLServerLimit() throws SQLException { - OptimizedStatement shardingStatement = new SelectOptimizedStatement(new SelectStatement(), - new GroupBy(Collections.emptyList(), 0), new OrderBy(Collections.emptyList(), false), - new SelectItems(0, 0, false, Collections.emptyList()), - new Pagination(new NumberLiteralLimitValueSegment(0, 0, 1), null, Collections.emptyList())); + SQLStatementContext selectSQLStatementContext = new SelectSQLStatementContext(new SelectStatement(), + new GroupByContext(Collections.emptyList(), 0), new OrderByContext(Collections.emptyList(), false), + new SelectItemsContext(0, 0, false, Collections.emptyList()), + new PaginationContext(new NumberLiteralLimitValueSegment(0, 0, 1), null, Collections.emptyList())); SQLRouteResult routeResult = new SQLRouteResult( - shardingStatement, new CommonOptimizedStatement(new SelectStatement()), new ShardingConditions(Collections.emptyList())); + selectSQLStatementContext, new CommonSQLStatementContext(new SelectStatement()), new ShardingConditions(Collections.emptyList())); mergeEngine = new DQLMergeEngine(DatabaseTypes.getActualDatabaseType("SQLServer"), null, routeResult, queryResults); MergedResult actual = mergeEngine.merge(); assertThat(actual, instanceOf(TopAndRowNumberDecoratorMergedResult.class)); @@ -162,25 +162,25 @@ public final class DQLMergeEngineTest { @Test public void assertBuildOrderByStreamMergedResult() throws SQLException { - OptimizedStatement shardingStatement = new SelectOptimizedStatement( - new SelectStatement(), new GroupBy(Collections.emptyList(), 0), - new OrderBy(Collections.singletonList(new OrderByItem(new IndexOrderByItemSegment(0, 0, 1, OrderDirection.DESC, OrderDirection.ASC))), false), - new SelectItems(0, 0, false, Collections.emptyList()), new Pagination(null, null, Collections.emptyList())); + SQLStatementContext selectSQLStatementContext = new SelectSQLStatementContext( + new SelectStatement(), new GroupByContext(Collections.emptyList(), 0), + new OrderByContext(Collections.singletonList(new OrderByItem(new IndexOrderByItemSegment(0, 0, 1, OrderDirection.DESC, OrderDirection.ASC))), false), + new SelectItemsContext(0, 0, false, Collections.emptyList()), new PaginationContext(null, null, Collections.emptyList())); SQLRouteResult routeResult = new SQLRouteResult( - shardingStatement, new CommonOptimizedStatement(new SelectStatement()), new ShardingConditions(Collections.emptyList())); + selectSQLStatementContext, new CommonSQLStatementContext(new SelectStatement()), new ShardingConditions(Collections.emptyList())); mergeEngine = new DQLMergeEngine(DatabaseTypes.getActualDatabaseType("MySQL"), null, routeResult, queryResults); assertThat(mergeEngine.merge(), instanceOf(OrderByStreamMergedResult.class)); } @Test public void assertBuildOrderByStreamMergedResultWithMySQLLimit() throws SQLException { - OptimizedStatement shardingStatement = new SelectOptimizedStatement( - new SelectStatement(), new GroupBy(Collections.emptyList(), 0), - new OrderBy(Collections.singletonList(new OrderByItem(new IndexOrderByItemSegment(0, 0, 1, OrderDirection.DESC, OrderDirection.ASC))), false), - new SelectItems(0, 0, false, Collections.emptyList()), - new Pagination(new NumberLiteralLimitValueSegment(0, 0, 1), null, Collections.emptyList())); + SQLStatementContext selectSQLStatementContext = new SelectSQLStatementContext( + new SelectStatement(), new GroupByContext(Collections.emptyList(), 0), + new OrderByContext(Collections.singletonList(new OrderByItem(new IndexOrderByItemSegment(0, 0, 1, OrderDirection.DESC, OrderDirection.ASC))), false), + new SelectItemsContext(0, 0, false, Collections.emptyList()), + new PaginationContext(new NumberLiteralLimitValueSegment(0, 0, 1), null, Collections.emptyList())); SQLRouteResult routeResult = new SQLRouteResult( - shardingStatement, new CommonOptimizedStatement(new SelectStatement()), new ShardingConditions(Collections.emptyList())); + selectSQLStatementContext, new CommonSQLStatementContext(new SelectStatement()), new ShardingConditions(Collections.emptyList())); mergeEngine = new DQLMergeEngine(DatabaseTypes.getActualDatabaseType("MySQL"), null, routeResult, queryResults); MergedResult actual = mergeEngine.merge(); assertThat(actual, instanceOf(LimitDecoratorMergedResult.class)); @@ -189,13 +189,13 @@ public final class DQLMergeEngineTest { @Test public void assertBuildOrderByStreamMergedResultWithOracleLimit() throws SQLException { - OptimizedStatement shardingStatement = new SelectOptimizedStatement( - new SelectStatement(), new GroupBy(Collections.emptyList(), 0), - new OrderBy(Collections.singletonList(new OrderByItem(new IndexOrderByItemSegment(0, 0, 1, OrderDirection.DESC, OrderDirection.ASC))), false), - new SelectItems(0, 0, false, Collections.emptyList()), - new Pagination(new NumberLiteralRowNumberValueSegment(0, 0, 1, true), null, Collections.emptyList())); + SQLStatementContext selectSQLStatementContext = new SelectSQLStatementContext( + new SelectStatement(), new GroupByContext(Collections.emptyList(), 0), + new OrderByContext(Collections.singletonList(new OrderByItem(new IndexOrderByItemSegment(0, 0, 1, OrderDirection.DESC, OrderDirection.ASC))), false), + new SelectItemsContext(0, 0, false, Collections.emptyList()), + new PaginationContext(new NumberLiteralRowNumberValueSegment(0, 0, 1, true), null, Collections.emptyList())); SQLRouteResult routeResult = new SQLRouteResult( - shardingStatement, new CommonOptimizedStatement(new SelectStatement()), new ShardingConditions(Collections.emptyList())); + selectSQLStatementContext, new CommonSQLStatementContext(new SelectStatement()), new ShardingConditions(Collections.emptyList())); mergeEngine = new DQLMergeEngine(DatabaseTypes.getActualDatabaseType("Oracle"), null, routeResult, queryResults); MergedResult actual = mergeEngine.merge(); assertThat(actual, instanceOf(RowNumberDecoratorMergedResult.class)); @@ -204,13 +204,13 @@ public final class DQLMergeEngineTest { @Test public void assertBuildOrderByStreamMergedResultWithSQLServerLimit() throws SQLException { - OptimizedStatement shardingStatement = new SelectOptimizedStatement( - new SelectStatement(), new GroupBy(Collections.emptyList(), 0), - new OrderBy(Collections.singletonList(new OrderByItem(new IndexOrderByItemSegment(0, 0, 1, OrderDirection.DESC, OrderDirection.ASC))), false), - new SelectItems(0, 0, false, Collections.emptyList()), - new Pagination(new NumberLiteralRowNumberValueSegment(0, 0, 1, true), null, Collections.emptyList())); + SQLStatementContext selectSQLStatementContext = new SelectSQLStatementContext( + new SelectStatement(), new GroupByContext(Collections.emptyList(), 0), + new OrderByContext(Collections.singletonList(new OrderByItem(new IndexOrderByItemSegment(0, 0, 1, OrderDirection.DESC, OrderDirection.ASC))), false), + new SelectItemsContext(0, 0, false, Collections.emptyList()), + new PaginationContext(new NumberLiteralRowNumberValueSegment(0, 0, 1, true), null, Collections.emptyList())); SQLRouteResult routeResult = new SQLRouteResult( - shardingStatement, new CommonOptimizedStatement(new SelectStatement()), new ShardingConditions(Collections.emptyList())); + selectSQLStatementContext, new CommonSQLStatementContext(new SelectStatement()), new ShardingConditions(Collections.emptyList())); mergeEngine = new DQLMergeEngine(DatabaseTypes.getActualDatabaseType("SQLServer"), null, routeResult, queryResults); MergedResult actual = mergeEngine.merge(); assertThat(actual, instanceOf(TopAndRowNumberDecoratorMergedResult.class)); @@ -219,25 +219,25 @@ public final class DQLMergeEngineTest { @Test public void assertBuildGroupByStreamMergedResult() throws SQLException { - OptimizedStatement shardingStatement = new SelectOptimizedStatement(new SelectStatement(), - new GroupBy(Collections.singletonList(new OrderByItem(new IndexOrderByItemSegment(0, 0, 1, OrderDirection.DESC, OrderDirection.ASC))), 0), - new OrderBy(Collections.singletonList(new OrderByItem(new IndexOrderByItemSegment(0, 0, 1, OrderDirection.DESC, OrderDirection.ASC))), false), - new SelectItems(0, 0, false, Collections.emptyList()), new Pagination(null, null, Collections.emptyList())); + SQLStatementContext selectSQLStatementContext = new SelectSQLStatementContext(new SelectStatement(), + new GroupByContext(Collections.singletonList(new OrderByItem(new IndexOrderByItemSegment(0, 0, 1, OrderDirection.DESC, OrderDirection.ASC))), 0), + new OrderByContext(Collections.singletonList(new OrderByItem(new IndexOrderByItemSegment(0, 0, 1, OrderDirection.DESC, OrderDirection.ASC))), false), + new SelectItemsContext(0, 0, false, Collections.emptyList()), new PaginationContext(null, null, Collections.emptyList())); SQLRouteResult routeResult = new SQLRouteResult( - shardingStatement, new CommonOptimizedStatement(new SelectStatement()), new ShardingConditions(Collections.emptyList())); + selectSQLStatementContext, new CommonSQLStatementContext(new SelectStatement()), new ShardingConditions(Collections.emptyList())); mergeEngine = new DQLMergeEngine(DatabaseTypes.getActualDatabaseType("MySQL"), null, routeResult, queryResults); assertThat(mergeEngine.merge(), instanceOf(GroupByStreamMergedResult.class)); } @Test public void assertBuildGroupByStreamMergedResultWithMySQLLimit() throws SQLException { - OptimizedStatement shardingStatement = new SelectOptimizedStatement(new SelectStatement(), - new GroupBy(Collections.singletonList(new OrderByItem(new IndexOrderByItemSegment(0, 0, 1, OrderDirection.DESC, OrderDirection.ASC))), 0), - new OrderBy(Collections.singletonList(new OrderByItem(new IndexOrderByItemSegment(0, 0, 1, OrderDirection.DESC, OrderDirection.ASC))), false), - new SelectItems(0, 0, false, Collections.emptyList()), - new Pagination(new NumberLiteralLimitValueSegment(0, 0, 1), null, Collections.emptyList())); + SQLStatementContext selectSQLStatementContext = new SelectSQLStatementContext(new SelectStatement(), + new GroupByContext(Collections.singletonList(new OrderByItem(new IndexOrderByItemSegment(0, 0, 1, OrderDirection.DESC, OrderDirection.ASC))), 0), + new OrderByContext(Collections.singletonList(new OrderByItem(new IndexOrderByItemSegment(0, 0, 1, OrderDirection.DESC, OrderDirection.ASC))), false), + new SelectItemsContext(0, 0, false, Collections.emptyList()), + new PaginationContext(new NumberLiteralLimitValueSegment(0, 0, 1), null, Collections.emptyList())); SQLRouteResult routeResult = new SQLRouteResult( - shardingStatement, new CommonOptimizedStatement(new SelectStatement()), new ShardingConditions(Collections.emptyList())); + selectSQLStatementContext, new CommonSQLStatementContext(new SelectStatement()), new ShardingConditions(Collections.emptyList())); mergeEngine = new DQLMergeEngine(DatabaseTypes.getActualDatabaseType("MySQL"), null, routeResult, queryResults); MergedResult actual = mergeEngine.merge(); assertThat(actual, instanceOf(LimitDecoratorMergedResult.class)); @@ -246,13 +246,13 @@ public final class DQLMergeEngineTest { @Test public void assertBuildGroupByStreamMergedResultWithOracleLimit() throws SQLException { - OptimizedStatement shardingStatement = new SelectOptimizedStatement(new SelectStatement(), - new GroupBy(Collections.singletonList(new OrderByItem(new IndexOrderByItemSegment(0, 0, 1, OrderDirection.DESC, OrderDirection.ASC))), 0), - new OrderBy(Collections.singletonList(new OrderByItem(new IndexOrderByItemSegment(0, 0, 1, OrderDirection.DESC, OrderDirection.ASC))), false), - new SelectItems(0, 0, false, Collections.emptyList()), - new Pagination(new NumberLiteralRowNumberValueSegment(0, 0, 1, true), null, Collections.emptyList())); + SQLStatementContext selectSQLStatementContext = new SelectSQLStatementContext(new SelectStatement(), + new GroupByContext(Collections.singletonList(new OrderByItem(new IndexOrderByItemSegment(0, 0, 1, OrderDirection.DESC, OrderDirection.ASC))), 0), + new OrderByContext(Collections.singletonList(new OrderByItem(new IndexOrderByItemSegment(0, 0, 1, OrderDirection.DESC, OrderDirection.ASC))), false), + new SelectItemsContext(0, 0, false, Collections.emptyList()), + new PaginationContext(new NumberLiteralRowNumberValueSegment(0, 0, 1, true), null, Collections.emptyList())); SQLRouteResult routeResult = new SQLRouteResult( - shardingStatement, new CommonOptimizedStatement(new SelectStatement()), new ShardingConditions(Collections.emptyList())); + selectSQLStatementContext, new CommonSQLStatementContext(new SelectStatement()), new ShardingConditions(Collections.emptyList())); mergeEngine = new DQLMergeEngine(DatabaseTypes.getActualDatabaseType("Oracle"), null, routeResult, queryResults); MergedResult actual = mergeEngine.merge(); assertThat(actual, instanceOf(RowNumberDecoratorMergedResult.class)); @@ -261,13 +261,13 @@ public final class DQLMergeEngineTest { @Test public void assertBuildGroupByStreamMergedResultWithSQLServerLimit() throws SQLException { - OptimizedStatement shardingStatement = new SelectOptimizedStatement(new SelectStatement(), - new GroupBy(Collections.singletonList(new OrderByItem(new IndexOrderByItemSegment(0, 0, 1, OrderDirection.DESC, OrderDirection.ASC))), 0), - new OrderBy(Collections.singletonList(new OrderByItem(new IndexOrderByItemSegment(0, 0, 1, OrderDirection.DESC, OrderDirection.ASC))), false), - new SelectItems(0, 0, false, Collections.emptyList()), - new Pagination(new NumberLiteralRowNumberValueSegment(0, 0, 1, true), null, Collections.emptyList())); + SQLStatementContext selectSQLStatementContext = new SelectSQLStatementContext(new SelectStatement(), + new GroupByContext(Collections.singletonList(new OrderByItem(new IndexOrderByItemSegment(0, 0, 1, OrderDirection.DESC, OrderDirection.ASC))), 0), + new OrderByContext(Collections.singletonList(new OrderByItem(new IndexOrderByItemSegment(0, 0, 1, OrderDirection.DESC, OrderDirection.ASC))), false), + new SelectItemsContext(0, 0, false, Collections.emptyList()), + new PaginationContext(new NumberLiteralRowNumberValueSegment(0, 0, 1, true), null, Collections.emptyList())); SQLRouteResult routeResult = new SQLRouteResult( - shardingStatement, new CommonOptimizedStatement(new SelectStatement()), new ShardingConditions(Collections.emptyList())); + selectSQLStatementContext, new CommonSQLStatementContext(new SelectStatement()), new ShardingConditions(Collections.emptyList())); mergeEngine = new DQLMergeEngine(DatabaseTypes.getActualDatabaseType("SQLServer"), null, routeResult, queryResults); MergedResult actual = mergeEngine.merge(); assertThat(actual, instanceOf(TopAndRowNumberDecoratorMergedResult.class)); @@ -276,24 +276,24 @@ public final class DQLMergeEngineTest { @Test public void assertBuildGroupByMemoryMergedResult() throws SQLException { - OptimizedStatement shardingStatement = new SelectOptimizedStatement(new SelectStatement(), - new GroupBy(Collections.singletonList(new OrderByItem(new IndexOrderByItemSegment(0, 0, 1, OrderDirection.DESC, OrderDirection.ASC))), 0), - new OrderBy(Collections.emptyList(), false), new SelectItems(0, 0, false, Collections.emptyList()), - new Pagination(null, null, Collections.emptyList())); + SQLStatementContext selectSQLStatementContext = new SelectSQLStatementContext(new SelectStatement(), + new GroupByContext(Collections.singletonList(new OrderByItem(new IndexOrderByItemSegment(0, 0, 1, OrderDirection.DESC, OrderDirection.ASC))), 0), + new OrderByContext(Collections.emptyList(), false), new SelectItemsContext(0, 0, false, Collections.emptyList()), + new PaginationContext(null, null, Collections.emptyList())); SQLRouteResult routeResult = new SQLRouteResult( - shardingStatement, new CommonOptimizedStatement(new SelectStatement()), new ShardingConditions(Collections.emptyList())); + selectSQLStatementContext, new CommonSQLStatementContext(new SelectStatement()), new ShardingConditions(Collections.emptyList())); mergeEngine = new DQLMergeEngine(DatabaseTypes.getActualDatabaseType("MySQL"), null, routeResult, queryResults); assertThat(mergeEngine.merge(), instanceOf(GroupByMemoryMergedResult.class)); } @Test public void assertBuildGroupByMemoryMergedResultWithMySQLLimit() throws SQLException { - OptimizedStatement shardingStatement = new SelectOptimizedStatement(new SelectStatement(), - new GroupBy(Collections.singletonList(new OrderByItem(new IndexOrderByItemSegment(0, 0, 1, OrderDirection.DESC, OrderDirection.ASC))), 0), - new OrderBy(Collections.emptyList(), false), new SelectItems(0, 0, false, Collections.emptyList()), - new Pagination(new NumberLiteralLimitValueSegment(0, 0, 1), null, Collections.emptyList())); + SQLStatementContext selectSQLStatementContext = new SelectSQLStatementContext(new SelectStatement(), + new GroupByContext(Collections.singletonList(new OrderByItem(new IndexOrderByItemSegment(0, 0, 1, OrderDirection.DESC, OrderDirection.ASC))), 0), + new OrderByContext(Collections.emptyList(), false), new SelectItemsContext(0, 0, false, Collections.emptyList()), + new PaginationContext(new NumberLiteralLimitValueSegment(0, 0, 1), null, Collections.emptyList())); SQLRouteResult routeResult = new SQLRouteResult( - shardingStatement, new CommonOptimizedStatement(new SelectStatement()), new ShardingConditions(Collections.emptyList())); + selectSQLStatementContext, new CommonSQLStatementContext(new SelectStatement()), new ShardingConditions(Collections.emptyList())); mergeEngine = new DQLMergeEngine(DatabaseTypes.getActualDatabaseType("MySQL"), null, routeResult, queryResults); MergedResult actual = mergeEngine.merge(); assertThat(actual, instanceOf(LimitDecoratorMergedResult.class)); @@ -302,13 +302,13 @@ public final class DQLMergeEngineTest { @Test public void assertBuildGroupByMemoryMergedResultWithOracleLimit() throws SQLException { - OptimizedStatement shardingStatement = new SelectOptimizedStatement(new SelectStatement(), - new GroupBy(Collections.singletonList(new OrderByItem(new IndexOrderByItemSegment(0, 0, 1, OrderDirection.DESC, OrderDirection.ASC))), 0), - new OrderBy(Collections.singletonList(new OrderByItem(new IndexOrderByItemSegment(0, 0, 2, OrderDirection.DESC, OrderDirection.ASC))), false), - new SelectItems(0, 0, false, Collections.emptyList()), - new Pagination(new NumberLiteralRowNumberValueSegment(0, 0, 1, true), null, Collections.emptyList())); + SQLStatementContext selectSQLStatementContext = new SelectSQLStatementContext(new SelectStatement(), + new GroupByContext(Collections.singletonList(new OrderByItem(new IndexOrderByItemSegment(0, 0, 1, OrderDirection.DESC, OrderDirection.ASC))), 0), + new OrderByContext(Collections.singletonList(new OrderByItem(new IndexOrderByItemSegment(0, 0, 2, OrderDirection.DESC, OrderDirection.ASC))), false), + new SelectItemsContext(0, 0, false, Collections.emptyList()), + new PaginationContext(new NumberLiteralRowNumberValueSegment(0, 0, 1, true), null, Collections.emptyList())); SQLRouteResult routeResult = new SQLRouteResult( - shardingStatement, new CommonOptimizedStatement(new SelectStatement()), new ShardingConditions(Collections.emptyList())); + selectSQLStatementContext, new CommonSQLStatementContext(new SelectStatement()), new ShardingConditions(Collections.emptyList())); mergeEngine = new DQLMergeEngine(DatabaseTypes.getActualDatabaseType("Oracle"), null, routeResult, queryResults); MergedResult actual = mergeEngine.merge(); assertThat(actual, instanceOf(RowNumberDecoratorMergedResult.class)); @@ -317,14 +317,14 @@ public final class DQLMergeEngineTest { @Test public void assertBuildGroupByMemoryMergedResultWithSQLServerLimit() throws SQLException { - OptimizedStatement shardingStatement = new SelectOptimizedStatement(new SelectStatement(), - new GroupBy(Arrays.asList( + SQLStatementContext selectSQLStatementContext = new SelectSQLStatementContext(new SelectStatement(), + new GroupByContext(Arrays.asList( new OrderByItem(new IndexOrderByItemSegment(0, 0, 1, OrderDirection.DESC, OrderDirection.ASC)), - new OrderByItem(new IndexOrderByItemSegment(0, 0, 1, OrderDirection.ASC, OrderDirection.ASC))), 0), new OrderBy(Collections.emptyList(), false), - new SelectItems(0, 0, false, Collections.emptyList()), - new Pagination(new NumberLiteralRowNumberValueSegment(0, 0, 1, true), null, Collections.emptyList())); + new OrderByItem(new IndexOrderByItemSegment(0, 0, 1, OrderDirection.ASC, OrderDirection.ASC))), 0), new OrderByContext(Collections.emptyList(), false), + new SelectItemsContext(0, 0, false, Collections.emptyList()), + new PaginationContext(new NumberLiteralRowNumberValueSegment(0, 0, 1, true), null, Collections.emptyList())); SQLRouteResult routeResult = new SQLRouteResult( - shardingStatement, new CommonOptimizedStatement(new SelectStatement()), new ShardingConditions(Collections.emptyList())); + selectSQLStatementContext, new CommonSQLStatementContext(new SelectStatement()), new ShardingConditions(Collections.emptyList())); mergeEngine = new DQLMergeEngine(DatabaseTypes.getActualDatabaseType("SQLServer"), null, routeResult, queryResults); MergedResult actual = mergeEngine.merge(); assertThat(actual, instanceOf(TopAndRowNumberDecoratorMergedResult.class)); @@ -333,24 +333,24 @@ public final class DQLMergeEngineTest { @Test public void assertBuildGroupByMemoryMergedResultWithAggregationOnly() throws SQLException { - SelectItems selectItems = new SelectItems( + SelectItemsContext selectItemsContext = new SelectItemsContext( 0, 0, false, Collections.singletonList(new AggregationSelectItem(AggregationType.COUNT, "(*)", null))); SQLRouteResult routeResult = new SQLRouteResult( - new SelectOptimizedStatement(new SelectStatement(), new GroupBy(Collections.emptyList(), 0), new OrderBy(Collections.emptyList(), false), - selectItems, new Pagination(null, null, Collections.emptyList())), - new CommonOptimizedStatement(new SelectStatement()), new ShardingConditions(Collections.emptyList())); + new SelectSQLStatementContext(new SelectStatement(), new GroupByContext(Collections.emptyList(), 0), new OrderByContext(Collections.emptyList(), false), + selectItemsContext, new PaginationContext(null, null, Collections.emptyList())), + new CommonSQLStatementContext(new SelectStatement()), new ShardingConditions(Collections.emptyList())); mergeEngine = new DQLMergeEngine(DatabaseTypes.getActualDatabaseType("MySQL"), null, routeResult, queryResults); assertThat(mergeEngine.merge(), instanceOf(GroupByMemoryMergedResult.class)); } @Test public void assertBuildGroupByMemoryMergedResultWithAggregationOnlyWithMySQLLimit() throws SQLException { - SelectItems selectItems = new SelectItems( + SelectItemsContext selectItemsContext = new SelectItemsContext( 0, 0, false, Collections.singletonList(new AggregationSelectItem(AggregationType.COUNT, "(*)", null))); SQLRouteResult routeResult = new SQLRouteResult( - new SelectOptimizedStatement(new SelectStatement(), new GroupBy(Collections.emptyList(), 0), new OrderBy(Collections.emptyList(), false), - selectItems, new Pagination(new NumberLiteralLimitValueSegment(0, 0, 1), null, Collections.emptyList())), - new CommonOptimizedStatement(new SelectStatement()), new ShardingConditions(Collections.emptyList())); + new SelectSQLStatementContext(new SelectStatement(), new GroupByContext(Collections.emptyList(), 0), new OrderByContext(Collections.emptyList(), false), + selectItemsContext, new PaginationContext(new NumberLiteralLimitValueSegment(0, 0, 1), null, Collections.emptyList())), + new CommonSQLStatementContext(new SelectStatement()), new ShardingConditions(Collections.emptyList())); mergeEngine = new DQLMergeEngine(DatabaseTypes.getActualDatabaseType("MySQL"), null, routeResult, queryResults); MergedResult actual = mergeEngine.merge(); assertThat(actual, instanceOf(LimitDecoratorMergedResult.class)); @@ -359,12 +359,12 @@ public final class DQLMergeEngineTest { @Test public void assertBuildGroupByMemoryMergedResultWithAggregationOnlyWithOracleLimit() throws SQLException { - SelectItems selectItems = new SelectItems( + SelectItemsContext selectItemsContext = new SelectItemsContext( 0, 0, false, Collections.singletonList(new AggregationSelectItem(AggregationType.COUNT, "(*)", null))); SQLRouteResult routeResult = new SQLRouteResult( - new SelectOptimizedStatement(new SelectStatement(), new GroupBy(Collections.emptyList(), 0), new OrderBy(Collections.emptyList(), false), - selectItems, new Pagination(new NumberLiteralRowNumberValueSegment(0, 0, 1, true), null, Collections.emptyList())), - new CommonOptimizedStatement(new SelectStatement()), new ShardingConditions(Collections.emptyList())); + new SelectSQLStatementContext(new SelectStatement(), new GroupByContext(Collections.emptyList(), 0), new OrderByContext(Collections.emptyList(), false), + selectItemsContext, new PaginationContext(new NumberLiteralRowNumberValueSegment(0, 0, 1, true), null, Collections.emptyList())), + new CommonSQLStatementContext(new SelectStatement()), new ShardingConditions(Collections.emptyList())); mergeEngine = new DQLMergeEngine(DatabaseTypes.getActualDatabaseType("Oracle"), null, routeResult, queryResults); MergedResult actual = mergeEngine.merge(); assertThat(actual, instanceOf(RowNumberDecoratorMergedResult.class)); @@ -373,12 +373,12 @@ public final class DQLMergeEngineTest { @Test public void assertBuildGroupByMemoryMergedResultWithAggregationOnlyWithSQLServerLimit() throws SQLException { - SelectItems selectItems = new SelectItems( + SelectItemsContext selectItemsContext = new SelectItemsContext( 0, 0, false, Collections.singletonList(new AggregationSelectItem(AggregationType.COUNT, "(*)", null))); SQLRouteResult routeResult = new SQLRouteResult( - new SelectOptimizedStatement(new SelectStatement(), new GroupBy(Collections.emptyList(), 0), new OrderBy(Collections.emptyList(), false), - selectItems, new Pagination(new NumberLiteralRowNumberValueSegment(0, 0, 1, true), null, Collections.emptyList())), - new CommonOptimizedStatement(new SelectStatement()), new ShardingConditions(Collections.emptyList())); + new SelectSQLStatementContext(new SelectStatement(), new GroupByContext(Collections.emptyList(), 0), new OrderByContext(Collections.emptyList(), false), + selectItemsContext, new PaginationContext(new NumberLiteralRowNumberValueSegment(0, 0, 1, true), null, Collections.emptyList())), + new CommonSQLStatementContext(new SelectStatement()), new ShardingConditions(Collections.emptyList())); mergeEngine = new DQLMergeEngine(DatabaseTypes.getActualDatabaseType("SQLServer"), null, routeResult, queryResults); MergedResult actual = mergeEngine.merge(); assertThat(actual, instanceOf(TopAndRowNumberDecoratorMergedResult.class)); diff --git a/sharding-core/sharding-core-merge/src/test/java/org/apache/shardingsphere/core/merge/dql/groupby/GroupByMemoryMergedResultTest.java b/sharding-core/sharding-core-merge/src/test/java/org/apache/shardingsphere/core/merge/dql/groupby/GroupByMemoryMergedResultTest.java index 0a7a06c6a3..81d39c534a 100644 --- a/sharding-core/sharding-core-merge/src/test/java/org/apache/shardingsphere/core/merge/dql/groupby/GroupByMemoryMergedResultTest.java +++ b/sharding-core/sharding-core-merge/src/test/java/org/apache/shardingsphere/core/merge/dql/groupby/GroupByMemoryMergedResultTest.java @@ -23,16 +23,16 @@ import org.apache.shardingsphere.core.execute.sql.execute.result.QueryResult; import org.apache.shardingsphere.core.merge.MergedResult; import org.apache.shardingsphere.core.merge.dql.DQLMergeEngine; import org.apache.shardingsphere.core.merge.fixture.TestQueryResult; -import org.apache.shardingsphere.core.optimize.segment.select.groupby.GroupBy; +import org.apache.shardingsphere.core.optimize.segment.select.groupby.GroupByContext; import org.apache.shardingsphere.core.optimize.segment.select.item.impl.AggregationSelectItem; import org.apache.shardingsphere.core.optimize.segment.select.item.SelectItem; -import org.apache.shardingsphere.core.optimize.segment.select.item.SelectItems; -import org.apache.shardingsphere.core.optimize.segment.select.orderby.OrderBy; +import org.apache.shardingsphere.core.optimize.segment.select.item.SelectItemsContext; +import org.apache.shardingsphere.core.optimize.segment.select.orderby.OrderByContext; import org.apache.shardingsphere.core.optimize.segment.select.orderby.OrderByItem; -import org.apache.shardingsphere.core.optimize.segment.select.pagination.Pagination; -import org.apache.shardingsphere.core.optimize.statement.OptimizedStatement; -import org.apache.shardingsphere.core.optimize.statement.impl.CommonOptimizedStatement; -import org.apache.shardingsphere.core.optimize.statement.impl.SelectOptimizedStatement; +import org.apache.shardingsphere.core.optimize.segment.select.pagination.PaginationContext; +import org.apache.shardingsphere.core.optimize.statement.SQLStatementContext; +import org.apache.shardingsphere.core.optimize.statement.impl.CommonSQLStatementContext; +import org.apache.shardingsphere.core.optimize.statement.impl.SelectSQLStatementContext; import org.apache.shardingsphere.core.parse.core.constant.AggregationType; import org.apache.shardingsphere.core.parse.core.constant.OrderDirection; import org.apache.shardingsphere.core.parse.sql.segment.dml.order.item.IndexOrderByItemSegment; @@ -86,12 +86,12 @@ public final class GroupByMemoryMergedResultTest { AggregationSelectItem derivedAggregationSelectItem2 = new AggregationSelectItem(AggregationType.SUM, "(num)", "AVG_DERIVED_SUM_0"); aggregationSelectItem2.setIndex(5); aggregationSelectItem2.getDerivedAggregationItems().add(derivedAggregationSelectItem2); - SelectItems selectItems = new SelectItems(0, 0, false, Arrays.asList(aggregationSelectItem1, aggregationSelectItem2)); - OptimizedStatement shardingStatement = new SelectOptimizedStatement(new SelectStatement(), - new GroupBy(Collections.singletonList(createOrderByItem(new IndexOrderByItemSegment(0, 0, 3, OrderDirection.ASC, OrderDirection.ASC))), 0), - new OrderBy(Collections.singletonList(createOrderByItem(new IndexOrderByItemSegment(0, 0, 3, OrderDirection.DESC, OrderDirection.ASC))), false), - selectItems, new Pagination(null, null, Collections.emptyList())); - routeResult = new SQLRouteResult(shardingStatement, new CommonOptimizedStatement(new SelectStatement()), new ShardingConditions(Collections.emptyList())); + SelectItemsContext selectItemsContext = new SelectItemsContext(0, 0, false, Arrays.asList(aggregationSelectItem1, aggregationSelectItem2)); + SQLStatementContext selectSQLStatementContext = new SelectSQLStatementContext(new SelectStatement(), + new GroupByContext(Collections.singletonList(createOrderByItem(new IndexOrderByItemSegment(0, 0, 3, OrderDirection.ASC, OrderDirection.ASC))), 0), + new OrderByContext(Collections.singletonList(createOrderByItem(new IndexOrderByItemSegment(0, 0, 3, OrderDirection.DESC, OrderDirection.ASC))), false), + selectItemsContext, new PaginationContext(null, null, Collections.emptyList())); + routeResult = new SQLRouteResult(selectSQLStatementContext, new CommonSQLStatementContext(new SelectStatement()), new ShardingConditions(Collections.emptyList())); } private ResultSet mockResultSet() throws SQLException { diff --git a/sharding-core/sharding-core-merge/src/test/java/org/apache/shardingsphere/core/merge/dql/groupby/GroupByRowComparatorTest.java b/sharding-core/sharding-core-merge/src/test/java/org/apache/shardingsphere/core/merge/dql/groupby/GroupByRowComparatorTest.java index 36d46db037..5b52921bd5 100644 --- a/sharding-core/sharding-core-merge/src/test/java/org/apache/shardingsphere/core/merge/dql/groupby/GroupByRowComparatorTest.java +++ b/sharding-core/sharding-core-merge/src/test/java/org/apache/shardingsphere/core/merge/dql/groupby/GroupByRowComparatorTest.java @@ -21,13 +21,13 @@ import com.google.common.collect.Lists; import org.apache.shardingsphere.core.execute.sql.execute.result.QueryResult; import org.apache.shardingsphere.core.merge.dql.common.MemoryQueryResultRow; import org.apache.shardingsphere.core.merge.fixture.TestQueryResult; -import org.apache.shardingsphere.core.optimize.segment.select.groupby.GroupBy; +import org.apache.shardingsphere.core.optimize.segment.select.groupby.GroupByContext; import org.apache.shardingsphere.core.optimize.segment.select.item.SelectItem; -import org.apache.shardingsphere.core.optimize.segment.select.item.SelectItems; -import org.apache.shardingsphere.core.optimize.segment.select.orderby.OrderBy; +import org.apache.shardingsphere.core.optimize.segment.select.item.SelectItemsContext; +import org.apache.shardingsphere.core.optimize.segment.select.orderby.OrderByContext; import org.apache.shardingsphere.core.optimize.segment.select.orderby.OrderByItem; -import org.apache.shardingsphere.core.optimize.segment.select.pagination.Pagination; -import org.apache.shardingsphere.core.optimize.statement.impl.SelectOptimizedStatement; +import org.apache.shardingsphere.core.optimize.segment.select.pagination.PaginationContext; +import org.apache.shardingsphere.core.optimize.statement.impl.SelectSQLStatementContext; import org.apache.shardingsphere.core.parse.core.constant.OrderDirection; import org.apache.shardingsphere.core.parse.sql.segment.dml.order.item.IndexOrderByItemSegment; import org.apache.shardingsphere.core.parse.sql.statement.dml.SelectStatement; @@ -52,15 +52,15 @@ public final class GroupByRowComparatorTest { @Test public void assertCompareToForAscWithOrderByItems() throws SQLException { - SelectOptimizedStatement optimizedStatement = new SelectOptimizedStatement(new SelectStatement(), - new GroupBy(Arrays.asList( + SelectSQLStatementContext selectSQLStatementContext = new SelectSQLStatementContext(new SelectStatement(), + new GroupByContext(Arrays.asList( createOrderByItem(new IndexOrderByItemSegment(0, 0, 1, OrderDirection.DESC, OrderDirection.ASC)), createOrderByItem(new IndexOrderByItemSegment(0, 0, 2, OrderDirection.DESC, OrderDirection.ASC))), 0), - new OrderBy(Arrays.asList( + new OrderByContext(Arrays.asList( createOrderByItem(new IndexOrderByItemSegment(0, 0, 1, OrderDirection.ASC, OrderDirection.ASC)), createOrderByItem(new IndexOrderByItemSegment(0, 0, 2, OrderDirection.ASC, OrderDirection.ASC))), false), - new SelectItems(0, 0, false, Collections.emptyList()), new Pagination(null, null, Collections.emptyList())); - GroupByRowComparator groupByRowComparator = new GroupByRowComparator(optimizedStatement, caseSensitives); + new SelectItemsContext(0, 0, false, Collections.emptyList()), new PaginationContext(null, null, Collections.emptyList())); + GroupByRowComparator groupByRowComparator = new GroupByRowComparator(selectSQLStatementContext, caseSensitives); MemoryQueryResultRow o1 = new MemoryQueryResultRow(mockQueryResult("1", "2")); MemoryQueryResultRow o2 = new MemoryQueryResultRow(mockQueryResult("3", "4")); assertTrue(groupByRowComparator.compare(o1, o2) < 0); @@ -68,15 +68,15 @@ public final class GroupByRowComparatorTest { @Test public void assertCompareToForDecsWithOrderByItems() throws SQLException { - SelectOptimizedStatement optimizedStatement = new SelectOptimizedStatement(new SelectStatement(), - new GroupBy(Arrays.asList( + SelectSQLStatementContext selectSQLStatementContext = new SelectSQLStatementContext(new SelectStatement(), + new GroupByContext(Arrays.asList( createOrderByItem(new IndexOrderByItemSegment(0, 0, 1, OrderDirection.ASC, OrderDirection.ASC)), createOrderByItem(new IndexOrderByItemSegment(0, 0, 2, OrderDirection.ASC, OrderDirection.ASC))), 0), - new OrderBy(Arrays.asList( + new OrderByContext(Arrays.asList( createOrderByItem(new IndexOrderByItemSegment(0, 0, 1, OrderDirection.DESC, OrderDirection.ASC)), createOrderByItem(new IndexOrderByItemSegment(0, 0, 2, OrderDirection.DESC, OrderDirection.ASC))), false), - new SelectItems(0, 0, false, Collections.emptyList()), new Pagination(null, null, Collections.emptyList())); - GroupByRowComparator groupByRowComparator = new GroupByRowComparator(optimizedStatement, caseSensitives); + new SelectItemsContext(0, 0, false, Collections.emptyList()), new PaginationContext(null, null, Collections.emptyList())); + GroupByRowComparator groupByRowComparator = new GroupByRowComparator(selectSQLStatementContext, caseSensitives); MemoryQueryResultRow o1 = new MemoryQueryResultRow(mockQueryResult("1", "2")); MemoryQueryResultRow o2 = new MemoryQueryResultRow(mockQueryResult("3", "4")); assertTrue(groupByRowComparator.compare(o1, o2) > 0); @@ -84,15 +84,15 @@ public final class GroupByRowComparatorTest { @Test public void assertCompareToForEqualWithOrderByItems() throws SQLException { - SelectOptimizedStatement optimizedStatement = new SelectOptimizedStatement(new SelectStatement(), - new GroupBy(Arrays.asList( + SelectSQLStatementContext selectSQLStatementContext = new SelectSQLStatementContext(new SelectStatement(), + new GroupByContext(Arrays.asList( createOrderByItem(new IndexOrderByItemSegment(0, 0, 1, OrderDirection.DESC, OrderDirection.ASC)), createOrderByItem(new IndexOrderByItemSegment(0, 0, 2, OrderDirection.ASC, OrderDirection.ASC))), 0), - new OrderBy(Arrays.asList( + new OrderByContext(Arrays.asList( createOrderByItem(new IndexOrderByItemSegment(0, 0, 1, OrderDirection.ASC, OrderDirection.ASC)), createOrderByItem(new IndexOrderByItemSegment(0, 0, 2, OrderDirection.DESC, OrderDirection.ASC))), false), - new SelectItems(0, 0, false, Collections.emptyList()), new Pagination(null, null, Collections.emptyList())); - GroupByRowComparator groupByRowComparator = new GroupByRowComparator(optimizedStatement, caseSensitives); + new SelectItemsContext(0, 0, false, Collections.emptyList()), new PaginationContext(null, null, Collections.emptyList())); + GroupByRowComparator groupByRowComparator = new GroupByRowComparator(selectSQLStatementContext, caseSensitives); MemoryQueryResultRow o1 = new MemoryQueryResultRow(mockQueryResult("1", "2")); MemoryQueryResultRow o2 = new MemoryQueryResultRow(mockQueryResult("1", "2")); assertThat(groupByRowComparator.compare(o1, o2), is(0)); @@ -100,12 +100,12 @@ public final class GroupByRowComparatorTest { @Test public void assertCompareToForAscWithGroupByItems() throws SQLException { - SelectOptimizedStatement optimizedStatement = new SelectOptimizedStatement(new SelectStatement(), - new GroupBy(Arrays.asList( + SelectSQLStatementContext selectSQLStatementContext = new SelectSQLStatementContext(new SelectStatement(), + new GroupByContext(Arrays.asList( createOrderByItem(new IndexOrderByItemSegment(0, 0, 1, OrderDirection.ASC, OrderDirection.ASC)), - createOrderByItem(new IndexOrderByItemSegment(0, 0, 2, OrderDirection.ASC, OrderDirection.ASC))), 0), new OrderBy(Collections.emptyList(), false), - new SelectItems(0, 0, false, Collections.emptyList()), new Pagination(null, null, Collections.emptyList())); - GroupByRowComparator groupByRowComparator = new GroupByRowComparator(optimizedStatement, caseSensitives); + createOrderByItem(new IndexOrderByItemSegment(0, 0, 2, OrderDirection.ASC, OrderDirection.ASC))), 0), new OrderByContext(Collections.emptyList(), false), + new SelectItemsContext(0, 0, false, Collections.emptyList()), new PaginationContext(null, null, Collections.emptyList())); + GroupByRowComparator groupByRowComparator = new GroupByRowComparator(selectSQLStatementContext, caseSensitives); MemoryQueryResultRow o1 = new MemoryQueryResultRow(mockQueryResult("1", "2")); MemoryQueryResultRow o2 = new MemoryQueryResultRow(mockQueryResult("3", "4")); assertTrue(groupByRowComparator.compare(o1, o2) < 0); @@ -113,12 +113,12 @@ public final class GroupByRowComparatorTest { @Test public void assertCompareToForDecsWithGroupByItems() throws SQLException { - SelectOptimizedStatement optimizedStatement = new SelectOptimizedStatement(new SelectStatement(), - new GroupBy(Arrays.asList( + SelectSQLStatementContext selectSQLStatementContext = new SelectSQLStatementContext(new SelectStatement(), + new GroupByContext(Arrays.asList( createOrderByItem(new IndexOrderByItemSegment(0, 0, 1, OrderDirection.DESC, OrderDirection.ASC)), - createOrderByItem(new IndexOrderByItemSegment(0, 0, 2, OrderDirection.DESC, OrderDirection.ASC))), 0), new OrderBy(Collections.emptyList(), false), - new SelectItems(0, 0, false, Collections.emptyList()), new Pagination(null, null, Collections.emptyList())); - GroupByRowComparator groupByRowComparator = new GroupByRowComparator(optimizedStatement, caseSensitives); + createOrderByItem(new IndexOrderByItemSegment(0, 0, 2, OrderDirection.DESC, OrderDirection.ASC))), 0), new OrderByContext(Collections.emptyList(), false), + new SelectItemsContext(0, 0, false, Collections.emptyList()), new PaginationContext(null, null, Collections.emptyList())); + GroupByRowComparator groupByRowComparator = new GroupByRowComparator(selectSQLStatementContext, caseSensitives); MemoryQueryResultRow o1 = new MemoryQueryResultRow(mockQueryResult("1", "2")); MemoryQueryResultRow o2 = new MemoryQueryResultRow(mockQueryResult("3", "4")); assertTrue(groupByRowComparator.compare(o1, o2) > 0); @@ -126,12 +126,12 @@ public final class GroupByRowComparatorTest { @Test public void assertCompareToForEqualWithGroupByItems() throws SQLException { - SelectOptimizedStatement optimizedStatement = new SelectOptimizedStatement(new SelectStatement(), - new GroupBy(Arrays.asList( + SelectSQLStatementContext selectSQLStatementContext = new SelectSQLStatementContext(new SelectStatement(), + new GroupByContext(Arrays.asList( createOrderByItem(new IndexOrderByItemSegment(0, 0, 1, OrderDirection.ASC, OrderDirection.ASC)), - createOrderByItem(new IndexOrderByItemSegment(0, 0, 2, OrderDirection.DESC, OrderDirection.ASC))), 0), new OrderBy(Collections.emptyList(), false), - new SelectItems(0, 0, false, Collections.emptyList()), new Pagination(null, null, Collections.emptyList())); - GroupByRowComparator groupByRowComparator = new GroupByRowComparator(optimizedStatement, caseSensitives); + createOrderByItem(new IndexOrderByItemSegment(0, 0, 2, OrderDirection.DESC, OrderDirection.ASC))), 0), new OrderByContext(Collections.emptyList(), false), + new SelectItemsContext(0, 0, false, Collections.emptyList()), new PaginationContext(null, null, Collections.emptyList())); + GroupByRowComparator groupByRowComparator = new GroupByRowComparator(selectSQLStatementContext, caseSensitives); MemoryQueryResultRow o1 = new MemoryQueryResultRow(mockQueryResult("1", "2")); MemoryQueryResultRow o2 = new MemoryQueryResultRow(mockQueryResult("1", "2")); assertThat(groupByRowComparator.compare(o1, o2), is(0)); diff --git a/sharding-core/sharding-core-merge/src/test/java/org/apache/shardingsphere/core/merge/dql/groupby/GroupByStreamMergedResultTest.java b/sharding-core/sharding-core-merge/src/test/java/org/apache/shardingsphere/core/merge/dql/groupby/GroupByStreamMergedResultTest.java index 2493fa19f3..0a6ddbd4d3 100644 --- a/sharding-core/sharding-core-merge/src/test/java/org/apache/shardingsphere/core/merge/dql/groupby/GroupByStreamMergedResultTest.java +++ b/sharding-core/sharding-core-merge/src/test/java/org/apache/shardingsphere/core/merge/dql/groupby/GroupByStreamMergedResultTest.java @@ -23,16 +23,16 @@ import org.apache.shardingsphere.core.execute.sql.execute.result.QueryResult; import org.apache.shardingsphere.core.merge.MergedResult; import org.apache.shardingsphere.core.merge.dql.DQLMergeEngine; import org.apache.shardingsphere.core.merge.fixture.TestQueryResult; -import org.apache.shardingsphere.core.optimize.segment.select.groupby.GroupBy; +import org.apache.shardingsphere.core.optimize.segment.select.groupby.GroupByContext; import org.apache.shardingsphere.core.optimize.segment.select.item.impl.AggregationSelectItem; import org.apache.shardingsphere.core.optimize.segment.select.item.SelectItem; -import org.apache.shardingsphere.core.optimize.segment.select.item.SelectItems; -import org.apache.shardingsphere.core.optimize.segment.select.orderby.OrderBy; +import org.apache.shardingsphere.core.optimize.segment.select.item.SelectItemsContext; +import org.apache.shardingsphere.core.optimize.segment.select.orderby.OrderByContext; import org.apache.shardingsphere.core.optimize.segment.select.orderby.OrderByItem; -import org.apache.shardingsphere.core.optimize.segment.select.pagination.Pagination; -import org.apache.shardingsphere.core.optimize.statement.OptimizedStatement; -import org.apache.shardingsphere.core.optimize.statement.impl.CommonOptimizedStatement; -import org.apache.shardingsphere.core.optimize.statement.impl.SelectOptimizedStatement; +import org.apache.shardingsphere.core.optimize.segment.select.pagination.PaginationContext; +import org.apache.shardingsphere.core.optimize.statement.SQLStatementContext; +import org.apache.shardingsphere.core.optimize.statement.impl.CommonSQLStatementContext; +import org.apache.shardingsphere.core.optimize.statement.impl.SelectSQLStatementContext; import org.apache.shardingsphere.core.parse.core.constant.AggregationType; import org.apache.shardingsphere.core.parse.core.constant.OrderDirection; import org.apache.shardingsphere.core.parse.sql.segment.dml.order.item.IndexOrderByItemSegment; @@ -88,12 +88,12 @@ public final class GroupByStreamMergedResultTest { AggregationSelectItem derivedAggregationSelectItem2 = new AggregationSelectItem(AggregationType.SUM, "(num)", "AVG_DERIVED_SUM_0"); aggregationSelectItem2.setIndex(6); aggregationSelectItem2.getDerivedAggregationItems().add(derivedAggregationSelectItem2); - SelectItems selectItems = new SelectItems(0, 0, false, Arrays.asList(aggregationSelectItem1, aggregationSelectItem2)); - OptimizedStatement shardingStatement = new SelectOptimizedStatement(new SelectStatement(), - new GroupBy(Collections.singletonList(new OrderByItem(new IndexOrderByItemSegment(0, 0, 3, OrderDirection.ASC, OrderDirection.ASC))), 0), - new OrderBy(Collections.singletonList(new OrderByItem(new IndexOrderByItemSegment(0, 0, 3, OrderDirection.ASC, OrderDirection.ASC))), false), - selectItems, new Pagination(null, null, Collections.emptyList())); - routeResult = new SQLRouteResult(shardingStatement, new CommonOptimizedStatement(new SelectStatement()), new ShardingConditions(Collections.emptyList())); + SelectItemsContext selectItemsContext = new SelectItemsContext(0, 0, false, Arrays.asList(aggregationSelectItem1, aggregationSelectItem2)); + SQLStatementContext selectSQLStatementContext = new SelectSQLStatementContext(new SelectStatement(), + new GroupByContext(Collections.singletonList(new OrderByItem(new IndexOrderByItemSegment(0, 0, 3, OrderDirection.ASC, OrderDirection.ASC))), 0), + new OrderByContext(Collections.singletonList(new OrderByItem(new IndexOrderByItemSegment(0, 0, 3, OrderDirection.ASC, OrderDirection.ASC))), false), + selectItemsContext, new PaginationContext(null, null, Collections.emptyList())); + routeResult = new SQLRouteResult(selectSQLStatementContext, new CommonSQLStatementContext(new SelectStatement()), new ShardingConditions(Collections.emptyList())); } private ResultSet mockResultSet() throws SQLException { diff --git a/sharding-core/sharding-core-merge/src/test/java/org/apache/shardingsphere/core/merge/dql/iterator/IteratorStreamMergedResultTest.java b/sharding-core/sharding-core-merge/src/test/java/org/apache/shardingsphere/core/merge/dql/iterator/IteratorStreamMergedResultTest.java index 13a31c7ff8..6afae14b36 100644 --- a/sharding-core/sharding-core-merge/src/test/java/org/apache/shardingsphere/core/merge/dql/iterator/IteratorStreamMergedResultTest.java +++ b/sharding-core/sharding-core-merge/src/test/java/org/apache/shardingsphere/core/merge/dql/iterator/IteratorStreamMergedResultTest.java @@ -23,14 +23,14 @@ import org.apache.shardingsphere.core.execute.sql.execute.result.QueryResult; import org.apache.shardingsphere.core.merge.MergedResult; import org.apache.shardingsphere.core.merge.dql.DQLMergeEngine; import org.apache.shardingsphere.core.merge.fixture.TestQueryResult; -import org.apache.shardingsphere.core.optimize.segment.select.groupby.GroupBy; +import org.apache.shardingsphere.core.optimize.segment.select.groupby.GroupByContext; import org.apache.shardingsphere.core.optimize.segment.select.item.SelectItem; -import org.apache.shardingsphere.core.optimize.segment.select.item.SelectItems; -import org.apache.shardingsphere.core.optimize.segment.select.orderby.OrderBy; +import org.apache.shardingsphere.core.optimize.segment.select.item.SelectItemsContext; +import org.apache.shardingsphere.core.optimize.segment.select.orderby.OrderByContext; import org.apache.shardingsphere.core.optimize.segment.select.orderby.OrderByItem; -import org.apache.shardingsphere.core.optimize.segment.select.pagination.Pagination; -import org.apache.shardingsphere.core.optimize.statement.impl.CommonOptimizedStatement; -import org.apache.shardingsphere.core.optimize.statement.impl.SelectOptimizedStatement; +import org.apache.shardingsphere.core.optimize.segment.select.pagination.PaginationContext; +import org.apache.shardingsphere.core.optimize.statement.impl.CommonSQLStatementContext; +import org.apache.shardingsphere.core.optimize.statement.impl.SelectSQLStatementContext; import org.apache.shardingsphere.core.parse.sql.statement.dml.SelectStatement; import org.apache.shardingsphere.core.route.SQLRouteResult; import org.apache.shardingsphere.core.route.router.sharding.condition.ShardingCondition; @@ -63,10 +63,10 @@ public final class IteratorStreamMergedResultTest { ResultSetMetaData resultSetMetaData = mock(ResultSetMetaData.class); when(resultSet.getMetaData()).thenReturn(resultSetMetaData); queryResults = Lists.newArrayList(new TestQueryResult(resultSet), new TestQueryResult(mock(ResultSet.class)), new TestQueryResult(mock(ResultSet.class))); - routeResult = new SQLRouteResult(new SelectOptimizedStatement(new SelectStatement(), - new GroupBy(Collections.emptyList(), 0), new OrderBy(Collections.emptyList(), false), - new SelectItems(0, 0, false, Collections.emptyList()), new Pagination(null, null, Collections.emptyList())), - new CommonOptimizedStatement(new SelectStatement()), new ShardingConditions(Collections.emptyList())); + routeResult = new SQLRouteResult(new SelectSQLStatementContext(new SelectStatement(), + new GroupByContext(Collections.emptyList(), 0), new OrderByContext(Collections.emptyList(), false), + new SelectItemsContext(0, 0, false, Collections.emptyList()), new PaginationContext(null, null, Collections.emptyList())), + new CommonSQLStatementContext(new SelectStatement()), new ShardingConditions(Collections.emptyList())); } @Test diff --git a/sharding-core/sharding-core-merge/src/test/java/org/apache/shardingsphere/core/merge/dql/orderby/OrderByStreamMergedResultTest.java b/sharding-core/sharding-core-merge/src/test/java/org/apache/shardingsphere/core/merge/dql/orderby/OrderByStreamMergedResultTest.java index 13c3927e7d..431101ce8c 100644 --- a/sharding-core/sharding-core-merge/src/test/java/org/apache/shardingsphere/core/merge/dql/orderby/OrderByStreamMergedResultTest.java +++ b/sharding-core/sharding-core-merge/src/test/java/org/apache/shardingsphere/core/merge/dql/orderby/OrderByStreamMergedResultTest.java @@ -23,15 +23,15 @@ import org.apache.shardingsphere.core.execute.sql.execute.result.QueryResult; import org.apache.shardingsphere.core.merge.MergedResult; import org.apache.shardingsphere.core.merge.dql.DQLMergeEngine; import org.apache.shardingsphere.core.merge.fixture.TestQueryResult; -import org.apache.shardingsphere.core.optimize.segment.select.groupby.GroupBy; +import org.apache.shardingsphere.core.optimize.segment.select.groupby.GroupByContext; import org.apache.shardingsphere.core.optimize.segment.select.item.SelectItem; -import org.apache.shardingsphere.core.optimize.segment.select.item.SelectItems; -import org.apache.shardingsphere.core.optimize.segment.select.orderby.OrderBy; +import org.apache.shardingsphere.core.optimize.segment.select.item.SelectItemsContext; +import org.apache.shardingsphere.core.optimize.segment.select.orderby.OrderByContext; import org.apache.shardingsphere.core.optimize.segment.select.orderby.OrderByItem; -import org.apache.shardingsphere.core.optimize.segment.select.pagination.Pagination; -import org.apache.shardingsphere.core.optimize.statement.OptimizedStatement; -import org.apache.shardingsphere.core.optimize.statement.impl.CommonOptimizedStatement; -import org.apache.shardingsphere.core.optimize.statement.impl.SelectOptimizedStatement; +import org.apache.shardingsphere.core.optimize.segment.select.pagination.PaginationContext; +import org.apache.shardingsphere.core.optimize.statement.SQLStatementContext; +import org.apache.shardingsphere.core.optimize.statement.impl.CommonSQLStatementContext; +import org.apache.shardingsphere.core.optimize.statement.impl.SelectSQLStatementContext; import org.apache.shardingsphere.core.parse.core.constant.OrderDirection; import org.apache.shardingsphere.core.parse.sql.segment.dml.order.item.IndexOrderByItemSegment; import org.apache.shardingsphere.core.parse.sql.statement.dml.SelectStatement; @@ -66,11 +66,11 @@ public final class OrderByStreamMergedResultTest { public void setUp() throws SQLException { queryResults = Lists.newArrayList( new TestQueryResult(mockResultSet()), new TestQueryResult(mockResultSet()), new TestQueryResult(mockResultSet())); - OptimizedStatement shardingStatement = new SelectOptimizedStatement(new SelectStatement(), - new GroupBy(Collections.emptyList(), 0), - new OrderBy(Collections.singletonList(new OrderByItem(new IndexOrderByItemSegment(0, 0, 1, OrderDirection.ASC, OrderDirection.ASC))), false), - new SelectItems(0, 0, false, Collections.emptyList()), new Pagination(null, null, Collections.emptyList())); - routeResult = new SQLRouteResult(shardingStatement, new CommonOptimizedStatement(new SelectStatement()), new ShardingConditions(Collections.emptyList())); + SQLStatementContext selectSQLStatementContext = new SelectSQLStatementContext(new SelectStatement(), + new GroupByContext(Collections.emptyList(), 0), + new OrderByContext(Collections.singletonList(new OrderByItem(new IndexOrderByItemSegment(0, 0, 1, OrderDirection.ASC, OrderDirection.ASC))), false), + new SelectItemsContext(0, 0, false, Collections.emptyList()), new PaginationContext(null, null, Collections.emptyList())); + routeResult = new SQLRouteResult(selectSQLStatementContext, new CommonSQLStatementContext(new SelectStatement()), new ShardingConditions(Collections.emptyList())); } private ResultSet mockResultSet() throws SQLException { diff --git a/sharding-core/sharding-core-merge/src/test/java/org/apache/shardingsphere/core/merge/dql/pagination/LimitDecoratorMergedResultTest.java b/sharding-core/sharding-core-merge/src/test/java/org/apache/shardingsphere/core/merge/dql/pagination/LimitDecoratorMergedResultTest.java index 6d124cfc31..c9fc10a902 100644 --- a/sharding-core/sharding-core-merge/src/test/java/org/apache/shardingsphere/core/merge/dql/pagination/LimitDecoratorMergedResultTest.java +++ b/sharding-core/sharding-core-merge/src/test/java/org/apache/shardingsphere/core/merge/dql/pagination/LimitDecoratorMergedResultTest.java @@ -23,15 +23,15 @@ import org.apache.shardingsphere.core.execute.sql.execute.result.QueryResult; import org.apache.shardingsphere.core.merge.MergedResult; import org.apache.shardingsphere.core.merge.dql.DQLMergeEngine; import org.apache.shardingsphere.core.merge.fixture.TestQueryResult; -import org.apache.shardingsphere.core.optimize.segment.select.groupby.GroupBy; +import org.apache.shardingsphere.core.optimize.segment.select.groupby.GroupByContext; import org.apache.shardingsphere.core.optimize.segment.select.item.SelectItem; -import org.apache.shardingsphere.core.optimize.segment.select.item.SelectItems; -import org.apache.shardingsphere.core.optimize.segment.select.orderby.OrderBy; +import org.apache.shardingsphere.core.optimize.segment.select.item.SelectItemsContext; +import org.apache.shardingsphere.core.optimize.segment.select.orderby.OrderByContext; import org.apache.shardingsphere.core.optimize.segment.select.orderby.OrderByItem; -import org.apache.shardingsphere.core.optimize.segment.select.pagination.Pagination; -import org.apache.shardingsphere.core.optimize.statement.OptimizedStatement; -import org.apache.shardingsphere.core.optimize.statement.impl.CommonOptimizedStatement; -import org.apache.shardingsphere.core.optimize.statement.impl.SelectOptimizedStatement; +import org.apache.shardingsphere.core.optimize.segment.select.pagination.PaginationContext; +import org.apache.shardingsphere.core.optimize.statement.SQLStatementContext; +import org.apache.shardingsphere.core.optimize.statement.impl.CommonSQLStatementContext; +import org.apache.shardingsphere.core.optimize.statement.impl.SelectSQLStatementContext; import org.apache.shardingsphere.core.parse.sql.segment.dml.pagination.limit.NumberLiteralLimitValueSegment; import org.apache.shardingsphere.core.parse.sql.statement.dml.SelectStatement; import org.apache.shardingsphere.core.route.SQLRouteResult; @@ -75,12 +75,12 @@ public final class LimitDecoratorMergedResultTest { @Test public void assertNextForSkipAll() throws SQLException { - OptimizedStatement shardingStatement = new SelectOptimizedStatement(new SelectStatement(), - new GroupBy(Collections.emptyList(), 0), new OrderBy(Collections.emptyList(), false), - new SelectItems(0, 0, false, Collections.emptyList()), - new Pagination(new NumberLiteralLimitValueSegment(0, 0, Integer.MAX_VALUE), null, Collections.emptyList())); + SQLStatementContext selectSQLStatementContext = new SelectSQLStatementContext(new SelectStatement(), + new GroupByContext(Collections.emptyList(), 0), new OrderByContext(Collections.emptyList(), false), + new SelectItemsContext(0, 0, false, Collections.emptyList()), + new PaginationContext(new NumberLiteralLimitValueSegment(0, 0, Integer.MAX_VALUE), null, Collections.emptyList())); SQLRouteResult routeResult = new SQLRouteResult( - shardingStatement, new CommonOptimizedStatement(new SelectStatement()), new ShardingConditions(Collections.emptyList())); + selectSQLStatementContext, new CommonSQLStatementContext(new SelectStatement()), new ShardingConditions(Collections.emptyList())); mergeEngine = new DQLMergeEngine(DatabaseTypes.getActualDatabaseType("MySQL"), null, routeResult, queryResults); MergedResult actual = mergeEngine.merge(); assertFalse(actual.next()); @@ -88,12 +88,12 @@ public final class LimitDecoratorMergedResultTest { @Test public void assertNextWithoutRowCount() throws SQLException { - OptimizedStatement shardingStatement = new SelectOptimizedStatement(new SelectStatement(), - new GroupBy(Collections.emptyList(), 0), new OrderBy(Collections.emptyList(), false), - new SelectItems(0, 0, false, Collections.emptyList()), - new Pagination(new NumberLiteralLimitValueSegment(0, 0, 2), null, Collections.emptyList())); + SQLStatementContext selectSQLStatementContext = new SelectSQLStatementContext(new SelectStatement(), + new GroupByContext(Collections.emptyList(), 0), new OrderByContext(Collections.emptyList(), false), + new SelectItemsContext(0, 0, false, Collections.emptyList()), + new PaginationContext(new NumberLiteralLimitValueSegment(0, 0, 2), null, Collections.emptyList())); SQLRouteResult routeResult = new SQLRouteResult( - shardingStatement, new CommonOptimizedStatement(new SelectStatement()), new ShardingConditions(Collections.emptyList())); + selectSQLStatementContext, new CommonSQLStatementContext(new SelectStatement()), new ShardingConditions(Collections.emptyList())); mergeEngine = new DQLMergeEngine(DatabaseTypes.getActualDatabaseType("MySQL"), null, routeResult, queryResults); MergedResult actual = mergeEngine.merge(); for (int i = 0; i < 6; i++) { @@ -104,12 +104,12 @@ public final class LimitDecoratorMergedResultTest { @Test public void assertNextWithRowCount() throws SQLException { - OptimizedStatement shardingStatement = new SelectOptimizedStatement(new SelectStatement(), - new GroupBy(Collections.emptyList(), 0), new OrderBy(Collections.emptyList(), false), - new SelectItems(0, 0, false, Collections.emptyList()), - new Pagination(new NumberLiteralLimitValueSegment(0, 0, 2), new NumberLiteralLimitValueSegment(0, 0, 2), Collections.emptyList())); + SQLStatementContext selectSQLStatementContext = new SelectSQLStatementContext(new SelectStatement(), + new GroupByContext(Collections.emptyList(), 0), new OrderByContext(Collections.emptyList(), false), + new SelectItemsContext(0, 0, false, Collections.emptyList()), + new PaginationContext(new NumberLiteralLimitValueSegment(0, 0, 2), new NumberLiteralLimitValueSegment(0, 0, 2), Collections.emptyList())); SQLRouteResult routeResult = new SQLRouteResult( - shardingStatement, new CommonOptimizedStatement(new SelectStatement()), new ShardingConditions(Collections.emptyList())); + selectSQLStatementContext, new CommonSQLStatementContext(new SelectStatement()), new ShardingConditions(Collections.emptyList())); mergeEngine = new DQLMergeEngine(DatabaseTypes.getActualDatabaseType("MySQL"), null, routeResult, queryResults); MergedResult actual = mergeEngine.merge(); assertTrue(actual.next()); diff --git a/sharding-core/sharding-core-merge/src/test/java/org/apache/shardingsphere/core/merge/dql/pagination/RowNumberDecoratorMergedResultTest.java b/sharding-core/sharding-core-merge/src/test/java/org/apache/shardingsphere/core/merge/dql/pagination/RowNumberDecoratorMergedResultTest.java index db327bc7ab..4b6e8e789d 100644 --- a/sharding-core/sharding-core-merge/src/test/java/org/apache/shardingsphere/core/merge/dql/pagination/RowNumberDecoratorMergedResultTest.java +++ b/sharding-core/sharding-core-merge/src/test/java/org/apache/shardingsphere/core/merge/dql/pagination/RowNumberDecoratorMergedResultTest.java @@ -23,15 +23,15 @@ import org.apache.shardingsphere.core.execute.sql.execute.result.QueryResult; import org.apache.shardingsphere.core.merge.MergedResult; import org.apache.shardingsphere.core.merge.dql.DQLMergeEngine; import org.apache.shardingsphere.core.merge.fixture.TestQueryResult; -import org.apache.shardingsphere.core.optimize.segment.select.groupby.GroupBy; +import org.apache.shardingsphere.core.optimize.segment.select.groupby.GroupByContext; import org.apache.shardingsphere.core.optimize.segment.select.item.SelectItem; -import org.apache.shardingsphere.core.optimize.segment.select.item.SelectItems; -import org.apache.shardingsphere.core.optimize.segment.select.orderby.OrderBy; +import org.apache.shardingsphere.core.optimize.segment.select.item.SelectItemsContext; +import org.apache.shardingsphere.core.optimize.segment.select.orderby.OrderByContext; import org.apache.shardingsphere.core.optimize.segment.select.orderby.OrderByItem; -import org.apache.shardingsphere.core.optimize.segment.select.pagination.Pagination; -import org.apache.shardingsphere.core.optimize.statement.OptimizedStatement; -import org.apache.shardingsphere.core.optimize.statement.impl.CommonOptimizedStatement; -import org.apache.shardingsphere.core.optimize.statement.impl.SelectOptimizedStatement; +import org.apache.shardingsphere.core.optimize.segment.select.pagination.PaginationContext; +import org.apache.shardingsphere.core.optimize.statement.SQLStatementContext; +import org.apache.shardingsphere.core.optimize.statement.impl.CommonSQLStatementContext; +import org.apache.shardingsphere.core.optimize.statement.impl.SelectSQLStatementContext; import org.apache.shardingsphere.core.parse.sql.segment.dml.pagination.rownum.NumberLiteralRowNumberValueSegment; import org.apache.shardingsphere.core.parse.sql.statement.dml.SelectStatement; import org.apache.shardingsphere.core.route.SQLRouteResult; @@ -75,12 +75,12 @@ public final class RowNumberDecoratorMergedResultTest { @Test public void assertNextForSkipAll() throws SQLException { - OptimizedStatement shardingStatement = new SelectOptimizedStatement(new SelectStatement(), - new GroupBy(Collections.emptyList(), 0), new OrderBy(Collections.emptyList(), false), - new SelectItems(0, 0, false, Collections.emptyList()), - new Pagination(new NumberLiteralRowNumberValueSegment(0, 0, Integer.MAX_VALUE, true), null, Collections.emptyList())); + SQLStatementContext selectSQLStatementContext = new SelectSQLStatementContext(new SelectStatement(), + new GroupByContext(Collections.emptyList(), 0), new OrderByContext(Collections.emptyList(), false), + new SelectItemsContext(0, 0, false, Collections.emptyList()), + new PaginationContext(new NumberLiteralRowNumberValueSegment(0, 0, Integer.MAX_VALUE, true), null, Collections.emptyList())); SQLRouteResult routeResult = new SQLRouteResult( - shardingStatement, new CommonOptimizedStatement(new SelectStatement()), new ShardingConditions(Collections.emptyList())); + selectSQLStatementContext, new CommonSQLStatementContext(new SelectStatement()), new ShardingConditions(Collections.emptyList())); mergeEngine = new DQLMergeEngine(DatabaseTypes.getActualDatabaseType("Oracle"), null, routeResult, queryResults); MergedResult actual = mergeEngine.merge(); assertFalse(actual.next()); @@ -88,11 +88,11 @@ public final class RowNumberDecoratorMergedResultTest { @Test public void assertNextWithoutOffsetWithoutRowCount() throws SQLException { - OptimizedStatement shardingStatement = new SelectOptimizedStatement(new SelectStatement(), - new GroupBy(Collections.emptyList(), 0), new OrderBy(Collections.emptyList(), false), - new SelectItems(0, 0, false, Collections.emptyList()), new Pagination(null, null, Collections.emptyList())); + SQLStatementContext selectSQLStatementContext = new SelectSQLStatementContext(new SelectStatement(), + new GroupByContext(Collections.emptyList(), 0), new OrderByContext(Collections.emptyList(), false), + new SelectItemsContext(0, 0, false, Collections.emptyList()), new PaginationContext(null, null, Collections.emptyList())); SQLRouteResult routeResult = new SQLRouteResult( - shardingStatement, new CommonOptimizedStatement(new SelectStatement()), new ShardingConditions(Collections.emptyList())); + selectSQLStatementContext, new CommonSQLStatementContext(new SelectStatement()), new ShardingConditions(Collections.emptyList())); mergeEngine = new DQLMergeEngine(DatabaseTypes.getActualDatabaseType("Oracle"), null, routeResult, queryResults); MergedResult actual = mergeEngine.merge(); for (int i = 0; i < 8; i++) { @@ -103,12 +103,12 @@ public final class RowNumberDecoratorMergedResultTest { @Test public void assertNextForRowCountBoundOpenedFalse() throws SQLException { - OptimizedStatement shardingStatement = new SelectOptimizedStatement(new SelectStatement(), - new GroupBy(Collections.emptyList(), 0), new OrderBy(Collections.emptyList(), false), - new SelectItems(0, 0, false, Collections.emptyList()), - new Pagination(new NumberLiteralRowNumberValueSegment(0, 0, 2, true), new NumberLiteralRowNumberValueSegment(0, 0, 4, false), Collections.emptyList())); + SQLStatementContext selectSQLStatementContext = new SelectSQLStatementContext(new SelectStatement(), + new GroupByContext(Collections.emptyList(), 0), new OrderByContext(Collections.emptyList(), false), + new SelectItemsContext(0, 0, false, Collections.emptyList()), + new PaginationContext(new NumberLiteralRowNumberValueSegment(0, 0, 2, true), new NumberLiteralRowNumberValueSegment(0, 0, 4, false), Collections.emptyList())); SQLRouteResult routeResult = new SQLRouteResult( - shardingStatement, new CommonOptimizedStatement(new SelectStatement()), new ShardingConditions(Collections.emptyList())); + selectSQLStatementContext, new CommonSQLStatementContext(new SelectStatement()), new ShardingConditions(Collections.emptyList())); mergeEngine = new DQLMergeEngine(DatabaseTypes.getActualDatabaseType("Oracle"), null, routeResult, queryResults); MergedResult actual = mergeEngine.merge(); assertTrue(actual.next()); @@ -118,12 +118,12 @@ public final class RowNumberDecoratorMergedResultTest { @Test public void assertNextForRowCountBoundOpenedTrue() throws SQLException { - OptimizedStatement shardingStatement = new SelectOptimizedStatement(new SelectStatement(), - new GroupBy(Collections.emptyList(), 0), new OrderBy(Collections.emptyList(), false), - new SelectItems(0, 0, false, Collections.emptyList()), - new Pagination(new NumberLiteralRowNumberValueSegment(0, 0, 2, true), new NumberLiteralRowNumberValueSegment(0, 0, 4, true), Collections.emptyList())); + SQLStatementContext selectSQLStatementContext = new SelectSQLStatementContext(new SelectStatement(), + new GroupByContext(Collections.emptyList(), 0), new OrderByContext(Collections.emptyList(), false), + new SelectItemsContext(0, 0, false, Collections.emptyList()), + new PaginationContext(new NumberLiteralRowNumberValueSegment(0, 0, 2, true), new NumberLiteralRowNumberValueSegment(0, 0, 4, true), Collections.emptyList())); SQLRouteResult routeResult = new SQLRouteResult( - shardingStatement, new CommonOptimizedStatement(new SelectStatement()), new ShardingConditions(Collections.emptyList())); + selectSQLStatementContext, new CommonSQLStatementContext(new SelectStatement()), new ShardingConditions(Collections.emptyList())); mergeEngine = new DQLMergeEngine(DatabaseTypes.getActualDatabaseType("Oracle"), null, routeResult, queryResults); MergedResult actual = mergeEngine.merge(); assertTrue(actual.next()); diff --git a/sharding-core/sharding-core-merge/src/test/java/org/apache/shardingsphere/core/merge/dql/pagination/TopAndRowNumberDecoratorMergedResultTest.java b/sharding-core/sharding-core-merge/src/test/java/org/apache/shardingsphere/core/merge/dql/pagination/TopAndRowNumberDecoratorMergedResultTest.java index a1b4274067..9f0c763aae 100644 --- a/sharding-core/sharding-core-merge/src/test/java/org/apache/shardingsphere/core/merge/dql/pagination/TopAndRowNumberDecoratorMergedResultTest.java +++ b/sharding-core/sharding-core-merge/src/test/java/org/apache/shardingsphere/core/merge/dql/pagination/TopAndRowNumberDecoratorMergedResultTest.java @@ -23,15 +23,15 @@ import org.apache.shardingsphere.core.execute.sql.execute.result.QueryResult; import org.apache.shardingsphere.core.merge.MergedResult; import org.apache.shardingsphere.core.merge.dql.DQLMergeEngine; import org.apache.shardingsphere.core.merge.fixture.TestQueryResult; -import org.apache.shardingsphere.core.optimize.segment.select.groupby.GroupBy; +import org.apache.shardingsphere.core.optimize.segment.select.groupby.GroupByContext; import org.apache.shardingsphere.core.optimize.segment.select.item.SelectItem; -import org.apache.shardingsphere.core.optimize.segment.select.item.SelectItems; -import org.apache.shardingsphere.core.optimize.segment.select.orderby.OrderBy; +import org.apache.shardingsphere.core.optimize.segment.select.item.SelectItemsContext; +import org.apache.shardingsphere.core.optimize.segment.select.orderby.OrderByContext; import org.apache.shardingsphere.core.optimize.segment.select.orderby.OrderByItem; -import org.apache.shardingsphere.core.optimize.segment.select.pagination.Pagination; -import org.apache.shardingsphere.core.optimize.statement.OptimizedStatement; -import org.apache.shardingsphere.core.optimize.statement.impl.CommonOptimizedStatement; -import org.apache.shardingsphere.core.optimize.statement.impl.SelectOptimizedStatement; +import org.apache.shardingsphere.core.optimize.segment.select.pagination.PaginationContext; +import org.apache.shardingsphere.core.optimize.statement.SQLStatementContext; +import org.apache.shardingsphere.core.optimize.statement.impl.CommonSQLStatementContext; +import org.apache.shardingsphere.core.optimize.statement.impl.SelectSQLStatementContext; import org.apache.shardingsphere.core.parse.sql.segment.dml.pagination.limit.NumberLiteralLimitValueSegment; import org.apache.shardingsphere.core.parse.sql.segment.dml.pagination.rownum.NumberLiteralRowNumberValueSegment; import org.apache.shardingsphere.core.parse.sql.statement.dml.SelectStatement; @@ -76,12 +76,12 @@ public final class TopAndRowNumberDecoratorMergedResultTest { @Test public void assertNextForSkipAll() throws SQLException { - OptimizedStatement shardingStatement = new SelectOptimizedStatement(new SelectStatement(), - new GroupBy(Collections.emptyList(), 0), new OrderBy(Collections.emptyList(), false), - new SelectItems(0, 0, false, Collections.emptyList()), - new Pagination(new NumberLiteralRowNumberValueSegment(0, 0, Integer.MAX_VALUE, true), null, Collections.emptyList())); + SQLStatementContext selectSQLStatementContext = new SelectSQLStatementContext(new SelectStatement(), + new GroupByContext(Collections.emptyList(), 0), new OrderByContext(Collections.emptyList(), false), + new SelectItemsContext(0, 0, false, Collections.emptyList()), + new PaginationContext(new NumberLiteralRowNumberValueSegment(0, 0, Integer.MAX_VALUE, true), null, Collections.emptyList())); SQLRouteResult routeResult = new SQLRouteResult( - shardingStatement, new CommonOptimizedStatement(new SelectStatement()), new ShardingConditions(Collections.emptyList())); + selectSQLStatementContext, new CommonSQLStatementContext(new SelectStatement()), new ShardingConditions(Collections.emptyList())); mergeEngine = new DQLMergeEngine(DatabaseTypes.getActualDatabaseType("SQLServer"), null, routeResult, queryResults); MergedResult actual = mergeEngine.merge(); assertFalse(actual.next()); @@ -89,12 +89,12 @@ public final class TopAndRowNumberDecoratorMergedResultTest { @Test public void assertNextWithoutOffsetWithRowCount() throws SQLException { - OptimizedStatement shardingStatement = new SelectOptimizedStatement(new SelectStatement(), - new GroupBy(Collections.emptyList(), 0), new OrderBy(Collections.emptyList(), false), - new SelectItems(0, 0, false, Collections.emptyList()), - new Pagination(null, new NumberLiteralLimitValueSegment(0, 0, 5), Collections.emptyList())); + SQLStatementContext selectSQLStatementContext = new SelectSQLStatementContext(new SelectStatement(), + new GroupByContext(Collections.emptyList(), 0), new OrderByContext(Collections.emptyList(), false), + new SelectItemsContext(0, 0, false, Collections.emptyList()), + new PaginationContext(null, new NumberLiteralLimitValueSegment(0, 0, 5), Collections.emptyList())); SQLRouteResult routeResult = new SQLRouteResult( - shardingStatement, new CommonOptimizedStatement(new SelectStatement()), new ShardingConditions(Collections.emptyList())); + selectSQLStatementContext, new CommonSQLStatementContext(new SelectStatement()), new ShardingConditions(Collections.emptyList())); mergeEngine = new DQLMergeEngine(DatabaseTypes.getActualDatabaseType("SQLServer"), null, routeResult, queryResults); MergedResult actual = mergeEngine.merge(); for (int i = 0; i < 5; i++) { @@ -105,12 +105,12 @@ public final class TopAndRowNumberDecoratorMergedResultTest { @Test public void assertNextWithOffsetWithoutRowCount() throws SQLException { - OptimizedStatement shardingStatement = new SelectOptimizedStatement(new SelectStatement(), - new GroupBy(Collections.emptyList(), 0), new OrderBy(Collections.emptyList(), false), - new SelectItems(0, 0, false, Collections.emptyList()), - new Pagination(new NumberLiteralRowNumberValueSegment(0, 0, 2, true), null, Collections.emptyList())); + SQLStatementContext selectSQLStatementContext = new SelectSQLStatementContext(new SelectStatement(), + new GroupByContext(Collections.emptyList(), 0), new OrderByContext(Collections.emptyList(), false), + new SelectItemsContext(0, 0, false, Collections.emptyList()), + new PaginationContext(new NumberLiteralRowNumberValueSegment(0, 0, 2, true), null, Collections.emptyList())); SQLRouteResult routeResult = new SQLRouteResult( - shardingStatement, new CommonOptimizedStatement(new SelectStatement()), new ShardingConditions(Collections.emptyList())); + selectSQLStatementContext, new CommonSQLStatementContext(new SelectStatement()), new ShardingConditions(Collections.emptyList())); mergeEngine = new DQLMergeEngine(DatabaseTypes.getActualDatabaseType("SQLServer"), null, routeResult, queryResults); MergedResult actual = mergeEngine.merge(); for (int i = 0; i < 7; i++) { @@ -121,12 +121,12 @@ public final class TopAndRowNumberDecoratorMergedResultTest { @Test public void assertNextWithOffsetBoundOpenedFalse() throws SQLException { - OptimizedStatement shardingStatement = new SelectOptimizedStatement(new SelectStatement(), - new GroupBy(Collections.emptyList(), 0), new OrderBy(Collections.emptyList(), false), - new SelectItems(0, 0, false, Collections.emptyList()), - new Pagination(new NumberLiteralRowNumberValueSegment(0, 0, 2, false), new NumberLiteralLimitValueSegment(0, 0, 4), Collections.emptyList())); + SQLStatementContext selectSQLStatementContext = new SelectSQLStatementContext(new SelectStatement(), + new GroupByContext(Collections.emptyList(), 0), new OrderByContext(Collections.emptyList(), false), + new SelectItemsContext(0, 0, false, Collections.emptyList()), + new PaginationContext(new NumberLiteralRowNumberValueSegment(0, 0, 2, false), new NumberLiteralLimitValueSegment(0, 0, 4), Collections.emptyList())); SQLRouteResult routeResult = new SQLRouteResult( - shardingStatement, new CommonOptimizedStatement(new SelectStatement()), new ShardingConditions(Collections.emptyList())); + selectSQLStatementContext, new CommonSQLStatementContext(new SelectStatement()), new ShardingConditions(Collections.emptyList())); mergeEngine = new DQLMergeEngine(DatabaseTypes.getActualDatabaseType("SQLServer"), null, routeResult, queryResults); MergedResult actual = mergeEngine.merge(); assertTrue(actual.next()); @@ -136,12 +136,12 @@ public final class TopAndRowNumberDecoratorMergedResultTest { @Test public void assertNextWithOffsetBoundOpenedTrue() throws SQLException { - OptimizedStatement shardingStatement = new SelectOptimizedStatement(new SelectStatement(), - new GroupBy(Collections.emptyList(), 0), new OrderBy(Collections.emptyList(), false), - new SelectItems(0, 0, false, Collections.emptyList()), - new Pagination(new NumberLiteralRowNumberValueSegment(0, 0, 2, true), new NumberLiteralLimitValueSegment(0, 0, 4), Collections.emptyList())); + SQLStatementContext selectSQLStatementContext = new SelectSQLStatementContext(new SelectStatement(), + new GroupByContext(Collections.emptyList(), 0), new OrderByContext(Collections.emptyList(), false), + new SelectItemsContext(0, 0, false, Collections.emptyList()), + new PaginationContext(new NumberLiteralRowNumberValueSegment(0, 0, 2, true), new NumberLiteralLimitValueSegment(0, 0, 4), Collections.emptyList())); SQLRouteResult routeResult = new SQLRouteResult( - shardingStatement, new CommonOptimizedStatement(new SelectStatement()), new ShardingConditions(Collections.emptyList())); + selectSQLStatementContext, new CommonSQLStatementContext(new SelectStatement()), new ShardingConditions(Collections.emptyList())); mergeEngine = new DQLMergeEngine(DatabaseTypes.getActualDatabaseType("SQLServer"), null, routeResult, queryResults); MergedResult actual = mergeEngine.merge(); assertTrue(actual.next()); diff --git a/sharding-core/sharding-core-optimize/src/main/java/org/apache/shardingsphere/core/optimize/OptimizedStatementFactory.java b/sharding-core/sharding-core-optimize/src/main/java/org/apache/shardingsphere/core/optimize/SQLStatementContextFactory.java similarity index 71% rename from sharding-core/sharding-core-optimize/src/main/java/org/apache/shardingsphere/core/optimize/OptimizedStatementFactory.java rename to sharding-core/sharding-core-optimize/src/main/java/org/apache/shardingsphere/core/optimize/SQLStatementContextFactory.java index 6e33a6e8b2..1e097eafa9 100644 --- a/sharding-core/sharding-core-optimize/src/main/java/org/apache/shardingsphere/core/optimize/OptimizedStatementFactory.java +++ b/sharding-core/sharding-core-optimize/src/main/java/org/apache/shardingsphere/core/optimize/SQLStatementContextFactory.java @@ -20,10 +20,10 @@ package org.apache.shardingsphere.core.optimize; import lombok.AccessLevel; import lombok.NoArgsConstructor; import org.apache.shardingsphere.core.metadata.table.TableMetas; -import org.apache.shardingsphere.core.optimize.statement.impl.CommonOptimizedStatement; -import org.apache.shardingsphere.core.optimize.statement.impl.InsertOptimizedStatement; -import org.apache.shardingsphere.core.optimize.statement.OptimizedStatement; -import org.apache.shardingsphere.core.optimize.statement.impl.SelectOptimizedStatement; +import org.apache.shardingsphere.core.optimize.statement.impl.CommonSQLStatementContext; +import org.apache.shardingsphere.core.optimize.statement.impl.InsertSQLStatementContext; +import org.apache.shardingsphere.core.optimize.statement.SQLStatementContext; +import org.apache.shardingsphere.core.optimize.statement.impl.SelectSQLStatementContext; import org.apache.shardingsphere.core.parse.sql.statement.SQLStatement; import org.apache.shardingsphere.core.parse.sql.statement.dml.InsertStatement; import org.apache.shardingsphere.core.parse.sql.statement.dml.SelectStatement; @@ -31,31 +31,31 @@ import org.apache.shardingsphere.core.parse.sql.statement.dml.SelectStatement; import java.util.List; /** - * Optimized statement factory. + * SQL statement context factory. * * @author zhangliang * @author maxiaoguang * @author panjuan */ @NoArgsConstructor(access = AccessLevel.PRIVATE) -public final class OptimizedStatementFactory { +public final class SQLStatementContextFactory { /** - * Create optimized statement. + * Create SQL statement context. * * @param tableMetas table meta data * @param sql SQL * @param parameters SQL parameters * @param sqlStatement SQL statement - * @return optimized statement + * @return SQL statement context */ - public static OptimizedStatement newInstance(final TableMetas tableMetas, final String sql, final List parameters, final SQLStatement sqlStatement) { + public static SQLStatementContext newInstance(final TableMetas tableMetas, final String sql, final List parameters, final SQLStatement sqlStatement) { if (sqlStatement instanceof SelectStatement) { - return new SelectOptimizedStatement(tableMetas, sql, parameters, (SelectStatement) sqlStatement); + return new SelectSQLStatementContext(tableMetas, sql, parameters, (SelectStatement) sqlStatement); } if (sqlStatement instanceof InsertStatement) { - return new InsertOptimizedStatement(tableMetas, parameters, (InsertStatement) sqlStatement); + return new InsertSQLStatementContext(tableMetas, parameters, (InsertStatement) sqlStatement); } - return new CommonOptimizedStatement(sqlStatement); + return new CommonSQLStatementContext(sqlStatement); } } diff --git a/sharding-core/sharding-core-optimize/src/main/java/org/apache/shardingsphere/core/optimize/segment/insert/InsertValue.java b/sharding-core/sharding-core-optimize/src/main/java/org/apache/shardingsphere/core/optimize/segment/insert/InsertValueContext.java similarity index 95% rename from sharding-core/sharding-core-optimize/src/main/java/org/apache/shardingsphere/core/optimize/segment/insert/InsertValue.java rename to sharding-core/sharding-core-optimize/src/main/java/org/apache/shardingsphere/core/optimize/segment/insert/InsertValueContext.java index 25138d3abd..11789a41de 100644 --- a/sharding-core/sharding-core-optimize/src/main/java/org/apache/shardingsphere/core/optimize/segment/insert/InsertValue.java +++ b/sharding-core/sharding-core-optimize/src/main/java/org/apache/shardingsphere/core/optimize/segment/insert/InsertValueContext.java @@ -32,14 +32,14 @@ import java.util.Collections; import java.util.List; /** - * Insert value. + * Insert value context. * * @author panjuan * @author zhangliang */ @Getter @ToString -public final class InsertValue { +public final class InsertValueContext { private final int parametersCount; @@ -47,7 +47,7 @@ public final class InsertValue { private final List parameters; - public InsertValue(final Collection assignments, final List parameters, final int parametersOffset) { + public InsertValueContext(final Collection assignments, final List parameters, final int parametersOffset) { parametersCount = calculateParametersCount(assignments); valueExpressions = getValueExpressions(assignments); this.parameters = getParameters(parameters, parametersOffset); diff --git a/sharding-core/sharding-core-optimize/src/main/java/org/apache/shardingsphere/core/optimize/segment/select/groupby/GroupBy.java b/sharding-core/sharding-core-optimize/src/main/java/org/apache/shardingsphere/core/optimize/segment/select/groupby/GroupByContext.java similarity index 95% rename from sharding-core/sharding-core-optimize/src/main/java/org/apache/shardingsphere/core/optimize/segment/select/groupby/GroupBy.java rename to sharding-core/sharding-core-optimize/src/main/java/org/apache/shardingsphere/core/optimize/segment/select/groupby/GroupByContext.java index 8254dd9fec..ab7b0403d4 100644 --- a/sharding-core/sharding-core-optimize/src/main/java/org/apache/shardingsphere/core/optimize/segment/select/groupby/GroupBy.java +++ b/sharding-core/sharding-core-optimize/src/main/java/org/apache/shardingsphere/core/optimize/segment/select/groupby/GroupByContext.java @@ -24,13 +24,13 @@ import org.apache.shardingsphere.core.optimize.segment.select.orderby.OrderByIte import java.util.Collection; /** - * Group by. + * Group by context. * * @author zhangliang */ @RequiredArgsConstructor @Getter -public final class GroupBy { +public final class GroupByContext { private final Collection items; diff --git a/sharding-core/sharding-core-optimize/src/main/java/org/apache/shardingsphere/core/optimize/segment/select/groupby/engine/GroupByEngine.java b/sharding-core/sharding-core-optimize/src/main/java/org/apache/shardingsphere/core/optimize/segment/select/groupby/engine/GroupByContextEngine.java similarity index 82% rename from sharding-core/sharding-core-optimize/src/main/java/org/apache/shardingsphere/core/optimize/segment/select/groupby/engine/GroupByEngine.java rename to sharding-core/sharding-core-optimize/src/main/java/org/apache/shardingsphere/core/optimize/segment/select/groupby/engine/GroupByContextEngine.java index 56ba253777..254578fcb2 100644 --- a/sharding-core/sharding-core-optimize/src/main/java/org/apache/shardingsphere/core/optimize/segment/select/groupby/engine/GroupByEngine.java +++ b/sharding-core/sharding-core-optimize/src/main/java/org/apache/shardingsphere/core/optimize/segment/select/groupby/engine/GroupByContextEngine.java @@ -17,7 +17,7 @@ package org.apache.shardingsphere.core.optimize.segment.select.groupby.engine; -import org.apache.shardingsphere.core.optimize.segment.select.groupby.GroupBy; +import org.apache.shardingsphere.core.optimize.segment.select.groupby.GroupByContext; import org.apache.shardingsphere.core.optimize.segment.select.orderby.OrderByItem; import org.apache.shardingsphere.core.parse.sql.segment.dml.order.item.IndexOrderByItemSegment; import org.apache.shardingsphere.core.parse.sql.segment.dml.order.item.OrderByItemSegment; @@ -28,21 +28,21 @@ import java.util.Collections; import java.util.LinkedList; /** - * Group by engine. + * Group by context engine. * * @author zhangliang */ -public final class GroupByEngine { +public final class GroupByContextEngine { /** - * Create group by. + * Create group by context. * * @param selectStatement select statement - * @return group by + * @return group by context */ - public GroupBy createGroupBy(final SelectStatement selectStatement) { + public GroupByContext createGroupByContext(final SelectStatement selectStatement) { if (!selectStatement.getGroupBy().isPresent()) { - return new GroupBy(Collections.emptyList(), 0); + return new GroupByContext(Collections.emptyList(), 0); } Collection groupByItems = new LinkedList<>(); for (OrderByItemSegment each : selectStatement.getGroupBy().get().getGroupByItems()) { @@ -52,6 +52,6 @@ public final class GroupByEngine { } groupByItems.add(orderByItem); } - return new GroupBy(groupByItems, selectStatement.getGroupBy().get().getStopIndex()); + return new GroupByContext(groupByItems, selectStatement.getGroupBy().get().getStopIndex()); } } diff --git a/sharding-core/sharding-core-optimize/src/main/java/org/apache/shardingsphere/core/optimize/segment/select/item/SelectItems.java b/sharding-core/sharding-core-optimize/src/main/java/org/apache/shardingsphere/core/optimize/segment/select/item/SelectItemsContext.java similarity index 98% rename from sharding-core/sharding-core-optimize/src/main/java/org/apache/shardingsphere/core/optimize/segment/select/item/SelectItems.java rename to sharding-core/sharding-core-optimize/src/main/java/org/apache/shardingsphere/core/optimize/segment/select/item/SelectItemsContext.java index 54296ca44d..d0b38aae07 100644 --- a/sharding-core/sharding-core-optimize/src/main/java/org/apache/shardingsphere/core/optimize/segment/select/item/SelectItems.java +++ b/sharding-core/sharding-core-optimize/src/main/java/org/apache/shardingsphere/core/optimize/segment/select/item/SelectItemsContext.java @@ -20,7 +20,6 @@ package org.apache.shardingsphere.core.optimize.segment.select.item; import com.google.common.base.Optional; import lombok.Getter; import lombok.RequiredArgsConstructor; -import lombok.Setter; import lombok.ToString; import org.apache.shardingsphere.core.metadata.table.TableMetas; import org.apache.shardingsphere.core.optimize.segment.select.item.impl.AggregationDistinctSelectItem; @@ -35,16 +34,15 @@ import java.util.LinkedList; import java.util.List; /** - * Select items. + * Select items context. * * @author zhangliang * @author sunbufu */ @RequiredArgsConstructor @Getter -@Setter @ToString -public final class SelectItems { +public final class SelectItemsContext { private final int startIndex; diff --git a/sharding-core/sharding-core-optimize/src/main/java/org/apache/shardingsphere/core/optimize/segment/select/item/engine/SelectItemsEngine.java b/sharding-core/sharding-core-optimize/src/main/java/org/apache/shardingsphere/core/optimize/segment/select/item/engine/SelectItemsContextEngine.java similarity index 69% rename from sharding-core/sharding-core-optimize/src/main/java/org/apache/shardingsphere/core/optimize/segment/select/item/engine/SelectItemsEngine.java rename to sharding-core/sharding-core-optimize/src/main/java/org/apache/shardingsphere/core/optimize/segment/select/item/engine/SelectItemsContextEngine.java index 5881dfe9c3..0cc91e6c94 100644 --- a/sharding-core/sharding-core-optimize/src/main/java/org/apache/shardingsphere/core/optimize/segment/select/item/engine/SelectItemsEngine.java +++ b/sharding-core/sharding-core-optimize/src/main/java/org/apache/shardingsphere/core/optimize/segment/select/item/engine/SelectItemsContextEngine.java @@ -21,16 +21,16 @@ import com.google.common.base.Optional; import com.google.common.base.Preconditions; import lombok.RequiredArgsConstructor; import org.apache.shardingsphere.core.metadata.table.TableMetas; -import org.apache.shardingsphere.core.optimize.segment.Table; -import org.apache.shardingsphere.core.optimize.segment.Tables; +import org.apache.shardingsphere.core.optimize.segment.table.Table; +import org.apache.shardingsphere.core.optimize.segment.table.TablesContext; import org.apache.shardingsphere.core.optimize.segment.select.item.DerivedColumn; import org.apache.shardingsphere.core.optimize.segment.select.item.SelectItem; -import org.apache.shardingsphere.core.optimize.segment.select.item.SelectItems; +import org.apache.shardingsphere.core.optimize.segment.select.item.SelectItemsContext; import org.apache.shardingsphere.core.optimize.segment.select.item.impl.ShorthandSelectItem; import org.apache.shardingsphere.core.optimize.segment.select.item.impl.DerivedSelectItem; import org.apache.shardingsphere.core.optimize.segment.select.orderby.OrderByItem; -import org.apache.shardingsphere.core.optimize.segment.select.groupby.GroupBy; -import org.apache.shardingsphere.core.optimize.segment.select.orderby.OrderBy; +import org.apache.shardingsphere.core.optimize.segment.select.groupby.GroupByContext; +import org.apache.shardingsphere.core.optimize.segment.select.orderby.OrderByContext; import org.apache.shardingsphere.core.parse.sql.segment.dml.item.SelectItemSegment; import org.apache.shardingsphere.core.parse.sql.segment.dml.item.SelectItemsSegment; import org.apache.shardingsphere.core.parse.sql.segment.dml.order.item.ColumnOrderByItemSegment; @@ -43,34 +43,34 @@ import java.util.Collection; import java.util.LinkedList; /** - * Select items engine. + * Select items context engine. * * @author zhangliang * @author sunbufu */ @RequiredArgsConstructor -public final class SelectItemsEngine { +public final class SelectItemsContextEngine { private final TableMetas tableMetas; private final SelectItemEngine selectItemEngine = new SelectItemEngine(); /** - * Create select items. + * Create select items context. * * @param sql SQL * @param selectStatement SQL statement - * @param groupBy group by - * @param orderBy order by - * @return select items + * @param groupByContext group by context + * @param orderByContext order by context + * @return select items context */ - public SelectItems createSelectItems(final String sql, final SelectStatement selectStatement, final GroupBy groupBy, final OrderBy orderBy) { + public SelectItemsContext createSelectItemsContext(final String sql, final SelectStatement selectStatement, final GroupByContext groupByContext, final OrderByContext orderByContext) { SelectItemsSegment selectItemsSegment = selectStatement.getSelectItems(); Collection items = getSelectItemList(sql, selectItemsSegment); - SelectItems result = new SelectItems(selectItemsSegment.getStartIndex(), selectItemsSegment.getStopIndex(), selectItemsSegment.isDistinctRow(), items); - Tables tables = new Tables(selectStatement); - result.getItems().addAll(getDerivedGroupByColumns(tables, items, groupBy)); - result.getItems().addAll(getDerivedOrderByColumns(tables, items, orderBy)); + SelectItemsContext result = new SelectItemsContext(selectItemsSegment.getStartIndex(), selectItemsSegment.getStopIndex(), selectItemsSegment.isDistinctRow(), items); + TablesContext tablesContext = new TablesContext(selectStatement); + result.getItems().addAll(getDerivedGroupByColumns(tablesContext, items, groupByContext)); + result.getItems().addAll(getDerivedOrderByColumns(tablesContext, items, orderByContext)); return result; } @@ -85,33 +85,34 @@ public final class SelectItemsEngine { return result; } - private Collection getDerivedGroupByColumns(final Tables tables, final Collection selectItems, final GroupBy groupBy) { - return getDerivedOrderColumns(tables, selectItems, groupBy.getItems(), DerivedColumn.GROUP_BY_ALIAS); + private Collection getDerivedGroupByColumns(final TablesContext tablesContext, final Collection selectItems, final GroupByContext groupByContext) { + return getDerivedOrderColumns(tablesContext, selectItems, groupByContext.getItems(), DerivedColumn.GROUP_BY_ALIAS); } - private Collection getDerivedOrderByColumns(final Tables tables, final Collection selectItems, final OrderBy orderBy) { - return getDerivedOrderColumns(tables, selectItems, orderBy.getItems(), DerivedColumn.ORDER_BY_ALIAS); + private Collection getDerivedOrderByColumns(final TablesContext tablesContext, final Collection selectItems, final OrderByContext orderByContext) { + return getDerivedOrderColumns(tablesContext, selectItems, orderByContext.getItems(), DerivedColumn.ORDER_BY_ALIAS); } - private Collection getDerivedOrderColumns(final Tables tables, final Collection selectItems, final Collection orderItems, final DerivedColumn derivedColumn) { + private Collection getDerivedOrderColumns(final TablesContext tablesContext, + final Collection selectItems, final Collection orderItems, final DerivedColumn derivedColumn) { Collection result = new LinkedList<>(); int derivedColumnOffset = 0; for (OrderByItem each : orderItems) { - if (!containsItem(tables, selectItems, each.getSegment())) { + if (!containsItem(tablesContext, selectItems, each.getSegment())) { result.add(new DerivedSelectItem(((TextOrderByItemSegment) each.getSegment()).getText(), derivedColumn.getDerivedColumnAlias(derivedColumnOffset++))); } } return result; } - private boolean containsItem(final Tables tables, final Collection items, final OrderByItemSegment orderByItemSegment) { + private boolean containsItem(final TablesContext tablesContext, final Collection items, final OrderByItemSegment orderByItemSegment) { return orderByItemSegment instanceof IndexOrderByItemSegment - || containsItemInShorthandItems(tables, items, orderByItemSegment) || containsItemInSelectItems(items, orderByItemSegment); + || containsItemInShorthandItems(tablesContext, items, orderByItemSegment) || containsItemInSelectItems(items, orderByItemSegment); } - private boolean containsItemInShorthandItems(final Tables tables, final Collection items, final OrderByItemSegment orderByItemSegment) { + private boolean containsItemInShorthandItems(final TablesContext tablesContext, final Collection items, final OrderByItemSegment orderByItemSegment) { return isUnqualifiedShorthandItem(items) - || containsItemWithOwnerInShorthandItems(tables, items, orderByItemSegment) || containsItemWithoutOwnerInShorthandItems(tables, items, orderByItemSegment); + || containsItemWithOwnerInShorthandItems(tablesContext, items, orderByItemSegment) || containsItemWithoutOwnerInShorthandItems(tablesContext, items, orderByItemSegment); } private boolean isUnqualifiedShorthandItem(final Collection items) { @@ -122,13 +123,13 @@ public final class SelectItemsEngine { return item instanceof ShorthandSelectItem && !((ShorthandSelectItem) item).getOwner().isPresent(); } - private boolean containsItemWithOwnerInShorthandItems(final Tables tables, final Collection items, final OrderByItemSegment orderItem) { + private boolean containsItemWithOwnerInShorthandItems(final TablesContext tablesContext, final Collection items, final OrderByItemSegment orderItem) { return orderItem instanceof ColumnOrderByItemSegment && ((ColumnOrderByItemSegment) orderItem).getColumn().getOwner().isPresent() - && findShorthandItem(tables, items, ((ColumnOrderByItemSegment) orderItem).getColumn().getOwner().get().getTableName()).isPresent(); + && findShorthandItem(tablesContext, items, ((ColumnOrderByItemSegment) orderItem).getColumn().getOwner().get().getTableName()).isPresent(); } - private Optional findShorthandItem(final Tables tables, final Collection items, final String tableNameOrAlias) { - Optional table = tables.find(tableNameOrAlias); + private Optional findShorthandItem(final TablesContext tablesContext, final Collection items, final String tableNameOrAlias) { + Optional
table = tablesContext.find(tableNameOrAlias); if (!table.isPresent()) { return Optional.absent(); } @@ -137,20 +138,20 @@ public final class SelectItemsEngine { continue; } ShorthandSelectItem shorthandSelectItem = (ShorthandSelectItem) each; - if (shorthandSelectItem.getOwner().isPresent() && tables.find(shorthandSelectItem.getOwner().get()).equals(table)) { + if (shorthandSelectItem.getOwner().isPresent() && tablesContext.find(shorthandSelectItem.getOwner().get()).equals(table)) { return Optional.of(shorthandSelectItem); } } return Optional.absent(); } - private boolean containsItemWithoutOwnerInShorthandItems(final Tables tables, final Collection items, final OrderByItemSegment orderItem) { + private boolean containsItemWithoutOwnerInShorthandItems(final TablesContext tablesContext, final Collection items, final OrderByItemSegment orderItem) { if (!(orderItem instanceof ColumnOrderByItemSegment)) { return false; } if (!((ColumnOrderByItemSegment) orderItem).getColumn().getOwner().isPresent()) { for (ShorthandSelectItem each : getQualifiedShorthandItems(items)) { - if (isSameSelectItem(tables, each, (ColumnOrderByItemSegment) orderItem)) { + if (isSameSelectItem(tablesContext, each, (ColumnOrderByItemSegment) orderItem)) { return true; } } @@ -168,9 +169,9 @@ public final class SelectItemsEngine { return result; } - private boolean isSameSelectItem(final Tables tables, final ShorthandSelectItem shorthandSelectItem, final ColumnOrderByItemSegment orderItem) { + private boolean isSameSelectItem(final TablesContext tablesContext, final ShorthandSelectItem shorthandSelectItem, final ColumnOrderByItemSegment orderItem) { Preconditions.checkState(shorthandSelectItem.getOwner().isPresent()); - Optional
table = tables.find(shorthandSelectItem.getOwner().get()); + Optional
table = tablesContext.find(shorthandSelectItem.getOwner().get()); return table.isPresent() && tableMetas.containsColumn(table.get().getName(), orderItem.getColumn().getName()); } diff --git a/sharding-core/sharding-core-optimize/src/main/java/org/apache/shardingsphere/core/optimize/segment/select/orderby/OrderBy.java b/sharding-core/sharding-core-optimize/src/main/java/org/apache/shardingsphere/core/optimize/segment/select/orderby/OrderByContext.java similarity index 95% rename from sharding-core/sharding-core-optimize/src/main/java/org/apache/shardingsphere/core/optimize/segment/select/orderby/OrderBy.java rename to sharding-core/sharding-core-optimize/src/main/java/org/apache/shardingsphere/core/optimize/segment/select/orderby/OrderByContext.java index 6fc60ee4e8..855e7199e9 100644 --- a/sharding-core/sharding-core-optimize/src/main/java/org/apache/shardingsphere/core/optimize/segment/select/orderby/OrderBy.java +++ b/sharding-core/sharding-core-optimize/src/main/java/org/apache/shardingsphere/core/optimize/segment/select/orderby/OrderByContext.java @@ -23,13 +23,13 @@ import lombok.RequiredArgsConstructor; import java.util.Collection; /** - * Order by. + * Order by context. * * @author zhangliang */ @RequiredArgsConstructor @Getter -public final class OrderBy { +public final class OrderByContext { private final Collection items; diff --git a/sharding-core/sharding-core-optimize/src/main/java/org/apache/shardingsphere/core/optimize/segment/select/orderby/engine/OrderByEngine.java b/sharding-core/sharding-core-optimize/src/main/java/org/apache/shardingsphere/core/optimize/segment/select/orderby/engine/OrderByContextEngine.java similarity index 80% rename from sharding-core/sharding-core-optimize/src/main/java/org/apache/shardingsphere/core/optimize/segment/select/orderby/engine/OrderByEngine.java rename to sharding-core/sharding-core-optimize/src/main/java/org/apache/shardingsphere/core/optimize/segment/select/orderby/engine/OrderByContextEngine.java index 591bb07bd4..3b23cffbee 100644 --- a/sharding-core/sharding-core-optimize/src/main/java/org/apache/shardingsphere/core/optimize/segment/select/orderby/engine/OrderByEngine.java +++ b/sharding-core/sharding-core-optimize/src/main/java/org/apache/shardingsphere/core/optimize/segment/select/orderby/engine/OrderByContextEngine.java @@ -17,8 +17,8 @@ package org.apache.shardingsphere.core.optimize.segment.select.orderby.engine; -import org.apache.shardingsphere.core.optimize.segment.select.groupby.GroupBy; -import org.apache.shardingsphere.core.optimize.segment.select.orderby.OrderBy; +import org.apache.shardingsphere.core.optimize.segment.select.groupby.GroupByContext; +import org.apache.shardingsphere.core.optimize.segment.select.orderby.OrderByContext; import org.apache.shardingsphere.core.optimize.segment.select.orderby.OrderByItem; import org.apache.shardingsphere.core.parse.sql.segment.dml.order.item.IndexOrderByItemSegment; import org.apache.shardingsphere.core.parse.sql.segment.dml.order.item.OrderByItemSegment; @@ -28,22 +28,22 @@ import java.util.LinkedList; import java.util.List; /** - * Order by engine. + * Order by context engine. * * @author zhangliang */ -public final class OrderByEngine { +public final class OrderByContextEngine { /** - * Create order by. + * Create order by context. * * @param selectStatement select statement - * @param groupBy group by - * @return order by + * @param groupByContext group by context + * @return order by context */ - public OrderBy createOrderBy(final SelectStatement selectStatement, final GroupBy groupBy) { + public OrderByContext createOrderBy(final SelectStatement selectStatement, final GroupByContext groupByContext) { if (!selectStatement.getOrderBy().isPresent() || selectStatement.getOrderBy().get().getOrderByItems().isEmpty()) { - return new OrderBy(groupBy.getItems(), !groupBy.getItems().isEmpty()); + return new OrderByContext(groupByContext.getItems(), !groupByContext.getItems().isEmpty()); } List orderByItems = new LinkedList<>(); for (OrderByItemSegment each : selectStatement.getOrderBy().get().getOrderByItems()) { @@ -53,6 +53,6 @@ public final class OrderByEngine { } orderByItems.add(orderByItem); } - return new OrderBy(orderByItems, false); + return new OrderByContext(orderByItems, false); } } diff --git a/sharding-core/sharding-core-optimize/src/main/java/org/apache/shardingsphere/core/optimize/segment/select/pagination/Pagination.java b/sharding-core/sharding-core-optimize/src/main/java/org/apache/shardingsphere/core/optimize/segment/select/pagination/PaginationContext.java similarity index 88% rename from sharding-core/sharding-core-optimize/src/main/java/org/apache/shardingsphere/core/optimize/segment/select/pagination/Pagination.java rename to sharding-core/sharding-core-optimize/src/main/java/org/apache/shardingsphere/core/optimize/segment/select/pagination/PaginationContext.java index c1e9db694e..e4cf2ead82 100644 --- a/sharding-core/sharding-core-optimize/src/main/java/org/apache/shardingsphere/core/optimize/segment/select/pagination/Pagination.java +++ b/sharding-core/sharding-core-optimize/src/main/java/org/apache/shardingsphere/core/optimize/segment/select/pagination/PaginationContext.java @@ -19,7 +19,7 @@ package org.apache.shardingsphere.core.optimize.segment.select.pagination; import com.google.common.base.Optional; import lombok.Getter; -import org.apache.shardingsphere.core.optimize.statement.impl.SelectOptimizedStatement; +import org.apache.shardingsphere.core.optimize.statement.impl.SelectSQLStatementContext; import org.apache.shardingsphere.core.parse.sql.segment.dml.pagination.NumberLiteralPaginationValueSegment; import org.apache.shardingsphere.core.parse.sql.segment.dml.pagination.PaginationValueSegment; import org.apache.shardingsphere.core.parse.sql.segment.dml.pagination.ParameterMarkerPaginationValueSegment; @@ -28,13 +28,13 @@ import org.apache.shardingsphere.core.parse.sql.segment.dml.pagination.limit.Lim import java.util.List; /** - * Pagination. + * Pagination context. * * @author zhangliang * @author caohao * @author zhangyonglun */ -public final class Pagination { +public final class PaginationContext { @Getter private final boolean hasPagination; @@ -47,7 +47,7 @@ public final class Pagination { private final Long actualRowCount; - public Pagination(final PaginationValueSegment offsetSegment, final PaginationValueSegment rowCountSegment, final List parameters) { + public PaginationContext(final PaginationValueSegment offsetSegment, final PaginationValueSegment rowCountSegment, final List parameters) { hasPagination = null != offsetSegment || null != rowCountSegment; this.offsetSegment = offsetSegment; this.rowCountSegment = rowCountSegment; @@ -140,15 +140,15 @@ public final class Pagination { * @param shardingStatement sharding optimized statement * @return revised row count */ - public long getRevisedRowCount(final SelectOptimizedStatement shardingStatement) { + public long getRevisedRowCount(final SelectSQLStatementContext shardingStatement) { if (isMaxRowCount(shardingStatement)) { return Integer.MAX_VALUE; } return rowCountSegment instanceof LimitValueSegment ? actualOffset + actualRowCount : actualRowCount; } - private boolean isMaxRowCount(final SelectOptimizedStatement shardingStatement) { - return (!shardingStatement.getGroupBy().getItems().isEmpty() - || !shardingStatement.getSelectItems().getAggregationSelectItems().isEmpty()) && !shardingStatement.isSameGroupByAndOrderByItems(); + private boolean isMaxRowCount(final SelectSQLStatementContext shardingStatement) { + return (!shardingStatement.getGroupByContext().getItems().isEmpty() + || !shardingStatement.getSelectItemsContext().getAggregationSelectItems().isEmpty()) && !shardingStatement.isSameGroupByAndOrderByItems(); } } diff --git a/sharding-core/sharding-core-optimize/src/main/java/org/apache/shardingsphere/core/optimize/segment/select/pagination/engine/LimitPaginationEngine.java b/sharding-core/sharding-core-optimize/src/main/java/org/apache/shardingsphere/core/optimize/segment/select/pagination/engine/LimitPaginationContextEngine.java similarity index 74% rename from sharding-core/sharding-core-optimize/src/main/java/org/apache/shardingsphere/core/optimize/segment/select/pagination/engine/LimitPaginationEngine.java rename to sharding-core/sharding-core-optimize/src/main/java/org/apache/shardingsphere/core/optimize/segment/select/pagination/engine/LimitPaginationContextEngine.java index 27f54ac052..712c9fee7f 100644 --- a/sharding-core/sharding-core-optimize/src/main/java/org/apache/shardingsphere/core/optimize/segment/select/pagination/engine/LimitPaginationEngine.java +++ b/sharding-core/sharding-core-optimize/src/main/java/org/apache/shardingsphere/core/optimize/segment/select/pagination/engine/LimitPaginationContextEngine.java @@ -17,26 +17,26 @@ package org.apache.shardingsphere.core.optimize.segment.select.pagination.engine; -import org.apache.shardingsphere.core.optimize.segment.select.pagination.Pagination; +import org.apache.shardingsphere.core.optimize.segment.select.pagination.PaginationContext; import org.apache.shardingsphere.core.parse.sql.segment.dml.pagination.limit.LimitSegment; import java.util.List; /** - * Pagination engine for limit. + * Pagination context engine for limit. * * @author zhangliang */ -public final class LimitPaginationEngine { +public final class LimitPaginationContextEngine { /** - * Create pagination. + * Create pagination context. * * @param limitSegment limit segment * @param parameters SQL parameters - * @return pagination + * @return pagination context */ - public Pagination createPagination(final LimitSegment limitSegment, final List parameters) { - return new Pagination(limitSegment.getOffset().orNull(), limitSegment.getRowCount().orNull(), parameters); + public PaginationContext createPaginationContext(final LimitSegment limitSegment, final List parameters) { + return new PaginationContext(limitSegment.getOffset().orNull(), limitSegment.getRowCount().orNull(), parameters); } } diff --git a/sharding-core/sharding-core-optimize/src/main/java/org/apache/shardingsphere/core/optimize/segment/select/pagination/engine/PaginationEngine.java b/sharding-core/sharding-core-optimize/src/main/java/org/apache/shardingsphere/core/optimize/segment/select/pagination/engine/PaginationContextEngine.java similarity index 70% rename from sharding-core/sharding-core-optimize/src/main/java/org/apache/shardingsphere/core/optimize/segment/select/pagination/engine/PaginationEngine.java rename to sharding-core/sharding-core-optimize/src/main/java/org/apache/shardingsphere/core/optimize/segment/select/pagination/engine/PaginationContextEngine.java index 39d8eb2ede..a072876264 100644 --- a/sharding-core/sharding-core-optimize/src/main/java/org/apache/shardingsphere/core/optimize/segment/select/pagination/engine/PaginationEngine.java +++ b/sharding-core/sharding-core-optimize/src/main/java/org/apache/shardingsphere/core/optimize/segment/select/pagination/engine/PaginationContextEngine.java @@ -18,8 +18,8 @@ package org.apache.shardingsphere.core.optimize.segment.select.pagination.engine; import com.google.common.base.Optional; -import org.apache.shardingsphere.core.optimize.segment.select.item.SelectItems; -import org.apache.shardingsphere.core.optimize.segment.select.pagination.Pagination; +import org.apache.shardingsphere.core.optimize.segment.select.item.SelectItemsContext; +import org.apache.shardingsphere.core.optimize.segment.select.pagination.PaginationContext; import org.apache.shardingsphere.core.parse.sql.segment.dml.pagination.limit.LimitSegment; import org.apache.shardingsphere.core.parse.sql.segment.dml.pagination.top.TopSegment; import org.apache.shardingsphere.core.parse.sql.segment.dml.predicate.AndPredicate; @@ -30,33 +30,34 @@ import java.util.Collections; import java.util.List; /** - * Pagination engine. + * Pagination context engine. * * @author zhangliang */ -public final class PaginationEngine { +public final class PaginationContextEngine { /** - * Create pagination. + * Create pagination context. * * @param selectStatement SQL statement * @param selectItems select items * @param parameters SQL parameters - * @return pagination + * @return pagination context */ - public Pagination createPagination(final SelectStatement selectStatement, final SelectItems selectItems, final List parameters) { + public PaginationContext createPaginationContext(final SelectStatement selectStatement, final SelectItemsContext selectItems, final List parameters) { Optional limitSegment = selectStatement.findSQLSegment(LimitSegment.class); if (limitSegment.isPresent()) { - return new LimitPaginationEngine().createPagination(limitSegment.get(), parameters); + return new LimitPaginationContextEngine().createPaginationContext(limitSegment.get(), parameters); } Optional topSegment = selectStatement.findSQLSegment(TopSegment.class); Optional whereSegment = selectStatement.findSQLSegment(WhereSegment.class); if (topSegment.isPresent()) { - return new TopPaginationEngine().createPagination(topSegment.get(), whereSegment.isPresent() ? whereSegment.get().getAndPredicates() : Collections.emptyList(), parameters); + return new TopPaginationContextEngine().createPaginationContext( + topSegment.get(), whereSegment.isPresent() ? whereSegment.get().getAndPredicates() : Collections.emptyList(), parameters); } if (whereSegment.isPresent()) { - return new RowNumberPaginationEngine().createPagination(whereSegment.get().getAndPredicates(), selectItems, parameters); + return new RowNumberPaginationContextEngine().createPaginationContext(whereSegment.get().getAndPredicates(), selectItems, parameters); } - return new Pagination(null, null, parameters); + return new PaginationContext(null, null, parameters); } } diff --git a/sharding-core/sharding-core-optimize/src/main/java/org/apache/shardingsphere/core/optimize/segment/select/pagination/engine/RowNumberPaginationEngine.java b/sharding-core/sharding-core-optimize/src/main/java/org/apache/shardingsphere/core/optimize/segment/select/pagination/engine/RowNumberPaginationContextEngine.java similarity index 86% rename from sharding-core/sharding-core-optimize/src/main/java/org/apache/shardingsphere/core/optimize/segment/select/pagination/engine/RowNumberPaginationEngine.java rename to sharding-core/sharding-core-optimize/src/main/java/org/apache/shardingsphere/core/optimize/segment/select/pagination/engine/RowNumberPaginationContextEngine.java index 0c33604245..453b7e820c 100644 --- a/sharding-core/sharding-core-optimize/src/main/java/org/apache/shardingsphere/core/optimize/segment/select/pagination/engine/RowNumberPaginationEngine.java +++ b/sharding-core/sharding-core-optimize/src/main/java/org/apache/shardingsphere/core/optimize/segment/select/pagination/engine/RowNumberPaginationContextEngine.java @@ -18,8 +18,8 @@ package org.apache.shardingsphere.core.optimize.segment.select.pagination.engine; import com.google.common.base.Optional; -import org.apache.shardingsphere.core.optimize.segment.select.item.SelectItems; -import org.apache.shardingsphere.core.optimize.segment.select.pagination.Pagination; +import org.apache.shardingsphere.core.optimize.segment.select.item.SelectItemsContext; +import org.apache.shardingsphere.core.optimize.segment.select.pagination.PaginationContext; import org.apache.shardingsphere.core.parse.sql.segment.dml.expr.ExpressionSegment; import org.apache.shardingsphere.core.parse.sql.segment.dml.expr.simple.LiteralExpressionSegment; import org.apache.shardingsphere.core.parse.sql.segment.dml.expr.simple.ParameterMarkerExpressionSegment; @@ -36,11 +36,11 @@ import java.util.LinkedList; import java.util.List; /** - * Pagination engine for row number. + * Pagination context engine for row number. * * @author zhangliang */ -public final class RowNumberPaginationEngine { +public final class RowNumberPaginationContextEngine { // TODO recognize database type, only oracle and sqlserver can use row number private static final Collection ROW_NUMBER_IDENTIFIERS = new HashSet<>(); @@ -51,20 +51,20 @@ public final class RowNumberPaginationEngine { } /** - * Create pagination. + * Create pagination context. * * @param andPredicates and predicates * @param selectItems select items * @param parameters SQL parameters - * @return pagination + * @return pagination context */ - public Pagination createPagination(final Collection andPredicates, final SelectItems selectItems, final List parameters) { + public PaginationContext createPaginationContext(final Collection andPredicates, final SelectItemsContext selectItems, final List parameters) { Optional rowNumberAlias = isRowNumberAlias(selectItems); if (!rowNumberAlias.isPresent()) { - return new Pagination(null, null, parameters); + return new PaginationContext(null, null, parameters); } Collection rowNumberPredicates = getRowNumberPredicates(andPredicates, rowNumberAlias.get()); - return rowNumberPredicates.isEmpty() ? new Pagination(null, null, parameters) : createPaginationWithRowNumber(rowNumberPredicates, parameters); + return rowNumberPredicates.isEmpty() ? new PaginationContext(null, null, parameters) : createPaginationWithRowNumber(rowNumberPredicates, parameters); } private Collection getRowNumberPredicates(final Collection andPredicates, final String rowNumberAlias) { @@ -79,7 +79,7 @@ public final class RowNumberPaginationEngine { return result; } - private Optional isRowNumberAlias(final SelectItems selectItems) { + private Optional isRowNumberAlias(final SelectItemsContext selectItems) { for (String each : ROW_NUMBER_IDENTIFIERS) { Optional result = selectItems.findAlias(each); if (result.isPresent()) { @@ -101,7 +101,7 @@ public final class RowNumberPaginationEngine { return false; } - private Pagination createPaginationWithRowNumber(final Collection rowNumberPredicates, final List parameters) { + private PaginationContext createPaginationWithRowNumber(final Collection rowNumberPredicates, final List parameters) { RowNumberValueSegment offset = null; RowNumberValueSegment rowCount = null; for (PredicateSegment each : rowNumberPredicates) { @@ -123,7 +123,7 @@ public final class RowNumberPaginationEngine { break; } } - return new Pagination(offset, rowCount, parameters); + return new PaginationContext(offset, rowCount, parameters); } private RowNumberValueSegment createRowNumberValueSegment(final ExpressionSegment expression, final boolean boundOpened) { diff --git a/sharding-core/sharding-core-optimize/src/main/java/org/apache/shardingsphere/core/optimize/segment/select/pagination/engine/TopPaginationEngine.java b/sharding-core/sharding-core-optimize/src/main/java/org/apache/shardingsphere/core/optimize/segment/select/pagination/engine/TopPaginationContextEngine.java similarity index 92% rename from sharding-core/sharding-core-optimize/src/main/java/org/apache/shardingsphere/core/optimize/segment/select/pagination/engine/TopPaginationEngine.java rename to sharding-core/sharding-core-optimize/src/main/java/org/apache/shardingsphere/core/optimize/segment/select/pagination/engine/TopPaginationContextEngine.java index 35f7a36a49..2654904b69 100644 --- a/sharding-core/sharding-core-optimize/src/main/java/org/apache/shardingsphere/core/optimize/segment/select/pagination/engine/TopPaginationEngine.java +++ b/sharding-core/sharding-core-optimize/src/main/java/org/apache/shardingsphere/core/optimize/segment/select/pagination/engine/TopPaginationContextEngine.java @@ -18,7 +18,7 @@ package org.apache.shardingsphere.core.optimize.segment.select.pagination.engine; import com.google.common.base.Optional; -import org.apache.shardingsphere.core.optimize.segment.select.pagination.Pagination; +import org.apache.shardingsphere.core.optimize.segment.select.pagination.PaginationContext; import org.apache.shardingsphere.core.parse.sql.segment.dml.expr.ExpressionSegment; import org.apache.shardingsphere.core.parse.sql.segment.dml.expr.simple.LiteralExpressionSegment; import org.apache.shardingsphere.core.parse.sql.segment.dml.expr.simple.ParameterMarkerExpressionSegment; @@ -35,25 +35,25 @@ import java.util.Collection; import java.util.List; /** - * Pagination engine for top. + * Pagination context engine for top. * * @author zhangliang */ -public final class TopPaginationEngine { +public final class TopPaginationContextEngine { /** - * Create pagination. + * Create pagination context. * * @param topSegment top segment * @param andPredicates and predicates * @param parameters SQL parameters - * @return pagination + * @return pagination context */ - public Pagination createPagination(final TopSegment topSegment, final Collection andPredicates, final List parameters) { + public PaginationContext createPaginationContext(final TopSegment topSegment, final Collection andPredicates, final List parameters) { Optional rowNumberPredicate = getRowNumberPredicate(andPredicates, topSegment.getRowNumberAlias()); Optional offset = rowNumberPredicate.isPresent() ? createOffsetWithRowNumber(rowNumberPredicate.get()) : Optional.absent(); PaginationValueSegment rowCount = topSegment.getTop(); - return new Pagination(offset.orNull(), rowCount, parameters); + return new PaginationContext(offset.orNull(), rowCount, parameters); } private Optional getRowNumberPredicate(final Collection andPredicates, final String rowNumberAlias) { diff --git a/sharding-core/sharding-core-optimize/src/main/java/org/apache/shardingsphere/core/optimize/segment/Table.java b/sharding-core/sharding-core-optimize/src/main/java/org/apache/shardingsphere/core/optimize/segment/table/Table.java similarity index 95% rename from sharding-core/sharding-core-optimize/src/main/java/org/apache/shardingsphere/core/optimize/segment/Table.java rename to sharding-core/sharding-core-optimize/src/main/java/org/apache/shardingsphere/core/optimize/segment/table/Table.java index c85bef535b..c2e724a376 100644 --- a/sharding-core/sharding-core-optimize/src/main/java/org/apache/shardingsphere/core/optimize/segment/Table.java +++ b/sharding-core/sharding-core-optimize/src/main/java/org/apache/shardingsphere/core/optimize/segment/table/Table.java @@ -15,7 +15,7 @@ * limitations under the License. */ -package org.apache.shardingsphere.core.optimize.segment; +package org.apache.shardingsphere.core.optimize.segment.table; import com.google.common.base.Optional; import lombok.EqualsAndHashCode; diff --git a/sharding-core/sharding-core-optimize/src/main/java/org/apache/shardingsphere/core/optimize/segment/Tables.java b/sharding-core/sharding-core-optimize/src/main/java/org/apache/shardingsphere/core/optimize/segment/table/TablesContext.java similarity index 97% rename from sharding-core/sharding-core-optimize/src/main/java/org/apache/shardingsphere/core/optimize/segment/Tables.java rename to sharding-core/sharding-core-optimize/src/main/java/org/apache/shardingsphere/core/optimize/segment/table/TablesContext.java index 007419728a..98c6990bef 100644 --- a/sharding-core/sharding-core-optimize/src/main/java/org/apache/shardingsphere/core/optimize/segment/Tables.java +++ b/sharding-core/sharding-core-optimize/src/main/java/org/apache/shardingsphere/core/optimize/segment/table/TablesContext.java @@ -15,7 +15,7 @@ * limitations under the License. */ -package org.apache.shardingsphere.core.optimize.segment; +package org.apache.shardingsphere.core.optimize.segment.table; import com.google.common.base.Optional; import com.google.common.base.Preconditions; @@ -35,18 +35,18 @@ import java.util.LinkedHashSet; import java.util.TreeSet; /** - * Tables. + * Tables context. * * @author zhangliang */ @ToString -public final class Tables { +public final class TablesContext { private final Collection
tables = new ArrayList<>(); private String schema; - public Tables(final SQLStatement sqlStatement) { + public TablesContext(final SQLStatement sqlStatement) { Collection aliases = new HashSet<>(); for (TableAvailable each : sqlStatement.findSQLSegments(TableAvailable.class)) { Optional alias = getAlias(each); diff --git a/sharding-core/sharding-core-optimize/src/main/java/org/apache/shardingsphere/core/optimize/statement/OptimizedStatement.java b/sharding-core/sharding-core-optimize/src/main/java/org/apache/shardingsphere/core/optimize/statement/SQLStatementContext.java similarity index 82% rename from sharding-core/sharding-core-optimize/src/main/java/org/apache/shardingsphere/core/optimize/statement/OptimizedStatement.java rename to sharding-core/sharding-core-optimize/src/main/java/org/apache/shardingsphere/core/optimize/statement/SQLStatementContext.java index 08ecc4d87f..28118a4851 100644 --- a/sharding-core/sharding-core-optimize/src/main/java/org/apache/shardingsphere/core/optimize/statement/OptimizedStatement.java +++ b/sharding-core/sharding-core-optimize/src/main/java/org/apache/shardingsphere/core/optimize/statement/SQLStatementContext.java @@ -17,15 +17,15 @@ package org.apache.shardingsphere.core.optimize.statement; -import org.apache.shardingsphere.core.optimize.segment.Tables; +import org.apache.shardingsphere.core.optimize.segment.table.TablesContext; import org.apache.shardingsphere.core.parse.sql.statement.SQLStatement; /** - * Optimized statement. + * SQL statement context. * * @author zhangliang */ -public interface OptimizedStatement { +public interface SQLStatementContext { /** * Get SQL statement. @@ -35,9 +35,9 @@ public interface OptimizedStatement { SQLStatement getSqlStatement(); /** - * Get tables. + * Get tables context. * - * @return tables + * @return tables context */ - Tables getTables(); + TablesContext getTablesContext(); } diff --git a/sharding-core/sharding-core-optimize/src/main/java/org/apache/shardingsphere/core/optimize/statement/impl/CommonOptimizedStatement.java b/sharding-core/sharding-core-optimize/src/main/java/org/apache/shardingsphere/core/optimize/statement/impl/CommonSQLStatementContext.java similarity index 72% rename from sharding-core/sharding-core-optimize/src/main/java/org/apache/shardingsphere/core/optimize/statement/impl/CommonOptimizedStatement.java rename to sharding-core/sharding-core-optimize/src/main/java/org/apache/shardingsphere/core/optimize/statement/impl/CommonSQLStatementContext.java index 34056ae631..a9a75eb5d2 100644 --- a/sharding-core/sharding-core-optimize/src/main/java/org/apache/shardingsphere/core/optimize/statement/impl/CommonOptimizedStatement.java +++ b/sharding-core/sharding-core-optimize/src/main/java/org/apache/shardingsphere/core/optimize/statement/impl/CommonSQLStatementContext.java @@ -19,25 +19,25 @@ package org.apache.shardingsphere.core.optimize.statement.impl; import lombok.Getter; import lombok.ToString; -import org.apache.shardingsphere.core.optimize.segment.Tables; -import org.apache.shardingsphere.core.optimize.statement.OptimizedStatement; +import org.apache.shardingsphere.core.optimize.segment.table.TablesContext; +import org.apache.shardingsphere.core.optimize.statement.SQLStatementContext; import org.apache.shardingsphere.core.parse.sql.statement.SQLStatement; /** - * Common optimized statement. + * Common SQL statement context. * * @author zhangliang */ @Getter @ToString -public class CommonOptimizedStatement implements OptimizedStatement { +public class CommonSQLStatementContext implements SQLStatementContext { private final SQLStatement sqlStatement; - private final Tables tables; + private final TablesContext tablesContext; - public CommonOptimizedStatement(final SQLStatement sqlStatement) { + public CommonSQLStatementContext(final SQLStatement sqlStatement) { this.sqlStatement = sqlStatement; - tables = new Tables(sqlStatement); + tablesContext = new TablesContext(sqlStatement); } } diff --git a/sharding-core/sharding-core-optimize/src/main/java/org/apache/shardingsphere/core/optimize/statement/impl/InsertOptimizedStatement.java b/sharding-core/sharding-core-optimize/src/main/java/org/apache/shardingsphere/core/optimize/statement/impl/InsertSQLStatementContext.java similarity index 66% rename from sharding-core/sharding-core-optimize/src/main/java/org/apache/shardingsphere/core/optimize/statement/impl/InsertOptimizedStatement.java rename to sharding-core/sharding-core-optimize/src/main/java/org/apache/shardingsphere/core/optimize/statement/impl/InsertSQLStatementContext.java index ccfd2fcb25..945cd6982f 100644 --- a/sharding-core/sharding-core-optimize/src/main/java/org/apache/shardingsphere/core/optimize/statement/impl/InsertOptimizedStatement.java +++ b/sharding-core/sharding-core-optimize/src/main/java/org/apache/shardingsphere/core/optimize/statement/impl/InsertSQLStatementContext.java @@ -20,7 +20,7 @@ package org.apache.shardingsphere.core.optimize.statement.impl; import lombok.Getter; import lombok.ToString; import org.apache.shardingsphere.core.metadata.table.TableMetas; -import org.apache.shardingsphere.core.optimize.segment.insert.InsertValue; +import org.apache.shardingsphere.core.optimize.segment.insert.InsertValueContext; import org.apache.shardingsphere.core.parse.sql.segment.dml.expr.ExpressionSegment; import org.apache.shardingsphere.core.parse.sql.statement.dml.InsertStatement; @@ -29,31 +29,31 @@ import java.util.LinkedList; import java.util.List; /** - * Insert optimized statement. + * Insert SQL statement context. * * @author zhangliang */ @Getter @ToString(callSuper = true) -public final class InsertOptimizedStatement extends CommonOptimizedStatement { +public final class InsertSQLStatementContext extends CommonSQLStatementContext { private final List columnNames; - private final List insertValues; + private final List insertValueContexts; - public InsertOptimizedStatement(final TableMetas tableMetas, final List parameters, final InsertStatement sqlStatement) { + public InsertSQLStatementContext(final TableMetas tableMetas, final List parameters, final InsertStatement sqlStatement) { super(sqlStatement); - columnNames = sqlStatement.useDefaultColumns() ? tableMetas.getAllColumnNames(getTables().getSingleTableName()) : sqlStatement.getColumnNames(); - insertValues = getInsertValues(parameters); + columnNames = sqlStatement.useDefaultColumns() ? tableMetas.getAllColumnNames(this.getTablesContext().getSingleTableName()) : sqlStatement.getColumnNames(); + insertValueContexts = getInsertValueContexts(parameters); } - private List getInsertValues(final List parameters) { - List result = new LinkedList<>(); + private List getInsertValueContexts(final List parameters) { + List result = new LinkedList<>(); int parametersOffset = 0; for (Collection each : ((InsertStatement) getSqlStatement()).getAllValueExpressions()) { - InsertValue insertValue = new InsertValue(each, parameters, parametersOffset); - result.add(insertValue); - parametersOffset += insertValue.getParametersCount(); + InsertValueContext insertValueContext = new InsertValueContext(each, parameters, parametersOffset); + result.add(insertValueContext); + parametersOffset += insertValueContext.getParametersCount(); } return result; } diff --git a/sharding-core/sharding-core-optimize/src/main/java/org/apache/shardingsphere/core/optimize/statement/impl/SelectOptimizedStatement.java b/sharding-core/sharding-core-optimize/src/main/java/org/apache/shardingsphere/core/optimize/statement/impl/SelectSQLStatementContext.java similarity index 73% rename from sharding-core/sharding-core-optimize/src/main/java/org/apache/shardingsphere/core/optimize/statement/impl/SelectOptimizedStatement.java rename to sharding-core/sharding-core-optimize/src/main/java/org/apache/shardingsphere/core/optimize/statement/impl/SelectSQLStatementContext.java index 3d74f24bad..e32bad8fab 100644 --- a/sharding-core/sharding-core-optimize/src/main/java/org/apache/shardingsphere/core/optimize/statement/impl/SelectOptimizedStatement.java +++ b/sharding-core/sharding-core-optimize/src/main/java/org/apache/shardingsphere/core/optimize/statement/impl/SelectSQLStatementContext.java @@ -22,17 +22,17 @@ import com.google.common.base.Preconditions; import lombok.Getter; import lombok.ToString; import org.apache.shardingsphere.core.metadata.table.TableMetas; -import org.apache.shardingsphere.core.optimize.segment.select.groupby.GroupBy; -import org.apache.shardingsphere.core.optimize.segment.select.groupby.engine.GroupByEngine; +import org.apache.shardingsphere.core.optimize.segment.select.groupby.GroupByContext; +import org.apache.shardingsphere.core.optimize.segment.select.groupby.engine.GroupByContextEngine; import org.apache.shardingsphere.core.optimize.segment.select.item.impl.AggregationSelectItem; import org.apache.shardingsphere.core.optimize.segment.select.item.SelectItem; -import org.apache.shardingsphere.core.optimize.segment.select.item.SelectItems; -import org.apache.shardingsphere.core.optimize.segment.select.item.engine.SelectItemsEngine; -import org.apache.shardingsphere.core.optimize.segment.select.orderby.OrderBy; +import org.apache.shardingsphere.core.optimize.segment.select.item.SelectItemsContext; +import org.apache.shardingsphere.core.optimize.segment.select.item.engine.SelectItemsContextEngine; +import org.apache.shardingsphere.core.optimize.segment.select.orderby.OrderByContext; import org.apache.shardingsphere.core.optimize.segment.select.orderby.OrderByItem; -import org.apache.shardingsphere.core.optimize.segment.select.orderby.engine.OrderByEngine; -import org.apache.shardingsphere.core.optimize.segment.select.pagination.Pagination; -import org.apache.shardingsphere.core.optimize.segment.select.pagination.engine.PaginationEngine; +import org.apache.shardingsphere.core.optimize.segment.select.orderby.engine.OrderByContextEngine; +import org.apache.shardingsphere.core.optimize.segment.select.pagination.PaginationContext; +import org.apache.shardingsphere.core.optimize.segment.select.pagination.engine.PaginationContextEngine; import org.apache.shardingsphere.core.parse.sql.segment.dml.order.item.ColumnOrderByItemSegment; import org.apache.shardingsphere.core.parse.sql.segment.dml.order.item.ExpressionOrderByItemSegment; import org.apache.shardingsphere.core.parse.sql.segment.dml.order.item.IndexOrderByItemSegment; @@ -46,40 +46,41 @@ import java.util.List; import java.util.Map; /** - * Select optimized statement. + * Select SQL statement context. * * @author zhangliang */ @Getter @ToString(callSuper = true) -public final class SelectOptimizedStatement extends CommonOptimizedStatement { +public final class SelectSQLStatementContext extends CommonSQLStatementContext { - private final GroupBy groupBy; + private final GroupByContext groupByContext; - private final OrderBy orderBy; + private final OrderByContext orderByContext; - private final SelectItems selectItems; + private final SelectItemsContext selectItemsContext; - private final Pagination pagination; + private final PaginationContext paginationContext; private final boolean containsSubquery; // TODO to be remove, for test case only - public SelectOptimizedStatement(final SelectStatement sqlStatement, final GroupBy groupBy, final OrderBy orderBy, final SelectItems selectItems, final Pagination pagination) { + public SelectSQLStatementContext(final SelectStatement sqlStatement, + final GroupByContext groupByContext, final OrderByContext orderByContext, final SelectItemsContext selectItemsContext, final PaginationContext paginationContext) { super(sqlStatement); - this.groupBy = groupBy; - this.orderBy = orderBy; - this.selectItems = selectItems; - this.pagination = pagination; + this.groupByContext = groupByContext; + this.orderByContext = orderByContext; + this.selectItemsContext = selectItemsContext; + this.paginationContext = paginationContext; containsSubquery = containsSubquery(); } - public SelectOptimizedStatement(final TableMetas tableMetas, final String sql, final List parameters, final SelectStatement sqlStatement) { + public SelectSQLStatementContext(final TableMetas tableMetas, final String sql, final List parameters, final SelectStatement sqlStatement) { super(sqlStatement); - this.groupBy = new GroupByEngine().createGroupBy(sqlStatement); - this.orderBy = new OrderByEngine().createOrderBy(sqlStatement, groupBy); - this.selectItems = new SelectItemsEngine(tableMetas).createSelectItems(sql, sqlStatement, groupBy, orderBy); - this.pagination = new PaginationEngine().createPagination(sqlStatement, selectItems, parameters); + groupByContext = new GroupByContextEngine().createGroupByContext(sqlStatement); + orderByContext = new OrderByContextEngine().createOrderBy(sqlStatement, groupByContext); + selectItemsContext = new SelectItemsContextEngine(tableMetas).createSelectItemsContext(sql, sqlStatement, groupByContext, orderByContext); + paginationContext = new PaginationContextEngine().createPaginationContext(sqlStatement, selectItemsContext, parameters); containsSubquery = containsSubquery(); } @@ -100,12 +101,12 @@ public final class SelectOptimizedStatement extends CommonOptimizedStatement { */ public void setIndexForItems(final Map columnLabelIndexMap) { setIndexForAggregationItem(columnLabelIndexMap); - setIndexForOrderItem(columnLabelIndexMap, orderBy.getItems()); - setIndexForOrderItem(columnLabelIndexMap, groupBy.getItems()); + setIndexForOrderItem(columnLabelIndexMap, orderByContext.getItems()); + setIndexForOrderItem(columnLabelIndexMap, groupByContext.getItems()); } private void setIndexForAggregationItem(final Map columnLabelIndexMap) { - for (AggregationSelectItem each : selectItems.getAggregationSelectItems()) { + for (AggregationSelectItem each : selectItemsContext.getAggregationSelectItems()) { Preconditions.checkState(columnLabelIndexMap.containsKey(each.getColumnLabel()), "Can't find index: %s, please add alias for aggregate selections", each); each.setIndex(columnLabelIndexMap.get(each.getColumnLabel())); for (AggregationSelectItem derived : each.getDerivedAggregationItems()) { @@ -122,7 +123,7 @@ public final class SelectOptimizedStatement extends CommonOptimizedStatement { continue; } if (each.getSegment() instanceof ColumnOrderByItemSegment && ((ColumnOrderByItemSegment) each.getSegment()).getColumn().getOwner().isPresent()) { - Optional itemIndex = selectItems.findItemIndex(((ColumnOrderByItemSegment) each.getSegment()).getText()); + Optional itemIndex = selectItemsContext.findItemIndex(((ColumnOrderByItemSegment) each.getSegment()).getText()); if (itemIndex.isPresent()) { each.setIndex(itemIndex.get()); continue; @@ -138,11 +139,11 @@ public final class SelectOptimizedStatement extends CommonOptimizedStatement { } private Optional getAlias(final String name) { - if (selectItems.isUnqualifiedShorthandItem()) { + if (selectItemsContext.isUnqualifiedShorthandItem()) { return Optional.absent(); } String rawName = SQLUtil.getExactlyValue(name); - for (SelectItem each : selectItems.getItems()) { + for (SelectItem each : selectItemsContext.getItems()) { if (SQLUtil.getExactlyExpression(rawName).equalsIgnoreCase(SQLUtil.getExactlyExpression(SQLUtil.getExactlyValue(each.getExpression())))) { return each.getAlias(); } @@ -165,7 +166,7 @@ public final class SelectOptimizedStatement extends CommonOptimizedStatement { * @return column labels */ public List getColumnLabels(final TableMetas tableMetas) { - return selectItems.getColumnLabels(tableMetas, ((SelectStatement) getSqlStatement()).getTables()); + return selectItemsContext.getColumnLabels(tableMetas, ((SelectStatement) getSqlStatement()).getTables()); } /** @@ -174,6 +175,6 @@ public final class SelectOptimizedStatement extends CommonOptimizedStatement { * @return group by and order by sequence is same or not */ public boolean isSameGroupByAndOrderByItems() { - return !groupBy.getItems().isEmpty() && groupBy.getItems().equals(orderBy.getItems()); + return !groupByContext.getItems().isEmpty() && groupByContext.getItems().equals(orderByContext.getItems()); } } diff --git a/sharding-core/sharding-core-optimize/src/test/java/org/apache/shardingsphere/core/optimize/segment/select/item/SelectItemsTest.java b/sharding-core/sharding-core-optimize/src/test/java/org/apache/shardingsphere/core/optimize/segment/select/item/SelectItemsContextTest.java similarity index 66% rename from sharding-core/sharding-core-optimize/src/test/java/org/apache/shardingsphere/core/optimize/segment/select/item/SelectItemsTest.java rename to sharding-core/sharding-core-optimize/src/test/java/org/apache/shardingsphere/core/optimize/segment/select/item/SelectItemsContextTest.java index 1d0848d980..f7c9912c10 100644 --- a/sharding-core/sharding-core-optimize/src/test/java/org/apache/shardingsphere/core/optimize/segment/select/item/SelectItemsTest.java +++ b/sharding-core/sharding-core-optimize/src/test/java/org/apache/shardingsphere/core/optimize/segment/select/item/SelectItemsContextTest.java @@ -21,8 +21,6 @@ import com.google.common.base.Optional; import org.apache.shardingsphere.core.metadata.column.ColumnMetaData; import org.apache.shardingsphere.core.metadata.table.TableMetaData; import org.apache.shardingsphere.core.metadata.table.TableMetas; -import org.apache.shardingsphere.core.optimize.segment.select.item.SelectItem; -import org.apache.shardingsphere.core.optimize.segment.select.item.SelectItems; import org.apache.shardingsphere.core.optimize.segment.select.item.impl.AggregationDistinctSelectItem; import org.apache.shardingsphere.core.optimize.segment.select.item.impl.AggregationSelectItem; import org.apache.shardingsphere.core.optimize.segment.select.item.impl.ColumnSelectItem; @@ -45,51 +43,51 @@ import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertThat; import static org.junit.Assert.assertTrue; -public final class SelectItemsTest { +public final class SelectItemsContextTest { @Test public void assertUnqualifiedShorthandItemWithEmptyItems() { - SelectItems selectItems = new SelectItems(0, 0, true, Collections.emptySet()); - assertFalse(selectItems.isUnqualifiedShorthandItem()); + SelectItemsContext selectItemsContext = new SelectItemsContext(0, 0, true, Collections.emptySet()); + assertFalse(selectItemsContext.isUnqualifiedShorthandItem()); } @Test public void assertUnqualifiedShorthandItemWithWrongSelectItem() { - SelectItems selectItems = new SelectItems(0, 0, true, Collections.singleton((SelectItem) getColumnSelectItem())); - assertFalse(selectItems.isUnqualifiedShorthandItem()); + SelectItemsContext selectItemsContext = new SelectItemsContext(0, 0, true, Collections.singleton((SelectItem) getColumnSelectItem())); + assertFalse(selectItemsContext.isUnqualifiedShorthandItem()); } @Test public void assertUnqualifiedShorthandItemWithWrongShortSelectItem() { - SelectItems selectItems = new SelectItems(0, 0, true, Collections.singleton((SelectItem) getShorthandSelectItem())); - assertFalse(selectItems.isUnqualifiedShorthandItem()); + SelectItemsContext selectItemsContext = new SelectItemsContext(0, 0, true, Collections.singleton((SelectItem) getShorthandSelectItem())); + assertFalse(selectItemsContext.isUnqualifiedShorthandItem()); } @Test public void assertUnqualifiedShorthandItem() { SelectItem selectItem = new ShorthandSelectItem(null); - SelectItems selectItems = new SelectItems(0, 0, true, Collections.singleton(selectItem)); - assertTrue(selectItems.isUnqualifiedShorthandItem()); + SelectItemsContext selectItemsContext = new SelectItemsContext(0, 0, true, Collections.singleton(selectItem)); + assertTrue(selectItemsContext.isUnqualifiedShorthandItem()); } @Test public void assertFindAliasWithOutAlias() { - SelectItems selectItems = new SelectItems(0, 0, true, Collections.emptyList()); - assertFalse(selectItems.findAlias("").isPresent()); + SelectItemsContext selectItemsContext = new SelectItemsContext(0, 0, true, Collections.emptyList()); + assertFalse(selectItemsContext.findAlias("").isPresent()); } @Test public void assertFindAlias() { SelectItem selectItem = getColumnSelectItemWithAlias(); - SelectItems selectItems = new SelectItems(0, 0, true, Collections.singleton(selectItem)); - assertTrue(selectItems.findAlias(selectItem.getExpression()).isPresent()); + SelectItemsContext selectItemsContext = new SelectItemsContext(0, 0, true, Collections.singleton(selectItem)); + assertTrue(selectItemsContext.findAlias(selectItem.getExpression()).isPresent()); } @Test public void assertFindItemIndex() { SelectItem selectItem = getColumnSelectItem(); - SelectItems selectItems = new SelectItems(0, 0, true, Collections.singleton(selectItem)); - Optional actual = selectItems.findItemIndex(selectItem.getExpression()); + SelectItemsContext selectItemsContext = new SelectItemsContext(0, 0, true, Collections.singleton(selectItem)); + Optional actual = selectItemsContext.findItemIndex(selectItem.getExpression()); assertTrue(actual.isPresent()); assertThat(actual.get(), is(1)); } @@ -97,16 +95,15 @@ public final class SelectItemsTest { @Test public void assertFindItemIndexFailure() { SelectItem selectItem = getColumnSelectItem(); - SelectItems selectItems = new SelectItems(0, 0, true, Collections.singleton(selectItem)); - Optional actual = selectItems.findItemIndex(""); + SelectItemsContext selectItemsContext = new SelectItemsContext(0, 0, true, Collections.singleton(selectItem)); + Optional actual = selectItemsContext.findItemIndex(""); assertFalse(actual.isPresent()); } @Test public void assertGetAggregationSelectItems() { SelectItem aggregationSelectItem = getAggregationSelectItem(); - List items = new SelectItems(0, 0, true, - Arrays.asList(aggregationSelectItem, getColumnSelectItem())).getAggregationSelectItems(); + List items = new SelectItemsContext(0, 0, true, Arrays.asList(aggregationSelectItem, getColumnSelectItem())).getAggregationSelectItems(); assertTrue(items.contains(aggregationSelectItem)); assertEquals(items.size(), 1); } @@ -114,7 +111,7 @@ public final class SelectItemsTest { @Test public void assertGetAggregationDistinctSelectItems() { SelectItem aggregationDistinctSelectItem = getAggregationDistinctSelectItem(); - List items = new SelectItems(0, 0, true, Arrays.asList(aggregationDistinctSelectItem, getColumnSelectItem())).getAggregationDistinctSelectItems(); + List items = new SelectItemsContext(0, 0, true, Arrays.asList(aggregationDistinctSelectItem, getColumnSelectItem())).getAggregationDistinctSelectItems(); assertTrue(items.contains(aggregationDistinctSelectItem)); assertEquals(items.size(), 1); } @@ -122,70 +119,72 @@ public final class SelectItemsTest { @Test public void assertGetColumnLabelWithShorthandSelectItem() { SelectItem selectItem = getShorthandSelectItem(); - List columnLabels = new SelectItems(0, 0, true, Collections.singletonList(selectItem)).getColumnLabels(createTableMetas(), Collections.singletonList(new TableSegment(0, 0, "table"))); + List columnLabels = new SelectItemsContext( + 0, 0, true, Collections.singletonList(selectItem)).getColumnLabels(createTableMetas(), Collections.singletonList(new TableSegment(0, 0, "table"))); assertEquals(columnLabels, Arrays.asList("id", "name")); } @Test public void assertGetColumnLabelWithShorthandSelectItem2() { SelectItem selectItem = getShorthandSelectItemWithOutOwner(); - List columnLabels = new SelectItems(0, 0, true, Collections.singletonList(selectItem)).getColumnLabels(createTableMetas(), Collections.singletonList(new TableSegment(0, 0, "table"))); + List columnLabels = new SelectItemsContext( + 0, 0, true, Collections.singletonList(selectItem)).getColumnLabels(createTableMetas(), Collections.singletonList(new TableSegment(0, 0, "table"))); assertEquals(columnLabels, Arrays.asList("id", "name")); } @Test public void assertGetColumnLabelsWithCommonSelectItem() { SelectItem selectItem = getColumnSelectItem(); - List columnLabels = new SelectItems(0, 0, true, Collections.singletonList(selectItem)).getColumnLabels(createTableMetas(), Collections.emptyList()); + List columnLabels = new SelectItemsContext(0, 0, true, Collections.singletonList(selectItem)).getColumnLabels(createTableMetas(), Collections.emptyList()); assertTrue(columnLabels.contains(selectItem.getColumnLabel())); } @Test public void assertGetColumnLabelsWithCommonSelectItemAlias() { SelectItem selectItem = getColumnSelectItemWithAlias(); - List columnLabels = new SelectItems(0, 0, true, Collections.singletonList(selectItem)).getColumnLabels(createTableMetas(), Collections.emptyList()); + List columnLabels = new SelectItemsContext(0, 0, true, Collections.singletonList(selectItem)).getColumnLabels(createTableMetas(), Collections.emptyList()); assertTrue(columnLabels.contains(selectItem.getAlias().or(""))); } @Test public void assertGetColumnLabelsWithExpressionSelectItem() { SelectItem selectItem = getExpressionSelectItem(); - List columnLabels = new SelectItems(0, 0, true, Collections.singletonList(selectItem)).getColumnLabels(createTableMetas(), Collections.emptyList()); + List columnLabels = new SelectItemsContext(0, 0, true, Collections.singletonList(selectItem)).getColumnLabels(createTableMetas(), Collections.emptyList()); assertTrue(columnLabels.contains(selectItem.getColumnLabel())); } @Test public void assertGetColumnLabelsWithExpressionSelectItemAlias() { SelectItem selectItem = getExpressionSelectItemWithAlias(); - List columnLabels = new SelectItems(0, 0, true, Collections.singletonList(selectItem)).getColumnLabels(createTableMetas(), Collections.emptyList()); + List columnLabels = new SelectItemsContext(0, 0, true, Collections.singletonList(selectItem)).getColumnLabels(createTableMetas(), Collections.emptyList()); assertTrue(columnLabels.contains(selectItem.getAlias().or(""))); } @Test public void assertGetColumnLabelsWithDerivedSelectItem() { SelectItem selectItem = getDerivedSelectItem(); - List columnLabels = new SelectItems(0, 0, true, Collections.singletonList(selectItem)).getColumnLabels(createTableMetas(), Collections.emptyList()); + List columnLabels = new SelectItemsContext(0, 0, true, Collections.singletonList(selectItem)).getColumnLabels(createTableMetas(), Collections.emptyList()); assertTrue(columnLabels.contains(selectItem.getColumnLabel())); } @Test public void assertGetColumnLabelsWithDerivedSelectItemAlias() { SelectItem selectItem = getDerivedSelectItemWithAlias(); - List columnLabels = new SelectItems(0, 0, true, Collections.singletonList(selectItem)).getColumnLabels(createTableMetas(), Collections.emptyList()); + List columnLabels = new SelectItemsContext(0, 0, true, Collections.singletonList(selectItem)).getColumnLabels(createTableMetas(), Collections.emptyList()); assertTrue(columnLabels.contains(selectItem.getAlias().or(""))); } @Test public void assertGetColumnLabelsWithAggregationSelectItem() { SelectItem selectItem = getAggregationSelectItem(); - List columnLabels = new SelectItems(0, 0, true, Collections.singletonList(selectItem)).getColumnLabels(createTableMetas(), Collections.emptyList()); + List columnLabels = new SelectItemsContext(0, 0, true, Collections.singletonList(selectItem)).getColumnLabels(createTableMetas(), Collections.emptyList()); assertTrue(columnLabels.contains(selectItem.getColumnLabel())); } @Test public void assertGetColumnLabelsWithAggregationDistinctSelectItem() { SelectItem selectItem = getAggregationDistinctSelectItem(); - List columnLabels = new SelectItems(0, 0, true, Collections.singletonList(selectItem)).getColumnLabels(createTableMetas(), Collections.emptyList()); + List columnLabels = new SelectItemsContext(0, 0, true, Collections.singletonList(selectItem)).getColumnLabels(createTableMetas(), Collections.emptyList()); assertTrue(columnLabels.contains(selectItem.getColumnLabel())); } diff --git a/sharding-core/sharding-core-optimize/src/test/java/org/apache/shardingsphere/core/optimize/segment/select/pagination/PaginationTest.java b/sharding-core/sharding-core-optimize/src/test/java/org/apache/shardingsphere/core/optimize/segment/select/pagination/PaginationContextTest.java similarity index 50% rename from sharding-core/sharding-core-optimize/src/test/java/org/apache/shardingsphere/core/optimize/segment/select/pagination/PaginationTest.java rename to sharding-core/sharding-core-optimize/src/test/java/org/apache/shardingsphere/core/optimize/segment/select/pagination/PaginationContextTest.java index 5f372a5d7b..d3624d5ef7 100644 --- a/sharding-core/sharding-core-optimize/src/test/java/org/apache/shardingsphere/core/optimize/segment/select/pagination/PaginationTest.java +++ b/sharding-core/sharding-core-optimize/src/test/java/org/apache/shardingsphere/core/optimize/segment/select/pagination/PaginationContextTest.java @@ -18,17 +18,14 @@ package org.apache.shardingsphere.core.optimize.segment.select.pagination; import com.google.common.collect.Lists; -import org.apache.shardingsphere.core.optimize.segment.select.groupby.GroupBy; -import org.apache.shardingsphere.core.optimize.segment.select.item.impl.AggregationSelectItem; -import org.apache.shardingsphere.core.optimize.segment.select.item.SelectItems; +import org.apache.shardingsphere.core.optimize.segment.select.groupby.GroupByContext; +import org.apache.shardingsphere.core.optimize.segment.select.item.SelectItemsContext; import org.apache.shardingsphere.core.optimize.segment.select.orderby.OrderByItem; -import org.apache.shardingsphere.core.optimize.segment.select.pagination.Pagination; -import org.apache.shardingsphere.core.optimize.statement.impl.SelectOptimizedStatement; +import org.apache.shardingsphere.core.optimize.statement.impl.SelectSQLStatementContext; import org.apache.shardingsphere.core.parse.sql.segment.dml.pagination.PaginationValueSegment; import org.apache.shardingsphere.core.parse.sql.segment.dml.pagination.limit.NumberLiteralLimitValueSegment; import org.apache.shardingsphere.core.parse.sql.segment.dml.pagination.limit.ParameterMarkerLimitValueSegment; import org.junit.Test; -import org.mockito.Mockito; import java.util.Collections; import java.util.List; @@ -37,59 +34,59 @@ import static org.hamcrest.CoreMatchers.is; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertThat; import static org.junit.Assert.assertTrue; -import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; -public class PaginationTest { +public final class PaginationContextTest { @Test public void assertSegmentWithNullOffsetSegment() { PaginationValueSegment rowCountSegment = getRowCountSegment(); - Pagination pagination = new Pagination(null, rowCountSegment, getParameters()); - assertTrue(pagination.isHasPagination()); - assertNull(pagination.getOffsetSegment().orNull()); - assertThat(pagination.getRowCountSegment().orNull(), is(rowCountSegment)); + PaginationContext paginationContext = new PaginationContext(null, rowCountSegment, getParameters()); + assertTrue(paginationContext.isHasPagination()); + assertNull(paginationContext.getOffsetSegment().orNull()); + assertThat(paginationContext.getRowCountSegment().orNull(), is(rowCountSegment)); } @Test public void assertGetSegmentWithRowCountSegment() { PaginationValueSegment offsetSegment = getOffsetSegment(); - Pagination pagination = new Pagination(offsetSegment, null, getParameters()); - assertTrue(pagination.isHasPagination()); - assertThat(pagination.getOffsetSegment().orNull(), is(offsetSegment)); - assertNull(pagination.getRowCountSegment().orNull()); + PaginationContext paginationContext = new PaginationContext(offsetSegment, null, getParameters()); + assertTrue(paginationContext.isHasPagination()); + assertThat(paginationContext.getOffsetSegment().orNull(), is(offsetSegment)); + assertNull(paginationContext.getRowCountSegment().orNull()); } @Test public void assertGetActualOffset() { - assertThat(new Pagination(getOffsetSegment(), getRowCountSegment(), getParameters()).getActualOffset(), is(30L)); + assertThat(new PaginationContext(getOffsetSegment(), getRowCountSegment(), getParameters()).getActualOffset(), is(30L)); } @Test public void assertGetActualOffsetWithNumberLiteralPaginationValueSegment() { - assertThat(new Pagination(getOffsetSegmentWithNumberLiteralPaginationValueSegment(), getRowCountSegmentWithNumberLiteralPaginationValueSegment(), getParameters()).getActualOffset(), is(30L)); + assertThat(new PaginationContext(getOffsetSegmentWithNumberLiteralPaginationValueSegment(), + getRowCountSegmentWithNumberLiteralPaginationValueSegment(), getParameters()).getActualOffset(), is(30L)); } @Test public void assertGetActualOffsetWithNullOffsetSegment() { - assertThat(new Pagination(null, getRowCountSegment(), getParameters()).getActualOffset(), is(0L)); + assertThat(new PaginationContext(null, getRowCountSegment(), getParameters()).getActualOffset(), is(0L)); } @Test public void assertGetActualRowCount() { - assertThat(new Pagination(getOffsetSegment(), getRowCountSegment(), getParameters()).getActualRowCount().orNull(), is(20L)); + assertThat(new PaginationContext(getOffsetSegment(), getRowCountSegment(), getParameters()).getActualRowCount().orNull(), is(20L)); } @Test public void assertGetActualRowCountWithNumberLiteralPaginationValueSegment() { - assertThat(new Pagination(getOffsetSegmentWithNumberLiteralPaginationValueSegment(), + assertThat(new PaginationContext(getOffsetSegmentWithNumberLiteralPaginationValueSegment(), getRowCountSegmentWithNumberLiteralPaginationValueSegment(), getParameters()).getActualRowCount().orNull(), is(20L)); } @Test public void assertGetActualRowCountWithNullRowCountSegment() { - assertNull(new Pagination(getOffsetSegment(), null, getParameters()).getActualRowCount().orNull()); + assertNull(new PaginationContext(getOffsetSegment(), null, getParameters()).getActualRowCount().orNull()); } private PaginationValueSegment getOffsetSegmentWithNumberLiteralPaginationValueSegment() { @@ -102,12 +99,12 @@ public class PaginationTest { @Test public void assertGetOffsetParameterIndex() { - assertThat(new Pagination(getOffsetSegment(), getRowCountSegment(), getParameters()).getOffsetParameterIndex().orNull(), is(0)); + assertThat(new PaginationContext(getOffsetSegment(), getRowCountSegment(), getParameters()).getOffsetParameterIndex().orNull(), is(0)); } @Test public void assertGetRowCountParameterIndex() { - assertThat(new Pagination(getOffsetSegment(), getRowCountSegment(), getParameters()).getRowCountParameterIndex().orNull(), is(1)); + assertThat(new PaginationContext(getOffsetSegment(), getRowCountSegment(), getParameters()).getRowCountParameterIndex().orNull(), is(1)); } private PaginationValueSegment getOffsetSegment() { @@ -124,43 +121,22 @@ public class PaginationTest { @Test public void assertGetRevisedOffset() { - assertThat(new Pagination(getOffsetSegment(), getRowCountSegment(), getParameters()).getRevisedOffset(), is(0L)); + assertThat(new PaginationContext(getOffsetSegment(), getRowCountSegment(), getParameters()).getRevisedOffset(), is(0L)); } @Test public void getRevisedRowCount() { - SelectOptimizedStatement shardingStatement = mock(SelectOptimizedStatement.class); - Mockito.doReturn(getSelectItemsWithEmptyAggregationSelectItems()).when(shardingStatement).getSelectItems(); - doReturn(getGroupByWithEmptyItems()).when(shardingStatement).getGroupBy(); - assertThat(new Pagination(getOffsetSegment(), getRowCountSegment(), getParameters()).getRevisedRowCount(shardingStatement), is(50L)); - } - - private GroupBy getGroupByWithEmptyItems() { - GroupBy groupBy = mock(GroupBy.class); - when(groupBy.getItems()).thenReturn(Collections.emptyList()); - return groupBy; - } - - private SelectItems getSelectItemsWithEmptyAggregationSelectItems() { - SelectItems selectItems = mock(SelectItems.class); - when(selectItems.getAggregationSelectItems()).thenReturn(Collections.emptyList()); - return selectItems; + SelectSQLStatementContext selectSQLStatementContext = mock(SelectSQLStatementContext.class); + when(selectSQLStatementContext.getSelectItemsContext()).thenReturn(mock(SelectItemsContext.class)); + when(selectSQLStatementContext.getGroupByContext()).thenReturn(new GroupByContext(Collections.emptyList(), 0)); + assertThat(new PaginationContext(getOffsetSegment(), getRowCountSegment(), getParameters()).getRevisedRowCount(selectSQLStatementContext), is(50L)); } @Test public void getRevisedRowCountWithMax() { - SelectOptimizedStatement shardingStatement = mock(SelectOptimizedStatement.class); - doReturn(getSelectItemsWithEmptyAggregationSelectItems()).when(shardingStatement).getSelectItems(); - doReturn(getGroupBy()).when(shardingStatement).getGroupBy(); - doReturn(false).when(shardingStatement).isSameGroupByAndOrderByItems(); - assertThat(new Pagination(getOffsetSegment(), getRowCountSegment(), getParameters()).getRevisedRowCount(shardingStatement), is((long) Integer.MAX_VALUE)); - } - - private GroupBy getGroupBy() { - GroupBy groupBy = mock(GroupBy.class); - List items = mock(List.class); - when(items.isEmpty()).thenReturn(false); - when(groupBy.getItems()).thenReturn(items); - return groupBy; + SelectSQLStatementContext selectSQLStatementContext = mock(SelectSQLStatementContext.class); + when(selectSQLStatementContext.getSelectItemsContext()).thenReturn(mock(SelectItemsContext.class)); + when(selectSQLStatementContext.getGroupByContext()).thenReturn(new GroupByContext(Collections.singletonList(mock(OrderByItem.class)), 1)); + assertThat(new PaginationContext(getOffsetSegment(), getRowCountSegment(), getParameters()).getRevisedRowCount(selectSQLStatementContext), is((long) Integer.MAX_VALUE)); } } diff --git a/sharding-core/sharding-core-optimize/src/test/java/org/apache/shardingsphere/core/optimize/segment/TablesTest.java b/sharding-core/sharding-core-optimize/src/test/java/org/apache/shardingsphere/core/optimize/segment/table/TablesContextTest.java similarity index 74% rename from sharding-core/sharding-core-optimize/src/test/java/org/apache/shardingsphere/core/optimize/segment/TablesTest.java rename to sharding-core/sharding-core-optimize/src/test/java/org/apache/shardingsphere/core/optimize/segment/table/TablesContextTest.java index 5d9f436d96..b1fff3d3de 100644 --- a/sharding-core/sharding-core-optimize/src/test/java/org/apache/shardingsphere/core/optimize/segment/TablesTest.java +++ b/sharding-core/sharding-core-optimize/src/test/java/org/apache/shardingsphere/core/optimize/segment/table/TablesContextTest.java @@ -15,7 +15,7 @@ * limitations under the License. */ -package org.apache.shardingsphere.core.optimize.segment; +package org.apache.shardingsphere.core.optimize.segment.table; import com.google.common.base.Optional; import com.google.common.collect.Sets; @@ -31,28 +31,28 @@ import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertThat; import static org.junit.Assert.assertTrue; -public final class TablesTest { +public final class TablesContextTest { @Test public void assertIsEmpty() { - Tables tables = new Tables(new SelectStatement()); - assertTrue(tables.isEmpty()); + TablesContext tablesContext = new TablesContext(new SelectStatement()); + assertTrue(tablesContext.isEmpty()); } @Test public void assertIsNotEmpty() { SelectStatement selectStatement = new SelectStatement(); selectStatement.getAllSQLSegments().add(createTableSegment("table", "tbl")); - Tables tables = new Tables(selectStatement); - assertFalse(tables.isEmpty()); + TablesContext tablesContext = new TablesContext(selectStatement); + assertFalse(tablesContext.isEmpty()); } @Test public void assertIsSingleTable() { SelectStatement selectStatement = new SelectStatement(); selectStatement.getAllSQLSegments().add(createTableSegment("table", "tbl")); - Tables tables = new Tables(selectStatement); - assertTrue(tables.isSingleTable()); + TablesContext tablesContext = new TablesContext(selectStatement); + assertTrue(tablesContext.isSingleTable()); } @Test @@ -60,8 +60,8 @@ public final class TablesTest { SelectStatement selectStatement = new SelectStatement(); selectStatement.getAllSQLSegments().add(createTableSegment("table", "tbl")); selectStatement.getAllSQLSegments().add(createTableSegment("Table", null)); - Tables tables = new Tables(selectStatement); - assertTrue(tables.isSingleTable()); + TablesContext tablesContext = new TablesContext(selectStatement); + assertTrue(tablesContext.isSingleTable()); } @Test @@ -69,8 +69,8 @@ public final class TablesTest { SelectStatement selectStatement = new SelectStatement(); selectStatement.getAllSQLSegments().add(createTableSegment("table", "tbl")); selectStatement.getAllSQLSegments().add(createTableSegment("tbl", null)); - Tables tables = new Tables(selectStatement); - assertTrue(tables.isSingleTable()); + TablesContext tablesContext = new TablesContext(selectStatement); + assertTrue(tablesContext.isSingleTable()); } @Test @@ -78,16 +78,16 @@ public final class TablesTest { SelectStatement selectStatement = new SelectStatement(); selectStatement.getAllSQLSegments().add(createTableSegment("table_1", "tbl_1")); selectStatement.getAllSQLSegments().add(createTableSegment("table_2", "tbl_2")); - Tables tables = new Tables(selectStatement); - assertFalse(tables.isSingleTable()); + TablesContext tablesContext = new TablesContext(selectStatement); + assertFalse(tablesContext.isSingleTable()); } @Test public void assertGetSingleTableName() { SelectStatement selectStatement = new SelectStatement(); selectStatement.getAllSQLSegments().add(createTableSegment("table", "tbl")); - Tables tables = new Tables(selectStatement); - assertThat(tables.getSingleTableName(), is("table")); + TablesContext tablesContext = new TablesContext(selectStatement); + assertThat(tablesContext.getSingleTableName(), is("table")); } @Test @@ -95,8 +95,8 @@ public final class TablesTest { SelectStatement selectStatement = new SelectStatement(); selectStatement.getAllSQLSegments().add(createTableSegment("table_1", "tbl_1")); selectStatement.getAllSQLSegments().add(createTableSegment("table_2", "tbl_2")); - Tables tables = new Tables(selectStatement); - assertThat(tables.getTableNames(), CoreMatchers.>is(Sets.newHashSet("table_1", "table_2"))); + TablesContext tablesContext = new TablesContext(selectStatement); + assertThat(tablesContext.getTableNames(), CoreMatchers.>is(Sets.newHashSet("table_1", "table_2"))); } @Test @@ -104,8 +104,8 @@ public final class TablesTest { SelectStatement selectStatement = new SelectStatement(); selectStatement.getAllSQLSegments().add(createTableSegment("table_1", "tbl_1")); selectStatement.getAllSQLSegments().add(createTableSegment("table_2", "tbl_2")); - Tables tables = new Tables(selectStatement); - Optional
table = tables.find("table_1"); + TablesContext tablesContext = new TablesContext(selectStatement); + Optional
table = tablesContext.find("table_1"); assertTrue(table.isPresent()); assertThat(table.get().getName(), is("table_1")); assertThat(table.get().getAlias().orNull(), is("tbl_1")); @@ -116,8 +116,8 @@ public final class TablesTest { SelectStatement selectStatement = new SelectStatement(); selectStatement.getAllSQLSegments().add(createTableSegment("table_1", "tbl_1")); selectStatement.getAllSQLSegments().add(createTableSegment("table_2", "tbl_2")); - Tables tables = new Tables(selectStatement); - Optional
table = tables.find("tbl_1"); + TablesContext tablesContext = new TablesContext(selectStatement); + Optional
table = tablesContext.find("tbl_1"); assertTrue(table.isPresent()); assertThat(table.get().getName(), is("table_1")); assertThat(table.get().getAlias().orNull(), is("tbl_1")); @@ -128,8 +128,8 @@ public final class TablesTest { SelectStatement selectStatement = new SelectStatement(); selectStatement.getAllSQLSegments().add(createTableSegment("table_1", "tbl_1")); selectStatement.getAllSQLSegments().add(createTableSegment("table_2", "tbl_2")); - Tables tables = new Tables(selectStatement); - Optional
table = tables.find("table_3"); + TablesContext tablesContext = new TablesContext(selectStatement); + Optional
table = tablesContext.find("table_3"); assertFalse(table.isPresent()); } diff --git a/sharding-core/sharding-core-optimize/src/test/java/org/apache/shardingsphere/core/optimize/statement/impl/InsertOptimizeStatementTest.java b/sharding-core/sharding-core-optimize/src/test/java/org/apache/shardingsphere/core/optimize/statement/impl/InsertSQLStatementContextTest.java similarity index 69% rename from sharding-core/sharding-core-optimize/src/test/java/org/apache/shardingsphere/core/optimize/statement/impl/InsertOptimizeStatementTest.java rename to sharding-core/sharding-core-optimize/src/test/java/org/apache/shardingsphere/core/optimize/statement/impl/InsertSQLStatementContextTest.java index 8d8bcdc304..50bf74a183 100644 --- a/sharding-core/sharding-core-optimize/src/test/java/org/apache/shardingsphere/core/optimize/statement/impl/InsertOptimizeStatementTest.java +++ b/sharding-core/sharding-core-optimize/src/test/java/org/apache/shardingsphere/core/optimize/statement/impl/InsertSQLStatementContextTest.java @@ -18,7 +18,6 @@ package org.apache.shardingsphere.core.optimize.statement.impl; import org.apache.shardingsphere.core.metadata.table.TableMetas; -import org.apache.shardingsphere.core.optimize.statement.impl.InsertOptimizedStatement; import org.apache.shardingsphere.core.parse.sql.segment.dml.assignment.InsertValuesSegment; import org.apache.shardingsphere.core.parse.sql.segment.dml.column.ColumnSegment; import org.apache.shardingsphere.core.parse.sql.segment.dml.expr.ExpressionSegment; @@ -35,27 +34,27 @@ import static org.junit.Assert.assertThat; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; -public final class InsertOptimizeStatementTest { +public final class InsertSQLStatementContextTest { @Test - public void assertInsertOptimizedStatementWithColumnNames() { + public void assertInsertSQLStatementContextWithColumnNames() { InsertStatement insertStatement = new InsertStatement(); insertStatement.getAllSQLSegments().add(new TableSegment(0, 0, "tbl")); insertStatement.getColumns().addAll(Arrays.asList(new ColumnSegment(0, 0, "id"), new ColumnSegment(0, 0, "name"), new ColumnSegment(0, 0, "status"))); setUpInsertValues(insertStatement); - InsertOptimizedStatement actual = new InsertOptimizedStatement(mock(TableMetas.class), Arrays.asList(1, "Tom", 2, "Jerry"), insertStatement); - assertInsertOptimizeStatement(actual); + InsertSQLStatementContext actual = new InsertSQLStatementContext(mock(TableMetas.class), Arrays.asList(1, "Tom", 2, "Jerry"), insertStatement); + assertInsertSQLStatementContext(actual); } @Test - public void assertInsertOptimizedStatementWithoutColumnNames() { + public void assertInsertSQLStatementContextWithoutColumnNames() { TableMetas tableMetas = mock(TableMetas.class); when(tableMetas.getAllColumnNames("tbl")).thenReturn(Arrays.asList("id", "name", "status")); InsertStatement insertStatement = new InsertStatement(); insertStatement.getAllSQLSegments().add(new TableSegment(0, 0, "tbl")); setUpInsertValues(insertStatement); - InsertOptimizedStatement actual = new InsertOptimizedStatement(tableMetas, Arrays.asList(1, "Tom", 2, "Jerry"), insertStatement); - assertInsertOptimizeStatement(actual); + InsertSQLStatementContext actual = new InsertSQLStatementContext(tableMetas, Arrays.asList(1, "Tom", 2, "Jerry"), insertStatement); + assertInsertSQLStatementContext(actual); } private void setUpInsertValues(final InsertStatement insertStatement) { @@ -65,15 +64,15 @@ public final class InsertOptimizeStatementTest { new ParameterMarkerExpressionSegment(0, 0, 3), new ParameterMarkerExpressionSegment(0, 0, 4), new LiteralExpressionSegment(0, 0, "init")))); } - private void assertInsertOptimizeStatement(final InsertOptimizedStatement actual) { - assertThat(actual.getTables().getSingleTableName(), is("tbl")); + private void assertInsertSQLStatementContext(final InsertSQLStatementContext actual) { + assertThat(actual.getTablesContext().getSingleTableName(), is("tbl")); assertThat(actual.getColumnNames(), is(Arrays.asList("id", "name", "status"))); - assertThat(actual.getInsertValues().size(), is(2)); - assertThat(actual.getInsertValues().get(0).getValue(0), is((Object) 1)); - assertThat(actual.getInsertValues().get(0).getValue(1), is((Object) "Tom")); - assertThat(actual.getInsertValues().get(0).getValue(2), is((Object) "init")); - assertThat(actual.getInsertValues().get(1).getValue(0), is((Object) 2)); - assertThat(actual.getInsertValues().get(1).getValue(1), is((Object) "Jerry")); - assertThat(actual.getInsertValues().get(1).getValue(2), is((Object) "init")); + assertThat(actual.getInsertValueContexts().size(), is(2)); + assertThat(actual.getInsertValueContexts().get(0).getValue(0), is((Object) 1)); + assertThat(actual.getInsertValueContexts().get(0).getValue(1), is((Object) "Tom")); + assertThat(actual.getInsertValueContexts().get(0).getValue(2), is((Object) "init")); + assertThat(actual.getInsertValueContexts().get(1).getValue(0), is((Object) 2)); + assertThat(actual.getInsertValueContexts().get(1).getValue(1), is((Object) "Jerry")); + assertThat(actual.getInsertValueContexts().get(1).getValue(2), is((Object) "init")); } } diff --git a/sharding-core/sharding-core-optimize/src/test/java/org/apache/shardingsphere/core/optimize/statement/impl/SelectOptimizedStatementTest.java b/sharding-core/sharding-core-optimize/src/test/java/org/apache/shardingsphere/core/optimize/statement/impl/SelectSQLStatementContextTest.java similarity index 69% rename from sharding-core/sharding-core-optimize/src/test/java/org/apache/shardingsphere/core/optimize/statement/impl/SelectOptimizedStatementTest.java rename to sharding-core/sharding-core-optimize/src/test/java/org/apache/shardingsphere/core/optimize/statement/impl/SelectSQLStatementContextTest.java index 2417573c97..a1788b03da 100644 --- a/sharding-core/sharding-core-optimize/src/test/java/org/apache/shardingsphere/core/optimize/statement/impl/SelectOptimizedStatementTest.java +++ b/sharding-core/sharding-core-optimize/src/test/java/org/apache/shardingsphere/core/optimize/statement/impl/SelectSQLStatementContextTest.java @@ -18,11 +18,11 @@ package org.apache.shardingsphere.core.optimize.statement.impl; import com.google.common.collect.Lists; -import org.apache.shardingsphere.core.optimize.segment.select.groupby.GroupBy; +import org.apache.shardingsphere.core.optimize.segment.select.groupby.GroupByContext; import org.apache.shardingsphere.core.optimize.segment.select.item.impl.ColumnSelectItem; import org.apache.shardingsphere.core.optimize.segment.select.item.SelectItem; -import org.apache.shardingsphere.core.optimize.segment.select.item.SelectItems; -import org.apache.shardingsphere.core.optimize.segment.select.orderby.OrderBy; +import org.apache.shardingsphere.core.optimize.segment.select.item.SelectItemsContext; +import org.apache.shardingsphere.core.optimize.segment.select.orderby.OrderByContext; import org.apache.shardingsphere.core.optimize.segment.select.orderby.OrderByItem; import org.apache.shardingsphere.core.parse.core.constant.OrderDirection; import org.apache.shardingsphere.core.parse.sql.segment.dml.column.ColumnSegment; @@ -46,7 +46,7 @@ import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertThat; import static org.junit.Assert.assertTrue; -public final class SelectOptimizedStatementTest { +public final class SelectSQLStatementContextTest { private static final String INDEX_ORDER_BY = "IndexOrderBy"; @@ -58,38 +58,38 @@ public final class SelectOptimizedStatementTest { @Test public void assertSetIndexForItemsByIndexOrderBy() { - SelectOptimizedStatement selectOptimizedStatement = new SelectOptimizedStatement( - new SelectStatement(), new GroupBy(Collections.emptyList(), 0), createOrderBy(INDEX_ORDER_BY), createSelectItems(), null); - selectOptimizedStatement.setIndexForItems(Collections.emptyMap()); - assertThat(selectOptimizedStatement.getOrderBy().getItems().iterator().next().getIndex(), is(4)); + SelectSQLStatementContext selectSQLStatementContext = new SelectSQLStatementContext( + new SelectStatement(), new GroupByContext(Collections.emptyList(), 0), createOrderBy(INDEX_ORDER_BY), createSelectItems(), null); + selectSQLStatementContext.setIndexForItems(Collections.emptyMap()); + assertThat(selectSQLStatementContext.getOrderByContext().getItems().iterator().next().getIndex(), is(4)); } @Test public void assertSetIndexForItemsByColumnOrderByWithOwner() { - SelectOptimizedStatement selectOptimizedStatement = new SelectOptimizedStatement( - new SelectStatement(), new GroupBy(Collections.emptyList(), 0), createOrderBy(COLUMN_ORDER_BY_WITH_OWNER), createSelectItems(), null); - selectOptimizedStatement.setIndexForItems(Collections.emptyMap()); - assertThat(selectOptimizedStatement.getOrderBy().getItems().iterator().next().getIndex(), is(1)); + SelectSQLStatementContext selectSQLStatementContext = new SelectSQLStatementContext( + new SelectStatement(), new GroupByContext(Collections.emptyList(), 0), createOrderBy(COLUMN_ORDER_BY_WITH_OWNER), createSelectItems(), null); + selectSQLStatementContext.setIndexForItems(Collections.emptyMap()); + assertThat(selectSQLStatementContext.getOrderByContext().getItems().iterator().next().getIndex(), is(1)); } @Test public void assertSetIndexForItemsByColumnOrderByWithAlias() { - SelectOptimizedStatement selectOptimizedStatement = new SelectOptimizedStatement( - new SelectStatement(), new GroupBy(Collections.emptyList(), 0), createOrderBy(COLUMN_ORDER_BY_WITH_ALIAS), createSelectItems(), null); + SelectSQLStatementContext selectSQLStatementContext = new SelectSQLStatementContext( + new SelectStatement(), new GroupByContext(Collections.emptyList(), 0), createOrderBy(COLUMN_ORDER_BY_WITH_ALIAS), createSelectItems(), null); Map columnLabelIndexMap = new HashMap<>(); columnLabelIndexMap.put("n", 2); - selectOptimizedStatement.setIndexForItems(columnLabelIndexMap); - assertThat(selectOptimizedStatement.getOrderBy().getItems().iterator().next().getIndex(), is(2)); + selectSQLStatementContext.setIndexForItems(columnLabelIndexMap); + assertThat(selectSQLStatementContext.getOrderByContext().getItems().iterator().next().getIndex(), is(2)); } @Test public void assertSetIndexForItemsByColumnOrderByWithoutAlias() { - SelectOptimizedStatement selectOptimizedStatement = new SelectOptimizedStatement( - new SelectStatement(), new GroupBy(Collections.emptyList(), 0), createOrderBy(COLUMN_ORDER_BY_WITHOUT_OWNER_ALIAS), createSelectItems(), null); + SelectSQLStatementContext selectSQLStatementContext = new SelectSQLStatementContext( + new SelectStatement(), new GroupByContext(Collections.emptyList(), 0), createOrderBy(COLUMN_ORDER_BY_WITHOUT_OWNER_ALIAS), createSelectItems(), null); Map columnLabelIndexMap = new HashMap<>(); columnLabelIndexMap.put("id", 3); - selectOptimizedStatement.setIndexForItems(columnLabelIndexMap); - assertThat(selectOptimizedStatement.getOrderBy().getItems().iterator().next().getIndex(), is(3)); + selectSQLStatementContext.setIndexForItems(columnLabelIndexMap); + assertThat(selectSQLStatementContext.getOrderByContext().getItems().iterator().next().getIndex(), is(3)); } @Test @@ -98,16 +98,16 @@ public final class SelectOptimizedStatementTest { selectStatement.setSelectItems(new SelectItemsSegment(0, 0, false)); selectStatement.setGroupBy(new GroupBySegment(0, 0, Collections.singletonList(new IndexOrderByItemSegment(0, 0, 1, OrderDirection.DESC, OrderDirection.DESC)))); selectStatement.setOrderBy(new OrderBySegment(0, 0, Collections.singletonList(new IndexOrderByItemSegment(0, 0, 1, OrderDirection.DESC, OrderDirection.DESC)))); - SelectOptimizedStatement selectOptimizedStatement = new SelectOptimizedStatement(null, "", Collections.emptyList(), selectStatement); - assertTrue(selectOptimizedStatement.isSameGroupByAndOrderByItems()); + SelectSQLStatementContext selectSQLStatementContext = new SelectSQLStatementContext(null, "", Collections.emptyList(), selectStatement); + assertTrue(selectSQLStatementContext.isSameGroupByAndOrderByItems()); } @Test public void assertIsNotSameGroupByAndOrderByItemsWhenEmptyGroupBy() { SelectStatement selectStatement = new SelectStatement(); selectStatement.setSelectItems(new SelectItemsSegment(0, 0, false)); - SelectOptimizedStatement selectOptimizedStatement = new SelectOptimizedStatement(null, "", Collections.emptyList(), selectStatement); - assertFalse(selectOptimizedStatement.isSameGroupByAndOrderByItems()); + SelectSQLStatementContext selectSQLStatementContext = new SelectSQLStatementContext(null, "", Collections.emptyList(), selectStatement); + assertFalse(selectSQLStatementContext.isSameGroupByAndOrderByItems()); } @Test @@ -116,14 +116,14 @@ public final class SelectOptimizedStatementTest { selectStatement.setSelectItems(new SelectItemsSegment(0, 0, false)); selectStatement.setGroupBy(new GroupBySegment(0, 0, Collections.singletonList(new IndexOrderByItemSegment(0, 0, 1, OrderDirection.ASC, OrderDirection.DESC)))); selectStatement.setOrderBy(new OrderBySegment(0, 0, Collections.singletonList(new IndexOrderByItemSegment(0, 0, 1, OrderDirection.DESC, OrderDirection.DESC)))); - SelectOptimizedStatement selectOptimizedStatement = new SelectOptimizedStatement(null, "", Collections.emptyList(), selectStatement); - assertFalse(selectOptimizedStatement.isSameGroupByAndOrderByItems()); + SelectSQLStatementContext selectSQLStatementContext = new SelectSQLStatementContext(null, "", Collections.emptyList(), selectStatement); + assertFalse(selectSQLStatementContext.isSameGroupByAndOrderByItems()); } - private OrderBy createOrderBy(final String type) { + private OrderByContext createOrderBy(final String type) { OrderByItemSegment orderByItemSegment = createOrderByItemSegment(type); OrderByItem orderByItem = new OrderByItem(orderByItemSegment); - return new OrderBy(Lists.newArrayList(orderByItem), true); + return new OrderByContext(Lists.newArrayList(orderByItem), true); } private OrderByItemSegment createOrderByItemSegment(final String type) { @@ -141,9 +141,9 @@ public final class SelectOptimizedStatementTest { } } - private SelectItems createSelectItems() { + private SelectItemsContext createSelectItems() { Collection selectItems = Lists.newArrayList(getColumnSelectItemWithoutOwner(), getColumnSelectItemWithoutOwner(true), getColumnSelectItemWithoutOwner(false)); - return new SelectItems(0, 0, true, selectItems); + return new SelectItemsContext(0, 0, true, selectItems); } private SelectItem getColumnSelectItemWithoutOwner() { diff --git a/sharding-core/sharding-core-rewrite/src/main/java/org/apache/shardingsphere/core/rewrite/SQLRewriteEngine.java b/sharding-core/sharding-core-rewrite/src/main/java/org/apache/shardingsphere/core/rewrite/SQLRewriteEngine.java index 81d6710c21..34f23f0741 100644 --- a/sharding-core/sharding-core-rewrite/src/main/java/org/apache/shardingsphere/core/rewrite/SQLRewriteEngine.java +++ b/sharding-core/sharding-core-rewrite/src/main/java/org/apache/shardingsphere/core/rewrite/SQLRewriteEngine.java @@ -18,9 +18,9 @@ package org.apache.shardingsphere.core.rewrite; import org.apache.shardingsphere.core.metadata.table.TableMetas; -import org.apache.shardingsphere.core.optimize.segment.insert.InsertValue; -import org.apache.shardingsphere.core.optimize.statement.impl.InsertOptimizedStatement; -import org.apache.shardingsphere.core.optimize.statement.OptimizedStatement; +import org.apache.shardingsphere.core.optimize.segment.insert.InsertValueContext; +import org.apache.shardingsphere.core.optimize.statement.impl.InsertSQLStatementContext; +import org.apache.shardingsphere.core.optimize.statement.SQLStatementContext; import org.apache.shardingsphere.core.rewrite.builder.parameter.ParameterBuilder; import org.apache.shardingsphere.core.rewrite.builder.parameter.group.GroupedParameterBuilder; import org.apache.shardingsphere.core.rewrite.builder.parameter.standard.StandardParameterBuilder; @@ -75,8 +75,8 @@ public final class SQLRewriteEngine { sqlBuilder = new SQLBuilder(sql, sqlTokens); } - public SQLRewriteEngine(final EncryptRule encryptRule, final TableMetas tableMetas, - final OptimizedStatement encryptStatement, final String sql, final List parameters, final boolean isQueryWithCipherColumn) { + public SQLRewriteEngine(final EncryptRule encryptRule, final TableMetas tableMetas, + final SQLStatementContext encryptStatement, final String sql, final List parameters, final boolean isQueryWithCipherColumn) { baseRule = encryptRule; rewriteStatement = RewriteStatementFactory.newInstance(encryptRule, tableMetas, encryptStatement); parameterBuilder = createParameterBuilder(parameters); @@ -84,28 +84,28 @@ public final class SQLRewriteEngine { sqlBuilder = new SQLBuilder(sql, sqlTokens); } - public SQLRewriteEngine(final MasterSlaveRule masterSlaveRule, final OptimizedStatement optimizedStatement, final String sql) { + public SQLRewriteEngine(final MasterSlaveRule masterSlaveRule, final SQLStatementContext sqlStatementContext, final String sql) { baseRule = masterSlaveRule; rewriteStatement = new RewriteStatement( - optimizedStatement, new ShardingConditions(Collections.emptyList()), new EncryptConditions(Collections.emptyList())); + sqlStatementContext, new ShardingConditions(Collections.emptyList()), new EncryptConditions(Collections.emptyList())); parameterBuilder = createParameterBuilder(Collections.emptyList()); sqlTokens = createSQLTokens(false, false); sqlBuilder = new SQLBuilder(sql, sqlTokens); } private ParameterBuilder createParameterBuilder(final List parameters, final SQLRouteResult sqlRouteResult) { - return rewriteStatement.getOptimizedStatement() instanceof InsertOptimizedStatement + return rewriteStatement.getSqlStatementContext() instanceof InsertSQLStatementContext ? new GroupedParameterBuilder(parameters, getGroupedParameters(), rewriteStatement.getShardingConditions()) : new StandardParameterBuilder(parameters, sqlRouteResult); } private ParameterBuilder createParameterBuilder(final List parameters) { - return rewriteStatement.getOptimizedStatement() instanceof InsertOptimizedStatement + return rewriteStatement.getSqlStatementContext() instanceof InsertSQLStatementContext ? new GroupedParameterBuilder(parameters, getGroupedParameters(), null) : new StandardParameterBuilder(parameters); } private List> getGroupedParameters() { List> result = new LinkedList<>(); - for (InsertValue each : ((InsertOptimizedStatement) rewriteStatement.getOptimizedStatement()).getInsertValues()) { + for (InsertValueContext each : ((InsertSQLStatementContext) rewriteStatement.getSqlStatementContext()).getInsertValueContexts()) { result.add(each.getParameters()); } return result; diff --git a/sharding-core/sharding-core-rewrite/src/main/java/org/apache/shardingsphere/core/rewrite/builder/parameter/standard/StandardParameterBuilder.java b/sharding-core/sharding-core-rewrite/src/main/java/org/apache/shardingsphere/core/rewrite/builder/parameter/standard/StandardParameterBuilder.java index fb9d17bee2..ea10abfad7 100644 --- a/sharding-core/sharding-core-rewrite/src/main/java/org/apache/shardingsphere/core/rewrite/builder/parameter/standard/StandardParameterBuilder.java +++ b/sharding-core/sharding-core-rewrite/src/main/java/org/apache/shardingsphere/core/rewrite/builder/parameter/standard/StandardParameterBuilder.java @@ -19,8 +19,8 @@ package org.apache.shardingsphere.core.rewrite.builder.parameter.standard; import com.google.common.base.Optional; import lombok.Getter; -import org.apache.shardingsphere.core.optimize.segment.select.pagination.Pagination; -import org.apache.shardingsphere.core.optimize.statement.impl.SelectOptimizedStatement; +import org.apache.shardingsphere.core.optimize.segment.select.pagination.PaginationContext; +import org.apache.shardingsphere.core.optimize.statement.impl.SelectSQLStatementContext; import org.apache.shardingsphere.core.rewrite.builder.parameter.ParameterBuilder; import org.apache.shardingsphere.core.route.SQLRouteResult; import org.apache.shardingsphere.core.route.type.RoutingUnit; @@ -57,7 +57,7 @@ public final class StandardParameterBuilder implements ParameterBuilder { private void setReplacedIndexAndParameters(final SQLRouteResult sqlRouteResult) { if (isNeedRewritePagination(sqlRouteResult)) { - Pagination pagination = ((SelectOptimizedStatement) sqlRouteResult.getShardingStatement()).getPagination(); + PaginationContext pagination = ((SelectSQLStatementContext) sqlRouteResult.getShardingStatementContext()).getPaginationContext(); Optional offsetParameterIndex = pagination.getOffsetParameterIndex(); if (offsetParameterIndex.isPresent()) { rewriteOffset(pagination, offsetParameterIndex.get()); @@ -70,16 +70,16 @@ public final class StandardParameterBuilder implements ParameterBuilder { } private boolean isNeedRewritePagination(final SQLRouteResult sqlRouteResult) { - return sqlRouteResult.getShardingStatement() instanceof SelectOptimizedStatement - && ((SelectOptimizedStatement) sqlRouteResult.getShardingStatement()).getPagination().isHasPagination() && !sqlRouteResult.getRoutingResult().isSingleRouting(); + return sqlRouteResult.getShardingStatementContext() instanceof SelectSQLStatementContext + && ((SelectSQLStatementContext) sqlRouteResult.getShardingStatementContext()).getPaginationContext().isHasPagination() && !sqlRouteResult.getRoutingResult().isSingleRouting(); } - private void rewriteOffset(final Pagination pagination, final int offsetParameterIndex) { + private void rewriteOffset(final PaginationContext pagination, final int offsetParameterIndex) { replacedIndexAndParameters.put(offsetParameterIndex, pagination.getRevisedOffset()); } - private void rewriteRowCount(final Pagination pagination, final int rowCountParameterIndex, final SQLRouteResult sqlRouteResult) { - replacedIndexAndParameters.put(rowCountParameterIndex, pagination.getRevisedRowCount((SelectOptimizedStatement) sqlRouteResult.getShardingStatement())); + private void rewriteRowCount(final PaginationContext pagination, final int rowCountParameterIndex, final SQLRouteResult sqlRouteResult) { + replacedIndexAndParameters.put(rowCountParameterIndex, pagination.getRevisedRowCount((SelectSQLStatementContext) sqlRouteResult.getShardingStatementContext())); } @Override diff --git a/sharding-core/sharding-core-rewrite/src/main/java/org/apache/shardingsphere/core/rewrite/encrypt/WhereClauseEncryptConditionEngine.java b/sharding-core/sharding-core-rewrite/src/main/java/org/apache/shardingsphere/core/rewrite/encrypt/WhereClauseEncryptConditionEngine.java index 389ccf3a62..d8e14e7872 100644 --- a/sharding-core/sharding-core-rewrite/src/main/java/org/apache/shardingsphere/core/rewrite/encrypt/WhereClauseEncryptConditionEngine.java +++ b/sharding-core/sharding-core-rewrite/src/main/java/org/apache/shardingsphere/core/rewrite/encrypt/WhereClauseEncryptConditionEngine.java @@ -21,7 +21,7 @@ import com.google.common.base.Optional; import lombok.RequiredArgsConstructor; import org.apache.shardingsphere.core.exception.ShardingException; import org.apache.shardingsphere.core.metadata.table.TableMetas; -import org.apache.shardingsphere.core.optimize.segment.Tables; +import org.apache.shardingsphere.core.optimize.segment.table.TablesContext; import org.apache.shardingsphere.core.parse.sql.segment.dml.expr.ExpressionSegment; import org.apache.shardingsphere.core.parse.sql.segment.dml.expr.simple.SimpleExpressionSegment; import org.apache.shardingsphere.core.parse.sql.segment.dml.predicate.AndPredicate; @@ -68,24 +68,24 @@ public final class WhereClauseEncryptConditionEngine { return Collections.emptyList(); } List result = new LinkedList<>(); - Tables tables = new Tables(sqlStatement); + TablesContext tablesContext = new TablesContext(sqlStatement); for (AndPredicate each : whereSegment.get().getAndPredicates()) { - result.addAll(createEncryptConditions(each, tables)); + result.addAll(createEncryptConditions(each, tablesContext)); } for (SubqueryPredicateSegment each : sqlStatement.findSQLSegments(SubqueryPredicateSegment.class)) { for (AndPredicate andPredicate : each.getAndPredicates()) { - result.addAll(createEncryptConditions(andPredicate, tables)); + result.addAll(createEncryptConditions(andPredicate, tablesContext)); } } return result; } - private Collection createEncryptConditions(final AndPredicate andPredicate, final Tables tables) { + private Collection createEncryptConditions(final AndPredicate andPredicate, final TablesContext tablesContext) { Collection result = new LinkedList<>(); Collection stopIndexes = new HashSet<>(); for (PredicateSegment predicate : andPredicate.getPredicates()) { if (stopIndexes.add(predicate.getStopIndex())) { - Optional condition = createEncryptCondition(predicate, tables); + Optional condition = createEncryptCondition(predicate, tablesContext); if (condition.isPresent()) { result.add(condition.get()); } @@ -94,8 +94,8 @@ public final class WhereClauseEncryptConditionEngine { return result; } - private Optional createEncryptCondition(final PredicateSegment predicateSegment, final Tables tables) { - Optional tableName = tables.findTableName(predicateSegment.getColumn(), tableMetas); + private Optional createEncryptCondition(final PredicateSegment predicateSegment, final TablesContext tablesContext) { + Optional tableName = tablesContext.findTableName(predicateSegment.getColumn(), tableMetas); return tableName.isPresent() && encryptRule.findShardingEncryptor(tableName.get(), predicateSegment.getColumn().getName()).isPresent() ? createEncryptCondition(predicateSegment, tableName.get()) : Optional.absent(); } diff --git a/sharding-core/sharding-core-rewrite/src/main/java/org/apache/shardingsphere/core/rewrite/statement/InsertRewriteStatement.java b/sharding-core/sharding-core-rewrite/src/main/java/org/apache/shardingsphere/core/rewrite/statement/InsertRewriteStatement.java index d978df25cf..8ce481efac 100644 --- a/sharding-core/sharding-core-rewrite/src/main/java/org/apache/shardingsphere/core/rewrite/statement/InsertRewriteStatement.java +++ b/sharding-core/sharding-core-rewrite/src/main/java/org/apache/shardingsphere/core/rewrite/statement/InsertRewriteStatement.java @@ -19,8 +19,8 @@ package org.apache.shardingsphere.core.rewrite.statement; import com.google.common.base.Optional; import com.google.common.base.Preconditions; -import org.apache.shardingsphere.core.optimize.segment.insert.InsertValue; -import org.apache.shardingsphere.core.optimize.statement.impl.InsertOptimizedStatement; +import org.apache.shardingsphere.core.optimize.segment.insert.InsertValueContext; +import org.apache.shardingsphere.core.optimize.statement.impl.InsertSQLStatementContext; import org.apache.shardingsphere.core.rewrite.encrypt.EncryptConditions; import org.apache.shardingsphere.core.rewrite.statement.constant.EncryptDerivedColumnType; import org.apache.shardingsphere.core.rewrite.statement.constant.ShardingDerivedColumnType; @@ -42,25 +42,25 @@ public final class InsertRewriteStatement extends RewriteStatement { private final GeneratedKey generatedKey; - public InsertRewriteStatement(final InsertOptimizedStatement optimizedStatement, + public InsertRewriteStatement(final InsertSQLStatementContext insertSQLStatementContext, final ShardingConditions shardingConditions, final EncryptConditions encryptConditions, final GeneratedKey generatedKey, final EncryptRule encryptRule) { - super(optimizedStatement, shardingConditions, encryptConditions); + super(insertSQLStatementContext, shardingConditions, encryptConditions); this.generatedKey = generatedKey; - processGeneratedKey(optimizedStatement); - processEncrypt(optimizedStatement, encryptRule); + processGeneratedKey(insertSQLStatementContext); + processEncrypt(insertSQLStatementContext, encryptRule); } - private void processGeneratedKey(final InsertOptimizedStatement optimizedStatement) { + private void processGeneratedKey(final InsertSQLStatementContext insertSQLStatementContext) { if (null != generatedKey && generatedKey.isGenerated()) { Iterator> generatedValues = generatedKey.getGeneratedValues().descendingIterator(); - for (InsertValue each : optimizedStatement.getInsertValues()) { + for (InsertValueContext each : insertSQLStatementContext.getInsertValueContexts()) { each.appendValue(generatedValues.next(), ShardingDerivedColumnType.KEY_GEN); } } } - private void processEncrypt(final InsertOptimizedStatement optimizedStatement, final EncryptRule encryptRule) { - String tableName = optimizedStatement.getTables().getSingleTableName(); + private void processEncrypt(final InsertSQLStatementContext insertSQLStatementContext, final EncryptRule encryptRule) { + String tableName = insertSQLStatementContext.getTablesContext().getSingleTableName(); Optional encryptTable = encryptRule.findEncryptTable(tableName); if (!encryptTable.isPresent()) { return; @@ -68,30 +68,30 @@ public final class InsertRewriteStatement extends RewriteStatement { for (String each : encryptTable.get().getLogicColumns()) { Optional shardingEncryptor = encryptRule.findShardingEncryptor(tableName, each); if (shardingEncryptor.isPresent()) { - encryptInsertValues(optimizedStatement, encryptRule, shardingEncryptor.get(), tableName, each); + encryptInsertValues(insertSQLStatementContext, encryptRule, shardingEncryptor.get(), tableName, each); } } } - private void encryptInsertValues(final InsertOptimizedStatement optimizedStatement, final EncryptRule encryptRule, final ShardingEncryptor shardingEncryptor, + private void encryptInsertValues(final InsertSQLStatementContext insertSQLStatementContext, final EncryptRule encryptRule, final ShardingEncryptor shardingEncryptor, final String tableName, final String encryptLogicColumnName) { - int columnIndex = optimizedStatement.getColumnNames().indexOf(encryptLogicColumnName); - for (InsertValue each : optimizedStatement.getInsertValues()) { + int columnIndex = insertSQLStatementContext.getColumnNames().indexOf(encryptLogicColumnName); + for (InsertValueContext each : insertSQLStatementContext.getInsertValueContexts()) { encryptInsertValue(encryptRule, shardingEncryptor, tableName, columnIndex, each, encryptLogicColumnName); } } private void encryptInsertValue(final EncryptRule encryptRule, final ShardingEncryptor shardingEncryptor, - final String tableName, final int columnIndex, final InsertValue insertValue, final String encryptLogicColumnName) { - Object originalValue = insertValue.getValue(columnIndex); - insertValue.setValue(columnIndex, shardingEncryptor.encrypt(originalValue)); + final String tableName, final int columnIndex, final InsertValueContext insertValueContext, final String encryptLogicColumnName) { + Object originalValue = insertValueContext.getValue(columnIndex); + insertValueContext.setValue(columnIndex, shardingEncryptor.encrypt(originalValue)); if (shardingEncryptor instanceof ShardingQueryAssistedEncryptor) { Optional assistedColumnName = encryptRule.findAssistedQueryColumn(tableName, encryptLogicColumnName); Preconditions.checkArgument(assistedColumnName.isPresent(), "Can not find assisted query Column Name"); - insertValue.appendValue(((ShardingQueryAssistedEncryptor) shardingEncryptor).queryAssistedEncrypt(originalValue.toString()), EncryptDerivedColumnType.ENCRYPT); + insertValueContext.appendValue(((ShardingQueryAssistedEncryptor) shardingEncryptor).queryAssistedEncrypt(originalValue.toString()), EncryptDerivedColumnType.ENCRYPT); } if (encryptRule.findPlainColumn(tableName, encryptLogicColumnName).isPresent()) { - insertValue.appendValue(originalValue, EncryptDerivedColumnType.ENCRYPT); + insertValueContext.appendValue(originalValue, EncryptDerivedColumnType.ENCRYPT); } } diff --git a/sharding-core/sharding-core-rewrite/src/main/java/org/apache/shardingsphere/core/rewrite/statement/RewriteStatement.java b/sharding-core/sharding-core-rewrite/src/main/java/org/apache/shardingsphere/core/rewrite/statement/RewriteStatement.java index d61301339e..dcadac7788 100644 --- a/sharding-core/sharding-core-rewrite/src/main/java/org/apache/shardingsphere/core/rewrite/statement/RewriteStatement.java +++ b/sharding-core/sharding-core-rewrite/src/main/java/org/apache/shardingsphere/core/rewrite/statement/RewriteStatement.java @@ -19,7 +19,7 @@ package org.apache.shardingsphere.core.rewrite.statement; import lombok.Getter; import lombok.RequiredArgsConstructor; -import org.apache.shardingsphere.core.optimize.statement.OptimizedStatement; +import org.apache.shardingsphere.core.optimize.statement.SQLStatementContext; import org.apache.shardingsphere.core.rewrite.encrypt.EncryptConditions; import org.apache.shardingsphere.core.route.router.sharding.condition.ShardingConditions; @@ -32,7 +32,7 @@ import org.apache.shardingsphere.core.route.router.sharding.condition.ShardingCo @Getter public class RewriteStatement { - private final OptimizedStatement optimizedStatement; + private final SQLStatementContext sqlStatementContext; private final ShardingConditions shardingConditions; diff --git a/sharding-core/sharding-core-rewrite/src/main/java/org/apache/shardingsphere/core/rewrite/statement/RewriteStatementFactory.java b/sharding-core/sharding-core-rewrite/src/main/java/org/apache/shardingsphere/core/rewrite/statement/RewriteStatementFactory.java index 7d91f8dee7..7d5553d67c 100644 --- a/sharding-core/sharding-core-rewrite/src/main/java/org/apache/shardingsphere/core/rewrite/statement/RewriteStatementFactory.java +++ b/sharding-core/sharding-core-rewrite/src/main/java/org/apache/shardingsphere/core/rewrite/statement/RewriteStatementFactory.java @@ -20,8 +20,8 @@ package org.apache.shardingsphere.core.rewrite.statement; import lombok.AccessLevel; import lombok.NoArgsConstructor; import org.apache.shardingsphere.core.metadata.table.TableMetas; -import org.apache.shardingsphere.core.optimize.statement.impl.InsertOptimizedStatement; -import org.apache.shardingsphere.core.optimize.statement.OptimizedStatement; +import org.apache.shardingsphere.core.optimize.statement.impl.InsertSQLStatementContext; +import org.apache.shardingsphere.core.optimize.statement.SQLStatementContext; import org.apache.shardingsphere.core.parse.sql.statement.dml.DMLStatement; import org.apache.shardingsphere.core.rewrite.encrypt.EncryptCondition; import org.apache.shardingsphere.core.rewrite.encrypt.EncryptConditions; @@ -51,14 +51,14 @@ public final class RewriteStatementFactory { * @return rewrite statement */ public static RewriteStatement newInstance(final ShardingRule shardingRule, final TableMetas tableMetas, final SQLRouteResult sqlRouteResult) { - EncryptConditions encryptConditions = sqlRouteResult.getEncryptStatement().getSqlStatement() instanceof DMLStatement - ? createEncryptConditions(shardingRule.getEncryptRule(), tableMetas, (DMLStatement) sqlRouteResult.getEncryptStatement().getSqlStatement()) + EncryptConditions encryptConditions = sqlRouteResult.getEncryptStatementContext().getSqlStatement() instanceof DMLStatement + ? createEncryptConditions(shardingRule.getEncryptRule(), tableMetas, (DMLStatement) sqlRouteResult.getEncryptStatementContext().getSqlStatement()) : new EncryptConditions(Collections.emptyList()); - return sqlRouteResult.getShardingStatement() instanceof InsertOptimizedStatement + return sqlRouteResult.getShardingStatementContext() instanceof InsertSQLStatementContext ? new InsertRewriteStatement( - (InsertOptimizedStatement) sqlRouteResult.getShardingStatement(), sqlRouteResult.getShardingConditions(), encryptConditions, + (InsertSQLStatementContext) sqlRouteResult.getShardingStatementContext(), sqlRouteResult.getShardingConditions(), encryptConditions, sqlRouteResult.getGeneratedKey().orNull(), shardingRule.getEncryptRule()) - : new RewriteStatement(sqlRouteResult.getShardingStatement(), sqlRouteResult.getShardingConditions(), encryptConditions); + : new RewriteStatement(sqlRouteResult.getShardingStatementContext(), sqlRouteResult.getShardingConditions(), encryptConditions); } /** @@ -66,16 +66,16 @@ public final class RewriteStatementFactory { * * @param encryptRule encrypt rule * @param tableMetas table metas - * @param encryptStatement encrypt statement + * @param sqlStatementContext SQL statement context * @return rewrite statement */ - public static RewriteStatement newInstance(final EncryptRule encryptRule, final TableMetas tableMetas, final OptimizedStatement encryptStatement) { + public static RewriteStatement newInstance(final EncryptRule encryptRule, final TableMetas tableMetas, final SQLStatementContext sqlStatementContext) { ShardingConditions shardingConditions = new ShardingConditions(Collections.emptyList()); - EncryptConditions encryptConditions = encryptStatement.getSqlStatement() instanceof DMLStatement - ? createEncryptConditions(encryptRule, tableMetas, (DMLStatement) encryptStatement.getSqlStatement()) : new EncryptConditions(Collections.emptyList()); - return encryptStatement instanceof InsertOptimizedStatement - ? new InsertRewriteStatement((InsertOptimizedStatement) encryptStatement, shardingConditions, encryptConditions, null, encryptRule) - : new RewriteStatement(encryptStatement, shardingConditions, encryptConditions); + EncryptConditions encryptConditions = sqlStatementContext.getSqlStatement() instanceof DMLStatement + ? createEncryptConditions(encryptRule, tableMetas, (DMLStatement) sqlStatementContext.getSqlStatement()) : new EncryptConditions(Collections.emptyList()); + return sqlStatementContext instanceof InsertSQLStatementContext + ? new InsertRewriteStatement((InsertSQLStatementContext) sqlStatementContext, shardingConditions, encryptConditions, null, encryptRule) + : new RewriteStatement(sqlStatementContext, shardingConditions, encryptConditions); } private static EncryptConditions createEncryptConditions(final EncryptRule encryptRule, final TableMetas tableMetas, final DMLStatement sqlStatement) { diff --git a/sharding-core/sharding-core-rewrite/src/main/java/org/apache/shardingsphere/core/rewrite/token/generator/AggregationDistinctTokenGenerator.java b/sharding-core/sharding-core-rewrite/src/main/java/org/apache/shardingsphere/core/rewrite/token/generator/AggregationDistinctTokenGenerator.java index 2e770cdd9b..08a7af811a 100644 --- a/sharding-core/sharding-core-rewrite/src/main/java/org/apache/shardingsphere/core/rewrite/token/generator/AggregationDistinctTokenGenerator.java +++ b/sharding-core/sharding-core-rewrite/src/main/java/org/apache/shardingsphere/core/rewrite/token/generator/AggregationDistinctTokenGenerator.java @@ -20,7 +20,7 @@ package org.apache.shardingsphere.core.rewrite.token.generator; import com.google.common.base.Preconditions; import org.apache.shardingsphere.core.optimize.segment.select.item.impl.AggregationDistinctSelectItem; import org.apache.shardingsphere.core.optimize.segment.select.item.DerivedColumn; -import org.apache.shardingsphere.core.optimize.statement.impl.SelectOptimizedStatement; +import org.apache.shardingsphere.core.optimize.statement.impl.SelectSQLStatementContext; import org.apache.shardingsphere.core.rewrite.builder.parameter.ParameterBuilder; import org.apache.shardingsphere.core.rewrite.statement.RewriteStatement; import org.apache.shardingsphere.core.rewrite.token.pojo.AggregationDistinctToken; @@ -40,11 +40,11 @@ public final class AggregationDistinctTokenGenerator implements CollectionSQLTok @Override public Collection generateSQLTokens( final RewriteStatement rewriteStatement, final ParameterBuilder parameterBuilder, final ShardingRule shardingRule, final boolean isQueryWithCipherColumn) { - if (!(rewriteStatement.getOptimizedStatement() instanceof SelectOptimizedStatement)) { + if (!(rewriteStatement.getSqlStatementContext() instanceof SelectSQLStatementContext)) { return Collections.emptyList(); } Collection result = new LinkedList<>(); - for (AggregationDistinctSelectItem each : ((SelectOptimizedStatement) rewriteStatement.getOptimizedStatement()).getSelectItems().getAggregationDistinctSelectItems()) { + for (AggregationDistinctSelectItem each : ((SelectSQLStatementContext) rewriteStatement.getSqlStatementContext()).getSelectItemsContext().getAggregationDistinctSelectItems()) { result.add(createAggregationDistinctToken(each)); } return result; diff --git a/sharding-core/sharding-core-rewrite/src/main/java/org/apache/shardingsphere/core/rewrite/token/generator/IndexTokenGenerator.java b/sharding-core/sharding-core-rewrite/src/main/java/org/apache/shardingsphere/core/rewrite/token/generator/IndexTokenGenerator.java index 1cc8eb538a..4a83352f1c 100644 --- a/sharding-core/sharding-core-rewrite/src/main/java/org/apache/shardingsphere/core/rewrite/token/generator/IndexTokenGenerator.java +++ b/sharding-core/sharding-core-rewrite/src/main/java/org/apache/shardingsphere/core/rewrite/token/generator/IndexTokenGenerator.java @@ -39,7 +39,7 @@ public final class IndexTokenGenerator implements CollectionSQLTokenGenerator generateSQLTokens( final RewriteStatement rewriteStatement, final ParameterBuilder parameterBuilder, final ShardingRule shardingRule, final boolean isQueryWithCipherColumn) { Collection result = new LinkedList<>(); - for (SQLSegment each : rewriteStatement.getOptimizedStatement().getSqlStatement().getAllSQLSegments()) { + for (SQLSegment each : rewriteStatement.getSqlStatementContext().getSqlStatement().getAllSQLSegments()) { if (each instanceof IndexSegment) { result.add(createIndexToken((IndexSegment) each)); } diff --git a/sharding-core/sharding-core-rewrite/src/main/java/org/apache/shardingsphere/core/rewrite/token/generator/InsertCipherNameTokenGenerator.java b/sharding-core/sharding-core-rewrite/src/main/java/org/apache/shardingsphere/core/rewrite/token/generator/InsertCipherNameTokenGenerator.java index 7c1e28a6d9..b046b637b9 100644 --- a/sharding-core/sharding-core-rewrite/src/main/java/org/apache/shardingsphere/core/rewrite/token/generator/InsertCipherNameTokenGenerator.java +++ b/sharding-core/sharding-core-rewrite/src/main/java/org/apache/shardingsphere/core/rewrite/token/generator/InsertCipherNameTokenGenerator.java @@ -18,8 +18,8 @@ package org.apache.shardingsphere.core.rewrite.token.generator; import com.google.common.base.Optional; -import org.apache.shardingsphere.core.optimize.statement.impl.InsertOptimizedStatement; -import org.apache.shardingsphere.core.optimize.statement.OptimizedStatement; +import org.apache.shardingsphere.core.optimize.statement.impl.InsertSQLStatementContext; +import org.apache.shardingsphere.core.optimize.statement.SQLStatementContext; import org.apache.shardingsphere.core.parse.sql.segment.dml.column.ColumnSegment; import org.apache.shardingsphere.core.parse.sql.segment.dml.column.InsertColumnsSegment; import org.apache.shardingsphere.core.rewrite.builder.parameter.ParameterBuilder; @@ -42,23 +42,23 @@ public final class InsertCipherNameTokenGenerator implements CollectionSQLTokenG @Override public Collection generateSQLTokens(final RewriteStatement rewriteStatement, final ParameterBuilder parameterBuilder, final EncryptRule rule, final boolean isQueryWithCipherColumn) { - if (!isNeedToGenerateSQLToken(rewriteStatement.getOptimizedStatement())) { + if (!isNeedToGenerateSQLToken(rewriteStatement.getSqlStatementContext())) { return Collections.emptyList(); } - return createInsertColumnTokens((InsertOptimizedStatement) rewriteStatement.getOptimizedStatement(), rule); + return createInsertColumnTokens((InsertSQLStatementContext) rewriteStatement.getSqlStatementContext(), rule); } - private boolean isNeedToGenerateSQLToken(final OptimizedStatement optimizedStatement) { - Optional insertColumnsSegment = optimizedStatement.getSqlStatement().findSQLSegment(InsertColumnsSegment.class); - return optimizedStatement instanceof InsertOptimizedStatement && insertColumnsSegment.isPresent() && !insertColumnsSegment.get().getColumns().isEmpty(); + private boolean isNeedToGenerateSQLToken(final SQLStatementContext sqlStatementContext) { + Optional insertColumnsSegment = sqlStatementContext.getSqlStatement().findSQLSegment(InsertColumnsSegment.class); + return sqlStatementContext instanceof InsertSQLStatementContext && insertColumnsSegment.isPresent() && !insertColumnsSegment.get().getColumns().isEmpty(); } - private Collection createInsertColumnTokens(final InsertOptimizedStatement optimizedStatement, final EncryptRule encryptRule) { - Optional insertColumnsSegment = optimizedStatement.getSqlStatement().findSQLSegment(InsertColumnsSegment.class); + private Collection createInsertColumnTokens(final InsertSQLStatementContext insertSQLStatementContext, final EncryptRule encryptRule) { + Optional insertColumnsSegment = insertSQLStatementContext.getSqlStatement().findSQLSegment(InsertColumnsSegment.class); if (!insertColumnsSegment.isPresent()) { return Collections.emptyList(); } - Map logicAndCipherColumns = encryptRule.getLogicAndCipherColumns(optimizedStatement.getTables().getSingleTableName()); + Map logicAndCipherColumns = encryptRule.getLogicAndCipherColumns(insertSQLStatementContext.getTablesContext().getSingleTableName()); Collection result = new LinkedList<>(); for (ColumnSegment each : insertColumnsSegment.get().getColumns()) { if (logicAndCipherColumns.keySet().contains(each.getName())) { diff --git a/sharding-core/sharding-core-rewrite/src/main/java/org/apache/shardingsphere/core/rewrite/token/generator/InsertGeneratedKeyNameTokenGenerator.java b/sharding-core/sharding-core-rewrite/src/main/java/org/apache/shardingsphere/core/rewrite/token/generator/InsertGeneratedKeyNameTokenGenerator.java index 55ae7f546f..3190d90faf 100644 --- a/sharding-core/sharding-core-rewrite/src/main/java/org/apache/shardingsphere/core/rewrite/token/generator/InsertGeneratedKeyNameTokenGenerator.java +++ b/sharding-core/sharding-core-rewrite/src/main/java/org/apache/shardingsphere/core/rewrite/token/generator/InsertGeneratedKeyNameTokenGenerator.java @@ -18,7 +18,7 @@ package org.apache.shardingsphere.core.rewrite.token.generator; import com.google.common.base.Optional; -import org.apache.shardingsphere.core.optimize.statement.impl.InsertOptimizedStatement; +import org.apache.shardingsphere.core.optimize.statement.impl.InsertSQLStatementContext; import org.apache.shardingsphere.core.parse.sql.segment.dml.column.InsertColumnsSegment; import org.apache.shardingsphere.core.rewrite.builder.parameter.ParameterBuilder; import org.apache.shardingsphere.core.rewrite.statement.InsertRewriteStatement; @@ -36,11 +36,11 @@ public final class InsertGeneratedKeyNameTokenGenerator implements OptionalSQLTo @Override public Optional generateSQLToken( final RewriteStatement rewriteStatement, final ParameterBuilder parameterBuilder, final ShardingRule shardingRule, final boolean isQueryWithCipherColumn) { - Optional insertColumnsSegment = rewriteStatement.getOptimizedStatement().getSqlStatement().findSQLSegment(InsertColumnsSegment.class); + Optional insertColumnsSegment = rewriteStatement.getSqlStatementContext().getSqlStatement().findSQLSegment(InsertColumnsSegment.class); if (!insertColumnsSegment.isPresent() || insertColumnsSegment.get().getColumns().isEmpty()) { return Optional.absent(); } - if (rewriteStatement.getOptimizedStatement() instanceof InsertOptimizedStatement) { + if (rewriteStatement.getSqlStatementContext() instanceof InsertSQLStatementContext) { return createInsertGeneratedKeyToken((InsertRewriteStatement) rewriteStatement, insertColumnsSegment.get(), shardingRule); } return Optional.absent(); @@ -48,7 +48,7 @@ public final class InsertGeneratedKeyNameTokenGenerator implements OptionalSQLTo private Optional createInsertGeneratedKeyToken( final InsertRewriteStatement rewriteStatement, final InsertColumnsSegment segment, final ShardingRule shardingRule) { - String tableName = rewriteStatement.getOptimizedStatement().getTables().getSingleTableName(); + String tableName = rewriteStatement.getSqlStatementContext().getTablesContext().getSingleTableName(); Optional generatedKeyColumnName = shardingRule.findGenerateKeyColumnName(tableName); return generatedKeyColumnName.isPresent() && rewriteStatement.getGeneratedKey().isPresent() && rewriteStatement.getGeneratedKey().get().isGenerated() ? Optional.of(new InsertGeneratedKeyNameToken(segment.getStopIndex(), generatedKeyColumnName.get(), isToAddCloseParenthesis(tableName, segment, shardingRule))) diff --git a/sharding-core/sharding-core-rewrite/src/main/java/org/apache/shardingsphere/core/rewrite/token/generator/InsertQueryAndPlainNamesTokenGenerator.java b/sharding-core/sharding-core-rewrite/src/main/java/org/apache/shardingsphere/core/rewrite/token/generator/InsertQueryAndPlainNamesTokenGenerator.java index c7d44d0b03..16b2416f13 100644 --- a/sharding-core/sharding-core-rewrite/src/main/java/org/apache/shardingsphere/core/rewrite/token/generator/InsertQueryAndPlainNamesTokenGenerator.java +++ b/sharding-core/sharding-core-rewrite/src/main/java/org/apache/shardingsphere/core/rewrite/token/generator/InsertQueryAndPlainNamesTokenGenerator.java @@ -18,8 +18,8 @@ package org.apache.shardingsphere.core.rewrite.token.generator; import com.google.common.base.Optional; -import org.apache.shardingsphere.core.optimize.statement.impl.InsertOptimizedStatement; -import org.apache.shardingsphere.core.optimize.statement.OptimizedStatement; +import org.apache.shardingsphere.core.optimize.statement.impl.InsertSQLStatementContext; +import org.apache.shardingsphere.core.optimize.statement.SQLStatementContext; import org.apache.shardingsphere.core.parse.sql.segment.dml.column.InsertColumnsSegment; import org.apache.shardingsphere.core.rewrite.builder.parameter.ParameterBuilder; import org.apache.shardingsphere.core.rewrite.statement.RewriteStatement; @@ -41,23 +41,23 @@ public final class InsertQueryAndPlainNamesTokenGenerator implements OptionalSQL @Override public Optional generateSQLToken( final RewriteStatement rewriteStatement, final ParameterBuilder parameterBuilder, final EncryptRule encryptRule, final boolean isQueryWithCipherColumn) { - if (!isNeedToGenerateSQLToken(rewriteStatement.getOptimizedStatement())) { + if (!isNeedToGenerateSQLToken(rewriteStatement.getSqlStatementContext())) { return Optional.absent(); } - return createInsertAssistedColumnsToken(rewriteStatement.getOptimizedStatement(), encryptRule); + return createInsertAssistedColumnsToken(rewriteStatement.getSqlStatementContext(), encryptRule); } - private boolean isNeedToGenerateSQLToken(final OptimizedStatement optimizedStatement) { - Optional insertColumnsSegment = optimizedStatement.getSqlStatement().findSQLSegment(InsertColumnsSegment.class); - return optimizedStatement instanceof InsertOptimizedStatement && insertColumnsSegment.isPresent(); + private boolean isNeedToGenerateSQLToken(final SQLStatementContext sqlStatementContext) { + Optional insertColumnsSegment = sqlStatementContext.getSqlStatement().findSQLSegment(InsertColumnsSegment.class); + return sqlStatementContext instanceof InsertSQLStatementContext && insertColumnsSegment.isPresent(); } - private Optional createInsertAssistedColumnsToken(final OptimizedStatement optimizedStatement, final EncryptRule encryptRule) { - Optional insertColumnsSegment = optimizedStatement.getSqlStatement().findSQLSegment(InsertColumnsSegment.class); + private Optional createInsertAssistedColumnsToken(final SQLStatementContext sqlStatementContext, final EncryptRule encryptRule) { + Optional insertColumnsSegment = sqlStatementContext.getSqlStatement().findSQLSegment(InsertColumnsSegment.class); if (!insertColumnsSegment.isPresent()) { return Optional.absent(); } - String tableName = optimizedStatement.getTables().getSingleTableName(); + String tableName = sqlStatementContext.getTablesContext().getSingleTableName(); return encryptRule.getAssistedQueryAndPlainColumns(tableName).isEmpty() ? Optional.absent() : Optional.of(new InsertQueryAndPlainNamesToken( insertColumnsSegment.get().getStopIndex(), getEncryptDerivedColumnNames(tableName, encryptRule), insertColumnsSegment.get().getColumns().isEmpty())); diff --git a/sharding-core/sharding-core-rewrite/src/main/java/org/apache/shardingsphere/core/rewrite/token/generator/InsertRegularNamesTokenGenerator.java b/sharding-core/sharding-core-rewrite/src/main/java/org/apache/shardingsphere/core/rewrite/token/generator/InsertRegularNamesTokenGenerator.java index 51aab376be..75aad4de4b 100644 --- a/sharding-core/sharding-core-rewrite/src/main/java/org/apache/shardingsphere/core/rewrite/token/generator/InsertRegularNamesTokenGenerator.java +++ b/sharding-core/sharding-core-rewrite/src/main/java/org/apache/shardingsphere/core/rewrite/token/generator/InsertRegularNamesTokenGenerator.java @@ -18,8 +18,8 @@ package org.apache.shardingsphere.core.rewrite.token.generator; import com.google.common.base.Optional; -import org.apache.shardingsphere.core.optimize.statement.impl.InsertOptimizedStatement; -import org.apache.shardingsphere.core.optimize.statement.OptimizedStatement; +import org.apache.shardingsphere.core.optimize.statement.impl.InsertSQLStatementContext; +import org.apache.shardingsphere.core.optimize.statement.SQLStatementContext; import org.apache.shardingsphere.core.parse.sql.segment.dml.column.InsertColumnsSegment; import org.apache.shardingsphere.core.rewrite.builder.parameter.ParameterBuilder; import org.apache.shardingsphere.core.rewrite.statement.InsertRewriteStatement; @@ -44,32 +44,32 @@ public final class InsertRegularNamesTokenGenerator implements OptionalSQLTokenG @Override public Optional generateSQLToken(final RewriteStatement rewriteStatement, final ParameterBuilder parameterBuilder, final BaseRule baseRule, final boolean isQueryWithCipherColumn) { - if (!isNeedToGenerateSQLToken(rewriteStatement.getOptimizedStatement(), baseRule)) { + if (!isNeedToGenerateSQLToken(rewriteStatement.getSqlStatementContext(), baseRule)) { return Optional.absent(); } return createInsertColumnsToken(rewriteStatement, baseRule); } - private boolean isNeedToGenerateSQLToken(final OptimizedStatement optimizedStatement, final BaseRule baseRule) { - Optional insertColumnsSegment = optimizedStatement.getSqlStatement().findSQLSegment(InsertColumnsSegment.class); + private boolean isNeedToGenerateSQLToken(final SQLStatementContext sqlStatementContext, final BaseRule baseRule) { + Optional insertColumnsSegment = sqlStatementContext.getSqlStatement().findSQLSegment(InsertColumnsSegment.class); return !(baseRule instanceof MasterSlaveRule) - && optimizedStatement instanceof InsertOptimizedStatement && insertColumnsSegment.isPresent() && insertColumnsSegment.get().getColumns().isEmpty(); + && sqlStatementContext instanceof InsertSQLStatementContext && insertColumnsSegment.isPresent() && insertColumnsSegment.get().getColumns().isEmpty(); } private Optional createInsertColumnsToken(final RewriteStatement rewriteStatement, final BaseRule baseRule) { - Optional insertColumnsSegment = rewriteStatement.getOptimizedStatement().getSqlStatement().findSQLSegment(InsertColumnsSegment.class); + Optional insertColumnsSegment = rewriteStatement.getSqlStatementContext().getSqlStatement().findSQLSegment(InsertColumnsSegment.class); return insertColumnsSegment.isPresent() ? Optional.of(new InsertRegularNamesToken(insertColumnsSegment.get().getStopIndex(), - getActualInsertColumns((InsertRewriteStatement) rewriteStatement, baseRule), !isNeedToAppendColumns((InsertOptimizedStatement) rewriteStatement.getOptimizedStatement(), baseRule))) + getActualInsertColumns((InsertRewriteStatement) rewriteStatement, baseRule), !isNeedToAppendColumns((InsertSQLStatementContext) rewriteStatement.getSqlStatementContext(), baseRule))) : Optional.absent(); } private Collection getActualInsertColumns(final InsertRewriteStatement rewriteStatement, final BaseRule baseRule) { Collection result = new LinkedList<>(); - Map logicAndCipherColumns = getEncryptRule(baseRule).getLogicAndCipherColumns(rewriteStatement.getOptimizedStatement().getTables().getSingleTableName()); - boolean isGeneratedKey = rewriteStatement.getOptimizedStatement() instanceof InsertOptimizedStatement + Map logicAndCipherColumns = getEncryptRule(baseRule).getLogicAndCipherColumns(rewriteStatement.getSqlStatementContext().getTablesContext().getSingleTableName()); + boolean isGeneratedKey = rewriteStatement.getSqlStatementContext() instanceof InsertSQLStatementContext && (rewriteStatement.getGeneratedKey().isPresent() && (rewriteStatement.getGeneratedKey().get().isGenerated())); - for (String each : ((InsertOptimizedStatement) rewriteStatement.getOptimizedStatement()).getColumnNames()) { + for (String each : ((InsertSQLStatementContext) rewriteStatement.getSqlStatementContext()).getColumnNames()) { if (!isGeneratedKey || !each.equalsIgnoreCase(rewriteStatement.getGeneratedKey().get().getColumnName())) { result.add(logicAndCipherColumns.keySet().contains(each) ? logicAndCipherColumns.get(each) : each); } @@ -84,16 +84,16 @@ public final class InsertRegularNamesTokenGenerator implements OptionalSQLTokenG return baseRule instanceof ShardingRule ? ((ShardingRule) baseRule).getEncryptRule() : (EncryptRule) baseRule; } - private boolean isNeedToAppendColumns(final InsertOptimizedStatement optimizedStatement, final BaseRule baseRule) { + private boolean isNeedToAppendColumns(final InsertSQLStatementContext insertSQLStatementContext, final BaseRule baseRule) { return baseRule instanceof ShardingRule - ? isNeedToAppendColumns(optimizedStatement, (ShardingRule) baseRule) : isNeedToAppendAssistedQueryAndPlainColumns(optimizedStatement, (EncryptRule) baseRule); + ? isNeedToAppendColumns(insertSQLStatementContext, (ShardingRule) baseRule) : isNeedToAppendAssistedQueryAndPlainColumns(insertSQLStatementContext, (EncryptRule) baseRule); } - private boolean isNeedToAppendColumns(final InsertOptimizedStatement optimizedStatement, final ShardingRule shardingRule) { - return isNeedToAppendAssistedQueryAndPlainColumns(optimizedStatement, shardingRule.getEncryptRule()); + private boolean isNeedToAppendColumns(final InsertSQLStatementContext insertSQLStatementContext, final ShardingRule shardingRule) { + return isNeedToAppendAssistedQueryAndPlainColumns(insertSQLStatementContext, shardingRule.getEncryptRule()); } - private boolean isNeedToAppendAssistedQueryAndPlainColumns(final OptimizedStatement optimizedStatement, final EncryptRule encryptRule) { - return !encryptRule.getAssistedQueryAndPlainColumns(optimizedStatement.getTables().getSingleTableName()).isEmpty(); + private boolean isNeedToAppendAssistedQueryAndPlainColumns(final SQLStatementContext sqlStatementContext, final EncryptRule encryptRule) { + return !encryptRule.getAssistedQueryAndPlainColumns(sqlStatementContext.getTablesContext().getSingleTableName()).isEmpty(); } } diff --git a/sharding-core/sharding-core-rewrite/src/main/java/org/apache/shardingsphere/core/rewrite/token/generator/InsertSetCipherColumnTokenGenerator.java b/sharding-core/sharding-core-rewrite/src/main/java/org/apache/shardingsphere/core/rewrite/token/generator/InsertSetCipherColumnTokenGenerator.java index 30e3c91ef6..a5e27b5dad 100644 --- a/sharding-core/sharding-core-rewrite/src/main/java/org/apache/shardingsphere/core/rewrite/token/generator/InsertSetCipherColumnTokenGenerator.java +++ b/sharding-core/sharding-core-rewrite/src/main/java/org/apache/shardingsphere/core/rewrite/token/generator/InsertSetCipherColumnTokenGenerator.java @@ -18,8 +18,8 @@ package org.apache.shardingsphere.core.rewrite.token.generator; import com.google.common.base.Optional; -import org.apache.shardingsphere.core.optimize.statement.impl.InsertOptimizedStatement; -import org.apache.shardingsphere.core.optimize.statement.OptimizedStatement; +import org.apache.shardingsphere.core.optimize.statement.impl.InsertSQLStatementContext; +import org.apache.shardingsphere.core.optimize.statement.SQLStatementContext; import org.apache.shardingsphere.core.parse.sql.segment.dml.assignment.AssignmentSegment; import org.apache.shardingsphere.core.parse.sql.segment.dml.assignment.SetAssignmentsSegment; import org.apache.shardingsphere.core.parse.sql.segment.dml.expr.ExpressionSegment; @@ -45,25 +45,25 @@ public final class InsertSetCipherColumnTokenGenerator implements CollectionSQLT @Override public Collection generateSQLTokens( final RewriteStatement rewriteStatement, final ParameterBuilder parameterBuilder, final EncryptRule encryptRule, final boolean isQueryWithCipherColumn) { - if (!isNeedToGenerateSQLToken(rewriteStatement.getOptimizedStatement())) { + if (!isNeedToGenerateSQLToken(rewriteStatement.getSqlStatementContext())) { return Collections.emptyList(); } - return createInsertSetEncryptValueTokens((InsertOptimizedStatement) rewriteStatement.getOptimizedStatement(), encryptRule); + return createInsertSetEncryptValueTokens((InsertSQLStatementContext) rewriteStatement.getSqlStatementContext(), encryptRule); } - private boolean isNeedToGenerateSQLToken(final OptimizedStatement optimizedStatement) { - Optional setAssignmentsSegment = optimizedStatement.getSqlStatement().findSQLSegment(SetAssignmentsSegment.class); - return optimizedStatement.getSqlStatement() instanceof InsertStatement && setAssignmentsSegment.isPresent(); + private boolean isNeedToGenerateSQLToken(final SQLStatementContext sqlStatementContext) { + Optional setAssignmentsSegment = sqlStatementContext.getSqlStatement().findSQLSegment(SetAssignmentsSegment.class); + return sqlStatementContext.getSqlStatement() instanceof InsertStatement && setAssignmentsSegment.isPresent(); } - private Collection createInsertSetEncryptValueTokens(final InsertOptimizedStatement optimizedStatement, final EncryptRule encryptRule) { - Optional setAssignmentsSegment = optimizedStatement.getSqlStatement().findSQLSegment(SetAssignmentsSegment.class); + private Collection createInsertSetEncryptValueTokens(final InsertSQLStatementContext insertSQLStatementContext, final EncryptRule encryptRule) { + Optional setAssignmentsSegment = insertSQLStatementContext.getSqlStatement().findSQLSegment(SetAssignmentsSegment.class); if (!setAssignmentsSegment.isPresent()) { return Collections.emptyList(); } Collection result = new LinkedList<>(); for (AssignmentSegment each : setAssignmentsSegment.get().getAssignments()) { - Optional insertSetEncryptValueToken = createInsertSetEncryptValueToken(optimizedStatement, encryptRule, each); + Optional insertSetEncryptValueToken = createInsertSetEncryptValueToken(insertSQLStatementContext, encryptRule, each); if (insertSetEncryptValueToken.isPresent()) { result.add(insertSetEncryptValueToken.get()); } @@ -71,19 +71,20 @@ public final class InsertSetCipherColumnTokenGenerator implements CollectionSQLT return result; } - private Optional createInsertSetEncryptValueToken(final InsertOptimizedStatement optimizedStatement, final EncryptRule encryptRule, final AssignmentSegment segment) { - String tableName = optimizedStatement.getTables().getSingleTableName(); + private Optional createInsertSetEncryptValueToken(final InsertSQLStatementContext insertSQLStatementContext, + final EncryptRule encryptRule, final AssignmentSegment segment) { + String tableName = insertSQLStatementContext.getTablesContext().getSingleTableName(); Optional shardingEncryptor = encryptRule.findShardingEncryptor(tableName, segment.getColumn().getName()); if (shardingEncryptor.isPresent()) { String cipherColumnName = encryptRule.getCipherColumn(tableName, segment.getColumn().getName()); - ExpressionSegment cipherValue = getCipherValue(optimizedStatement, segment); + ExpressionSegment cipherValue = getCipherValue(insertSQLStatementContext, segment); return Optional.of(new InsertSetCipherColumnToken(segment.getStartIndex(), segment.getStopIndex(), cipherColumnName, cipherValue)); } return Optional.absent(); } - private ExpressionSegment getCipherValue(final InsertOptimizedStatement optimizedStatement, final AssignmentSegment assignmentSegment) { + private ExpressionSegment getCipherValue(final InsertSQLStatementContext insertSQLStatementContext, final AssignmentSegment assignmentSegment) { return assignmentSegment.getValue() instanceof ParameterMarkerExpressionSegment ? assignmentSegment.getValue() - : optimizedStatement.getInsertValues().get(0).getValueExpressions().get(optimizedStatement.getColumnNames().indexOf(assignmentSegment.getColumn().getName())); + : insertSQLStatementContext.getInsertValueContexts().get(0).getValueExpressions().get(insertSQLStatementContext.getColumnNames().indexOf(assignmentSegment.getColumn().getName())); } } diff --git a/sharding-core/sharding-core-rewrite/src/main/java/org/apache/shardingsphere/core/rewrite/token/generator/InsertSetGeneratedKeyColumnTokenGenerator.java b/sharding-core/sharding-core-rewrite/src/main/java/org/apache/shardingsphere/core/rewrite/token/generator/InsertSetGeneratedKeyColumnTokenGenerator.java index 316657bc32..d48f66a45e 100644 --- a/sharding-core/sharding-core-rewrite/src/main/java/org/apache/shardingsphere/core/rewrite/token/generator/InsertSetGeneratedKeyColumnTokenGenerator.java +++ b/sharding-core/sharding-core-rewrite/src/main/java/org/apache/shardingsphere/core/rewrite/token/generator/InsertSetGeneratedKeyColumnTokenGenerator.java @@ -18,7 +18,7 @@ package org.apache.shardingsphere.core.rewrite.token.generator; import com.google.common.base.Optional; -import org.apache.shardingsphere.core.optimize.statement.impl.InsertOptimizedStatement; +import org.apache.shardingsphere.core.optimize.statement.impl.InsertSQLStatementContext; import org.apache.shardingsphere.core.parse.sql.segment.dml.assignment.AssignmentSegment; import org.apache.shardingsphere.core.parse.sql.segment.dml.assignment.SetAssignmentsSegment; import org.apache.shardingsphere.core.parse.sql.segment.dml.expr.ExpressionSegment; @@ -40,31 +40,32 @@ public final class InsertSetGeneratedKeyColumnTokenGenerator implements Optional @Override public Optional generateSQLToken(final RewriteStatement rewriteStatement, final ParameterBuilder parameterBuilder, final ShardingRule shardingRule, final boolean isQueryWithCipherColumn) { - Optional setAssignmentsSegment = rewriteStatement.getOptimizedStatement().getSqlStatement().findSQLSegment(SetAssignmentsSegment.class); - if (!(rewriteStatement.getOptimizedStatement() instanceof InsertOptimizedStatement && setAssignmentsSegment.isPresent())) { + Optional setAssignmentsSegment = rewriteStatement.getSqlStatementContext().getSqlStatement().findSQLSegment(SetAssignmentsSegment.class); + if (!(rewriteStatement.getSqlStatementContext() instanceof InsertSQLStatementContext && setAssignmentsSegment.isPresent())) { return Optional.absent(); } - return createInsertSetGeneratedKeyColumnToken((InsertOptimizedStatement) rewriteStatement.getOptimizedStatement(), shardingRule, setAssignmentsSegment.get()); + return createInsertSetGeneratedKeyColumnToken((InsertSQLStatementContext) rewriteStatement.getSqlStatementContext(), shardingRule, setAssignmentsSegment.get()); } - private Optional createInsertSetGeneratedKeyColumnToken(final InsertOptimizedStatement optimizedStatement, + private Optional createInsertSetGeneratedKeyColumnToken(final InsertSQLStatementContext insertSQLStatementContext, final ShardingRule shardingRule, final SetAssignmentsSegment segment) { - Optional generatedKeyColumn = getGeneratedKeyColumn(optimizedStatement, shardingRule); + Optional generatedKeyColumn = getGeneratedKeyColumn(insertSQLStatementContext, shardingRule); if (generatedKeyColumn.isPresent()) { - return Optional.of(createInsertSetGeneratedKeyColumnToken(optimizedStatement, generatedKeyColumn.get(), new ArrayList<>(segment.getAssignments()))); + return Optional.of(createInsertSetGeneratedKeyColumnToken(insertSQLStatementContext, generatedKeyColumn.get(), new ArrayList<>(segment.getAssignments()))); } return Optional.absent(); } - private InsertSetGeneratedKeyColumnToken createInsertSetGeneratedKeyColumnToken(final InsertOptimizedStatement optimizedStatement, + private InsertSetGeneratedKeyColumnToken createInsertSetGeneratedKeyColumnToken(final InsertSQLStatementContext insertSQLStatementContext, final String generatedKeyColumn, final List assignments) { - int index = optimizedStatement.getColumnNames().contains(generatedKeyColumn) ? optimizedStatement.getColumnNames().indexOf(generatedKeyColumn) : optimizedStatement.getColumnNames().size(); - ExpressionSegment expressionSegment = optimizedStatement.getInsertValues().get(0).getValueExpressions().get(index); + int index = insertSQLStatementContext.getColumnNames().contains(generatedKeyColumn) + ? insertSQLStatementContext.getColumnNames().indexOf(generatedKeyColumn) : insertSQLStatementContext.getColumnNames().size(); + ExpressionSegment expressionSegment = insertSQLStatementContext.getInsertValueContexts().get(0).getValueExpressions().get(index); return new InsertSetGeneratedKeyColumnToken(assignments.get(assignments.size() - 1).getStopIndex() + 1, generatedKeyColumn, expressionSegment); } - private Optional getGeneratedKeyColumn(final InsertOptimizedStatement optimizedStatement, final ShardingRule shardingRule) { - Optional generateKeyColumn = shardingRule.findGenerateKeyColumnName(optimizedStatement.getTables().getSingleTableName()); - return generateKeyColumn.isPresent() && !optimizedStatement.getColumnNames().contains(generateKeyColumn.get()) ? generateKeyColumn : Optional.absent(); + private Optional getGeneratedKeyColumn(final InsertSQLStatementContext insertSQLStatementContext, final ShardingRule shardingRule) { + Optional generateKeyColumn = shardingRule.findGenerateKeyColumnName(insertSQLStatementContext.getTablesContext().getSingleTableName()); + return generateKeyColumn.isPresent() && !insertSQLStatementContext.getColumnNames().contains(generateKeyColumn.get()) ? generateKeyColumn : Optional.absent(); } } diff --git a/sharding-core/sharding-core-rewrite/src/main/java/org/apache/shardingsphere/core/rewrite/token/generator/InsertSetQueryAndPlainColumnsTokenGenerator.java b/sharding-core/sharding-core-rewrite/src/main/java/org/apache/shardingsphere/core/rewrite/token/generator/InsertSetQueryAndPlainColumnsTokenGenerator.java index 6383442c44..de40c22357 100644 --- a/sharding-core/sharding-core-rewrite/src/main/java/org/apache/shardingsphere/core/rewrite/token/generator/InsertSetQueryAndPlainColumnsTokenGenerator.java +++ b/sharding-core/sharding-core-rewrite/src/main/java/org/apache/shardingsphere/core/rewrite/token/generator/InsertSetQueryAndPlainColumnsTokenGenerator.java @@ -20,8 +20,8 @@ package org.apache.shardingsphere.core.rewrite.token.generator; import com.google.common.base.Optional; import com.google.common.base.Preconditions; import org.apache.shardingsphere.core.optimize.segment.insert.expression.DerivedSimpleExpressionSegment; -import org.apache.shardingsphere.core.optimize.statement.impl.InsertOptimizedStatement; -import org.apache.shardingsphere.core.optimize.statement.OptimizedStatement; +import org.apache.shardingsphere.core.optimize.statement.impl.InsertSQLStatementContext; +import org.apache.shardingsphere.core.optimize.statement.SQLStatementContext; import org.apache.shardingsphere.core.rewrite.statement.constant.EncryptDerivedColumnType; import org.apache.shardingsphere.core.parse.sql.segment.dml.assignment.AssignmentSegment; import org.apache.shardingsphere.core.parse.sql.segment.dml.assignment.SetAssignmentsSegment; @@ -47,24 +47,24 @@ public final class InsertSetQueryAndPlainColumnsTokenGenerator implements Option @Override public Optional generateSQLToken( final RewriteStatement rewriteStatement, final ParameterBuilder parameterBuilder, final EncryptRule encryptRule, final boolean isQueryWithCipherColumn) { - if (!isNeedToGenerateSQLToken(rewriteStatement.getOptimizedStatement())) { + if (!isNeedToGenerateSQLToken(rewriteStatement.getSqlStatementContext())) { return Optional.absent(); } - return createInsertSetAddItemsToken((InsertOptimizedStatement) rewriteStatement.getOptimizedStatement(), encryptRule); + return createInsertSetAddItemsToken((InsertSQLStatementContext) rewriteStatement.getSqlStatementContext(), encryptRule); } - private boolean isNeedToGenerateSQLToken(final OptimizedStatement optimizedStatement) { - Optional setAssignmentsSegment = optimizedStatement.getSqlStatement().findSQLSegment(SetAssignmentsSegment.class); - return optimizedStatement instanceof InsertOptimizedStatement && setAssignmentsSegment.isPresent(); + private boolean isNeedToGenerateSQLToken(final SQLStatementContext sqlStatementContext) { + Optional setAssignmentsSegment = sqlStatementContext.getSqlStatement().findSQLSegment(SetAssignmentsSegment.class); + return sqlStatementContext instanceof InsertSQLStatementContext && setAssignmentsSegment.isPresent(); } - private Optional createInsertSetAddItemsToken(final InsertOptimizedStatement optimizedStatement, final EncryptRule encryptRule) { - String tableName = optimizedStatement.getTables().getSingleTableName(); + private Optional createInsertSetAddItemsToken(final InsertSQLStatementContext insertSQLStatementContext, final EncryptRule encryptRule) { + String tableName = insertSQLStatementContext.getTablesContext().getSingleTableName(); if (encryptRule.getAssistedQueryAndPlainColumns(tableName).isEmpty()) { return Optional.absent(); } List encryptDerivedColumnNames = getEncryptDerivedColumnNames(tableName, encryptRule); - return Optional.of(new InsertSetQueryAndPlainColumnsToken(getStartIndex(optimizedStatement), encryptDerivedColumnNames, getEncryptDerivedValues(optimizedStatement))); + return Optional.of(new InsertSetQueryAndPlainColumnsToken(getStartIndex(insertSQLStatementContext), encryptDerivedColumnNames, getEncryptDerivedValues(insertSQLStatementContext))); } private List getEncryptDerivedColumnNames(final String tableName, final EncryptRule encryptRule) { @@ -86,16 +86,16 @@ public final class InsertSetQueryAndPlainColumnsTokenGenerator implements Option return result; } - private int getStartIndex(final InsertOptimizedStatement optimizedStatement) { - Optional setAssignmentsSegment = optimizedStatement.getSqlStatement().findSQLSegment(SetAssignmentsSegment.class); + private int getStartIndex(final InsertSQLStatementContext insertSQLStatementContext) { + Optional setAssignmentsSegment = insertSQLStatementContext.getSqlStatement().findSQLSegment(SetAssignmentsSegment.class); Preconditions.checkState(setAssignmentsSegment.isPresent()); List assignments = new ArrayList<>(setAssignmentsSegment.get().getAssignments()); return assignments.get(assignments.size() - 1).getStopIndex() + 1; } - private List getEncryptDerivedValues(final InsertOptimizedStatement optimizedStatement) { + private List getEncryptDerivedValues(final InsertSQLStatementContext insertSQLStatementContext) { List result = new LinkedList<>(); - for (ExpressionSegment each : optimizedStatement.getInsertValues().get(0).getValueExpressions()) { + for (ExpressionSegment each : insertSQLStatementContext.getInsertValueContexts().get(0).getValueExpressions()) { if (each instanceof DerivedSimpleExpressionSegment && EncryptDerivedColumnType.ENCRYPT.equals(((DerivedSimpleExpressionSegment) each).getType())) { result.add(each); } diff --git a/sharding-core/sharding-core-rewrite/src/main/java/org/apache/shardingsphere/core/rewrite/token/generator/InsertValuesTokenGenerator.java b/sharding-core/sharding-core-rewrite/src/main/java/org/apache/shardingsphere/core/rewrite/token/generator/InsertValuesTokenGenerator.java index 0e7781573b..c4684176c1 100644 --- a/sharding-core/sharding-core-rewrite/src/main/java/org/apache/shardingsphere/core/rewrite/token/generator/InsertValuesTokenGenerator.java +++ b/sharding-core/sharding-core-rewrite/src/main/java/org/apache/shardingsphere/core/rewrite/token/generator/InsertValuesTokenGenerator.java @@ -18,9 +18,9 @@ package org.apache.shardingsphere.core.rewrite.token.generator; import com.google.common.base.Optional; -import org.apache.shardingsphere.core.optimize.segment.insert.InsertValue; -import org.apache.shardingsphere.core.optimize.statement.impl.InsertOptimizedStatement; -import org.apache.shardingsphere.core.optimize.statement.OptimizedStatement; +import org.apache.shardingsphere.core.optimize.segment.insert.InsertValueContext; +import org.apache.shardingsphere.core.optimize.statement.impl.InsertSQLStatementContext; +import org.apache.shardingsphere.core.optimize.statement.SQLStatementContext; import org.apache.shardingsphere.core.parse.sql.segment.dml.assignment.InsertValuesSegment; import org.apache.shardingsphere.core.parse.sql.statement.dml.InsertStatement; import org.apache.shardingsphere.core.rewrite.builder.parameter.ParameterBuilder; @@ -44,22 +44,22 @@ public final class InsertValuesTokenGenerator implements OptionalSQLTokenGenerat @Override public Optional generateSQLToken(final RewriteStatement rewriteStatement, final ParameterBuilder parameterBuilder, final EncryptRule encryptRule, final boolean isQueryWithCipherColumn) { - Collection insertValuesSegments = rewriteStatement.getOptimizedStatement().getSqlStatement().findSQLSegments(InsertValuesSegment.class); - return isNeedToGenerateSQLToken(rewriteStatement.getOptimizedStatement(), insertValuesSegments) + Collection insertValuesSegments = rewriteStatement.getSqlStatementContext().getSqlStatement().findSQLSegments(InsertValuesSegment.class); + return isNeedToGenerateSQLToken(rewriteStatement.getSqlStatementContext(), insertValuesSegments) ? Optional.of(createInsertValuesToken(rewriteStatement, insertValuesSegments)) : Optional.absent(); } - private boolean isNeedToGenerateSQLToken(final OptimizedStatement optimizedStatement, final Collection insertValuesSegments) { - return optimizedStatement.getSqlStatement() instanceof InsertStatement && !insertValuesSegments.isEmpty(); + private boolean isNeedToGenerateSQLToken(final SQLStatementContext sqlStatementContext, final Collection insertValuesSegments) { + return sqlStatementContext.getSqlStatement() instanceof InsertStatement && !insertValuesSegments.isEmpty(); } private InsertValuesToken createInsertValuesToken(final RewriteStatement rewriteStatement, final Collection insertValuesSegments) { InsertValuesToken result = new InsertValuesToken(getStartIndex(insertValuesSegments), getStopIndex(insertValuesSegments)); Iterator shardingConditions = null; - if (rewriteStatement.getOptimizedStatement() instanceof InsertOptimizedStatement) { + if (rewriteStatement.getSqlStatementContext() instanceof InsertSQLStatementContext) { shardingConditions = rewriteStatement.getShardingConditions().getConditions().isEmpty() ? null : rewriteStatement.getShardingConditions().getConditions().iterator(); } - for (InsertValue each : ((InsertOptimizedStatement) rewriteStatement.getOptimizedStatement()).getInsertValues()) { + for (InsertValueContext each : ((InsertSQLStatementContext) rewriteStatement.getSqlStatementContext()).getInsertValueContexts()) { Collection dataNodes = null == shardingConditions ? Collections.emptyList() : shardingConditions.next().getDataNodes(); result.addInsertValueToken(each.getValueExpressions(), dataNodes); } diff --git a/sharding-core/sharding-core-rewrite/src/main/java/org/apache/shardingsphere/core/rewrite/token/generator/OffsetTokenGenerator.java b/sharding-core/sharding-core-rewrite/src/main/java/org/apache/shardingsphere/core/rewrite/token/generator/OffsetTokenGenerator.java index fd3e7fd7ee..f974b16609 100644 --- a/sharding-core/sharding-core-rewrite/src/main/java/org/apache/shardingsphere/core/rewrite/token/generator/OffsetTokenGenerator.java +++ b/sharding-core/sharding-core-rewrite/src/main/java/org/apache/shardingsphere/core/rewrite/token/generator/OffsetTokenGenerator.java @@ -18,8 +18,8 @@ package org.apache.shardingsphere.core.rewrite.token.generator; import com.google.common.base.Optional; -import org.apache.shardingsphere.core.optimize.segment.select.pagination.Pagination; -import org.apache.shardingsphere.core.optimize.statement.impl.SelectOptimizedStatement; +import org.apache.shardingsphere.core.optimize.segment.select.pagination.PaginationContext; +import org.apache.shardingsphere.core.optimize.statement.impl.SelectSQLStatementContext; import org.apache.shardingsphere.core.parse.sql.segment.dml.pagination.NumberLiteralPaginationValueSegment; import org.apache.shardingsphere.core.rewrite.builder.parameter.ParameterBuilder; import org.apache.shardingsphere.core.rewrite.statement.RewriteStatement; @@ -36,10 +36,10 @@ public final class OffsetTokenGenerator implements OptionalSQLTokenGenerator generateSQLToken( final RewriteStatement rewriteStatement, final ParameterBuilder parameterBuilder, final ShardingRule shardingRule, final boolean isQueryWithCipherColumn) { - if (!(rewriteStatement.getOptimizedStatement() instanceof SelectOptimizedStatement)) { + if (!(rewriteStatement.getSqlStatementContext() instanceof SelectSQLStatementContext)) { return Optional.absent(); } - Pagination pagination = ((SelectOptimizedStatement) rewriteStatement.getOptimizedStatement()).getPagination(); + PaginationContext pagination = ((SelectSQLStatementContext) rewriteStatement.getSqlStatementContext()).getPaginationContext(); return pagination.getOffsetSegment().isPresent() && pagination.getOffsetSegment().get() instanceof NumberLiteralPaginationValueSegment ? Optional.of(new OffsetToken(pagination.getOffsetSegment().get().getStartIndex(), pagination.getOffsetSegment().get().getStopIndex(), pagination.getRevisedOffset())) : Optional.absent(); diff --git a/sharding-core/sharding-core-rewrite/src/main/java/org/apache/shardingsphere/core/rewrite/token/generator/OrderByTokenGenerator.java b/sharding-core/sharding-core-rewrite/src/main/java/org/apache/shardingsphere/core/rewrite/token/generator/OrderByTokenGenerator.java index efdbd7397d..7854cb4423 100644 --- a/sharding-core/sharding-core-rewrite/src/main/java/org/apache/shardingsphere/core/rewrite/token/generator/OrderByTokenGenerator.java +++ b/sharding-core/sharding-core-rewrite/src/main/java/org/apache/shardingsphere/core/rewrite/token/generator/OrderByTokenGenerator.java @@ -19,7 +19,7 @@ package org.apache.shardingsphere.core.rewrite.token.generator; import com.google.common.base.Optional; import org.apache.shardingsphere.core.optimize.segment.select.orderby.OrderByItem; -import org.apache.shardingsphere.core.optimize.statement.impl.SelectOptimizedStatement; +import org.apache.shardingsphere.core.optimize.statement.impl.SelectSQLStatementContext; import org.apache.shardingsphere.core.parse.core.constant.QuoteCharacter; import org.apache.shardingsphere.core.parse.sql.segment.dml.order.item.ColumnOrderByItemSegment; import org.apache.shardingsphere.core.parse.sql.segment.dml.order.item.ExpressionOrderByItemSegment; @@ -38,19 +38,19 @@ public final class OrderByTokenGenerator implements OptionalSQLTokenGenerator generateSQLToken( final RewriteStatement rewriteStatement, final ParameterBuilder parameterBuilder, final ShardingRule shardingRule, final boolean isQueryWithCipherColumn) { - if (!(rewriteStatement.getOptimizedStatement() instanceof SelectOptimizedStatement)) { + if (!(rewriteStatement.getSqlStatementContext() instanceof SelectSQLStatementContext)) { return Optional.absent(); } - if (((SelectOptimizedStatement) rewriteStatement.getOptimizedStatement()).getOrderBy().isGenerated()) { - return Optional.of(createOrderByToken((SelectOptimizedStatement) rewriteStatement.getOptimizedStatement())); + if (((SelectSQLStatementContext) rewriteStatement.getSqlStatementContext()).getOrderByContext().isGenerated()) { + return Optional.of(createOrderByToken((SelectSQLStatementContext) rewriteStatement.getSqlStatementContext())); } return Optional.absent(); } - private OrderByToken createOrderByToken(final SelectOptimizedStatement optimizedStatement) { - OrderByToken result = new OrderByToken(optimizedStatement.getGroupBy().getLastIndex() + 1); + private OrderByToken createOrderByToken(final SelectSQLStatementContext selectSQLStatementContext) { + OrderByToken result = new OrderByToken(selectSQLStatementContext.getGroupByContext().getLastIndex() + 1); String columnLabel; - for (OrderByItem each : optimizedStatement.getOrderBy().getItems()) { + for (OrderByItem each : selectSQLStatementContext.getOrderByContext().getItems()) { if (each.getSegment() instanceof ColumnOrderByItemSegment) { ColumnOrderByItemSegment columnOrderByItemSegment = (ColumnOrderByItemSegment) each.getSegment(); QuoteCharacter quoteCharacter = columnOrderByItemSegment.getColumn().getQuoteCharacter(); diff --git a/sharding-core/sharding-core-rewrite/src/main/java/org/apache/shardingsphere/core/rewrite/token/generator/RemoveTokenGenerator.java b/sharding-core/sharding-core-rewrite/src/main/java/org/apache/shardingsphere/core/rewrite/token/generator/RemoveTokenGenerator.java index da8f8b15b7..db6b099b15 100644 --- a/sharding-core/sharding-core-rewrite/src/main/java/org/apache/shardingsphere/core/rewrite/token/generator/RemoveTokenGenerator.java +++ b/sharding-core/sharding-core-rewrite/src/main/java/org/apache/shardingsphere/core/rewrite/token/generator/RemoveTokenGenerator.java @@ -38,7 +38,7 @@ public final class RemoveTokenGenerator implements CollectionSQLTokenGenerator generateSQLTokens( final RewriteStatement rewriteStatement, final ParameterBuilder parameterBuilder, final BaseRule baseRule, final boolean isQueryWithCipherColumn) { Collection result = new LinkedList<>(); - for (SQLSegment each : rewriteStatement.getOptimizedStatement().getSqlStatement().getAllSQLSegments()) { + for (SQLSegment each : rewriteStatement.getSqlStatementContext().getSqlStatement().getAllSQLSegments()) { if (each instanceof RemoveAvailable) { result.add(new RemoveToken(each.getStartIndex(), each.getStopIndex())); } diff --git a/sharding-core/sharding-core-rewrite/src/main/java/org/apache/shardingsphere/core/rewrite/token/generator/RowCountTokenGenerator.java b/sharding-core/sharding-core-rewrite/src/main/java/org/apache/shardingsphere/core/rewrite/token/generator/RowCountTokenGenerator.java index 8bf992e315..61d27237f3 100644 --- a/sharding-core/sharding-core-rewrite/src/main/java/org/apache/shardingsphere/core/rewrite/token/generator/RowCountTokenGenerator.java +++ b/sharding-core/sharding-core-rewrite/src/main/java/org/apache/shardingsphere/core/rewrite/token/generator/RowCountTokenGenerator.java @@ -18,8 +18,8 @@ package org.apache.shardingsphere.core.rewrite.token.generator; import com.google.common.base.Optional; -import org.apache.shardingsphere.core.optimize.segment.select.pagination.Pagination; -import org.apache.shardingsphere.core.optimize.statement.impl.SelectOptimizedStatement; +import org.apache.shardingsphere.core.optimize.segment.select.pagination.PaginationContext; +import org.apache.shardingsphere.core.optimize.statement.impl.SelectSQLStatementContext; import org.apache.shardingsphere.core.parse.sql.segment.dml.pagination.NumberLiteralPaginationValueSegment; import org.apache.shardingsphere.core.rewrite.builder.parameter.ParameterBuilder; import org.apache.shardingsphere.core.rewrite.statement.RewriteStatement; @@ -36,12 +36,12 @@ public final class RowCountTokenGenerator implements OptionalSQLTokenGenerator generateSQLToken( final RewriteStatement rewriteStatement, final ParameterBuilder parameterBuilder, final ShardingRule shardingRule, final boolean isQueryWithCipherColumn) { - if (!(rewriteStatement.getOptimizedStatement() instanceof SelectOptimizedStatement)) { + if (!(rewriteStatement.getSqlStatementContext() instanceof SelectSQLStatementContext)) { return Optional.absent(); } - Pagination pagination = ((SelectOptimizedStatement) rewriteStatement.getOptimizedStatement()).getPagination(); + PaginationContext pagination = ((SelectSQLStatementContext) rewriteStatement.getSqlStatementContext()).getPaginationContext(); return pagination.getRowCountSegment().isPresent() && pagination.getRowCountSegment().get() instanceof NumberLiteralPaginationValueSegment ? Optional.of(new RowCountToken(pagination.getRowCountSegment().get().getStartIndex(), pagination.getRowCountSegment().get().getStopIndex(), - pagination.getRevisedRowCount((SelectOptimizedStatement) rewriteStatement.getOptimizedStatement()))) : Optional.absent(); + pagination.getRevisedRowCount((SelectSQLStatementContext) rewriteStatement.getSqlStatementContext()))) : Optional.absent(); } } diff --git a/sharding-core/sharding-core-rewrite/src/main/java/org/apache/shardingsphere/core/rewrite/token/generator/SelectEncryptItemTokenGenerator.java b/sharding-core/sharding-core-rewrite/src/main/java/org/apache/shardingsphere/core/rewrite/token/generator/SelectEncryptItemTokenGenerator.java index 8231771de2..491425fea0 100644 --- a/sharding-core/sharding-core-rewrite/src/main/java/org/apache/shardingsphere/core/rewrite/token/generator/SelectEncryptItemTokenGenerator.java +++ b/sharding-core/sharding-core-rewrite/src/main/java/org/apache/shardingsphere/core/rewrite/token/generator/SelectEncryptItemTokenGenerator.java @@ -18,7 +18,7 @@ package org.apache.shardingsphere.core.rewrite.token.generator; import com.google.common.base.Optional; -import org.apache.shardingsphere.core.optimize.statement.OptimizedStatement; +import org.apache.shardingsphere.core.optimize.statement.SQLStatementContext; import org.apache.shardingsphere.core.parse.sql.segment.dml.item.ColumnSelectItemSegment; import org.apache.shardingsphere.core.parse.sql.segment.dml.item.SelectItemSegment; import org.apache.shardingsphere.core.parse.sql.segment.dml.item.SelectItemsSegment; @@ -44,31 +44,31 @@ public final class SelectEncryptItemTokenGenerator implements CollectionSQLToken @Override public Collection generateSQLTokens(final RewriteStatement rewriteStatement, final ParameterBuilder parameterBuilder, final EncryptRule rule, final boolean isQueryWithCipherColumn) { - if (!isNeedToGenerateSQLToken(rewriteStatement.getOptimizedStatement())) { + if (!isNeedToGenerateSQLToken(rewriteStatement.getSqlStatementContext())) { return Collections.emptyList(); } - return createSelectCipherItemTokens(rule, rewriteStatement.getOptimizedStatement(), isQueryWithCipherColumn); + return createSelectCipherItemTokens(rule, rewriteStatement.getSqlStatementContext(), isQueryWithCipherColumn); } - private boolean isNeedToGenerateSQLToken(final OptimizedStatement optimizedStatement) { - if (!isSelectStatementWithTable(optimizedStatement)) { + private boolean isNeedToGenerateSQLToken(final SQLStatementContext sqlStatementContext) { + if (!isSelectStatementWithTable(sqlStatementContext)) { return false; } - Optional selectItemsSegment = optimizedStatement.getSqlStatement().findSQLSegment(SelectItemsSegment.class); + Optional selectItemsSegment = sqlStatementContext.getSqlStatement().findSQLSegment(SelectItemsSegment.class); return selectItemsSegment.isPresent() && !selectItemsSegment.get().getSelectItems().isEmpty(); } - private boolean isSelectStatementWithTable(final OptimizedStatement optimizedStatement) { - return optimizedStatement.getSqlStatement() instanceof SelectStatement && !optimizedStatement.getTables().isEmpty(); + private boolean isSelectStatementWithTable(final SQLStatementContext sqlStatementContext) { + return sqlStatementContext.getSqlStatement() instanceof SelectStatement && !sqlStatementContext.getTablesContext().isEmpty(); } - private Collection createSelectCipherItemTokens(final EncryptRule encryptRule, final OptimizedStatement optimizedStatement, final boolean isQueryWithCipherColumn) { + private Collection createSelectCipherItemTokens(final EncryptRule encryptRule, final SQLStatementContext sqlStatementContext, final boolean isQueryWithCipherColumn) { Collection result = new LinkedList<>(); - Optional selectItemsSegment = optimizedStatement.getSqlStatement().findSQLSegment(SelectItemsSegment.class); + Optional selectItemsSegment = sqlStatementContext.getSqlStatement().findSQLSegment(SelectItemsSegment.class); if (!selectItemsSegment.isPresent()) { return Collections.emptyList(); } - String tableName = optimizedStatement.getTables().getSingleTableName(); + String tableName = sqlStatementContext.getTablesContext().getSingleTableName(); Optional encryptTable = encryptRule.findEncryptTable(tableName); if (!encryptTable.isPresent()) { return Collections.emptyList(); diff --git a/sharding-core/sharding-core-rewrite/src/main/java/org/apache/shardingsphere/core/rewrite/token/generator/SelectItemPrefixTokenGenerator.java b/sharding-core/sharding-core-rewrite/src/main/java/org/apache/shardingsphere/core/rewrite/token/generator/SelectItemPrefixTokenGenerator.java index d5c6490fb9..861a4d2e6f 100644 --- a/sharding-core/sharding-core-rewrite/src/main/java/org/apache/shardingsphere/core/rewrite/token/generator/SelectItemPrefixTokenGenerator.java +++ b/sharding-core/sharding-core-rewrite/src/main/java/org/apache/shardingsphere/core/rewrite/token/generator/SelectItemPrefixTokenGenerator.java @@ -18,8 +18,8 @@ package org.apache.shardingsphere.core.rewrite.token.generator; import com.google.common.base.Optional; -import org.apache.shardingsphere.core.optimize.segment.select.item.SelectItems; -import org.apache.shardingsphere.core.optimize.statement.impl.SelectOptimizedStatement; +import org.apache.shardingsphere.core.optimize.segment.select.item.SelectItemsContext; +import org.apache.shardingsphere.core.optimize.statement.impl.SelectSQLStatementContext; import org.apache.shardingsphere.core.rewrite.builder.parameter.ParameterBuilder; import org.apache.shardingsphere.core.rewrite.statement.RewriteStatement; import org.apache.shardingsphere.core.rewrite.token.pojo.SelectItemPrefixToken; @@ -35,10 +35,10 @@ public final class SelectItemPrefixTokenGenerator implements OptionalSQLTokenGen @Override public Optional generateSQLToken( final RewriteStatement rewriteStatement, final ParameterBuilder parameterBuilder, final ShardingRule shardingRule, final boolean isQueryWithCipherColumn) { - if (!(rewriteStatement.getOptimizedStatement() instanceof SelectOptimizedStatement)) { + if (!(rewriteStatement.getSqlStatementContext() instanceof SelectSQLStatementContext)) { return Optional.absent(); } - SelectItems selectItems = ((SelectOptimizedStatement) rewriteStatement.getOptimizedStatement()).getSelectItems(); + SelectItemsContext selectItems = ((SelectSQLStatementContext) rewriteStatement.getSqlStatementContext()).getSelectItemsContext(); return selectItems.getAggregationDistinctSelectItems().isEmpty() ? Optional.absent() : Optional.of(new SelectItemPrefixToken(selectItems.getStartIndex())); } } diff --git a/sharding-core/sharding-core-rewrite/src/main/java/org/apache/shardingsphere/core/rewrite/token/generator/SelectItemsTokenGenerator.java b/sharding-core/sharding-core-rewrite/src/main/java/org/apache/shardingsphere/core/rewrite/token/generator/SelectItemsTokenGenerator.java index 81dd5d56e0..6db3de7108 100644 --- a/sharding-core/sharding-core-rewrite/src/main/java/org/apache/shardingsphere/core/rewrite/token/generator/SelectItemsTokenGenerator.java +++ b/sharding-core/sharding-core-rewrite/src/main/java/org/apache/shardingsphere/core/rewrite/token/generator/SelectItemsTokenGenerator.java @@ -25,7 +25,7 @@ import org.apache.shardingsphere.core.optimize.segment.select.item.impl.Aggregat import org.apache.shardingsphere.core.optimize.segment.select.item.impl.AggregationSelectItem; import org.apache.shardingsphere.core.optimize.segment.select.item.impl.DerivedSelectItem; import org.apache.shardingsphere.core.optimize.segment.select.item.SelectItem; -import org.apache.shardingsphere.core.optimize.statement.impl.SelectOptimizedStatement; +import org.apache.shardingsphere.core.optimize.statement.impl.SelectSQLStatementContext; import org.apache.shardingsphere.core.rewrite.builder.parameter.ParameterBuilder; import org.apache.shardingsphere.core.rewrite.statement.RewriteStatement; import org.apache.shardingsphere.core.rewrite.token.pojo.SelectItemsToken; @@ -44,18 +44,18 @@ public final class SelectItemsTokenGenerator implements OptionalSQLTokenGenerato @Override public Optional generateSQLToken( final RewriteStatement rewriteStatement, final ParameterBuilder parameterBuilder, final ShardingRule shardingRule, final boolean isQueryWithCipherColumn) { - if (!(rewriteStatement.getOptimizedStatement() instanceof SelectOptimizedStatement)) { + if (!(rewriteStatement.getSqlStatementContext() instanceof SelectSQLStatementContext)) { return Optional.absent(); } - Collection derivedItemTexts = getDerivedItemTexts((SelectOptimizedStatement) rewriteStatement.getOptimizedStatement()); + Collection derivedItemTexts = getDerivedItemTexts((SelectSQLStatementContext) rewriteStatement.getSqlStatementContext()); return derivedItemTexts.isEmpty() ? Optional.absent() : Optional.of( - new SelectItemsToken(((SelectOptimizedStatement) rewriteStatement.getOptimizedStatement()).getSelectItems().getStopIndex() + 1 + " ".length(), derivedItemTexts)); + new SelectItemsToken(((SelectSQLStatementContext) rewriteStatement.getSqlStatementContext()).getSelectItemsContext().getStopIndex() + 1 + " ".length(), derivedItemTexts)); } - private Collection getDerivedItemTexts(final SelectOptimizedStatement optimizedStatement) { + private Collection getDerivedItemTexts(final SelectSQLStatementContext selectSQLStatementContext) { Collection result = new LinkedList<>(); - for (SelectItem each : optimizedStatement.getSelectItems().getItems()) { + for (SelectItem each : selectSQLStatementContext.getSelectItemsContext().getItems()) { if (each instanceof AggregationSelectItem && !((AggregationSelectItem) each).getDerivedAggregationItems().isEmpty()) { result.addAll(Lists.transform(((AggregationSelectItem) each).getDerivedAggregationItems(), new Function() { diff --git a/sharding-core/sharding-core-rewrite/src/main/java/org/apache/shardingsphere/core/rewrite/token/generator/TableTokenGenerator.java b/sharding-core/sharding-core-rewrite/src/main/java/org/apache/shardingsphere/core/rewrite/token/generator/TableTokenGenerator.java index ea8529a1d6..d7efd94763 100644 --- a/sharding-core/sharding-core-rewrite/src/main/java/org/apache/shardingsphere/core/rewrite/token/generator/TableTokenGenerator.java +++ b/sharding-core/sharding-core-rewrite/src/main/java/org/apache/shardingsphere/core/rewrite/token/generator/TableTokenGenerator.java @@ -18,8 +18,8 @@ package org.apache.shardingsphere.core.rewrite.token.generator; import com.google.common.base.Optional; -import org.apache.shardingsphere.core.optimize.segment.Table; -import org.apache.shardingsphere.core.optimize.statement.OptimizedStatement; +import org.apache.shardingsphere.core.optimize.segment.table.Table; +import org.apache.shardingsphere.core.optimize.statement.SQLStatementContext; import org.apache.shardingsphere.core.parse.sql.segment.SQLSegment; import org.apache.shardingsphere.core.parse.sql.segment.dml.column.ColumnSegment; import org.apache.shardingsphere.core.parse.sql.segment.dml.item.SelectItemSegment; @@ -52,16 +52,16 @@ public final class TableTokenGenerator implements CollectionSQLTokenGenerator generateSQLTokens( final RewriteStatement rewriteStatement, final ParameterBuilder parameterBuilder, final BaseRule baseRule, final boolean isQueryWithCipherColumn) { Collection result = new LinkedList<>(); - for (SQLSegment each : rewriteStatement.getOptimizedStatement().getSqlStatement().getAllSQLSegments()) { + for (SQLSegment each : rewriteStatement.getSqlStatementContext().getSqlStatement().getAllSQLSegments()) { if (each instanceof SelectItemsSegment) { - result.addAll(createTableTokens(rewriteStatement.getOptimizedStatement(), baseRule, (SelectItemsSegment) each)); + result.addAll(createTableTokens(rewriteStatement.getSqlStatementContext(), baseRule, (SelectItemsSegment) each)); } else if (each instanceof ColumnSegment) { - Optional tableToken = createTableToken(rewriteStatement.getOptimizedStatement(), baseRule, (ColumnSegment) each); + Optional tableToken = createTableToken(rewriteStatement.getSqlStatementContext(), baseRule, (ColumnSegment) each); if (tableToken.isPresent()) { result.add(tableToken.get()); } } else if (each instanceof TableAvailable) { - Optional tableToken = createTableToken(rewriteStatement.getOptimizedStatement().getSqlStatement(), baseRule, (TableAvailable) each); + Optional tableToken = createTableToken(rewriteStatement.getSqlStatementContext().getSqlStatement(), baseRule, (TableAvailable) each); if (tableToken.isPresent()) { result.add(tableToken.get()); } @@ -70,11 +70,11 @@ public final class TableTokenGenerator implements CollectionSQLTokenGenerator createTableTokens(final OptimizedStatement optimizedStatement, final BaseRule baseRule, final SelectItemsSegment selectItemsSegment) { + private Collection createTableTokens(final SQLStatementContext sqlStatementContext, final BaseRule baseRule, final SelectItemsSegment selectItemsSegment) { Collection result = new LinkedList<>(); for (SelectItemSegment each : selectItemsSegment.getSelectItems()) { if (each instanceof ShorthandSelectItemSegment) { - Optional tableToken = createTableToken(optimizedStatement, baseRule, (ShorthandSelectItemSegment) each); + Optional tableToken = createTableToken(sqlStatementContext, baseRule, (ShorthandSelectItemSegment) each); if (tableToken.isPresent()) { result.add(tableToken.get()); } @@ -83,12 +83,12 @@ public final class TableTokenGenerator implements CollectionSQLTokenGenerator createTableToken(final OptimizedStatement optimizedStatement, final BaseRule baseRule, final OwnerAvailable segment) { + private Optional createTableToken(final SQLStatementContext sqlStatementContext, final BaseRule baseRule, final OwnerAvailable segment) { Optional owner = segment.getOwner(); if (!owner.isPresent()) { return Optional.absent(); } - if (isToGenerateTableToken(optimizedStatement, baseRule, owner.get())) { + if (isToGenerateTableToken(sqlStatementContext, baseRule, owner.get())) { return Optional.of(new TableToken(owner.get().getStartIndex(), owner.get().getStopIndex(), owner.get().getTableName(), owner.get().getQuoteCharacter())); } return Optional.absent(); @@ -101,9 +101,9 @@ public final class TableTokenGenerator implements CollectionSQLTokenGenerator table = optimizedStatement.getTables().find(tableSegment.getTableName()); + Optional
table = sqlStatementContext.getTablesContext().find(tableSegment.getTableName()); return table.isPresent() && !table.get().getAlias().isPresent() && ((ShardingRule) baseRule).findTableRule(table.get().getName()).isPresent(); } return baseRule instanceof MasterSlaveRule; diff --git a/sharding-core/sharding-core-rewrite/src/main/java/org/apache/shardingsphere/core/rewrite/token/generator/UpdateEncryptColumnTokenGenerator.java b/sharding-core/sharding-core-rewrite/src/main/java/org/apache/shardingsphere/core/rewrite/token/generator/UpdateEncryptColumnTokenGenerator.java index 2e6422c281..e8cfca4d77 100644 --- a/sharding-core/sharding-core-rewrite/src/main/java/org/apache/shardingsphere/core/rewrite/token/generator/UpdateEncryptColumnTokenGenerator.java +++ b/sharding-core/sharding-core-rewrite/src/main/java/org/apache/shardingsphere/core/rewrite/token/generator/UpdateEncryptColumnTokenGenerator.java @@ -18,7 +18,7 @@ package org.apache.shardingsphere.core.rewrite.token.generator; import com.google.common.base.Optional; -import org.apache.shardingsphere.core.optimize.statement.OptimizedStatement; +import org.apache.shardingsphere.core.optimize.statement.SQLStatementContext; import org.apache.shardingsphere.core.parse.sql.segment.dml.assignment.AssignmentSegment; import org.apache.shardingsphere.core.parse.sql.segment.dml.expr.simple.LiteralExpressionSegment; import org.apache.shardingsphere.core.parse.sql.segment.dml.expr.simple.ParameterMarkerExpressionSegment; @@ -45,14 +45,14 @@ public final class UpdateEncryptColumnTokenGenerator implements CollectionSQLTok @Override public Collection generateSQLTokens( final RewriteStatement rewriteStatement, final ParameterBuilder parameterBuilder, final EncryptRule encryptRule, final boolean isQueryWithCipherColumn) { - return rewriteStatement.getOptimizedStatement().getSqlStatement() instanceof UpdateStatement - ? createUpdateEncryptColumnTokens(parameterBuilder, encryptRule, rewriteStatement.getOptimizedStatement()) : Collections.emptyList(); + return rewriteStatement.getSqlStatementContext().getSqlStatement() instanceof UpdateStatement + ? createUpdateEncryptColumnTokens(parameterBuilder, encryptRule, rewriteStatement.getSqlStatementContext()) : Collections.emptyList(); } - private Collection createUpdateEncryptColumnTokens(final ParameterBuilder parameterBuilder, final EncryptRule encryptRule, final OptimizedStatement optimizedStatement) { + private Collection createUpdateEncryptColumnTokens(final ParameterBuilder parameterBuilder, final EncryptRule encryptRule, final SQLStatementContext sqlStatementContext) { Collection result = new LinkedList<>(); - String tableName = optimizedStatement.getTables().getSingleTableName(); - for (AssignmentSegment each : ((UpdateStatement) optimizedStatement.getSqlStatement()).getSetAssignment().getAssignments()) { + String tableName = sqlStatementContext.getTablesContext().getSingleTableName(); + for (AssignmentSegment each : ((UpdateStatement) sqlStatementContext.getSqlStatement()).getSetAssignment().getAssignments()) { if (encryptRule.findShardingEncryptor(tableName, each.getColumn().getName()).isPresent()) { result.add(createUpdateEncryptColumnToken(parameterBuilder, encryptRule, tableName, each)); } diff --git a/sharding-core/sharding-core-rewrite/src/test/java/org/apache/shardingsphere/core/rewrite/builder/parameter/standard/StandardParameterBuilderTest.java b/sharding-core/sharding-core-rewrite/src/test/java/org/apache/shardingsphere/core/rewrite/builder/parameter/standard/StandardParameterBuilderTest.java index ae7bc7d3fd..19300b2603 100644 --- a/sharding-core/sharding-core-rewrite/src/test/java/org/apache/shardingsphere/core/rewrite/builder/parameter/standard/StandardParameterBuilderTest.java +++ b/sharding-core/sharding-core-rewrite/src/test/java/org/apache/shardingsphere/core/rewrite/builder/parameter/standard/StandardParameterBuilderTest.java @@ -18,9 +18,9 @@ package org.apache.shardingsphere.core.rewrite.builder.parameter.standard; import com.google.common.base.Optional; -import org.apache.shardingsphere.core.optimize.statement.OptimizedStatement; -import org.apache.shardingsphere.core.optimize.segment.select.pagination.Pagination; -import org.apache.shardingsphere.core.optimize.statement.impl.SelectOptimizedStatement; +import org.apache.shardingsphere.core.optimize.statement.SQLStatementContext; +import org.apache.shardingsphere.core.optimize.segment.select.pagination.PaginationContext; +import org.apache.shardingsphere.core.optimize.statement.impl.SelectSQLStatementContext; import org.apache.shardingsphere.core.route.SQLRouteResult; import org.apache.shardingsphere.core.route.router.sharding.condition.ShardingCondition; import org.apache.shardingsphere.core.route.router.sharding.condition.ShardingConditions; @@ -51,15 +51,15 @@ public final class StandardParameterBuilderTest { } private SQLRouteResult createSQLRouteResult() { - Pagination pagination = mock(Pagination.class); + PaginationContext pagination = mock(PaginationContext.class); when(pagination.isHasPagination()).thenReturn(true); when(pagination.getOffsetParameterIndex()).thenReturn(Optional.of(2)); when(pagination.getRowCountParameterIndex()).thenReturn(Optional.of(3)); - when(pagination.getRevisedRowCount(any(SelectOptimizedStatement.class))).thenReturn(6L); - SelectOptimizedStatement shardingStatement = mock(SelectOptimizedStatement.class); - when(shardingStatement.getPagination()).thenReturn(pagination); - OptimizedStatement encryptStatement = mock(OptimizedStatement.class); - SQLRouteResult result = new SQLRouteResult(shardingStatement, encryptStatement, new ShardingConditions(Collections.emptyList()), null); + when(pagination.getRevisedRowCount(any(SelectSQLStatementContext.class))).thenReturn(6L); + SelectSQLStatementContext selectSQLStatementContext = mock(SelectSQLStatementContext.class); + when(selectSQLStatementContext.getPaginationContext()).thenReturn(pagination); + SQLStatementContext sqlStatementContext = mock(SQLStatementContext.class); + SQLRouteResult result = new SQLRouteResult(selectSQLStatementContext, sqlStatementContext, new ShardingConditions(Collections.emptyList()), null); result.setRoutingResult(new RoutingResult()); return result; } diff --git a/sharding-core/sharding-core-rewrite/src/test/java/org/apache/shardingsphere/core/rewrite/rewriter/EncryptSQLRewriteEngineTest.java b/sharding-core/sharding-core-rewrite/src/test/java/org/apache/shardingsphere/core/rewrite/rewriter/EncryptSQLRewriteEngineTest.java index caabcfd171..a713295c06 100644 --- a/sharding-core/sharding-core-rewrite/src/test/java/org/apache/shardingsphere/core/rewrite/rewriter/EncryptSQLRewriteEngineTest.java +++ b/sharding-core/sharding-core-rewrite/src/test/java/org/apache/shardingsphere/core/rewrite/rewriter/EncryptSQLRewriteEngineTest.java @@ -23,8 +23,8 @@ import org.apache.shardingsphere.api.config.encrypt.EncryptTableRuleConfiguratio import org.apache.shardingsphere.api.config.encrypt.EncryptorRuleConfiguration; import org.apache.shardingsphere.core.database.DatabaseTypes; import org.apache.shardingsphere.core.metadata.table.TableMetas; -import org.apache.shardingsphere.core.optimize.statement.OptimizedStatement; -import org.apache.shardingsphere.core.optimize.OptimizedStatementFactory; +import org.apache.shardingsphere.core.optimize.statement.SQLStatementContext; +import org.apache.shardingsphere.core.optimize.SQLStatementContextFactory; import org.apache.shardingsphere.core.parse.SQLParseEngine; import org.apache.shardingsphere.core.parse.sql.statement.SQLStatement; import org.apache.shardingsphere.core.rewrite.SQLRewriteEngine; @@ -336,8 +336,8 @@ public final class EncryptSQLRewriteEngineTest { private SQLUnit getSQLUnit(final String sql, final List parameters, final boolean isQueryWithCipherColumn) { // TODO panjuan: should mock sqlStatement, do not call parse module on rewrite test case SQLStatement sqlStatement = parseEngine.parse(sql, false); - OptimizedStatement encryptStatement = OptimizedStatementFactory.newInstance(mock(TableMetas.class), sql, parameters, sqlStatement); - SQLRewriteEngine sqlRewriteEngine = new SQLRewriteEngine(encryptRule, mock(TableMetas.class), encryptStatement, sql, parameters, isQueryWithCipherColumn); + SQLStatementContext sqlStatementContext = SQLStatementContextFactory.newInstance(mock(TableMetas.class), sql, parameters, sqlStatement); + SQLRewriteEngine sqlRewriteEngine = new SQLRewriteEngine(encryptRule, mock(TableMetas.class), sqlStatementContext, sql, parameters, isQueryWithCipherColumn); return sqlRewriteEngine.generateSQL(); } } diff --git a/sharding-core/sharding-core-rewrite/src/test/java/org/apache/shardingsphere/core/rewrite/rewriter/ShardingSQLRewriteEngineTest.java b/sharding-core/sharding-core-rewrite/src/test/java/org/apache/shardingsphere/core/rewrite/rewriter/ShardingSQLRewriteEngineTest.java index 71f7769e08..e9b924f1ab 100644 --- a/sharding-core/sharding-core-rewrite/src/test/java/org/apache/shardingsphere/core/rewrite/rewriter/ShardingSQLRewriteEngineTest.java +++ b/sharding-core/sharding-core-rewrite/src/test/java/org/apache/shardingsphere/core/rewrite/rewriter/ShardingSQLRewriteEngineTest.java @@ -19,19 +19,19 @@ package org.apache.shardingsphere.core.rewrite.rewriter; import com.google.common.base.Preconditions; import lombok.SneakyThrows; -import org.apache.shardingsphere.core.optimize.statement.impl.CommonOptimizedStatement; -import org.apache.shardingsphere.core.optimize.statement.impl.InsertOptimizedStatement; -import org.apache.shardingsphere.core.optimize.statement.OptimizedStatement; -import org.apache.shardingsphere.core.optimize.segment.select.groupby.GroupBy; +import org.apache.shardingsphere.core.optimize.statement.impl.CommonSQLStatementContext; +import org.apache.shardingsphere.core.optimize.statement.impl.InsertSQLStatementContext; +import org.apache.shardingsphere.core.optimize.statement.SQLStatementContext; +import org.apache.shardingsphere.core.optimize.segment.select.groupby.GroupByContext; import org.apache.shardingsphere.core.optimize.segment.select.item.impl.AggregationDistinctSelectItem; import org.apache.shardingsphere.core.optimize.segment.select.item.impl.AggregationSelectItem; import org.apache.shardingsphere.core.optimize.segment.select.item.impl.DerivedSelectItem; import org.apache.shardingsphere.core.optimize.segment.select.item.SelectItem; -import org.apache.shardingsphere.core.optimize.segment.select.item.SelectItems; -import org.apache.shardingsphere.core.optimize.segment.select.orderby.OrderBy; +import org.apache.shardingsphere.core.optimize.segment.select.item.SelectItemsContext; +import org.apache.shardingsphere.core.optimize.segment.select.orderby.OrderByContext; import org.apache.shardingsphere.core.optimize.segment.select.orderby.OrderByItem; -import org.apache.shardingsphere.core.optimize.segment.select.pagination.Pagination; -import org.apache.shardingsphere.core.optimize.statement.impl.SelectOptimizedStatement; +import org.apache.shardingsphere.core.optimize.segment.select.pagination.PaginationContext; +import org.apache.shardingsphere.core.optimize.statement.impl.SelectSQLStatementContext; import org.apache.shardingsphere.core.parse.core.constant.AggregationType; import org.apache.shardingsphere.core.parse.core.constant.OrderDirection; import org.apache.shardingsphere.core.parse.core.constant.QuoteCharacter; @@ -130,10 +130,10 @@ public final class ShardingSQLRewriteEngineTest { private SQLRouteResult createSQLRouteResultWithoutChange() { SelectStatement selectStatement = new SelectStatement(); - SQLRouteResult result = new SQLRouteResult(new SelectOptimizedStatement(selectStatement, - new GroupBy(Collections.emptyList(), 0), new OrderBy(Collections.emptyList(), false), - new SelectItems(0, 0, false, Collections.emptyList()), new Pagination(null, null, Collections.emptyList())), - new CommonOptimizedStatement(selectStatement), new ShardingConditions(Collections.emptyList()), null); + SQLRouteResult result = new SQLRouteResult(new SelectSQLStatementContext(selectStatement, + new GroupByContext(Collections.emptyList(), 0), new OrderByContext(Collections.emptyList(), false), + new SelectItemsContext(0, 0, false, Collections.emptyList()), new PaginationContext(null, null, Collections.emptyList())), + new CommonSQLStatementContext(selectStatement), new ShardingConditions(Collections.emptyList()), null); result.setRoutingResult(new RoutingResult()); return result; } @@ -150,10 +150,10 @@ public final class ShardingSQLRewriteEngineTest { selectStatement.getAllSQLSegments().add(new TableSegment(7, 13, "table_x")); selectStatement.getAllSQLSegments().add(new TableSegment(31, 37, "table_x")); selectStatement.getAllSQLSegments().add(new TableSegment(47, 53, "table_x")); - SQLRouteResult result = new SQLRouteResult(new SelectOptimizedStatement(selectStatement, - new GroupBy(Collections.emptyList(), 0), new OrderBy(Collections.emptyList(), false), - new SelectItems(0, 0, false, Collections.emptyList()), new Pagination(null, null, Collections.emptyList())), - new CommonOptimizedStatement(selectStatement), new ShardingConditions(Collections.emptyList())); + SQLRouteResult result = new SQLRouteResult(new SelectSQLStatementContext(selectStatement, + new GroupByContext(Collections.emptyList(), 0), new OrderByContext(Collections.emptyList(), false), + new SelectItemsContext(0, 0, false, Collections.emptyList()), new PaginationContext(null, null, Collections.emptyList())), + new CommonSQLStatementContext(selectStatement), new ShardingConditions(Collections.emptyList())); result.setRoutingResult(new RoutingResult()); return result; } @@ -171,11 +171,11 @@ public final class ShardingSQLRewriteEngineTest { selectStatement.getAllSQLSegments().add(new TableSegment(18, 24, "table_x")); DerivedSelectItem selectItem1 = new DerivedSelectItem("x.id", "GROUP_BY_DERIVED_0"); DerivedSelectItem selectItem2 = new DerivedSelectItem("x.name", "ORDER_BY_DERIVED_0"); - SelectItems selectItems = new SelectItems(6, 11, false, Arrays.asList(selectItem1, selectItem2)); - SQLRouteResult result = new SQLRouteResult(new SelectOptimizedStatement(selectStatement, - new GroupBy(Collections.emptyList(), 0), new OrderBy(Collections.emptyList(), false), - selectItems, new Pagination(null, null, Collections.emptyList())), - new CommonOptimizedStatement(selectStatement), new ShardingConditions(Collections.emptyList())); + SelectItemsContext selectItemsContext = new SelectItemsContext(6, 11, false, Arrays.asList(selectItem1, selectItem2)); + SQLRouteResult result = new SQLRouteResult(new SelectSQLStatementContext(selectStatement, + new GroupByContext(Collections.emptyList(), 0), new OrderByContext(Collections.emptyList(), false), + selectItemsContext, new PaginationContext(null, null, Collections.emptyList())), + new CommonSQLStatementContext(selectStatement), new ShardingConditions(Collections.emptyList())); result.setRoutingResult(new RoutingResult()); return result; } @@ -195,11 +195,11 @@ public final class ShardingSQLRewriteEngineTest { AggregationSelectItem avgSelectItem = new AggregationSelectItem(AggregationType.AVG, "(x.age)", null); avgSelectItem.getDerivedAggregationItems().add(countSelectItem); avgSelectItem.getDerivedAggregationItems().add(sumSelectItem); - SelectItems selectItems = new SelectItems(6, 16, false, Collections.singletonList(avgSelectItem)); - SQLRouteResult result = new SQLRouteResult(new SelectOptimizedStatement(selectStatement, - new GroupBy(Collections.emptyList(), 0), new OrderBy(Collections.emptyList(), false), - selectItems, new Pagination(null, null, Collections.emptyList())), - new CommonOptimizedStatement(selectStatement), new ShardingConditions(Collections.emptyList())); + SelectItemsContext selectItemsContext = new SelectItemsContext(6, 16, false, Collections.singletonList(avgSelectItem)); + SQLRouteResult result = new SQLRouteResult(new SelectSQLStatementContext(selectStatement, + new GroupByContext(Collections.emptyList(), 0), new OrderByContext(Collections.emptyList(), false), + selectItemsContext, new PaginationContext(null, null, Collections.emptyList())), + new CommonSQLStatementContext(selectStatement), new ShardingConditions(Collections.emptyList())); result.setRoutingResult(new RoutingResult()); return result; } @@ -221,13 +221,13 @@ public final class ShardingSQLRewriteEngineTest { insertStatement.getValues().add(new InsertValuesSegment(39, 44, Arrays.asList(new ParameterMarkerExpressionSegment(0, 0, 0), new ParameterMarkerExpressionSegment(0, 0, 1)))); insertStatement.getAllSQLSegments().add(new TableSegment(12, 18, "table_x")); - InsertOptimizedStatement shardingStatement = new InsertOptimizedStatement(null, Arrays.asList("x", 1), insertStatement); + InsertSQLStatementContext insertSQLStatementContext = new InsertSQLStatementContext(null, Arrays.asList("x", 1), insertStatement); ShardingCondition shardingCondition = new ShardingCondition(); shardingCondition.getDataNodes().add(new DataNode("db0.table_1")); GeneratedKey generatedKey = new GeneratedKey("id", true); generatedKey.getGeneratedValues().add(1); SQLRouteResult result = new SQLRouteResult( - shardingStatement, new CommonOptimizedStatement(insertStatement), new ShardingConditions(Collections.singletonList(shardingCondition)), generatedKey); + insertSQLStatementContext, new CommonSQLStatementContext(insertStatement), new ShardingConditions(Collections.singletonList(shardingCondition)), generatedKey); result.setRoutingResult(new RoutingResult()); return result; } @@ -248,13 +248,13 @@ public final class ShardingSQLRewriteEngineTest { insertStatement.getValues().add(new InsertValuesSegment(29, 31, Collections.singletonList(new ParameterMarkerExpressionSegment(0, 0, 0)))); insertStatement.setTable(new TableSegment(0, 0, "table_x")); insertStatement.getAllSQLSegments().add(new TableSegment(12, 20, "`table_x`")); - InsertOptimizedStatement shardingStatement = new InsertOptimizedStatement(null, Collections.singletonList("Bill"), insertStatement); + InsertSQLStatementContext insertSQLStatementContext = new InsertSQLStatementContext(null, Collections.singletonList("Bill"), insertStatement); ShardingCondition shardingCondition = new ShardingCondition(); shardingCondition.getDataNodes().add(new DataNode("db0.table_1")); GeneratedKey generatedKey = new GeneratedKey("id", true); generatedKey.getGeneratedValues().add(1); SQLRouteResult result = new SQLRouteResult( - shardingStatement, new CommonOptimizedStatement(insertStatement), new ShardingConditions(Collections.singletonList(shardingCondition)), generatedKey); + insertSQLStatementContext, new CommonSQLStatementContext(insertStatement), new ShardingConditions(Collections.singletonList(shardingCondition)), generatedKey); result.setRoutingResult(new RoutingResult()); return result; } @@ -275,13 +275,13 @@ public final class ShardingSQLRewriteEngineTest { insertStatement.getValues().add(new InsertValuesSegment(29, 32, Collections.singletonList(new LiteralExpressionSegment(0, 0, 10)))); insertStatement.setTable(new TableSegment(0, 0, "table_x")); insertStatement.getAllSQLSegments().add(new TableSegment(12, 20, "`table_x`")); - InsertOptimizedStatement shardingStatement = new InsertOptimizedStatement(null, Collections.emptyList(), insertStatement); + InsertSQLStatementContext insertSQLStatementContext = new InsertSQLStatementContext(null, Collections.emptyList(), insertStatement); ShardingCondition shardingCondition = new ShardingCondition(); shardingCondition.getDataNodes().add(new DataNode("db0.table_1")); GeneratedKey generatedKey = new GeneratedKey("id", true); generatedKey.getGeneratedValues().add(1); SQLRouteResult result = new SQLRouteResult( - shardingStatement, new CommonOptimizedStatement(insertStatement), new ShardingConditions(Collections.singletonList(shardingCondition)), generatedKey); + insertSQLStatementContext, new CommonSQLStatementContext(insertStatement), new ShardingConditions(Collections.singletonList(shardingCondition)), generatedKey); result.setRoutingResult(new RoutingResult()); return result; } @@ -302,13 +302,13 @@ public final class ShardingSQLRewriteEngineTest { insertStatement.getValues().add(new InsertValuesSegment(29, 32, Collections.singletonList(new LiteralExpressionSegment(0, 0, 10)))); insertStatement.setTable(new TableSegment(0, 0, "table_x")); insertStatement.getAllSQLSegments().add(new TableSegment(12, 20, "`table_x`")); - InsertOptimizedStatement shardingStatement = new InsertOptimizedStatement(null, Collections.emptyList(), insertStatement); + InsertSQLStatementContext insertSQLStatementContext = new InsertSQLStatementContext(null, Collections.emptyList(), insertStatement); ShardingCondition shardingCondition = new ShardingCondition(); shardingCondition.getDataNodes().add(new DataNode("db0.table_1")); GeneratedKey generatedKey = new GeneratedKey("id", true); generatedKey.getGeneratedValues().add(1); SQLRouteResult result = new SQLRouteResult( - shardingStatement, new CommonOptimizedStatement(insertStatement), new ShardingConditions(Collections.singletonList(shardingCondition)), generatedKey); + insertSQLStatementContext, new CommonSQLStatementContext(insertStatement), new ShardingConditions(Collections.singletonList(shardingCondition)), generatedKey); result.setRoutingResult(new RoutingResult()); return result; } @@ -326,10 +326,10 @@ public final class ShardingSQLRewriteEngineTest { insertStatement.getColumns().add(new ColumnSegment(0, 0, "id")); insertStatement.setTable(new TableSegment(0, 0, "table_x")); insertStatement.getAllSQLSegments().add(new TableSegment(12, 20, "`table_x`")); - InsertOptimizedStatement shardingStatement = new InsertOptimizedStatement(null, Collections.emptyList(), insertStatement); + InsertSQLStatementContext insertSQLStatementContext = new InsertSQLStatementContext(null, Collections.emptyList(), insertStatement); ShardingCondition shardingCondition = new ShardingCondition(); shardingCondition.getDataNodes().add(new DataNode("db0.table_1")); - SQLRouteResult result = new SQLRouteResult(shardingStatement, new CommonOptimizedStatement(insertStatement), new ShardingConditions(Collections.singletonList(shardingCondition))); + SQLRouteResult result = new SQLRouteResult(insertSQLStatementContext, new CommonSQLStatementContext(insertStatement), new ShardingConditions(Collections.singletonList(shardingCondition))); result.setRoutingResult(new RoutingResult()); return result; } @@ -349,10 +349,10 @@ public final class ShardingSQLRewriteEngineTest { insertStatement.getAllSQLSegments().add(new InsertColumnsSegment(21, 21, Collections.emptyList())); insertStatement.getAllSQLSegments().add(new InsertValuesSegment(29, 35, Arrays.asList(new LiteralExpressionSegment(0, 0, 10), new LiteralExpressionSegment(0, 0, 1)))); insertStatement.getValues().add(new InsertValuesSegment(29, 35, Arrays.asList(new LiteralExpressionSegment(0, 0, 10), new LiteralExpressionSegment(0, 0, 1)))); - InsertOptimizedStatement shardingStatement = new InsertOptimizedStatement(null, Collections.emptyList(), insertStatement); + InsertSQLStatementContext insertSQLStatementContext = new InsertSQLStatementContext(null, Collections.emptyList(), insertStatement); ShardingCondition shardingCondition = new ShardingCondition(); shardingCondition.getDataNodes().add(new DataNode("db0.table_1")); - SQLRouteResult result = new SQLRouteResult(shardingStatement, new CommonOptimizedStatement(insertStatement), new ShardingConditions(Collections.singletonList(shardingCondition))); + SQLRouteResult result = new SQLRouteResult(insertSQLStatementContext, new CommonSQLStatementContext(insertStatement), new ShardingConditions(Collections.singletonList(shardingCondition))); result.setRoutingResult(new RoutingResult()); return result; } @@ -374,13 +374,13 @@ public final class ShardingSQLRewriteEngineTest { new InsertValuesSegment(29, 34, Arrays.asList(new ParameterMarkerExpressionSegment(0, 0, 0), new ParameterMarkerExpressionSegment(0, 0, 1)))); insertStatement.setTable(new TableSegment(0, 0, "table_x")); insertStatement.getAllSQLSegments().add(new TableSegment(12, 20, "`table_x`")); - InsertOptimizedStatement shardingStatement = new InsertOptimizedStatement(null, Arrays.asList("x", 1), insertStatement); + InsertSQLStatementContext insertSQLStatementContext = new InsertSQLStatementContext(null, Arrays.asList("x", 1), insertStatement); ShardingCondition shardingCondition = new ShardingCondition(); shardingCondition.getDataNodes().add(new DataNode("db0.table_1")); GeneratedKey generatedKey = new GeneratedKey("id", false); generatedKey.getGeneratedValues().add(1); SQLRouteResult result = new SQLRouteResult( - shardingStatement, new CommonOptimizedStatement(insertStatement), new ShardingConditions(Collections.singletonList(shardingCondition)), generatedKey); + insertSQLStatementContext, new CommonSQLStatementContext(insertStatement), new ShardingConditions(Collections.singletonList(shardingCondition)), generatedKey); result.setRoutingResult(new RoutingResult()); return result; } @@ -394,11 +394,11 @@ public final class ShardingSQLRewriteEngineTest { private SQLRouteResult createRouteResultForLimit() { SelectStatement selectStatement = new SelectStatement(); selectStatement.getAllSQLSegments().add(new TableSegment(17, 23, "table_x")); - OptimizedStatement shardingStatement = new SelectOptimizedStatement(selectStatement, - new GroupBy(Collections.emptyList(), 0), new OrderBy(Collections.emptyList(), false), - new SelectItems(0, 0, false, Collections.emptyList()), - new Pagination(new NumberLiteralLimitValueSegment(33, 33, 2), new NumberLiteralLimitValueSegment(36, 36, 2), Collections.emptyList())); - SQLRouteResult result = new SQLRouteResult(shardingStatement, new CommonOptimizedStatement(selectStatement), new ShardingConditions(Collections.emptyList())); + SQLStatementContext selectSQLStatementContext = new SelectSQLStatementContext(selectStatement, + new GroupByContext(Collections.emptyList(), 0), new OrderByContext(Collections.emptyList(), false), + new SelectItemsContext(0, 0, false, Collections.emptyList()), + new PaginationContext(new NumberLiteralLimitValueSegment(33, 33, 2), new NumberLiteralLimitValueSegment(36, 36, 2), Collections.emptyList())); + SQLRouteResult result = new SQLRouteResult(selectSQLStatementContext, new CommonSQLStatementContext(selectStatement), new ShardingConditions(Collections.emptyList())); result.setRoutingResult(new RoutingResult()); return result; } @@ -414,11 +414,11 @@ public final class ShardingSQLRewriteEngineTest { private SQLRouteResult createRouteResultForRowNumber() { SelectStatement selectStatement = new SelectStatement(); selectStatement.getAllSQLSegments().add(new TableSegment(68, 74, "table_x")); - OptimizedStatement shardingStatement = new SelectOptimizedStatement(selectStatement, - new GroupBy(Collections.emptyList(), 0), new OrderBy(Collections.emptyList(), false), - new SelectItems(0, 0, false, Collections.emptyList()), - new Pagination(new NumberLiteralRowNumberValueSegment(119, 119, 2, true), new NumberLiteralRowNumberValueSegment(98, 98, 4, false), Collections.emptyList())); - SQLRouteResult result = new SQLRouteResult(shardingStatement, new CommonOptimizedStatement(selectStatement), new ShardingConditions(Collections.emptyList())); + SQLStatementContext selectSQLStatementContext = new SelectSQLStatementContext(selectStatement, + new GroupByContext(Collections.emptyList(), 0), new OrderByContext(Collections.emptyList(), false), + new SelectItemsContext(0, 0, false, Collections.emptyList()), + new PaginationContext(new NumberLiteralRowNumberValueSegment(119, 119, 2, true), new NumberLiteralRowNumberValueSegment(98, 98, 4, false), Collections.emptyList())); + SQLRouteResult result = new SQLRouteResult(selectSQLStatementContext, new CommonSQLStatementContext(selectStatement), new ShardingConditions(Collections.emptyList())); result.setRoutingResult(new RoutingResult()); return result; } @@ -434,11 +434,11 @@ public final class ShardingSQLRewriteEngineTest { private SQLRouteResult createRouteResultForTopAndRowNumber() { SelectStatement selectStatement = new SelectStatement(); selectStatement.getAllSQLSegments().add(new TableSegment(85, 91, "table_x")); - OptimizedStatement shardingStatement = new SelectOptimizedStatement(selectStatement, - new GroupBy(Collections.emptyList(), 0), new OrderBy(Collections.emptyList(), false), - new SelectItems(0, 0, false, Collections.emptyList()), - new Pagination(new NumberLiteralRowNumberValueSegment(123, 123, 2, true), new NumberLiteralRowNumberValueSegment(26, 26, 4, false), Collections.emptyList())); - SQLRouteResult result = new SQLRouteResult(shardingStatement, new CommonOptimizedStatement(selectStatement), new ShardingConditions(Collections.emptyList())); + SQLStatementContext selectSQLStatementContext = new SelectSQLStatementContext(selectStatement, + new GroupByContext(Collections.emptyList(), 0), new OrderByContext(Collections.emptyList(), false), + new SelectItemsContext(0, 0, false, Collections.emptyList()), + new PaginationContext(new NumberLiteralRowNumberValueSegment(123, 123, 2, true), new NumberLiteralRowNumberValueSegment(26, 26, 4, false), Collections.emptyList())); + SQLRouteResult result = new SQLRouteResult(selectSQLStatementContext, new CommonSQLStatementContext(selectStatement), new ShardingConditions(Collections.emptyList())); result.setRoutingResult(new RoutingResult()); return result; } @@ -454,12 +454,12 @@ public final class ShardingSQLRewriteEngineTest { ColumnSegment columnSegment = new ColumnSegment(0, 0, "id"); columnSegment.setOwner(new TableSegment(0, 0, "x")); selectStatement.getAllSQLSegments().add(new TableSegment(17, 23, "table_x")); - OptimizedStatement shardingStatement = new SelectOptimizedStatement(selectStatement, - new GroupBy(Collections.singletonList(new OrderByItem(new ColumnOrderByItemSegment(0, 0, columnSegment, OrderDirection.DESC, OrderDirection.ASC))), 0), - new OrderBy(Collections.singletonList(new OrderByItem(new ColumnOrderByItemSegment(0, 0, columnSegment, OrderDirection.ASC, OrderDirection.ASC))), false), - new SelectItems(0, 0, false, Collections.emptyList()), - new Pagination(new NumberLiteralLimitValueSegment(33, 33, 2), new NumberLiteralLimitValueSegment(36, 36, 2), Collections.emptyList())); - SQLRouteResult result = new SQLRouteResult(shardingStatement, new CommonOptimizedStatement(selectStatement), new ShardingConditions(Collections.emptyList())); + SQLStatementContext selectSQLStatementContext = new SelectSQLStatementContext(selectStatement, + new GroupByContext(Collections.singletonList(new OrderByItem(new ColumnOrderByItemSegment(0, 0, columnSegment, OrderDirection.DESC, OrderDirection.ASC))), 0), + new OrderByContext(Collections.singletonList(new OrderByItem(new ColumnOrderByItemSegment(0, 0, columnSegment, OrderDirection.ASC, OrderDirection.ASC))), false), + new SelectItemsContext(0, 0, false, Collections.emptyList()), + new PaginationContext(new NumberLiteralLimitValueSegment(33, 33, 2), new NumberLiteralLimitValueSegment(36, 36, 2), Collections.emptyList())); + SQLRouteResult result = new SQLRouteResult(selectSQLStatementContext, new CommonSQLStatementContext(selectStatement), new ShardingConditions(Collections.emptyList())); result.setRoutingResult(new RoutingResult()); return result; } @@ -477,12 +477,12 @@ public final class ShardingSQLRewriteEngineTest { selectStatement.getAllSQLSegments().add(new TableSegment(68, 74, "table_x")); ColumnSegment columnSegment = new ColumnSegment(0, 0, "id"); columnSegment.setOwner(new TableSegment(0, 0, "x")); - OptimizedStatement shardingStatement = new SelectOptimizedStatement(selectStatement, - new GroupBy(Collections.singletonList(new OrderByItem(new ColumnOrderByItemSegment(0, 0, columnSegment, OrderDirection.DESC, OrderDirection.ASC))), 0), - new OrderBy(Collections.singletonList(new OrderByItem(new ColumnOrderByItemSegment(0, 0, columnSegment, OrderDirection.ASC, OrderDirection.ASC))), false), - new SelectItems(0, 0, false, Collections.emptyList()), - new Pagination(new NumberLiteralRowNumberValueSegment(119, 119, 2, true), new NumberLiteralRowNumberValueSegment(98, 98, 4, false), Collections.emptyList())); - SQLRouteResult result = new SQLRouteResult(shardingStatement, new CommonOptimizedStatement(selectStatement), new ShardingConditions(Collections.emptyList())); + SQLStatementContext selectSQLStatementContext = new SelectSQLStatementContext(selectStatement, + new GroupByContext(Collections.singletonList(new OrderByItem(new ColumnOrderByItemSegment(0, 0, columnSegment, OrderDirection.DESC, OrderDirection.ASC))), 0), + new OrderByContext(Collections.singletonList(new OrderByItem(new ColumnOrderByItemSegment(0, 0, columnSegment, OrderDirection.ASC, OrderDirection.ASC))), false), + new SelectItemsContext(0, 0, false, Collections.emptyList()), + new PaginationContext(new NumberLiteralRowNumberValueSegment(119, 119, 2, true), new NumberLiteralRowNumberValueSegment(98, 98, 4, false), Collections.emptyList())); + SQLRouteResult result = new SQLRouteResult(selectSQLStatementContext, new CommonSQLStatementContext(selectStatement), new ShardingConditions(Collections.emptyList())); result.setRoutingResult(new RoutingResult()); return result; } @@ -500,12 +500,12 @@ public final class ShardingSQLRewriteEngineTest { selectStatement.getAllSQLSegments().add(new TableSegment(85, 91, "table_x")); ColumnSegment columnSegment = new ColumnSegment(0, 0, "id"); columnSegment.setOwner(new TableSegment(0, 0, "x")); - OptimizedStatement shardingStatement = new SelectOptimizedStatement(selectStatement, - new GroupBy(Collections.singletonList(new OrderByItem(new ColumnOrderByItemSegment(0, 0, columnSegment, OrderDirection.DESC, OrderDirection.ASC))), 0), - new OrderBy(Collections.singletonList(new OrderByItem(new ColumnOrderByItemSegment(0, 0, columnSegment, OrderDirection.ASC, OrderDirection.ASC))), false), - new SelectItems(0, 0, false, Collections.emptyList()), - new Pagination(new NumberLiteralRowNumberValueSegment(123, 123, 2, false), new NumberLiteralRowNumberValueSegment(26, 26, 4, false), Collections.emptyList())); - SQLRouteResult result = new SQLRouteResult(shardingStatement, new CommonOptimizedStatement(selectStatement), new ShardingConditions(Collections.emptyList())); + SQLStatementContext selectSQLStatementContext = new SelectSQLStatementContext(selectStatement, + new GroupByContext(Collections.singletonList(new OrderByItem(new ColumnOrderByItemSegment(0, 0, columnSegment, OrderDirection.DESC, OrderDirection.ASC))), 0), + new OrderByContext(Collections.singletonList(new OrderByItem(new ColumnOrderByItemSegment(0, 0, columnSegment, OrderDirection.ASC, OrderDirection.ASC))), false), + new SelectItemsContext(0, 0, false, Collections.emptyList()), + new PaginationContext(new NumberLiteralRowNumberValueSegment(123, 123, 2, false), new NumberLiteralRowNumberValueSegment(26, 26, 4, false), Collections.emptyList())); + SQLRouteResult result = new SQLRouteResult(selectSQLStatementContext, new CommonSQLStatementContext(selectStatement), new ShardingConditions(Collections.emptyList())); result.setRoutingResult(new RoutingResult()); return result; } @@ -519,11 +519,11 @@ public final class ShardingSQLRewriteEngineTest { private SQLRouteResult createRouteResultForLimitForNotRewritePagination() { SelectStatement selectStatement = new SelectStatement(); selectStatement.getAllSQLSegments().add(new TableSegment(17, 23, "table_x")); - OptimizedStatement shardingStatement = new SelectOptimizedStatement(selectStatement, - new GroupBy(Collections.emptyList(), 0), new OrderBy(Collections.emptyList(), false), - new SelectItems(0, 0, false, Collections.emptyList()), - new Pagination(new NumberLiteralLimitValueSegment(33, 33, 2), new NumberLiteralLimitValueSegment(36, 36, 2), Collections.emptyList())); - SQLRouteResult result = new SQLRouteResult(shardingStatement, new CommonOptimizedStatement(selectStatement), new ShardingConditions(Collections.emptyList())); + SQLStatementContext selectSQLStatementContext = new SelectSQLStatementContext(selectStatement, + new GroupByContext(Collections.emptyList(), 0), new OrderByContext(Collections.emptyList(), false), + new SelectItemsContext(0, 0, false, Collections.emptyList()), + new PaginationContext(new NumberLiteralLimitValueSegment(33, 33, 2), new NumberLiteralLimitValueSegment(36, 36, 2), Collections.emptyList())); + SQLRouteResult result = new SQLRouteResult(selectSQLStatementContext, new CommonSQLStatementContext(selectStatement), new ShardingConditions(Collections.emptyList())); RoutingResult routingResult = new RoutingResult(); routingResult.getRoutingUnits().add(new RoutingUnit("ds")); result.setRoutingResult(routingResult); @@ -541,11 +541,11 @@ public final class ShardingSQLRewriteEngineTest { private SQLRouteResult createRouteResultForRowNumForNotRewritePagination() { SelectStatement selectStatement = new SelectStatement(); selectStatement.getAllSQLSegments().add(new TableSegment(68, 74, "table_x")); - OptimizedStatement shardingStatement = new SelectOptimizedStatement(selectStatement, - new GroupBy(Collections.emptyList(), 0), new OrderBy(Collections.emptyList(), false), - new SelectItems(0, 0, false, Collections.emptyList()), - new Pagination(new NumberLiteralRowNumberValueSegment(119, 119, 2, true), new NumberLiteralRowNumberValueSegment(98, 98, 4, false), Collections.emptyList())); - SQLRouteResult result = new SQLRouteResult(shardingStatement, new CommonOptimizedStatement(selectStatement), new ShardingConditions(Collections.emptyList())); + SQLStatementContext selectSQLStatementContext = new SelectSQLStatementContext(selectStatement, + new GroupByContext(Collections.emptyList(), 0), new OrderByContext(Collections.emptyList(), false), + new SelectItemsContext(0, 0, false, Collections.emptyList()), + new PaginationContext(new NumberLiteralRowNumberValueSegment(119, 119, 2, true), new NumberLiteralRowNumberValueSegment(98, 98, 4, false), Collections.emptyList())); + SQLRouteResult result = new SQLRouteResult(selectSQLStatementContext, new CommonSQLStatementContext(selectStatement), new ShardingConditions(Collections.emptyList())); RoutingResult routingResult = new RoutingResult(); routingResult.getRoutingUnits().add(new RoutingUnit("ds")); result.setRoutingResult(routingResult); @@ -564,11 +564,11 @@ public final class ShardingSQLRewriteEngineTest { private SQLRouteResult createRouteResultForTopAndRowNumberForNotRewritePagination() { SelectStatement selectStatement = new SelectStatement(); selectStatement.getAllSQLSegments().add(new TableSegment(85, 91, "table_x")); - OptimizedStatement shardingStatement = new SelectOptimizedStatement(selectStatement, - new GroupBy(Collections.emptyList(), 0), new OrderBy(Collections.emptyList(), false), - new SelectItems(0, 0, false, Collections.emptyList()), - new Pagination(new NumberLiteralRowNumberValueSegment(123, 123, 2, true), new NumberLiteralRowNumberValueSegment(26, 26, 4, false), Collections.emptyList())); - SQLRouteResult result = new SQLRouteResult(shardingStatement, new CommonOptimizedStatement(selectStatement), new ShardingConditions(Collections.emptyList())); + SQLStatementContext selectSQLStatementContext = new SelectSQLStatementContext(selectStatement, + new GroupByContext(Collections.emptyList(), 0), new OrderByContext(Collections.emptyList(), false), + new SelectItemsContext(0, 0, false, Collections.emptyList()), + new PaginationContext(new NumberLiteralRowNumberValueSegment(123, 123, 2, true), new NumberLiteralRowNumberValueSegment(26, 26, 4, false), Collections.emptyList())); + SQLRouteResult result = new SQLRouteResult(selectSQLStatementContext, new CommonSQLStatementContext(selectStatement), new ShardingConditions(Collections.emptyList())); RoutingResult routingResult = new RoutingResult(); routingResult.getRoutingUnits().add(new RoutingUnit("ds")); result.setRoutingResult(routingResult); @@ -588,15 +588,15 @@ public final class ShardingSQLRewriteEngineTest { columnSegment1.setOwner(new TableSegment(0, 0, "x")); ColumnSegment columnSegment2 = new ColumnSegment(0, 0, "name"); columnSegment2.setOwner(new TableSegment(0, 0, "x")); - OptimizedStatement shardingStatement = new SelectOptimizedStatement(selectStatement, - new GroupBy(Arrays.asList( + SQLStatementContext selectSQLStatementContext = new SelectSQLStatementContext(selectStatement, + new GroupByContext(Arrays.asList( new OrderByItem(new ColumnOrderByItemSegment(0, 0, columnSegment1, OrderDirection.ASC, OrderDirection.ASC)), new OrderByItem(new ColumnOrderByItemSegment(0, 0, columnSegment2, OrderDirection.DESC, OrderDirection.ASC))), 60), - new OrderBy(Arrays.asList( + new OrderByContext(Arrays.asList( new OrderByItem(new ColumnOrderByItemSegment(0, 0, columnSegment1, OrderDirection.ASC, OrderDirection.ASC)), new OrderByItem(new ColumnOrderByItemSegment(0, 0, columnSegment2, OrderDirection.DESC, OrderDirection.ASC))), true), - new SelectItems(0, 0, false, Collections.emptyList()), new Pagination(null, null, Collections.emptyList())); - SQLRouteResult result = new SQLRouteResult(shardingStatement, new CommonOptimizedStatement(selectStatement), new ShardingConditions(Collections.emptyList())); + new SelectItemsContext(0, 0, false, Collections.emptyList()), new PaginationContext(null, null, Collections.emptyList())); + SQLRouteResult result = new SQLRouteResult(selectSQLStatementContext, new CommonSQLStatementContext(selectStatement), new ShardingConditions(Collections.emptyList())); result.setRoutingResult(new RoutingResult()); return result; } @@ -611,10 +611,10 @@ public final class ShardingSQLRewriteEngineTest { SelectStatement selectStatement = new SelectStatement(); selectStatement.getAllSQLSegments().add(new IndexSegment(13, 22, "index_name", QuoteCharacter.NONE)); selectStatement.getAllSQLSegments().add(new TableSegment(27, 33, "table_x")); - SQLRouteResult result = new SQLRouteResult(new SelectOptimizedStatement(selectStatement, - new GroupBy(Collections.emptyList(), 0), new OrderBy(Collections.emptyList(), false), - new SelectItems(0, 0, false, Collections.emptyList()), new Pagination(null, null, Collections.emptyList())), - new CommonOptimizedStatement(selectStatement), new ShardingConditions(Collections.emptyList())); + SQLRouteResult result = new SQLRouteResult(new SelectSQLStatementContext(selectStatement, + new GroupByContext(Collections.emptyList(), 0), new OrderByContext(Collections.emptyList(), false), + new SelectItemsContext(0, 0, false, Collections.emptyList()), new PaginationContext(null, null, Collections.emptyList())), + new CommonSQLStatementContext(selectStatement), new ShardingConditions(Collections.emptyList())); result.setRoutingResult(new RoutingResult()); return result; } @@ -630,10 +630,10 @@ public final class ShardingSQLRewriteEngineTest { SelectStatement selectStatement = new SelectStatement(); selectStatement.getAllSQLSegments().add(new IndexSegment(13, 23, "logic_index", QuoteCharacter.NONE)); selectStatement.getAllSQLSegments().add(new TableSegment(28, 34, "table_x")); - SQLRouteResult result = new SQLRouteResult(new SelectOptimizedStatement(selectStatement, - new GroupBy(Collections.emptyList(), 0), new OrderBy(Collections.emptyList(), false), - new SelectItems(0, 0, false, Collections.emptyList()), new Pagination(null, null, Collections.emptyList())), - new CommonOptimizedStatement(selectStatement), new ShardingConditions(Collections.emptyList())); + SQLRouteResult result = new SQLRouteResult(new SelectSQLStatementContext(selectStatement, + new GroupByContext(Collections.emptyList(), 0), new OrderByContext(Collections.emptyList(), false), + new SelectItemsContext(0, 0, false, Collections.emptyList()), new PaginationContext(null, null, Collections.emptyList())), + new CommonSQLStatementContext(selectStatement), new ShardingConditions(Collections.emptyList())); result.setRoutingResult(new RoutingResult()); return result; } @@ -647,8 +647,8 @@ public final class ShardingSQLRewriteEngineTest { private SQLRouteResult createRouteResultForTableTokenWithoutBackQuoteForShow() { DALStatement showTablesStatement = new DALStatement(); showTablesStatement.getAllSQLSegments().add(new TableSegment(18, 24, "table_x")); - SQLRouteResult result = new SQLRouteResult(new CommonOptimizedStatement(showTablesStatement), - new CommonOptimizedStatement(showTablesStatement), new ShardingConditions(Collections.emptyList())); + SQLRouteResult result = new SQLRouteResult(new CommonSQLStatementContext(showTablesStatement), + new CommonSQLStatementContext(showTablesStatement), new ShardingConditions(Collections.emptyList())); RoutingResult routingResult = new RoutingResult(); routingResult.getRoutingUnits().add(new RoutingUnit("ds")); result.setRoutingResult(routingResult); @@ -666,8 +666,8 @@ public final class ShardingSQLRewriteEngineTest { DALStatement showTablesStatement = new DALStatement(); showTablesStatement.getAllSQLSegments().add(new FromSchemaSegment(25, 43)); showTablesStatement.getAllSQLSegments().add(new TableSegment(18, 24, "table_x")); - SQLRouteResult result = new SQLRouteResult(new CommonOptimizedStatement(showTablesStatement), - new CommonOptimizedStatement(showTablesStatement), new ShardingConditions(Collections.emptyList())); + SQLRouteResult result = new SQLRouteResult(new CommonSQLStatementContext(showTablesStatement), + new CommonSQLStatementContext(showTablesStatement), new ShardingConditions(Collections.emptyList())); RoutingResult routingResult = new RoutingResult(); routingResult.getRoutingUnits().add(new RoutingUnit("ds")); result.setRoutingResult(routingResult); @@ -683,8 +683,8 @@ public final class ShardingSQLRewriteEngineTest { private SQLRouteResult createRouteResultForTableTokenWithBackQuoteForShow() { DALStatement showTablesStatement = new DALStatement(); showTablesStatement.getAllSQLSegments().add(new TableSegment(18, 26, "`table_x`")); - SQLRouteResult result = new SQLRouteResult(new CommonOptimizedStatement(showTablesStatement), - new CommonOptimizedStatement(showTablesStatement), new ShardingConditions(Collections.emptyList())); + SQLRouteResult result = new SQLRouteResult(new CommonSQLStatementContext(showTablesStatement), + new CommonSQLStatementContext(showTablesStatement), new ShardingConditions(Collections.emptyList())); RoutingResult routingResult = new RoutingResult(); routingResult.getRoutingUnits().add(new RoutingUnit("ds")); result.setRoutingResult(routingResult); @@ -701,8 +701,8 @@ public final class ShardingSQLRewriteEngineTest { private SQLRouteResult createSQLRouteResultForTableTokenWithBackQuoteFromSchemaForShow() { DALStatement showTablesStatement = new DALStatement(); showTablesStatement.getAllSQLSegments().add(new TableSegment(18, 26, "`table_x`")); - SQLRouteResult result = new SQLRouteResult(new CommonOptimizedStatement(showTablesStatement), - new CommonOptimizedStatement(showTablesStatement), new ShardingConditions(Collections.emptyList())); + SQLRouteResult result = new SQLRouteResult(new CommonSQLStatementContext(showTablesStatement), + new CommonSQLStatementContext(showTablesStatement), new ShardingConditions(Collections.emptyList())); RoutingResult routingResult = new RoutingResult(); routingResult.getRoutingUnits().add(new RoutingUnit("ds")); result.setRoutingResult(routingResult); @@ -718,8 +718,8 @@ public final class ShardingSQLRewriteEngineTest { private SQLRouteResult createSQLRouteResultForTableTokenWithSchemaForShow() { DALStatement showTablesStatement = new DALStatement(); showTablesStatement.getAllSQLSegments().add(new TableSegment(18, 36, "table_x")); - SQLRouteResult result = new SQLRouteResult(new CommonOptimizedStatement(showTablesStatement), - new CommonOptimizedStatement(showTablesStatement), new ShardingConditions(Collections.emptyList())); + SQLRouteResult result = new SQLRouteResult(new CommonSQLStatementContext(showTablesStatement), + new CommonSQLStatementContext(showTablesStatement), new ShardingConditions(Collections.emptyList())); RoutingResult routingResult = new RoutingResult(); routingResult.getRoutingUnits().add(new RoutingUnit("ds")); result.setRoutingResult(routingResult); @@ -736,8 +736,8 @@ public final class ShardingSQLRewriteEngineTest { private SQLRouteResult createSQLRouteResultForTableTokenWithSchemaFromSchemaForShow() { DALStatement showTablesStatement = new DALStatement(); showTablesStatement.getAllSQLSegments().add(new TableSegment(18, 36, "table_x")); - SQLRouteResult result = new SQLRouteResult(new CommonOptimizedStatement(showTablesStatement), - new CommonOptimizedStatement(showTablesStatement), new ShardingConditions(Collections.emptyList())); + SQLRouteResult result = new SQLRouteResult(new CommonSQLStatementContext(showTablesStatement), + new CommonSQLStatementContext(showTablesStatement), new ShardingConditions(Collections.emptyList())); RoutingResult routingResult = new RoutingResult(); routingResult.getRoutingUnits().add(new RoutingUnit("ds")); result.setRoutingResult(routingResult); @@ -753,8 +753,8 @@ public final class ShardingSQLRewriteEngineTest { private SQLRouteResult createSQLRouteResultForTableTokenWithBackQuoteWithSchemaForShow() { DALStatement showTablesStatement = new DALStatement(); showTablesStatement.getAllSQLSegments().add(new TableSegment(18, 38, "`table_x`")); - SQLRouteResult result = new SQLRouteResult(new CommonOptimizedStatement(showTablesStatement), - new CommonOptimizedStatement(showTablesStatement), new ShardingConditions(Collections.emptyList())); + SQLRouteResult result = new SQLRouteResult(new CommonSQLStatementContext(showTablesStatement), + new CommonSQLStatementContext(showTablesStatement), new ShardingConditions(Collections.emptyList())); RoutingResult routingResult = new RoutingResult(); routingResult.getRoutingUnits().add(new RoutingUnit("ds")); result.setRoutingResult(routingResult); @@ -771,8 +771,8 @@ public final class ShardingSQLRewriteEngineTest { private SQLRouteResult createSQLRouteResultForTableTokenWithBackQuoteWithSchemaFromSchemaForShow() { DALStatement showTablesStatement = new DALStatement(); showTablesStatement.getAllSQLSegments().add(new TableSegment(18, 38, "`table_x`")); - SQLRouteResult result = new SQLRouteResult(new CommonOptimizedStatement(showTablesStatement), - new CommonOptimizedStatement(showTablesStatement), new ShardingConditions(Collections.emptyList())); + SQLRouteResult result = new SQLRouteResult(new CommonSQLStatementContext(showTablesStatement), + new CommonSQLStatementContext(showTablesStatement), new ShardingConditions(Collections.emptyList())); RoutingResult routingResult = new RoutingResult(); routingResult.getRoutingUnits().add(new RoutingUnit("ds")); result.setRoutingResult(routingResult); @@ -789,8 +789,8 @@ public final class ShardingSQLRewriteEngineTest { private SQLRouteResult createSQLRouteResultForTableTokenWithSchemaWithBackQuoteForShow() { DALStatement showTablesStatement = new DALStatement(); showTablesStatement.getAllSQLSegments().add(new TableSegment(18, 40, "`table_x`")); - SQLRouteResult result = new SQLRouteResult(new CommonOptimizedStatement(showTablesStatement), - new CommonOptimizedStatement(showTablesStatement), new ShardingConditions(Collections.emptyList())); + SQLRouteResult result = new SQLRouteResult(new CommonSQLStatementContext(showTablesStatement), + new CommonSQLStatementContext(showTablesStatement), new ShardingConditions(Collections.emptyList())); RoutingResult routingResult = new RoutingResult(); routingResult.getRoutingUnits().add(new RoutingUnit("ds")); result.setRoutingResult(routingResult); @@ -807,8 +807,8 @@ public final class ShardingSQLRewriteEngineTest { private SQLRouteResult createSQLRouteResultForTableTokenWithSchemaWithBackQuoteFromSchemaForShow() { DALStatement showTablesStatement = new DALStatement(); showTablesStatement.getAllSQLSegments().add(new TableSegment(18, 40, "`table_x`")); - SQLRouteResult result = new SQLRouteResult(new CommonOptimizedStatement(showTablesStatement), - new CommonOptimizedStatement(showTablesStatement), new ShardingConditions(Collections.emptyList())); + SQLRouteResult result = new SQLRouteResult(new CommonSQLStatementContext(showTablesStatement), + new CommonSQLStatementContext(showTablesStatement), new ShardingConditions(Collections.emptyList())); RoutingResult routingResult = new RoutingResult(); routingResult.getRoutingUnits().add(new RoutingUnit("ds")); result.setRoutingResult(routingResult); @@ -826,10 +826,10 @@ public final class ShardingSQLRewriteEngineTest { TableSegment tableSegment = new TableSegment(14, 32, "table_x"); tableSegment.setOwner(new SchemaSegment(14, 24, "sharding_db")); selectStatement.getAllSQLSegments().add(tableSegment); - SQLRouteResult result = new SQLRouteResult(new SelectOptimizedStatement(selectStatement, - new GroupBy(Collections.emptyList(), 0), new OrderBy(Collections.emptyList(), false), - new SelectItems(0, 0, false, Collections.emptyList()), new Pagination(null, null, Collections.emptyList())), - new CommonOptimizedStatement(selectStatement), new ShardingConditions(Collections.emptyList())); + SQLRouteResult result = new SQLRouteResult(new SelectSQLStatementContext(selectStatement, + new GroupByContext(Collections.emptyList(), 0), new OrderByContext(Collections.emptyList(), false), + new SelectItemsContext(0, 0, false, Collections.emptyList()), new PaginationContext(null, null, Collections.emptyList())), + new CommonSQLStatementContext(selectStatement), new ShardingConditions(Collections.emptyList())); RoutingResult routingResult = new RoutingResult(); routingResult.getRoutingUnits().add(new RoutingUnit("ds")); result.setRoutingResult(routingResult); @@ -850,8 +850,8 @@ public final class ShardingSQLRewriteEngineTest { insertStatement.getColumns().add(new ColumnSegment(33, 41, "order_id")); insertStatement.getColumns().add(new ColumnSegment(43, 50, "user_id")); insertStatement.getColumns().add(new ColumnSegment(52, 58, "status")); - InsertOptimizedStatement shardingStatement = new InsertOptimizedStatement(null, Collections.emptyList(), insertStatement); - SQLRouteResult result = new SQLRouteResult(shardingStatement, new CommonOptimizedStatement(insertStatement), new ShardingConditions(Collections.emptyList())); + InsertSQLStatementContext insertSQLStatementContext = new InsertSQLStatementContext(null, Collections.emptyList(), insertStatement); + SQLRouteResult result = new SQLRouteResult(insertSQLStatementContext, new CommonSQLStatementContext(insertStatement), new ShardingConditions(Collections.emptyList())); result.setRoutingResult(new RoutingResult()); return result; } @@ -868,8 +868,8 @@ public final class ShardingSQLRewriteEngineTest { updateStatement.getAllSQLSegments().add(new TableSegment(7, 27, "table_x")); updateStatement.setSetAssignment( new SetAssignmentsSegment(28, 42, Collections.singleton(new AssignmentSegment(33, 42, new ColumnSegment(33, 40, "id"), new LiteralExpressionSegment(41, 42, 1))))); - SQLRouteResult result = new SQLRouteResult(new CommonOptimizedStatement(updateStatement), - new CommonOptimizedStatement(updateStatement), new ShardingConditions(Collections.emptyList())); + SQLRouteResult result = new SQLRouteResult(new CommonSQLStatementContext(updateStatement), + new CommonSQLStatementContext(updateStatement), new ShardingConditions(Collections.emptyList())); result.setRoutingResult(new RoutingResult()); return result; } @@ -883,8 +883,8 @@ public final class ShardingSQLRewriteEngineTest { private SQLRouteResult createSQLRouteResultForTableTokenWithSchemaForDelete() { DeleteStatement deleteStatement = new DeleteStatement(); deleteStatement.getAllSQLSegments().add(new TableSegment(12, 34, "`table_x`")); - SQLRouteResult result = new SQLRouteResult(new CommonOptimizedStatement(deleteStatement), - new CommonOptimizedStatement(deleteStatement), new ShardingConditions(Collections.emptyList())); + SQLRouteResult result = new SQLRouteResult(new CommonSQLStatementContext(deleteStatement), + new CommonSQLStatementContext(deleteStatement), new ShardingConditions(Collections.emptyList())); RoutingResult routingResult = new RoutingResult(); routingResult.getRoutingUnits().add(new RoutingUnit("ds")); result.setRoutingResult(routingResult); @@ -915,10 +915,10 @@ public final class ShardingSQLRewriteEngineTest { WhereSegment whereSegment = new WhereSegment(23, 51, 2); whereSegment.getAndPredicates().add(andPredicate); selectStatement.setWhere(whereSegment); - SQLRouteResult result = new SQLRouteResult(new SelectOptimizedStatement(selectStatement, - new GroupBy(Collections.emptyList(), 0), new OrderBy(Collections.emptyList(), false), - new SelectItems(0, 0, false, Collections.emptyList()), new Pagination(null, null, Collections.emptyList())), - new CommonOptimizedStatement(selectStatement), new ShardingConditions(Collections.emptyList())); + SQLRouteResult result = new SQLRouteResult(new SelectSQLStatementContext(selectStatement, + new GroupByContext(Collections.emptyList(), 0), new OrderByContext(Collections.emptyList(), false), + new SelectItemsContext(0, 0, false, Collections.emptyList()), new PaginationContext(null, null, Collections.emptyList())), + new CommonSQLStatementContext(selectStatement), new ShardingConditions(Collections.emptyList())); result.setRoutingResult(new RoutingResult()); return result; } @@ -943,10 +943,10 @@ public final class ShardingSQLRewriteEngineTest { WhereSegment whereSegment = new WhereSegment(29, 46, 0); whereSegment.getAndPredicates().add(andPredicate); selectStatement.setWhere(whereSegment); - SQLRouteResult result = new SQLRouteResult(new SelectOptimizedStatement(selectStatement, - new GroupBy(Collections.emptyList(), 0), new OrderBy(Collections.emptyList(), false), - new SelectItems(0, 0, false, Collections.emptyList()), new Pagination(null, null, Collections.emptyList())), - new CommonOptimizedStatement(selectStatement), new ShardingConditions(Collections.emptyList())); + SQLRouteResult result = new SQLRouteResult(new SelectSQLStatementContext(selectStatement, + new GroupByContext(Collections.emptyList(), 0), new OrderByContext(Collections.emptyList(), false), + new SelectItemsContext(0, 0, false, Collections.emptyList()), new PaginationContext(null, null, Collections.emptyList())), + new CommonSQLStatementContext(selectStatement), new ShardingConditions(Collections.emptyList())); result.setRoutingResult(new RoutingResult()); return result; } @@ -967,8 +967,8 @@ public final class ShardingSQLRewriteEngineTest { WhereSegment whereSegment = new WhereSegment(26, 38, 0); whereSegment.getAndPredicates().add(andPredicate); updateStatement.setWhere(whereSegment); - SQLRouteResult result = new SQLRouteResult(new CommonOptimizedStatement(updateStatement), - new CommonOptimizedStatement(updateStatement), new ShardingConditions(Collections.emptyList())); + SQLRouteResult result = new SQLRouteResult(new CommonSQLStatementContext(updateStatement), + new CommonSQLStatementContext(updateStatement), new ShardingConditions(Collections.emptyList())); result.setRoutingResult(new RoutingResult()); return result; } @@ -990,11 +990,11 @@ public final class ShardingSQLRewriteEngineTest { LiteralExpressionSegment expressionSegment = new LiteralExpressionSegment(33, 34, 10); insertStatement.getAllSQLSegments().add(new SetAssignmentsSegment(26, 34, Collections.singleton(new AssignmentSegment(26, 34, columnSegment, expressionSegment)))); insertStatement.setSetAssignment(new SetAssignmentsSegment(26, 34, Collections.singleton(new AssignmentSegment(26, 34, columnSegment, expressionSegment)))); - InsertOptimizedStatement shardingStatement = new InsertOptimizedStatement(null, Collections.emptyList(), insertStatement); - shardingStatement.getInsertValues().get(0).appendValue(1, ShardingDerivedColumnType.KEY_GEN); + InsertSQLStatementContext insertSQLStatementContext = new InsertSQLStatementContext(null, Collections.emptyList(), insertStatement); + insertSQLStatementContext.getInsertValueContexts().get(0).appendValue(1, ShardingDerivedColumnType.KEY_GEN); ShardingCondition shardingCondition = new ShardingCondition(); shardingCondition.getDataNodes().add(new DataNode("db0.table_1")); - SQLRouteResult result = new SQLRouteResult(shardingStatement, new CommonOptimizedStatement(insertStatement), new ShardingConditions(Collections.singletonList(shardingCondition))); + SQLRouteResult result = new SQLRouteResult(insertSQLStatementContext, new CommonSQLStatementContext(insertStatement), new ShardingConditions(Collections.singletonList(shardingCondition))); result.setRoutingResult(new RoutingResult()); return result; } @@ -1011,10 +1011,10 @@ public final class ShardingSQLRewriteEngineTest { selectStatement.getAllSQLSegments().add(new TableSegment(49, 55, "table_z")); SelectItem selectItem1 = new AggregationDistinctSelectItem(7, 24, AggregationType.COUNT, "(DISTINCT id)", "a", "id"); SelectItem selectItem2 = new AggregationDistinctSelectItem(27, 42, AggregationType.SUM, "(DISTINCT id)", "a", "id"); - SelectItems selectItems = new SelectItems(7, 42, true, Arrays.asList(selectItem1, selectItem2)); - SQLRouteResult result = new SQLRouteResult(new SelectOptimizedStatement(selectStatement, - new GroupBy(Collections.emptyList(), 0), new OrderBy(Collections.emptyList(), false), - selectItems, new Pagination(null, null, Collections.emptyList())), new CommonOptimizedStatement(selectStatement), + SelectItemsContext selectItemsContext = new SelectItemsContext(7, 42, true, Arrays.asList(selectItem1, selectItem2)); + SQLRouteResult result = new SQLRouteResult(new SelectSQLStatementContext(selectStatement, + new GroupByContext(Collections.emptyList(), 0), new OrderByContext(Collections.emptyList(), false), + selectItemsContext, new PaginationContext(null, null, Collections.emptyList())), new CommonSQLStatementContext(selectStatement), new ShardingConditions(Collections.emptyList())); result.setRoutingResult(new RoutingResult()); return result; @@ -1047,10 +1047,10 @@ public final class ShardingSQLRewriteEngineTest { WhereSegment whereSegment = new WhereSegment(23, 44, 2); whereSegment.getAndPredicates().add(andPredicate); selectStatement.setWhere(whereSegment); - SQLRouteResult result = new SQLRouteResult(new SelectOptimizedStatement(selectStatement, - new GroupBy(Collections.emptyList(), 0), new OrderBy(Collections.emptyList(), false), - new SelectItems(0, 0, false, Collections.emptyList()), new Pagination(null, null, Collections.emptyList())), - new CommonOptimizedStatement(selectStatement), new ShardingConditions(Collections.emptyList())); + SQLRouteResult result = new SQLRouteResult(new SelectSQLStatementContext(selectStatement, + new GroupByContext(Collections.emptyList(), 0), new OrderByContext(Collections.emptyList(), false), + new SelectItemsContext(0, 0, false, Collections.emptyList()), new PaginationContext(null, null, Collections.emptyList())), + new CommonSQLStatementContext(selectStatement), new ShardingConditions(Collections.emptyList())); result.setRoutingResult(new RoutingResult()); return result; } @@ -1087,10 +1087,10 @@ public final class ShardingSQLRewriteEngineTest { WhereSegment whereSegment = new WhereSegment(23, 51, 2); whereSegment.getAndPredicates().add(andPredicate); selectStatement.setWhere(whereSegment); - SQLRouteResult result = new SQLRouteResult(new SelectOptimizedStatement(selectStatement, - new GroupBy(Collections.emptyList(), 0), new OrderBy(Collections.emptyList(), false), - new SelectItems(0, 0, false, Collections.emptyList()), new Pagination(null, null, Collections.emptyList())), - new CommonOptimizedStatement(selectStatement), new ShardingConditions(Collections.emptyList())); + SQLRouteResult result = new SQLRouteResult(new SelectSQLStatementContext(selectStatement, + new GroupByContext(Collections.emptyList(), 0), new OrderByContext(Collections.emptyList(), false), + new SelectItemsContext(0, 0, false, Collections.emptyList()), new PaginationContext(null, null, Collections.emptyList())), + new CommonSQLStatementContext(selectStatement), new ShardingConditions(Collections.emptyList())); result.setRoutingResult(new RoutingResult()); return result; } @@ -1116,10 +1116,10 @@ public final class ShardingSQLRewriteEngineTest { WhereSegment whereSegment = new WhereSegment(32, 53, 2); whereSegment.getAndPredicates().add(andPredicate); selectStatement.setWhere(whereSegment); - SQLRouteResult result = new SQLRouteResult(new SelectOptimizedStatement(selectStatement, - new GroupBy(Collections.emptyList(), 0), new OrderBy(Collections.emptyList(), false), - new SelectItems(0, 0, false, Collections.emptyList()), new Pagination(null, null, Collections.emptyList())), - new CommonOptimizedStatement(selectStatement), new ShardingConditions(Collections.emptyList())); + SQLRouteResult result = new SQLRouteResult(new SelectSQLStatementContext(selectStatement, + new GroupByContext(Collections.emptyList(), 0), new OrderByContext(Collections.emptyList(), false), + new SelectItemsContext(0, 0, false, Collections.emptyList()), new PaginationContext(null, null, Collections.emptyList())), + new CommonSQLStatementContext(selectStatement), new ShardingConditions(Collections.emptyList())); result.setRoutingResult(new RoutingResult()); return result; } diff --git a/sharding-core/sharding-core-rewrite/src/test/java/org/apache/shardingsphere/core/rewrite/token/SQLTokenGenerateEngineTest.java b/sharding-core/sharding-core-rewrite/src/test/java/org/apache/shardingsphere/core/rewrite/token/SQLTokenGenerateEngineTest.java index 4fa71de23d..9dc247976a 100644 --- a/sharding-core/sharding-core-rewrite/src/test/java/org/apache/shardingsphere/core/rewrite/token/SQLTokenGenerateEngineTest.java +++ b/sharding-core/sharding-core-rewrite/src/test/java/org/apache/shardingsphere/core/rewrite/token/SQLTokenGenerateEngineTest.java @@ -17,14 +17,14 @@ package org.apache.shardingsphere.core.rewrite.token; -import org.apache.shardingsphere.core.optimize.segment.select.groupby.GroupBy; +import org.apache.shardingsphere.core.optimize.segment.select.groupby.GroupByContext; import org.apache.shardingsphere.core.optimize.segment.select.item.impl.AggregationDistinctSelectItem; import org.apache.shardingsphere.core.optimize.segment.select.item.SelectItem; -import org.apache.shardingsphere.core.optimize.segment.select.item.SelectItems; -import org.apache.shardingsphere.core.optimize.segment.select.orderby.OrderBy; +import org.apache.shardingsphere.core.optimize.segment.select.item.SelectItemsContext; +import org.apache.shardingsphere.core.optimize.segment.select.orderby.OrderByContext; import org.apache.shardingsphere.core.optimize.segment.select.orderby.OrderByItem; -import org.apache.shardingsphere.core.optimize.segment.select.pagination.Pagination; -import org.apache.shardingsphere.core.optimize.statement.impl.SelectOptimizedStatement; +import org.apache.shardingsphere.core.optimize.segment.select.pagination.PaginationContext; +import org.apache.shardingsphere.core.optimize.statement.impl.SelectSQLStatementContext; import org.apache.shardingsphere.core.parse.core.constant.AggregationType; import org.apache.shardingsphere.core.parse.sql.statement.dml.SelectStatement; import org.apache.shardingsphere.core.rewrite.encrypt.EncryptCondition; @@ -60,12 +60,13 @@ public final class SQLTokenGenerateEngineTest { @Before public void setUp() { SelectStatement selectStatement = new SelectStatement(); - SelectItems selectItems = new SelectItems(1, 20, false, + SelectItemsContext selectItemsContext = new SelectItemsContext(1, 20, false, Collections.singletonList(new AggregationDistinctSelectItem(1, 2, AggregationType.COUNT, "(DISTINCT id)", "c", "id"))); - SelectOptimizedStatement optimizedStatement = new SelectOptimizedStatement(selectStatement, - new GroupBy(Collections.emptyList(), 1), new OrderBy(Collections.emptyList(), false), selectItems, new Pagination(null, null, Collections.emptyList())); + SelectSQLStatementContext selectSQLStatementContext = new SelectSQLStatementContext(selectStatement, + new GroupByContext(Collections.emptyList(), 0), new OrderByContext(Collections.emptyList(), false), + selectItemsContext, new PaginationContext(null, null, Collections.emptyList())); rewriteStatement = new RewriteStatement( - optimizedStatement, new ShardingConditions(Collections.emptyList()), new EncryptConditions(Collections.emptyList())); + selectSQLStatementContext, new ShardingConditions(Collections.emptyList()), new EncryptConditions(Collections.emptyList())); } @SuppressWarnings("unchecked") diff --git a/sharding-core/sharding-core-route/src/main/java/org/apache/shardingsphere/core/route/SQLLogger.java b/sharding-core/sharding-core-route/src/main/java/org/apache/shardingsphere/core/route/SQLLogger.java index f4f37991fe..f0c3853f43 100644 --- a/sharding-core/sharding-core-route/src/main/java/org/apache/shardingsphere/core/route/SQLLogger.java +++ b/sharding-core/sharding-core-route/src/main/java/org/apache/shardingsphere/core/route/SQLLogger.java @@ -21,7 +21,7 @@ import com.google.common.base.Joiner; import lombok.AccessLevel; import lombok.NoArgsConstructor; import lombok.extern.slf4j.Slf4j; -import org.apache.shardingsphere.core.optimize.statement.OptimizedStatement; +import org.apache.shardingsphere.core.optimize.statement.SQLStatementContext; import java.util.Collection; import java.util.HashSet; @@ -63,13 +63,13 @@ public final class SQLLogger { * * @param logicSQL logic SQL * @param showSimple whether show SQL in simple style - * @param optimizedStatement optimized statement + * @param sqlStatementContext SQL statement context * @param routeUnits route units */ - public static void logSQL(final String logicSQL, final boolean showSimple, final OptimizedStatement optimizedStatement, final Collection routeUnits) { + public static void logSQL(final String logicSQL, final boolean showSimple, final SQLStatementContext sqlStatementContext, final Collection routeUnits) { log("Rule Type: sharding"); log("Logic SQL: {}", logicSQL); - log("SQLStatement: {}", optimizedStatement); + log("SQLStatement: {}", sqlStatementContext); if (showSimple) { logSimpleMode(routeUnits); } else { diff --git a/sharding-core/sharding-core-route/src/main/java/org/apache/shardingsphere/core/route/SQLRouteResult.java b/sharding-core/sharding-core-route/src/main/java/org/apache/shardingsphere/core/route/SQLRouteResult.java index 1347bc5e9b..8386b71893 100644 --- a/sharding-core/sharding-core-route/src/main/java/org/apache/shardingsphere/core/route/SQLRouteResult.java +++ b/sharding-core/sharding-core-route/src/main/java/org/apache/shardingsphere/core/route/SQLRouteResult.java @@ -21,7 +21,7 @@ import com.google.common.base.Optional; import lombok.Getter; import lombok.RequiredArgsConstructor; import lombok.Setter; -import org.apache.shardingsphere.core.optimize.statement.OptimizedStatement; +import org.apache.shardingsphere.core.optimize.statement.SQLStatementContext; import org.apache.shardingsphere.core.route.router.sharding.condition.ShardingConditions; import org.apache.shardingsphere.core.route.router.sharding.keygen.GeneratedKey; import org.apache.shardingsphere.core.route.type.RoutingResult; @@ -41,9 +41,9 @@ import java.util.LinkedHashSet; @Setter public final class SQLRouteResult { - private final OptimizedStatement shardingStatement; + private final SQLStatementContext shardingStatementContext; - private final OptimizedStatement encryptStatement; + private final SQLStatementContext encryptStatementContext; private final ShardingConditions shardingConditions; @@ -53,8 +53,8 @@ public final class SQLRouteResult { private RoutingResult routingResult; - public SQLRouteResult(final OptimizedStatement shardingStatement, final OptimizedStatement encryptStatement, final ShardingConditions shardingConditions) { - this(shardingStatement, encryptStatement, shardingConditions, null); + public SQLRouteResult(final SQLStatementContext shardingStatementContext, final SQLStatementContext encryptStatementContext, final ShardingConditions shardingConditions) { + this(shardingStatementContext, encryptStatementContext, shardingConditions, null); } /** diff --git a/sharding-core/sharding-core-route/src/main/java/org/apache/shardingsphere/core/route/router/masterslave/ShardingMasterSlaveRouter.java b/sharding-core/sharding-core-route/src/main/java/org/apache/shardingsphere/core/route/router/masterslave/ShardingMasterSlaveRouter.java index c1a3ef5c18..84eaccee1b 100644 --- a/sharding-core/sharding-core-route/src/main/java/org/apache/shardingsphere/core/route/router/masterslave/ShardingMasterSlaveRouter.java +++ b/sharding-core/sharding-core-route/src/main/java/org/apache/shardingsphere/core/route/router/masterslave/ShardingMasterSlaveRouter.java @@ -61,7 +61,7 @@ public final class ShardingMasterSlaveRouter { } toBeRemoved.add(each); String actualDataSourceName; - if (isMasterRoute(sqlRouteResult.getShardingStatement().getSqlStatement())) { + if (isMasterRoute(sqlRouteResult.getShardingStatementContext().getSqlStatement())) { MasterVisitedManager.setMasterVisited(); actualDataSourceName = masterSlaveRule.getMasterDataSourceName(); } else { diff --git a/sharding-core/sharding-core-route/src/main/java/org/apache/shardingsphere/core/route/router/sharding/RoutingEngineFactory.java b/sharding-core/sharding-core-route/src/main/java/org/apache/shardingsphere/core/route/router/sharding/RoutingEngineFactory.java index 3cdb962489..42856e9eec 100644 --- a/sharding-core/sharding-core-route/src/main/java/org/apache/shardingsphere/core/route/router/sharding/RoutingEngineFactory.java +++ b/sharding-core/sharding-core-route/src/main/java/org/apache/shardingsphere/core/route/router/sharding/RoutingEngineFactory.java @@ -20,7 +20,7 @@ package org.apache.shardingsphere.core.route.router.sharding; import lombok.AccessLevel; import lombok.NoArgsConstructor; import org.apache.shardingsphere.core.metadata.ShardingSphereMetaData; -import org.apache.shardingsphere.core.optimize.statement.OptimizedStatement; +import org.apache.shardingsphere.core.optimize.statement.SQLStatementContext; import org.apache.shardingsphere.core.parse.sql.statement.SQLStatement; import org.apache.shardingsphere.core.parse.sql.statement.dal.DALStatement; import org.apache.shardingsphere.core.parse.sql.statement.dal.dialect.mysql.ShowDatabasesStatement; @@ -60,25 +60,25 @@ public final class RoutingEngineFactory { * * @param shardingRule sharding rule * @param metaData meta data of ShardingSphere - * @param optimizedStatement optimized statement + * @param sqlStatementContext SQL statement context * @param shardingConditions shardingConditions * @return new instance of routing engine */ public static RoutingEngine newInstance(final ShardingRule shardingRule, - final ShardingSphereMetaData metaData, final OptimizedStatement optimizedStatement, final ShardingConditions shardingConditions) { - SQLStatement sqlStatement = optimizedStatement.getSqlStatement(); - Collection tableNames = optimizedStatement.getTables().getTableNames(); + final ShardingSphereMetaData metaData, final SQLStatementContext sqlStatementContext, final ShardingConditions shardingConditions) { + SQLStatement sqlStatement = sqlStatementContext.getSqlStatement(); + Collection tableNames = sqlStatementContext.getTablesContext().getTableNames(); if (sqlStatement instanceof TCLStatement) { return new DatabaseBroadcastRoutingEngine(shardingRule); } if (sqlStatement instanceof DDLStatement) { - return new TableBroadcastRoutingEngine(shardingRule, metaData.getTables(), optimizedStatement); + return new TableBroadcastRoutingEngine(shardingRule, metaData.getTables(), sqlStatementContext); } if (sqlStatement instanceof DALStatement) { return getDALRoutingEngine(shardingRule, sqlStatement, tableNames); } if (sqlStatement instanceof DCLStatement) { - return getDCLRoutingEngine(shardingRule, optimizedStatement, metaData); + return getDCLRoutingEngine(shardingRule, sqlStatementContext, metaData); } if (shardingRule.isAllInDefaultDataSource(tableNames)) { return new DefaultDatabaseRoutingEngine(shardingRule, tableNames); @@ -86,10 +86,10 @@ public final class RoutingEngineFactory { if (shardingRule.isAllBroadcastTables(tableNames)) { return sqlStatement instanceof SelectStatement ? new UnicastRoutingEngine(shardingRule, tableNames) : new DatabaseBroadcastRoutingEngine(shardingRule); } - if (optimizedStatement.getSqlStatement() instanceof DMLStatement && shardingConditions.isAlwaysFalse() || tableNames.isEmpty()) { + if (sqlStatementContext.getSqlStatement() instanceof DMLStatement && shardingConditions.isAlwaysFalse() || tableNames.isEmpty()) { return new UnicastRoutingEngine(shardingRule, tableNames); } - return getShardingRoutingEngine(shardingRule, optimizedStatement, shardingConditions, tableNames); + return getShardingRoutingEngine(shardingRule, sqlStatementContext, shardingConditions, tableNames); } private static RoutingEngine getDALRoutingEngine(final ShardingRule shardingRule, final SQLStatement sqlStatement, final Collection tableNames) { @@ -105,22 +105,22 @@ public final class RoutingEngineFactory { return new DataSourceGroupBroadcastRoutingEngine(shardingRule); } - private static RoutingEngine getDCLRoutingEngine(final ShardingRule shardingRule, final OptimizedStatement optimizedStatement, final ShardingSphereMetaData metaData) { - return isGrantForSingleTable(optimizedStatement) - ? new TableBroadcastRoutingEngine(shardingRule, metaData.getTables(), optimizedStatement) : new MasterInstanceBroadcastRoutingEngine(shardingRule, metaData.getDataSources()); + private static RoutingEngine getDCLRoutingEngine(final ShardingRule shardingRule, final SQLStatementContext sqlStatementContext, final ShardingSphereMetaData metaData) { + return isGrantForSingleTable(sqlStatementContext) + ? new TableBroadcastRoutingEngine(shardingRule, metaData.getTables(), sqlStatementContext) : new MasterInstanceBroadcastRoutingEngine(shardingRule, metaData.getDataSources()); } - private static boolean isGrantForSingleTable(final OptimizedStatement optimizedStatement) { - return !optimizedStatement.getTables().isEmpty() && !"*".equals(optimizedStatement.getTables().getSingleTableName()); + private static boolean isGrantForSingleTable(final SQLStatementContext sqlStatementContext) { + return !sqlStatementContext.getTablesContext().isEmpty() && !"*".equals(sqlStatementContext.getTablesContext().getSingleTableName()); } - private static RoutingEngine getShardingRoutingEngine(final ShardingRule shardingRule, final OptimizedStatement optimizedStatement, + private static RoutingEngine getShardingRoutingEngine(final ShardingRule shardingRule, final SQLStatementContext sqlStatementContext, final ShardingConditions shardingConditions, final Collection tableNames) { Collection shardingTableNames = shardingRule.getShardingLogicTableNames(tableNames); if (1 == shardingTableNames.size() || shardingRule.isAllBindingTables(shardingTableNames)) { - return new StandardRoutingEngine(shardingRule, shardingTableNames.iterator().next(), optimizedStatement, shardingConditions); + return new StandardRoutingEngine(shardingRule, shardingTableNames.iterator().next(), sqlStatementContext, shardingConditions); } // TODO config for cartesian set - return new ComplexRoutingEngine(shardingRule, tableNames, optimizedStatement, shardingConditions); + return new ComplexRoutingEngine(shardingRule, tableNames, sqlStatementContext, shardingConditions); } } diff --git a/sharding-core/sharding-core-route/src/main/java/org/apache/shardingsphere/core/route/router/sharding/ShardingRouter.java b/sharding-core/sharding-core-route/src/main/java/org/apache/shardingsphere/core/route/router/sharding/ShardingRouter.java index 5f6a7cdaa3..f3b621c8ec 100644 --- a/sharding-core/sharding-core-route/src/main/java/org/apache/shardingsphere/core/route/router/sharding/ShardingRouter.java +++ b/sharding-core/sharding-core-route/src/main/java/org/apache/shardingsphere/core/route/router/sharding/ShardingRouter.java @@ -22,10 +22,10 @@ import com.google.common.base.Preconditions; import lombok.RequiredArgsConstructor; import org.apache.shardingsphere.api.hint.HintManager; import org.apache.shardingsphere.core.metadata.ShardingSphereMetaData; -import org.apache.shardingsphere.core.optimize.statement.impl.InsertOptimizedStatement; -import org.apache.shardingsphere.core.optimize.statement.OptimizedStatement; -import org.apache.shardingsphere.core.optimize.OptimizedStatementFactory; -import org.apache.shardingsphere.core.optimize.statement.impl.SelectOptimizedStatement; +import org.apache.shardingsphere.core.optimize.statement.impl.InsertSQLStatementContext; +import org.apache.shardingsphere.core.optimize.statement.SQLStatementContext; +import org.apache.shardingsphere.core.optimize.SQLStatementContextFactory; +import org.apache.shardingsphere.core.optimize.statement.impl.SelectSQLStatementContext; import org.apache.shardingsphere.core.parse.SQLParseEngine; import org.apache.shardingsphere.core.parse.sql.statement.SQLStatement; import org.apache.shardingsphere.core.parse.sql.statement.dml.DMLStatement; @@ -94,47 +94,47 @@ public final class ShardingRouter { if (shardingStatementValidator.isPresent()) { shardingStatementValidator.get().validate(shardingRule, sqlStatement); } - OptimizedStatement shardingStatement = OptimizedStatementFactory.newInstance(metaData.getTables(), logicSQL, parameters, sqlStatement); + SQLStatementContext sqlStatementContext = SQLStatementContextFactory.newInstance(metaData.getTables(), logicSQL, parameters, sqlStatement); Optional generatedKey = sqlStatement instanceof InsertStatement ? GeneratedKey.getGenerateKey(shardingRule, metaData.getTables(), parameters, (InsertStatement) sqlStatement) : Optional.absent(); - ShardingConditions shardingConditions = getShardingConditions(parameters, shardingStatement, generatedKey.orNull()); - boolean needMergeShardingValues = isNeedMergeShardingValues(shardingStatement); - if (shardingStatement.getSqlStatement() instanceof DMLStatement && needMergeShardingValues) { - checkSubqueryShardingValues(shardingStatement, shardingConditions); + ShardingConditions shardingConditions = getShardingConditions(parameters, sqlStatementContext, generatedKey.orNull()); + boolean needMergeShardingValues = isNeedMergeShardingValues(sqlStatementContext); + if (sqlStatementContext.getSqlStatement() instanceof DMLStatement && needMergeShardingValues) { + checkSubqueryShardingValues(sqlStatementContext, shardingConditions); mergeShardingConditions(shardingConditions); } - RoutingEngine routingEngine = RoutingEngineFactory.newInstance(shardingRule, metaData, shardingStatement, shardingConditions); + RoutingEngine routingEngine = RoutingEngineFactory.newInstance(shardingRule, metaData, sqlStatementContext, shardingConditions); RoutingResult routingResult = routingEngine.route(); if (needMergeShardingValues) { Preconditions.checkState(1 == routingResult.getRoutingUnits().size(), "Must have one sharding with subquery."); } - OptimizedStatement encryptStatement = OptimizedStatementFactory.newInstance(metaData.getTables(), logicSQL, parameters, sqlStatement); - SQLRouteResult result = new SQLRouteResult(shardingStatement, encryptStatement, shardingConditions, generatedKey.orNull()); + SQLStatementContext encryptStatement = SQLStatementContextFactory.newInstance(metaData.getTables(), logicSQL, parameters, sqlStatement); + SQLRouteResult result = new SQLRouteResult(sqlStatementContext, encryptStatement, shardingConditions, generatedKey.orNull()); result.setRoutingResult(routingResult); - if (shardingStatement instanceof InsertOptimizedStatement) { + if (sqlStatementContext instanceof InsertSQLStatementContext) { setGeneratedValues(result); } return result; } - private ShardingConditions getShardingConditions(final List parameters, final OptimizedStatement shardingStatement, final GeneratedKey generatedKey) { - if (shardingStatement.getSqlStatement() instanceof DMLStatement) { - if (shardingStatement instanceof InsertOptimizedStatement) { - InsertOptimizedStatement shardingInsertStatement = (InsertOptimizedStatement) shardingStatement; + private ShardingConditions getShardingConditions(final List parameters, final SQLStatementContext sqlStatementContext, final GeneratedKey generatedKey) { + if (sqlStatementContext.getSqlStatement() instanceof DMLStatement) { + if (sqlStatementContext instanceof InsertSQLStatementContext) { + InsertSQLStatementContext shardingInsertStatement = (InsertSQLStatementContext) sqlStatementContext; return new ShardingConditions(new InsertClauseShardingConditionEngine(shardingRule).createShardingConditions(shardingInsertStatement, generatedKey, parameters)); } - return new ShardingConditions(new WhereClauseShardingConditionEngine(shardingRule, metaData.getTables()).createShardingConditions(shardingStatement.getSqlStatement(), parameters)); + return new ShardingConditions(new WhereClauseShardingConditionEngine(shardingRule, metaData.getTables()).createShardingConditions(sqlStatementContext.getSqlStatement(), parameters)); } return new ShardingConditions(Collections.emptyList()); } - private boolean isNeedMergeShardingValues(final OptimizedStatement optimizedStatement) { - return optimizedStatement instanceof SelectOptimizedStatement && ((SelectOptimizedStatement) optimizedStatement).isContainsSubquery() - && !shardingRule.getShardingLogicTableNames(optimizedStatement.getTables().getTableNames()).isEmpty(); + private boolean isNeedMergeShardingValues(final SQLStatementContext sqlStatementContext) { + return sqlStatementContext instanceof SelectSQLStatementContext && ((SelectSQLStatementContext) sqlStatementContext).isContainsSubquery() + && !shardingRule.getShardingLogicTableNames(sqlStatementContext.getTablesContext().getTableNames()).isEmpty(); } - private void checkSubqueryShardingValues(final OptimizedStatement optimizedStatement, final ShardingConditions shardingConditions) { - for (String each : optimizedStatement.getTables().getTableNames()) { + private void checkSubqueryShardingValues(final SQLStatementContext sqlStatementContext, final ShardingConditions shardingConditions) { + for (String each : sqlStatementContext.getTablesContext().getTableNames()) { Optional tableRule = shardingRule.findTableRule(each); if (tableRule.isPresent() && shardingRule.isRoutingByHint(tableRule.get()) && !HintManager.getDatabaseShardingValues(each).isEmpty() && !HintManager.getTableShardingValues(each).isEmpty()) { diff --git a/sharding-core/sharding-core-route/src/main/java/org/apache/shardingsphere/core/route/router/sharding/condition/engine/InsertClauseShardingConditionEngine.java b/sharding-core/sharding-core-route/src/main/java/org/apache/shardingsphere/core/route/router/sharding/condition/engine/InsertClauseShardingConditionEngine.java index 0d503ec0b2..c9f2114bba 100644 --- a/sharding-core/sharding-core-route/src/main/java/org/apache/shardingsphere/core/route/router/sharding/condition/engine/InsertClauseShardingConditionEngine.java +++ b/sharding-core/sharding-core-route/src/main/java/org/apache/shardingsphere/core/route/router/sharding/condition/engine/InsertClauseShardingConditionEngine.java @@ -19,8 +19,8 @@ package org.apache.shardingsphere.core.route.router.sharding.condition.engine; import com.google.common.base.Preconditions; import lombok.RequiredArgsConstructor; -import org.apache.shardingsphere.core.optimize.segment.insert.InsertValue; -import org.apache.shardingsphere.core.optimize.statement.impl.InsertOptimizedStatement; +import org.apache.shardingsphere.core.optimize.segment.insert.InsertValueContext; +import org.apache.shardingsphere.core.optimize.statement.impl.InsertSQLStatementContext; import org.apache.shardingsphere.core.parse.sql.segment.dml.expr.ExpressionSegment; import org.apache.shardingsphere.core.parse.sql.segment.dml.expr.simple.LiteralExpressionSegment; import org.apache.shardingsphere.core.parse.sql.segment.dml.expr.simple.ParameterMarkerExpressionSegment; @@ -49,16 +49,16 @@ public final class InsertClauseShardingConditionEngine { /** * Create sharding conditions. * - * @param shardingStatement sharding insert optimized statement + * @param insertSQLStatementContext insert SQL statement context * @param generatedKey generated key * @param parameters SQL parameters * @return sharding conditions */ - public List createShardingConditions(final InsertOptimizedStatement shardingStatement, final GeneratedKey generatedKey, final List parameters) { + public List createShardingConditions(final InsertSQLStatementContext insertSQLStatementContext, final GeneratedKey generatedKey, final List parameters) { List result = new LinkedList<>(); - String tableName = shardingStatement.getTables().getSingleTableName(); - Collection columnNames = getColumnNames(shardingStatement, generatedKey); - for (InsertValue each : shardingStatement.getInsertValues()) { + String tableName = insertSQLStatementContext.getTablesContext().getSingleTableName(); + Collection columnNames = getColumnNames(insertSQLStatementContext, generatedKey); + for (InsertValueContext each : insertSQLStatementContext.getInsertValueContexts()) { result.add(createShardingCondition(tableName, columnNames.iterator(), each, parameters)); } if (null != generatedKey && generatedKey.isGenerated() && shardingRule.isShardingColumn(generatedKey.getColumnName(), tableName)) { @@ -67,18 +67,18 @@ public final class InsertClauseShardingConditionEngine { return result; } - private Collection getColumnNames(final InsertOptimizedStatement shardingStatement, final GeneratedKey generatedKey) { + private Collection getColumnNames(final InsertSQLStatementContext insertSQLStatementContext, final GeneratedKey generatedKey) { if (null == generatedKey || !generatedKey.isGenerated()) { - return shardingStatement.getColumnNames(); + return insertSQLStatementContext.getColumnNames(); } - Collection result = new LinkedList<>(shardingStatement.getColumnNames()); + Collection result = new LinkedList<>(insertSQLStatementContext.getColumnNames()); result.remove(generatedKey.getColumnName()); return result; } - private ShardingCondition createShardingCondition(final String tableName, final Iterator columnNames, final InsertValue insertValue, final List parameters) { + private ShardingCondition createShardingCondition(final String tableName, final Iterator columnNames, final InsertValueContext insertValueContext, final List parameters) { ShardingCondition result = new ShardingCondition(); - for (ExpressionSegment each : insertValue.getValueExpressions()) { + for (ExpressionSegment each : insertValueContext.getValueExpressions()) { String columnName = columnNames.next(); if (each instanceof SimpleExpressionSegment && shardingRule.isShardingColumn(columnName, tableName)) { result.getRouteValues().add(new ListRouteValue<>(columnName, tableName, Collections.singletonList(getRouteValue((SimpleExpressionSegment) each, parameters)))); diff --git a/sharding-core/sharding-core-route/src/main/java/org/apache/shardingsphere/core/route/router/sharding/condition/engine/WhereClauseShardingConditionEngine.java b/sharding-core/sharding-core-route/src/main/java/org/apache/shardingsphere/core/route/router/sharding/condition/engine/WhereClauseShardingConditionEngine.java index 4954ce8a5f..47fc16b30d 100644 --- a/sharding-core/sharding-core-route/src/main/java/org/apache/shardingsphere/core/route/router/sharding/condition/engine/WhereClauseShardingConditionEngine.java +++ b/sharding-core/sharding-core-route/src/main/java/org/apache/shardingsphere/core/route/router/sharding/condition/engine/WhereClauseShardingConditionEngine.java @@ -23,7 +23,7 @@ import lombok.RequiredArgsConstructor; import org.apache.shardingsphere.core.exception.ShardingException; import org.apache.shardingsphere.core.metadata.table.TableMetas; import org.apache.shardingsphere.core.route.router.sharding.condition.Column; -import org.apache.shardingsphere.core.optimize.segment.Tables; +import org.apache.shardingsphere.core.optimize.segment.table.TablesContext; import org.apache.shardingsphere.core.route.router.sharding.condition.AlwaysFalseRouteValue; import org.apache.shardingsphere.core.route.router.sharding.condition.AlwaysFalseShardingCondition; import org.apache.shardingsphere.core.route.router.sharding.condition.ShardingCondition; @@ -73,13 +73,13 @@ public final class WhereClauseShardingConditionEngine { } List result = new ArrayList<>(); Optional whereSegment = ((WhereSegmentAvailable) sqlStatement).getWhere(); - Tables tables = new Tables(sqlStatement); + TablesContext tablesContext = new TablesContext(sqlStatement); if (whereSegment.isPresent()) { - result.addAll(createShardingConditions(tables, whereSegment.get().getAndPredicates(), parameters)); + result.addAll(createShardingConditions(tablesContext, whereSegment.get().getAndPredicates(), parameters)); } Collection subqueryPredicateSegments = sqlStatement.findSQLSegments(SubqueryPredicateSegment.class); for (SubqueryPredicateSegment each : subqueryPredicateSegments) { - Collection subqueryShardingConditions = createShardingConditions(tables, each.getAndPredicates(), parameters); + Collection subqueryShardingConditions = createShardingConditions(tablesContext, each.getAndPredicates(), parameters); if (!result.containsAll(subqueryShardingConditions)) { result.addAll(subqueryShardingConditions); } @@ -87,10 +87,10 @@ public final class WhereClauseShardingConditionEngine { return result; } - private Collection createShardingConditions(final Tables tables, final Collection andPredicates, final List parameters) { + private Collection createShardingConditions(final TablesContext tablesContext, final Collection andPredicates, final List parameters) { Collection result = new LinkedList<>(); for (AndPredicate each : andPredicates) { - Map> routeValueMap = createRouteValueMap(tables, each, parameters); + Map> routeValueMap = createRouteValueMap(tablesContext, each, parameters); if (routeValueMap.isEmpty()) { return Collections.emptyList(); } @@ -99,10 +99,10 @@ public final class WhereClauseShardingConditionEngine { return result; } - private Map> createRouteValueMap(final Tables tables, final AndPredicate andPredicate, final List parameters) { + private Map> createRouteValueMap(final TablesContext tablesContext, final AndPredicate andPredicate, final List parameters) { Map> result = new HashMap<>(); for (PredicateSegment each : andPredicate.getPredicates()) { - Optional tableName = tables.findTableName(each.getColumn(), tableMetas); + Optional tableName = tablesContext.findTableName(each.getColumn(), tableMetas); if (!tableName.isPresent() || !shardingRule.isShardingColumn(each.getColumn().getName(), tableName.get())) { continue; } diff --git a/sharding-core/sharding-core-route/src/main/java/org/apache/shardingsphere/core/route/router/sharding/validator/impl/ShardingUpdateStatementValidator.java b/sharding-core/sharding-core-route/src/main/java/org/apache/shardingsphere/core/route/router/sharding/validator/impl/ShardingUpdateStatementValidator.java index 58a9c16af9..e4f57839d0 100644 --- a/sharding-core/sharding-core-route/src/main/java/org/apache/shardingsphere/core/route/router/sharding/validator/impl/ShardingUpdateStatementValidator.java +++ b/sharding-core/sharding-core-route/src/main/java/org/apache/shardingsphere/core/route/router/sharding/validator/impl/ShardingUpdateStatementValidator.java @@ -18,7 +18,7 @@ package org.apache.shardingsphere.core.route.router.sharding.validator.impl; import org.apache.shardingsphere.core.exception.ShardingException; -import org.apache.shardingsphere.core.optimize.segment.Tables; +import org.apache.shardingsphere.core.optimize.segment.table.TablesContext; import org.apache.shardingsphere.core.parse.sql.segment.dml.assignment.AssignmentSegment; import org.apache.shardingsphere.core.parse.sql.statement.dml.UpdateStatement; import org.apache.shardingsphere.core.route.router.sharding.validator.ShardingStatementValidator; @@ -33,7 +33,7 @@ public final class ShardingUpdateStatementValidator implements ShardingStatement @Override public void validate(final ShardingRule shardingRule, final UpdateStatement sqlStatement) { - String tableName = new Tables(sqlStatement).getSingleTableName(); + String tableName = new TablesContext(sqlStatement).getSingleTableName(); for (AssignmentSegment each : sqlStatement.getSetAssignment().getAssignments()) { if (shardingRule.isShardingColumn(each.getColumn().getName(), tableName)) { throw new ShardingException("Can not update sharding key, logic table: [%s], column: [%s].", tableName, each); diff --git a/sharding-core/sharding-core-route/src/main/java/org/apache/shardingsphere/core/route/type/broadcast/TableBroadcastRoutingEngine.java b/sharding-core/sharding-core-route/src/main/java/org/apache/shardingsphere/core/route/type/broadcast/TableBroadcastRoutingEngine.java index 2a8d1e188d..ce24408924 100644 --- a/sharding-core/sharding-core-route/src/main/java/org/apache/shardingsphere/core/route/type/broadcast/TableBroadcastRoutingEngine.java +++ b/sharding-core/sharding-core-route/src/main/java/org/apache/shardingsphere/core/route/type/broadcast/TableBroadcastRoutingEngine.java @@ -21,7 +21,7 @@ import com.google.common.base.Optional; import com.google.common.base.Preconditions; import lombok.RequiredArgsConstructor; import org.apache.shardingsphere.core.metadata.table.TableMetas; -import org.apache.shardingsphere.core.optimize.statement.OptimizedStatement; +import org.apache.shardingsphere.core.optimize.statement.SQLStatementContext; import org.apache.shardingsphere.core.parse.sql.segment.ddl.index.IndexSegment; import org.apache.shardingsphere.core.parse.sql.statement.ddl.DropIndexStatement; import org.apache.shardingsphere.core.route.type.RoutingEngine; @@ -49,7 +49,7 @@ public final class TableBroadcastRoutingEngine implements RoutingEngine { private final TableMetas tableMetas; - private final OptimizedStatement optimizedStatement; + private final SQLStatementContext sqlStatementContext; @Override public RoutingResult route() { @@ -61,8 +61,8 @@ public final class TableBroadcastRoutingEngine implements RoutingEngine { } private Collection getLogicTableNames() { - return optimizedStatement.getSqlStatement() instanceof DropIndexStatement - ? getTableNames((DropIndexStatement) optimizedStatement.getSqlStatement()) : optimizedStatement.getTables().getTableNames(); + return sqlStatementContext.getSqlStatement() instanceof DropIndexStatement + ? getTableNames((DropIndexStatement) sqlStatementContext.getSqlStatement()) : sqlStatementContext.getTablesContext().getTableNames(); } private Collection getTableNames(final DropIndexStatement dropIndexStatement) { diff --git a/sharding-core/sharding-core-route/src/main/java/org/apache/shardingsphere/core/route/type/complex/ComplexRoutingEngine.java b/sharding-core/sharding-core-route/src/main/java/org/apache/shardingsphere/core/route/type/complex/ComplexRoutingEngine.java index 08ac06c8ea..426d2b473b 100644 --- a/sharding-core/sharding-core-route/src/main/java/org/apache/shardingsphere/core/route/type/complex/ComplexRoutingEngine.java +++ b/sharding-core/sharding-core-route/src/main/java/org/apache/shardingsphere/core/route/type/complex/ComplexRoutingEngine.java @@ -22,7 +22,7 @@ import com.google.common.base.Optional; import com.google.common.collect.Lists; import lombok.RequiredArgsConstructor; import org.apache.shardingsphere.core.exception.ShardingException; -import org.apache.shardingsphere.core.optimize.statement.OptimizedStatement; +import org.apache.shardingsphere.core.optimize.statement.SQLStatementContext; import org.apache.shardingsphere.core.route.router.sharding.condition.ShardingConditions; import org.apache.shardingsphere.core.route.type.RoutingEngine; import org.apache.shardingsphere.core.route.type.RoutingResult; @@ -48,7 +48,7 @@ public final class ComplexRoutingEngine implements RoutingEngine { private final Collection logicTables; - private final OptimizedStatement optimizedStatement; + private final SQLStatementContext sqlStatementContext; private final ShardingConditions shardingConditions; @@ -60,7 +60,7 @@ public final class ComplexRoutingEngine implements RoutingEngine { Optional tableRule = shardingRule.findTableRule(each); if (tableRule.isPresent()) { if (!bindingTableNames.contains(each)) { - result.add(new StandardRoutingEngine(shardingRule, tableRule.get().getLogicTable(), optimizedStatement, shardingConditions).route()); + result.add(new StandardRoutingEngine(shardingRule, tableRule.get().getLogicTable(), sqlStatementContext, shardingConditions).route()); } Optional bindingTableRule = shardingRule.findBindingTableRule(each); if (bindingTableRule.isPresent()) { diff --git a/sharding-core/sharding-core-route/src/main/java/org/apache/shardingsphere/core/route/type/standard/StandardRoutingEngine.java b/sharding-core/sharding-core-route/src/main/java/org/apache/shardingsphere/core/route/type/standard/StandardRoutingEngine.java index f5809028d6..4b451f499d 100644 --- a/sharding-core/sharding-core-route/src/main/java/org/apache/shardingsphere/core/route/type/standard/StandardRoutingEngine.java +++ b/sharding-core/sharding-core-route/src/main/java/org/apache/shardingsphere/core/route/type/standard/StandardRoutingEngine.java @@ -22,7 +22,7 @@ import com.google.common.base.Preconditions; import lombok.RequiredArgsConstructor; import org.apache.shardingsphere.api.hint.HintManager; import org.apache.shardingsphere.core.exception.ShardingException; -import org.apache.shardingsphere.core.optimize.statement.OptimizedStatement; +import org.apache.shardingsphere.core.optimize.statement.SQLStatementContext; import org.apache.shardingsphere.core.parse.sql.statement.SQLStatement; import org.apache.shardingsphere.core.parse.sql.statement.dml.DeleteStatement; import org.apache.shardingsphere.core.parse.sql.statement.dml.InsertStatement; @@ -63,14 +63,14 @@ public final class StandardRoutingEngine implements RoutingEngine { private final String logicTableName; - private final OptimizedStatement optimizedStatement; + private final SQLStatementContext sqlStatementContext; private final ShardingConditions shardingConditions; @Override public RoutingResult route() { - if (isDMLForModify(optimizedStatement.getSqlStatement()) && !optimizedStatement.getTables().isSingleTable()) { - throw new ShardingException("Cannot support Multiple-Table for '%s'.", optimizedStatement.getSqlStatement()); + if (isDMLForModify(sqlStatementContext.getSqlStatement()) && !sqlStatementContext.getTablesContext().isSingleTable()) { + throw new ShardingException("Cannot support Multiple-Table for '%s'.", sqlStatementContext.getSqlStatement()); } return generateRoutingResult(getDataNodes(shardingRule.getTableRule(logicTableName))); } diff --git a/sharding-core/sharding-core-route/src/test/java/org/apache/shardingsphere/core/route/DatabaseTest.java b/sharding-core/sharding-core-route/src/test/java/org/apache/shardingsphere/core/route/DatabaseTest.java index 27077bc657..39c8386e57 100644 --- a/sharding-core/sharding-core-route/src/test/java/org/apache/shardingsphere/core/route/DatabaseTest.java +++ b/sharding-core/sharding-core-route/src/test/java/org/apache/shardingsphere/core/route/DatabaseTest.java @@ -28,7 +28,7 @@ import org.apache.shardingsphere.core.metadata.column.ColumnMetaData; import org.apache.shardingsphere.core.metadata.datasource.DataSourceMetas; import org.apache.shardingsphere.core.metadata.table.TableMetaData; import org.apache.shardingsphere.core.metadata.table.TableMetas; -import org.apache.shardingsphere.core.optimize.statement.impl.SelectOptimizedStatement; +import org.apache.shardingsphere.core.optimize.statement.impl.SelectSQLStatementContext; import org.apache.shardingsphere.core.parse.SQLParseEngine; import org.apache.shardingsphere.core.route.fixture.HintShardingAlgorithmFixture; import org.apache.shardingsphere.core.rule.ShardingRule; @@ -92,12 +92,12 @@ public final class DatabaseTest { String originSQL = "select user_id from tbl_pagination limit 0,5"; SQLParseEngine parseEngine = new SQLParseEngine(DatabaseTypes.getActualDatabaseType("MySQL")); SQLRouteResult actual = new StatementRoutingEngine(shardingRule, getMetaDataForPagination(), parseEngine).route(originSQL); - assertThat(((SelectOptimizedStatement) actual.getShardingStatement()).getPagination().getActualOffset(), is(0L)); - assertThat(((SelectOptimizedStatement) actual.getShardingStatement()).getPagination().getActualRowCount().orNull(), is(5L)); + assertThat(((SelectSQLStatementContext) actual.getShardingStatementContext()).getPaginationContext().getActualOffset(), is(0L)); + assertThat(((SelectSQLStatementContext) actual.getShardingStatementContext()).getPaginationContext().getActualRowCount().orNull(), is(5L)); originSQL = "select user_id from tbl_pagination limit 5,5"; actual = new StatementRoutingEngine(shardingRule, getMetaDataForPagination(), parseEngine).route(originSQL); - assertThat(((SelectOptimizedStatement) actual.getShardingStatement()).getPagination().getActualOffset(), is(5L)); - assertThat(((SelectOptimizedStatement) actual.getShardingStatement()).getPagination().getActualRowCount().orNull(), is(5L)); + assertThat(((SelectSQLStatementContext) actual.getShardingStatementContext()).getPaginationContext().getActualOffset(), is(5L)); + assertThat(((SelectSQLStatementContext) actual.getShardingStatementContext()).getPaginationContext().getActualRowCount().orNull(), is(5L)); } private ShardingSphereMetaData getMetaDataForPagination() { @@ -131,12 +131,12 @@ public final class DatabaseTest { when(metaData.getTables()).thenReturn(mock(TableMetas.class)); SQLParseEngine parseEngine = new SQLParseEngine(DatabaseTypes.getActualDatabaseType("MySQL")); SQLRouteResult actual = new PreparedStatementRoutingEngine(originSQL, rule, metaData, parseEngine).route(Lists.newArrayList(13, 173)); - assertThat(((SelectOptimizedStatement) actual.getShardingStatement()).getPagination().getActualOffset(), is(5L)); - assertThat(((SelectOptimizedStatement) actual.getShardingStatement()).getPagination().getActualRowCount().orNull(), is(10L)); + assertThat(((SelectSQLStatementContext) actual.getShardingStatementContext()).getPaginationContext().getActualOffset(), is(5L)); + assertThat(((SelectSQLStatementContext) actual.getShardingStatementContext()).getPaginationContext().getActualRowCount().orNull(), is(10L)); assertThat(actual.getRoutingResult().getRoutingUnits().size(), is(1)); originSQL = "select city_id from t_user where city_id in (?,?) limit 5,10"; actual = new PreparedStatementRoutingEngine(originSQL, rule, metaData, parseEngine).route(Lists.newArrayList(89, 84)); - assertThat(((SelectOptimizedStatement) actual.getShardingStatement()).getPagination().getActualOffset(), is(5L)); - assertThat(((SelectOptimizedStatement) actual.getShardingStatement()).getPagination().getActualRowCount().orNull(), is(10L)); + assertThat(((SelectSQLStatementContext) actual.getShardingStatementContext()).getPaginationContext().getActualOffset(), is(5L)); + assertThat(((SelectSQLStatementContext) actual.getShardingStatementContext()).getPaginationContext().getActualRowCount().orNull(), is(10L)); } } diff --git a/sharding-core/sharding-core-route/src/test/java/org/apache/shardingsphere/core/route/router/masterslave/ShardingMasterSlaveRouterTest.java b/sharding-core/sharding-core-route/src/test/java/org/apache/shardingsphere/core/route/router/masterslave/ShardingMasterSlaveRouterTest.java index c08b63e984..a3c246392e 100644 --- a/sharding-core/sharding-core-route/src/test/java/org/apache/shardingsphere/core/route/router/masterslave/ShardingMasterSlaveRouterTest.java +++ b/sharding-core/sharding-core-route/src/test/java/org/apache/shardingsphere/core/route/router/masterslave/ShardingMasterSlaveRouterTest.java @@ -18,7 +18,7 @@ package org.apache.shardingsphere.core.route.router.masterslave; import com.google.common.collect.Lists; -import org.apache.shardingsphere.core.optimize.statement.OptimizedStatement; +import org.apache.shardingsphere.core.optimize.statement.SQLStatementContext; import org.apache.shardingsphere.core.parse.sql.statement.SQLStatement; import org.apache.shardingsphere.core.parse.sql.statement.dml.InsertStatement; import org.apache.shardingsphere.core.parse.sql.statement.dml.SelectStatement; @@ -56,7 +56,7 @@ public class ShardingMasterSlaveRouterTest { private MasterSlaveRule masterSlaveRule; @Mock - private OptimizedStatement shardingOptimizedStatement; + private SQLStatementContext shardingStatementContext; @Mock private InsertStatement insertStatement; @@ -99,8 +99,8 @@ public class ShardingMasterSlaveRouterTest { } private SQLRouteResult mockSQLRouteResult(final SQLStatement sqlStatement) { - when(shardingOptimizedStatement.getSqlStatement()).thenReturn(sqlStatement); - SQLRouteResult result = new SQLRouteResult(shardingOptimizedStatement, null, null, null); + when(shardingStatementContext.getSqlStatement()).thenReturn(sqlStatement); + SQLRouteResult result = new SQLRouteResult(shardingStatementContext, null, null, null); result.setRoutingResult(mockRoutingResult()); return result; } diff --git a/sharding-core/sharding-core-route/src/test/java/org/apache/shardingsphere/core/route/router/sharding/RoutingEngineFactoryTest.java b/sharding-core/sharding-core-route/src/test/java/org/apache/shardingsphere/core/route/router/sharding/RoutingEngineFactoryTest.java index 632bb846a3..c0afeaadb7 100644 --- a/sharding-core/sharding-core-route/src/test/java/org/apache/shardingsphere/core/route/router/sharding/RoutingEngineFactoryTest.java +++ b/sharding-core/sharding-core-route/src/test/java/org/apache/shardingsphere/core/route/router/sharding/RoutingEngineFactoryTest.java @@ -18,8 +18,8 @@ package org.apache.shardingsphere.core.route.router.sharding; import org.apache.shardingsphere.core.metadata.ShardingSphereMetaData; -import org.apache.shardingsphere.core.optimize.segment.Tables; -import org.apache.shardingsphere.core.optimize.statement.OptimizedStatement; +import org.apache.shardingsphere.core.optimize.segment.table.TablesContext; +import org.apache.shardingsphere.core.optimize.statement.SQLStatementContext; import org.apache.shardingsphere.core.parse.sql.statement.SQLStatement; import org.apache.shardingsphere.core.parse.sql.statement.dal.DALStatement; import org.apache.shardingsphere.core.parse.sql.statement.dal.dialect.mysql.ShowDatabasesStatement; @@ -65,10 +65,10 @@ public class RoutingEngineFactoryTest { private ShardingSphereMetaData shardingSphereMetaData; @Mock - private OptimizedStatement optimizedStatement; + private SQLStatementContext sqlStatementContext; @Mock - private Tables tables; + private TablesContext tablesContext; @Mock private ShardingConditions shardingConditions; @@ -77,24 +77,24 @@ public class RoutingEngineFactoryTest { @Before public void setUp() { - when(optimizedStatement.getTables()).thenReturn(tables); + when(sqlStatementContext.getTablesContext()).thenReturn(tablesContext); tableNames = new ArrayList<>(); - when(tables.getTableNames()).thenReturn(tableNames); + when(tablesContext.getTableNames()).thenReturn(tableNames); } @Test public void assertNewInstanceForTCL() { TCLStatement tclStatement = mock(TCLStatement.class); - when(optimizedStatement.getSqlStatement()).thenReturn(tclStatement); - RoutingEngine actual = RoutingEngineFactory.newInstance(shardingRule, shardingSphereMetaData, optimizedStatement, shardingConditions); + when(sqlStatementContext.getSqlStatement()).thenReturn(tclStatement); + RoutingEngine actual = RoutingEngineFactory.newInstance(shardingRule, shardingSphereMetaData, sqlStatementContext, shardingConditions); assertThat(actual, instanceOf(DatabaseBroadcastRoutingEngine.class)); } @Test public void assertNewInstanceForDDL() { DDLStatement ddlStatement = mock(DDLStatement.class); - when(optimizedStatement.getSqlStatement()).thenReturn(ddlStatement); - RoutingEngine actual = RoutingEngineFactory.newInstance(shardingRule, shardingSphereMetaData, optimizedStatement, shardingConditions); + when(sqlStatementContext.getSqlStatement()).thenReturn(ddlStatement); + RoutingEngine actual = RoutingEngineFactory.newInstance(shardingRule, shardingSphereMetaData, sqlStatementContext, shardingConditions); assertThat(actual, instanceOf(TableBroadcastRoutingEngine.class)); } @@ -102,51 +102,51 @@ public class RoutingEngineFactoryTest { public void assertNewInstanceForDALWithTables() { tableNames.add(""); DALStatement dalStatement = mock(DALStatement.class); - when(optimizedStatement.getSqlStatement()).thenReturn(dalStatement); - RoutingEngine actual = RoutingEngineFactory.newInstance(shardingRule, shardingSphereMetaData, optimizedStatement, shardingConditions); + when(sqlStatementContext.getSqlStatement()).thenReturn(dalStatement); + RoutingEngine actual = RoutingEngineFactory.newInstance(shardingRule, shardingSphereMetaData, sqlStatementContext, shardingConditions); assertThat(actual, instanceOf(UnicastRoutingEngine.class)); } @Test public void assertNewInstanceForDALWithoutTables() { DALStatement dalStatement = mock(DALStatement.class); - when(optimizedStatement.getSqlStatement()).thenReturn(dalStatement); - RoutingEngine actual = RoutingEngineFactory.newInstance(shardingRule, shardingSphereMetaData, optimizedStatement, shardingConditions); + when(sqlStatementContext.getSqlStatement()).thenReturn(dalStatement); + RoutingEngine actual = RoutingEngineFactory.newInstance(shardingRule, shardingSphereMetaData, sqlStatementContext, shardingConditions); assertThat(actual, instanceOf(DataSourceGroupBroadcastRoutingEngine.class)); } @Test public void assertNewInstanceForDALShow() { DALStatement dalStatement = mock(ShowDatabasesStatement.class); - when(optimizedStatement.getSqlStatement()).thenReturn(dalStatement); - RoutingEngine actual = RoutingEngineFactory.newInstance(shardingRule, shardingSphereMetaData, optimizedStatement, shardingConditions); + when(sqlStatementContext.getSqlStatement()).thenReturn(dalStatement); + RoutingEngine actual = RoutingEngineFactory.newInstance(shardingRule, shardingSphereMetaData, sqlStatementContext, shardingConditions); assertThat(actual, instanceOf(IgnoreRoutingEngine.class)); } @Test public void assertNewInstanceForDALSet() { DALStatement dalStatement = mock(SetStatement.class); - when(optimizedStatement.getSqlStatement()).thenReturn(dalStatement); - RoutingEngine actual = RoutingEngineFactory.newInstance(shardingRule, shardingSphereMetaData, optimizedStatement, shardingConditions); + when(sqlStatementContext.getSqlStatement()).thenReturn(dalStatement); + RoutingEngine actual = RoutingEngineFactory.newInstance(shardingRule, shardingSphereMetaData, sqlStatementContext, shardingConditions); assertThat(actual, instanceOf(DatabaseBroadcastRoutingEngine.class)); } @Test public void assertNewInstanceForDCLForSingleTable() { - when(tables.isEmpty()).thenReturn(false); - when(tables.getSingleTableName()).thenReturn(""); + when(tablesContext.isEmpty()).thenReturn(false); + when(tablesContext.getSingleTableName()).thenReturn(""); DCLStatement dclStatement = mock(DCLStatement.class); - when(optimizedStatement.getSqlStatement()).thenReturn(dclStatement); - RoutingEngine actual = RoutingEngineFactory.newInstance(shardingRule, shardingSphereMetaData, optimizedStatement, shardingConditions); + when(sqlStatementContext.getSqlStatement()).thenReturn(dclStatement); + RoutingEngine actual = RoutingEngineFactory.newInstance(shardingRule, shardingSphereMetaData, sqlStatementContext, shardingConditions); assertThat(actual, instanceOf(TableBroadcastRoutingEngine.class)); } @Test public void assertNewInstanceForDCLForNoSingleTable() { - when(tables.isEmpty()).thenReturn(true); + when(tablesContext.isEmpty()).thenReturn(true); DCLStatement dclStatement = mock(DCLStatement.class); - when(optimizedStatement.getSqlStatement()).thenReturn(dclStatement); - RoutingEngine actual = RoutingEngineFactory.newInstance(shardingRule, shardingSphereMetaData, optimizedStatement, shardingConditions); + when(sqlStatementContext.getSqlStatement()).thenReturn(dclStatement); + RoutingEngine actual = RoutingEngineFactory.newInstance(shardingRule, shardingSphereMetaData, sqlStatementContext, shardingConditions); assertThat(actual, instanceOf(MasterInstanceBroadcastRoutingEngine.class)); } @@ -154,8 +154,8 @@ public class RoutingEngineFactoryTest { public void assertNewInstanceForDefaultDataSource() { when(shardingRule.isAllInDefaultDataSource(tableNames)).thenReturn(true); SQLStatement sqlStatement = mock(SQLStatement.class); - when(optimizedStatement.getSqlStatement()).thenReturn(sqlStatement); - RoutingEngine actual = RoutingEngineFactory.newInstance(shardingRule, shardingSphereMetaData, optimizedStatement, shardingConditions); + when(sqlStatementContext.getSqlStatement()).thenReturn(sqlStatement); + RoutingEngine actual = RoutingEngineFactory.newInstance(shardingRule, shardingSphereMetaData, sqlStatementContext, shardingConditions); assertThat(actual, instanceOf(DefaultDatabaseRoutingEngine.class)); } @@ -163,8 +163,8 @@ public class RoutingEngineFactoryTest { public void assertNewInstanceForInsertBroadcastTable() { when(shardingRule.isAllBroadcastTables(tableNames)).thenReturn(true); SQLStatement sqlStatement = mock(InsertStatement.class); - when(optimizedStatement.getSqlStatement()).thenReturn(sqlStatement); - RoutingEngine actual = RoutingEngineFactory.newInstance(shardingRule, shardingSphereMetaData, optimizedStatement, shardingConditions); + when(sqlStatementContext.getSqlStatement()).thenReturn(sqlStatement); + RoutingEngine actual = RoutingEngineFactory.newInstance(shardingRule, shardingSphereMetaData, sqlStatementContext, shardingConditions); assertThat(actual, instanceOf(DatabaseBroadcastRoutingEngine.class)); } @@ -172,37 +172,37 @@ public class RoutingEngineFactoryTest { public void assertNewInstanceForSelectBroadcastTable() { when(shardingRule.isAllBroadcastTables(tableNames)).thenReturn(true); SQLStatement sqlStatement = mock(SelectStatement.class); - when(optimizedStatement.getSqlStatement()).thenReturn(sqlStatement); - RoutingEngine actual = RoutingEngineFactory.newInstance(shardingRule, shardingSphereMetaData, optimizedStatement, shardingConditions); + when(sqlStatementContext.getSqlStatement()).thenReturn(sqlStatement); + RoutingEngine actual = RoutingEngineFactory.newInstance(shardingRule, shardingSphereMetaData, sqlStatementContext, shardingConditions); assertThat(actual, instanceOf(UnicastRoutingEngine.class)); } @Test public void assertNewInstanceForAlwaysFalse() { SQLStatement sqlStatement = mock(SQLStatement.class); - when(optimizedStatement.getSqlStatement()).thenReturn(sqlStatement); - RoutingEngine actual = RoutingEngineFactory.newInstance(shardingRule, shardingSphereMetaData, optimizedStatement, shardingConditions); + when(sqlStatementContext.getSqlStatement()).thenReturn(sqlStatement); + RoutingEngine actual = RoutingEngineFactory.newInstance(shardingRule, shardingSphereMetaData, sqlStatementContext, shardingConditions); assertThat(actual, instanceOf(UnicastRoutingEngine.class)); } @Test public void assertNewInstanceForStandard() { SQLStatement sqlStatement = mock(SQLStatement.class); - when(optimizedStatement.getSqlStatement()).thenReturn(sqlStatement); + when(sqlStatementContext.getSqlStatement()).thenReturn(sqlStatement); tableNames.add(""); when(shardingRule.getShardingLogicTableNames(tableNames)).thenReturn(tableNames); - RoutingEngine actual = RoutingEngineFactory.newInstance(shardingRule, shardingSphereMetaData, optimizedStatement, shardingConditions); + RoutingEngine actual = RoutingEngineFactory.newInstance(shardingRule, shardingSphereMetaData, sqlStatementContext, shardingConditions); assertThat(actual, instanceOf(StandardRoutingEngine.class)); } @Test public void assertNewInstanceForComplex() { SQLStatement sqlStatement = mock(SQLStatement.class); - when(optimizedStatement.getSqlStatement()).thenReturn(sqlStatement); + when(sqlStatementContext.getSqlStatement()).thenReturn(sqlStatement); tableNames.add("1"); tableNames.add("2"); when(shardingRule.getShardingLogicTableNames(tableNames)).thenReturn(tableNames); - RoutingEngine actual = RoutingEngineFactory.newInstance(shardingRule, shardingSphereMetaData, optimizedStatement, shardingConditions); + RoutingEngine actual = RoutingEngineFactory.newInstance(shardingRule, shardingSphereMetaData, sqlStatementContext, shardingConditions); assertThat(actual, instanceOf(ComplexRoutingEngine.class)); } } diff --git a/sharding-core/sharding-core-route/src/test/java/org/apache/shardingsphere/core/route/type/broadcast/TableBroadcastRoutingEngineTest.java b/sharding-core/sharding-core-route/src/test/java/org/apache/shardingsphere/core/route/type/broadcast/TableBroadcastRoutingEngineTest.java index 2945677d97..86a211ac2c 100644 --- a/sharding-core/sharding-core-route/src/test/java/org/apache/shardingsphere/core/route/type/broadcast/TableBroadcastRoutingEngineTest.java +++ b/sharding-core/sharding-core-route/src/test/java/org/apache/shardingsphere/core/route/type/broadcast/TableBroadcastRoutingEngineTest.java @@ -22,8 +22,8 @@ import org.apache.shardingsphere.api.config.sharding.ShardingRuleConfiguration; import org.apache.shardingsphere.api.config.sharding.TableRuleConfiguration; import org.apache.shardingsphere.core.metadata.table.TableMetaData; import org.apache.shardingsphere.core.metadata.table.TableMetas; -import org.apache.shardingsphere.core.optimize.segment.Tables; -import org.apache.shardingsphere.core.optimize.statement.OptimizedStatement; +import org.apache.shardingsphere.core.optimize.segment.table.TablesContext; +import org.apache.shardingsphere.core.optimize.statement.SQLStatementContext; import org.apache.shardingsphere.core.parse.sql.segment.ddl.index.IndexSegment; import org.apache.shardingsphere.core.parse.sql.statement.ddl.DDLStatement; import org.apache.shardingsphere.core.parse.sql.statement.ddl.DropIndexStatement; @@ -49,10 +49,10 @@ import static org.mockito.Mockito.when; public final class TableBroadcastRoutingEngineTest { @Mock - private OptimizedStatement optimizedStatement; + private SQLStatementContext sqlStatementContext; @Mock - private Tables tables; + private TablesContext tablesContext; @Mock private TableMetas tableMetas; @@ -68,18 +68,18 @@ public final class TableBroadcastRoutingEngineTest { ShardingRuleConfiguration shardingRuleConfig = new ShardingRuleConfiguration(); shardingRuleConfig.getTableRuleConfigs().add(tableRuleConfig); ShardingRule shardingRule = new ShardingRule(shardingRuleConfig, Arrays.asList("ds0", "ds1")); - when(optimizedStatement.getTables()).thenReturn(tables); - when(tables.getTableNames()).thenReturn(Lists.newArrayList("t_order")); + when(sqlStatementContext.getTablesContext()).thenReturn(tablesContext); + when(tablesContext.getTableNames()).thenReturn(Lists.newArrayList("t_order")); when(tableMetas.getAllTableNames()).thenReturn(Lists.newArrayList("t_order")); when(tableMetas.get("t_order")).thenReturn(tableMetaData); when(tableMetaData.containsIndex("index_name")).thenReturn(true); - tableBroadcastRoutingEngine = new TableBroadcastRoutingEngine(shardingRule, tableMetas, optimizedStatement); + tableBroadcastRoutingEngine = new TableBroadcastRoutingEngine(shardingRule, tableMetas, sqlStatementContext); } @Test public void assertRouteForNormalDDL() { DDLStatement ddlStatement = mock(DDLStatement.class); - when(optimizedStatement.getSqlStatement()).thenReturn(ddlStatement); + when(sqlStatementContext.getSqlStatement()).thenReturn(ddlStatement); RoutingResult actual = tableBroadcastRoutingEngine.route(); assertRoutingResult(actual); } @@ -90,7 +90,7 @@ public final class TableBroadcastRoutingEngineTest { IndexSegment indexSegment = mock(IndexSegment.class); when(indexSegment.getName()).thenReturn("no_index"); when(indexStatement.getIndexes()).thenReturn(Lists.newArrayList(indexSegment)); - when(optimizedStatement.getSqlStatement()).thenReturn(indexStatement); + when(sqlStatementContext.getSqlStatement()).thenReturn(indexStatement); tableBroadcastRoutingEngine.route(); } @@ -100,7 +100,7 @@ public final class TableBroadcastRoutingEngineTest { IndexSegment indexSegment = mock(IndexSegment.class); when(indexSegment.getName()).thenReturn("index_name"); when(indexStatement.getIndexes()).thenReturn(Lists.newArrayList(indexSegment)); - when(optimizedStatement.getSqlStatement()).thenReturn(indexStatement); + when(sqlStatementContext.getSqlStatement()).thenReturn(indexStatement); RoutingResult actual = tableBroadcastRoutingEngine.route(); assertRoutingResult(actual); } diff --git a/sharding-core/sharding-core-route/src/test/java/org/apache/shardingsphere/core/route/type/complex/ComplexRoutingEngineTest.java b/sharding-core/sharding-core-route/src/test/java/org/apache/shardingsphere/core/route/type/complex/ComplexRoutingEngineTest.java index 6c725cea57..e700c97fc5 100644 --- a/sharding-core/sharding-core-route/src/test/java/org/apache/shardingsphere/core/route/type/complex/ComplexRoutingEngineTest.java +++ b/sharding-core/sharding-core-route/src/test/java/org/apache/shardingsphere/core/route/type/complex/ComplexRoutingEngineTest.java @@ -18,13 +18,13 @@ package org.apache.shardingsphere.core.route.type.complex; import org.apache.shardingsphere.core.exception.ShardingException; -import org.apache.shardingsphere.core.optimize.segment.select.groupby.GroupBy; +import org.apache.shardingsphere.core.optimize.segment.select.groupby.GroupByContext; import org.apache.shardingsphere.core.optimize.segment.select.item.SelectItem; -import org.apache.shardingsphere.core.optimize.segment.select.item.SelectItems; -import org.apache.shardingsphere.core.optimize.segment.select.orderby.OrderBy; +import org.apache.shardingsphere.core.optimize.segment.select.item.SelectItemsContext; +import org.apache.shardingsphere.core.optimize.segment.select.orderby.OrderByContext; import org.apache.shardingsphere.core.optimize.segment.select.orderby.OrderByItem; -import org.apache.shardingsphere.core.optimize.segment.select.pagination.Pagination; -import org.apache.shardingsphere.core.optimize.statement.impl.SelectOptimizedStatement; +import org.apache.shardingsphere.core.optimize.segment.select.pagination.PaginationContext; +import org.apache.shardingsphere.core.optimize.statement.impl.SelectSQLStatementContext; import org.apache.shardingsphere.core.parse.sql.statement.dml.SelectStatement; import org.apache.shardingsphere.core.route.fixture.AbstractRoutingEngineTest; import org.apache.shardingsphere.core.route.type.RoutingResult; @@ -46,11 +46,11 @@ public final class ComplexRoutingEngineTest extends AbstractRoutingEngineTest { @Test public void assertRoutingForBindingTables() { ShardingRule shardingRule = createBindingShardingRule(); - SelectOptimizedStatement optimizedStatement = new SelectOptimizedStatement(new SelectStatement(), - new GroupBy(Collections.emptyList(), 0), new OrderBy(Collections.emptyList(), false), - new SelectItems(0, 0, false, Collections.emptyList()), - new Pagination(null, null, Collections.emptyList())); - ComplexRoutingEngine complexRoutingEngine = new ComplexRoutingEngine(shardingRule, Arrays.asList("t_order", "t_order_item"), optimizedStatement, createShardingConditions("t_order")); + SelectSQLStatementContext selectSQLStatementContext = new SelectSQLStatementContext(new SelectStatement(), + new GroupByContext(Collections.emptyList(), 0), new OrderByContext(Collections.emptyList(), false), + new SelectItemsContext(0, 0, false, Collections.emptyList()), + new PaginationContext(null, null, Collections.emptyList())); + ComplexRoutingEngine complexRoutingEngine = new ComplexRoutingEngine(shardingRule, Arrays.asList("t_order", "t_order_item"), selectSQLStatementContext, createShardingConditions("t_order")); RoutingResult routingResult = complexRoutingEngine.route(); List tableUnitList = new ArrayList<>(routingResult.getRoutingUnits()); assertThat(routingResult, instanceOf(RoutingResult.class)); @@ -64,11 +64,11 @@ public final class ComplexRoutingEngineTest extends AbstractRoutingEngineTest { @Test public void assertRoutingForShardingTableJoinBroadcastTable() { ShardingRule shardingRule = createBroadcastShardingRule(); - SelectOptimizedStatement optimizedStatement = new SelectOptimizedStatement(new SelectStatement(), - new GroupBy(Collections.emptyList(), 0), new OrderBy(Collections.emptyList(), false), - new SelectItems(0, 0, false, Collections.emptyList()), - new Pagination(null, null, Collections.emptyList())); - ComplexRoutingEngine complexRoutingEngine = new ComplexRoutingEngine(shardingRule, Arrays.asList("t_order", "t_config"), optimizedStatement, createShardingConditions("t_order")); + SelectSQLStatementContext selectSQLStatementContext = new SelectSQLStatementContext(new SelectStatement(), + new GroupByContext(Collections.emptyList(), 0), new OrderByContext(Collections.emptyList(), false), + new SelectItemsContext(0, 0, false, Collections.emptyList()), + new PaginationContext(null, null, Collections.emptyList())); + ComplexRoutingEngine complexRoutingEngine = new ComplexRoutingEngine(shardingRule, Arrays.asList("t_order", "t_config"), selectSQLStatementContext, createShardingConditions("t_order")); RoutingResult routingResult = complexRoutingEngine.route(); List tableUnitList = new ArrayList<>(routingResult.getRoutingUnits()); assertThat(routingResult, instanceOf(RoutingResult.class)); diff --git a/sharding-core/sharding-core-route/src/test/java/org/apache/shardingsphere/core/route/type/standard/StandardRoutingEngineTest.java b/sharding-core/sharding-core-route/src/test/java/org/apache/shardingsphere/core/route/type/standard/StandardRoutingEngineTest.java index d0fe9a0a29..6a238a1c3c 100644 --- a/sharding-core/sharding-core-route/src/test/java/org/apache/shardingsphere/core/route/type/standard/StandardRoutingEngineTest.java +++ b/sharding-core/sharding-core-route/src/test/java/org/apache/shardingsphere/core/route/type/standard/StandardRoutingEngineTest.java @@ -19,15 +19,15 @@ package org.apache.shardingsphere.core.route.type.standard; import org.apache.shardingsphere.api.hint.HintManager; import org.apache.shardingsphere.core.exception.ShardingException; -import org.apache.shardingsphere.core.optimize.segment.Tables; -import org.apache.shardingsphere.core.optimize.segment.select.groupby.GroupBy; +import org.apache.shardingsphere.core.optimize.segment.table.TablesContext; +import org.apache.shardingsphere.core.optimize.segment.select.groupby.GroupByContext; import org.apache.shardingsphere.core.optimize.segment.select.item.SelectItem; -import org.apache.shardingsphere.core.optimize.segment.select.item.SelectItems; -import org.apache.shardingsphere.core.optimize.segment.select.orderby.OrderBy; +import org.apache.shardingsphere.core.optimize.segment.select.item.SelectItemsContext; +import org.apache.shardingsphere.core.optimize.segment.select.orderby.OrderByContext; import org.apache.shardingsphere.core.optimize.segment.select.orderby.OrderByItem; -import org.apache.shardingsphere.core.optimize.segment.select.pagination.Pagination; -import org.apache.shardingsphere.core.optimize.statement.OptimizedStatement; -import org.apache.shardingsphere.core.optimize.statement.impl.SelectOptimizedStatement; +import org.apache.shardingsphere.core.optimize.segment.select.pagination.PaginationContext; +import org.apache.shardingsphere.core.optimize.statement.SQLStatementContext; +import org.apache.shardingsphere.core.optimize.statement.impl.SelectSQLStatementContext; import org.apache.shardingsphere.core.parse.sql.statement.dml.InsertStatement; import org.apache.shardingsphere.core.parse.sql.statement.dml.SelectStatement; import org.apache.shardingsphere.core.route.fixture.AbstractRoutingEngineTest; @@ -58,12 +58,12 @@ public final class StandardRoutingEngineTest extends AbstractRoutingEngineTest { @Test(expected = ShardingException.class) public void assertRouteByUnsupported() { - OptimizedStatement optimizedStatement = mock(OptimizedStatement.class); - when(optimizedStatement.getSqlStatement()).thenReturn(new InsertStatement()); - Tables tables = mock(Tables.class); - when(tables.isSingleTable()).thenReturn(false); - when(optimizedStatement.getTables()).thenReturn(tables); - StandardRoutingEngine standardRoutingEngine = new StandardRoutingEngine(null, null, optimizedStatement, null); + SQLStatementContext sqlStatementContext = mock(SQLStatementContext.class); + when(sqlStatementContext.getSqlStatement()).thenReturn(new InsertStatement()); + TablesContext tablesContext = mock(TablesContext.class); + when(tablesContext.isSingleTable()).thenReturn(false); + when(sqlStatementContext.getTablesContext()).thenReturn(tablesContext); + StandardRoutingEngine standardRoutingEngine = new StandardRoutingEngine(null, null, sqlStatementContext, null); standardRoutingEngine.route(); } @@ -190,9 +190,9 @@ public final class StandardRoutingEngineTest extends AbstractRoutingEngineTest { } private StandardRoutingEngine createStandardRoutingEngine(final ShardingRule shardingRule, final String logicTableName, final ShardingConditions shardingConditions) { - return new StandardRoutingEngine(shardingRule, logicTableName, new SelectOptimizedStatement(new SelectStatement(), - new GroupBy(Collections.emptyList(), 0), new OrderBy(Collections.emptyList(), false), - new SelectItems(0, 0, false, Collections.emptyList()), - new Pagination(null, null, Collections.emptyList())), shardingConditions); + return new StandardRoutingEngine(shardingRule, logicTableName, new SelectSQLStatementContext(new SelectStatement(), + new GroupByContext(Collections.emptyList(), 0), new OrderByContext(Collections.emptyList(), false), + new SelectItemsContext(0, 0, false, Collections.emptyList()), + new PaginationContext(null, null, Collections.emptyList())), shardingConditions); } } diff --git a/sharding-jdbc/sharding-jdbc-core/src/main/java/org/apache/shardingsphere/shardingjdbc/executor/AbstractStatementExecutor.java b/sharding-jdbc/sharding-jdbc-core/src/main/java/org/apache/shardingsphere/shardingjdbc/executor/AbstractStatementExecutor.java index 5bbe78bbfe..563bd994f6 100644 --- a/sharding-jdbc/sharding-jdbc-core/src/main/java/org/apache/shardingsphere/shardingjdbc/executor/AbstractStatementExecutor.java +++ b/sharding-jdbc/sharding-jdbc-core/src/main/java/org/apache/shardingsphere/shardingjdbc/executor/AbstractStatementExecutor.java @@ -34,7 +34,7 @@ import org.apache.shardingsphere.core.execute.sql.execute.SQLExecuteTemplate; import org.apache.shardingsphere.core.execute.sql.prepare.SQLExecutePrepareTemplate; import org.apache.shardingsphere.core.metadata.table.TableMetaData; import org.apache.shardingsphere.core.metadata.table.TableMetas; -import org.apache.shardingsphere.core.optimize.statement.OptimizedStatement; +import org.apache.shardingsphere.core.optimize.statement.SQLStatementContext; import org.apache.shardingsphere.core.parse.sql.segment.ddl.index.IndexSegment; import org.apache.shardingsphere.core.parse.sql.statement.ddl.AlterTableStatement; import org.apache.shardingsphere.core.parse.sql.statement.ddl.CreateIndexStatement; @@ -85,7 +85,7 @@ public class AbstractStatementExecutor { @Getter @Setter - private OptimizedStatement optimizedStatement; + private SQLStatementContext sqlStatementContext; @Getter private final List> parameterSets = new LinkedList<>(); @@ -132,12 +132,12 @@ public class AbstractStatementExecutor { @SuppressWarnings("unchecked") protected final List executeCallback(final SQLExecuteCallback executeCallback) throws SQLException { List result = sqlExecuteTemplate.executeGroup((Collection) executeGroups, executeCallback); - refreshMetaDataIfNeeded(connection.getRuntimeContext(), optimizedStatement); + refreshMetaDataIfNeeded(connection.getRuntimeContext(), sqlStatementContext); return result; } protected final boolean isAccumulate() { - return !connection.getRuntimeContext().getRule().isAllBroadcastTables(optimizedStatement.getTables().getTableNames()); + return !connection.getRuntimeContext().getRule().isAllBroadcastTables(sqlStatementContext.getTablesContext().getTableNames()); } /** @@ -160,52 +160,52 @@ public class AbstractStatementExecutor { } } - private void refreshMetaDataIfNeeded(final ShardingRuntimeContext runtimeContext, final OptimizedStatement optimizedStatement) throws SQLException { - if (null == optimizedStatement) { + private void refreshMetaDataIfNeeded(final ShardingRuntimeContext runtimeContext, final SQLStatementContext sqlStatementContext) throws SQLException { + if (null == sqlStatementContext) { return; } - if (optimizedStatement.getSqlStatement() instanceof CreateTableStatement) { - refreshTableMetaDataForCreateTable(runtimeContext, optimizedStatement); - } else if (optimizedStatement.getSqlStatement() instanceof AlterTableStatement) { - refreshTableMetaDataForAlterTable(runtimeContext, optimizedStatement); - } else if (optimizedStatement.getSqlStatement() instanceof DropTableStatement) { - refreshTableMetaDataForDropTable(runtimeContext, optimizedStatement); - } else if (optimizedStatement.getSqlStatement() instanceof CreateIndexStatement) { - refreshTableMetaDataForCreateIndex(runtimeContext, optimizedStatement); - } else if (optimizedStatement.getSqlStatement() instanceof DropIndexStatement) { - refreshTableMetaDataForDropIndex(runtimeContext, optimizedStatement); + if (sqlStatementContext.getSqlStatement() instanceof CreateTableStatement) { + refreshTableMetaDataForCreateTable(runtimeContext, sqlStatementContext); + } else if (sqlStatementContext.getSqlStatement() instanceof AlterTableStatement) { + refreshTableMetaDataForAlterTable(runtimeContext, sqlStatementContext); + } else if (sqlStatementContext.getSqlStatement() instanceof DropTableStatement) { + refreshTableMetaDataForDropTable(runtimeContext, sqlStatementContext); + } else if (sqlStatementContext.getSqlStatement() instanceof CreateIndexStatement) { + refreshTableMetaDataForCreateIndex(runtimeContext, sqlStatementContext); + } else if (sqlStatementContext.getSqlStatement() instanceof DropIndexStatement) { + refreshTableMetaDataForDropIndex(runtimeContext, sqlStatementContext); } } - private void refreshTableMetaDataForCreateTable(final ShardingRuntimeContext runtimeContext, final OptimizedStatement optimizedStatement) throws SQLException { - String tableName = optimizedStatement.getTables().getSingleTableName(); + private void refreshTableMetaDataForCreateTable(final ShardingRuntimeContext runtimeContext, final SQLStatementContext sqlStatementContext) throws SQLException { + String tableName = sqlStatementContext.getTablesContext().getSingleTableName(); runtimeContext.getMetaData().getTables().put(tableName, getTableMetaDataInitializer().load(tableName, runtimeContext.getRule())); } - private void refreshTableMetaDataForAlterTable(final ShardingRuntimeContext runtimeContext, final OptimizedStatement optimizedStatement) throws SQLException { - String tableName = optimizedStatement.getTables().getSingleTableName(); + private void refreshTableMetaDataForAlterTable(final ShardingRuntimeContext runtimeContext, final SQLStatementContext sqlStatementContext) throws SQLException { + String tableName = sqlStatementContext.getTablesContext().getSingleTableName(); runtimeContext.getMetaData().getTables().put(tableName, getTableMetaDataInitializer().load(tableName, runtimeContext.getRule())); } - private void refreshTableMetaDataForDropTable(final ShardingRuntimeContext runtimeContext, final OptimizedStatement optimizedStatement) { - for (String each : optimizedStatement.getTables().getTableNames()) { + private void refreshTableMetaDataForDropTable(final ShardingRuntimeContext runtimeContext, final SQLStatementContext sqlStatementContext) { + for (String each : sqlStatementContext.getTablesContext().getTableNames()) { runtimeContext.getMetaData().getTables().remove(each); } } - private void refreshTableMetaDataForCreateIndex(final ShardingRuntimeContext runtimeContext, final OptimizedStatement optimizedStatement) { - CreateIndexStatement createIndexStatement = (CreateIndexStatement) optimizedStatement.getSqlStatement(); + private void refreshTableMetaDataForCreateIndex(final ShardingRuntimeContext runtimeContext, final SQLStatementContext sqlStatementContext) { + CreateIndexStatement createIndexStatement = (CreateIndexStatement) sqlStatementContext.getSqlStatement(); if (null == createIndexStatement.getIndex()) { return; } - runtimeContext.getMetaData().getTables().get(optimizedStatement.getTables().getSingleTableName()).getIndexes().add(createIndexStatement.getIndex().getName()); + runtimeContext.getMetaData().getTables().get(sqlStatementContext.getTablesContext().getSingleTableName()).getIndexes().add(createIndexStatement.getIndex().getName()); } - private void refreshTableMetaDataForDropIndex(final ShardingRuntimeContext runtimeContext, final OptimizedStatement optimizedStatement) { - DropIndexStatement dropIndexStatement = (DropIndexStatement) optimizedStatement.getSqlStatement(); + private void refreshTableMetaDataForDropIndex(final ShardingRuntimeContext runtimeContext, final SQLStatementContext sqlStatementContext) { + DropIndexStatement dropIndexStatement = (DropIndexStatement) sqlStatementContext.getSqlStatement(); Collection indexNames = getIndexNames(dropIndexStatement); - TableMetaData tableMetaData = runtimeContext.getMetaData().getTables().get(optimizedStatement.getTables().getSingleTableName()); - if (!optimizedStatement.getTables().isEmpty()) { + TableMetaData tableMetaData = runtimeContext.getMetaData().getTables().get(sqlStatementContext.getTablesContext().getSingleTableName()); + if (!sqlStatementContext.getTablesContext().isEmpty()) { tableMetaData.getIndexes().removeAll(indexNames); } for (String each : indexNames) { diff --git a/sharding-jdbc/sharding-jdbc-core/src/main/java/org/apache/shardingsphere/shardingjdbc/executor/BatchPreparedStatementExecutor.java b/sharding-jdbc/sharding-jdbc-core/src/main/java/org/apache/shardingsphere/shardingjdbc/executor/BatchPreparedStatementExecutor.java index c796966f4c..d5f20be9ac 100644 --- a/sharding-jdbc/sharding-jdbc-core/src/main/java/org/apache/shardingsphere/shardingjdbc/executor/BatchPreparedStatementExecutor.java +++ b/sharding-jdbc/sharding-jdbc-core/src/main/java/org/apache/shardingsphere/shardingjdbc/executor/BatchPreparedStatementExecutor.java @@ -75,7 +75,7 @@ public final class BatchPreparedStatementExecutor extends AbstractStatementExecu * @throws SQLException SQL exception */ public void init(final SQLRouteResult routeResult) throws SQLException { - setOptimizedStatement(routeResult.getShardingStatement()); + setSqlStatementContext(routeResult.getShardingStatementContext()); getExecuteGroups().addAll(obtainExecuteGroups(routeUnits)); } diff --git a/sharding-jdbc/sharding-jdbc-core/src/main/java/org/apache/shardingsphere/shardingjdbc/executor/PreparedStatementExecutor.java b/sharding-jdbc/sharding-jdbc-core/src/main/java/org/apache/shardingsphere/shardingjdbc/executor/PreparedStatementExecutor.java index e1447f5283..4a72069861 100644 --- a/sharding-jdbc/sharding-jdbc-core/src/main/java/org/apache/shardingsphere/shardingjdbc/executor/PreparedStatementExecutor.java +++ b/sharding-jdbc/sharding-jdbc-core/src/main/java/org/apache/shardingsphere/shardingjdbc/executor/PreparedStatementExecutor.java @@ -67,7 +67,7 @@ public final class PreparedStatementExecutor extends AbstractStatementExecutor { * @throws SQLException SQL exception */ public void init(final SQLRouteResult routeResult) throws SQLException { - setOptimizedStatement(routeResult.getShardingStatement()); + setSqlStatementContext(routeResult.getShardingStatementContext()); getExecuteGroups().addAll(obtainExecuteGroups(routeResult.getRouteUnits())); cacheStatements(); } diff --git a/sharding-jdbc/sharding-jdbc-core/src/main/java/org/apache/shardingsphere/shardingjdbc/executor/StatementExecutor.java b/sharding-jdbc/sharding-jdbc-core/src/main/java/org/apache/shardingsphere/shardingjdbc/executor/StatementExecutor.java index 90d69f174c..68c7502b23 100644 --- a/sharding-jdbc/sharding-jdbc-core/src/main/java/org/apache/shardingsphere/shardingjdbc/executor/StatementExecutor.java +++ b/sharding-jdbc/sharding-jdbc-core/src/main/java/org/apache/shardingsphere/shardingjdbc/executor/StatementExecutor.java @@ -61,7 +61,7 @@ public final class StatementExecutor extends AbstractStatementExecutor { * @throws SQLException SQL exception */ public void init(final SQLRouteResult routeResult) throws SQLException { - setOptimizedStatement(routeResult.getShardingStatement()); + setSqlStatementContext(routeResult.getShardingStatementContext()); getExecuteGroups().addAll(obtainExecuteGroups(routeResult.getRouteUnits())); cacheStatements(); } diff --git a/sharding-jdbc/sharding-jdbc-core/src/main/java/org/apache/shardingsphere/shardingjdbc/jdbc/adapter/AbstractResultSetAdapter.java b/sharding-jdbc/sharding-jdbc-core/src/main/java/org/apache/shardingsphere/shardingjdbc/jdbc/adapter/AbstractResultSetAdapter.java index 11385c8b6e..40e3e7ccbe 100644 --- a/sharding-jdbc/sharding-jdbc-core/src/main/java/org/apache/shardingsphere/shardingjdbc/jdbc/adapter/AbstractResultSetAdapter.java +++ b/sharding-jdbc/sharding-jdbc-core/src/main/java/org/apache/shardingsphere/shardingjdbc/jdbc/adapter/AbstractResultSetAdapter.java @@ -72,7 +72,7 @@ public abstract class AbstractResultSetAdapter extends AbstractUnsupportedOperat @Override public final ResultSetMetaData getMetaData() throws SQLException { - return new ShardingResultSetMetaData(resultSets.get(0).getMetaData(), getShardingRule(), sqlRouteResult.getShardingStatement(), logicAndActualColumns); + return new ShardingResultSetMetaData(resultSets.get(0).getMetaData(), getShardingRule(), sqlRouteResult.getShardingStatementContext(), logicAndActualColumns); } private Map createLogicAndActualColumns() { @@ -81,7 +81,7 @@ public abstract class AbstractResultSetAdapter extends AbstractUnsupportedOperat private Map createLogicAndCipherColumns() { Map result = new LinkedHashMap<>(); - for (String each : sqlRouteResult.getShardingStatement().getTables().getTableNames()) { + for (String each : sqlRouteResult.getShardingStatementContext().getTablesContext().getTableNames()) { result.putAll(getShardingRule().getEncryptRule().getLogicAndCipherColumns(each)); } return result; @@ -89,7 +89,7 @@ public abstract class AbstractResultSetAdapter extends AbstractUnsupportedOperat private Map createLogicAndPlainColumns() { Map result = new LinkedHashMap<>(); - for (String each : sqlRouteResult.getShardingStatement().getTables().getTableNames()) { + for (String each : sqlRouteResult.getShardingStatementContext().getTablesContext().getTableNames()) { result.putAll(getShardingRule().getEncryptRule().getLogicAndPlainColumns(each)); } return result; diff --git a/sharding-jdbc/sharding-jdbc-core/src/main/java/org/apache/shardingsphere/shardingjdbc/jdbc/core/resultset/EncryptResultSet.java b/sharding-jdbc/sharding-jdbc-core/src/main/java/org/apache/shardingsphere/shardingjdbc/jdbc/core/resultset/EncryptResultSet.java index a9e74eb553..ee5a1f6066 100644 --- a/sharding-jdbc/sharding-jdbc-core/src/main/java/org/apache/shardingsphere/shardingjdbc/jdbc/core/resultset/EncryptResultSet.java +++ b/sharding-jdbc/sharding-jdbc-core/src/main/java/org/apache/shardingsphere/shardingjdbc/jdbc/core/resultset/EncryptResultSet.java @@ -21,7 +21,7 @@ import org.apache.shardingsphere.core.constant.properties.ShardingPropertiesCons import org.apache.shardingsphere.core.execute.sql.execute.result.QueryResult; import org.apache.shardingsphere.core.execute.sql.execute.result.StreamQueryResult; import org.apache.shardingsphere.core.merge.dql.iterator.IteratorStreamMergedResult; -import org.apache.shardingsphere.core.optimize.statement.OptimizedStatement; +import org.apache.shardingsphere.core.optimize.statement.SQLStatementContext; import org.apache.shardingsphere.core.rule.EncryptRule; import org.apache.shardingsphere.shardingjdbc.jdbc.core.context.EncryptRuntimeContext; import org.apache.shardingsphere.shardingjdbc.jdbc.unsupported.AbstractUnsupportedOperationResultSet; @@ -55,7 +55,7 @@ public final class EncryptResultSet extends AbstractUnsupportedOperationResultSe private final EncryptRule encryptRule; - private final OptimizedStatement optimizedStatement; + private final SQLStatementContext sqlStatementContext; private final Statement encryptStatement; @@ -65,10 +65,10 @@ public final class EncryptResultSet extends AbstractUnsupportedOperationResultSe private final Map logicAndActualColumns; - public EncryptResultSet(final EncryptRuntimeContext encryptRuntimeContext, - final OptimizedStatement optimizedStatement, final Statement encryptStatement, final ResultSet resultSet) throws SQLException { + public EncryptResultSet(final EncryptRuntimeContext encryptRuntimeContext, + final SQLStatementContext sqlStatementContext, final Statement encryptStatement, final ResultSet resultSet) throws SQLException { this.encryptRule = encryptRuntimeContext.getRule(); - this.optimizedStatement = optimizedStatement; + this.sqlStatementContext = sqlStatementContext; this.encryptStatement = encryptStatement; originalResultSet = resultSet; QueryResult queryResult = new StreamQueryResult(resultSet, encryptRule, encryptRuntimeContext.getProps()); @@ -82,7 +82,7 @@ public final class EncryptResultSet extends AbstractUnsupportedOperationResultSe private Map createLogicAndCipherColumns() { Map result = new LinkedHashMap<>(); - for (String each : optimizedStatement.getTables().getTableNames()) { + for (String each : sqlStatementContext.getTablesContext().getTableNames()) { result.putAll(encryptRule.getLogicAndCipherColumns(each)); } return result; @@ -90,7 +90,7 @@ public final class EncryptResultSet extends AbstractUnsupportedOperationResultSe private Map createLogicAndPlainColumns() { Map result = new LinkedHashMap<>(); - for (String each : optimizedStatement.getTables().getTableNames()) { + for (String each : sqlStatementContext.getTablesContext().getTableNames()) { result.putAll(encryptRule.getLogicAndPlainColumns(each)); } return result; @@ -372,7 +372,7 @@ public final class EncryptResultSet extends AbstractUnsupportedOperationResultSe @Override public ResultSetMetaData getMetaData() throws SQLException { - return new EncryptResultSetMetaData(originalResultSet.getMetaData(), encryptRule, optimizedStatement, logicAndActualColumns); + return new EncryptResultSetMetaData(originalResultSet.getMetaData(), encryptRule, sqlStatementContext, logicAndActualColumns); } @Override diff --git a/sharding-jdbc/sharding-jdbc-core/src/main/java/org/apache/shardingsphere/shardingjdbc/jdbc/core/resultset/EncryptResultSetMetaData.java b/sharding-jdbc/sharding-jdbc-core/src/main/java/org/apache/shardingsphere/shardingjdbc/jdbc/core/resultset/EncryptResultSetMetaData.java index b4520b14ef..e15149aa3a 100644 --- a/sharding-jdbc/sharding-jdbc-core/src/main/java/org/apache/shardingsphere/shardingjdbc/jdbc/core/resultset/EncryptResultSetMetaData.java +++ b/sharding-jdbc/sharding-jdbc-core/src/main/java/org/apache/shardingsphere/shardingjdbc/jdbc/core/resultset/EncryptResultSetMetaData.java @@ -18,7 +18,7 @@ package org.apache.shardingsphere.shardingjdbc.jdbc.core.resultset; import org.apache.shardingsphere.core.constant.ShardingConstant; -import org.apache.shardingsphere.core.optimize.statement.OptimizedStatement; +import org.apache.shardingsphere.core.optimize.statement.SQLStatementContext; import org.apache.shardingsphere.core.rule.EncryptRule; import org.apache.shardingsphere.shardingjdbc.jdbc.adapter.WrapperAdapter; @@ -40,15 +40,15 @@ public final class EncryptResultSetMetaData extends WrapperAdapter implements Re private final EncryptRule encryptRule; - private final OptimizedStatement optimizedStatement; + private final SQLStatementContext sqlStatementContext; private final Map logicAndActualColumns; - public EncryptResultSetMetaData(final ResultSetMetaData resultSetMetaData, - final EncryptRule encryptRule, final OptimizedStatement optimizedStatement, final Map logicAndActualColumns) { + public EncryptResultSetMetaData(final ResultSetMetaData resultSetMetaData, + final EncryptRule encryptRule, final SQLStatementContext sqlStatementContext, final Map logicAndActualColumns) { this.resultSetMetaData = resultSetMetaData; this.encryptRule = encryptRule; - this.optimizedStatement = optimizedStatement; + this.sqlStatementContext = sqlStatementContext; this.logicAndActualColumns = logicAndActualColumns; } @@ -71,7 +71,7 @@ public final class EncryptResultSetMetaData extends WrapperAdapter implements Re private Collection getAssistedQueryColumns() { Collection result = new LinkedList<>(); - for (String each : optimizedStatement.getTables().getTableNames()) { + for (String each : sqlStatementContext.getTablesContext().getTableNames()) { result.addAll(encryptRule.getAssistedQueryColumns(each)); } return result; diff --git a/sharding-jdbc/sharding-jdbc-core/src/main/java/org/apache/shardingsphere/shardingjdbc/jdbc/core/resultset/ShardingResultSetMetaData.java b/sharding-jdbc/sharding-jdbc-core/src/main/java/org/apache/shardingsphere/shardingjdbc/jdbc/core/resultset/ShardingResultSetMetaData.java index e411af9fc0..007801620e 100644 --- a/sharding-jdbc/sharding-jdbc-core/src/main/java/org/apache/shardingsphere/shardingjdbc/jdbc/core/resultset/ShardingResultSetMetaData.java +++ b/sharding-jdbc/sharding-jdbc-core/src/main/java/org/apache/shardingsphere/shardingjdbc/jdbc/core/resultset/ShardingResultSetMetaData.java @@ -18,7 +18,7 @@ package org.apache.shardingsphere.shardingjdbc.jdbc.core.resultset; import org.apache.shardingsphere.core.constant.ShardingConstant; -import org.apache.shardingsphere.core.optimize.statement.OptimizedStatement; +import org.apache.shardingsphere.core.optimize.statement.SQLStatementContext; import org.apache.shardingsphere.core.optimize.segment.select.item.DerivedColumn; import org.apache.shardingsphere.core.rule.ShardingRule; import org.apache.shardingsphere.shardingjdbc.jdbc.adapter.WrapperAdapter; @@ -42,15 +42,15 @@ public final class ShardingResultSetMetaData extends WrapperAdapter implements R private final ShardingRule shardingRule; - private final OptimizedStatement optimizedStatement; + private final SQLStatementContext sqlStatementContext; private final Map logicAndActualColumns; - public ShardingResultSetMetaData(final ResultSetMetaData resultSetMetaData, - final ShardingRule shardingRule, final OptimizedStatement optimizedStatement, final Map logicAndActualColumns) { + public ShardingResultSetMetaData(final ResultSetMetaData resultSetMetaData, + final ShardingRule shardingRule, final SQLStatementContext sqlStatementContext, final Map logicAndActualColumns) { this.resultSetMetaData = resultSetMetaData; this.shardingRule = shardingRule; - this.optimizedStatement = optimizedStatement; + this.sqlStatementContext = sqlStatementContext; this.logicAndActualColumns = logicAndActualColumns; } @@ -73,7 +73,7 @@ public final class ShardingResultSetMetaData extends WrapperAdapter implements R private Collection getAssistedQueryColumns() { Collection result = new LinkedList<>(); - for (String each : optimizedStatement.getTables().getTableNames()) { + for (String each : sqlStatementContext.getTablesContext().getTableNames()) { result.addAll(shardingRule.getEncryptRule().getAssistedQueryColumns(each)); } return result; diff --git a/sharding-jdbc/sharding-jdbc-core/src/main/java/org/apache/shardingsphere/shardingjdbc/jdbc/core/statement/EncryptPreparedStatement.java b/sharding-jdbc/sharding-jdbc-core/src/main/java/org/apache/shardingsphere/shardingjdbc/jdbc/core/statement/EncryptPreparedStatement.java index 3398278586..a2b752fc68 100644 --- a/sharding-jdbc/sharding-jdbc-core/src/main/java/org/apache/shardingsphere/shardingjdbc/jdbc/core/statement/EncryptPreparedStatement.java +++ b/sharding-jdbc/sharding-jdbc-core/src/main/java/org/apache/shardingsphere/shardingjdbc/jdbc/core/statement/EncryptPreparedStatement.java @@ -19,8 +19,8 @@ package org.apache.shardingsphere.shardingjdbc.jdbc.core.statement; import lombok.RequiredArgsConstructor; import org.apache.shardingsphere.core.constant.properties.ShardingPropertiesConstant; -import org.apache.shardingsphere.core.optimize.statement.OptimizedStatement; -import org.apache.shardingsphere.core.optimize.OptimizedStatementFactory; +import org.apache.shardingsphere.core.optimize.statement.SQLStatementContext; +import org.apache.shardingsphere.core.optimize.SQLStatementContextFactory; import org.apache.shardingsphere.core.parse.sql.statement.SQLStatement; import org.apache.shardingsphere.core.rewrite.SQLRewriteEngine; import org.apache.shardingsphere.core.route.SQLLogger; @@ -48,7 +48,7 @@ public final class EncryptPreparedStatement extends AbstractShardingPreparedStat private PreparedStatement preparedStatement; - private OptimizedStatement encryptStatement; + private SQLStatementContext sqlStatementContext; private EncryptResultSet resultSet; @@ -93,7 +93,7 @@ public final class EncryptPreparedStatement extends AbstractShardingPreparedStat preparedStatement = preparedStatementGenerator.createPreparedStatement(sqlUnit.getSql()); replayMethodsInvocation(preparedStatement); replaySetParameter(preparedStatement, sqlUnit.getParameters()); - this.resultSet = new EncryptResultSet(preparedStatementGenerator.connection.getRuntimeContext(), encryptStatement, this, preparedStatement.executeQuery()); + this.resultSet = new EncryptResultSet(preparedStatementGenerator.connection.getRuntimeContext(), sqlStatementContext, this, preparedStatement.executeQuery()); return resultSet; } finally { clearParameters(); @@ -135,7 +135,7 @@ public final class EncryptPreparedStatement extends AbstractShardingPreparedStat private EncryptResultSet createEncryptResultSet(final PreparedStatement preparedStatement) throws SQLException { return null == preparedStatement.getResultSet() - ? null : new EncryptResultSet(preparedStatementGenerator.connection.getRuntimeContext(), encryptStatement, this, preparedStatement.getResultSet()); + ? null : new EncryptResultSet(preparedStatementGenerator.connection.getRuntimeContext(), sqlStatementContext, this, preparedStatement.getResultSet()); } @Override @@ -148,9 +148,8 @@ public final class EncryptPreparedStatement extends AbstractShardingPreparedStat private SQLUnit getSQLUnit(final String sql) { EncryptConnection connection = preparedStatementGenerator.connection; SQLStatement sqlStatement = connection.getRuntimeContext().getParseEngine().parse(sql, true); - encryptStatement = OptimizedStatementFactory.newInstance(connection.getRuntimeContext().getTableMetas(), sql, getParameters(), sqlStatement); - SQLRewriteEngine encryptSQLRewriteEngine = new SQLRewriteEngine(connection.getRuntimeContext().getRule(), connection.getRuntimeContext().getTableMetas(), - encryptStatement, sql, getParameters(), connection.getRuntimeContext().getProps().getValue(ShardingPropertiesConstant.QUERY_WITH_CIPHER_COLUMN)); + sqlStatementContext = SQLStatementContextFactory.newInstance(connection.getRuntimeContext().getTableMetas(), sql, getParameters(), sqlStatement); + SQLRewriteEngine encryptSQLRewriteEngine = new SQLRewriteEngine(connection.getRuntimeContext().getRule(), connection.getRuntimeContext().getTableMetas(), sqlStatementContext, sql, getParameters(), connection.getRuntimeContext().getProps().getValue(ShardingPropertiesConstant.QUERY_WITH_CIPHER_COLUMN)); SQLUnit result = encryptSQLRewriteEngine.generateSQL(); showSQL(result.getSql()); return result; diff --git a/sharding-jdbc/sharding-jdbc-core/src/main/java/org/apache/shardingsphere/shardingjdbc/jdbc/core/statement/EncryptStatement.java b/sharding-jdbc/sharding-jdbc-core/src/main/java/org/apache/shardingsphere/shardingjdbc/jdbc/core/statement/EncryptStatement.java index ca7719ab7f..d7a6583d7a 100644 --- a/sharding-jdbc/sharding-jdbc-core/src/main/java/org/apache/shardingsphere/shardingjdbc/jdbc/core/statement/EncryptStatement.java +++ b/sharding-jdbc/sharding-jdbc-core/src/main/java/org/apache/shardingsphere/shardingjdbc/jdbc/core/statement/EncryptStatement.java @@ -18,8 +18,8 @@ package org.apache.shardingsphere.shardingjdbc.jdbc.core.statement; import org.apache.shardingsphere.core.constant.properties.ShardingPropertiesConstant; -import org.apache.shardingsphere.core.optimize.statement.OptimizedStatement; -import org.apache.shardingsphere.core.optimize.OptimizedStatementFactory; +import org.apache.shardingsphere.core.optimize.statement.SQLStatementContext; +import org.apache.shardingsphere.core.optimize.SQLStatementContextFactory; import org.apache.shardingsphere.core.parse.sql.statement.SQLStatement; import org.apache.shardingsphere.core.rewrite.SQLRewriteEngine; import org.apache.shardingsphere.core.route.SQLLogger; @@ -45,7 +45,7 @@ public final class EncryptStatement extends AbstractUnsupportedOperationStatemen private final Statement statement; - private OptimizedStatement encryptStatement; + private SQLStatementContext sqlStatementContext; private EncryptResultSet resultSet; @@ -67,7 +67,7 @@ public final class EncryptStatement extends AbstractUnsupportedOperationStatemen @Override public ResultSet executeQuery(final String sql) throws SQLException { ResultSet resultSet = statement.executeQuery(getRewriteSQL(sql)); - this.resultSet = new EncryptResultSet(connection.getRuntimeContext(), encryptStatement, this, resultSet); + this.resultSet = new EncryptResultSet(connection.getRuntimeContext(), sqlStatementContext, this, resultSet); return this.resultSet; } @@ -79,9 +79,8 @@ public final class EncryptStatement extends AbstractUnsupportedOperationStatemen @SuppressWarnings("unchecked") private String getRewriteSQL(final String sql) { SQLStatement sqlStatement = connection.getRuntimeContext().getParseEngine().parse(sql, false); - encryptStatement = OptimizedStatementFactory.newInstance(connection.getRuntimeContext().getTableMetas(), sql, Collections.emptyList(), sqlStatement); - SQLRewriteEngine encryptSQLRewriteEngine = new SQLRewriteEngine(connection.getRuntimeContext().getRule(), connection.getRuntimeContext().getTableMetas(), - encryptStatement, sql, Collections.emptyList(), connection.getRuntimeContext().getProps().getValue(ShardingPropertiesConstant.QUERY_WITH_CIPHER_COLUMN)); + sqlStatementContext = SQLStatementContextFactory.newInstance(connection.getRuntimeContext().getTableMetas(), sql, Collections.emptyList(), sqlStatement); + SQLRewriteEngine encryptSQLRewriteEngine = new SQLRewriteEngine(connection.getRuntimeContext().getRule(), connection.getRuntimeContext().getTableMetas(), sqlStatementContext, sql, Collections.emptyList(), connection.getRuntimeContext().getProps().getValue(ShardingPropertiesConstant.QUERY_WITH_CIPHER_COLUMN)); String result = encryptSQLRewriteEngine.generateSQL().getSql(); showSQL(result); return result; @@ -143,7 +142,7 @@ public final class EncryptStatement extends AbstractUnsupportedOperationStatemen } private EncryptResultSet createEncryptResultSet(final Statement statement) throws SQLException { - return null == statement.getResultSet() ? null : new EncryptResultSet(connection.getRuntimeContext(), encryptStatement, this, statement.getResultSet()); + return null == statement.getResultSet() ? null : new EncryptResultSet(connection.getRuntimeContext(), sqlStatementContext, this, statement.getResultSet()); } @Override diff --git a/sharding-jdbc/sharding-jdbc-core/src/main/java/org/apache/shardingsphere/shardingjdbc/jdbc/core/statement/ShardingPreparedStatement.java b/sharding-jdbc/sharding-jdbc-core/src/main/java/org/apache/shardingsphere/shardingjdbc/jdbc/core/statement/ShardingPreparedStatement.java index 5b2bd84df6..a83ad25556 100644 --- a/sharding-jdbc/sharding-jdbc-core/src/main/java/org/apache/shardingsphere/shardingjdbc/jdbc/core/statement/ShardingPreparedStatement.java +++ b/sharding-jdbc/sharding-jdbc-core/src/main/java/org/apache/shardingsphere/shardingjdbc/jdbc/core/statement/ShardingPreparedStatement.java @@ -28,7 +28,7 @@ import org.apache.shardingsphere.core.execute.sql.execute.result.StreamQueryResu import org.apache.shardingsphere.core.merge.MergeEngine; import org.apache.shardingsphere.core.merge.MergeEngineFactory; import org.apache.shardingsphere.core.route.router.sharding.keygen.GeneratedKey; -import org.apache.shardingsphere.core.optimize.statement.impl.SelectOptimizedStatement; +import org.apache.shardingsphere.core.optimize.statement.impl.SelectSQLStatementContext; import org.apache.shardingsphere.core.parse.sql.statement.dal.DALStatement; import org.apache.shardingsphere.core.parse.sql.statement.dml.InsertStatement; import org.apache.shardingsphere.core.route.SQLRouteResult; @@ -128,7 +128,7 @@ public final class ShardingPreparedStatement extends AbstractShardingPreparedSta resultSets.add(resultSet); queryResults.add(new StreamQueryResult(resultSet, connection.getRuntimeContext().getRule(), connection.getRuntimeContext().getProps())); } - if (sqlRouteResult.getShardingStatement() instanceof SelectOptimizedStatement || sqlRouteResult.getShardingStatement().getSqlStatement() instanceof DALStatement) { + if (sqlRouteResult.getShardingStatementContext() instanceof SelectSQLStatementContext || sqlRouteResult.getShardingStatementContext().getSqlStatement() instanceof DALStatement) { MergeEngine mergeEngine = MergeEngineFactory.newInstance(connection.getRuntimeContext().getDatabaseType(), connection.getRuntimeContext().getRule(), sqlRouteResult, connection.getRuntimeContext().getMetaData().getTables(), queryResults); currentResultSet = getCurrentResultSet(resultSets, mergeEngine); @@ -182,7 +182,7 @@ public final class ShardingPreparedStatement extends AbstractShardingPreparedSta } private Optional getGeneratedKey() { - return null != sqlRouteResult && sqlRouteResult.getShardingStatement().getSqlStatement() instanceof InsertStatement + return null != sqlRouteResult && sqlRouteResult.getShardingStatementContext().getSqlStatement() instanceof InsertStatement ? sqlRouteResult.getGeneratedKey() : Optional.absent(); } @@ -274,7 +274,7 @@ public final class ShardingPreparedStatement extends AbstractShardingPreparedSta @Override public boolean isAccumulate() { - return !connection.getRuntimeContext().getRule().isAllBroadcastTables(sqlRouteResult.getShardingStatement().getTables().getTableNames()); + return !connection.getRuntimeContext().getRule().isAllBroadcastTables(sqlRouteResult.getShardingStatementContext().getTablesContext().getTableNames()); } @Override diff --git a/sharding-jdbc/sharding-jdbc-core/src/main/java/org/apache/shardingsphere/shardingjdbc/jdbc/core/statement/ShardingStatement.java b/sharding-jdbc/sharding-jdbc-core/src/main/java/org/apache/shardingsphere/shardingjdbc/jdbc/core/statement/ShardingStatement.java index 7d25b46885..79437ffecf 100644 --- a/sharding-jdbc/sharding-jdbc-core/src/main/java/org/apache/shardingsphere/shardingjdbc/jdbc/core/statement/ShardingStatement.java +++ b/sharding-jdbc/sharding-jdbc-core/src/main/java/org/apache/shardingsphere/shardingjdbc/jdbc/core/statement/ShardingStatement.java @@ -26,7 +26,7 @@ import org.apache.shardingsphere.core.execute.sql.execute.result.StreamQueryResu import org.apache.shardingsphere.core.merge.MergeEngine; import org.apache.shardingsphere.core.merge.MergeEngineFactory; import org.apache.shardingsphere.core.route.router.sharding.keygen.GeneratedKey; -import org.apache.shardingsphere.core.optimize.statement.impl.SelectOptimizedStatement; +import org.apache.shardingsphere.core.optimize.statement.impl.SelectSQLStatementContext; import org.apache.shardingsphere.core.parse.sql.statement.dal.DALStatement; import org.apache.shardingsphere.core.parse.sql.statement.dml.InsertStatement; import org.apache.shardingsphere.core.route.SQLRouteResult; @@ -110,7 +110,7 @@ public final class ShardingStatement extends AbstractStatementAdapter { resultSets.add(resultSet); queryResults.add(new StreamQueryResult(resultSet, connection.getRuntimeContext().getRule(), connection.getRuntimeContext().getProps())); } - if (sqlRouteResult.getShardingStatement() instanceof SelectOptimizedStatement || sqlRouteResult.getShardingStatement().getSqlStatement() instanceof DALStatement) { + if (sqlRouteResult.getShardingStatementContext() instanceof SelectSQLStatementContext || sqlRouteResult.getShardingStatementContext().getSqlStatement() instanceof DALStatement) { MergeEngine mergeEngine = MergeEngineFactory.newInstance(connection.getRuntimeContext().getDatabaseType(), connection.getRuntimeContext().getRule(), sqlRouteResult, connection.getRuntimeContext().getMetaData().getTables(), queryResults); currentResultSet = getCurrentResultSet(resultSets, mergeEngine); @@ -272,7 +272,7 @@ public final class ShardingStatement extends AbstractStatementAdapter { @Override public boolean isAccumulate() { - return !connection.getRuntimeContext().getRule().isAllBroadcastTables(sqlRouteResult.getShardingStatement().getTables().getTableNames()); + return !connection.getRuntimeContext().getRule().isAllBroadcastTables(sqlRouteResult.getShardingStatementContext().getTablesContext().getTableNames()); } @Override @@ -294,6 +294,6 @@ public final class ShardingStatement extends AbstractStatementAdapter { } private Optional getGeneratedKey() { - return null != sqlRouteResult && sqlRouteResult.getShardingStatement().getSqlStatement() instanceof InsertStatement ? sqlRouteResult.getGeneratedKey() : Optional.absent(); + return null != sqlRouteResult && sqlRouteResult.getShardingStatementContext().getSqlStatement() instanceof InsertStatement ? sqlRouteResult.getGeneratedKey() : Optional.absent(); } } diff --git a/sharding-jdbc/sharding-jdbc-core/src/test/java/org/apache/shardingsphere/shardingjdbc/jdbc/core/resultset/ShardingResultSetTest.java b/sharding-jdbc/sharding-jdbc-core/src/test/java/org/apache/shardingsphere/shardingjdbc/jdbc/core/resultset/ShardingResultSetTest.java index 91b5278a19..b37fa00d65 100644 --- a/sharding-jdbc/sharding-jdbc-core/src/test/java/org/apache/shardingsphere/shardingjdbc/jdbc/core/resultset/ShardingResultSetTest.java +++ b/sharding-jdbc/sharding-jdbc-core/src/test/java/org/apache/shardingsphere/shardingjdbc/jdbc/core/resultset/ShardingResultSetTest.java @@ -20,8 +20,8 @@ package org.apache.shardingsphere.shardingjdbc.jdbc.core.resultset; import com.google.common.base.Optional; import org.apache.shardingsphere.core.constant.properties.ShardingProperties; import org.apache.shardingsphere.core.merge.MergedResult; -import org.apache.shardingsphere.core.optimize.segment.Tables; -import org.apache.shardingsphere.core.optimize.statement.OptimizedStatement; +import org.apache.shardingsphere.core.optimize.segment.table.TablesContext; +import org.apache.shardingsphere.core.optimize.statement.SQLStatementContext; import org.apache.shardingsphere.core.route.SQLRouteResult; import org.apache.shardingsphere.core.rule.EncryptRule; import org.apache.shardingsphere.core.rule.ShardingRule; @@ -75,11 +75,11 @@ public final class ShardingResultSetTest { private SQLRouteResult createSQLRouteResult() { SQLRouteResult result = mock(SQLRouteResult.class); - OptimizedStatement shardingStatement = mock(OptimizedStatement.class); - Tables tables = mock(Tables.class); - when(tables.getTableNames()).thenReturn(Collections.emptyList()); - when(shardingStatement.getTables()).thenReturn(tables); - when(result.getShardingStatement()).thenReturn(shardingStatement); + SQLStatementContext sqlStatementContext = mock(SQLStatementContext.class); + TablesContext tablesContext = mock(TablesContext.class); + when(tablesContext.getTableNames()).thenReturn(Collections.emptyList()); + when(sqlStatementContext.getTablesContext()).thenReturn(tablesContext); + when(result.getShardingStatementContext()).thenReturn(sqlStatementContext); return result; } diff --git a/sharding-proxy/sharding-proxy-backend/src/main/java/org/apache/shardingsphere/shardingproxy/backend/communication/jdbc/JDBCDatabaseCommunicationEngine.java b/sharding-proxy/sharding-proxy-backend/src/main/java/org/apache/shardingsphere/shardingproxy/backend/communication/jdbc/JDBCDatabaseCommunicationEngine.java index 25695337c5..cac36ce9c8 100644 --- a/sharding-proxy/sharding-proxy-backend/src/main/java/org/apache/shardingsphere/shardingproxy/backend/communication/jdbc/JDBCDatabaseCommunicationEngine.java +++ b/sharding-proxy/sharding-proxy-backend/src/main/java/org/apache/shardingsphere/shardingproxy/backend/communication/jdbc/JDBCDatabaseCommunicationEngine.java @@ -21,7 +21,7 @@ import lombok.RequiredArgsConstructor; import org.apache.shardingsphere.core.merge.MergeEngineFactory; import org.apache.shardingsphere.core.merge.MergedResult; import org.apache.shardingsphere.core.merge.dal.show.ShowTablesMergedResult; -import org.apache.shardingsphere.core.optimize.statement.OptimizedStatement; +import org.apache.shardingsphere.core.optimize.statement.SQLStatementContext; import org.apache.shardingsphere.core.optimize.segment.select.item.DerivedColumn; import org.apache.shardingsphere.core.parse.sql.statement.SQLStatement; import org.apache.shardingsphere.core.parse.sql.statement.ddl.DDLStatement; @@ -85,13 +85,14 @@ public final class JDBCDatabaseCommunicationEngine implements DatabaseCommunicat if (routeResult.getRouteUnits().isEmpty()) { return new UpdateResponse(); } - OptimizedStatement shardingStatement = routeResult.getShardingStatement(); - if (isExecuteDDLInXATransaction(shardingStatement.getSqlStatement())) { - return new ErrorResponse(new TableModifyInTransactionException(shardingStatement.getTables().isSingleTable() ? shardingStatement.getTables().getSingleTableName() : "unknown_table")); + SQLStatementContext shardingStatementContext = routeResult.getShardingStatementContext(); + if (isExecuteDDLInXATransaction(shardingStatementContext.getSqlStatement())) { + return new ErrorResponse(new TableModifyInTransactionException( + shardingStatementContext.getTablesContext().isSingleTable() ? shardingStatementContext.getTablesContext().getSingleTableName() : "unknown_table")); } response = executeEngine.execute(routeResult); if (logicSchema instanceof ShardingSchema) { - logicSchema.refreshTableMetaData(routeResult.getShardingStatement()); + logicSchema.refreshTableMetaData(routeResult.getShardingStatementContext()); } return merge(routeResult); } @@ -113,13 +114,13 @@ public final class JDBCDatabaseCommunicationEngine implements DatabaseCommunicat } private void mergeUpdateCount(final SQLRouteResult routeResult) { - if (!isAllBroadcastTables(routeResult.getShardingStatement())) { + if (!isAllBroadcastTables(routeResult.getShardingStatementContext())) { ((UpdateResponse) response).mergeUpdateCount(); } } - private boolean isAllBroadcastTables(final OptimizedStatement optimizedStatement) { - return logicSchema instanceof ShardingSchema && logicSchema.getShardingRule().isAllBroadcastTables(optimizedStatement.getTables().getTableNames()); + private boolean isAllBroadcastTables(final SQLStatementContext sqlStatementContext) { + return logicSchema instanceof ShardingSchema && logicSchema.getShardingRule().isAllBroadcastTables(sqlStatementContext.getTablesContext().getTableNames()); } private void setMergedResult(final SQLRouteResult routeResult) throws SQLException { @@ -165,7 +166,7 @@ public final class JDBCDatabaseCommunicationEngine implements DatabaseCommunicat private Collection getAssistedQueryColumns(final SQLRouteResult routeResult) { Collection result = new LinkedList<>(); EncryptRule encryptRule = getEncryptRule(); - for (String each : routeResult.getShardingStatement().getTables().getTableNames()) { + for (String each : routeResult.getShardingStatementContext().getTablesContext().getTableNames()) { result.addAll(encryptRule.getAssistedQueryColumns(each)); } return result; diff --git a/sharding-proxy/sharding-proxy-backend/src/main/java/org/apache/shardingsphere/shardingproxy/backend/communication/jdbc/execute/JDBCExecuteEngine.java b/sharding-proxy/sharding-proxy-backend/src/main/java/org/apache/shardingsphere/shardingproxy/backend/communication/jdbc/execute/JDBCExecuteEngine.java index b320ec6f80..b27c1adccf 100644 --- a/sharding-proxy/sharding-proxy-backend/src/main/java/org/apache/shardingsphere/shardingproxy/backend/communication/jdbc/execute/JDBCExecuteEngine.java +++ b/sharding-proxy/sharding-proxy-backend/src/main/java/org/apache/shardingsphere/shardingproxy/backend/communication/jdbc/execute/JDBCExecuteEngine.java @@ -73,7 +73,7 @@ public final class JDBCExecuteEngine implements SQLExecuteEngine { @SuppressWarnings("unchecked") @Override public BackendResponse execute(final SQLRouteResult routeResult) throws SQLException { - boolean isReturnGeneratedKeys = routeResult.getShardingStatement().getSqlStatement() instanceof InsertStatement; + boolean isReturnGeneratedKeys = routeResult.getShardingStatementContext().getSqlStatement() instanceof InsertStatement; boolean isExceptionThrown = ExecutorExceptionHandler.isExceptionThrown(); Collection> sqlExecuteGroups = sqlExecutePrepareTemplate.getExecuteUnitGroups( routeResult.getRouteUnits(), new ProxyJDBCExecutePrepareCallback(backendConnection, jdbcExecutorWrapper, isReturnGeneratedKeys)); diff --git a/sharding-proxy/sharding-proxy-backend/src/main/java/org/apache/shardingsphere/shardingproxy/backend/communication/jdbc/wrapper/PreparedStatementExecutorWrapper.java b/sharding-proxy/sharding-proxy-backend/src/main/java/org/apache/shardingsphere/shardingproxy/backend/communication/jdbc/wrapper/PreparedStatementExecutorWrapper.java index 5222535e74..4ae28314cb 100644 --- a/sharding-proxy/sharding-proxy-backend/src/main/java/org/apache/shardingsphere/shardingproxy/backend/communication/jdbc/wrapper/PreparedStatementExecutorWrapper.java +++ b/sharding-proxy/sharding-proxy-backend/src/main/java/org/apache/shardingsphere/shardingproxy/backend/communication/jdbc/wrapper/PreparedStatementExecutorWrapper.java @@ -20,9 +20,9 @@ package org.apache.shardingsphere.shardingproxy.backend.communication.jdbc.wrapp import lombok.RequiredArgsConstructor; import org.apache.shardingsphere.core.PreparedQueryShardingEngine; import org.apache.shardingsphere.core.constant.properties.ShardingPropertiesConstant; -import org.apache.shardingsphere.core.optimize.statement.impl.CommonOptimizedStatement; -import org.apache.shardingsphere.core.optimize.statement.OptimizedStatement; -import org.apache.shardingsphere.core.optimize.OptimizedStatementFactory; +import org.apache.shardingsphere.core.optimize.statement.impl.CommonSQLStatementContext; +import org.apache.shardingsphere.core.optimize.statement.SQLStatementContext; +import org.apache.shardingsphere.core.optimize.SQLStatementContextFactory; import org.apache.shardingsphere.core.parse.sql.statement.SQLStatement; import org.apache.shardingsphere.core.rewrite.SQLRewriteEngine; import org.apache.shardingsphere.core.route.RouteUnit; @@ -78,10 +78,10 @@ public final class PreparedStatementExecutorWrapper implements JDBCExecutorWrapp private SQLRouteResult doMasterSlaveRoute(final String sql) { SQLStatement sqlStatement = logicSchema.getParseEngine().parse(sql, true); - CommonOptimizedStatement shardingStatement = new CommonOptimizedStatement(sqlStatement); - SQLRewriteEngine sqlRewriteEngine = new SQLRewriteEngine(((MasterSlaveSchema) logicSchema).getMasterSlaveRule(), shardingStatement, sql); + CommonSQLStatementContext commonSQLStatementContext = new CommonSQLStatementContext(sqlStatement); + SQLRewriteEngine sqlRewriteEngine = new SQLRewriteEngine(((MasterSlaveSchema) logicSchema).getMasterSlaveRule(), commonSQLStatementContext, sql); String rewriteSQL = sqlRewriteEngine.generateSQL().getSql(); - SQLRouteResult result = new SQLRouteResult(shardingStatement, new CommonOptimizedStatement(sqlStatement), new ShardingConditions(Collections.emptyList())); + SQLRouteResult result = new SQLRouteResult(commonSQLStatementContext, new CommonSQLStatementContext(sqlStatement), new ShardingConditions(Collections.emptyList())); for (String each : new MasterSlaveRouter(((MasterSlaveSchema) logicSchema).getMasterSlaveRule(), logicSchema.getParseEngine(), SHARDING_PROXY_CONTEXT.getShardingProperties().getValue(ShardingPropertiesConstant.SQL_SHOW)).route(rewriteSQL, true)) { result.getRouteUnits().add(new RouteUnit(each, new SQLUnit(rewriteSQL, parameters))); @@ -93,10 +93,10 @@ public final class PreparedStatementExecutorWrapper implements JDBCExecutorWrapp private SQLRouteResult doEncryptRoute(final String sql) { EncryptSchema encryptSchema = (EncryptSchema) logicSchema; SQLStatement sqlStatement = encryptSchema.getParseEngine().parse(sql, true); - OptimizedStatement encryptStatement = OptimizedStatementFactory.newInstance(logicSchema.getMetaData().getTables(), sql, parameters, sqlStatement); + SQLStatementContext sqlStatementContext = SQLStatementContextFactory.newInstance(logicSchema.getMetaData().getTables(), sql, parameters, sqlStatement); SQLRewriteEngine sqlRewriteEngine = new SQLRewriteEngine(encryptSchema.getEncryptRule(), logicSchema.getMetaData().getTables(), - encryptStatement, sql, parameters, ShardingProxyContext.getInstance().getShardingProperties().getValue(ShardingPropertiesConstant.QUERY_WITH_CIPHER_COLUMN)); - SQLRouteResult result = new SQLRouteResult(new CommonOptimizedStatement(sqlStatement), encryptStatement, new ShardingConditions(Collections.emptyList())); + sqlStatementContext, sql, parameters, ShardingProxyContext.getInstance().getShardingProperties().getValue(ShardingPropertiesConstant.QUERY_WITH_CIPHER_COLUMN)); + SQLRouteResult result = new SQLRouteResult(new CommonSQLStatementContext(sqlStatement), sqlStatementContext, new ShardingConditions(Collections.emptyList())); result.getRouteUnits().add(new RouteUnit(logicSchema.getDataSources().keySet().iterator().next(), new SQLUnit(sqlRewriteEngine.generateSQL().getSql(), parameters))); return result; } diff --git a/sharding-proxy/sharding-proxy-backend/src/main/java/org/apache/shardingsphere/shardingproxy/backend/communication/jdbc/wrapper/StatementExecutorWrapper.java b/sharding-proxy/sharding-proxy-backend/src/main/java/org/apache/shardingsphere/shardingproxy/backend/communication/jdbc/wrapper/StatementExecutorWrapper.java index efc37732aa..81ea90f71f 100644 --- a/sharding-proxy/sharding-proxy-backend/src/main/java/org/apache/shardingsphere/shardingproxy/backend/communication/jdbc/wrapper/StatementExecutorWrapper.java +++ b/sharding-proxy/sharding-proxy-backend/src/main/java/org/apache/shardingsphere/shardingproxy/backend/communication/jdbc/wrapper/StatementExecutorWrapper.java @@ -20,9 +20,9 @@ package org.apache.shardingsphere.shardingproxy.backend.communication.jdbc.wrapp import lombok.RequiredArgsConstructor; import org.apache.shardingsphere.core.SimpleQueryShardingEngine; import org.apache.shardingsphere.core.constant.properties.ShardingPropertiesConstant; -import org.apache.shardingsphere.core.optimize.statement.impl.CommonOptimizedStatement; -import org.apache.shardingsphere.core.optimize.statement.OptimizedStatement; -import org.apache.shardingsphere.core.optimize.OptimizedStatementFactory; +import org.apache.shardingsphere.core.optimize.statement.impl.CommonSQLStatementContext; +import org.apache.shardingsphere.core.optimize.statement.SQLStatementContext; +import org.apache.shardingsphere.core.optimize.SQLStatementContextFactory; import org.apache.shardingsphere.core.parse.sql.statement.SQLStatement; import org.apache.shardingsphere.core.rewrite.SQLRewriteEngine; import org.apache.shardingsphere.core.route.RouteUnit; @@ -78,10 +78,10 @@ public final class StatementExecutorWrapper implements JDBCExecutorWrapper { private SQLRouteResult doMasterSlaveRoute(final String sql) { SQLStatement sqlStatement = logicSchema.getParseEngine().parse(sql, false); - CommonOptimizedStatement shardingStatement = new CommonOptimizedStatement(sqlStatement); - SQLRewriteEngine sqlRewriteEngine = new SQLRewriteEngine(((MasterSlaveSchema) logicSchema).getMasterSlaveRule(), shardingStatement, sql); + CommonSQLStatementContext commonSQLStatementContext = new CommonSQLStatementContext(sqlStatement); + SQLRewriteEngine sqlRewriteEngine = new SQLRewriteEngine(((MasterSlaveSchema) logicSchema).getMasterSlaveRule(), commonSQLStatementContext, sql); String rewriteSQL = sqlRewriteEngine.generateSQL().getSql(); - SQLRouteResult result = new SQLRouteResult(shardingStatement, new CommonOptimizedStatement(sqlStatement), new ShardingConditions(Collections.emptyList())); + SQLRouteResult result = new SQLRouteResult(commonSQLStatementContext, new CommonSQLStatementContext(sqlStatement), new ShardingConditions(Collections.emptyList())); for (String each : new MasterSlaveRouter(((MasterSlaveSchema) logicSchema).getMasterSlaveRule(), logicSchema.getParseEngine(), SHARDING_PROXY_CONTEXT.getShardingProperties().getValue(ShardingPropertiesConstant.SQL_SHOW)).route(rewriteSQL, false)) { result.getRouteUnits().add(new RouteUnit(each, new SQLUnit(rewriteSQL, Collections.emptyList()))); @@ -93,10 +93,10 @@ public final class StatementExecutorWrapper implements JDBCExecutorWrapper { private SQLRouteResult doEncryptRoute(final String sql) { EncryptSchema encryptSchema = (EncryptSchema) logicSchema; SQLStatement sqlStatement = encryptSchema.getParseEngine().parse(sql, false); - OptimizedStatement encryptStatement = OptimizedStatementFactory.newInstance(logicSchema.getMetaData().getTables(), sql, new LinkedList<>(), sqlStatement); + SQLStatementContext sqlStatementContext = SQLStatementContextFactory.newInstance(logicSchema.getMetaData().getTables(), sql, new LinkedList<>(), sqlStatement); SQLRewriteEngine sqlRewriteEngine = new SQLRewriteEngine(encryptSchema.getEncryptRule(), logicSchema.getMetaData().getTables(), - encryptStatement, sql, Collections.emptyList(), ShardingProxyContext.getInstance().getShardingProperties().getValue(ShardingPropertiesConstant.QUERY_WITH_CIPHER_COLUMN)); - SQLRouteResult result = new SQLRouteResult(new CommonOptimizedStatement(sqlStatement), encryptStatement, new ShardingConditions(Collections.emptyList())); + sqlStatementContext, sql, Collections.emptyList(), ShardingProxyContext.getInstance().getShardingProperties().getValue(ShardingPropertiesConstant.QUERY_WITH_CIPHER_COLUMN)); + SQLRouteResult result = new SQLRouteResult(new CommonSQLStatementContext(sqlStatement), sqlStatementContext, new ShardingConditions(Collections.emptyList())); result.getRouteUnits().add(new RouteUnit(logicSchema.getDataSources().keySet().iterator().next(), new SQLUnit(sqlRewriteEngine.generateSQL().getSql(), Collections.emptyList()))); return result; } @@ -104,7 +104,7 @@ public final class StatementExecutorWrapper implements JDBCExecutorWrapper { private SQLRouteResult doTransparentRoute(final String sql) { SQLStatement sqlStatement = logicSchema.getParseEngine().parse(sql, false); SQLRouteResult result = new SQLRouteResult( - new CommonOptimizedStatement(sqlStatement), new CommonOptimizedStatement(sqlStatement), new ShardingConditions(Collections.emptyList())); + new CommonSQLStatementContext(sqlStatement), new CommonSQLStatementContext(sqlStatement), new ShardingConditions(Collections.emptyList())); result.getRouteUnits().add(new RouteUnit(logicSchema.getDataSources().keySet().iterator().next(), new SQLUnit(sql, Collections.emptyList()))); return result; } diff --git a/sharding-proxy/sharding-proxy-backend/src/main/java/org/apache/shardingsphere/shardingproxy/backend/schema/LogicSchema.java b/sharding-proxy/sharding-proxy-backend/src/main/java/org/apache/shardingsphere/shardingproxy/backend/schema/LogicSchema.java index d326221a23..85041dea79 100644 --- a/sharding-proxy/sharding-proxy-backend/src/main/java/org/apache/shardingsphere/shardingproxy/backend/schema/LogicSchema.java +++ b/sharding-proxy/sharding-proxy-backend/src/main/java/org/apache/shardingsphere/shardingproxy/backend/schema/LogicSchema.java @@ -24,7 +24,7 @@ import org.apache.shardingsphere.core.constant.properties.ShardingPropertiesCons import org.apache.shardingsphere.core.execute.metadata.TableMetaDataInitializer; import org.apache.shardingsphere.core.metadata.ShardingSphereMetaData; import org.apache.shardingsphere.core.metadata.datasource.DataSourceMetas; -import org.apache.shardingsphere.core.optimize.statement.OptimizedStatement; +import org.apache.shardingsphere.core.optimize.statement.SQLStatementContext; import org.apache.shardingsphere.core.parse.SQLParseEngine; import org.apache.shardingsphere.core.parse.SQLParseEngineFactory; import org.apache.shardingsphere.core.rule.ShardingRule; @@ -119,9 +119,9 @@ public abstract class LogicSchema { /** * Refresh table meta data. * - * @param optimizedStatement optimized statement + * @param sqlStatementContext SQL statement context * @throws SQLException SQL exception */ - public void refreshTableMetaData(final OptimizedStatement optimizedStatement) throws SQLException { + public void refreshTableMetaData(final SQLStatementContext sqlStatementContext) throws SQLException { } } diff --git a/sharding-proxy/sharding-proxy-backend/src/main/java/org/apache/shardingsphere/shardingproxy/backend/schema/impl/ShardingSchema.java b/sharding-proxy/sharding-proxy-backend/src/main/java/org/apache/shardingsphere/shardingproxy/backend/schema/impl/ShardingSchema.java index 11c0095498..75c649efd0 100644 --- a/sharding-proxy/sharding-proxy-backend/src/main/java/org/apache/shardingsphere/shardingproxy/backend/schema/impl/ShardingSchema.java +++ b/sharding-proxy/sharding-proxy-backend/src/main/java/org/apache/shardingsphere/shardingproxy/backend/schema/impl/ShardingSchema.java @@ -24,7 +24,7 @@ import org.apache.shardingsphere.api.config.sharding.ShardingRuleConfiguration; import org.apache.shardingsphere.core.metadata.ShardingSphereMetaData; import org.apache.shardingsphere.core.metadata.datasource.DataSourceMetas; import org.apache.shardingsphere.core.metadata.table.TableMetas; -import org.apache.shardingsphere.core.optimize.statement.OptimizedStatement; +import org.apache.shardingsphere.core.optimize.statement.SQLStatementContext; import org.apache.shardingsphere.core.parse.sql.segment.ddl.index.IndexSegment; import org.apache.shardingsphere.core.parse.sql.statement.ddl.AlterTableStatement; import org.apache.shardingsphere.core.parse.sql.statement.ddl.CreateIndexStatement; @@ -111,56 +111,56 @@ public final class ShardingSchema extends LogicSchema { } @Override - public void refreshTableMetaData(final OptimizedStatement optimizedStatement) throws SQLException { - if (null == optimizedStatement) { + public void refreshTableMetaData(final SQLStatementContext sqlStatementContext) throws SQLException { + if (null == sqlStatementContext) { return; } - if (optimizedStatement.getSqlStatement() instanceof CreateTableStatement) { - refreshTableMetaDataForCreateTable(optimizedStatement); - } else if (optimizedStatement.getSqlStatement() instanceof AlterTableStatement) { - refreshTableMetaDataForAlterTable(optimizedStatement); - } else if (optimizedStatement.getSqlStatement() instanceof DropTableStatement) { - refreshTableMetaDataForDropTable(optimizedStatement); - } else if (optimizedStatement.getSqlStatement() instanceof CreateIndexStatement) { - refreshTableMetaDataForCreateIndex(optimizedStatement); - } else if (optimizedStatement.getSqlStatement() instanceof DropIndexStatement) { - refreshTableMetaDataForDropIndex(optimizedStatement); + if (sqlStatementContext.getSqlStatement() instanceof CreateTableStatement) { + refreshTableMetaDataForCreateTable(sqlStatementContext); + } else if (sqlStatementContext.getSqlStatement() instanceof AlterTableStatement) { + refreshTableMetaDataForAlterTable(sqlStatementContext); + } else if (sqlStatementContext.getSqlStatement() instanceof DropTableStatement) { + refreshTableMetaDataForDropTable(sqlStatementContext); + } else if (sqlStatementContext.getSqlStatement() instanceof CreateIndexStatement) { + refreshTableMetaDataForCreateIndex(sqlStatementContext); + } else if (sqlStatementContext.getSqlStatement() instanceof DropIndexStatement) { + refreshTableMetaDataForDropIndex(sqlStatementContext); } } - private void refreshTableMetaDataForCreateTable(final OptimizedStatement optimizedStatement) throws SQLException { - String tableName = optimizedStatement.getTables().getSingleTableName(); + private void refreshTableMetaDataForCreateTable(final SQLStatementContext sqlStatementContext) throws SQLException { + String tableName = sqlStatementContext.getTablesContext().getSingleTableName(); getMetaData().getTables().put(tableName, getTableMetaDataInitializer(metaData.getDataSources()).load(tableName, shardingRule)); } - private void refreshTableMetaDataForAlterTable(final OptimizedStatement optimizedStatement) throws SQLException { - String tableName = optimizedStatement.getTables().getSingleTableName(); + private void refreshTableMetaDataForAlterTable(final SQLStatementContext sqlStatementContext) throws SQLException { + String tableName = sqlStatementContext.getTablesContext().getSingleTableName(); getMetaData().getTables().put(tableName, getTableMetaDataInitializer(metaData.getDataSources()).load(tableName, shardingRule)); } - private void refreshTableMetaDataForDropTable(final OptimizedStatement optimizedStatement) { - for (String each : optimizedStatement.getTables().getTableNames()) { + private void refreshTableMetaDataForDropTable(final SQLStatementContext sqlStatementContext) { + for (String each : sqlStatementContext.getTablesContext().getTableNames()) { getMetaData().getTables().remove(each); } } - private void refreshTableMetaDataForCreateIndex(final OptimizedStatement optimizedStatement) { - CreateIndexStatement createIndexStatement = (CreateIndexStatement) optimizedStatement.getSqlStatement(); + private void refreshTableMetaDataForCreateIndex(final SQLStatementContext sqlStatementContext) { + CreateIndexStatement createIndexStatement = (CreateIndexStatement) sqlStatementContext.getSqlStatement(); if (null != createIndexStatement.getIndex()) { - getMetaData().getTables().get(optimizedStatement.getTables().getSingleTableName()).getIndexes().add(createIndexStatement.getIndex().getName()); + getMetaData().getTables().get(sqlStatementContext.getTablesContext().getSingleTableName()).getIndexes().add(createIndexStatement.getIndex().getName()); } } - private void refreshTableMetaDataForDropIndex(final OptimizedStatement optimizedStatement) { - DropIndexStatement dropIndexStatement = (DropIndexStatement) optimizedStatement.getSqlStatement(); + private void refreshTableMetaDataForDropIndex(final SQLStatementContext sqlStatementContext) { + DropIndexStatement dropIndexStatement = (DropIndexStatement) sqlStatementContext.getSqlStatement(); Collection indexNames = getIndexNames(dropIndexStatement); - if (!optimizedStatement.getTables().isEmpty()) { - getMetaData().getTables().get(optimizedStatement.getTables().getSingleTableName()).getIndexes().removeAll(indexNames); + if (!sqlStatementContext.getTablesContext().isEmpty()) { + getMetaData().getTables().get(sqlStatementContext.getTablesContext().getSingleTableName()).getIndexes().removeAll(indexNames); } for (String each : indexNames) { Optional logicTableName = findLogicTableName(getMetaData().getTables(), each); if (logicTableName.isPresent()) { - getMetaData().getTables().get(optimizedStatement.getTables().getSingleTableName()).getIndexes().remove(each); + getMetaData().getTables().get(sqlStatementContext.getTablesContext().getSingleTableName()).getIndexes().remove(each); } } } diff --git a/sharding-proxy/sharding-proxy-frontend/sharding-proxy-frontend-mysql/src/main/java/org/apache/shardingsphere/shardingproxy/frontend/mysql/command/query/binary/prepare/MySQLComStmtPrepareExecutor.java b/sharding-proxy/sharding-proxy-frontend/sharding-proxy-frontend-mysql/src/main/java/org/apache/shardingsphere/shardingproxy/frontend/mysql/command/query/binary/prepare/MySQLComStmtPrepareExecutor.java index 10217dad19..707995805b 100644 --- a/sharding-proxy/sharding-proxy-frontend/sharding-proxy-frontend-mysql/src/main/java/org/apache/shardingsphere/shardingproxy/frontend/mysql/command/query/binary/prepare/MySQLComStmtPrepareExecutor.java +++ b/sharding-proxy/sharding-proxy-frontend/sharding-proxy-frontend-mysql/src/main/java/org/apache/shardingsphere/shardingproxy/frontend/mysql/command/query/binary/prepare/MySQLComStmtPrepareExecutor.java @@ -17,7 +17,7 @@ package org.apache.shardingsphere.shardingproxy.frontend.mysql.command.query.binary.prepare; -import org.apache.shardingsphere.core.optimize.segment.Tables; +import org.apache.shardingsphere.core.optimize.segment.table.TablesContext; import org.apache.shardingsphere.core.parse.sql.statement.SQLStatement; import org.apache.shardingsphere.shardingproxy.backend.communication.jdbc.connection.BackendConnection; import org.apache.shardingsphere.shardingproxy.backend.schema.LogicSchema; @@ -62,11 +62,11 @@ public final class MySQLComStmtPrepareExecutor implements CommandExecutor { SQLStatement sqlStatement = logicSchema.getParseEngine().parse(packet.getSql(), true); int parametersCount = sqlStatement.getParametersCount(); result.add(new MySQLComStmtPrepareOKPacket(++currentSequenceId, PREPARED_STATEMENT_REGISTRY.register(packet.getSql(), parametersCount), getNumColumns(), parametersCount, 0)); - Tables tables = new Tables(sqlStatement); + TablesContext tablesContext = new TablesContext(sqlStatement); for (int i = 0; i < parametersCount; i++) { // TODO add column name result.add(new MySQLColumnDefinition41Packet(++currentSequenceId, schemaName, - tables.isSingleTable() ? tables.getSingleTableName() : "", "", "", "", 100, MySQLColumnType.MYSQL_TYPE_VARCHAR, 0)); + tablesContext.isSingleTable() ? tablesContext.getSingleTableName() : "", "", "", "", 100, MySQLColumnType.MYSQL_TYPE_VARCHAR, 0)); } if (parametersCount > 0) { result.add(new MySQLEofPacket(++currentSequenceId)); -- GitLab