Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
apache
Shardingsphere
提交
64e267fd
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,发现更多精彩内容 >>
提交
64e267fd
编写于
3月 28, 2018
作者:
T
terrymanu
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
For #660: use yaml config file to instead of ShardingRuleMockBuilder on rewrite
上级
a642bf29
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
28 addition
and
117 deletion
+28
-117
sharding-core/src/test/java/io/shardingjdbc/core/api/fixture/ShardingRuleMockBuilder.java
...hardingjdbc/core/api/fixture/ShardingRuleMockBuilder.java
+0
-114
sharding-core/src/test/java/io/shardingjdbc/core/rewrite/SQLRewriteEngineTest.java
...va/io/shardingjdbc/core/rewrite/SQLRewriteEngineTest.java
+7
-3
sharding-core/src/test/resources/yaml/rewrite-rule.yaml
sharding-core/src/test/resources/yaml/rewrite-rule.yaml
+21
-0
未找到文件。
sharding-core/src/test/java/io/shardingjdbc/core/api/fixture/ShardingRuleMockBuilder.java
已删除
100644 → 0
浏览文件 @
a642bf29
/*
* Copyright 1999-2015 dangdang.com.
* <p>
* Licensed 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.
* </p>
*/
package
io.shardingjdbc.core.api.fixture
;
import
com.google.common.base.Function
;
import
com.google.common.base.Joiner
;
import
com.google.common.collect.Iterators
;
import
com.google.common.collect.Lists
;
import
io.shardingjdbc.core.api.config.ShardingRuleConfiguration
;
import
io.shardingjdbc.core.api.config.TableRuleConfiguration
;
import
io.shardingjdbc.core.keygen.fixture.IncrementKeyGenerator
;
import
io.shardingjdbc.core.rule.ShardingRule
;
import
java.util.Collection
;
import
java.util.HashMap
;
import
java.util.HashSet
;
import
java.util.LinkedList
;
import
java.util.List
;
import
java.util.Map
;
import
java.util.Set
;
@Deprecated
// TODO refactor
public
class
ShardingRuleMockBuilder
{
private
final
List
<
TableRuleConfiguration
>
tableRuleConfigs
=
new
LinkedList
<>();
private
final
List
<
String
>
shardingColumns
=
new
LinkedList
<>();
private
final
Map
<
String
,
String
>
generateKeyColumnsMap
=
new
HashMap
<>();
private
final
Set
<
String
>
bindTables
=
new
HashSet
<>();
public
ShardingRuleMockBuilder
addTableRuleConfig
(
final
TableRuleConfiguration
tableRuleConfig
)
{
tableRuleConfigs
.
add
(
tableRuleConfig
);
return
this
;
}
public
ShardingRuleMockBuilder
addShardingColumns
(
final
String
shardingColumnName
)
{
shardingColumns
.
add
(
shardingColumnName
);
return
this
;
}
public
ShardingRuleMockBuilder
addGenerateKeyColumn
(
final
String
tableName
,
final
String
columnName
)
{
generateKeyColumnsMap
.
put
(
tableName
,
columnName
);
return
this
;
}
public
ShardingRuleMockBuilder
addBindingTable
(
final
String
bindingTableName
)
{
bindTables
.
add
(
bindingTableName
);
return
this
;
}
public
ShardingRule
build
()
{
Collection
<
TableRuleConfiguration
>
tableRuleConfigs
=
Lists
.
newArrayList
(
Iterators
.
transform
(
generateKeyColumnsMap
.
keySet
().
iterator
(),
new
Function
<
String
,
TableRuleConfiguration
>()
{
@Override
public
TableRuleConfiguration
apply
(
final
String
input
)
{
TableRuleConfiguration
result
=
new
TableRuleConfiguration
();
result
.
setLogicTable
(
input
);
result
.
setActualDataNodes
(
"db0."
+
input
+
",db1."
+
input
);
result
.
setKeyGeneratorColumnName
(
generateKeyColumnsMap
.
get
(
input
));
return
result
;
}
}));
tableRuleConfigs
.
addAll
(
this
.
tableRuleConfigs
);
if
(
tableRuleConfigs
.
isEmpty
())
{
TableRuleConfiguration
tableRuleConfig
=
new
TableRuleConfiguration
();
tableRuleConfig
.
setLogicTable
(
"mock"
);
tableRuleConfig
.
setActualDataNodes
(
"mock"
);
tableRuleConfigs
.
add
(
tableRuleConfig
);
}
ShardingRuleConfiguration
shardingRuleConfig
=
new
ShardingRuleConfiguration
();
for
(
String
each
:
bindTables
)
{
if
(
existInTableRuleConfig
(
each
))
{
continue
;
}
TableRuleConfiguration
tableRuleConfig
=
new
TableRuleConfiguration
();
tableRuleConfig
.
setLogicTable
(
each
);
tableRuleConfigs
.
add
(
tableRuleConfig
);
}
for
(
TableRuleConfiguration
each
:
tableRuleConfigs
)
{
shardingRuleConfig
.
getTableRuleConfigs
().
add
(
each
);
bindTables
.
add
(
each
.
getLogicTable
());
}
shardingRuleConfig
.
getBindingTableGroups
().
add
(
Joiner
.
on
(
","
).
join
(
bindTables
));
shardingRuleConfig
.
setDefaultKeyGenerator
(
new
IncrementKeyGenerator
());
return
new
ShardingRule
(
shardingRuleConfig
,
Lists
.
newArrayList
(
"db0"
,
"db1"
));
}
private
boolean
existInTableRuleConfig
(
final
String
logicTableName
)
{
for
(
TableRuleConfiguration
tableRuleConfig
:
tableRuleConfigs
)
{
if
(
tableRuleConfig
.
getLogicTable
().
equals
(
logicTableName
))
{
return
true
;
}
}
return
false
;
}
}
sharding-core/src/test/java/io/shardingjdbc/core/rewrite/SQLRewriteEngineTest.java
浏览文件 @
64e267fd
...
...
@@ -18,9 +18,9 @@
package
io.shardingjdbc.core.rewrite
;
import
com.google.common.base.Optional
;
import
io.shardingjdbc.core.api.fixture.ShardingRuleMockBuilder
;
import
io.shardingjdbc.core.constant.DatabaseType
;
import
io.shardingjdbc.core.constant.OrderType
;
import
io.shardingjdbc.core.parsing.SQLParsingEngineTest
;
import
io.shardingjdbc.core.parsing.parser.context.OrderItem
;
import
io.shardingjdbc.core.parsing.parser.context.limit.Limit
;
import
io.shardingjdbc.core.parsing.parser.context.limit.LimitValue
;
...
...
@@ -34,9 +34,12 @@ import io.shardingjdbc.core.parsing.parser.token.TableToken;
import
io.shardingjdbc.core.routing.type.TableUnit
;
import
io.shardingjdbc.core.routing.type.complex.CartesianTableReference
;
import
io.shardingjdbc.core.rule.ShardingRule
;
import
io.shardingjdbc.core.yaml.sharding.YamlShardingConfiguration
;
import
org.junit.Before
;
import
org.junit.Test
;
import
java.io.File
;
import
java.io.IOException
;
import
java.util.Arrays
;
import
java.util.Collections
;
import
java.util.HashMap
;
...
...
@@ -54,8 +57,9 @@ public final class SQLRewriteEngineTest {
private
Map
<
String
,
String
>
tableTokens
;
@Before
public
void
setUp
()
{
shardingRule
=
new
ShardingRuleMockBuilder
().
addGenerateKeyColumn
(
"table_x"
,
"id"
).
addBindingTable
(
"table_y"
).
build
();
public
void
setUp
()
throws
IOException
{
YamlShardingConfiguration
yamlShardingConfig
=
YamlShardingConfiguration
.
unmarshal
(
new
File
(
SQLParsingEngineTest
.
class
.
getClassLoader
().
getResource
(
"yaml/rewrite-rule.yaml"
).
getFile
()));
shardingRule
=
yamlShardingConfig
.
getShardingRule
(
yamlShardingConfig
.
getDataSources
().
keySet
());
selectStatement
=
new
SelectStatement
();
tableTokens
=
new
HashMap
<>(
1
,
1
);
tableTokens
.
put
(
"table_x"
,
"table_1"
);
...
...
sharding-core/src/test/resources/yaml/rewrite-rule.yaml
0 → 100644
浏览文件 @
64e267fd
dataSources
:
db0
:
!!org.apache.commons.dbcp2.BasicDataSource
driverClassName
:
org.h2.Driver
url
:
jdbc:h2:mem:db0;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false;MODE=MYSQL
username
:
sa
password
:
db1
:
!!org.apache.commons.dbcp2.BasicDataSource
driverClassName
:
org.h2.Driver
url
:
jdbc:h2:mem:db1;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false;MODE=MYSQL
username
:
sa
password
:
shardingRule
:
tables
:
table_x
:
actualDataNodes
:
db${0..1}.table_x
keyGeneratorColumnName
:
id
table_y
:
actualDataNodes
:
db${0..1}.table_y
bindingTables
:
-
table_x, table_y
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录