Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
apache
Shardingsphere
提交
2d19ab16
Shardingsphere
项目概览
apache
/
Shardingsphere
通知
56
Star
3
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
Shardingsphere
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
未验证
提交
2d19ab16
编写于
9月 17, 2019
作者:
ShardingSphere
提交者:
GitHub
9月 17, 2019
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Fixes #2432 (#3050)
* fix #2432
上级
ec22ada5
变更
18
隐藏空白更改
内联
并排
Showing
18 changed file
with
35 addition
and
268 deletion
+35
-268
sharding-core/sharding-core-entry/src/main/java/org/apache/shardingsphere/core/PreparedQueryShardingEngine.java
...ache/shardingsphere/core/PreparedQueryShardingEngine.java
+3
-4
sharding-core/sharding-core-entry/src/main/java/org/apache/shardingsphere/core/SimpleQueryShardingEngine.java
...apache/shardingsphere/core/SimpleQueryShardingEngine.java
+2
-4
sharding-core/sharding-core-entry/src/test/java/org/apache/shardingsphere/core/PreparedQueryShardingEngineTest.java
.../shardingsphere/core/PreparedQueryShardingEngineTest.java
+1
-3
sharding-core/sharding-core-entry/src/test/java/org/apache/shardingsphere/core/SimpleQueryShardingEngineTest.java
...he/shardingsphere/core/SimpleQueryShardingEngineTest.java
+1
-3
sharding-core/sharding-core-route/src/main/java/org/apache/shardingsphere/core/route/PreparedStatementRoutingEngine.java
...dingsphere/core/route/PreparedStatementRoutingEngine.java
+3
-5
sharding-core/sharding-core-route/src/main/java/org/apache/shardingsphere/core/route/StatementRoutingEngine.java
...che/shardingsphere/core/route/StatementRoutingEngine.java
+3
-4
sharding-core/sharding-core-route/src/main/java/org/apache/shardingsphere/core/route/router/sharding/DatabaseHintSQLRouter.java
...ere/core/route/router/sharding/DatabaseHintSQLRouter.java
+0
-63
sharding-core/sharding-core-route/src/main/java/org/apache/shardingsphere/core/route/router/sharding/ShardingRouterFactory.java
...ere/core/route/router/sharding/ShardingRouterFactory.java
+0
-51
sharding-core/sharding-core-route/src/test/java/org/apache/shardingsphere/core/route/DatabaseTest.java
...va/org/apache/shardingsphere/core/route/DatabaseTest.java
+5
-31
sharding-core/sharding-core-route/src/test/java/org/apache/shardingsphere/core/route/router/DatabaseHintSQLRouterTest.java
...ngsphere/core/route/router/DatabaseHintSQLRouterTest.java
+0
-71
sharding-core/sharding-core-route/src/test/java/org/apache/shardingsphere/core/route/type/standard/AbstractSQLRouteTest.java
...sphere/core/route/type/standard/AbstractSQLRouteTest.java
+1
-1
sharding-jdbc/sharding-jdbc-core/src/main/java/org/apache/shardingsphere/shardingjdbc/jdbc/core/statement/ShardingPreparedStatement.java
...ngjdbc/jdbc/core/statement/ShardingPreparedStatement.java
+1
-3
sharding-jdbc/sharding-jdbc-core/src/main/java/org/apache/shardingsphere/shardingjdbc/jdbc/core/statement/ShardingStatement.java
...e/shardingjdbc/jdbc/core/statement/ShardingStatement.java
+1
-3
sharding-proxy/sharding-proxy-backend/src/main/java/org/apache/shardingsphere/shardingproxy/backend/communication/jdbc/JDBCDatabaseCommunicationEngine.java
...d/communication/jdbc/JDBCDatabaseCommunicationEngine.java
+2
-5
sharding-proxy/sharding-proxy-backend/src/main/java/org/apache/shardingsphere/shardingproxy/backend/communication/jdbc/wrapper/JDBCExecutorWrapper.java
...ckend/communication/jdbc/wrapper/JDBCExecutorWrapper.java
+1
-3
sharding-proxy/sharding-proxy-backend/src/main/java/org/apache/shardingsphere/shardingproxy/backend/communication/jdbc/wrapper/PreparedStatementExecutorWrapper.java
...cation/jdbc/wrapper/PreparedStatementExecutorWrapper.java
+5
-6
sharding-proxy/sharding-proxy-backend/src/main/java/org/apache/shardingsphere/shardingproxy/backend/communication/jdbc/wrapper/StatementExecutorWrapper.java
.../communication/jdbc/wrapper/StatementExecutorWrapper.java
+5
-6
sharding-proxy/sharding-proxy-backend/src/main/java/org/apache/shardingsphere/shardingproxy/backend/text/sctl/explain/ShardingCTLExplainBackendHandler.java
...d/text/sctl/explain/ShardingCTLExplainBackendHandler.java
+1
-2
未找到文件。
sharding-core/sharding-core-entry/src/main/java/org/apache/shardingsphere/core/PreparedQueryShardingEngine.java
浏览文件 @
2d19ab16
...
...
@@ -23,7 +23,6 @@ import org.apache.shardingsphere.core.parse.SQLParseEngine;
import
org.apache.shardingsphere.core.route.PreparedStatementRoutingEngine
;
import
org.apache.shardingsphere.core.route.SQLRouteResult
;
import
org.apache.shardingsphere.core.rule.ShardingRule
;
import
org.apache.shardingsphere.spi.database.DatabaseType
;
import
java.util.ArrayList
;
import
java.util.List
;
...
...
@@ -44,10 +43,10 @@ public final class PreparedQueryShardingEngine extends BaseShardingEngine {
private
final
PreparedStatementRoutingEngine
routingEngine
;
public
PreparedQueryShardingEngine
(
final
String
sql
,
final
ShardingRule
shardingRule
,
final
ShardingProperties
shardingProperties
,
final
Sharding
SphereMetaData
metaData
,
final
DatabaseType
databaseType
,
final
SQLParseEngine
sqlParseEngine
)
{
public
PreparedQueryShardingEngine
(
final
String
sql
,
final
Sharding
Rule
shardingRule
,
final
ShardingProperties
shardingProperties
,
final
ShardingSphereMetaData
metaData
,
final
SQLParseEngine
sqlParseEngine
)
{
super
(
shardingRule
,
shardingProperties
,
metaData
);
routingEngine
=
new
PreparedStatementRoutingEngine
(
sql
,
shardingRule
,
metaData
,
databaseType
,
sqlParseEngine
);
routingEngine
=
new
PreparedStatementRoutingEngine
(
sql
,
shardingRule
,
metaData
,
sqlParseEngine
);
}
@Override
...
...
sharding-core/sharding-core-entry/src/main/java/org/apache/shardingsphere/core/SimpleQueryShardingEngine.java
浏览文件 @
2d19ab16
...
...
@@ -23,7 +23,6 @@ import org.apache.shardingsphere.core.parse.SQLParseEngine;
import
org.apache.shardingsphere.core.route.SQLRouteResult
;
import
org.apache.shardingsphere.core.route.StatementRoutingEngine
;
import
org.apache.shardingsphere.core.rule.ShardingRule
;
import
org.apache.shardingsphere.spi.database.DatabaseType
;
import
java.util.Collections
;
import
java.util.List
;
...
...
@@ -44,10 +43,9 @@ public final class SimpleQueryShardingEngine extends BaseShardingEngine {
private
final
StatementRoutingEngine
routingEngine
;
public
SimpleQueryShardingEngine
(
final
ShardingRule
shardingRule
,
final
ShardingProperties
shardingProperties
,
final
ShardingSphereMetaData
metaData
,
final
DatabaseType
databaseType
,
final
SQLParseEngine
sqlParseEngine
)
{
public
SimpleQueryShardingEngine
(
final
ShardingRule
shardingRule
,
final
ShardingProperties
shardingProperties
,
final
ShardingSphereMetaData
metaData
,
final
SQLParseEngine
sqlParseEngine
)
{
super
(
shardingRule
,
shardingProperties
,
metaData
);
routingEngine
=
new
StatementRoutingEngine
(
shardingRule
,
metaData
,
databaseType
,
sqlParseEngine
);
routingEngine
=
new
StatementRoutingEngine
(
shardingRule
,
metaData
,
sqlParseEngine
);
}
@Override
...
...
sharding-core/sharding-core-entry/src/test/java/org/apache/shardingsphere/core/PreparedQueryShardingEngineTest.java
浏览文件 @
2d19ab16
...
...
@@ -18,7 +18,6 @@
package
org.apache.shardingsphere.core
;
import
lombok.SneakyThrows
;
import
org.apache.shardingsphere.core.database.DatabaseTypes
;
import
org.apache.shardingsphere.core.metadata.ShardingSphereMetaData
;
import
org.apache.shardingsphere.core.parse.SQLParseEngine
;
import
org.apache.shardingsphere.core.route.PreparedStatementRoutingEngine
;
...
...
@@ -54,8 +53,7 @@ public final class PreparedQueryShardingEngineTest extends BaseShardingEngineTes
ShardingRule
shardingRule
=
mock
(
ShardingRule
.
class
);
EncryptRule
encryptRule
=
mock
(
EncryptRule
.
class
);
when
(
shardingRule
.
getEncryptRule
()).
thenReturn
(
encryptRule
);
shardingEngine
=
new
PreparedQueryShardingEngine
(
getSql
(),
shardingRule
,
getShardingProperties
(),
mock
(
ShardingSphereMetaData
.
class
),
DatabaseTypes
.
getActualDatabaseType
(
"MySQL"
),
mock
(
SQLParseEngine
.
class
));
shardingEngine
=
new
PreparedQueryShardingEngine
(
getSql
(),
shardingRule
,
getShardingProperties
(),
mock
(
ShardingSphereMetaData
.
class
),
mock
(
SQLParseEngine
.
class
));
setRoutingEngine
();
}
...
...
sharding-core/sharding-core-entry/src/test/java/org/apache/shardingsphere/core/SimpleQueryShardingEngineTest.java
浏览文件 @
2d19ab16
...
...
@@ -18,7 +18,6 @@
package
org.apache.shardingsphere.core
;
import
lombok.SneakyThrows
;
import
org.apache.shardingsphere.core.database.DatabaseTypes
;
import
org.apache.shardingsphere.core.metadata.ShardingSphereMetaData
;
import
org.apache.shardingsphere.core.parse.SQLParseEngine
;
import
org.apache.shardingsphere.core.route.StatementRoutingEngine
;
...
...
@@ -52,8 +51,7 @@ public final class SimpleQueryShardingEngineTest extends BaseShardingEngineTest
ShardingRule
shardingRule
=
mock
(
ShardingRule
.
class
);
EncryptRule
encryptRule
=
mock
(
EncryptRule
.
class
);
when
(
shardingRule
.
getEncryptRule
()).
thenReturn
(
encryptRule
);
shardingEngine
=
new
SimpleQueryShardingEngine
(
shardingRule
,
getShardingProperties
(),
mock
(
ShardingSphereMetaData
.
class
),
DatabaseTypes
.
getActualDatabaseType
(
"MySQL"
),
mock
(
SQLParseEngine
.
class
));
shardingEngine
=
new
SimpleQueryShardingEngine
(
shardingRule
,
getShardingProperties
(),
mock
(
ShardingSphereMetaData
.
class
),
mock
(
SQLParseEngine
.
class
));
setRoutingEngine
();
}
...
...
sharding-core/sharding-core-route/src/main/java/org/apache/shardingsphere/core/route/PreparedStatementRoutingEngine.java
浏览文件 @
2d19ab16
...
...
@@ -21,10 +21,9 @@ import org.apache.shardingsphere.core.metadata.ShardingSphereMetaData;
import
org.apache.shardingsphere.core.parse.SQLParseEngine
;
import
org.apache.shardingsphere.core.parse.sql.statement.SQLStatement
;
import
org.apache.shardingsphere.core.route.router.masterslave.ShardingMasterSlaveRouter
;
import
org.apache.shardingsphere.core.route.router.sharding.ParsingSQLRouter
;
import
org.apache.shardingsphere.core.route.router.sharding.ShardingRouter
;
import
org.apache.shardingsphere.core.route.router.sharding.ShardingRouterFactory
;
import
org.apache.shardingsphere.core.rule.ShardingRule
;
import
org.apache.shardingsphere.spi.database.DatabaseType
;
import
java.util.List
;
...
...
@@ -44,10 +43,9 @@ public final class PreparedStatementRoutingEngine {
private
SQLStatement
sqlStatement
;
public
PreparedStatementRoutingEngine
(
final
String
logicSQL
,
final
ShardingRule
shardingRule
,
final
ShardingSphereMetaData
metaData
,
final
DatabaseType
databaseType
,
final
SQLParseEngine
sqlParseEngine
)
{
public
PreparedStatementRoutingEngine
(
final
String
logicSQL
,
final
ShardingRule
shardingRule
,
final
ShardingSphereMetaData
metaData
,
final
SQLParseEngine
sqlParseEngine
)
{
this
.
logicSQL
=
logicSQL
;
shardingRouter
=
ShardingRouterFactory
.
newInstance
(
shardingRule
,
metaData
,
databaseType
,
sqlParseEngine
);
shardingRouter
=
new
ParsingSQLRouter
(
shardingRule
,
metaData
,
sqlParseEngine
);
masterSlaveRouter
=
new
ShardingMasterSlaveRouter
(
shardingRule
.
getMasterSlaveRules
());
}
...
...
sharding-core/sharding-core-route/src/main/java/org/apache/shardingsphere/core/route/StatementRoutingEngine.java
浏览文件 @
2d19ab16
...
...
@@ -21,10 +21,9 @@ import org.apache.shardingsphere.core.metadata.ShardingSphereMetaData;
import
org.apache.shardingsphere.core.parse.SQLParseEngine
;
import
org.apache.shardingsphere.core.parse.sql.statement.SQLStatement
;
import
org.apache.shardingsphere.core.route.router.masterslave.ShardingMasterSlaveRouter
;
import
org.apache.shardingsphere.core.route.router.sharding.ParsingSQLRouter
;
import
org.apache.shardingsphere.core.route.router.sharding.ShardingRouter
;
import
org.apache.shardingsphere.core.route.router.sharding.ShardingRouterFactory
;
import
org.apache.shardingsphere.core.rule.ShardingRule
;
import
org.apache.shardingsphere.spi.database.DatabaseType
;
import
java.util.Collections
;
...
...
@@ -40,8 +39,8 @@ public final class StatementRoutingEngine {
private
final
ShardingMasterSlaveRouter
masterSlaveRouter
;
public
StatementRoutingEngine
(
final
ShardingRule
shardingRule
,
final
ShardingSphereMetaData
metaData
,
final
DatabaseType
databaseType
,
final
SQLParseEngine
sqlParseEngine
)
{
shardingRouter
=
ShardingRouterFactory
.
newInstance
(
shardingRule
,
metaData
,
databaseType
,
sqlParseEngine
);
public
StatementRoutingEngine
(
final
ShardingRule
shardingRule
,
final
ShardingSphereMetaData
metaData
,
final
SQLParseEngine
sqlParseEngine
)
{
shardingRouter
=
new
ParsingSQLRouter
(
shardingRule
,
metaData
,
sqlParseEngine
);
masterSlaveRouter
=
new
ShardingMasterSlaveRouter
(
shardingRule
.
getMasterSlaveRules
());
}
...
...
sharding-core/sharding-core-route/src/main/java/org/apache/shardingsphere/core/route/router/sharding/DatabaseHintSQLRouter.java
已删除
100644 → 0
浏览文件 @
ec22ada5
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package
org.apache.shardingsphere.core.route.router.sharding
;
import
lombok.RequiredArgsConstructor
;
import
org.apache.shardingsphere.core.optimize.encrypt.statement.EncryptTransparentOptimizedStatement
;
import
org.apache.shardingsphere.core.optimize.sharding.statement.ShardingTransparentOptimizedStatement
;
import
org.apache.shardingsphere.core.parse.core.SQLParseKernel
;
import
org.apache.shardingsphere.core.parse.core.rule.registry.ParseRuleRegistry
;
import
org.apache.shardingsphere.core.parse.sql.statement.SQLStatement
;
import
org.apache.shardingsphere.core.route.SQLRouteResult
;
import
org.apache.shardingsphere.core.route.type.RoutingResult
;
import
org.apache.shardingsphere.core.route.type.hint.DatabaseHintRoutingEngine
;
import
org.apache.shardingsphere.core.rule.ShardingRule
;
import
org.apache.shardingsphere.core.strategy.route.hint.HintShardingStrategy
;
import
org.apache.shardingsphere.spi.database.DatabaseType
;
import
java.util.List
;
/**
* Sharding router for hint database only.
*
* @author zhangliang
* @author maxiaoguang
*/
// TODO removed after all ANTLR parser finished
@RequiredArgsConstructor
public
final
class
DatabaseHintSQLRouter
implements
ShardingRouter
{
private
final
DatabaseType
databaseType
;
private
final
ShardingRule
shardingRule
;
@Override
public
SQLStatement
parse
(
final
String
logicSQL
,
final
boolean
useCache
)
{
return
new
SQLParseKernel
(
ParseRuleRegistry
.
getInstance
(),
databaseType
,
logicSQL
).
parse
();
}
@Override
// TODO insert SQL need parse gen key
public
SQLRouteResult
route
(
final
String
logicSQL
,
final
List
<
Object
>
parameters
,
final
SQLStatement
sqlStatement
)
{
SQLRouteResult
result
=
new
SQLRouteResult
(
new
ShardingTransparentOptimizedStatement
(
sqlStatement
),
new
EncryptTransparentOptimizedStatement
(
sqlStatement
));
RoutingResult
routingResult
=
new
DatabaseHintRoutingEngine
(
shardingRule
.
getShardingDataSourceNames
().
getDataSourceNames
(),
(
HintShardingStrategy
)
shardingRule
.
getDefaultDatabaseShardingStrategy
()).
route
();
result
.
setRoutingResult
(
routingResult
);
return
result
;
}
}
sharding-core/sharding-core-route/src/main/java/org/apache/shardingsphere/core/route/router/sharding/ShardingRouterFactory.java
已删除
100644 → 0
浏览文件 @
ec22ada5
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package
org.apache.shardingsphere.core.route.router.sharding
;
import
lombok.AccessLevel
;
import
lombok.NoArgsConstructor
;
import
org.apache.shardingsphere.api.hint.HintManager
;
import
org.apache.shardingsphere.core.metadata.ShardingSphereMetaData
;
import
org.apache.shardingsphere.core.parse.SQLParseEngine
;
import
org.apache.shardingsphere.core.rule.ShardingRule
;
import
org.apache.shardingsphere.spi.database.DatabaseType
;
/**
* Sharding router factory.
*
* @author zhangliang
* @author panjuan
*/
@NoArgsConstructor
(
access
=
AccessLevel
.
PRIVATE
)
public
final
class
ShardingRouterFactory
{
/**
* Create new instance of sharding router.
*
* @param shardingRule sharding rule
* @param metaData meta data of ShardingSphere
* @param databaseType database type
* @param sqlParseEngine parsing engine
* @return sharding router instance
*/
public
static
ShardingRouter
newInstance
(
final
ShardingRule
shardingRule
,
final
ShardingSphereMetaData
metaData
,
final
DatabaseType
databaseType
,
final
SQLParseEngine
sqlParseEngine
)
{
return
HintManager
.
isDatabaseShardingOnly
()
?
new
DatabaseHintSQLRouter
(
databaseType
,
shardingRule
)
:
new
ParsingSQLRouter
(
shardingRule
,
metaData
,
sqlParseEngine
);
}
}
sharding-core/sharding-core-route/src/test/java/org/apache/shardingsphere/core/route/DatabaseTest.java
浏览文件 @
2d19ab16
...
...
@@ -22,7 +22,6 @@ import org.apache.shardingsphere.api.config.sharding.ShardingRuleConfiguration;
import
org.apache.shardingsphere.api.config.sharding.TableRuleConfiguration
;
import
org.apache.shardingsphere.api.config.sharding.strategy.HintShardingStrategyConfiguration
;
import
org.apache.shardingsphere.api.config.sharding.strategy.InlineShardingStrategyConfiguration
;
import
org.apache.shardingsphere.api.hint.HintManager
;
import
org.apache.shardingsphere.core.database.DatabaseTypes
;
import
org.apache.shardingsphere.core.metadata.ShardingSphereMetaData
;
import
org.apache.shardingsphere.core.metadata.column.ColumnMetaData
;
...
...
@@ -42,7 +41,6 @@ import java.util.HashMap;
import
java.util.LinkedHashMap
;
import
java.util.Map
;
import
static
org
.
hamcrest
.
CoreMatchers
.
hasItems
;
import
static
org
.
hamcrest
.
CoreMatchers
.
is
;
import
static
org
.
junit
.
Assert
.
assertThat
;
import
static
org
.
mockito
.
Mockito
.
mock
;
...
...
@@ -62,26 +60,11 @@ public final class DatabaseTest {
shardingRule
=
new
ShardingRule
(
shardingRuleConfig
,
dataSourceMap
.
keySet
());
}
@Test
public
void
assertHintSQL
()
{
try
(
HintManager
hintManager
=
HintManager
.
getInstance
())
{
hintManager
.
setDatabaseShardingValue
(
1
);
assertTarget
(
"select * from tesT"
,
"ds_1"
);
assertTarget
(
"insert into test values (1,2)"
,
"ds_1"
);
assertTarget
(
"update test set a = 1"
,
"ds_1"
);
assertTarget
(
"delete from test where id = 2"
,
"ds_1"
);
hintManager
.
setDatabaseShardingValue
(
2
);
assertTarget
(
"select * from tesT"
,
"ds_0"
);
hintManager
.
close
();
}
}
@Test
public
void
assertDatabaseAllRoutingSQL
()
{
String
originSQL
=
"select * from tesT"
;
SQLParseEngine
parseEngine
=
new
SQLParseEngine
(
DatabaseTypes
.
getActualDatabaseType
(
"MySQL"
));
SQLRouteResult
actual
=
new
StatementRoutingEngine
(
shardingRule
,
getMetaDataForAllRoutingSQL
(),
DatabaseTypes
.
getActualDatabaseType
(
"MySQL"
),
parseEngine
).
route
(
originSQL
);
SQLRouteResult
actual
=
new
StatementRoutingEngine
(
shardingRule
,
getMetaDataForAllRoutingSQL
(),
parseEngine
).
route
(
originSQL
);
assertThat
(
actual
.
getRoutingResult
().
getRoutingUnits
().
size
(),
is
(
1
));
Collection
<
String
>
actualDataSources
=
actual
.
getRoutingResult
().
getDataSourceNames
();
assertThat
(
actualDataSources
.
size
(),
is
(
1
));
...
...
@@ -101,12 +84,11 @@ public final class DatabaseTest {
public
void
assertDatabaseSelectSQLPagination
()
{
String
originSQL
=
"select user_id from tbl_pagination limit 0,5"
;
SQLParseEngine
parseEngine
=
new
SQLParseEngine
(
DatabaseTypes
.
getActualDatabaseType
(
"MySQL"
));
SQLRouteResult
actual
=
new
StatementRoutingEngine
(
shardingRule
,
getMetaDataForPagination
(),
DatabaseTypes
.
getActualDatabaseType
(
"MySQL"
),
parseEngine
).
route
(
originSQL
);
SQLRouteResult
actual
=
new
StatementRoutingEngine
(
shardingRule
,
getMetaDataForPagination
(),
parseEngine
).
route
(
originSQL
);
assertThat
(((
ShardingSelectOptimizedStatement
)
actual
.
getShardingStatement
()).
getPagination
().
getActualOffset
(),
is
(
0L
));
assertThat
(((
ShardingSelectOptimizedStatement
)
actual
.
getShardingStatement
()).
getPagination
().
getActualRowCount
().
orNull
(),
is
(
5L
));
originSQL
=
"select user_id from tbl_pagination limit 5,5"
;
actual
=
new
StatementRoutingEngine
(
shardingRule
,
getMetaDataForPagination
(),
DatabaseTypes
.
getActualDatabaseType
(
"MySQL"
),
parseEngine
).
route
(
originSQL
);
actual
=
new
StatementRoutingEngine
(
shardingRule
,
getMetaDataForPagination
(),
parseEngine
).
route
(
originSQL
);
assertThat
(((
ShardingSelectOptimizedStatement
)
actual
.
getShardingStatement
()).
getPagination
().
getActualOffset
(),
is
(
5L
));
assertThat
(((
ShardingSelectOptimizedStatement
)
actual
.
getShardingStatement
()).
getPagination
().
getActualRowCount
().
orNull
(),
is
(
5L
));
}
...
...
@@ -137,21 +119,13 @@ public final class DatabaseTest {
ShardingSphereMetaData
metaData
=
mock
(
ShardingSphereMetaData
.
class
);
when
(
metaData
.
getTables
()).
thenReturn
(
mock
(
TableMetas
.
class
));
SQLParseEngine
parseEngine
=
new
SQLParseEngine
(
DatabaseTypes
.
getActualDatabaseType
(
"MySQL"
));
SQLRouteResult
actual
=
new
PreparedStatementRoutingEngine
(
originSQL
,
rule
,
metaData
,
DatabaseTypes
.
getActualDatabaseType
(
"MySQL"
),
parseEngine
).
route
(
Lists
.<
Object
>
newArrayList
(
13
,
173
));
SQLRouteResult
actual
=
new
PreparedStatementRoutingEngine
(
originSQL
,
rule
,
metaData
,
parseEngine
).
route
(
Lists
.<
Object
>
newArrayList
(
13
,
173
));
assertThat
(((
ShardingSelectOptimizedStatement
)
actual
.
getShardingStatement
()).
getPagination
().
getActualOffset
(),
is
(
5L
));
assertThat
(((
ShardingSelectOptimizedStatement
)
actual
.
getShardingStatement
()).
getPagination
().
getActualRowCount
().
orNull
(),
is
(
10L
));
assertThat
(
actual
.
getRoutingResult
().
getRoutingUnits
().
size
(),
is
(
1
));
originSQL
=
"select city_id from t_user where city_id in (?,?) limit 5,10"
;
actual
=
new
PreparedStatementRoutingEngine
(
originSQL
,
rule
,
metaData
,
DatabaseTypes
.
getActualDatabaseType
(
"MySQL"
),
parseEngine
).
route
(
Lists
.<
Object
>
newArrayList
(
89
,
84
));
actual
=
new
PreparedStatementRoutingEngine
(
originSQL
,
rule
,
metaData
,
parseEngine
).
route
(
Lists
.<
Object
>
newArrayList
(
89
,
84
));
assertThat
(((
ShardingSelectOptimizedStatement
)
actual
.
getShardingStatement
()).
getPagination
().
getActualOffset
(),
is
(
5L
));
assertThat
(((
ShardingSelectOptimizedStatement
)
actual
.
getShardingStatement
()).
getPagination
().
getActualRowCount
().
orNull
(),
is
(
10L
));
}
private
void
assertTarget
(
final
String
originalSQL
,
final
String
targetDataSource
)
{
SQLRouteResult
actual
=
new
StatementRoutingEngine
(
shardingRule
,
mock
(
ShardingSphereMetaData
.
class
),
DatabaseTypes
.
getActualDatabaseType
(
"MySQL"
),
mock
(
SQLParseEngine
.
class
)).
route
(
originalSQL
);
assertThat
(
actual
.
getRoutingResult
().
getRoutingUnits
().
size
(),
is
(
1
));
assertThat
(
actual
.
getRoutingResult
().
getDataSourceNames
(),
hasItems
(
targetDataSource
));
}
}
sharding-core/sharding-core-route/src/test/java/org/apache/shardingsphere/core/route/router/DatabaseHintSQLRouterTest.java
已删除
100644 → 0
浏览文件 @
ec22ada5
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package
org.apache.shardingsphere.core.route.router
;
import
org.apache.shardingsphere.api.config.sharding.ShardingRuleConfiguration
;
import
org.apache.shardingsphere.api.config.sharding.TableRuleConfiguration
;
import
org.apache.shardingsphere.api.config.sharding.strategy.HintShardingStrategyConfiguration
;
import
org.apache.shardingsphere.api.hint.HintManager
;
import
org.apache.shardingsphere.core.database.DatabaseTypes
;
import
org.apache.shardingsphere.core.parse.sql.statement.dml.SelectStatement
;
import
org.apache.shardingsphere.core.route.fixture.HintShardingAlgorithmFixture
;
import
org.apache.shardingsphere.core.route.router.sharding.DatabaseHintSQLRouter
;
import
org.apache.shardingsphere.core.rule.ShardingRule
;
import
org.junit.After
;
import
org.junit.Before
;
import
org.junit.Test
;
import
java.util.Arrays
;
import
java.util.Collections
;
import
static
org
.
hamcrest
.
CoreMatchers
.
instanceOf
;
import
static
org
.
junit
.
Assert
.
assertNotNull
;
import
static
org
.
junit
.
Assert
.
assertThat
;
public
final
class
DatabaseHintSQLRouterTest
{
private
final
HintManager
hintManager
=
HintManager
.
getInstance
();
private
DatabaseHintSQLRouter
databaseHintSQLRouter
;
@After
public
void
tearDown
()
{
hintManager
.
close
();
}
@Before
public
void
setRouterContext
()
{
ShardingRuleConfiguration
shardingRuleConfig
=
new
ShardingRuleConfiguration
();
shardingRuleConfig
.
getTableRuleConfigs
().
add
(
new
TableRuleConfiguration
(
"LOGIC_TABLE"
,
"ds${0..1}.table_${0..2}"
));
shardingRuleConfig
.
setDefaultDatabaseShardingStrategyConfig
(
new
HintShardingStrategyConfiguration
(
new
HintShardingAlgorithmFixture
()));
ShardingRule
shardingRule
=
new
ShardingRule
(
shardingRuleConfig
,
Arrays
.
asList
(
"ds0"
,
"ds1"
));
databaseHintSQLRouter
=
new
DatabaseHintSQLRouter
(
DatabaseTypes
.
getActualDatabaseType
(
"MySQL"
),
shardingRule
);
}
@Test
public
void
assertParse
()
{
assertThat
(
databaseHintSQLRouter
.
parse
(
"select t from tbl t"
,
false
),
instanceOf
(
SelectStatement
.
class
));
}
@Test
public
void
assertRoute
()
{
hintManager
.
addDatabaseShardingValue
(
""
,
1
);
SelectStatement
dqlStatement
=
new
SelectStatement
();
assertNotNull
(
databaseHintSQLRouter
.
route
(
"select t from tbl t"
,
Collections
.
emptyList
(),
dqlStatement
));
}
}
sharding-core/sharding-core-route/src/test/java/org/apache/shardingsphere/core/route/type/standard/AbstractSQLRouteTest.java
浏览文件 @
2d19ab16
...
...
@@ -50,7 +50,7 @@ public abstract class AbstractSQLRouteTest {
ShardingRule
shardingRule
=
createShardingRule
();
ShardingSphereMetaData
metaData
=
new
ShardingSphereMetaData
(
buildDataSourceMetas
(),
buildTableMetas
());
SQLParseEngine
parseEngine
=
new
SQLParseEngine
(
DatabaseTypes
.
getActualDatabaseType
(
"MySQL"
));
PreparedStatementRoutingEngine
engine
=
new
PreparedStatementRoutingEngine
(
sql
,
shardingRule
,
metaData
,
DatabaseTypes
.
getActualDatabaseType
(
"MySQL"
),
parseEngine
);
PreparedStatementRoutingEngine
engine
=
new
PreparedStatementRoutingEngine
(
sql
,
shardingRule
,
metaData
,
parseEngine
);
SQLRouteResult
result
=
engine
.
route
(
parameters
);
assertThat
(
result
.
getRoutingResult
().
getRoutingUnits
().
size
(),
is
(
1
));
return
result
;
...
...
sharding-jdbc/sharding-jdbc-core/src/main/java/org/apache/shardingsphere/shardingjdbc/jdbc/core/statement/ShardingPreparedStatement.java
浏览文件 @
2d19ab16
...
...
@@ -32,7 +32,6 @@ import org.apache.shardingsphere.core.optimize.sharding.statement.dml.ShardingIn
import
org.apache.shardingsphere.core.optimize.sharding.statement.dml.ShardingSelectOptimizedStatement
;
import
org.apache.shardingsphere.core.parse.sql.statement.dal.DALStatement
;
import
org.apache.shardingsphere.core.parse.sql.statement.dml.InsertStatement
;
import
org.apache.shardingsphere.core.parse.sql.statement.dml.SelectStatement
;
import
org.apache.shardingsphere.core.route.SQLRouteResult
;
import
org.apache.shardingsphere.shardingjdbc.executor.BatchPreparedStatementExecutor
;
import
org.apache.shardingsphere.shardingjdbc.executor.PreparedStatementExecutor
;
...
...
@@ -96,8 +95,7 @@ public final class ShardingPreparedStatement extends AbstractShardingPreparedSta
this
.
connection
=
connection
;
this
.
sql
=
sql
;
ShardingRuntimeContext
runtimeContext
=
connection
.
getRuntimeContext
();
shardingEngine
=
new
PreparedQueryShardingEngine
(
sql
,
runtimeContext
.
getRule
(),
runtimeContext
.
getProps
(),
runtimeContext
.
getMetaData
(),
runtimeContext
.
getDatabaseType
(),
runtimeContext
.
getParseEngine
());
shardingEngine
=
new
PreparedQueryShardingEngine
(
sql
,
runtimeContext
.
getRule
(),
runtimeContext
.
getProps
(),
runtimeContext
.
getMetaData
(),
runtimeContext
.
getParseEngine
());
preparedStatementExecutor
=
new
PreparedStatementExecutor
(
resultSetType
,
resultSetConcurrency
,
resultSetHoldability
,
returnGeneratedKeys
,
connection
);
batchPreparedStatementExecutor
=
new
BatchPreparedStatementExecutor
(
resultSetType
,
resultSetConcurrency
,
resultSetHoldability
,
returnGeneratedKeys
,
connection
);
}
...
...
sharding-jdbc/sharding-jdbc-core/src/main/java/org/apache/shardingsphere/shardingjdbc/jdbc/core/statement/ShardingStatement.java
浏览文件 @
2d19ab16
...
...
@@ -30,7 +30,6 @@ import org.apache.shardingsphere.core.optimize.sharding.statement.dml.ShardingIn
import
org.apache.shardingsphere.core.optimize.sharding.statement.dml.ShardingSelectOptimizedStatement
;
import
org.apache.shardingsphere.core.parse.sql.statement.dal.DALStatement
;
import
org.apache.shardingsphere.core.parse.sql.statement.dml.InsertStatement
;
import
org.apache.shardingsphere.core.parse.sql.statement.dml.SelectStatement
;
import
org.apache.shardingsphere.core.route.SQLRouteResult
;
import
org.apache.shardingsphere.shardingjdbc.executor.StatementExecutor
;
import
org.apache.shardingsphere.shardingjdbc.jdbc.adapter.AbstractStatementAdapter
;
...
...
@@ -247,8 +246,7 @@ public final class ShardingStatement extends AbstractStatementAdapter {
private
void
shard
(
final
String
sql
)
{
ShardingRuntimeContext
runtimeContext
=
connection
.
getRuntimeContext
();
SimpleQueryShardingEngine
shardingEngine
=
new
SimpleQueryShardingEngine
(
runtimeContext
.
getRule
(),
runtimeContext
.
getProps
(),
runtimeContext
.
getMetaData
(),
runtimeContext
.
getDatabaseType
(),
runtimeContext
.
getParseEngine
());
SimpleQueryShardingEngine
shardingEngine
=
new
SimpleQueryShardingEngine
(
runtimeContext
.
getRule
(),
runtimeContext
.
getProps
(),
runtimeContext
.
getMetaData
(),
runtimeContext
.
getParseEngine
());
sqlRouteResult
=
shardingEngine
.
shard
(
sql
,
Collections
.
emptyList
());
}
...
...
sharding-proxy/sharding-proxy-backend/src/main/java/org/apache/shardingsphere/shardingproxy/backend/communication/jdbc/JDBCDatabaseCommunicationEngine.java
浏览文件 @
2d19ab16
...
...
@@ -43,7 +43,6 @@ import org.apache.shardingsphere.shardingproxy.backend.schema.LogicSchema;
import
org.apache.shardingsphere.shardingproxy.backend.schema.LogicSchemas
;
import
org.apache.shardingsphere.shardingproxy.backend.schema.impl.EncryptSchema
;
import
org.apache.shardingsphere.shardingproxy.backend.schema.impl.ShardingSchema
;
import
org.apache.shardingsphere.spi.database.DatabaseType
;
import
org.apache.shardingsphere.transaction.core.TransactionType
;
import
java.sql.SQLException
;
...
...
@@ -63,8 +62,6 @@ import java.util.List;
@RequiredArgsConstructor
public
final
class
JDBCDatabaseCommunicationEngine
implements
DatabaseCommunicationEngine
{
private
final
DatabaseType
databaseType
=
LogicSchemas
.
getInstance
().
getDatabaseType
();
private
final
LogicSchema
logicSchema
;
private
final
String
sql
;
...
...
@@ -78,7 +75,7 @@ public final class JDBCDatabaseCommunicationEngine implements DatabaseCommunicat
@Override
public
BackendResponse
execute
()
{
try
{
SQLRouteResult
routeResult
=
executeEngine
.
getJdbcExecutorWrapper
().
route
(
sql
,
databaseType
);
SQLRouteResult
routeResult
=
executeEngine
.
getJdbcExecutorWrapper
().
route
(
sql
);
return
execute
(
routeResult
);
}
catch
(
final
SQLException
ex
)
{
return
new
ErrorResponse
(
ex
);
...
...
@@ -127,7 +124,7 @@ public final class JDBCDatabaseCommunicationEngine implements DatabaseCommunicat
}
private
void
setMergedResult
(
final
SQLRouteResult
routeResult
)
throws
SQLException
{
mergedResult
=
MergeEngineFactory
.
newInstance
(
databaseType
,
mergedResult
=
MergeEngineFactory
.
newInstance
(
LogicSchemas
.
getInstance
().
getDatabaseType
()
,
logicSchema
.
getShardingRule
(),
routeResult
,
logicSchema
.
getMetaData
().
getTables
(),
((
QueryResponse
)
response
).
getQueryResults
()).
merge
();
}
...
...
sharding-proxy/sharding-proxy-backend/src/main/java/org/apache/shardingsphere/shardingproxy/backend/communication/jdbc/wrapper/JDBCExecutorWrapper.java
浏览文件 @
2d19ab16
...
...
@@ -19,7 +19,6 @@ package org.apache.shardingsphere.shardingproxy.backend.communication.jdbc.wrapp
import
org.apache.shardingsphere.core.route.SQLRouteResult
;
import
org.apache.shardingsphere.core.route.SQLUnit
;
import
org.apache.shardingsphere.spi.database.DatabaseType
;
import
java.sql.Connection
;
import
java.sql.SQLException
;
...
...
@@ -36,10 +35,9 @@ public interface JDBCExecutorWrapper {
* Route SQL.
*
* @param sql SQL to be routed
* @param databaseType database type
* @return SQL route result
*/
SQLRouteResult
route
(
String
sql
,
DatabaseType
databaseType
);
SQLRouteResult
route
(
String
sql
);
/**
* Create statement.
...
...
sharding-proxy/sharding-proxy-backend/src/main/java/org/apache/shardingsphere/shardingproxy/backend/communication/jdbc/wrapper/PreparedStatementExecutorWrapper.java
浏览文件 @
2d19ab16
...
...
@@ -36,7 +36,6 @@ import org.apache.shardingsphere.shardingproxy.backend.schema.impl.EncryptSchema
import
org.apache.shardingsphere.shardingproxy.backend.schema.impl.MasterSlaveSchema
;
import
org.apache.shardingsphere.shardingproxy.backend.schema.impl.ShardingSchema
;
import
org.apache.shardingsphere.shardingproxy.context.ShardingProxyContext
;
import
org.apache.shardingsphere.spi.database.DatabaseType
;
import
java.sql.Connection
;
import
java.sql.PreparedStatement
;
...
...
@@ -60,9 +59,9 @@ public final class PreparedStatementExecutorWrapper implements JDBCExecutorWrapp
private
final
List
<
Object
>
parameters
;
@Override
public
SQLRouteResult
route
(
final
String
sql
,
final
DatabaseType
databaseType
)
{
public
SQLRouteResult
route
(
final
String
sql
)
{
if
(
logicSchema
instanceof
ShardingSchema
)
{
return
doShardingRoute
(
sql
,
databaseType
);
return
doShardingRoute
(
sql
);
}
if
(
logicSchema
instanceof
MasterSlaveSchema
)
{
return
doMasterSlaveRoute
(
sql
);
...
...
@@ -70,9 +69,9 @@ public final class PreparedStatementExecutorWrapper implements JDBCExecutorWrapp
return
doEncryptRoute
(
sql
);
}
private
SQLRouteResult
doShardingRoute
(
final
String
sql
,
final
DatabaseType
databaseType
)
{
PreparedQueryShardingEngine
shardingEngine
=
new
PreparedQueryShardingEngine
(
sql
,
logicSchema
.
getShardingRule
(),
ShardingProxyContext
.
getInstance
().
getShardingProperties
(),
logicSchema
.
getMetaData
(),
databaseType
,
logicSchema
.
getParseEngine
());
private
SQLRouteResult
doShardingRoute
(
final
String
sql
)
{
PreparedQueryShardingEngine
shardingEngine
=
new
PreparedQueryShardingEngine
(
sql
,
logicSchema
.
getShardingRule
(),
ShardingProxyContext
.
getInstance
().
getShardingProperties
(),
logicSchema
.
getMetaData
()
,
logicSchema
.
getParseEngine
());
return
shardingEngine
.
shard
(
sql
,
parameters
);
}
...
...
sharding-proxy/sharding-proxy-backend/src/main/java/org/apache/shardingsphere/shardingproxy/backend/communication/jdbc/wrapper/StatementExecutorWrapper.java
浏览文件 @
2d19ab16
...
...
@@ -36,7 +36,6 @@ import org.apache.shardingsphere.shardingproxy.backend.schema.impl.EncryptSchema
import
org.apache.shardingsphere.shardingproxy.backend.schema.impl.MasterSlaveSchema
;
import
org.apache.shardingsphere.shardingproxy.backend.schema.impl.ShardingSchema
;
import
org.apache.shardingsphere.shardingproxy.context.ShardingProxyContext
;
import
org.apache.shardingsphere.spi.database.DatabaseType
;
import
java.sql.Connection
;
import
java.sql.SQLException
;
...
...
@@ -58,9 +57,9 @@ public final class StatementExecutorWrapper implements JDBCExecutorWrapper {
private
final
LogicSchema
logicSchema
;
@Override
public
SQLRouteResult
route
(
final
String
sql
,
final
DatabaseType
databaseType
)
{
public
SQLRouteResult
route
(
final
String
sql
)
{
if
(
logicSchema
instanceof
ShardingSchema
)
{
return
doShardingRoute
(
sql
,
databaseType
);
return
doShardingRoute
(
sql
);
}
if
(
logicSchema
instanceof
MasterSlaveSchema
)
{
return
doMasterSlaveRoute
(
sql
);
...
...
@@ -71,9 +70,9 @@ public final class StatementExecutorWrapper implements JDBCExecutorWrapper {
return
doTransparentRoute
(
sql
);
}
private
SQLRouteResult
doShardingRoute
(
final
String
sql
,
final
DatabaseType
databaseType
)
{
SimpleQueryShardingEngine
shardingEngine
=
new
SimpleQueryShardingEngine
(
logicSchema
.
getShardingRule
(),
ShardingProxyContext
.
getInstance
().
getShardingProperties
(),
logicSchema
.
getMetaData
(),
databaseType
,
logicSchema
.
getParseEngine
());
private
SQLRouteResult
doShardingRoute
(
final
String
sql
)
{
SimpleQueryShardingEngine
shardingEngine
=
new
SimpleQueryShardingEngine
(
logicSchema
.
getShardingRule
(),
ShardingProxyContext
.
getInstance
().
getShardingProperties
(),
logicSchema
.
getMetaData
()
,
logicSchema
.
getParseEngine
());
return
shardingEngine
.
shard
(
sql
,
Collections
.
emptyList
());
}
...
...
sharding-proxy/sharding-proxy-backend/src/main/java/org/apache/shardingsphere/shardingproxy/backend/text/sctl/explain/ShardingCTLExplainBackendHandler.java
浏览文件 @
2d19ab16
...
...
@@ -27,7 +27,6 @@ import org.apache.shardingsphere.shardingproxy.backend.response.error.ErrorRespo
import
org.apache.shardingsphere.shardingproxy.backend.response.query.QueryData
;
import
org.apache.shardingsphere.shardingproxy.backend.response.query.QueryHeader
;
import
org.apache.shardingsphere.shardingproxy.backend.response.query.QueryResponse
;
import
org.apache.shardingsphere.shardingproxy.backend.schema.LogicSchemas
;
import
org.apache.shardingsphere.shardingproxy.backend.text.TextProtocolBackendHandler
;
import
org.apache.shardingsphere.shardingproxy.backend.text.sctl.exception.InvalidShardingCTLFormatException
;
...
...
@@ -59,7 +58,7 @@ public final class ShardingCTLExplainBackendHandler implements TextProtocolBacke
return
new
ErrorResponse
(
new
InvalidShardingCTLFormatException
(
sql
));
}
StatementExecutorWrapper
statementExecutorWrapper
=
new
StatementExecutorWrapper
(
backendConnection
.
getLogicSchema
());
routeUnits
=
statementExecutorWrapper
.
route
(
explainStatement
.
get
().
getSql
()
,
LogicSchemas
.
getInstance
().
getDatabaseType
()
).
getRouteUnits
().
iterator
();
routeUnits
=
statementExecutorWrapper
.
route
(
explainStatement
.
get
().
getSql
()).
getRouteUnits
().
iterator
();
queryHeaders
=
new
ArrayList
<>(
2
);
queryHeaders
.
add
(
new
QueryHeader
(
""
,
""
,
"datasource_name"
,
""
,
255
,
Types
.
CHAR
,
0
));
queryHeaders
.
add
(
new
QueryHeader
(
""
,
""
,
"sql"
,
""
,
255
,
Types
.
CHAR
,
0
));
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录