Skip to content

Commit

Permalink
fix #2136
Browse files Browse the repository at this point in the history
  • Loading branch information
terrymanu committed Apr 4, 2019
1 parent 3df920f commit 35c1d0f
Show file tree
Hide file tree
Showing 20 changed files with 300 additions and 161 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ private Collection<RouteUnit> convert(final String sql, final List<Object> param
}

private Collection<RouteUnit> rewriteAndConvert(final String sql, final List<Object> parameters, final SQLRouteResult sqlRouteResult) {
SQLRewriteEngine rewriteEngine = new SQLRewriteEngine(shardingRule, sql, databaseType, sqlRouteResult.getSqlStatement(), parameters, sqlRouteResult.getOptimizeResult());
SQLRewriteEngine rewriteEngine = new SQLRewriteEngine(shardingRule, sql, databaseType, sqlRouteResult, parameters, sqlRouteResult.getOptimizeResult());
SQLBuilder sqlBuilder = rewriteEngine.rewrite(sqlRouteResult.getRoutingResult().isSingleRouting());
Collection<RouteUnit> result = new LinkedHashSet<>();
for (TableUnit each : sqlRouteResult.getRoutingResult().getTableUnits().getTableUnits()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@
import org.apache.shardingsphere.core.merge.dal.DALMergeEngine;
import org.apache.shardingsphere.core.merge.dql.DQLMergeEngine;
import org.apache.shardingsphere.core.metadata.table.ShardingTableMetaData;
import org.apache.shardingsphere.core.parse.parser.sql.SQLStatement;
import org.apache.shardingsphere.core.parse.parser.sql.dal.DALStatement;
import org.apache.shardingsphere.core.parse.parser.sql.dql.select.SelectStatement;
import org.apache.shardingsphere.core.route.SQLRouteResult;
import org.apache.shardingsphere.core.rule.ShardingRule;

import java.sql.SQLException;
Expand All @@ -46,20 +46,20 @@ public final class MergeEngineFactory {
*
* @param databaseType database type
* @param shardingRule sharding rule
* @param sqlStatement SQL statement
* @param routeResult SQL route result
* @param shardingTableMetaData sharding table meta Data
* @param queryResults query results
* @return merge engine instance
* @throws SQLException SQL exception
*/
public static MergeEngine newInstance(final DatabaseType databaseType, final ShardingRule shardingRule,
final SQLStatement sqlStatement, final ShardingTableMetaData shardingTableMetaData, final List<QueryResult> queryResults) throws SQLException {
if (sqlStatement instanceof SelectStatement) {
return new DQLMergeEngine(databaseType, (SelectStatement) sqlStatement, queryResults);
public static MergeEngine newInstance(final DatabaseType databaseType, final ShardingRule shardingRule,
final SQLRouteResult routeResult, final ShardingTableMetaData shardingTableMetaData, final List<QueryResult> queryResults) throws SQLException {
if (routeResult.getSqlStatement() instanceof SelectStatement) {
return new DQLMergeEngine(databaseType, routeResult, queryResults);
}
if (sqlStatement instanceof DALStatement) {
return new DALMergeEngine(shardingRule, queryResults, (DALStatement) sqlStatement, shardingTableMetaData);
if (routeResult.getSqlStatement() instanceof DALStatement) {
return new DALMergeEngine(shardingRule, queryResults, (DALStatement) routeResult.getSqlStatement(), shardingTableMetaData);
}
throw new UnsupportedOperationException(String.format("Cannot support type '%s'", sqlStatement.getType()));
throw new UnsupportedOperationException(String.format("Cannot support type '%s'", routeResult.getSqlStatement().getType()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import org.apache.shardingsphere.core.parse.parser.context.limit.Limit;
import org.apache.shardingsphere.core.parse.parser.sql.dql.select.SelectStatement;
import org.apache.shardingsphere.core.parse.util.SQLUtil;
import org.apache.shardingsphere.core.route.SQLRouteResult;

import java.sql.SQLException;
import java.util.ArrayList;
Expand All @@ -53,16 +54,19 @@ public final class DQLMergeEngine implements MergeEngine {

private final DatabaseType databaseType;

private final SQLRouteResult routeResult;

private final SelectStatement selectStatement;

private final List<QueryResult> queryResults;

@Getter
private final Map<String, Integer> columnLabelIndexMap;

public DQLMergeEngine(final DatabaseType databaseType, final SelectStatement selectStatement, final List<QueryResult> queryResults) throws SQLException {
public DQLMergeEngine(final DatabaseType databaseType, final SQLRouteResult routeResult, final List<QueryResult> queryResults) throws SQLException {
this.databaseType = databaseType;
this.selectStatement = selectStatement;
this.routeResult = routeResult;
this.selectStatement = (SelectStatement) routeResult.getSqlStatement();
this.queryResults = getRealQueryResults(queryResults);
columnLabelIndexMap = getColumnLabelIndexMap(this.queryResults.get(0));
}
Expand Down Expand Up @@ -131,18 +135,18 @@ private MergedResult getGroupByMergedResult() throws SQLException {
}

private MergedResult decorate(final MergedResult mergedResult) throws SQLException {
Limit limit = selectStatement.getLimit();
Limit limit = routeResult.getLimit();
if (null == limit || 1 == queryResults.size()) {
return mergedResult;
}
if (DatabaseType.MySQL == databaseType || DatabaseType.PostgreSQL == databaseType || DatabaseType.H2 == databaseType) {
return new LimitDecoratorMergedResult(mergedResult, selectStatement.getLimit());
return new LimitDecoratorMergedResult(mergedResult, routeResult.getLimit());
}
if (DatabaseType.Oracle == databaseType) {
return new RowNumberDecoratorMergedResult(mergedResult, selectStatement.getLimit());
return new RowNumberDecoratorMergedResult(mergedResult, routeResult.getLimit());
}
if (DatabaseType.SQLServer == databaseType) {
return new TopAndRowNumberDecoratorMergedResult(mergedResult, selectStatement.getLimit());
return new TopAndRowNumberDecoratorMergedResult(mergedResult, routeResult.getLimit());
}
return mergedResult;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,6 @@
ShowOtherMergedResultTest.class,
ShowTablesMergedResultTest.class,
MergeEngineFactoryTest.class
})
})
public final class AllMergerTests {
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,11 @@
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.parse.parser.sql.SQLStatement;
import org.apache.shardingsphere.core.parse.parser.context.limit.Limit;
import org.apache.shardingsphere.core.parse.parser.sql.dal.DALStatement;
import org.apache.shardingsphere.core.parse.parser.sql.dml.insert.InsertStatement;
import org.apache.shardingsphere.core.parse.parser.sql.dql.select.SelectStatement;
import org.apache.shardingsphere.core.route.SQLRouteResult;
import org.junit.Before;
import org.junit.Test;

Expand Down Expand Up @@ -59,19 +60,20 @@ public void setUp() throws SQLException {

@Test
public void assertNewInstanceWithSelectStatement() throws SQLException {
SQLStatement selectStatement = new SelectStatement();
assertThat(MergeEngineFactory.newInstance(DatabaseType.MySQL, null, selectStatement, null, queryResults), instanceOf(DQLMergeEngine.class));
SQLRouteResult routeResult = new SQLRouteResult(new SelectStatement());
routeResult.setLimit(new Limit());
assertThat(MergeEngineFactory.newInstance(DatabaseType.MySQL, null, routeResult, null, queryResults), instanceOf(DQLMergeEngine.class));
}

@Test
public void assertNewInstanceWithDALStatement() throws SQLException {
SQLStatement dalStatement = new DALStatement();
assertThat(MergeEngineFactory.newInstance(DatabaseType.MySQL, null, dalStatement, null, queryResults), instanceOf(DALMergeEngine.class));
SQLRouteResult routeResult = new SQLRouteResult(new DALStatement());
assertThat(MergeEngineFactory.newInstance(DatabaseType.MySQL, null, routeResult, null, queryResults), instanceOf(DALMergeEngine.class));
}

@Test(expected = UnsupportedOperationException.class)
public void assertNewInstanceWithOtherStatement() throws SQLException {
SQLStatement insertStatement = new InsertStatement();
MergeEngineFactory.newInstance(DatabaseType.MySQL, null, insertStatement, null, queryResults);
SQLRouteResult routeResult = new SQLRouteResult(new InsertStatement());
MergeEngineFactory.newInstance(DatabaseType.MySQL, null, routeResult, null, queryResults);
}
}
Loading

0 comments on commit 35c1d0f

Please sign in to comment.