This is an automated email from the ASF dual-hosted git repository.
jianglongtao pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git
The following commit(s) were added to refs/heads/master by this push:
new f55a636e814 Replace `EncryptRuleResultSet` with
`ShowEncryptRuleExecutor` (#23797)
f55a636e814 is described below
commit f55a636e814a38020fd2f93d4c5f4b617e98d865
Author: Zichao <[email protected]>
AuthorDate: Sun Jan 29 21:30:12 2023 +1300
Replace `EncryptRuleResultSet` with `ShowEncryptRuleExecutor` (#23797)
* Replace `DatabaseDiscoveryHeartbeatResultSet` with
`ShowDatabaseDiscoveryHeartbeatExecutor`
* Replace `EncryptRuleResultSet` with `ShowEncryptRuleExecutor`
---
...ResultSet.java => ShowEncryptRuleExecutor.java} | 42 ++++++----------
...ardingsphere.distsql.handler.query.RQLExecutor} | 3 +-
...here.distsql.handler.resultset.DistSQLResultSet | 1 -
...tTest.java => ShowEncryptRuleExecutorTest.java} | 57 ++++++++++++++++------
4 files changed, 60 insertions(+), 43 deletions(-)
diff --git
a/features/encrypt/distsql/handler/src/main/java/org/apache/shardingsphere/encrypt/distsql/handler/query/EncryptRuleResultSet.java
b/features/encrypt/distsql/handler/src/main/java/org/apache/shardingsphere/encrypt/distsql/handler/query/ShowEncryptRuleExecutor.java
similarity index 76%
rename from
features/encrypt/distsql/handler/src/main/java/org/apache/shardingsphere/encrypt/distsql/handler/query/EncryptRuleResultSet.java
rename to
features/encrypt/distsql/handler/src/main/java/org/apache/shardingsphere/encrypt/distsql/handler/query/ShowEncryptRuleExecutor.java
index 667c2f1ee7d..0ff6aa14a12 100644
---
a/features/encrypt/distsql/handler/src/main/java/org/apache/shardingsphere/encrypt/distsql/handler/query/EncryptRuleResultSet.java
+++
b/features/encrypt/distsql/handler/src/main/java/org/apache/shardingsphere/encrypt/distsql/handler/query/ShowEncryptRuleExecutor.java
@@ -17,21 +17,19 @@
package org.apache.shardingsphere.encrypt.distsql.handler.query;
-import
org.apache.shardingsphere.distsql.handler.resultset.DatabaseDistSQLResultSet;
+import org.apache.shardingsphere.distsql.handler.query.RQLExecutor;
import org.apache.shardingsphere.encrypt.api.config.EncryptRuleConfiguration;
import
org.apache.shardingsphere.encrypt.api.config.rule.EncryptColumnRuleConfiguration;
import
org.apache.shardingsphere.encrypt.api.config.rule.EncryptTableRuleConfiguration;
import
org.apache.shardingsphere.encrypt.distsql.parser.statement.ShowEncryptRulesStatement;
import org.apache.shardingsphere.encrypt.rule.EncryptRule;
import org.apache.shardingsphere.infra.config.algorithm.AlgorithmConfiguration;
+import
org.apache.shardingsphere.infra.merge.result.impl.local.LocalDataQueryResultRow;
import
org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import org.apache.shardingsphere.infra.util.props.PropertiesConverter;
-import org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement;
import java.util.Arrays;
import java.util.Collection;
-import java.util.Collections;
-import java.util.Iterator;
import java.util.LinkedList;
import java.util.Map;
import java.util.Objects;
@@ -39,31 +37,33 @@ import java.util.Optional;
import java.util.stream.Collectors;
/**
- * Result set for show encrypt rule.
+ * Show encrypt rule executor.
*/
-public final class EncryptRuleResultSet implements DatabaseDistSQLResultSet {
-
- private Iterator<Collection<Object>> data = Collections.emptyIterator();
+public final class ShowEncryptRuleExecutor implements
RQLExecutor<ShowEncryptRulesStatement> {
@Override
- public void init(final ShardingSphereDatabase database, final SQLStatement
sqlStatement) {
+ public Collection<LocalDataQueryResultRow> getRows(final
ShardingSphereDatabase database, final ShowEncryptRulesStatement sqlStatement) {
Optional<EncryptRule> rule =
database.getRuleMetaData().findSingleRule(EncryptRule.class);
- rule.ifPresent(optional -> data = buildData((EncryptRuleConfiguration)
optional.getConfiguration(), (ShowEncryptRulesStatement)
sqlStatement).iterator());
+ Collection<LocalDataQueryResultRow> result = new LinkedList<>();
+ if (rule.isPresent()) {
+ result = buildData((EncryptRuleConfiguration)
rule.get().getConfiguration(), sqlStatement);
+ }
+ return result;
}
- private Collection<Collection<Object>> buildData(final
EncryptRuleConfiguration ruleConfig, final ShowEncryptRulesStatement
sqlStatement) {
+ private Collection<LocalDataQueryResultRow> buildData(final
EncryptRuleConfiguration ruleConfig, final ShowEncryptRulesStatement
sqlStatement) {
return ruleConfig.getTables().stream().filter(each ->
Objects.isNull(sqlStatement.getTableName()) ||
each.getName().equals(sqlStatement.getTableName()))
.map(each -> buildColumnData(each, ruleConfig.getEncryptors(),
ruleConfig.isQueryWithCipherColumn())).flatMap(Collection::stream).collect(Collectors.toList());
}
- private Collection<Collection<Object>> buildColumnData(final
EncryptTableRuleConfiguration tableRuleConfig, final Map<String,
AlgorithmConfiguration> algorithmMap,
- final boolean
queryWithCipherColumn) {
- Collection<Collection<Object>> result = new LinkedList<>();
+ private Collection<LocalDataQueryResultRow> buildColumnData(final
EncryptTableRuleConfiguration tableRuleConfig, final Map<String,
AlgorithmConfiguration> algorithmMap,
+ final boolean
queryWithCipherColumn) {
+ Collection<LocalDataQueryResultRow> result = new LinkedList<>();
tableRuleConfig.getColumns().forEach(each -> {
AlgorithmConfiguration encryptorAlgorithmConfig =
algorithmMap.get(each.getEncryptorName());
AlgorithmConfiguration assistedQueryEncryptorAlgorithmConfig =
algorithmMap.get(each.getAssistedQueryEncryptorName());
AlgorithmConfiguration likeQueryEncryptorAlgorithmConfig =
algorithmMap.get(each.getLikeQueryEncryptorName());
- result.add(Arrays.asList(tableRuleConfig.getName(),
each.getLogicColumn(),
+ result.add(new
LocalDataQueryResultRow(Arrays.asList(tableRuleConfig.getName(),
each.getLogicColumn(),
each.getCipherColumn(),
nullToEmptyString(each.getPlainColumn()),
nullToEmptyString(each.getAssistedQueryColumn()),
@@ -73,7 +73,7 @@ public final class EncryptRuleResultSet implements
DatabaseDistSQLResultSet {
Objects.isNull(assistedQueryEncryptorAlgorithmConfig) ?
nullToEmptyString(null) :
PropertiesConverter.convert(assistedQueryEncryptorAlgorithmConfig.getProps()),
Objects.isNull(likeQueryEncryptorAlgorithmConfig) ?
nullToEmptyString(null) : likeQueryEncryptorAlgorithmConfig.getType(),
Objects.isNull(likeQueryEncryptorAlgorithmConfig) ?
nullToEmptyString(null) :
PropertiesConverter.convert(likeQueryEncryptorAlgorithmConfig.getProps()),
- isQueryWithCipherColumn(queryWithCipherColumn,
tableRuleConfig, each).toString()));
+ isQueryWithCipherColumn(queryWithCipherColumn,
tableRuleConfig, each).toString())));
});
return result;
}
@@ -99,16 +99,6 @@ public final class EncryptRuleResultSet implements
DatabaseDistSQLResultSet {
"assisted_query_type", "assisted_query_props",
"like_query_type", "like_query_props", "query_with_cipher_column");
}
- @Override
- public boolean next() {
- return data.hasNext();
- }
-
- @Override
- public Collection<Object> getRowData() {
- return data.next();
- }
-
@Override
public String getType() {
return ShowEncryptRulesStatement.class.getName();
diff --git
a/features/encrypt/distsql/handler/src/main/resources/META-INF/services/org.apache.shardingsphere.distsql.handler.resultset.DistSQLResultSet
b/features/encrypt/distsql/handler/src/main/resources/META-INF/services/org.apache.shardingsphere.distsql.handler.query.RQLExecutor
similarity index 83%
copy from
features/encrypt/distsql/handler/src/main/resources/META-INF/services/org.apache.shardingsphere.distsql.handler.resultset.DistSQLResultSet
copy to
features/encrypt/distsql/handler/src/main/resources/META-INF/services/org.apache.shardingsphere.distsql.handler.query.RQLExecutor
index 9939a49635c..45f51c9169f 100644
---
a/features/encrypt/distsql/handler/src/main/resources/META-INF/services/org.apache.shardingsphere.distsql.handler.resultset.DistSQLResultSet
+++
b/features/encrypt/distsql/handler/src/main/resources/META-INF/services/org.apache.shardingsphere.distsql.handler.query.RQLExecutor
@@ -15,5 +15,4 @@
# limitations under the License.
#
-org.apache.shardingsphere.encrypt.distsql.handler.query.EncryptRuleResultSet
-org.apache.shardingsphere.encrypt.distsql.handler.query.CountEncryptRuleResultSet
+org.apache.shardingsphere.encrypt.distsql.handler.query.ShowEncryptRuleExecutor
diff --git
a/features/encrypt/distsql/handler/src/main/resources/META-INF/services/org.apache.shardingsphere.distsql.handler.resultset.DistSQLResultSet
b/features/encrypt/distsql/handler/src/main/resources/META-INF/services/org.apache.shardingsphere.distsql.handler.resultset.DistSQLResultSet
index 9939a49635c..7a48a16312d 100644
---
a/features/encrypt/distsql/handler/src/main/resources/META-INF/services/org.apache.shardingsphere.distsql.handler.resultset.DistSQLResultSet
+++
b/features/encrypt/distsql/handler/src/main/resources/META-INF/services/org.apache.shardingsphere.distsql.handler.resultset.DistSQLResultSet
@@ -15,5 +15,4 @@
# limitations under the License.
#
-org.apache.shardingsphere.encrypt.distsql.handler.query.EncryptRuleResultSet
org.apache.shardingsphere.encrypt.distsql.handler.query.CountEncryptRuleResultSet
diff --git
a/features/encrypt/distsql/handler/src/test/java/org/apache/shardingsphere/encrypt/distsql/handler/query/EncryptRuleResultSetTest.java
b/features/encrypt/distsql/handler/src/test/java/org/apache/shardingsphere/encrypt/distsql/handler/query/ShowEncryptRuleExecutorTest.java
similarity index 57%
rename from
features/encrypt/distsql/handler/src/test/java/org/apache/shardingsphere/encrypt/distsql/handler/query/EncryptRuleResultSetTest.java
rename to
features/encrypt/distsql/handler/src/test/java/org/apache/shardingsphere/encrypt/distsql/handler/query/ShowEncryptRuleExecutorTest.java
index 9333e6be69c..29677db41a2 100644
---
a/features/encrypt/distsql/handler/src/test/java/org/apache/shardingsphere/encrypt/distsql/handler/query/EncryptRuleResultSetTest.java
+++
b/features/encrypt/distsql/handler/src/test/java/org/apache/shardingsphere/encrypt/distsql/handler/query/ShowEncryptRuleExecutorTest.java
@@ -17,7 +17,7 @@
package org.apache.shardingsphere.encrypt.distsql.handler.query;
-import
org.apache.shardingsphere.distsql.handler.resultset.DatabaseDistSQLResultSet;
+import org.apache.shardingsphere.distsql.handler.query.RQLExecutor;
import org.apache.shardingsphere.encrypt.api.config.EncryptRuleConfiguration;
import
org.apache.shardingsphere.encrypt.api.config.rule.EncryptColumnRuleConfiguration;
import
org.apache.shardingsphere.encrypt.api.config.rule.EncryptTableRuleConfiguration;
@@ -25,37 +25,66 @@ import
org.apache.shardingsphere.encrypt.distsql.parser.statement.ShowEncryptRul
import org.apache.shardingsphere.encrypt.rule.EncryptRule;
import org.apache.shardingsphere.infra.config.algorithm.AlgorithmConfiguration;
import org.apache.shardingsphere.infra.config.rule.RuleConfiguration;
+import
org.apache.shardingsphere.infra.merge.result.impl.local.LocalDataQueryResultRow;
import
org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import
org.apache.shardingsphere.infra.metadata.database.rule.ShardingSphereRuleMetaData;
import org.junit.Test;
import java.util.Collection;
import java.util.Collections;
+import java.util.Iterator;
import java.util.Properties;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
-import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
-public final class EncryptRuleResultSetTest {
+public final class ShowEncryptRuleExecutorTest {
@Test
public void assertGetRowData() {
ShardingSphereDatabase database = mockDatabase();
- DatabaseDistSQLResultSet resultSet = new EncryptRuleResultSet();
- resultSet.init(database, mock(ShowEncryptRulesStatement.class));
- Collection<Object> actual = resultSet.getRowData();
- assertThat(actual.size(), is(13));
- assertTrue(actual.contains("t_encrypt"));
- assertTrue(actual.contains("user_id"));
- assertTrue(actual.contains("user_cipher"));
- assertTrue(actual.contains("user_plain"));
- assertTrue(actual.contains("user_assisted"));
- assertTrue(actual.contains("user_like"));
- assertTrue(actual.contains("md5"));
+ RQLExecutor<ShowEncryptRulesStatement> executor = new
ShowEncryptRuleExecutor();
+ Collection<LocalDataQueryResultRow> actual =
executor.getRows(database, mock(ShowEncryptRulesStatement.class));
+ assertThat(actual.size(), is(1));
+ Iterator<LocalDataQueryResultRow> iterator = actual.iterator();
+ LocalDataQueryResultRow row = iterator.next();
+ assertThat(row.getCell(1), is("t_encrypt"));
+ assertThat(row.getCell(2), is("user_id"));
+ assertThat(row.getCell(3), is("user_cipher"));
+ assertThat(row.getCell(4), is("user_plain"));
+ assertThat(row.getCell(5), is("user_assisted"));
+ assertThat(row.getCell(6), is("user_like"));
+ assertThat(row.getCell(7), is("md5"));
+ assertThat(row.getCell(8), is(""));
+ assertThat(row.getCell(9), is(""));
+ assertThat(row.getCell(10), is(""));
+ assertThat(row.getCell(11), is(""));
+ assertThat(row.getCell(12), is(""));
+ assertThat(row.getCell(13), is("true"));
+ }
+
+ @Test
+ public void assertGetColumnNames() {
+ RQLExecutor<ShowEncryptRulesStatement> executor = new
ShowEncryptRuleExecutor();
+ Collection<String> columns = executor.getColumnNames();
+ assertThat(columns.size(), is(13));
+ Iterator<String> iterator = columns.iterator();
+ assertThat(iterator.next(), is("table"));
+ assertThat(iterator.next(), is("logic_column"));
+ assertThat(iterator.next(), is("cipher_column"));
+ assertThat(iterator.next(), is("plain_column"));
+ assertThat(iterator.next(), is("assisted_query_column"));
+ assertThat(iterator.next(), is("like_query_column"));
+ assertThat(iterator.next(), is("encryptor_type"));
+ assertThat(iterator.next(), is("encryptor_props"));
+ assertThat(iterator.next(), is("assisted_query_type"));
+ assertThat(iterator.next(), is("assisted_query_props"));
+ assertThat(iterator.next(), is("like_query_type"));
+ assertThat(iterator.next(), is("like_query_props"));
+ assertThat(iterator.next(), is("query_with_cipher_column"));
}
private ShardingSphereDatabase mockDatabase() {