Skip to content

Commit

Permalink
Add DistSQLConstants (#30983)
Browse files Browse the repository at this point in the history
  • Loading branch information
terrymanu authored Apr 21, 2024
1 parent bc4861a commit 406339a
Show file tree
Hide file tree
Showing 9 changed files with 63 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,6 @@
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class EncryptDistSQLConstants {

public static final String COMMA = ",";

public static final String SEMI = ";";

public static final String CREATE_ENCRYPT = "CREATE ENCRYPT RULE";

public static final String ENCRYPT = " %s ("
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package org.apache.shardingsphere.encrypt.distsql.handler.provider;

import com.google.common.base.Strings;
import org.apache.shardingsphere.distsql.handler.constant.DistSQLConstants;
import org.apache.shardingsphere.distsql.handler.engine.query.ral.convert.AlgorithmDistSQLConverter;
import org.apache.shardingsphere.distsql.handler.engine.query.ral.convert.RuleConfigurationToDistSQLConverter;
import org.apache.shardingsphere.encrypt.api.config.EncryptRuleConfiguration;
Expand Down Expand Up @@ -47,10 +48,10 @@ public String convert(final EncryptRuleConfiguration ruleConfig) {
EncryptTableRuleConfiguration tableRuleConfig = iterator.next();
result.append(String.format(EncryptDistSQLConstants.ENCRYPT, tableRuleConfig.getName(), getEncryptColumns(tableRuleConfig.getColumns(), ruleConfig.getEncryptors())));
if (iterator.hasNext()) {
result.append(EncryptDistSQLConstants.COMMA).append(System.lineSeparator());
result.append(DistSQLConstants.COMMA).append(System.lineSeparator());
}
}
result.append(EncryptDistSQLConstants.SEMI);
result.append(DistSQLConstants.SEMI);
return result.toString();
}

Expand All @@ -61,7 +62,7 @@ private String getEncryptColumns(final Collection<EncryptColumnRuleConfiguration
EncryptColumnRuleConfiguration columnRuleConfig = iterator.next();
result.append(String.format(EncryptDistSQLConstants.ENCRYPT_COLUMN, columnRuleConfig.getName(), getColumns(columnRuleConfig), getEncryptAlgorithms(columnRuleConfig, encryptors)));
if (iterator.hasNext()) {
result.append(EncryptDistSQLConstants.COMMA).append(System.lineSeparator());
result.append(DistSQLConstants.COMMA).append(System.lineSeparator());
}
}
return result.toString();
Expand All @@ -74,10 +75,10 @@ private String getColumns(final EncryptColumnRuleConfiguration ruleConfig) {
result.append(String.format(EncryptDistSQLConstants.CIPHER, cipherColumnName));
}
if (ruleConfig.getAssistedQuery().isPresent()) {
result.append(EncryptDistSQLConstants.COMMA).append(' ').append(String.format(EncryptDistSQLConstants.ASSISTED_QUERY_COLUMN, ruleConfig.getAssistedQuery().get().getName()));
result.append(DistSQLConstants.COMMA).append(' ').append(String.format(EncryptDistSQLConstants.ASSISTED_QUERY_COLUMN, ruleConfig.getAssistedQuery().get().getName()));
}
if (ruleConfig.getLikeQuery().isPresent()) {
result.append(EncryptDistSQLConstants.COMMA).append(' ').append(String.format(EncryptDistSQLConstants.LIKE_QUERY_COLUMN, ruleConfig.getLikeQuery().get().getName()));
result.append(DistSQLConstants.COMMA).append(' ').append(String.format(EncryptDistSQLConstants.LIKE_QUERY_COLUMN, ruleConfig.getLikeQuery().get().getName()));
}
return result.toString();
}
Expand All @@ -91,11 +92,11 @@ private String getEncryptAlgorithms(final EncryptColumnRuleConfiguration ruleCon
result.append(String.format(EncryptDistSQLConstants.ENCRYPT_ALGORITHM, AlgorithmDistSQLConverter.getAlgorithmType(encryptors.get(cipherEncryptorName))));
}
if (!Strings.isNullOrEmpty(assistedQueryEncryptorName)) {
result.append(EncryptDistSQLConstants.COMMA).append(' ')
result.append(DistSQLConstants.COMMA).append(' ')
.append(String.format(EncryptDistSQLConstants.ASSISTED_QUERY_ALGORITHM, AlgorithmDistSQLConverter.getAlgorithmType(encryptors.get(assistedQueryEncryptorName))));
}
if (!Strings.isNullOrEmpty(likeQueryEncryptorName)) {
result.append(EncryptDistSQLConstants.COMMA).append(' ')
result.append(DistSQLConstants.COMMA).append(' ')
.append(String.format(EncryptDistSQLConstants.LIKE_QUERY_ALGORITHM, AlgorithmDistSQLConverter.getAlgorithmType(encryptors.get(likeQueryEncryptorName))));
}
return result.toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,6 @@
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class ReadwriteSplittingDistSQLConstants {

public static final String COMMA = ",";

public static final String SEMI = ";";

public static final String CREATE_READWRITE_SPLITTING_RULE = "CREATE READWRITE_SPLITTING RULE";

public static final String READWRITE_SPLITTING_FOR_STATIC = " %s ("
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package org.apache.shardingsphere.readwritesplitting.distsql.handler.provider;

import com.google.common.base.Strings;
import org.apache.shardingsphere.distsql.handler.constant.DistSQLConstants;
import org.apache.shardingsphere.distsql.handler.engine.query.ral.convert.AlgorithmDistSQLConverter;
import org.apache.shardingsphere.distsql.handler.engine.query.ral.convert.RuleConfigurationToDistSQLConverter;
import org.apache.shardingsphere.infra.algorithm.core.config.AlgorithmConfiguration;
Expand All @@ -44,10 +45,10 @@ public String convert(final ReadwriteSplittingRuleConfiguration ruleConfig) {
while (iterator.hasNext()) {
appendStaticReadWriteSplittingRule(ruleConfig.getLoadBalancers(), iterator.next(), result);
if (iterator.hasNext()) {
result.append(ReadwriteSplittingDistSQLConstants.COMMA);
result.append(DistSQLConstants.COMMA);
}
}
result.append(ReadwriteSplittingDistSQLConstants.SEMI);
result.append(DistSQLConstants.SEMI);
return result.toString();
}

Expand All @@ -66,7 +67,7 @@ private String getReadDataSourceNames(final Collection<String> readDataSourceNam
while (iterator.hasNext()) {
result.append(String.format(ReadwriteSplittingDistSQLConstants.READ_RESOURCE, iterator.next()));
if (iterator.hasNext()) {
result.append(ReadwriteSplittingDistSQLConstants.COMMA);
result.append(DistSQLConstants.COMMA);
}
}
return result.toString();
Expand All @@ -76,7 +77,7 @@ private String getLoadBalancerType(final AlgorithmConfiguration algorithmConfig)
StringBuilder result = new StringBuilder();
String loadBalancerType = AlgorithmDistSQLConverter.getAlgorithmType(algorithmConfig);
if (!Strings.isNullOrEmpty(loadBalancerType)) {
result.append(ReadwriteSplittingDistSQLConstants.COMMA).append(System.lineSeparator()).append(loadBalancerType);
result.append(DistSQLConstants.COMMA).append(System.lineSeparator()).append(loadBalancerType);
}
return result.toString();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,6 @@
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class ShadowDistSQLConstants {

public static final String COMMA = ",";

public static final String SEMI = ";";

public static final String CREATE_SHADOW = "CREATE SHADOW RULE";

public static final String SHADOW = " %s("
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

package org.apache.shardingsphere.shadow.distsql.handler.provider;

import org.apache.shardingsphere.distsql.handler.constant.DistSQLConstants;
import org.apache.shardingsphere.distsql.handler.engine.query.ral.convert.AlgorithmDistSQLConverter;
import org.apache.shardingsphere.distsql.handler.engine.query.ral.convert.RuleConfigurationToDistSQLConverter;
import org.apache.shardingsphere.infra.algorithm.core.config.AlgorithmConfiguration;
Expand Down Expand Up @@ -47,10 +48,10 @@ public String convert(final ShadowRuleConfiguration ruleConfig) {
String shadowTables = getShadowTables(shadowRuleName, ruleConfig.getTables(), ruleConfig.getShadowAlgorithms());
result.append(String.format(ShadowDistSQLConstants.SHADOW, shadowRuleName, dataSourceConfig.getProductionDataSourceName(), dataSourceConfig.getShadowDataSourceName(), shadowTables));
if (iterator.hasNext()) {
result.append(ShadowDistSQLConstants.COMMA);
result.append(DistSQLConstants.COMMA);
}
}
result.append(ShadowDistSQLConstants.SEMI);
result.append(DistSQLConstants.SEMI);
return result.toString();
}

Expand All @@ -64,7 +65,7 @@ private String getShadowTables(final String shadowRuleName, final Map<String, Sh
result.append(String.format(ShadowDistSQLConstants.SHADOW_TABLE, shadowTableConfig.getKey(), shadowTableTypes));
}
if (iterator.hasNext()) {
result.append(ShadowDistSQLConstants.COMMA).append(System.lineSeparator());
result.append(DistSQLConstants.COMMA).append(System.lineSeparator());
}
}
return result.toString();
Expand All @@ -76,7 +77,7 @@ private String getShadowTableTypes(final Collection<String> shadowAlgorithmNames
while (iterator.hasNext()) {
result.append(AlgorithmDistSQLConverter.getAlgorithmType(algorithmConfigs.get(iterator.next())));
if (iterator.hasNext()) {
result.append(ShadowDistSQLConstants.COMMA).append(' ');
result.append(DistSQLConstants.COMMA).append(' ');
}
}
return result.toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,6 @@ public final class ShardingDistSQLConstants {

public static final String NONE = "";

public static final String COMMA = ",";

public static final String SEMI = ";";

public static final String CREATE_SHARDING_TABLE = "CREATE SHARDING TABLE RULE";

public static final String SHARDING_TABLE = " %s ("
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package org.apache.shardingsphere.sharding.distsql.handler.provider;

import com.google.common.base.Strings;
import org.apache.shardingsphere.distsql.handler.constant.DistSQLConstants;
import org.apache.shardingsphere.distsql.handler.engine.query.ral.convert.AlgorithmDistSQLConverter;
import org.apache.shardingsphere.distsql.handler.engine.query.ral.convert.RuleConfigurationToDistSQLConverter;
import org.apache.shardingsphere.infra.algorithm.core.config.AlgorithmConfiguration;
Expand Down Expand Up @@ -65,9 +66,9 @@ private void appendShardingTableRules(final ShardingRuleConfiguration ruleConfig
String autoTableRules = getAutoTableRules(ruleConfig);
stringBuilder.append(ShardingDistSQLConstants.CREATE_SHARDING_TABLE).append(tableRules);
if (!Strings.isNullOrEmpty(tableRules) && !Strings.isNullOrEmpty(autoTableRules)) {
stringBuilder.append(ShardingDistSQLConstants.COMMA);
stringBuilder.append(DistSQLConstants.COMMA);
}
stringBuilder.append(autoTableRules).append(ShardingDistSQLConstants.SEMI);
stringBuilder.append(autoTableRules).append(DistSQLConstants.SEMI);
}

private void appendShardingBindingTableRules(final ShardingRuleConfiguration ruleConfig, final StringBuilder stringBuilder) {
Expand All @@ -77,23 +78,23 @@ private void appendShardingBindingTableRules(final ShardingRuleConfiguration rul
ShardingTableReferenceRuleConfiguration referenceRuleConfig = iterator.next();
stringBuilder.append(String.format(ShardingDistSQLConstants.BINDING_TABLES, referenceRuleConfig.getName(), referenceRuleConfig.getReference()));
if (iterator.hasNext()) {
stringBuilder.append(ShardingDistSQLConstants.COMMA);
stringBuilder.append(DistSQLConstants.COMMA);
}
}
stringBuilder.append(ShardingDistSQLConstants.SEMI);
stringBuilder.append(DistSQLConstants.SEMI);
}

private void appendDefaultShardingStrategy(final ShardingRuleConfiguration ruleConfig, final StringBuilder result) {
if (null != ruleConfig.getDefaultDatabaseShardingStrategy()) {
appendStrategy(ruleConfig.getDefaultDatabaseShardingStrategy(), ShardingDistSQLConstants.DEFAULT_DATABASE_STRATEGY, result, ruleConfig.getShardingAlgorithms());
result.append(ShardingDistSQLConstants.SEMI);
result.append(DistSQLConstants.SEMI);
}
if (null != ruleConfig.getDefaultTableShardingStrategy()) {
if (null != ruleConfig.getDefaultDatabaseShardingStrategy()) {
result.append(System.lineSeparator()).append(System.lineSeparator());
}
appendStrategy(ruleConfig.getDefaultTableShardingStrategy(), ShardingDistSQLConstants.DEFAULT_TABLE_STRATEGY, result, ruleConfig.getShardingAlgorithms());
result.append(ShardingDistSQLConstants.SEMI);
result.append(DistSQLConstants.SEMI);
}
}

Expand All @@ -106,7 +107,7 @@ private String getTableRules(final ShardingRuleConfiguration ruleConfig) {
result.append(String.format(ShardingDistSQLConstants.SHARDING_TABLE, tableRuleConfig.getLogicTable(), tableRuleConfig.getActualDataNodes(),
appendTableStrategy(tableRuleConfig, ruleConfig)));
if (iterator.hasNext()) {
result.append(ShardingDistSQLConstants.COMMA);
result.append(DistSQLConstants.COMMA);
}
}
}
Expand All @@ -122,7 +123,7 @@ private String getAutoTableRules(final ShardingRuleConfiguration ruleConfig) {
result.append(String.format(ShardingDistSQLConstants.SHARDING_AUTO_TABLE, autoTableRuleConfig.getLogicTable(), autoTableRuleConfig.getActualDataSources(),
appendAutoTableStrategy(autoTableRuleConfig, ruleConfig)));
if (iterator.hasNext()) {
result.append(ShardingDistSQLConstants.COMMA);
result.append(DistSQLConstants.COMMA);
}
}
}
Expand Down Expand Up @@ -155,7 +156,7 @@ private void appendStrategy(final ShardingStrategyConfiguration strategyConfig,
return;
}
if (Objects.equals(strategyType, ShardingDistSQLConstants.DATABASE_STRATEGY) || Objects.equals(strategyType, ShardingDistSQLConstants.TABLE_STRATEGY)) {
stringBuilder.append(ShardingDistSQLConstants.COMMA).append(System.lineSeparator());
stringBuilder.append(DistSQLConstants.COMMA).append(System.lineSeparator());
}
String type = strategyConfig.getType().toLowerCase();
String algorithmDefinition = AlgorithmDistSQLConverter.getAlgorithmType(shardingAlgorithms.get(strategyConfig.getShardingAlgorithmName()));
Expand Down Expand Up @@ -184,14 +185,14 @@ private void appendKeyGenerateStrategy(final Map<String, AlgorithmConfiguration>
if (null == keyGenerateStrategyConfig) {
return;
}
stringBuilder.append(ShardingDistSQLConstants.COMMA).append(System.lineSeparator());
stringBuilder.append(DistSQLConstants.COMMA).append(System.lineSeparator());
String algorithmDefinition = AlgorithmDistSQLConverter.getAlgorithmType(keyGenerators.get(keyGenerateStrategyConfig.getKeyGeneratorName()));
stringBuilder.append(String.format(ShardingDistSQLConstants.KEY_GENERATOR_STRATEGY, keyGenerateStrategyConfig.getColumn(), algorithmDefinition));
}

private void appendAuditStrategy(final Map<String, AlgorithmConfiguration> auditors, final ShardingAuditStrategyConfiguration auditStrategy, final StringBuilder stringBuilder) {
if (null != auditStrategy) {
stringBuilder.append(ShardingDistSQLConstants.COMMA).append(System.lineSeparator());
stringBuilder.append(DistSQLConstants.COMMA).append(System.lineSeparator());
stringBuilder.append(String.format(ShardingDistSQLConstants.AUDIT_STRATEGY, getAlgorithmTypes(auditors, auditStrategy.getAuditorNames()), auditStrategy.isAllowHintDisable()));
}
}
Expand All @@ -203,7 +204,7 @@ private String getAlgorithmTypes(final Map<String, AlgorithmConfiguration> audit
while (iterator.hasNext()) {
result.append(AlgorithmDistSQLConverter.getAlgorithmType(auditors.get(iterator.next())));
if (iterator.hasNext()) {
result.append(ShardingDistSQLConstants.COMMA);
result.append(DistSQLConstants.COMMA);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* 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.distsql.handler.constant;

import lombok.AccessLevel;
import lombok.NoArgsConstructor;

/**
* DistSQL constants.
*/
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class DistSQLConstants {

public static final String COMMA = ",";

public static final String SEMI = ";";
}

0 comments on commit 406339a

Please sign in to comment.