Skip to content

Commit dce9698

Browse files
authored
Merge branch 'dev' into dev
2 parents 25c3df7 + 3843a50 commit dce9698

File tree

70 files changed

+1495
-590
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

70 files changed

+1495
-590
lines changed

sharding-core/src/main/java/io/shardingsphere/core/api/config/ProxyBasicRule.java renamed to sharding-core/src/main/java/io/shardingsphere/core/api/config/ProxySchemaRule.java

+2-4
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717

1818
package io.shardingsphere.core.api.config;
1919

20-
import io.shardingsphere.core.rule.ProxyAuthority;
2120
import io.shardingsphere.core.yaml.masterslave.YamlMasterSlaveRuleConfiguration;
2221
import io.shardingsphere.core.yaml.sharding.YamlShardingRuleConfiguration;
2322
import lombok.AllArgsConstructor;
@@ -26,19 +25,18 @@
2625
import lombok.Setter;
2726

2827
/**
29-
* Basic proxy rule.
28+
* schema sharding rule.
3029
*
3130
* @author panjuan
3231
*/
3332
@NoArgsConstructor
3433
@AllArgsConstructor
3534
@Getter
3635
@Setter
37-
public final class ProxyBasicRule {
36+
public final class ProxySchemaRule {
3837

3938
private YamlShardingRuleConfiguration shardingRule;
4039

4140
private YamlMasterSlaveRuleConfiguration masterSlaveRule;
4241

43-
private ProxyAuthority proxyAuthority = new ProxyAuthority();
4442
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Copyright 2016-2018 shardingsphere.io.
3+
* <p>
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
* </p>
16+
*/
17+
18+
package io.shardingsphere.core.api.config;
19+
20+
import io.shardingsphere.core.rule.ProxyAuthority;
21+
import lombok.AllArgsConstructor;
22+
import lombok.Getter;
23+
import lombok.NoArgsConstructor;
24+
import lombok.Setter;
25+
26+
import java.util.Properties;
27+
28+
/**
29+
* proxy server configuration.
30+
*
31+
* @author chenqingyang
32+
*/
33+
@NoArgsConstructor
34+
@AllArgsConstructor
35+
@Getter
36+
@Setter
37+
public final class ProxyServerConfiguration {
38+
39+
private ProxyAuthority proxyAuthority = new ProxyAuthority();
40+
41+
private Properties props = new Properties();
42+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/*
2+
* Copyright 2016-2018 shardingsphere.io.
3+
* <p>
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
* </p>
16+
*/
17+
18+
package io.shardingsphere.core.merger.dal.show;
19+
20+
import io.shardingsphere.core.merger.MergedResult;
21+
22+
import java.io.InputStream;
23+
import java.io.Reader;
24+
import java.sql.Blob;
25+
import java.sql.Clob;
26+
import java.sql.SQLException;
27+
import java.sql.SQLFeatureNotSupportedException;
28+
import java.sql.SQLXML;
29+
import java.util.Calendar;
30+
import java.util.List;
31+
32+
/**
33+
* Proxy merged result for show databases.
34+
*
35+
* @author chenqingyang
36+
*/
37+
public final class ProxyShowDatabasesMergedResult implements MergedResult {
38+
39+
private List<String> schemas;
40+
41+
private int currenIndex;
42+
43+
public ProxyShowDatabasesMergedResult(final List<String> schemas) {
44+
this.schemas = schemas;
45+
}
46+
47+
@Override
48+
public boolean next() throws SQLException {
49+
if (currenIndex < schemas.size()) {
50+
currenIndex++;
51+
return true;
52+
}
53+
return false;
54+
}
55+
56+
@Override
57+
public Object getValue(final int columnIndex, final Class<?> type) throws SQLException {
58+
if (Blob.class == type || Clob.class == type || Reader.class == type || InputStream.class == type || SQLXML.class == type) {
59+
throw new SQLFeatureNotSupportedException();
60+
}
61+
Object result = schemas.get(currenIndex - 1);
62+
return result;
63+
}
64+
65+
@Override
66+
public Object getValue(final String columnLabel, final Class<?> type) throws SQLException {
67+
return new SQLFeatureNotSupportedException();
68+
}
69+
70+
@Override
71+
public Object getCalendarValue(final int columnIndex, final Class<?> type, final Calendar calendar) throws SQLException {
72+
throw new SQLFeatureNotSupportedException();
73+
}
74+
75+
@Override
76+
public Object getCalendarValue(final String columnLabel, final Class<?> type, final Calendar calendar) throws SQLException {
77+
throw new SQLFeatureNotSupportedException();
78+
}
79+
80+
@Override
81+
public InputStream getInputStream(final int columnIndex, final String type) throws SQLException {
82+
throw new SQLFeatureNotSupportedException();
83+
}
84+
85+
@Override
86+
public InputStream getInputStream(final String columnLabel, final String type) throws SQLException {
87+
throw new SQLFeatureNotSupportedException();
88+
}
89+
90+
@Override
91+
public boolean wasNull() throws SQLException {
92+
return false;
93+
}
94+
}

sharding-core/src/main/java/io/shardingsphere/core/parsing/SQLJudgeEngine.java

+4-1
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,10 @@ private SQLStatement getTCLStatement() {
119119

120120
private SQLStatement getDALStatement(final TokenType tokenType, final LexerEngine lexerEngine) {
121121
if (DefaultKeyword.USE == tokenType) {
122-
return new UseStatement();
122+
UseStatement result = new UseStatement();
123+
lexerEngine.nextToken();
124+
result.setSchema(lexerEngine.getCurrentToken().getLiterals());
125+
return result;
123126
}
124127
if (DefaultKeyword.DESC == tokenType || MySQLKeyword.DESCRIBE == tokenType) {
125128
return new DescribeStatement();

sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/dialect/mysql/sql/MySQLUseParser.java

+11-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
package io.shardingsphere.core.parsing.parser.dialect.mysql.sql;
1919

20+
import io.shardingsphere.core.parsing.lexer.LexerEngine;
2021
import io.shardingsphere.core.parsing.parser.dialect.mysql.statement.UseStatement;
2122
import io.shardingsphere.core.parsing.parser.sql.dal.use.AbstractUseParser;
2223

@@ -27,8 +28,17 @@
2728
*/
2829
public final class MySQLUseParser extends AbstractUseParser {
2930

31+
private final LexerEngine lexerEngine;
32+
33+
public MySQLUseParser(final LexerEngine lexerEngine) {
34+
this.lexerEngine = lexerEngine;
35+
}
36+
3037
@Override
3138
public UseStatement parse() {
32-
return new UseStatement();
39+
UseStatement result = new UseStatement();
40+
lexerEngine.nextToken();
41+
result.setSchema(lexerEngine.getCurrentToken().getLiterals());
42+
return result;
3343
}
3444
}

sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/dialect/mysql/statement/UseStatement.java

+6
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,17 @@
1818
package io.shardingsphere.core.parsing.parser.dialect.mysql.statement;
1919

2020
import io.shardingsphere.core.parsing.parser.sql.dal.DALStatement;
21+
import lombok.Getter;
22+
import lombok.Setter;
2123

2224
/**
2325
* Use statement.
2426
*
2527
* @author zhangliang
2628
*/
2729
public final class UseStatement extends DALStatement {
30+
31+
@Getter
32+
@Setter
33+
private String schema;
2834
}

sharding-core/src/main/java/io/shardingsphere/core/parsing/parser/sql/dal/use/UseParserFactory.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public static AbstractUseParser newInstance(final DatabaseType dbType, final Sha
4444
switch (dbType) {
4545
case H2:
4646
case MySQL:
47-
return new MySQLUseParser();
47+
return new MySQLUseParser(lexerEngine);
4848
default:
4949
throw new UnsupportedOperationException(String.format("Cannot support database [%s].", dbType));
5050
}

sharding-core/src/main/java/io/shardingsphere/core/rule/ShardingRule.java

+10-10
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141

4242
/**
4343
* Databases and tables sharding rule configuration.
44-
*
44+
*
4545
* @author zhangliang
4646
* @author maxiaoguang
4747
* @author panjuan
@@ -92,7 +92,7 @@ public ShardingRule(final ShardingRuleConfiguration shardingRuleConfig, final Co
9292

9393
/**
9494
* Try to find table rule though logic table name.
95-
*
95+
*
9696
* @param logicTableName logic table name
9797
* @return table rule
9898
*/
@@ -139,11 +139,11 @@ public TableRule getTableRuleByLogicTableName(final String logicTableName) {
139139

140140
/**
141141
* Get database sharding strategy.
142-
*
142+
*
143143
* <p>
144144
* Use default database sharding strategy if not found.
145145
* </p>
146-
*
146+
*
147147
* @param tableRule table rule
148148
* @return database sharding strategy
149149
*/
@@ -153,11 +153,11 @@ public ShardingStrategy getDatabaseShardingStrategy(final TableRule tableRule) {
153153

154154
/**
155155
* Get table sharding strategy.
156-
*
156+
*
157157
* <p>
158158
* Use default table sharding strategy if not found.
159159
* </p>
160-
*
160+
*
161161
* @param tableRule table rule
162162
* @return table sharding strategy
163163
*/
@@ -250,7 +250,7 @@ public boolean isShardingColumn(final Column column) {
250250

251251
/**
252252
* get generated key's column.
253-
*
253+
*
254254
* @param logicTableName logic table name
255255
* @return generated key's column
256256
*/
@@ -297,7 +297,7 @@ public String getLogicTableName(final String logicIndexName) {
297297

298298
/**
299299
* Find data node by logic table.
300-
*
300+
*
301301
* @param logicTableName logic table name
302302
* @return data node
303303
*/
@@ -339,9 +339,9 @@ public boolean isLogicIndex(final String logicIndexName, final String logicTable
339339

340340
/**
341341
* Find actual default data source name.
342-
*
342+
*
343343
* <p>If use master-slave rule, return master data source name.</p>
344-
*
344+
*
345345
* @return actual default data source name
346346
*/
347347
public Optional<String> findActualDefaultDataSourceName() {

sharding-core/src/test/java/io/shardingsphere/core/parsing/SQLJudgeEngineTest.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import io.shardingsphere.core.parsing.parser.dialect.mysql.statement.ShowTablesStatement;
2525
import io.shardingsphere.core.parsing.parser.dialect.mysql.statement.UseStatement;
2626
import io.shardingsphere.core.parsing.parser.exception.SQLParsingException;
27+
import io.shardingsphere.core.parsing.parser.sql.SQLStatement;
2728
import io.shardingsphere.core.parsing.parser.sql.dml.DMLStatement;
2829
import io.shardingsphere.core.parsing.parser.sql.dml.insert.InsertStatement;
2930
import io.shardingsphere.core.parsing.parser.sql.dql.DQLStatement;
@@ -32,6 +33,7 @@
3233

3334
import static org.hamcrest.CoreMatchers.instanceOf;
3435
import static org.junit.Assert.assertThat;
36+
import static org.hamcrest.CoreMatchers.is;
3537

3638
public final class SQLJudgeEngineTest {
3739

@@ -82,7 +84,9 @@ public void assertJudgeForBegin() {
8284

8385
@Test
8486
public void assertJudgeForUse() {
85-
assertThat(new SQLJudgeEngine(" /*+ HINT SELECT * FROM TT*/ \t \n \r \fuse sharding_db ").judge(), instanceOf(UseStatement.class));
87+
SQLStatement statement = new SQLJudgeEngine(" /*+ HINT SELECT * FROM TT*/ \t \n \r \fuse sharding_db ").judge();
88+
assertThat(statement, instanceOf(UseStatement.class));
89+
assertThat(((UseStatement) statement).getSchema(), is("sharding_db"));
8690
}
8791

8892
@Test

sharding-jdbc/src/main/java/io/shardingsphere/core/jdbc/adapter/AbstractConnectionAdapter.java

+13-11
Original file line numberDiff line numberDiff line change
@@ -168,18 +168,20 @@ public final void close() throws SQLException {
168168
@Override
169169
public void execute(final Map.Entry<String, Connection> cachedConnectionsEntrySet) throws SQLException {
170170
Connection connection = cachedConnectionsEntrySet.getValue();
171-
ConnectionCloseEvent event = new ConnectionCloseEvent(cachedConnectionsEntrySet.getKey(), connection.getMetaData().getURL());
172-
ShardingEventBusInstance.getInstance().post(event);
173-
try {
174-
connection.close();
175-
event.setExecuteSuccess();
176-
// CHECKSTYLE:OFF
177-
} catch (final Exception ex) {
178-
// CHECKSTYLE:ON
179-
event.setExecuteFailure(ex);
180-
throw ex;
181-
} finally {
171+
if (!connection.isClosed()) {
172+
ConnectionCloseEvent event = new ConnectionCloseEvent(cachedConnectionsEntrySet.getKey(), connection.getMetaData().getURL());
182173
ShardingEventBusInstance.getInstance().post(event);
174+
try {
175+
connection.close();
176+
event.setExecuteSuccess();
177+
// CHECKSTYLE:OFF
178+
} catch (final Exception ex) {
179+
// CHECKSTYLE:ON
180+
event.setExecuteFailure(ex);
181+
throw ex;
182+
} finally {
183+
ShardingEventBusInstance.getInstance().post(event);
184+
}
183185
}
184186
}
185187
});

0 commit comments

Comments
 (0)