Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
apache
Shardingsphere
提交
615cee1b
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,体验更适合开发者的 AI 搜索 >>
提交
615cee1b
编写于
5月 06, 2018
作者:
马
马晓光
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
use iterator.next() instead of get()
上级
77e358d9
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
22 addition
and
17 deletion
+22
-17
sharding-core/src/main/java/io/shardingjdbc/core/optimizer/insert/InsertOptimizeEngine.java
...rdingjdbc/core/optimizer/insert/InsertOptimizeEngine.java
+18
-13
sharding-core/src/test/java/io/shardingjdbc/core/optimizer/InsertOptimizeEngineTest.java
...shardingjdbc/core/optimizer/InsertOptimizeEngineTest.java
+4
-4
未找到文件。
sharding-core/src/main/java/io/shardingjdbc/core/optimizer/insert/InsertOptimizeEngine.java
浏览文件 @
615cee1b
...
...
@@ -34,6 +34,7 @@ import lombok.RequiredArgsConstructor;
import
java.util.ArrayList
;
import
java.util.Collection
;
import
java.util.Iterator
;
import
java.util.LinkedList
;
import
java.util.List
;
...
...
@@ -59,6 +60,7 @@ public final class InsertOptimizeEngine implements OptimizeEngine {
List
<
AndCondition
>
andConditions
=
insertStatement
.
getConditions
().
getOrCondition
().
getAndConditions
();
List
<
InsertValue
>
insertValues
=
insertStatement
.
getInsertValues
().
getInsertValues
();
List
<
ShardingCondition
>
result
=
new
ArrayList
<>(
andConditions
.
size
());
Iterator
<
Number
>
generatedKeys
=
null
;
int
count
=
0
;
for
(
AndCondition
each
:
andConditions
)
{
InsertValue
insertValue
=
insertValues
.
get
(
count
);
...
...
@@ -67,28 +69,36 @@ public final class InsertOptimizeEngine implements OptimizeEngine {
String
logicTableName
=
insertStatement
.
getTables
().
getSingleTableName
();
Optional
<
Column
>
generateKeyColumn
=
shardingRule
.
getGenerateKeyColumn
(
logicTableName
);
String
express
ion
;
InsertShardingCondition
insertShardingCondit
ion
;
if
(-
1
!=
insertStatement
.
getGenerateKeyColumnIndex
()
||
!
generateKeyColumn
.
isPresent
())
{
expression
=
insertValue
.
getExpression
(
);
insertShardingCondition
=
new
InsertShardingCondition
(
insertValue
.
getExpression
(),
currentParameters
);
}
else
{
if
(
null
==
generatedKeys
)
{
generatedKeys
=
generatedKey
.
getGeneratedKeys
().
iterator
();
}
String
expression
;
Number
currentGeneratedKey
=
generatedKeys
.
next
();
if
(
0
==
parameters
.
size
())
{
expression
=
insertValue
.
getExpression
().
substring
(
0
,
insertValue
.
getExpression
().
length
()
-
1
)
+
", "
+
generatedKey
.
getGeneratedKeys
().
get
(
count
)
.
toString
()
+
")"
;
expression
=
insertValue
.
getExpression
().
substring
(
0
,
insertValue
.
getExpression
().
length
()
-
1
)
+
", "
+
currentGeneratedKey
.
toString
()
+
")"
;
}
else
{
expression
=
insertValue
.
getExpression
().
substring
(
0
,
insertValue
.
getExpression
().
length
()
-
1
)
+
", ?)"
;
currentParameters
.
add
(
generatedKey
.
getGeneratedKeys
().
get
(
count
)
);
currentParameters
.
add
(
currentGeneratedKey
);
}
insertShardingCondition
=
new
InsertShardingCondition
(
expression
,
currentParameters
);
insertShardingCondition
.
getShardingValues
().
add
(
getShardingCondition
(
generateKeyColumn
.
get
(),
currentGeneratedKey
));
}
InsertShardingCondition
insertShardingCondition
=
new
InsertShardingCondition
(
expression
,
currentParameters
);
insertShardingCondition
.
getShardingValues
().
addAll
(
getShardingCondition
(
each
));
if
(-
1
==
insertStatement
.
getGenerateKeyColumnIndex
()
&&
generateKeyColumn
.
isPresent
())
{
insertShardingCondition
.
getShardingValues
().
add
(
getShardingCondition
(
generateKeyColumn
.
get
(),
generatedKey
.
getGeneratedKeys
().
get
(
count
)));
}
result
.
add
(
insertShardingCondition
);
count
++;
}
return
new
ShardingConditions
(
result
);
}
private
ListShardingValue
getShardingCondition
(
final
Column
column
,
final
Number
value
)
{
return
new
ListShardingValue
<>(
column
.
getTableName
(),
column
.
getName
(),
new
GeneratedKeyCondition
(
column
,
-
1
,
value
).
getConditionValues
(
parameters
));
}
private
Collection
<
ListShardingValue
>
getShardingCondition
(
final
AndCondition
andCondition
)
{
Collection
<
ListShardingValue
>
result
=
new
LinkedList
<>();
for
(
Condition
each
:
andCondition
.
getConditions
())
{
...
...
@@ -96,9 +106,4 @@ public final class InsertOptimizeEngine implements OptimizeEngine {
}
return
result
;
}
private
ListShardingValue
getShardingCondition
(
final
Column
column
,
final
Number
value
)
{
return
new
ListShardingValue
<>(
column
.
getTableName
(),
column
.
getName
(),
new
GeneratedKeyCondition
(
column
,
-
1
,
value
).
getConditionValues
(
parameters
));
}
}
sharding-core/src/test/java/io/shardingjdbc/core/optimizer/InsertOptimizeEngineTest.java
浏览文件 @
615cee1b
...
...
@@ -104,10 +104,10 @@ public final class InsertOptimizeEngineTest {
assertThat
(((
InsertShardingCondition
)
actual
.
getShardingConditions
().
get
(
1
)).
getInsertValueExpression
(),
is
(
"(?, ?, ?)"
));
assertThat
(
actual
.
getShardingConditions
().
get
(
0
).
getShardingValues
().
size
(),
is
(
2
));
assertThat
(
actual
.
getShardingConditions
().
get
(
1
).
getShardingValues
().
size
(),
is
(
2
));
assertShardingValue
((
ListShardingValue
)
actual
.
getShardingConditions
().
get
(
0
).
getShardingValues
().
get
(
0
),
1
0
);
assertShardingValue
((
ListShardingValue
)
actual
.
getShardingConditions
().
get
(
0
).
getShardingValues
().
get
(
1
),
1
);
assertShardingValue
((
ListShardingValue
)
actual
.
getShardingConditions
().
get
(
1
).
getShardingValues
().
get
(
0
),
11
);
assertShardingValue
((
ListShardingValue
)
actual
.
getShardingConditions
().
get
(
1
).
getShardingValues
().
get
(
1
),
2
);
assertShardingValue
((
ListShardingValue
)
actual
.
getShardingConditions
().
get
(
0
).
getShardingValues
().
get
(
0
),
1
);
assertShardingValue
((
ListShardingValue
)
actual
.
getShardingConditions
().
get
(
0
).
getShardingValues
().
get
(
1
),
1
0
);
assertShardingValue
((
ListShardingValue
)
actual
.
getShardingConditions
().
get
(
1
).
getShardingValues
().
get
(
0
),
2
);
assertShardingValue
((
ListShardingValue
)
actual
.
getShardingConditions
().
get
(
1
).
getShardingValues
().
get
(
1
),
11
);
}
@Test
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录