Skip to content

Commit

Permalink
add case4
Browse files Browse the repository at this point in the history
  • Loading branch information
junwen12221 committed Jun 18, 2021
1 parent e716c69 commit 6e06b00
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 8 deletions.
25 changes: 17 additions & 8 deletions example/src/test/java/io/mycat/assemble/MycatTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,17 @@
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;
import java.util.stream.Collectors;


public interface MycatTest {

String DB_MYCAT = System.getProperty("db_mycat","jdbc:mysql://localhost:8066/mysql?username=root&password=123456&characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true");
String DB1 = System.getProperty("db1","jdbc:mysql://localhost:3306/mysql?username=root&password=123456&characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true");
String DB2 = System.getProperty("db2","jdbc:mysql://localhost:3307/mysql?username=root&password=123456&characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true");
String DB_MYCAT_PSTMT = System.getProperty("db_mycat","jdbc:mysql://localhost:8066/mysql?username=root&password=123456&characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true&useServerPrepStmts=true");
String DB_MYCAT = System.getProperty("db_mycat", "jdbc:mysql://localhost:8066/mysql?username=root&password=123456&characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true");
String DB1 = System.getProperty("db1", "jdbc:mysql://localhost:3306/mysql?username=root&password=123456&characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true");
String DB2 = System.getProperty("db2", "jdbc:mysql://localhost:3307/mysql?username=root&password=123456&characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true");
String DB_MYCAT_PSTMT = System.getProperty("db_mycat", "jdbc:mysql://localhost:8066/mysql?username=root&password=123456&characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true&useServerPrepStmts=true");

String RESET_CONFIG ="/*+ mycat:resetConfig{} */";
String RESET_CONFIG = "/*+ mycat:resetConfig{} */";

Map<String, DruidDataSource> dsMap = new ConcurrentHashMap<>();
Logger LOGGER = LoggerFactory.getLogger(MycatTest.class);
Expand All @@ -41,8 +42,8 @@ default Connection getMySQLConnection(String url) throws Exception {
@SneakyThrows
public DruidDataSource apply(String url) {
Map<String, String> urlParameters = JsonUtil.urlSplit(url);
String username = urlParameters.getOrDefault("username","root");
String password = urlParameters.getOrDefault("password","123456");
String username = urlParameters.getOrDefault("username", "root");
String password = urlParameters.getOrDefault("password", "123456");

DruidDataSource dataSource = new DruidDataSource();
dataSource.setUrl(url);
Expand All @@ -66,10 +67,12 @@ public default boolean existTable(Connection connection, String db, String table
public default boolean hasData(Connection connection, String db, String table) throws Exception {
return !executeQuery(connection, String.format("select * from %s.%s limit 1", db, table)).isEmpty();
}

public default long count(Connection connection, String db, String table) throws Exception {
Number count = (Number)executeQuery(connection, String.format("select count(1) as `count` from %s.%s", db, table)).get(0).get("count");
Number count = (Number) executeQuery(connection, String.format("select count(1) as `count` from %s.%s", db, table)).get(0).get("count");
return count.longValue();
}

public default void deleteData(Connection connection, String db, String table) throws Exception {
execute(connection, String.format("delete from %s.%s", db, table));
}
Expand All @@ -83,6 +86,12 @@ public default List<Map<String, Object>> executeQuery(Connection mySQLConnection
LOGGER.info(sql);
return JdbcUtils.executeQuery(mySQLConnection, sql, Collections.emptyList());
}

public default String explain(Connection mySQLConnection, String sql) throws Exception {
List<Map<String, Object>> maps = JdbcUtils.executeQuery(mySQLConnection, "explain "+sql, Collections.emptyList());
return maps.stream().flatMap(i -> i.values().stream()).map(i -> (String) i).collect(Collectors.joining("\n"));
}

public default void addC0(Connection connection) throws Exception {
execute(connection, CreateDataSourceHint
.create("newDs",
Expand Down
76 changes: 76 additions & 0 deletions example/src/test/java/io/mycat/sql/UserCaseTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -184,4 +184,80 @@ public void case3() throws Exception {
Assert.assertTrue("[{id=1, name=abc, is_enable=1}]".equals(maps.toString())||"[{id=1, name=abc, is_enable=true}]".equals(maps.toString()));
}
}

@Test
public void case4() throws Exception {
try (Connection mycatConnection = getMySQLConnection(DB_MYCAT)) {
execute(mycatConnection, RESET_CONFIG);

execute(mycatConnection, "DROP DATABASE cloud");


execute(mycatConnection, "CREATE DATABASE cloud");


execute(mycatConnection, CreateDataSourceHint
.create("ds0",
DB1));

execute(mycatConnection, CreateDataSourceHint
.create("ds1",
DB2));

execute(mycatConnection,
CreateClusterHint.create("c0",
Arrays.asList("ds0"), Collections.emptyList()));

execute(mycatConnection,
CreateClusterHint.create("c1",
Arrays.asList("ds1"), Collections.emptyList()));

execute(mycatConnection, "USE `cloud`;");

execute(mycatConnection,"CREATE TABLE IF NOT EXISTS `service` (\n" +
" `id` bigint(20) NOT NULL,\n" +
" `name` varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,\n" +
" PRIMARY KEY (`id`)\n" +
") ENGINE=InnoDB DEFAULT CHARSET=utf8;");

execute(mycatConnection,"CREATE TABLE IF NOT EXISTS `user` (\n" +
" `id` bigint(20) NOT NULL,\n" +
" `name` varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,\n" +
" PRIMARY KEY (`id`)\n" +
") ENGINE=InnoDB DEFAULT CHARSET=utf8;");

execute(mycatConnection,"CREATE TABLE cloud.log (\n" +
" `id` BIGINT(20) DEFAULT NULL,\n" +
" `user_id` BIGINT(20) DEFAULT NULL,\n" +
" `service_id` INT(11) DEFAULT NULL,\n" +
" `submit_time` DATETIME DEFAULT NULL\n" +
") ENGINE=INNODB DEFAULT CHARSET=utf8 dbpartition BY YYYYDD(submit_time) dbpartitions 2 tbpartition BY MOD_HASH (id) tbpartitions 8;\n");

deleteData(mycatConnection, "cloud", "service");
deleteData(mycatConnection, "cloud", "user");
deleteData(mycatConnection, "cloud", "log");

String sql1="SELECT log.id AS log_id,user.name AS user_name, service.name AS service_name,log.submit_time\n" +
"FROM\n" +
"`log` INNER JOIN `user`\n" +
"ON log.user_id = user.id\n" +
"INNER JOIN service\n" +
"ON user.id = service_id\n" +
"ORDER BY log.submit_time DESC LIMIT 0,20;";

String explain1 = explain(mycatConnection, sql1);
System.out.println(explain1);
executeQuery(mycatConnection,sql1);

Assert.assertTrue(explain1.contains("MycatView(distribution=[[cloud.log]], conditions=[=($3, CAST(?0):TIMESTAMP(0) NOT NULL)])"));

String sql2 = "SELECT log.id ,user.id,service.`id` FROM (SELECT log.`id` ,log.`service_id`,log.`submit_time`,log.`user_id` FROM LOG WHERE log.submit_time = '2021-5-31' ORDER BY log.submit_time DESC LIMIT 0,20) AS `log` INNER JOIN `user` ON log.user_id = user.id INNER JOIN `service` ON service.id = log.service_id ORDER BY log.submit_time DESC LIMIT 0,20;";

String explain2 = explain(mycatConnection, sql2);
System.out.println(explain2);
executeQuery(mycatConnection,sql2);
Assert.assertTrue(explain2.contains("WHERE (`submit_time` = ?) ORDER BY (`submit_time` IS NULL) DESC, `submit_time` DESC LIMIT 20 OFFSET 0)"));

}
}
}

0 comments on commit 6e06b00

Please sign in to comment.