Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
apache
Shardingsphere
提交
3f461022
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,发现更多精彩内容 >>
提交
3f461022
编写于
9月 01, 2019
作者:
T
terrymanu
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
for #2567, decouple ShardingInsertOptimizeEngine & ShardingInsertColumns
上级
e616ac67
变更
11
隐藏空白更改
内联
并排
Showing
11 changed file
with
54 addition
and
80 deletion
+54
-80
sharding-core/sharding-core-merge/src/test/java/org/apache/shardingsphere/core/merge/MergeEngineFactoryTest.java
...che/shardingsphere/core/merge/MergeEngineFactoryTest.java
+1
-4
sharding-core/sharding-core-optimize/src/main/java/org/apache/shardingsphere/core/optimize/sharding/engnie/dml/ShardingInsertOptimizeEngine.java
...ize/sharding/engnie/dml/ShardingInsertOptimizeEngine.java
+28
-20
sharding-core/sharding-core-optimize/src/main/java/org/apache/shardingsphere/core/optimize/sharding/statement/dml/ShardingInsertOptimizedStatement.java
...rding/statement/dml/ShardingInsertOptimizedStatement.java
+4
-5
sharding-core/sharding-core-optimize/src/test/java/org/apache/shardingsphere/core/optimize/sharding/segment/insert/InsertOptimizedStatementTest.java
...sharding/segment/insert/InsertOptimizedStatementTest.java
+2
-5
sharding-core/sharding-core-rewrite/src/main/java/org/apache/shardingsphere/core/rewrite/SQLRewriteEngine.java
.../apache/shardingsphere/core/rewrite/SQLRewriteEngine.java
+1
-1
sharding-core/sharding-core-rewrite/src/main/java/org/apache/shardingsphere/core/rewrite/token/generator/InsertGeneratedKeyNameTokenGenerator.java
...token/generator/InsertGeneratedKeyNameTokenGenerator.java
+1
-2
sharding-core/sharding-core-rewrite/src/main/java/org/apache/shardingsphere/core/rewrite/token/generator/InsertQueryAndPlainNamesTokenGenerator.java
...ken/generator/InsertQueryAndPlainNamesTokenGenerator.java
+2
-4
sharding-core/sharding-core-rewrite/src/main/java/org/apache/shardingsphere/core/rewrite/token/generator/InsertRegularNamesTokenGenerator.java
...ite/token/generator/InsertRegularNamesTokenGenerator.java
+2
-4
sharding-core/sharding-core-rewrite/src/main/java/org/apache/shardingsphere/core/rewrite/token/generator/InsertSetGeneratedKeyColumnTokenGenerator.java
.../generator/InsertSetGeneratedKeyColumnTokenGenerator.java
+1
-1
sharding-core/sharding-core-rewrite/src/main/java/org/apache/shardingsphere/core/rewrite/token/generator/InsertSetQueryAndPlainColumnsTokenGenerator.java
...enerator/InsertSetQueryAndPlainColumnsTokenGenerator.java
+2
-4
sharding-core/sharding-core-rewrite/src/test/java/org/apache/shardingsphere/core/rewrite/rewriter/ShardingSQLRewriteEngineTest.java
...e/core/rewrite/rewriter/ShardingSQLRewriteEngineTest.java
+10
-30
未找到文件。
sharding-core/sharding-core-merge/src/test/java/org/apache/shardingsphere/core/merge/MergeEngineFactoryTest.java
浏览文件 @
3f461022
...
...
@@ -26,7 +26,6 @@ import org.apache.shardingsphere.core.merge.fixture.TestQueryResult;
import
org.apache.shardingsphere.core.optimize.encrypt.condition.EncryptCondition
;
import
org.apache.shardingsphere.core.optimize.encrypt.statement.EncryptTransparentOptimizedStatement
;
import
org.apache.shardingsphere.core.optimize.sharding.segment.condition.ShardingCondition
;
import
org.apache.shardingsphere.core.optimize.sharding.segment.insert.ShardingInsertColumns
;
import
org.apache.shardingsphere.core.optimize.sharding.segment.select.groupby.GroupBy
;
import
org.apache.shardingsphere.core.optimize.sharding.segment.select.item.SelectItem
;
import
org.apache.shardingsphere.core.optimize.sharding.segment.select.item.SelectItems
;
...
...
@@ -90,9 +89,7 @@ public final class MergeEngineFactoryTest {
@Test
public
void
assertNewInstanceWithOtherStatement
()
throws
SQLException
{
ShardingInsertColumns
insertColumns
=
mock
(
ShardingInsertColumns
.
class
);
when
(
insertColumns
.
getRegularColumnNames
()).
thenReturn
(
Collections
.<
String
>
emptySet
());
SQLRouteResult
routeResult
=
new
SQLRouteResult
(
new
ShardingInsertOptimizedStatement
(
new
InsertStatement
(),
Collections
.<
ShardingCondition
>
emptyList
(),
insertColumns
,
null
),
SQLRouteResult
routeResult
=
new
SQLRouteResult
(
new
ShardingInsertOptimizedStatement
(
new
InsertStatement
(),
Collections
.<
ShardingCondition
>
emptyList
(),
Collections
.<
String
>
emptySet
(),
null
),
new
EncryptTransparentOptimizedStatement
(
new
InsertStatement
()));
assertThat
(
MergeEngineFactory
.
newInstance
(
DatabaseTypes
.
getActualDatabaseType
(
"MySQL"
),
null
,
routeResult
,
null
,
queryResults
),
instanceOf
(
TransparentMergeEngine
.
class
));
}
...
...
sharding-core/sharding-core-optimize/src/main/java/org/apache/shardingsphere/core/optimize/sharding/engnie/dml/ShardingInsertOptimizeEngine.java
浏览文件 @
3f461022
...
...
@@ -25,7 +25,6 @@ import org.apache.shardingsphere.core.optimize.sharding.engnie.ShardingOptimizeE
import
org.apache.shardingsphere.core.optimize.sharding.segment.condition.ShardingCondition
;
import
org.apache.shardingsphere.core.optimize.sharding.segment.condition.engine.InsertClauseShardingConditionEngine
;
import
org.apache.shardingsphere.core.optimize.sharding.segment.insert.GeneratedKey
;
import
org.apache.shardingsphere.core.optimize.sharding.segment.insert.ShardingInsertColumns
;
import
org.apache.shardingsphere.core.optimize.sharding.statement.dml.ShardingInsertOptimizedStatement
;
import
org.apache.shardingsphere.core.parse.sql.segment.dml.column.ColumnSegment
;
import
org.apache.shardingsphere.core.parse.sql.segment.dml.column.OnDuplicateKeyColumnsSegment
;
...
...
@@ -53,42 +52,56 @@ public final class ShardingInsertOptimizeEngine implements ShardingOptimizeEngin
final
TableMetas
tableMetas
,
final
String
sql
,
final
List
<
Object
>
parameters
,
final
InsertStatement
sqlStatement
)
{
InsertClauseShardingConditionEngine
shardingConditionEngine
=
new
InsertClauseShardingConditionEngine
(
shardingRule
);
String
tableName
=
sqlStatement
.
getTable
().
getTableName
();
ShardingInsertColumns
insertColumns
=
new
ShardingInsertColumns
(
shardingRule
,
tableMetas
,
sqlStatement
);
Collection
<
String
>
columnNames
=
new
LinkedHashSet
<>(
sqlStatement
.
useDefaultColumns
()
?
tableMetas
.
getAllColumnNames
(
tableName
)
:
sqlStatement
.
getColumnNames
());
Collection
<
String
>
columnNames
=
sqlStatement
.
useDefaultColumns
()
?
tableMetas
.
getAllColumnNames
(
tableName
)
:
sqlStatement
.
getColumnNames
();
Optional
<
GeneratedKey
>
generatedKey
=
GeneratedKey
.
getGenerateKey
(
shardingRule
,
parameters
,
sqlStatement
,
columnNames
);
Optional
<
String
>
generateKeyColumnName
=
shardingRule
.
findGenerateKeyColumnName
(
tableName
);
boolean
isGeneratedValue
=
generatedKey
.
isPresent
()
&&
generatedKey
.
get
().
isGenerated
();
Iterator
<
Comparable
<?>>
generatedValues
=
isGeneratedValue
?
generatedKey
.
get
().
getGeneratedValues
().
iterator
()
:
null
;
Collection
<
String
>
allColumnNames
=
new
LinkedHashSet
<>(
insertColumns
.
getRegularColumnNames
());
if
(
generatedKey
.
isPresent
())
{
allColumnNames
.
add
(
generatedKey
.
get
().
getColumnName
()
);
Iterator
<
Comparable
<?>>
generatedValues
=
null
;
if
(
isGeneratedValue
)
{
columnNames
.
remove
(
generatedKey
.
get
().
getColumnName
());
generatedValues
=
generatedKey
.
get
().
getGeneratedValues
().
iterator
(
);
}
allColumnNames
.
addAll
(
shardingRule
.
getEncryptRule
().
getAssistedQueryAndPlainColumns
(
sqlStatement
.
getTable
().
getTableName
()));
Collection
<
String
>
derivedColumnNames
=
getDerivedColumnNames
(
shardingRule
,
tableName
,
generatedKey
.
orNull
());
Collection
<
String
>
allColumnNames
=
new
LinkedHashSet
<>(
columnNames
);
allColumnNames
.
addAll
(
derivedColumnNames
);
int
derivedColumnsCount
=
getDerivedColumnsCount
(
shardingRule
,
tableName
,
isGeneratedValue
);
List
<
ShardingCondition
>
shardingConditions
=
shardingConditionEngine
.
createShardingConditions
(
sqlStatement
,
parameters
,
allColumnNames
,
generatedKey
.
orNull
());
ShardingInsertOptimizedStatement
result
=
new
ShardingInsertOptimizedStatement
(
sqlStatement
,
shardingConditions
,
insertColumn
s
,
generatedKey
.
orNull
());
ShardingInsertOptimizedStatement
result
=
new
ShardingInsertOptimizedStatement
(
sqlStatement
,
shardingConditions
,
columnName
s
,
generatedKey
.
orNull
());
checkDuplicateKeyForShardingKey
(
shardingRule
,
sqlStatement
,
tableName
);
int
parametersCount
=
0
;
for
(
Collection
<
ExpressionSegment
>
each
:
sqlStatement
.
getAllValueExpressions
())
{
String
generateKeyColumnName
=
shardingRule
.
findGenerateKeyColumnName
(
sqlStatement
.
getTable
().
getTableName
()).
orNull
();
Collection
<
String
>
encryptDerivedColumnNames
=
shardingRule
.
getEncryptRule
().
getAssistedQueryAndPlainColumns
(
sqlStatement
.
getTable
().
getTableName
());
InsertValue
insertValue
=
result
.
createInsertValue
(
generateKeyColumnName
,
encryptDerivedColumnNames
,
each
,
derivedColumnsCount
,
parameters
,
parametersCount
);
Collection
<
String
>
encryptDerivedColumnNames
=
shardingRule
.
getEncryptRule
().
getAssistedQueryAndPlainColumns
(
tableName
);
InsertValue
insertValue
=
result
.
createInsertValue
(
generateKeyColumnName
.
orNull
(),
encryptDerivedColumnNames
,
each
,
derivedColumnsCount
,
parameters
,
parametersCount
);
result
.
addInsertValue
(
insertValue
);
Object
[]
currentParameters
=
insertValue
.
getParameters
();
if
(
isGeneratedValue
)
{
insertValue
.
appendValue
(
generatedValues
.
next
(),
Arrays
.
asList
(
currentParameters
));
}
if
(
shardingRule
.
getEncryptRule
().
containsQueryAssistedColumn
(
tableName
))
{
fillAssistedQueryInsertValue
(
shardingRule
,
tableName
,
insertColumns
.
getRegularColumnNames
()
,
insertValue
,
Arrays
.
asList
(
currentParameters
));
fillAssistedQueryInsertValue
(
shardingRule
,
tableName
,
columnNames
,
insertValue
,
Arrays
.
asList
(
currentParameters
));
}
if
(
shardingRule
.
getEncryptRule
().
containsPlainColumn
(
tableName
))
{
fillPlainInsertValue
(
shardingRule
,
tableName
,
insertColumns
.
getRegularColumnNames
()
,
insertValue
,
Arrays
.
asList
(
currentParameters
));
fillPlainInsertValue
(
shardingRule
,
tableName
,
columnNames
,
insertValue
,
Arrays
.
asList
(
currentParameters
));
}
parametersCount
+=
insertValue
.
getParametersCount
();
}
return
result
;
}
private
Collection
<
String
>
getDerivedColumnNames
(
final
ShardingRule
shardingRule
,
final
String
tableName
,
final
GeneratedKey
generatedKey
)
{
Collection
<
String
>
result
=
new
LinkedHashSet
<>();
if
(
null
!=
generatedKey
)
{
result
.
add
(
generatedKey
.
getColumnName
());
}
result
.
addAll
(
shardingRule
.
getEncryptRule
().
getAssistedQueryAndPlainColumns
(
tableName
));
return
result
;
}
private
int
getDerivedColumnsCount
(
final
ShardingRule
shardingRule
,
final
String
tableName
,
final
boolean
isGeneratedValue
)
{
int
assistedQueryAndPlainColumnsCount
=
shardingRule
.
getEncryptRule
().
getAssistedQueryAndPlainColumnCount
(
tableName
);
return
isGeneratedValue
?
assistedQueryAndPlainColumnsCount
+
1
:
assistedQueryAndPlainColumnsCount
;
}
private
void
checkDuplicateKeyForShardingKey
(
final
ShardingRule
shardingRule
,
final
InsertStatement
sqlStatement
,
final
String
tableName
)
{
Optional
<
OnDuplicateKeyColumnsSegment
>
onDuplicateKeyColumnsSegment
=
sqlStatement
.
findSQLSegment
(
OnDuplicateKeyColumnsSegment
.
class
);
if
(
onDuplicateKeyColumnsSegment
.
isPresent
()
&&
isUpdateShardingKey
(
shardingRule
,
onDuplicateKeyColumnsSegment
.
get
(),
tableName
))
{
...
...
@@ -105,11 +118,6 @@ public final class ShardingInsertOptimizeEngine implements ShardingOptimizeEngin
return
false
;
}
private
int
getDerivedColumnsCount
(
final
ShardingRule
shardingRule
,
final
String
tableName
,
final
boolean
isGeneratedValue
)
{
int
assistedQueryAndPlainColumnsCount
=
shardingRule
.
getEncryptRule
().
getAssistedQueryAndPlainColumnCount
(
tableName
);
return
isGeneratedValue
?
assistedQueryAndPlainColumnsCount
+
1
:
assistedQueryAndPlainColumnsCount
;
}
private
void
fillAssistedQueryInsertValue
(
final
ShardingRule
shardingRule
,
final
String
tableName
,
final
Collection
<
String
>
columnNames
,
final
InsertValue
insertValue
,
final
List
<
Object
>
parameters
)
{
for
(
String
each
:
columnNames
)
{
...
...
sharding-core/sharding-core-optimize/src/main/java/org/apache/shardingsphere/core/optimize/sharding/statement/dml/ShardingInsertOptimizedStatement.java
浏览文件 @
3f461022
...
...
@@ -28,7 +28,6 @@ import org.apache.shardingsphere.core.optimize.encrypt.condition.EncryptConditio
import
org.apache.shardingsphere.core.optimize.sharding.segment.condition.ShardingCondition
;
import
org.apache.shardingsphere.core.optimize.sharding.segment.condition.ShardingConditions
;
import
org.apache.shardingsphere.core.optimize.sharding.segment.insert.GeneratedKey
;
import
org.apache.shardingsphere.core.optimize.sharding.segment.insert.ShardingInsertColumns
;
import
org.apache.shardingsphere.core.parse.sql.segment.dml.expr.ExpressionSegment
;
import
org.apache.shardingsphere.core.parse.sql.statement.SQLStatement
;
...
...
@@ -48,17 +47,17 @@ public final class ShardingInsertOptimizedStatement extends ShardingConditionOpt
private
final
Tables
tables
;
private
final
ShardingInsertColumns
insertColumn
s
;
private
final
Collection
<
String
>
columnName
s
;
private
final
GeneratedKey
generatedKey
;
private
final
List
<
InsertValue
>
insertValues
=
new
LinkedList
<>();
public
ShardingInsertOptimizedStatement
(
final
SQLStatement
sqlStatement
,
final
List
<
ShardingCondition
>
shardingConditions
,
final
ShardingInsertColumns
insertColumn
s
,
final
GeneratedKey
generatedKey
)
{
final
List
<
ShardingCondition
>
shardingConditions
,
final
Collection
<
String
>
columnName
s
,
final
GeneratedKey
generatedKey
)
{
super
(
sqlStatement
,
new
ShardingConditions
(
shardingConditions
),
new
EncryptConditions
(
Collections
.<
EncryptCondition
>
emptyList
()));
tables
=
new
Tables
(
sqlStatement
);
this
.
insertColumns
=
insertColumn
s
;
this
.
columnNames
=
columnName
s
;
this
.
generatedKey
=
generatedKey
;
}
...
...
@@ -75,7 +74,7 @@ public final class ShardingInsertOptimizedStatement extends ShardingConditionOpt
*/
public
InsertValue
createInsertValue
(
final
String
generateKeyColumnName
,
final
Collection
<
String
>
derivedColumnNames
,
final
Collection
<
ExpressionSegment
>
assignments
,
final
int
derivedColumnsCount
,
final
List
<
Object
>
parameters
,
final
int
startIndexOfAppendedParameters
)
{
List
<
String
>
allColumnNames
=
new
LinkedList
<>(
insertColumns
.
getRegularColumnNames
()
);
List
<
String
>
allColumnNames
=
new
LinkedList
<>(
columnNames
);
allColumnNames
.
add
(
generateKeyColumnName
);
allColumnNames
.
addAll
(
derivedColumnNames
);
return
new
InsertValue
(
allColumnNames
,
assignments
,
derivedColumnsCount
,
parameters
,
startIndexOfAppendedParameters
);
...
...
sharding-core/sharding-core-optimize/src/test/java/org/apache/shardingsphere/core/optimize/sharding/segment/insert/InsertOptimizedStatementTest.java
浏览文件 @
3f461022
...
...
@@ -34,16 +34,13 @@ import java.util.List;
import
static
org
.
hamcrest
.
CoreMatchers
.
is
;
import
static
org
.
junit
.
Assert
.
assertNull
;
import
static
org
.
junit
.
Assert
.
assertThat
;
import
static
org
.
mockito
.
Mockito
.
mock
;
import
static
org
.
mockito
.
Mockito
.
when
;
public
final
class
InsertOptimizedStatementTest
{
@Test
public
void
assertAddInsertValueWithSet
()
{
ShardingInsertColumns
insertColumns
=
mock
(
ShardingInsertColumns
.
class
);
when
(
insertColumns
.
getRegularColumnNames
()).
thenReturn
(
Arrays
.
asList
(
"id"
,
"value"
,
"status"
));
ShardingInsertOptimizedStatement
insertClauseOptimizedStatement
=
new
ShardingInsertOptimizedStatement
(
new
InsertStatement
(),
Collections
.<
ShardingCondition
>
emptyList
(),
insertColumns
,
null
);
ShardingInsertOptimizedStatement
insertClauseOptimizedStatement
=
new
ShardingInsertOptimizedStatement
(
new
InsertStatement
(),
Collections
.<
ShardingCondition
>
emptyList
(),
Arrays
.
asList
(
"id"
,
"value"
,
"status"
),
null
);
ExpressionSegment
assignment1
=
new
LiteralExpressionSegment
(
0
,
0
,
1
);
ExpressionSegment
assignment2
=
new
ParameterMarkerExpressionSegment
(
0
,
0
,
1
);
ExpressionSegment
assignment3
=
new
LiteralExpressionSegment
(
0
,
0
,
"test"
);
...
...
sharding-core/sharding-core-rewrite/src/main/java/org/apache/shardingsphere/core/rewrite/SQLRewriteEngine.java
浏览文件 @
3f461022
...
...
@@ -108,7 +108,7 @@ public final class SQLRewriteEngine {
private
void
encryptInsertOptimizedStatement
(
final
EncryptRule
encryptRule
,
final
ShardingInsertOptimizedStatement
insertOptimizedStatement
)
{
for
(
InsertValue
insertValue
:
insertOptimizedStatement
.
getInsertValues
())
{
for
(
String
each
:
insertOptimizedStatement
.
get
InsertColumns
().
getRegular
ColumnNames
())
{
for
(
String
each
:
insertOptimizedStatement
.
getColumnNames
())
{
encryptInsertValue
(
encryptRule
,
insertValue
,
insertOptimizedStatement
.
getTables
().
getSingleTableName
(),
each
);
}
}
...
...
sharding-core/sharding-core-rewrite/src/main/java/org/apache/shardingsphere/core/rewrite/token/generator/InsertGeneratedKeyNameTokenGenerator.java
浏览文件 @
3f461022
...
...
@@ -46,7 +46,7 @@ public final class InsertGeneratedKeyNameTokenGenerator implements OptionalSQLTo
final
ShardingInsertOptimizedStatement
optimizedStatement
,
final
InsertColumnsSegment
segment
,
final
ShardingRule
shardingRule
)
{
String
tableName
=
optimizedStatement
.
getTables
().
getSingleTableName
();
Optional
<
String
>
generatedKeyColumnName
=
shardingRule
.
findGenerateKeyColumnName
(
tableName
);
return
generatedKeyColumnName
.
isPresent
()
&&
!
optimizedStatement
.
get
InsertColumns
().
getRegular
ColumnNames
().
contains
(
generatedKeyColumnName
.
get
())
return
generatedKeyColumnName
.
isPresent
()
&&
!
optimizedStatement
.
getColumnNames
().
contains
(
generatedKeyColumnName
.
get
())
?
Optional
.
of
(
new
InsertGeneratedKeyNameToken
(
segment
.
getStopIndex
(),
generatedKeyColumnName
.
get
(),
isToAddCloseParenthesis
(
tableName
,
segment
,
shardingRule
)))
:
Optional
.<
InsertGeneratedKeyNameToken
>
absent
();
}
...
...
@@ -54,5 +54,4 @@ public final class InsertGeneratedKeyNameTokenGenerator implements OptionalSQLTo
private
boolean
isToAddCloseParenthesis
(
final
String
tableName
,
final
InsertColumnsSegment
segment
,
final
ShardingRule
shardingRule
)
{
return
segment
.
getColumns
().
isEmpty
()
&&
0
==
shardingRule
.
getEncryptRule
().
getAssistedQueryAndPlainColumnCount
(
tableName
);
}
}
sharding-core/sharding-core-rewrite/src/main/java/org/apache/shardingsphere/core/rewrite/token/generator/InsertQueryAndPlainNamesTokenGenerator.java
浏览文件 @
3f461022
...
...
@@ -72,8 +72,7 @@ public final class InsertQueryAndPlainNamesTokenGenerator implements OptionalSQL
private
Collection
<
String
>
getAssistedQueryColumns
(
final
InsertOptimizedStatement
optimizedStatement
,
final
EncryptRule
encryptRule
)
{
Collection
<
String
>
result
=
new
LinkedList
<>();
Collection
<
String
>
columnNames
=
optimizedStatement
instanceof
ShardingInsertOptimizedStatement
?
((
ShardingInsertOptimizedStatement
)
optimizedStatement
).
getInsertColumns
().
getRegularColumnNames
()
:
((
EncryptInsertOptimizedStatement
)
optimizedStatement
).
getColumnNames
();
?
((
ShardingInsertOptimizedStatement
)
optimizedStatement
).
getColumnNames
()
:
((
EncryptInsertOptimizedStatement
)
optimizedStatement
).
getColumnNames
();
for
(
String
each
:
columnNames
)
{
Optional
<
String
>
assistedQueryColumn
=
encryptRule
.
getAssistedQueryColumn
(
optimizedStatement
.
getTables
().
getSingleTableName
(),
each
);
if
(
assistedQueryColumn
.
isPresent
())
{
...
...
@@ -86,8 +85,7 @@ public final class InsertQueryAndPlainNamesTokenGenerator implements OptionalSQL
private
Collection
<
String
>
getPlainColumns
(
final
InsertOptimizedStatement
optimizedStatement
,
final
EncryptRule
encryptRule
)
{
Collection
<
String
>
result
=
new
LinkedList
<>();
Collection
<
String
>
columnNames
=
optimizedStatement
instanceof
ShardingInsertOptimizedStatement
?
((
ShardingInsertOptimizedStatement
)
optimizedStatement
).
getInsertColumns
().
getRegularColumnNames
()
:
((
EncryptInsertOptimizedStatement
)
optimizedStatement
).
getColumnNames
();
?
((
ShardingInsertOptimizedStatement
)
optimizedStatement
).
getColumnNames
()
:
((
EncryptInsertOptimizedStatement
)
optimizedStatement
).
getColumnNames
();
for
(
String
each
:
columnNames
)
{
Optional
<
String
>
plainColumn
=
encryptRule
.
getPlainColumn
(
optimizedStatement
.
getTables
().
getSingleTableName
(),
each
);
if
(
plainColumn
.
isPresent
())
{
...
...
sharding-core/sharding-core-rewrite/src/main/java/org/apache/shardingsphere/core/rewrite/token/generator/InsertRegularNamesTokenGenerator.java
浏览文件 @
3f461022
...
...
@@ -68,8 +68,7 @@ public final class InsertRegularNamesTokenGenerator implements OptionalSQLTokenG
Collection
<
String
>
result
=
new
LinkedList
<>();
Map
<
String
,
String
>
logicAndCipherColumns
=
getEncryptRule
(
baseRule
).
getLogicAndCipherColumns
(
optimizedStatement
.
getTables
().
getSingleTableName
());
Collection
<
String
>
columnNames
=
optimizedStatement
instanceof
ShardingInsertOptimizedStatement
?
((
ShardingInsertOptimizedStatement
)
optimizedStatement
).
getInsertColumns
().
getRegularColumnNames
()
:
((
EncryptInsertOptimizedStatement
)
optimizedStatement
).
getColumnNames
();
?
((
ShardingInsertOptimizedStatement
)
optimizedStatement
).
getColumnNames
()
:
((
EncryptInsertOptimizedStatement
)
optimizedStatement
).
getColumnNames
();
for
(
String
each
:
columnNames
)
{
result
.
add
(
getCipherColumn
(
each
,
logicAndCipherColumns
));
}
...
...
@@ -96,8 +95,7 @@ public final class InsertRegularNamesTokenGenerator implements OptionalSQLTokenG
private
boolean
isNeedToAppendGeneratedKey
(
final
InsertOptimizedStatement
optimizedStatement
,
final
ShardingRule
shardingRule
)
{
Optional
<
String
>
generateKeyColumnName
=
shardingRule
.
findGenerateKeyColumnName
(
optimizedStatement
.
getTables
().
getSingleTableName
());
Collection
<
String
>
columnNames
=
optimizedStatement
instanceof
ShardingInsertOptimizedStatement
?
((
ShardingInsertOptimizedStatement
)
optimizedStatement
).
getInsertColumns
().
getRegularColumnNames
()
:
((
EncryptInsertOptimizedStatement
)
optimizedStatement
).
getColumnNames
();
?
((
ShardingInsertOptimizedStatement
)
optimizedStatement
).
getColumnNames
()
:
((
EncryptInsertOptimizedStatement
)
optimizedStatement
).
getColumnNames
();
return
generateKeyColumnName
.
isPresent
()
&&
!
columnNames
.
contains
(
generateKeyColumnName
.
get
());
}
...
...
sharding-core/sharding-core-rewrite/src/main/java/org/apache/shardingsphere/core/rewrite/token/generator/InsertSetGeneratedKeyColumnTokenGenerator.java
浏览文件 @
3f461022
...
...
@@ -64,6 +64,6 @@ public final class InsertSetGeneratedKeyColumnTokenGenerator implements Optional
private
Optional
<
String
>
getGeneratedKeyColumn
(
final
ShardingInsertOptimizedStatement
optimizedStatement
,
final
ShardingRule
shardingRule
)
{
Optional
<
String
>
generateKeyColumn
=
shardingRule
.
findGenerateKeyColumnName
(
optimizedStatement
.
getTables
().
getSingleTableName
());
return
generateKeyColumn
.
isPresent
()
&&
!
optimizedStatement
.
get
InsertColumns
().
getRegular
ColumnNames
().
contains
(
generateKeyColumn
.
get
())
?
generateKeyColumn
:
Optional
.<
String
>
absent
();
return
generateKeyColumn
.
isPresent
()
&&
!
optimizedStatement
.
getColumnNames
().
contains
(
generateKeyColumn
.
get
())
?
generateKeyColumn
:
Optional
.<
String
>
absent
();
}
}
sharding-core/sharding-core-rewrite/src/main/java/org/apache/shardingsphere/core/rewrite/token/generator/InsertSetQueryAndPlainColumnsTokenGenerator.java
浏览文件 @
3f461022
...
...
@@ -75,8 +75,7 @@ public final class InsertSetQueryAndPlainColumnsTokenGenerator implements Option
private
List
<
String
>
getAssistedQueryColumnNames
(
final
InsertOptimizedStatement
optimizedStatement
,
final
EncryptRule
encryptRule
)
{
List
<
String
>
result
=
new
LinkedList
<>();
Collection
<
String
>
columnNames
=
optimizedStatement
instanceof
ShardingInsertOptimizedStatement
?
((
ShardingInsertOptimizedStatement
)
optimizedStatement
).
getInsertColumns
().
getRegularColumnNames
()
:
((
EncryptInsertOptimizedStatement
)
optimizedStatement
).
getColumnNames
();
?
((
ShardingInsertOptimizedStatement
)
optimizedStatement
).
getColumnNames
()
:
((
EncryptInsertOptimizedStatement
)
optimizedStatement
).
getColumnNames
();
for
(
String
each
:
columnNames
)
{
Optional
<
String
>
assistedQueryColumn
=
encryptRule
.
getAssistedQueryColumn
(
optimizedStatement
.
getTables
().
getSingleTableName
(),
each
);
if
(
assistedQueryColumn
.
isPresent
())
{
...
...
@@ -89,8 +88,7 @@ public final class InsertSetQueryAndPlainColumnsTokenGenerator implements Option
private
List
<
String
>
getPlainColumnNames
(
final
InsertOptimizedStatement
optimizedStatement
,
final
EncryptRule
encryptRule
)
{
List
<
String
>
result
=
new
LinkedList
<>();
Collection
<
String
>
columnNames
=
optimizedStatement
instanceof
ShardingInsertOptimizedStatement
?
((
ShardingInsertOptimizedStatement
)
optimizedStatement
).
getInsertColumns
().
getRegularColumnNames
()
:
((
EncryptInsertOptimizedStatement
)
optimizedStatement
).
getColumnNames
();
?
((
ShardingInsertOptimizedStatement
)
optimizedStatement
).
getColumnNames
()
:
((
EncryptInsertOptimizedStatement
)
optimizedStatement
).
getColumnNames
();
for
(
String
each
:
columnNames
)
{
Optional
<
String
>
plainColumn
=
encryptRule
.
getPlainColumn
(
optimizedStatement
.
getTables
().
getSingleTableName
(),
each
);
if
(
plainColumn
.
isPresent
())
{
...
...
sharding-core/sharding-core-rewrite/src/test/java/org/apache/shardingsphere/core/rewrite/rewriter/ShardingSQLRewriteEngineTest.java
浏览文件 @
3f461022
...
...
@@ -25,7 +25,6 @@ import org.apache.shardingsphere.core.optimize.encrypt.condition.EncryptConditio
import
org.apache.shardingsphere.core.optimize.encrypt.statement.EncryptTransparentOptimizedStatement
;
import
org.apache.shardingsphere.core.optimize.sharding.segment.condition.ShardingCondition
;
import
org.apache.shardingsphere.core.optimize.sharding.segment.condition.ShardingConditions
;
import
org.apache.shardingsphere.core.optimize.sharding.segment.insert.ShardingInsertColumns
;
import
org.apache.shardingsphere.core.optimize.sharding.segment.select.groupby.GroupBy
;
import
org.apache.shardingsphere.core.optimize.sharding.segment.select.item.AggregationDistinctSelectItem
;
import
org.apache.shardingsphere.core.optimize.sharding.segment.select.item.AggregationSelectItem
;
...
...
@@ -93,7 +92,6 @@ import java.util.Map;
import
static
org
.
hamcrest
.
CoreMatchers
.
is
;
import
static
org
.
junit
.
Assert
.
assertThat
;
import
static
org
.
mockito
.
Mockito
.
mock
;
import
static
org
.
mockito
.
Mockito
.
when
;
public
final
class
ShardingSQLRewriteEngineTest
{
...
...
@@ -220,9 +218,7 @@ public final class ShardingSQLRewriteEngineTest {
insertStatement
.
getAllSQLSegments
().
add
(
new
InsertColumnsSegment
(
30
,
30
,
Collections
.
singleton
(
mock
(
ColumnSegment
.
class
))));
insertStatement
.
getAllSQLSegments
().
add
(
new
InsertValuesSegment
(
39
,
44
,
Collections
.<
ExpressionSegment
>
emptyList
()));
insertStatement
.
getAllSQLSegments
().
add
(
new
TableSegment
(
12
,
18
,
"table_x"
));
ShardingInsertColumns
insertColumns
=
mock
(
ShardingInsertColumns
.
class
);
when
(
insertColumns
.
getRegularColumnNames
()).
thenReturn
(
Arrays
.
asList
(
"name"
,
"age"
));
ShardingInsertOptimizedStatement
shardingStatement
=
new
ShardingInsertOptimizedStatement
(
insertStatement
,
Collections
.<
ShardingCondition
>
emptyList
(),
insertColumns
,
null
);
ShardingInsertOptimizedStatement
shardingStatement
=
new
ShardingInsertOptimizedStatement
(
insertStatement
,
Collections
.<
ShardingCondition
>
emptyList
(),
Arrays
.
asList
(
"name"
,
"age"
),
null
);
Collection
<
ExpressionSegment
>
assignments
=
Arrays
.<
ExpressionSegment
>
asList
(
new
ParameterMarkerExpressionSegment
(
0
,
0
,
0
),
new
ParameterMarkerExpressionSegment
(
0
,
0
,
1
),
new
ParameterMarkerExpressionSegment
(
0
,
0
,
2
));
List
<
Object
>
parameters
=
Arrays
.<
Object
>
asList
(
"x"
,
1
,
1
);
...
...
@@ -248,9 +244,7 @@ public final class ShardingSQLRewriteEngineTest {
insertStatement
.
getAllSQLSegments
().
add
(
new
InsertValuesSegment
(
29
,
31
,
Collections
.<
ExpressionSegment
>
emptyList
()));
insertStatement
.
setTable
(
new
TableSegment
(
0
,
0
,
"table_x"
));
insertStatement
.
getAllSQLSegments
().
add
(
new
TableSegment
(
12
,
20
,
"`table_x`"
));
ShardingInsertColumns
insertColumns
=
mock
(
ShardingInsertColumns
.
class
);
when
(
insertColumns
.
getRegularColumnNames
()).
thenReturn
(
Collections
.
singletonList
(
"name"
));
ShardingInsertOptimizedStatement
shardingStatement
=
new
ShardingInsertOptimizedStatement
(
insertStatement
,
Collections
.<
ShardingCondition
>
emptyList
(),
insertColumns
,
null
);
ShardingInsertOptimizedStatement
shardingStatement
=
new
ShardingInsertOptimizedStatement
(
insertStatement
,
Collections
.<
ShardingCondition
>
emptyList
(),
Collections
.
singletonList
(
"name"
),
null
);
Collection
<
ExpressionSegment
>
assignments
=
Arrays
.<
ExpressionSegment
>
asList
(
new
ParameterMarkerExpressionSegment
(
0
,
0
,
0
),
new
ParameterMarkerExpressionSegment
(
0
,
0
,
1
));
List
<
Object
>
parameters
=
Arrays
.<
Object
>
asList
(
"Bill"
,
1
);
InsertValue
insertValue
=
shardingStatement
.
createInsertValue
(
"id"
,
Collections
.<
String
>
emptyList
(),
assignments
,
0
,
parameters
,
0
);
...
...
@@ -275,9 +269,7 @@ public final class ShardingSQLRewriteEngineTest {
insertStatement
.
getAllSQLSegments
().
add
(
new
InsertValuesSegment
(
29
,
32
,
Collections
.<
ExpressionSegment
>
emptyList
()));
insertStatement
.
setTable
(
new
TableSegment
(
0
,
0
,
"table_x"
));
insertStatement
.
getAllSQLSegments
().
add
(
new
TableSegment
(
12
,
20
,
"`table_x`"
));
ShardingInsertColumns
insertColumns
=
mock
(
ShardingInsertColumns
.
class
);
when
(
insertColumns
.
getRegularColumnNames
()).
thenReturn
(
Collections
.
singletonList
(
"name"
));
ShardingInsertOptimizedStatement
shardingStatement
=
new
ShardingInsertOptimizedStatement
(
insertStatement
,
Collections
.<
ShardingCondition
>
emptyList
(),
insertColumns
,
null
);
ShardingInsertOptimizedStatement
shardingStatement
=
new
ShardingInsertOptimizedStatement
(
insertStatement
,
Collections
.<
ShardingCondition
>
emptyList
(),
Collections
.
singletonList
(
"name"
),
null
);
Collection
<
ExpressionSegment
>
assignments
=
Arrays
.<
ExpressionSegment
>
asList
(
new
LiteralExpressionSegment
(
0
,
0
,
10
),
new
LiteralExpressionSegment
(
0
,
0
,
1
));
List
<
Object
>
parameters
=
Collections
.
emptyList
();
InsertValue
insertValue
=
shardingStatement
.
createInsertValue
(
"id"
,
Collections
.<
String
>
emptyList
(),
assignments
,
0
,
parameters
,
0
);
...
...
@@ -302,9 +294,7 @@ public final class ShardingSQLRewriteEngineTest {
insertStatement
.
getAllSQLSegments
().
add
(
new
InsertValuesSegment
(
29
,
32
,
Collections
.<
ExpressionSegment
>
emptyList
()));
insertStatement
.
setTable
(
new
TableSegment
(
0
,
0
,
"table_x"
));
insertStatement
.
getAllSQLSegments
().
add
(
new
TableSegment
(
12
,
20
,
"`table_x`"
));
ShardingInsertColumns
insertColumns
=
mock
(
ShardingInsertColumns
.
class
);
when
(
insertColumns
.
getRegularColumnNames
()).
thenReturn
(
Collections
.
singletonList
(
"name"
));
ShardingInsertOptimizedStatement
shardingStatement
=
new
ShardingInsertOptimizedStatement
(
insertStatement
,
Collections
.<
ShardingCondition
>
emptyList
(),
insertColumns
,
null
);
ShardingInsertOptimizedStatement
shardingStatement
=
new
ShardingInsertOptimizedStatement
(
insertStatement
,
Collections
.<
ShardingCondition
>
emptyList
(),
Collections
.
singletonList
(
"name"
),
null
);
Collection
<
ExpressionSegment
>
assignments
=
Arrays
.<
ExpressionSegment
>
asList
(
new
LiteralExpressionSegment
(
0
,
0
,
10
),
new
LiteralExpressionSegment
(
0
,
0
,
1
));
List
<
Object
>
parameters
=
Collections
.
emptyList
();
InsertValue
insertValue
=
shardingStatement
.
createInsertValue
(
"id"
,
Collections
.<
String
>
emptyList
(),
assignments
,
0
,
parameters
,
0
);
...
...
@@ -328,9 +318,7 @@ public final class ShardingSQLRewriteEngineTest {
insertStatement
.
getColumns
().
add
(
new
ColumnSegment
(
0
,
0
,
"id"
));
insertStatement
.
setTable
(
new
TableSegment
(
0
,
0
,
"table_x"
));
insertStatement
.
getAllSQLSegments
().
add
(
new
TableSegment
(
12
,
20
,
"`table_x`"
));
ShardingInsertColumns
insertColumns
=
mock
(
ShardingInsertColumns
.
class
);
when
(
insertColumns
.
getRegularColumnNames
()).
thenReturn
(
Arrays
.
asList
(
"name"
,
"id"
));
ShardingInsertOptimizedStatement
shardingStatement
=
new
ShardingInsertOptimizedStatement
(
insertStatement
,
Collections
.<
ShardingCondition
>
emptyList
(),
insertColumns
,
null
);
ShardingInsertOptimizedStatement
shardingStatement
=
new
ShardingInsertOptimizedStatement
(
insertStatement
,
Collections
.<
ShardingCondition
>
emptyList
(),
Arrays
.
asList
(
"name"
,
"id"
),
null
);
Collection
<
ExpressionSegment
>
assignments
=
Arrays
.<
ExpressionSegment
>
asList
(
new
LiteralExpressionSegment
(
0
,
0
,
10
),
new
LiteralExpressionSegment
(
0
,
0
,
1
));
List
<
Object
>
parameters
=
Collections
.
emptyList
();
InsertValue
insertValue
=
shardingStatement
.
createInsertValue
(
"id"
,
Collections
.<
String
>
emptyList
(),
assignments
,
1
,
parameters
,
0
);
...
...
@@ -355,9 +343,7 @@ public final class ShardingSQLRewriteEngineTest {
insertStatement
.
getAllSQLSegments
().
add
(
new
TableSegment
(
12
,
20
,
"`table_x`"
));
insertStatement
.
getAllSQLSegments
().
add
(
new
InsertColumnsSegment
(
21
,
21
,
Collections
.<
ColumnSegment
>
emptyList
()));
insertStatement
.
getAllSQLSegments
().
add
(
new
InsertValuesSegment
(
29
,
35
,
Collections
.<
ExpressionSegment
>
emptyList
()));
ShardingInsertColumns
insertColumns
=
mock
(
ShardingInsertColumns
.
class
);
when
(
insertColumns
.
getRegularColumnNames
()).
thenReturn
(
Arrays
.
asList
(
"name"
,
"id"
));
ShardingInsertOptimizedStatement
shardingStatement
=
new
ShardingInsertOptimizedStatement
(
insertStatement
,
Collections
.<
ShardingCondition
>
emptyList
(),
insertColumns
,
null
);
ShardingInsertOptimizedStatement
shardingStatement
=
new
ShardingInsertOptimizedStatement
(
insertStatement
,
Collections
.<
ShardingCondition
>
emptyList
(),
Arrays
.
asList
(
"name"
,
"id"
),
null
);
Collection
<
ExpressionSegment
>
assignments
=
Arrays
.<
ExpressionSegment
>
asList
(
new
LiteralExpressionSegment
(
0
,
0
,
10
),
new
LiteralExpressionSegment
(
0
,
0
,
1
));
List
<
Object
>
parameters
=
Collections
.
emptyList
();
InsertValue
insertValue
=
shardingStatement
.
createInsertValue
(
"id"
,
Collections
.<
String
>
emptyList
(),
assignments
,
0
,
parameters
,
0
);
...
...
@@ -381,9 +367,7 @@ public final class ShardingSQLRewriteEngineTest {
insertStatement
.
getAllSQLSegments
().
add
(
new
InsertValuesSegment
(
29
,
34
,
Collections
.<
ExpressionSegment
>
emptyList
()));
insertStatement
.
setTable
(
new
TableSegment
(
0
,
0
,
"table_x"
));
insertStatement
.
getAllSQLSegments
().
add
(
new
TableSegment
(
12
,
20
,
"`table_x`"
));
ShardingInsertColumns
insertColumns
=
mock
(
ShardingInsertColumns
.
class
);
when
(
insertColumns
.
getRegularColumnNames
()).
thenReturn
(
Collections
.
singletonList
(
"name"
));
ShardingInsertOptimizedStatement
shardingStatement
=
new
ShardingInsertOptimizedStatement
(
insertStatement
,
Collections
.<
ShardingCondition
>
emptyList
(),
insertColumns
,
null
);
ShardingInsertOptimizedStatement
shardingStatement
=
new
ShardingInsertOptimizedStatement
(
insertStatement
,
Collections
.<
ShardingCondition
>
emptyList
(),
Collections
.
singletonList
(
"name"
),
null
);
Collection
<
ExpressionSegment
>
assignments
=
Arrays
.<
ExpressionSegment
>
asList
(
new
ParameterMarkerExpressionSegment
(
0
,
0
,
0
),
new
ParameterMarkerExpressionSegment
(
0
,
0
,
1
));
List
<
Object
>
parameters
=
Arrays
.<
Object
>
asList
(
"x"
,
1
);
InsertValue
insertValue
=
shardingStatement
.
createInsertValue
(
"id"
,
Collections
.<
String
>
emptyList
(),
assignments
,
0
,
parameters
,
0
);
...
...
@@ -850,10 +834,8 @@ public final class ShardingSQLRewriteEngineTest {
private
SQLRouteResult
createSQLRouteResultForTableTokenWithSchemaForInsert
()
{
InsertStatement
insertStatement
=
new
InsertStatement
();
insertStatement
.
getAllSQLSegments
().
add
(
new
TableSegment
(
12
,
30
,
"table_x"
));
ShardingInsertColumns
insertColumns
=
mock
(
ShardingInsertColumns
.
class
);
when
(
insertColumns
.
getRegularColumnNames
()).
thenReturn
(
Arrays
.
asList
(
"order_id"
,
"user_id"
,
"status"
,
"id"
));
SQLRouteResult
result
=
new
SQLRouteResult
(
new
ShardingInsertOptimizedStatement
(
insertStatement
,
Collections
.<
ShardingCondition
>
emptyList
(),
insertColumns
,
null
),
new
EncryptTransparentOptimizedStatement
(
insertStatement
));
SQLRouteResult
result
=
new
SQLRouteResult
(
new
ShardingInsertOptimizedStatement
(
insertStatement
,
Collections
.<
ShardingCondition
>
emptyList
(),
Arrays
.
asList
(
"order_id"
,
"user_id"
,
"status"
,
"id"
),
null
),
new
EncryptTransparentOptimizedStatement
(
insertStatement
));
result
.
setRoutingResult
(
new
RoutingResult
());
return
result
;
}
...
...
@@ -989,9 +971,7 @@ public final class ShardingSQLRewriteEngineTest {
insertStatement
.
getAllSQLSegments
().
add
(
new
SetAssignmentsSegment
(
26
,
34
,
Collections
.
singleton
(
new
AssignmentSegment
(
26
,
34
,
columnSegment
,
expressionSegment
))));
insertStatement
.
getAllSQLSegments
().
add
(
new
TableSegment
(
12
,
20
,
"`table_w`"
));
insertStatement
.
setTable
(
new
TableSegment
(
0
,
0
,
"table_w"
));
ShardingInsertColumns
insertColumns
=
mock
(
ShardingInsertColumns
.
class
);
when
(
insertColumns
.
getRegularColumnNames
()).
thenReturn
(
Collections
.
singletonList
(
"name"
));
ShardingInsertOptimizedStatement
shardingStatement
=
new
ShardingInsertOptimizedStatement
(
insertStatement
,
Collections
.<
ShardingCondition
>
emptyList
(),
insertColumns
,
null
);
ShardingInsertOptimizedStatement
shardingStatement
=
new
ShardingInsertOptimizedStatement
(
insertStatement
,
Collections
.<
ShardingCondition
>
emptyList
(),
Collections
.
singletonList
(
"name"
),
null
);
Collection
<
ExpressionSegment
>
assignments
=
Arrays
.<
ExpressionSegment
>
asList
(
new
LiteralExpressionSegment
(
0
,
0
,
10
),
new
LiteralExpressionSegment
(
0
,
0
,
1
),
new
LiteralExpressionSegment
(
0
,
0
,
1
),
new
LiteralExpressionSegment
(
0
,
0
,
10
));
List
<
Object
>
parameters
=
Collections
.
emptyList
();
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录