use of io.shardingjdbc.core.parsing.parser.sql.dql.select.SelectStatement in project sharding-jdbc by shardingjdbc.
the class GroupByStreamMergedResultTest method setUp.
@Before
public void setUp() throws SQLException {
resultSets = Lists.newArrayList(mockResultSet(), mockResultSet(), mockResultSet());
queryResults = new ArrayList<>(resultSets.size());
for (ResultSet each : resultSets) {
queryResults.add(new TestQueryResult(each));
}
selectStatement = new SelectStatement();
AggregationSelectItem aggregationSelectItem1 = new AggregationSelectItem(AggregationType.COUNT, "(*)", Optional.<String>absent());
aggregationSelectItem1.setIndex(1);
AggregationSelectItem aggregationSelectItem2 = new AggregationSelectItem(AggregationType.AVG, "(num)", Optional.<String>absent());
aggregationSelectItem2.setIndex(2);
AggregationSelectItem derivedAggregationSelectItem1 = new AggregationSelectItem(AggregationType.COUNT, "(num)", Optional.of("AVG_DERIVED_COUNT_0"));
aggregationSelectItem2.setIndex(5);
aggregationSelectItem2.getDerivedAggregationSelectItems().add(derivedAggregationSelectItem1);
AggregationSelectItem derivedAggregationSelectItem2 = new AggregationSelectItem(AggregationType.SUM, "(num)", Optional.of("AVG_DERIVED_SUM_0"));
aggregationSelectItem2.setIndex(6);
aggregationSelectItem2.getDerivedAggregationSelectItems().add(derivedAggregationSelectItem2);
selectStatement.getItems().add(aggregationSelectItem1);
selectStatement.getItems().add(aggregationSelectItem2);
selectStatement.getGroupByItems().add(new OrderItem(3, OrderDirection.ASC, OrderDirection.ASC));
selectStatement.getOrderByItems().add(new OrderItem(3, OrderDirection.ASC, OrderDirection.ASC));
}
use of io.shardingjdbc.core.parsing.parser.sql.dql.select.SelectStatement in project sharding-jdbc by shardingjdbc.
the class MergeEngineFactoryTest method assertNewInstanceWithSelectStatement.
@Test
public void assertNewInstanceWithSelectStatement() throws SQLException {
SQLStatement selectStatement = new SelectStatement();
assertThat(MergeEngineFactory.newInstance(null, queryResults, selectStatement), instanceOf(DQLMergeEngine.class));
}
use of io.shardingjdbc.core.parsing.parser.sql.dql.select.SelectStatement in project sharding-jdbc by shardingjdbc.
the class SQLRewriteEngine method appendLimitRowCount.
private void appendLimitRowCount(final SQLBuilder sqlBuilder, final RowCountToken rowCountToken, final int count, final List<SQLToken> sqlTokens, final boolean isRewrite) {
SelectStatement selectStatement = (SelectStatement) sqlStatement;
Limit limit = selectStatement.getLimit();
if (!isRewrite) {
sqlBuilder.appendLiterals(String.valueOf(rowCountToken.getRowCount()));
} else if ((!selectStatement.getGroupByItems().isEmpty() || !selectStatement.getAggregationSelectItems().isEmpty()) && !selectStatement.isSameGroupByAndOrderByItems()) {
sqlBuilder.appendLiterals(String.valueOf(Integer.MAX_VALUE));
} else {
sqlBuilder.appendLiterals(String.valueOf(limit.isNeedRewriteRowCount() ? rowCountToken.getRowCount() + limit.getOffsetValue() : rowCountToken.getRowCount()));
}
int beginPosition = rowCountToken.getBeginPosition() + String.valueOf(rowCountToken.getRowCount()).length();
appendRest(sqlBuilder, count, sqlTokens, beginPosition);
}
use of io.shardingjdbc.core.parsing.parser.sql.dql.select.SelectStatement in project sharding-jdbc by shardingjdbc.
the class ParsingSQLRouter method route.
@Override
public SQLRouteResult route(final String logicSQL, final List<Object> parameters, final SQLStatement sqlStatement) {
SQLRouteResult result = new SQLRouteResult(sqlStatement);
if (sqlStatement instanceof InsertStatement && null != ((InsertStatement) sqlStatement).getGeneratedKey()) {
processGeneratedKey(parameters, (InsertStatement) sqlStatement, result);
}
RoutingResult routingResult = route(parameters, sqlStatement);
SQLRewriteEngine rewriteEngine = new SQLRewriteEngine(shardingRule, logicSQL, databaseType, sqlStatement);
boolean isSingleRouting = routingResult.isSingleRouting();
if (sqlStatement instanceof SelectStatement && null != ((SelectStatement) sqlStatement).getLimit()) {
processLimit(parameters, (SelectStatement) sqlStatement, isSingleRouting);
}
SQLBuilder sqlBuilder = rewriteEngine.rewrite(!isSingleRouting);
if (routingResult instanceof CartesianRoutingResult) {
for (CartesianDataSource cartesianDataSource : ((CartesianRoutingResult) routingResult).getRoutingDataSources()) {
for (CartesianTableReference cartesianTableReference : cartesianDataSource.getRoutingTableReferences()) {
result.getExecutionUnits().add(new SQLExecutionUnit(cartesianDataSource.getDataSource(), rewriteEngine.generateSQL(cartesianTableReference, sqlBuilder)));
}
}
} else {
for (TableUnit each : routingResult.getTableUnits().getTableUnits()) {
result.getExecutionUnits().add(new SQLExecutionUnit(each.getDataSourceName(), rewriteEngine.generateSQL(each, sqlBuilder)));
}
}
if (showSQL) {
SQLLogger.logSQL(logicSQL, sqlStatement, result.getExecutionUnits(), parameters);
}
return result;
}
use of io.shardingjdbc.core.parsing.parser.sql.dql.select.SelectStatement in project sharding-jdbc by shardingjdbc.
the class ShardingPreparedStatement method getResultSet.
@Override
public ResultSet getResultSet() throws SQLException {
if (null != currentResultSet) {
return currentResultSet;
}
if (1 == routedStatements.size() && routeResult.getSqlStatement() instanceof DQLStatement) {
currentResultSet = routedStatements.iterator().next().getResultSet();
return currentResultSet;
}
List<ResultSet> resultSets = new ArrayList<>(routedStatements.size());
List<QueryResult> queryResults = new ArrayList<>(routedStatements.size());
for (PreparedStatement each : routedStatements) {
ResultSet resultSet = each.getResultSet();
resultSets.add(resultSet);
queryResults.add(new JDBCQueryResult(resultSet));
}
if (routeResult.getSqlStatement() instanceof SelectStatement || routeResult.getSqlStatement() instanceof DALStatement) {
MergeEngine mergeEngine = MergeEngineFactory.newInstance(connection.getShardingContext().getShardingRule(), queryResults, routeResult.getSqlStatement());
currentResultSet = new ShardingResultSet(resultSets, mergeEngine.merge(), this);
}
return currentResultSet;
}
Aggregations