Skip to content

Commit

Permalink
add convert rule configuration provider for Single table.
Browse files Browse the repository at this point in the history
  • Loading branch information
dongzl committed Mar 19, 2024
1 parent 1107d39 commit 2179e36
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* 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.single.distsql.handler.constant;

import lombok.AccessLevel;
import lombok.NoArgsConstructor;

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

public static final String COMMA = ", ";

public static final String SEMI = ";";

public static final String LOAD_SINGLE_TABLE = "LOAD SINGLE TABLE ";

public static final String DATASOURCE_AND_TABLE = "%s.%s";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* 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.single.distsql.handler.provider;

import org.apache.shardingsphere.distsql.handler.engine.query.ral.convert.ConvertRuleConfigurationProvider;
import org.apache.shardingsphere.infra.config.rule.RuleConfiguration;
import org.apache.shardingsphere.single.api.config.SingleRuleConfiguration;
import org.apache.shardingsphere.single.distsql.handler.constant.SingleDistSQLConstants;

import java.util.Iterator;

/**
* Single convert rule configuration provider.
*/
public final class SingleConvertRuleConfigurationProvider implements ConvertRuleConfigurationProvider {

@Override
public String convert(final RuleConfiguration ruleConfig) {
return getSingleDistSQL((SingleRuleConfiguration) ruleConfig);
}

private String getSingleDistSQL(final SingleRuleConfiguration ruleConfig) {
if (ruleConfig.getTables().isEmpty()) {
return "";
}
StringBuilder result = new StringBuilder(SingleDistSQLConstants.LOAD_SINGLE_TABLE);
Iterator<String> iterator = ruleConfig.getTables().iterator();
while (iterator.hasNext()) {
String tableName = iterator.next();
result.append(String.format(SingleDistSQLConstants.DATASOURCE_AND_TABLE, ruleConfig.getDefaultDataSource(), tableName));
if (iterator.hasNext()) {
result.append(SingleDistSQLConstants.COMMA);
}
}
result.append(SingleDistSQLConstants.SEMI).append(System.lineSeparator()).append(System.lineSeparator());
return result.toString();
}

@Override
public Class<? extends RuleConfiguration> getType() {
return SingleRuleConfiguration.class;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#
# 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.
#

org.apache.shardingsphere.single.distsql.handler.provider.SingleConvertRuleConfigurationProvider

0 comments on commit 2179e36

Please sign in to comment.