Skip to content

Commit

Permalink
Merge branch 'dev' of ssh://github.com/shardingjdbc/sharding-jdbc int…
Browse files Browse the repository at this point in the history
…o dev
  • Loading branch information
tristaZero committed Jul 18, 2018
2 parents 102aa94 + 5e40cc7 commit 1517b35
Show file tree
Hide file tree
Showing 17 changed files with 228 additions and 112 deletions.
1 change: 1 addition & 0 deletions RELEASE-NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
1. [ISSUE #806](https://github.com/sharding-sphere/sharding-sphere/issues/806) SQL parse error with `NOT IN`
1. [ISSUE #827](https://github.com/sharding-sphere/sharding-sphere/issues/827) Fix endless loop for bad SQL like `SELECT * FROM table WHERE id IN ()`
1. [ISSUE #919](https://github.com/sharding-sphere/sharding-sphere/issues/919) Use groovy to parse inline expression may cause memory leak
1. [ISSUE #1011](https://github.com/sharding-sphere/sharding-sphere/issues/1011) Can't resolve placeholder in spring boot configuration yaml
1. [ISSUE #1015](https://github.com/sharding-sphere/sharding-sphere/issues/1015) Support the statement of `SELECT id, COUNT(*) FROM table GROUP BY 1,2`

## 2.0.3
Expand Down
1 change: 1 addition & 0 deletions RELEASE-NOTES_ZH.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
1. [ISSUE #806](https://github.com/sharding-sphere/sharding-sphere/issues/806) `NOT IN`解析异常
1. [ISSUE #827](https://github.com/sharding-sphere/sharding-sphere/issues/827)`SELECT * FROM table WHERE id IN ()`这种SQL跳出死循环
1. [ISSUE #919](https://github.com/sharding-sphere/sharding-sphere/issues/919) 使用Groovy解析行表达式可能导致内存泄漏
1. [ISSUE #1011](https://github.com/sharding-sphere/sharding-sphere/issues/1011) 无法在Spring Boot的yaml中处理占位符
1. [ISSUE #1015](https://github.com/sharding-sphere/sharding-sphere/issues/1015) 支持使用`SELECT id, COUNT(*) FROM table GROUP BY 1,2`

## 2.0.3
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,12 +121,12 @@ public void assertRewriteForTableName() {
public void assertRewriteForOrderByAndGroupByDerivedColumns() {
selectStatement.getSqlTokens().add(new TableToken(18, 0, "table_x"));
ItemsToken itemsToken = new ItemsToken(12);
itemsToken.getItems().addAll(Arrays.asList("x.id as ORDER_BY_DERIVED_0", "x.name as GROUP_BY_DERIVED_0"));
itemsToken.getItems().addAll(Arrays.asList("x.id as GROUP_BY_DERIVED_0", "x.name as ORDER_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, null, Collections.emptyList());
assertThat(rewriteEngine.rewrite(true).toSQL(null, tableTokens, null, shardingDataSourceMetaData).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"));
"SELECT x.age, x.id as GROUP_BY_DERIVED_0, x.name as ORDER_BY_DERIVED_0 FROM table_1 x GROUP BY x.id ORDER BY x.name"));
}

@Test
Expand All @@ -153,9 +153,9 @@ public void assertRewriteForAutoGeneratedKeyColumn() {
insertStatement.getSqlTokens().add(itemsToken);
insertStatement.getSqlTokens().add(new InsertValuesToken(39, "table_x"));
InsertShardingCondition shardingCondition = new InsertShardingCondition("(?, ?, ?)", parameters);
shardingCondition.getDataNodes().add(new DataNode("db0.table_x"));
shardingCondition.getDataNodes().add(new DataNode("db0.table_1"));
TableUnit tableUnit = new TableUnit("db0");
tableUnit.getRoutingTables().add(new RoutingTable("table_x", "table_x"));
tableUnit.getRoutingTables().add(new RoutingTable("table_x", "table_1"));
SQLRewriteEngine rewriteEngine = new SQLRewriteEngine(shardingRule, "INSERT INTO table_x (name, age) VALUES (?, ?)",
DatabaseType.MySQL, insertStatement, new ShardingConditions(Collections.<ShardingCondition>singletonList(shardingCondition)), parameters);
assertThat(rewriteEngine.rewrite(true).toSQL(tableUnit, tableTokens, null, shardingDataSourceMetaData).getSql(), is("INSERT INTO table_1 (name, age, id) VALUES (?, ?, ?)"));
Expand All @@ -178,9 +178,9 @@ public void assertRewriteForAutoGeneratedKeyColumnWithoutColumnsWithParameter()
insertStatement.getSqlTokens().add(new InsertColumnToken(21, ")"));
insertStatement.getSqlTokens().add(new InsertValuesToken(29, "table_x"));
InsertShardingCondition shardingCondition = new InsertShardingCondition("(?, ?)", parameters);
shardingCondition.getDataNodes().add(new DataNode("db0.table_x"));
shardingCondition.getDataNodes().add(new DataNode("db0.table_1"));
TableUnit tableUnit = new TableUnit("db0");
tableUnit.getRoutingTables().add(new RoutingTable("table_x", "table_x"));
tableUnit.getRoutingTables().add(new RoutingTable("table_x", "table_1"));
SQLRewriteEngine rewriteEngine = new SQLRewriteEngine(shardingRule,
"INSERT INTO `table_x` VALUES (?)", DatabaseType.MySQL, insertStatement, new ShardingConditions(Collections.<ShardingCondition>singletonList(shardingCondition)), parameters);
assertThat(rewriteEngine.rewrite(true).toSQL(tableUnit, tableTokens, null, shardingDataSourceMetaData).getSql(), is("INSERT INTO table_1(name, id) VALUES (?, ?)"));
Expand All @@ -200,9 +200,9 @@ public void assertRewriteForAutoGeneratedKeyColumnWithoutColumnsWithoutParameter
insertStatement.getSqlTokens().add(new InsertColumnToken(21, ")"));
insertStatement.getSqlTokens().add(new InsertValuesToken(29, "table_x"));
InsertShardingCondition shardingCondition = new InsertShardingCondition("(10, 1)", Collections.emptyList());
shardingCondition.getDataNodes().add(new DataNode("db0.table_x"));
shardingCondition.getDataNodes().add(new DataNode("db0.table_1"));
TableUnit tableUnit = new TableUnit("db0");
tableUnit.getRoutingTables().add(new RoutingTable("table_x", "table_x"));
tableUnit.getRoutingTables().add(new RoutingTable("table_x", "table_1"));
SQLRewriteEngine rewriteEngine = new SQLRewriteEngine(shardingRule, "INSERT INTO `table_x` VALUES (10)",
DatabaseType.MySQL, insertStatement, new ShardingConditions(Collections.<ShardingCondition>singletonList(shardingCondition)), Collections.emptyList());
assertThat(rewriteEngine.rewrite(true).toSQL(tableUnit, tableTokens, null, shardingDataSourceMetaData).getSql(), is("INSERT INTO table_1(name, id) VALUES (10, 1)"));
Expand All @@ -225,9 +225,9 @@ public void assertRewriteColumnWithoutColumnsWithoutParameter() {
insertStatement.getSqlTokens().add(new InsertColumnToken(21, ")"));
insertStatement.getSqlTokens().add(new InsertValuesToken(29, "table_x"));
InsertShardingCondition shardingCondition = new InsertShardingCondition("(10, 1)", parameters);
shardingCondition.getDataNodes().add(new DataNode("db0.table_x"));
shardingCondition.getDataNodes().add(new DataNode("db0.table_1"));
TableUnit tableUnit = new TableUnit("db0");
tableUnit.getRoutingTables().add(new RoutingTable("table_x", "table_x"));
tableUnit.getRoutingTables().add(new RoutingTable("table_x", "table_1"));
SQLRewriteEngine rewriteEngine = new SQLRewriteEngine(shardingRule,
"INSERT INTO `table_x` VALUES (10, 1)", DatabaseType.MySQL, insertStatement, new ShardingConditions(Collections.<ShardingCondition>singletonList(shardingCondition)), parameters);
assertThat(rewriteEngine.rewrite(true).toSQL(tableUnit, tableTokens, null, shardingDataSourceMetaData).getSql(), is("INSERT INTO table_1(name, id) VALUES (10, 1)"));
Expand All @@ -250,9 +250,9 @@ public void assertRewriteColumnWithoutColumnsWithParameter() {
insertStatement.getSqlTokens().add(new InsertColumnToken(21, ")"));
insertStatement.getSqlTokens().add(new InsertValuesToken(29, "table_x"));
InsertShardingCondition shardingCondition = new InsertShardingCondition("(?, ?)", parameters);
shardingCondition.getDataNodes().add(new DataNode("db0.table_x"));
shardingCondition.getDataNodes().add(new DataNode("db0.table_1"));
TableUnit tableUnit = new TableUnit("db0");
tableUnit.getRoutingTables().add(new RoutingTable("table_x", "table_x"));
tableUnit.getRoutingTables().add(new RoutingTable("table_x", "table_1"));
SQLRewriteEngine rewriteEngine = new SQLRewriteEngine(shardingRule,
"INSERT INTO `table_x` VALUES (?, ?)", DatabaseType.MySQL, insertStatement, new ShardingConditions(Collections.<ShardingCondition>singletonList(shardingCondition)), parameters);
assertThat(rewriteEngine.rewrite(true).toSQL(tableUnit, tableTokens, null, shardingDataSourceMetaData).getSql(), is("INSERT INTO table_1(name, id) VALUES (?, ?)"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.config.PlaceholderConfigurerSupport;
import org.springframework.core.env.Environment;
import org.springframework.core.env.PropertyResolver;

Expand Down Expand Up @@ -55,15 +59,30 @@ public static <T> T handle(final Environment environment, final String prefix, f
return (T) v2(environment, prefix, targetClass);
}
}


@SuppressWarnings("unchecked")
private static Object v1(final Environment environment, final String prefix) {
try {
Class<?> resolverClass = Class.forName("org.springframework.boot.bind.RelaxedPropertyResolver");
Constructor<?> resolverConstructor = resolverClass.getDeclaredConstructor(PropertyResolver.class);
Method getSubPropertiesMethod = resolverClass.getDeclaredMethod("getSubProperties", String.class);
Object resolverObject = resolverConstructor.newInstance(environment);
String prefixParam = prefix.endsWith(".") ? prefix : prefix + ".";
return getSubPropertiesMethod.invoke(resolverObject, prefixParam);
Method getPropertyMethod = resolverClass.getDeclaredMethod("getProperty", String.class);
Map<String, Object> dataSourceProps = (Map<String, Object>) getSubPropertiesMethod.invoke(resolverObject, prefixParam);
Map<String, Object> propertiesWithPlaceholderResolved = new HashMap<>();
for (Map.Entry<String, Object> entry : dataSourceProps.entrySet()) {
String key = entry.getKey();
Object value = entry.getValue();
if (value instanceof String && ((String) value).contains(
PlaceholderConfigurerSupport.DEFAULT_PLACEHOLDER_PREFIX)) {
String resolvedValue = (String) getPropertyMethod.invoke(resolverObject, prefixParam + key);
propertiesWithPlaceholderResolved.put(key, resolvedValue);
} else {
propertiesWithPlaceholderResolved.put(key, value);
}
}
return Collections.unmodifiableMap(propertiesWithPlaceholderResolved);
} catch (final ClassNotFoundException | NoSuchMethodException | SecurityException | InstantiationException
| IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) {
throw new ShardingException(ex.getMessage(), ex);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,12 @@ public void assertWithMasterSlaveDataSource() {
assertTrue(dataSource instanceof MasterSlaveDataSource);
for (DataSource each : ((MasterSlaveDataSource) dataSource).getAllDataSources().values()) {
assertThat(((BasicDataSource) each).getMaxTotal(), is(16));
assertThat(((BasicDataSource) each).getUsername(), is("root"));
}
Map<String, Object> configMap = new ConcurrentHashMap<>();
configMap.put("key1", "value1");
configMap.put("key2", "value1");
configMap.put("username", "root");
assertThat(ConfigMapContext.getInstance().getMasterSlaveConfig(), is(configMap));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ sharding.jdbc.datasource.ds_master.max-total=16
sharding.jdbc.datasource.ds_slave_0.type=org.apache.commons.dbcp2.BasicDataSource
sharding.jdbc.datasource.ds_slave_0.driver-class-name=org.h2.Driver
sharding.jdbc.datasource.ds_slave_0.url=jdbc:h2:mem:demo_ds_slave_0;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false;MODE=MYSQL
sharding.jdbc.datasource.ds_slave_0.username=root
sharding.jdbc.datasource.ds_slave_0.username=${sharding.jdbc.config.masterslave.config-map.username}
sharding.jdbc.datasource.ds_slave_0.password=
sharding.jdbc.datasource.ds_slave_0.max-total=16

Expand All @@ -32,3 +32,5 @@ sharding.jdbc.config.orchestration.zookeeper.namespace=orchestration-spring-boot
sharding.jdbc.config.orchestration.zookeeper.server-lists=localhost:3181

sharding.jdbc.config.masterslave.config-map.key1=value1
sharding.jdbc.config.masterslave.config-map.key2=${sharding.jdbc.config.masterslave.config-map.key1}
sharding.jdbc.config.masterslave.config-map.username=root
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.config.PlaceholderConfigurerSupport;
import org.springframework.core.env.Environment;
import org.springframework.core.env.PropertyResolver;

Expand Down Expand Up @@ -55,15 +59,30 @@ public static <T> T handle(final Environment environment, final String prefix, f
return (T) v2(environment, prefix, targetClass);
}
}


@SuppressWarnings("unchecked")
private static Object v1(final Environment environment, final String prefix) {
try {
Class<?> resolverClass = Class.forName("org.springframework.boot.bind.RelaxedPropertyResolver");
Constructor<?> resolverConstructor = resolverClass.getDeclaredConstructor(PropertyResolver.class);
Method getSubPropertiesMethod = resolverClass.getDeclaredMethod("getSubProperties", String.class);
Object resolverObject = resolverConstructor.newInstance(environment);
String prefixParam = prefix.endsWith(".") ? prefix : prefix + ".";
return getSubPropertiesMethod.invoke(resolverObject, prefixParam);
Method getPropertyMethod = resolverClass.getDeclaredMethod("getProperty", String.class);
Map<String, Object> dataSourceProps = (Map<String, Object>) getSubPropertiesMethod.invoke(resolverObject, prefixParam);
Map<String, Object> propertiesWithPlaceholderResolved = new HashMap<>();
for (Map.Entry<String, Object> entry : dataSourceProps.entrySet()) {
String key = entry.getKey();
Object value = entry.getValue();
if (value instanceof String && ((String) value).contains(
PlaceholderConfigurerSupport.DEFAULT_PLACEHOLDER_PREFIX)) {
String resolvedValue = (String) getPropertyMethod.invoke(resolverObject, prefixParam + key);
propertiesWithPlaceholderResolved.put(key, resolvedValue);
} else {
propertiesWithPlaceholderResolved.put(key, value);
}
}
return Collections.unmodifiableMap(propertiesWithPlaceholderResolved);
} catch (final ClassNotFoundException | NoSuchMethodException | SecurityException | InstantiationException
| IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) {
throw new ShardingException(ex.getMessage(), ex);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ public void assertWithMasterSlaveDataSource() {
}
Map<String, Object> configMap = new ConcurrentHashMap<>();
configMap.put("key1", "value1");
configMap.put("key2", "value1");
configMap.put("username", "root");
assertThat(ConfigMapContext.getInstance().getMasterSlaveConfig(), is(configMap));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ sharding.jdbc.datasource.ds_master.max-total=16
sharding.jdbc.datasource.ds_slave_0.type=org.apache.commons.dbcp2.BasicDataSource
sharding.jdbc.datasource.ds_slave_0.driver-class-name=org.h2.Driver
sharding.jdbc.datasource.ds_slave_0.url=jdbc:h2:mem:demo_ds_slave_0;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false;MODE=MYSQL
sharding.jdbc.datasource.ds_slave_0.username=root
sharding.jdbc.datasource.ds_slave_0.username=${sharding.jdbc.config.masterslave.config-map.username}
sharding.jdbc.datasource.ds_slave_0.password=
sharding.jdbc.datasource.ds_slave_0.max-total=16

Expand All @@ -26,3 +26,5 @@ sharding.jdbc.config.masterslave.master-data-source-name=ds_master
sharding.jdbc.config.masterslave.slave-data-source-names=ds_slave_0,ds_slave_1

sharding.jdbc.config.masterslave.config-map.key1=value1
sharding.jdbc.config.masterslave.config-map.key2=${sharding.jdbc.config.masterslave.config-map.key1}
sharding.jdbc.config.masterslave.config-map.username=root
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,14 @@
import io.shardingsphere.core.routing.router.masterslave.MasterVisitedManager;
import io.shardingsphere.proxy.backend.common.ProxyMode;
import io.shardingsphere.proxy.config.RuleRegistry;
import lombok.Getter;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CopyOnWriteArrayList;

/**
* Connection manager.
Expand All @@ -44,9 +40,6 @@ public final class ConnectionManager implements AutoCloseable {

private final Collection<Connection> cachedConnections = new LinkedList<>();

@Getter
private final List<ResultSet> resultSets = new CopyOnWriteArrayList<>();

/**
* Get connection of current thread datasource.
*
Expand Down
Loading

0 comments on commit 1517b35

Please sign in to comment.