Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
apache
Shardingsphere
提交
fe0765d1
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,发现更多精彩内容 >>
提交
fe0765d1
编写于
5月 25, 2017
作者:
T
terrymanu
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
refactor api for generate key, multiple keys => single key 2nd version
上级
3f496c7a
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
28 addition
and
29 deletion
+28
-29
sharding-jdbc-core/src/main/java/com/dangdang/ddframe/rdb/sharding/jdbc/ShardingStatement.java
...dangdang/ddframe/rdb/sharding/jdbc/ShardingStatement.java
+2
-4
sharding-jdbc-core/src/main/java/com/dangdang/ddframe/rdb/sharding/parsing/parser/context/GeneratedKeyContext.java
.../sharding/parsing/parser/context/GeneratedKeyContext.java
+3
-3
sharding-jdbc-core/src/main/java/com/dangdang/ddframe/rdb/sharding/parsing/parser/context/InsertSQLContext.java
...rdb/sharding/parsing/parser/context/InsertSQLContext.java
+21
-21
sharding-jdbc-core/src/main/java/com/dangdang/ddframe/rdb/sharding/parsing/parser/type/insert/AbstractInsertParser.java
...ding/parsing/parser/type/insert/AbstractInsertParser.java
+2
-1
未找到文件。
sharding-jdbc-core/src/main/java/com/dangdang/ddframe/rdb/sharding/jdbc/ShardingStatement.java
浏览文件 @
fe0765d1
...
@@ -226,13 +226,11 @@ public class ShardingStatement extends AbstractStatementAdapter {
...
@@ -226,13 +226,11 @@ public class ShardingStatement extends AbstractStatementAdapter {
}
}
Optional
<
GeneratedKeyContext
>
generatedKeyContext
=
getGeneratedKeyContext
();
Optional
<
GeneratedKeyContext
>
generatedKeyContext
=
getGeneratedKeyContext
();
if
(!
generatedKeyContext
.
isPresent
())
{
if
(!
generatedKeyContext
.
isPresent
())
{
return
generatedKeysResultSet
=
new
GeneratedKeysResultSet
();
}
if
(
generatedKeyContext
.
get
().
getColumnNameToIndexMap
().
isEmpty
())
{
Collection
<?
extends
Statement
>
routedStatements
=
getRoutedStatements
();
Collection
<?
extends
Statement
>
routedStatements
=
getRoutedStatements
();
if
(
1
==
routedStatements
.
size
())
{
if
(
1
==
routedStatements
.
size
())
{
return
generatedKeysResultSet
=
routedStatements
.
iterator
().
next
().
getGeneratedKeys
();
return
generatedKeysResultSet
=
routedStatements
.
iterator
().
next
().
getGeneratedKeys
();
}
}
return
generatedKeysResultSet
=
new
GeneratedKeysResultSet
();
}
}
if
(
Statement
.
RETURN_GENERATED_KEYS
!=
generatedKeyContext
.
get
().
getAutoGeneratedKeys
()
if
(
Statement
.
RETURN_GENERATED_KEYS
!=
generatedKeyContext
.
get
().
getAutoGeneratedKeys
()
&&
null
==
generatedKeyContext
.
get
().
getColumnIndexes
()
&&
null
==
generatedKeyContext
.
get
().
getColumnNames
())
{
&&
null
==
generatedKeyContext
.
get
().
getColumnIndexes
()
&&
null
==
generatedKeyContext
.
get
().
getColumnNames
())
{
...
@@ -274,7 +272,7 @@ public class ShardingStatement extends AbstractStatementAdapter {
...
@@ -274,7 +272,7 @@ public class ShardingStatement extends AbstractStatementAdapter {
protected
final
Optional
<
GeneratedKeyContext
>
getGeneratedKeyContext
()
{
protected
final
Optional
<
GeneratedKeyContext
>
getGeneratedKeyContext
()
{
if
(
null
!=
sqlContext
&&
sqlContext
instanceof
InsertSQLContext
)
{
if
(
null
!=
sqlContext
&&
sqlContext
instanceof
InsertSQLContext
)
{
return
Optional
.
of
(((
InsertSQLContext
)
sqlContext
).
getGeneratedKeyContext
());
return
Optional
.
fromNullable
(((
InsertSQLContext
)
sqlContext
).
getGeneratedKeyContext
());
}
}
return
Optional
.
absent
();
return
Optional
.
absent
();
}
}
...
...
sharding-jdbc-core/src/main/java/com/dangdang/ddframe/rdb/sharding/parsing/parser/context/GeneratedKeyContext.java
浏览文件 @
fe0765d1
...
@@ -20,12 +20,11 @@ package com.dangdang.ddframe.rdb.sharding.parsing.parser.context;
...
@@ -20,12 +20,11 @@ package com.dangdang.ddframe.rdb.sharding.parsing.parser.context;
import
com.google.common.collect.Table
;
import
com.google.common.collect.Table
;
import
com.google.common.collect.TreeBasedTable
;
import
com.google.common.collect.TreeBasedTable
;
import
lombok.Getter
;
import
lombok.Getter
;
import
lombok.RequiredArgsConstructor
;
import
lombok.Setter
;
import
lombok.Setter
;
import
lombok.ToString
;
import
lombok.ToString
;
import
java.util.HashMap
;
import
java.util.HashMap
;
import
java.util.LinkedList
;
import
java.util.List
;
import
java.util.Map
;
import
java.util.Map
;
/**
/**
...
@@ -33,11 +32,12 @@ import java.util.Map;
...
@@ -33,11 +32,12 @@ import java.util.Map;
*
*
* @author gaohongtao
* @author gaohongtao
*/
*/
@RequiredArgsConstructor
@ToString
@ToString
public
final
class
GeneratedKeyContext
{
public
final
class
GeneratedKeyContext
{
@Getter
@Getter
private
final
List
<
String
>
columns
=
new
LinkedList
<>()
;
private
final
String
column
;
@Getter
@Getter
private
final
Map
<
String
,
Integer
>
columnNameToIndexMap
=
new
HashMap
<>();
private
final
Map
<
String
,
Integer
>
columnNameToIndexMap
=
new
HashMap
<>();
...
...
sharding-jdbc-core/src/main/java/com/dangdang/ddframe/rdb/sharding/parsing/parser/context/InsertSQLContext.java
浏览文件 @
fe0765d1
...
@@ -45,10 +45,10 @@ import java.util.Map;
...
@@ -45,10 +45,10 @@ import java.util.Map;
@Setter
@Setter
public
final
class
InsertSQLContext
extends
AbstractSQLContext
{
public
final
class
InsertSQLContext
extends
AbstractSQLContext
{
private
final
GeneratedKeyContext
generatedKeyContext
=
new
GeneratedKeyContext
();
private
final
Collection
<
ShardingColumnContext
>
shardingColumnContexts
=
new
LinkedList
<>();
private
final
Collection
<
ShardingColumnContext
>
shardingColumnContexts
=
new
LinkedList
<>();
private
GeneratedKeyContext
generatedKeyContext
;
private
int
columnsListLastPosition
;
private
int
columnsListLastPosition
;
private
int
valuesListLastPosition
;
private
int
valuesListLastPosition
;
...
@@ -79,25 +79,26 @@ public final class InsertSQLContext extends AbstractSQLContext {
...
@@ -79,25 +79,26 @@ public final class InsertSQLContext extends AbstractSQLContext {
* @return 自增列与主键映射表
* @return 自增列与主键映射表
*/
*/
public
Map
<
String
,
Number
>
generateKeys
(
final
ShardingRule
shardingRule
)
{
public
Map
<
String
,
Number
>
generateKeys
(
final
ShardingRule
shardingRule
)
{
if
(
null
==
generatedKeyContext
)
{
return
Collections
.
emptyMap
();
}
Optional
<
TableRule
>
tableRuleOptional
=
shardingRule
.
tryFindTableRule
(
getTables
().
iterator
().
next
().
getName
());
Optional
<
TableRule
>
tableRuleOptional
=
shardingRule
.
tryFindTableRule
(
getTables
().
iterator
().
next
().
getName
());
if
(!
tableRuleOptional
.
isPresent
())
{
if
(!
tableRuleOptional
.
isPresent
())
{
return
Collections
.
emptyMap
();
return
Collections
.
emptyMap
();
}
}
TableRule
tableRule
=
tableRuleOptional
.
get
();
TableRule
tableRule
=
tableRuleOptional
.
get
();
Map
<
String
,
Number
>
result
=
new
LinkedHashMap
<>(
generatedKeyContext
.
getColumns
().
size
());
Map
<
String
,
Number
>
result
=
new
LinkedHashMap
<>(
1
,
1
);
for
(
String
each
:
generatedKeyContext
.
getColumns
())
{
Number
generatedKey
;
Number
generatedKey
;
if
(
null
!=
tableRule
.
getIdGenerator
())
{
if
(
null
!=
tableRule
.
getIdGenerator
())
{
generatedKey
=
tableRule
.
getIdGenerator
().
generateId
();
generatedKey
=
tableRule
.
getIdGenerator
().
generateId
();
}
else
if
(
null
!=
shardingRule
.
getIdGenerator
())
{
}
else
if
(
null
!=
shardingRule
.
getIdGenerator
())
{
generatedKey
=
shardingRule
.
getIdGenerator
().
generateId
();
generatedKey
=
shardingRule
.
getIdGenerator
().
generateId
();
}
else
{
}
else
{
// TODO 使用default id生成器
// TODO 使用default id生成器
generatedKey
=
null
;
generatedKey
=
null
;
}
result
.
put
(
each
,
generatedKey
);
generatedKeyContext
.
putValue
(
each
,
generatedKey
);
}
}
result
.
put
(
generatedKeyContext
.
getColumn
(),
generatedKey
);
generatedKeyContext
.
putValue
(
generatedKeyContext
.
getColumn
(),
generatedKey
);
return
result
;
return
result
;
}
}
...
@@ -129,18 +130,17 @@ public final class InsertSQLContext extends AbstractSQLContext {
...
@@ -129,18 +130,17 @@ public final class InsertSQLContext extends AbstractSQLContext {
* @param parametersSize 参数个数
* @param parametersSize 参数个数
*/
*/
public
void
appendGenerateKeysToken
(
final
ShardingRule
shardingRule
,
final
int
parametersSize
)
{
public
void
appendGenerateKeysToken
(
final
ShardingRule
shardingRule
,
final
int
parametersSize
)
{
if
(
null
==
generatedKeyContext
)
{
return
;
}
Optional
<
AutoGeneratedKeysToken
>
autoGeneratedKeysToken
=
findAutoGeneratedKeysToken
();
Optional
<
AutoGeneratedKeysToken
>
autoGeneratedKeysToken
=
findAutoGeneratedKeysToken
();
if
(!
autoGeneratedKeysToken
.
isPresent
())
{
if
(!
autoGeneratedKeysToken
.
isPresent
())
{
return
;
return
;
}
}
String
tableName
=
getTables
().
get
(
0
).
getName
();
String
tableName
=
getTables
().
get
(
0
).
getName
();
ItemsToken
valuesToken
=
new
ItemsToken
(
autoGeneratedKeysToken
.
get
().
getBeginPosition
());
ItemsToken
valuesToken
=
new
ItemsToken
(
autoGeneratedKeysToken
.
get
().
getBeginPosition
());
int
offset
=
0
;
valuesToken
.
getItems
().
add
(
"?"
);
for
(
String
each
:
generatedKeyContext
.
getColumns
())
{
addCondition
(
shardingRule
,
new
ShardingColumnContext
(
generatedKeyContext
.
getColumn
(),
tableName
,
true
),
new
SQLPlaceholderExpr
(
parametersSize
));
valuesToken
.
getItems
().
add
(
"?"
);
addCondition
(
shardingRule
,
new
ShardingColumnContext
(
each
,
tableName
,
true
),
new
SQLPlaceholderExpr
(
parametersSize
+
offset
));
offset
++;
}
getSqlTokens
().
remove
(
autoGeneratedKeysToken
.
get
());
getSqlTokens
().
remove
(
autoGeneratedKeysToken
.
get
());
getSqlTokens
().
add
(
valuesToken
);
getSqlTokens
().
add
(
valuesToken
);
}
}
...
...
sharding-jdbc-core/src/main/java/com/dangdang/ddframe/rdb/sharding/parsing/parser/type/insert/AbstractInsertParser.java
浏览文件 @
fe0765d1
...
@@ -23,6 +23,7 @@ import com.dangdang.ddframe.rdb.sharding.parsing.lexer.token.DefaultKeyword;
...
@@ -23,6 +23,7 @@ import com.dangdang.ddframe.rdb.sharding.parsing.lexer.token.DefaultKeyword;
import
com.dangdang.ddframe.rdb.sharding.parsing.lexer.token.Symbol
;
import
com.dangdang.ddframe.rdb.sharding.parsing.lexer.token.Symbol
;
import
com.dangdang.ddframe.rdb.sharding.parsing.lexer.token.TokenType
;
import
com.dangdang.ddframe.rdb.sharding.parsing.lexer.token.TokenType
;
import
com.dangdang.ddframe.rdb.sharding.parsing.parser.context.ConditionContext
;
import
com.dangdang.ddframe.rdb.sharding.parsing.parser.context.ConditionContext
;
import
com.dangdang.ddframe.rdb.sharding.parsing.parser.context.GeneratedKeyContext
;
import
com.dangdang.ddframe.rdb.sharding.parsing.parser.context.InsertSQLContext
;
import
com.dangdang.ddframe.rdb.sharding.parsing.parser.context.InsertSQLContext
;
import
com.dangdang.ddframe.rdb.sharding.parsing.parser.context.ShardingColumnContext
;
import
com.dangdang.ddframe.rdb.sharding.parsing.parser.context.ShardingColumnContext
;
import
com.dangdang.ddframe.rdb.sharding.parsing.parser.exception.SQLParsingUnsupportedException
;
import
com.dangdang.ddframe.rdb.sharding.parsing.parser.exception.SQLParsingUnsupportedException
;
...
@@ -167,7 +168,7 @@ public abstract class AbstractInsertParser implements SQLParser {
...
@@ -167,7 +168,7 @@ public abstract class AbstractInsertParser implements SQLParser {
if
(
autoGeneratedKeyColumn
.
isPresent
())
{
if
(
autoGeneratedKeyColumn
.
isPresent
())
{
if
(!
sqlContext
.
hasColumn
(
autoGeneratedKeyColumn
.
get
()))
{
if
(!
sqlContext
.
hasColumn
(
autoGeneratedKeyColumn
.
get
()))
{
columnsToken
.
getItems
().
add
(
autoGeneratedKeyColumn
.
get
());
columnsToken
.
getItems
().
add
(
autoGeneratedKeyColumn
.
get
());
sqlContext
.
getGeneratedKeyContext
().
getColumns
().
add
(
autoGeneratedKeyColumn
.
get
(
));
sqlContext
.
setGeneratedKeyContext
(
new
GeneratedKeyContext
(
autoGeneratedKeyColumn
.
get
()
));
}
}
}
}
if
(!
columnsToken
.
getItems
().
isEmpty
())
{
if
(!
columnsToken
.
getItems
().
isEmpty
())
{
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录