Skip to content

Commit

Permalink
for checkstyle
Browse files Browse the repository at this point in the history
  • Loading branch information
terrymanu committed Apr 27, 2018
1 parent 5323391 commit 4a4cab8
Show file tree
Hide file tree
Showing 9 changed files with 59 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ public final class SQLRewriteEngine {
* @param sqlStatement SQL statement
* @param generatedKey generated key
*/
public SQLRewriteEngine(final ShardingRule shardingRule, final String originalSQL, final DatabaseType databaseType, final SQLStatement sqlStatement, final List<Object> parameters, final GeneratedKey generatedKey) {
public SQLRewriteEngine(final ShardingRule shardingRule, final String originalSQL,
final DatabaseType databaseType, final SQLStatement sqlStatement, final List<Object> parameters, final GeneratedKey generatedKey) {
this.shardingRule = shardingRule;
this.originalSQL = originalSQL;
this.databaseType = databaseType;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -64,9 +65,9 @@ private static ShardingRule buildShardingRule() throws IOException {

private static ShardingMetaData buildShardingMetaData() {
Map<String, TableMetaData> tableMetaDataMap = new HashMap<>();
tableMetaDataMap.put("t_order", getTableMetaData(new ArrayList<String>(){{add("user_id");add("order_id");}}));
tableMetaDataMap.put("t_order_item", getTableMetaData(new ArrayList<String>(){{add("user_id");add("order_id");add("item_id");add("c_date");}}));
tableMetaDataMap.put("t_place", getTableMetaData(new ArrayList<String>(){{add("user_new_id");add("guid");}}));
tableMetaDataMap.put("t_order", getTableMetaData(Arrays.asList("user_id", "order_id")));
tableMetaDataMap.put("t_order_item", getTableMetaData(Arrays.asList("user_id", "order_id", "item_id", "c_date")));
tableMetaDataMap.put("t_place", getTableMetaData(Arrays.asList("user_new_id", "guid")));
ShardingMetaData shardingMetaData = Mockito.mock(ShardingMetaData.class);
when(shardingMetaData.getTableMetaDataMap()).thenReturn(tableMetaDataMap);
return shardingMetaData;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,8 @@ private void assertUpdateStatementWitParameter(final DMLStatement updateStatemen
@Test
public void parseWithOr() {
ShardingRule shardingRule = createShardingRule();
DMLStatement updateStatement = (DMLStatement) new SQLParsingEngine(DatabaseType.Oracle, "UPDATE TABLE_XXX AS xxx SET field1=1 WHERE field1<1 AND (field1 >2 OR xxx.field2 =1)", shardingRule, null).parse(false);
DMLStatement updateStatement = (DMLStatement) new SQLParsingEngine(
DatabaseType.Oracle, "UPDATE TABLE_XXX AS xxx SET field1=1 WHERE field1<1 AND (field1 >2 OR xxx.field2 =1)", shardingRule, null).parse(false);
assertUpdateStatementWitOr(updateStatement);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ public void setUp() throws IOException {

@Test
public void assertRewriteWithoutChange() {
SQLRewriteEngine rewriteEngine = new SQLRewriteEngine(shardingRule, "SELECT table_y.id FROM table_y WHERE table_y.id=?", DatabaseType.MySQL, selectStatement, Collections.<Object>singletonList(1), null);
SQLRewriteEngine rewriteEngine = new SQLRewriteEngine(
shardingRule, "SELECT table_y.id FROM table_y WHERE table_y.id=?", DatabaseType.MySQL, selectStatement, Collections.<Object>singletonList(1), null);
assertThat(rewriteEngine.rewrite(true).toSQL(tableTokens, null).getSql(), is("SELECT table_y.id FROM table_y WHERE table_y.id=?"));
}

Expand All @@ -93,7 +94,8 @@ public void assertRewriteForTableName() {
selectStatement.getSqlTokens().add(new TableToken(7, "table_x"));
selectStatement.getSqlTokens().add(new TableToken(31, "table_x"));
selectStatement.getSqlTokens().add(new TableToken(47, "table_x"));
SQLRewriteEngine rewriteEngine = new SQLRewriteEngine(shardingRule, "SELECT table_x.id, x.name FROM table_x x WHERE table_x.id=? AND x.name=?", DatabaseType.MySQL, selectStatement, parameters, null);
SQLRewriteEngine rewriteEngine = new SQLRewriteEngine(
shardingRule, "SELECT table_x.id, x.name FROM table_x x WHERE table_x.id=? AND x.name=?", DatabaseType.MySQL, selectStatement, parameters, null);
assertThat(rewriteEngine.rewrite(true).toSQL(tableTokens, null).getSql(), is("SELECT table_1.id, x.name FROM table_1 x WHERE table_1.id=? AND x.name=?"));
}

Expand All @@ -103,8 +105,10 @@ public void assertRewriteForOrderByAndGroupByDerivedColumns() {
ItemsToken itemsToken = new ItemsToken(12);
itemsToken.getItems().addAll(Arrays.asList("x.id as ORDER_BY_DERIVED_0", "x.name as GROUP_BY_DERIVED_0"));
selectStatement.getSqlTokens().add(itemsToken);
SQLRewriteEngine rewriteEngine = new SQLRewriteEngine(shardingRule, "SELECT x.age FROM table_x x GROUP BY x.id ORDER BY x.name", DatabaseType.MySQL, selectStatement, Collections.emptyList(), null);
assertThat(rewriteEngine.rewrite(true).toSQL(tableTokens, null).getSql(), is("SELECT x.age, x.id as ORDER_BY_DERIVED_0, x.name as GROUP_BY_DERIVED_0 FROM table_1 x GROUP BY x.id ORDER BY x.name"));
SQLRewriteEngine rewriteEngine = new SQLRewriteEngine(
shardingRule, "SELECT x.age FROM table_x x GROUP BY x.id ORDER BY x.name", DatabaseType.MySQL, selectStatement, Collections.emptyList(), null);
assertThat(rewriteEngine.rewrite(true).toSQL(
tableTokens, null).getSql(), is("SELECT x.age, x.id as ORDER_BY_DERIVED_0, x.name as GROUP_BY_DERIVED_0 FROM table_1 x GROUP BY x.id ORDER BY x.name"));
}

@Test
Expand Down Expand Up @@ -154,7 +158,8 @@ public void assertRewriteForRowNum() {
selectStatement.getSqlTokens().add(new OffsetToken(119, 2));
selectStatement.getSqlTokens().add(new RowCountToken(98, 4));
SQLRewriteEngine rewriteEngine = new SQLRewriteEngine(shardingRule,
"SELECT * FROM (SELECT row_.*, rownum rownum_ FROM (SELECT x.id FROM table_x x) row_ WHERE rownum<=4) t WHERE t.rownum_>2", DatabaseType.Oracle, selectStatement, Collections.emptyList(), null);
"SELECT * FROM (SELECT row_.*, rownum rownum_ FROM (SELECT x.id FROM table_x x) row_ WHERE rownum<=4) t WHERE t.rownum_>2",
DatabaseType.Oracle, selectStatement, Collections.emptyList(), null);
assertThat(rewriteEngine.rewrite(true).toSQL(tableTokens, null).getSql(),
is("SELECT * FROM (SELECT row_.*, rownum rownum_ FROM (SELECT x.id FROM table_1 x) row_ WHERE rownum<=4) t WHERE t.rownum_>0"));
}
Expand All @@ -168,7 +173,8 @@ public void assertRewriteForTopAndRowNumber() {
selectStatement.getSqlTokens().add(new OffsetToken(123, 2));
selectStatement.getSqlTokens().add(new RowCountToken(26, 4));
SQLRewriteEngine rewriteEngine = new SQLRewriteEngine(shardingRule,
"SELECT * FROM (SELECT TOP(4) row_number() OVER (ORDER BY x.id) AS rownum_, x.id FROM table_x x) AS row_ WHERE row_.rownum_>2", DatabaseType.SQLServer, selectStatement, Collections.emptyList(), null);
"SELECT * FROM (SELECT TOP(4) row_number() OVER (ORDER BY x.id) AS rownum_, x.id FROM table_x x) AS row_ WHERE row_.rownum_>2",
DatabaseType.SQLServer, selectStatement, Collections.emptyList(), null);
assertThat(rewriteEngine.rewrite(true).toSQL(tableTokens, null).getSql(),
is("SELECT * FROM (SELECT TOP(4) row_number() OVER (ORDER BY x.id) AS rownum_, x.id FROM table_1 x) AS row_ WHERE row_.rownum_>0"));
}
Expand Down Expand Up @@ -198,7 +204,8 @@ public void assertRewriteForRowNumForMemoryGroupBy() {
selectStatement.getOrderByItems().add(new OrderItem("x", "id", OrderDirection.ASC, OrderDirection.ASC, Optional.<String>absent()));
selectStatement.getGroupByItems().add(new OrderItem("x", "id", OrderDirection.DESC, OrderDirection.ASC, Optional.<String>absent()));
SQLRewriteEngine rewriteEngine = new SQLRewriteEngine(shardingRule,
"SELECT * FROM (SELECT row_.*, rownum rownum_ FROM (SELECT x.id FROM table_x x) row_ WHERE rownum<=4) t WHERE t.rownum_>2", DatabaseType.Oracle, selectStatement, Collections.emptyList(), null);
"SELECT * FROM (SELECT row_.*, rownum rownum_ FROM (SELECT x.id FROM table_x x) row_ WHERE rownum<=4) t WHERE t.rownum_>2",
DatabaseType.Oracle, selectStatement, Collections.emptyList(), null);
assertThat(rewriteEngine.rewrite(true).toSQL(tableTokens, null).getSql(),
is("SELECT * FROM (SELECT row_.*, rownum rownum_ FROM (SELECT x.id FROM table_1 x) row_ WHERE rownum<=2147483647) t WHERE t.rownum_>0"));
}
Expand All @@ -214,7 +221,8 @@ public void assertRewriteForTopAndRowNumberForMemoryGroupBy() {
selectStatement.getOrderByItems().add(new OrderItem("x", "id", OrderDirection.ASC, OrderDirection.ASC, Optional.<String>absent()));
selectStatement.getGroupByItems().add(new OrderItem("x", "id", OrderDirection.DESC, OrderDirection.ASC, Optional.<String>absent()));
SQLRewriteEngine rewriteEngine = new SQLRewriteEngine(shardingRule,
"SELECT * FROM (SELECT TOP(4) row_number() OVER (ORDER BY x.id) AS rownum_, x.id FROM table_x x) AS row_ WHERE row_.rownum_>2", DatabaseType.SQLServer, selectStatement, Collections.emptyList(), null);
"SELECT * FROM (SELECT TOP(4) row_number() OVER (ORDER BY x.id) AS rownum_, x.id FROM table_x x) AS row_ WHERE row_.rownum_>2",
DatabaseType.SQLServer, selectStatement, Collections.emptyList(), null);
assertThat(rewriteEngine.rewrite(true).toSQL(tableTokens, null).getSql(),
is("SELECT * FROM (SELECT TOP(2147483647) row_number() OVER (ORDER BY x.id) AS rownum_, x.id FROM table_1 x) AS row_ WHERE row_.rownum_>0"));
}
Expand All @@ -240,7 +248,8 @@ public void assertRewriteForRowNumForNotRewriteLimit() {
selectStatement.getSqlTokens().add(new OffsetToken(119, 2));
selectStatement.getSqlTokens().add(new RowCountToken(98, 4));
SQLRewriteEngine rewriteEngine = new SQLRewriteEngine(shardingRule,
"SELECT * FROM (SELECT row_.*, rownum rownum_ FROM (SELECT x.id FROM table_x x) row_ WHERE rownum<=4) t WHERE t.rownum_>2", DatabaseType.Oracle, selectStatement, Collections.emptyList(), null);
"SELECT * FROM (SELECT row_.*, rownum rownum_ FROM (SELECT x.id FROM table_x x) row_ WHERE rownum<=4) t WHERE t.rownum_>2",
DatabaseType.Oracle, selectStatement, Collections.emptyList(), null);
assertThat(rewriteEngine.rewrite(false).toSQL(tableTokens, null).getSql(),
is("SELECT * FROM (SELECT row_.*, rownum rownum_ FROM (SELECT x.id FROM table_1 x) row_ WHERE rownum<=4) t WHERE t.rownum_>2"));
}
Expand All @@ -254,7 +263,8 @@ public void assertRewriteForTopAndRowNumberForNotRewriteLimit() {
selectStatement.getSqlTokens().add(new OffsetToken(123, 2));
selectStatement.getSqlTokens().add(new RowCountToken(26, 4));
SQLRewriteEngine rewriteEngine = new SQLRewriteEngine(shardingRule,
"SELECT * FROM (SELECT TOP(4) row_number() OVER (ORDER BY x.id) AS rownum_, x.id FROM table_x x) AS row_ WHERE row_.rownum_>2", DatabaseType.SQLServer, selectStatement, Collections.emptyList(), null);
"SELECT * FROM (SELECT TOP(4) row_number() OVER (ORDER BY x.id) AS rownum_, x.id FROM table_x x) AS row_ WHERE row_.rownum_>2",
DatabaseType.SQLServer, selectStatement, Collections.emptyList(), null);
assertThat(rewriteEngine.rewrite(false).toSQL(tableTokens, null).getSql(),
is("SELECT * FROM (SELECT TOP(4) row_number() OVER (ORDER BY x.id) AS rownum_, x.id FROM table_1 x) AS row_ WHERE row_.rownum_>2"));
}
Expand All @@ -266,7 +276,8 @@ public void assertRewriteForDerivedOrderBy() {
selectStatement.getOrderByItems().add(new OrderItem("x", "name", OrderDirection.DESC, OrderDirection.ASC, Optional.<String>absent()));
selectStatement.getSqlTokens().add(new TableToken(25, "table_x"));
selectStatement.getSqlTokens().add(new OrderByToken(61));
SQLRewriteEngine rewriteEngine = new SQLRewriteEngine(shardingRule, "SELECT x.id, x.name FROM table_x x GROUP BY x.id, x.name DESC", DatabaseType.MySQL, selectStatement, Collections.emptyList(), null);
SQLRewriteEngine rewriteEngine = new SQLRewriteEngine(shardingRule, "SELECT x.id, x.name FROM table_x x GROUP BY x.id, x.name DESC",
DatabaseType.MySQL, selectStatement, Collections.emptyList(), null);
assertThat(rewriteEngine.rewrite(true).toSQL(tableTokens, null).getSql(), is("SELECT x.id, x.name FROM table_1 x GROUP BY x.id, x.name DESC ORDER BY id ASC,name DESC "));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public final class ShardingPreparedStatement extends AbstractShardingPreparedSta

private final Collection<PreparedStatement> routedStatements = new LinkedList<>();

private int batchCount = 0;
private int batchCount;

@Getter(AccessLevel.NONE)
private boolean returnGeneratedKeys;
Expand Down Expand Up @@ -114,7 +114,8 @@ public ShardingPreparedStatement(final ShardingConnection connection, final Stri
this.resultSetConcurrency = resultSetConcurrency;
this.resultSetHoldability = resultSetHoldability;
ShardingContext shardingContext = connection.getShardingContext();
routingEngine = new PreparedStatementRoutingEngine(sql, shardingContext.getShardingRule(), shardingContext.getShardingMetaData(), shardingContext.getDatabaseType(), shardingContext.isShowSQL());
routingEngine = new PreparedStatementRoutingEngine(
sql, shardingContext.getShardingRule(), shardingContext.getShardingMetaData(), shardingContext.getDatabaseType(), shardingContext.isShowSQL());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,16 @@
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedList;

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.when;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

public final class BatchPreparedStatementExecutorTest extends AbstractBaseExecutorTest {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,25 @@

import javax.sql.DataSource;
import java.lang.reflect.Field;
import java.sql.*;
import java.util.*;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.not;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.*;
import static org.mockito.Mockito.atLeast;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

public final class ShardingDataSourceTest {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,11 @@ public void before() {
public void assertSingleStatement() throws Exception {
Statement statement = mock(Statement.class);
when(statement.getConnection()).thenReturn(mock(Connection.class));
executorEngine.execute(SQLType.DML, Collections.singleton(new StatementUnit(new SQLExecutionUnit("ds_0", new SQLUnit("insert into ...", Collections.singletonList(Collections.<Object>singletonList(1)))), statement)), new ExecuteCallback<Integer>() {
executorEngine.execute(SQLType.DML, Collections.singleton(new StatementUnit(new SQLExecutionUnit("ds_0",
new SQLUnit("insert into ...", Collections.singletonList(Collections.<Object>singletonList(1)))), statement)), new ExecuteCallback<Integer>() {

@Override
public Integer execute(final BaseStatementUnit baseStatementUnit) throws Exception {
public Integer execute(final BaseStatementUnit baseStatementUnit) {
return 0;
}
});
Expand Down Expand Up @@ -94,7 +96,9 @@ public Integer execute(final BaseStatementUnit baseStatementUnit) throws Excepti
public void assertSQLException() throws Exception {
Statement statement = mock(Statement.class);
when(statement.getConnection()).thenReturn(mock(Connection.class));
executorEngine.execute(SQLType.DQL, Collections.singleton(new StatementUnit(new SQLExecutionUnit("ds_0", new SQLUnit("select ...", Collections.singletonList(Collections.<Object>singletonList(1)))), statement)), new ExecuteCallback<Integer>() {
executorEngine.execute(SQLType.DQL, Collections.singleton(new StatementUnit(new SQLExecutionUnit("ds_0",
new SQLUnit("select ...", Collections.singletonList(Collections.<Object>singletonList(1)))), statement)), new ExecuteCallback<Integer>() {

@Override
public Integer execute(final BaseStatementUnit baseStatementUnit) throws Exception {
throw new SQLException();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@ public final class StatementExecuteBackendHandler implements BackendHandler {

public StatementExecuteBackendHandler(final List<PreparedStatementParameter> preparedStatementParameters, final int statementId, final DatabaseType databaseType, final boolean showSQL) {
this.preparedStatementParameters = preparedStatementParameters;
routingEngine = new PreparedStatementRoutingEngine(PreparedStatementRegistry.getInstance().getSQL(statementId), ShardingRuleRegistry.getInstance().getShardingRule(), ShardingRuleRegistry.getInstance().getShardingMetaData(), databaseType, showSQL);
routingEngine = new PreparedStatementRoutingEngine(PreparedStatementRegistry.getInstance().getSQL(statementId),
ShardingRuleRegistry.getInstance().getShardingRule(), ShardingRuleRegistry.getInstance().getShardingMetaData(), databaseType, showSQL);
connections = new ArrayList<>(1024);
resultSets = new ArrayList<>(1024);
columnTypes = new ArrayList<>(32);
Expand Down

0 comments on commit 4a4cab8

Please sign in to comment.