Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add convert rule configuration provider for Single table. #30544

Merged
merged 1 commit into from
Mar 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 = ", ";
RaigorJiang marked this conversation as resolved.
Show resolved Hide resolved

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