Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
apache
Shardingsphere
提交
f301bdd2
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,发现更多精彩内容 >>
未验证
提交
f301bdd2
编写于
9月 03, 2019
作者:
P
panjuan
提交者:
GitHub
9月 03, 2019
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #2934 from betterjava/bug-2784
hide assistedQueryColumn and show logicColumn when desc EncryptTable
上级
687d9393
cde5faf1
变更
6
隐藏空白更改
内联
并排
Showing
6 changed file
with
453 addition
and
7 deletion
+453
-7
sharding-core/sharding-core-merge/src/main/java/org/apache/shardingsphere/core/merge/MergeEngineFactory.java
.../apache/shardingsphere/core/merge/MergeEngineFactory.java
+1
-1
sharding-core/sharding-core-merge/src/main/java/org/apache/shardingsphere/core/merge/dal/DALMergeEngine.java
.../apache/shardingsphere/core/merge/dal/DALMergeEngine.java
+8
-1
sharding-core/sharding-core-merge/src/main/java/org/apache/shardingsphere/core/merge/dal/desc/DescribeTableMergedResult.java
...sphere/core/merge/dal/desc/DescribeTableMergedResult.java
+108
-0
sharding-core/sharding-core-merge/src/test/java/org/apache/shardingsphere/core/merge/dal/DALMergeEngineTest.java
...che/shardingsphere/core/merge/dal/DALMergeEngineTest.java
+27
-5
sharding-core/sharding-core-merge/src/test/java/org/apache/shardingsphere/core/merge/dal/desc/DescribeTableMergedResultTest.java
...re/core/merge/dal/desc/DescribeTableMergedResultTest.java
+204
-0
sharding-core/sharding-core-merge/src/test/java/org/apache/shardingsphere/core/merge/fixture/DescribeQueryResultFixture.java
...sphere/core/merge/fixture/DescribeQueryResultFixture.java
+105
-0
未找到文件。
sharding-core/sharding-core-merge/src/main/java/org/apache/shardingsphere/core/merge/MergeEngineFactory.java
浏览文件 @
f301bdd2
...
...
@@ -58,7 +58,7 @@ public final class MergeEngineFactory {
return
new
DQLMergeEngine
(
databaseType
,
routeResult
,
queryResults
);
}
if
(
routeResult
.
getShardingStatement
().
getSQLStatement
()
instanceof
DALStatement
)
{
return
new
DALMergeEngine
(
shardingRule
,
queryResults
,
(
DALStatement
)
routeResult
.
getShardingStatement
().
getSQL
Statement
(),
tableMetas
);
return
new
DALMergeEngine
(
shardingRule
,
queryResults
,
routeResult
.
getSharding
Statement
(),
tableMetas
);
}
return
new
TransparentMergeEngine
(
queryResults
);
}
...
...
sharding-core/sharding-core-merge/src/main/java/org/apache/shardingsphere/core/merge/dal/DALMergeEngine.java
浏览文件 @
f301bdd2
...
...
@@ -21,6 +21,7 @@ import lombok.RequiredArgsConstructor;
import
org.apache.shardingsphere.core.execute.sql.execute.result.QueryResult
;
import
org.apache.shardingsphere.core.merge.MergeEngine
;
import
org.apache.shardingsphere.core.merge.MergedResult
;
import
org.apache.shardingsphere.core.merge.dal.desc.DescribeTableMergedResult
;
import
org.apache.shardingsphere.core.merge.dal.show.ShowCreateTableMergedResult
;
import
org.apache.shardingsphere.core.merge.dal.show.ShowDatabasesMergedResult
;
import
org.apache.shardingsphere.core.merge.dal.show.ShowIndexMergedResult
;
...
...
@@ -28,7 +29,9 @@ import org.apache.shardingsphere.core.merge.dal.show.ShowOtherMergedResult;
import
org.apache.shardingsphere.core.merge.dal.show.ShowTableStatusMergedResult
;
import
org.apache.shardingsphere.core.merge.dal.show.ShowTablesMergedResult
;
import
org.apache.shardingsphere.core.metadata.table.TableMetas
;
import
org.apache.shardingsphere.core.optimize.api.statement.OptimizedStatement
;
import
org.apache.shardingsphere.core.parse.sql.statement.dal.DALStatement
;
import
org.apache.shardingsphere.core.parse.sql.statement.dal.dialect.mysql.DescribeStatement
;
import
org.apache.shardingsphere.core.parse.sql.statement.dal.dialect.mysql.ShowCreateTableStatement
;
import
org.apache.shardingsphere.core.parse.sql.statement.dal.dialect.mysql.ShowDatabasesStatement
;
import
org.apache.shardingsphere.core.parse.sql.statement.dal.dialect.mysql.ShowIndexStatement
;
...
...
@@ -52,12 +55,13 @@ public final class DALMergeEngine implements MergeEngine {
private
final
List
<
QueryResult
>
queryResults
;
private
final
DALStatement
dal
Statement
;
private
final
OptimizedStatement
optimized
Statement
;
private
final
TableMetas
tableMetas
;
@Override
public
MergedResult
merge
()
throws
SQLException
{
final
DALStatement
dalStatement
=
(
DALStatement
)
optimizedStatement
.
getSQLStatement
();
if
(
dalStatement
instanceof
ShowDatabasesStatement
)
{
return
new
ShowDatabasesMergedResult
();
}
...
...
@@ -73,6 +77,9 @@ public final class DALMergeEngine implements MergeEngine {
if
(
dalStatement
instanceof
ShowIndexStatement
)
{
return
new
ShowIndexMergedResult
(
shardingRule
,
queryResults
,
tableMetas
);
}
if
(
dalStatement
instanceof
DescribeStatement
)
{
return
new
DescribeTableMergedResult
(
shardingRule
,
queryResults
,
optimizedStatement
);
}
return
new
ShowOtherMergedResult
(
queryResults
.
get
(
0
));
}
}
sharding-core/sharding-core-merge/src/main/java/org/apache/shardingsphere/core/merge/dal/desc/DescribeTableMergedResult.java
0 → 100644
浏览文件 @
f301bdd2
/*
* 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.merge.dal.desc
;
import
com.google.common.base.Optional
;
import
org.apache.shardingsphere.core.execute.sql.execute.result.QueryResult
;
import
org.apache.shardingsphere.core.merge.dql.common.MemoryMergedResult
;
import
org.apache.shardingsphere.core.merge.dql.common.MemoryQueryResultRow
;
import
org.apache.shardingsphere.core.optimize.api.statement.OptimizedStatement
;
import
org.apache.shardingsphere.core.rule.ShardingRule
;
import
org.apache.shardingsphere.core.strategy.encrypt.EncryptTable
;
import
java.sql.SQLException
;
import
java.util.HashMap
;
import
java.util.Iterator
;
import
java.util.LinkedList
;
import
java.util.List
;
import
java.util.Map
;
/**
* Merged result for desc table.
*
* @author liya
*/
public
final
class
DescribeTableMergedResult
extends
MemoryMergedResult
{
private
static
final
Map
<
String
,
Integer
>
LABEL_AND_INDEX_MAP
=
new
HashMap
<>(
6
,
1
);
static
{
LABEL_AND_INDEX_MAP
.
put
(
"Field"
,
1
);
LABEL_AND_INDEX_MAP
.
put
(
"Type"
,
2
);
LABEL_AND_INDEX_MAP
.
put
(
"Null"
,
3
);
LABEL_AND_INDEX_MAP
.
put
(
"Key"
,
4
);
LABEL_AND_INDEX_MAP
.
put
(
"Default"
,
5
);
LABEL_AND_INDEX_MAP
.
put
(
"Extra"
,
6
);
}
private
final
ShardingRule
shardingRule
;
private
final
OptimizedStatement
optimizedStatement
;
private
final
Iterator
<
MemoryQueryResultRow
>
memoryResultSetRows
;
public
DescribeTableMergedResult
(
final
ShardingRule
shardingRule
,
final
List
<
QueryResult
>
queryResults
,
final
OptimizedStatement
optimizedStatement
)
throws
SQLException
{
super
(
LABEL_AND_INDEX_MAP
);
this
.
shardingRule
=
shardingRule
;
this
.
optimizedStatement
=
optimizedStatement
;
this
.
memoryResultSetRows
=
init
(
queryResults
);
}
private
Iterator
<
MemoryQueryResultRow
>
init
(
final
List
<
QueryResult
>
queryResults
)
throws
SQLException
{
List
<
MemoryQueryResultRow
>
result
=
new
LinkedList
<>();
for
(
QueryResult
each
:
queryResults
)
{
while
(
each
.
next
())
{
Optional
<
MemoryQueryResultRow
>
memoryQueryResultRow
=
optimize
(
each
);
if
(
memoryQueryResultRow
.
isPresent
())
{
result
.
add
(
memoryQueryResultRow
.
get
());
}
}
}
if
(!
result
.
isEmpty
())
{
setCurrentResultSetRow
(
result
.
get
(
0
));
}
return
result
.
iterator
();
}
private
Optional
<
MemoryQueryResultRow
>
optimize
(
final
QueryResult
queryResult
)
throws
SQLException
{
MemoryQueryResultRow
memoryQueryResultRow
=
new
MemoryQueryResultRow
(
queryResult
);
String
logicTableName
=
optimizedStatement
.
getTables
().
getSingleTableName
();
Map
<
String
,
EncryptTable
>
tables
=
shardingRule
.
getEncryptRule
().
getTables
();
boolean
encrypted
=
tables
.
containsKey
(
logicTableName
);
if
(
encrypted
)
{
EncryptTable
encryptTable
=
tables
.
get
(
logicTableName
);
String
columnName
=
memoryQueryResultRow
.
getCell
(
1
).
toString
();
if
(
encryptTable
.
getAssistedQueryColumns
().
contains
(
columnName
))
{
return
Optional
.
absent
();
}
if
(
encryptTable
.
getCipherColumns
().
contains
(
columnName
))
{
memoryQueryResultRow
.
setCell
(
1
,
encryptTable
.
getLogicColumn
(
columnName
));
}
}
return
Optional
.
of
(
memoryQueryResultRow
);
}
@Override
public
boolean
next
()
throws
SQLException
{
if
(
memoryResultSetRows
.
hasNext
())
{
setCurrentResultSetRow
(
memoryResultSetRows
.
next
());
return
true
;
}
return
false
;
}
}
sharding-core/sharding-core-merge/src/test/java/org/apache/shardingsphere/core/merge/dal/DALMergeEngineTest.java
浏览文件 @
f301bdd2
...
...
@@ -18,12 +18,15 @@
package
org.apache.shardingsphere.core.merge.dal
;
import
org.apache.shardingsphere.core.execute.sql.execute.result.QueryResult
;
import
org.apache.shardingsphere.core.merge.dal.desc.DescribeTableMergedResult
;
import
org.apache.shardingsphere.core.merge.dal.show.ShowCreateTableMergedResult
;
import
org.apache.shardingsphere.core.merge.dal.show.ShowDatabasesMergedResult
;
import
org.apache.shardingsphere.core.merge.dal.show.ShowOtherMergedResult
;
import
org.apache.shardingsphere.core.merge.dal.show.ShowTablesMergedResult
;
import
org.apache.shardingsphere.core.merge.fixture.TestQueryResult
;
import
org.apache.shardingsphere.core.optimize.api.statement.OptimizedStatement
;
import
org.apache.shardingsphere.core.parse.sql.statement.dal.DALStatement
;
import
org.apache.shardingsphere.core.parse.sql.statement.dal.dialect.mysql.DescribeStatement
;
import
org.apache.shardingsphere.core.parse.sql.statement.dal.dialect.mysql.ShowCreateTableStatement
;
import
org.apache.shardingsphere.core.parse.sql.statement.dal.dialect.mysql.ShowDatabasesStatement
;
import
org.apache.shardingsphere.core.parse.sql.statement.dal.dialect.mysql.ShowOtherStatement
;
...
...
@@ -39,6 +42,7 @@ import java.util.List;
import
static
org
.
hamcrest
.
CoreMatchers
.
instanceOf
;
import
static
org
.
junit
.
Assert
.
assertThat
;
import
static
org
.
mockito
.
Mockito
.
mock
;
import
static
org
.
mockito
.
Mockito
.
when
;
public
final
class
DALMergeEngineTest
{
...
...
@@ -53,28 +57,46 @@ public final class DALMergeEngineTest {
@Test
public
void
assertMergeForShowDatabasesStatement
()
throws
SQLException
{
DALStatement
dalStatement
=
new
ShowDatabasesStatement
();
DALMergeEngine
dalMergeEngine
=
new
DALMergeEngine
(
null
,
queryResults
,
dalStatement
,
null
);
OptimizedStatement
optimizedStatement
=
mockOptimizedStatement
(
dalStatement
);
DALMergeEngine
dalMergeEngine
=
new
DALMergeEngine
(
null
,
queryResults
,
optimizedStatement
,
null
);
assertThat
(
dalMergeEngine
.
merge
(),
instanceOf
(
ShowDatabasesMergedResult
.
class
));
}
@Test
public
void
assertMergeForShowShowTablesStatement
()
throws
SQLException
{
DALStatement
dalStatement
=
new
ShowTablesStatement
();
DALMergeEngine
dalMergeEngine
=
new
DALMergeEngine
(
null
,
queryResults
,
dalStatement
,
null
);
OptimizedStatement
optimizedStatement
=
mockOptimizedStatement
(
dalStatement
);
DALMergeEngine
dalMergeEngine
=
new
DALMergeEngine
(
null
,
queryResults
,
optimizedStatement
,
null
);
assertThat
(
dalMergeEngine
.
merge
(),
instanceOf
(
ShowTablesMergedResult
.
class
));
}
@Test
public
void
assertMergeForShowCreateTableStatement
()
throws
SQLException
{
DALStatement
dalStatement
=
new
ShowCreateTableStatement
();
DALMergeEngine
dalMergeEngine
=
new
DALMergeEngine
(
null
,
queryResults
,
dalStatement
,
null
);
OptimizedStatement
optimizedStatement
=
mockOptimizedStatement
(
dalStatement
);
DALMergeEngine
dalMergeEngine
=
new
DALMergeEngine
(
null
,
queryResults
,
optimizedStatement
,
null
);
assertThat
(
dalMergeEngine
.
merge
(),
instanceOf
(
ShowCreateTableMergedResult
.
class
));
}
@Test
public
void
assertMergeForShowOtherStatement
()
throws
SQLException
{
DALStatement
dalStatement
=
new
ShowOtherStatement
();
DALMergeEngine
dalMergeEngine
=
new
DALMergeEngine
(
null
,
queryResults
,
dalStatement
,
null
);
OptimizedStatement
optimizedStatement
=
mockOptimizedStatement
(
dalStatement
);
DALMergeEngine
dalMergeEngine
=
new
DALMergeEngine
(
null
,
queryResults
,
optimizedStatement
,
null
);
assertThat
(
dalMergeEngine
.
merge
(),
instanceOf
(
ShowOtherMergedResult
.
class
));
}
@Test
public
void
assertMergeForDescribeStatement
()
throws
SQLException
{
DALStatement
dalStatement
=
new
DescribeStatement
();
OptimizedStatement
optimizedStatement
=
mockOptimizedStatement
(
dalStatement
);
DALMergeEngine
dalMergeEngine
=
new
DALMergeEngine
(
null
,
queryResults
,
optimizedStatement
,
null
);
assertThat
(
dalMergeEngine
.
merge
(),
instanceOf
(
DescribeTableMergedResult
.
class
));
}
private
OptimizedStatement
mockOptimizedStatement
(
final
DALStatement
dalStatement
)
{
OptimizedStatement
optimizedStatement
=
mock
(
OptimizedStatement
.
class
);
when
(
optimizedStatement
.
getSQLStatement
()).
thenReturn
(
dalStatement
);
return
optimizedStatement
;
}
}
sharding-core/sharding-core-merge/src/test/java/org/apache/shardingsphere/core/merge/dal/desc/DescribeTableMergedResultTest.java
0 → 100644
浏览文件 @
f301bdd2
/*
* 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.merge.dal.desc
;
import
com.google.common.collect.Lists
;
import
org.apache.shardingsphere.core.execute.sql.execute.result.QueryResult
;
import
org.apache.shardingsphere.core.merge.fixture.DescribeQueryResultFixture
;
import
org.apache.shardingsphere.core.optimize.api.segment.Tables
;
import
org.apache.shardingsphere.core.optimize.api.statement.OptimizedStatement
;
import
org.apache.shardingsphere.core.rule.EncryptRule
;
import
org.apache.shardingsphere.core.rule.ShardingRule
;
import
org.apache.shardingsphere.core.strategy.encrypt.EncryptTable
;
import
org.junit.Before
;
import
org.junit.Test
;
import
java.sql.SQLException
;
import
java.util.Arrays
;
import
java.util.Collections
;
import
java.util.HashMap
;
import
java.util.LinkedList
;
import
java.util.List
;
import
java.util.Map
;
import
static
org
.
junit
.
Assert
.
assertEquals
;
import
static
org
.
junit
.
Assert
.
assertFalse
;
import
static
org
.
junit
.
Assert
.
assertTrue
;
import
static
org
.
mockito
.
Mockito
.
mock
;
import
static
org
.
mockito
.
Mockito
.
when
;
public
final
class
DescribeTableMergedResultTest
{
private
List
<
QueryResult
>
queryResults
;
private
OptimizedStatement
optimizedStatement
;
@Before
public
void
setUp
()
throws
SQLException
{
optimizedStatement
=
mock
(
OptimizedStatement
.
class
);
Tables
tables
=
mock
(
Tables
.
class
);
when
(
tables
.
getSingleTableName
()).
thenReturn
(
"user"
);
when
(
optimizedStatement
.
getTables
()).
thenReturn
(
tables
);
List
<
QueryResult
>
mockQueryResults
=
mockQueryResults
();
QueryResult
queryResult
=
new
DescribeQueryResultFixture
(
mockQueryResults
.
iterator
());
queryResults
=
Lists
.
newArrayList
(
queryResult
);
}
private
List
<
QueryResult
>
mockQueryResults
()
throws
SQLException
{
List
<
QueryResult
>
queryResults
=
new
LinkedList
<>();
queryResults
.
add
(
mockQueryResult
(
6
,
"id"
,
"int(11) unsigned"
,
"NO"
,
"PRI"
,
""
,
"auto_increment"
));
queryResults
.
add
(
mockQueryResult
(
6
,
"name"
,
"varchar(100)"
,
"YES"
,
""
,
""
,
""
));
queryResults
.
add
(
mockQueryResult
(
6
,
"pre_name"
,
"varchar(100)"
,
"YES"
,
""
,
""
,
""
));
queryResults
.
add
(
mockQueryResult
(
6
,
"name_assisted"
,
"varchar(100)"
,
"YES"
,
""
,
""
,
""
));
return
queryResults
;
}
private
QueryResult
mockQueryResult
(
final
int
columnNum
,
final
String
field
,
final
String
type
,
final
String
nullValue
,
final
String
key
,
final
String
defaultValue
,
final
String
extra
)
throws
SQLException
{
QueryResult
queryResult
=
mock
(
QueryResult
.
class
);
when
(
queryResult
.
getColumnCount
()).
thenReturn
(
columnNum
);
when
(
queryResult
.
getValue
(
1
,
String
.
class
)).
thenReturn
(
field
);
when
(
queryResult
.
getValue
(
1
,
Object
.
class
)).
thenReturn
(
field
);
when
(
queryResult
.
getValue
(
"Field"
,
String
.
class
)).
thenReturn
(
field
);
when
(
queryResult
.
getValue
(
"Field"
,
Object
.
class
)).
thenReturn
(
field
);
when
(
queryResult
.
getValue
(
2
,
String
.
class
)).
thenReturn
(
type
);
when
(
queryResult
.
getValue
(
2
,
Object
.
class
)).
thenReturn
(
type
);
when
(
queryResult
.
getValue
(
"Type"
,
String
.
class
)).
thenReturn
(
type
);
when
(
queryResult
.
getValue
(
"Type"
,
Object
.
class
)).
thenReturn
(
type
);
when
(
queryResult
.
getValue
(
3
,
String
.
class
)).
thenReturn
(
nullValue
);
when
(
queryResult
.
getValue
(
3
,
Object
.
class
)).
thenReturn
(
nullValue
);
when
(
queryResult
.
getValue
(
"Null"
,
String
.
class
)).
thenReturn
(
nullValue
);
when
(
queryResult
.
getValue
(
"Null"
,
Object
.
class
)).
thenReturn
(
nullValue
);
when
(
queryResult
.
getValue
(
4
,
String
.
class
)).
thenReturn
(
key
);
when
(
queryResult
.
getValue
(
4
,
Object
.
class
)).
thenReturn
(
key
);
when
(
queryResult
.
getValue
(
"Key"
,
String
.
class
)).
thenReturn
(
key
);
when
(
queryResult
.
getValue
(
"Key"
,
Object
.
class
)).
thenReturn
(
key
);
when
(
queryResult
.
getValue
(
5
,
String
.
class
)).
thenReturn
(
defaultValue
);
when
(
queryResult
.
getValue
(
5
,
Object
.
class
)).
thenReturn
(
defaultValue
);
when
(
queryResult
.
getValue
(
"Default"
,
String
.
class
)).
thenReturn
(
defaultValue
);
when
(
queryResult
.
getValue
(
"Default"
,
Object
.
class
)).
thenReturn
(
defaultValue
);
when
(
queryResult
.
getValue
(
6
,
String
.
class
)).
thenReturn
(
extra
);
when
(
queryResult
.
getValue
(
6
,
Object
.
class
)).
thenReturn
(
extra
);
when
(
queryResult
.
getValue
(
"Extra"
,
String
.
class
)).
thenReturn
(
extra
);
when
(
queryResult
.
getValue
(
"Extra"
,
Object
.
class
)).
thenReturn
(
extra
);
return
queryResult
;
}
@Test
public
void
assertNextForEmptyQueryResult
()
throws
SQLException
{
ShardingRule
shardingRule
=
mock
(
ShardingRule
.
class
);
List
<
QueryResult
>
queryResults
=
Collections
.
emptyList
();
DescribeTableMergedResult
describeTableMergedResult
=
new
DescribeTableMergedResult
(
shardingRule
,
queryResults
,
optimizedStatement
);
assertFalse
(
describeTableMergedResult
.
next
());
}
@Test
public
void
assertFieldWithEncryptRule
()
throws
SQLException
{
ShardingRule
shardingRule
=
mockShardingRuleWithEncryptRule
();
DescribeTableMergedResult
describeTableMergedResult
=
new
DescribeTableMergedResult
(
shardingRule
,
queryResults
,
optimizedStatement
);
assertTrue
(
describeTableMergedResult
.
next
());
assertEquals
(
describeTableMergedResult
.
getValue
(
1
,
String
.
class
),
"id"
);
assertEquals
(
describeTableMergedResult
.
getValue
(
"Field"
,
String
.
class
),
"id"
);
assertTrue
(
describeTableMergedResult
.
next
());
assertEquals
(
describeTableMergedResult
.
getValue
(
1
,
String
.
class
),
"logic_name"
);
assertEquals
(
describeTableMergedResult
.
getValue
(
"Field"
,
String
.
class
),
"logic_name"
);
assertTrue
(
describeTableMergedResult
.
next
());
assertEquals
(
describeTableMergedResult
.
getValue
(
1
,
String
.
class
),
"pre_name"
);
assertEquals
(
describeTableMergedResult
.
getValue
(
"Field"
,
String
.
class
),
"pre_name"
);
assertFalse
(
describeTableMergedResult
.
next
());
}
@Test
public
void
assertFieldWithoutEncryptRule
()
throws
SQLException
{
ShardingRule
shardingRule
=
mockShardingRuleWithoutEncryptRule
();
DescribeTableMergedResult
describeTableMergedResult
=
new
DescribeTableMergedResult
(
shardingRule
,
queryResults
,
optimizedStatement
);
assertTrue
(
describeTableMergedResult
.
next
());
assertEquals
(
describeTableMergedResult
.
getValue
(
1
,
String
.
class
),
"id"
);
assertEquals
(
describeTableMergedResult
.
getValue
(
"Field"
,
String
.
class
),
"id"
);
assertTrue
(
describeTableMergedResult
.
next
());
assertEquals
(
describeTableMergedResult
.
getValue
(
1
,
String
.
class
),
"name"
);
assertEquals
(
describeTableMergedResult
.
getValue
(
"Field"
,
String
.
class
),
"name"
);
assertTrue
(
describeTableMergedResult
.
next
());
assertEquals
(
describeTableMergedResult
.
getValue
(
1
,
String
.
class
),
"pre_name"
);
assertEquals
(
describeTableMergedResult
.
getValue
(
"Field"
,
String
.
class
),
"pre_name"
);
assertTrue
(
describeTableMergedResult
.
next
());
assertEquals
(
describeTableMergedResult
.
getValue
(
1
,
String
.
class
),
"name_assisted"
);
assertEquals
(
describeTableMergedResult
.
getValue
(
"Field"
,
String
.
class
),
"name_assisted"
);
}
@Test
public
void
assertAllWithoutEncryptRule
()
throws
SQLException
{
ShardingRule
shardingRule
=
mockShardingRuleWithoutEncryptRule
();
DescribeTableMergedResult
describeTableMergedResult
=
new
DescribeTableMergedResult
(
shardingRule
,
queryResults
,
optimizedStatement
);
assertTrue
(
describeTableMergedResult
.
next
());
assertEquals
(
describeTableMergedResult
.
getValue
(
1
,
String
.
class
),
"id"
);
assertEquals
(
describeTableMergedResult
.
getValue
(
"Field"
,
String
.
class
),
"id"
);
assertEquals
(
describeTableMergedResult
.
getValue
(
2
,
String
.
class
),
"int(11) unsigned"
);
assertEquals
(
describeTableMergedResult
.
getValue
(
"Type"
,
String
.
class
),
"int(11) unsigned"
);
assertEquals
(
describeTableMergedResult
.
getValue
(
3
,
String
.
class
),
"NO"
);
assertEquals
(
describeTableMergedResult
.
getValue
(
"Null"
,
String
.
class
),
"NO"
);
assertEquals
(
describeTableMergedResult
.
getValue
(
4
,
String
.
class
),
"PRI"
);
assertEquals
(
describeTableMergedResult
.
getValue
(
"Key"
,
String
.
class
),
"PRI"
);
assertEquals
(
describeTableMergedResult
.
getValue
(
5
,
String
.
class
),
""
);
assertEquals
(
describeTableMergedResult
.
getValue
(
"Default"
,
String
.
class
),
""
);
assertEquals
(
describeTableMergedResult
.
getValue
(
6
,
String
.
class
),
"auto_increment"
);
assertEquals
(
describeTableMergedResult
.
getValue
(
"Extra"
,
String
.
class
),
"auto_increment"
);
}
@Test
public
void
assertAllWithEncryptRule
()
throws
SQLException
{
ShardingRule
shardingRule
=
mockShardingRuleWithEncryptRule
();
DescribeTableMergedResult
describeTableMergedResult
=
new
DescribeTableMergedResult
(
shardingRule
,
queryResults
,
optimizedStatement
);
assertTrue
(
describeTableMergedResult
.
next
());
assertEquals
(
describeTableMergedResult
.
getValue
(
1
,
String
.
class
),
"id"
);
assertEquals
(
describeTableMergedResult
.
getValue
(
"Field"
,
String
.
class
),
"id"
);
assertEquals
(
describeTableMergedResult
.
getValue
(
2
,
String
.
class
),
"int(11) unsigned"
);
assertEquals
(
describeTableMergedResult
.
getValue
(
"Type"
,
String
.
class
),
"int(11) unsigned"
);
assertEquals
(
describeTableMergedResult
.
getValue
(
3
,
String
.
class
),
"NO"
);
assertEquals
(
describeTableMergedResult
.
getValue
(
"Null"
,
String
.
class
),
"NO"
);
assertEquals
(
describeTableMergedResult
.
getValue
(
4
,
String
.
class
),
"PRI"
);
assertEquals
(
describeTableMergedResult
.
getValue
(
"Key"
,
String
.
class
),
"PRI"
);
assertEquals
(
describeTableMergedResult
.
getValue
(
5
,
String
.
class
),
""
);
assertEquals
(
describeTableMergedResult
.
getValue
(
"Default"
,
String
.
class
),
""
);
assertEquals
(
describeTableMergedResult
.
getValue
(
6
,
String
.
class
),
"auto_increment"
);
assertEquals
(
describeTableMergedResult
.
getValue
(
"Extra"
,
String
.
class
),
"auto_increment"
);
}
private
ShardingRule
mockShardingRuleWithoutEncryptRule
()
{
ShardingRule
shardingRule
=
mock
(
ShardingRule
.
class
);
EncryptRule
encryptRule
=
mock
(
EncryptRule
.
class
);
when
(
encryptRule
.
getTables
()).
thenReturn
(
Collections
.<
String
,
EncryptTable
>
emptyMap
());
when
(
shardingRule
.
getEncryptRule
()).
thenReturn
(
encryptRule
);
return
shardingRule
;
}
private
ShardingRule
mockShardingRuleWithEncryptRule
()
{
ShardingRule
shardingRule
=
mock
(
ShardingRule
.
class
);
EncryptRule
encryptRule
=
mock
(
EncryptRule
.
class
);
EncryptTable
encryptTable
=
mock
(
EncryptTable
.
class
);
Map
<
String
,
EncryptTable
>
tables
=
new
HashMap
<>();
tables
.
put
(
"user"
,
encryptTable
);
when
(
shardingRule
.
getEncryptRule
()).
thenReturn
(
encryptRule
);
when
(
encryptRule
.
getTables
()).
thenReturn
(
tables
);
when
(
encryptTable
.
getAssistedQueryColumns
()).
thenReturn
(
Arrays
.
asList
(
"name_assisted"
));
when
(
encryptTable
.
getCipherColumns
()).
thenReturn
(
Arrays
.
asList
(
"name"
));
when
(
encryptTable
.
getLogicColumn
(
"name"
)).
thenReturn
(
"logic_name"
);
return
shardingRule
;
}
}
sharding-core/sharding-core-merge/src/test/java/org/apache/shardingsphere/core/merge/fixture/DescribeQueryResultFixture.java
0 → 100644
浏览文件 @
f301bdd2
/*
* 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.merge.fixture
;
import
lombok.RequiredArgsConstructor
;
import
org.apache.shardingsphere.core.execute.sql.execute.result.QueryResult
;
import
org.apache.shardingsphere.core.execute.sql.execute.result.QueryResultMetaData
;
import
java.io.InputStream
;
import
java.sql.SQLException
;
import
java.util.Calendar
;
import
java.util.Iterator
;
/**
* fixture for test DescribeTableMergedResult.
*
* @author liya
*/
@RequiredArgsConstructor
public
final
class
DescribeQueryResultFixture
implements
QueryResult
{
private
final
Iterator
<
QueryResult
>
queryResults
;
private
QueryResult
currQueryResult
;
@Override
public
boolean
next
()
throws
SQLException
{
boolean
hasNext
=
queryResults
.
hasNext
();
if
(
hasNext
)
{
currQueryResult
=
queryResults
.
next
();
}
return
hasNext
;
}
@Override
public
Object
getValue
(
final
int
columnIndex
,
final
Class
<?>
type
)
throws
SQLException
{
return
currQueryResult
.
getValue
(
columnIndex
,
type
);
}
@Override
public
Object
getValue
(
final
String
columnLabel
,
final
Class
<?>
type
)
throws
SQLException
{
return
null
;
}
@Override
public
Object
getCalendarValue
(
final
int
columnIndex
,
final
Class
<?>
type
,
final
Calendar
calendar
)
throws
SQLException
{
return
null
;
}
@Override
public
Object
getCalendarValue
(
final
String
columnLabel
,
final
Class
<?>
type
,
final
Calendar
calendar
)
throws
SQLException
{
return
null
;
}
@Override
public
InputStream
getInputStream
(
final
int
columnIndex
,
final
String
type
)
throws
SQLException
{
return
null
;
}
@Override
public
InputStream
getInputStream
(
final
String
columnLabel
,
final
String
type
)
throws
SQLException
{
return
null
;
}
@Override
public
boolean
wasNull
()
throws
SQLException
{
return
false
;
}
@Override
public
boolean
isCaseSensitive
(
final
int
columnIndex
)
throws
SQLException
{
return
false
;
}
@Override
public
QueryResultMetaData
getQueryResultMetaData
()
{
return
null
;
}
@Override
public
int
getColumnCount
()
throws
SQLException
{
return
currQueryResult
.
getColumnCount
();
}
@Override
public
String
getColumnLabel
(
final
int
columnIndex
)
throws
SQLException
{
return
null
;
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录