Skip to content

Commit

Permalink
Add YamlColumnSwapper (#33247)
Browse files Browse the repository at this point in the history
  • Loading branch information
terrymanu authored Oct 14, 2024
1 parent 6d5cf99 commit cb70d6b
Show file tree
Hide file tree
Showing 4 changed files with 159 additions and 63 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* 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.infra.yaml.schema.swapper;

import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereColumn;
import org.apache.shardingsphere.infra.util.yaml.swapper.YamlConfigurationSwapper;
import org.apache.shardingsphere.infra.yaml.schema.pojo.YamlShardingSphereColumn;

/**
* YAML column swapper.
*/
public final class YamlColumnSwapper implements YamlConfigurationSwapper<YamlShardingSphereColumn, ShardingSphereColumn> {

@Override
public YamlShardingSphereColumn swapToYamlConfiguration(final ShardingSphereColumn data) {
YamlShardingSphereColumn result = new YamlShardingSphereColumn();
result.setName(data.getName());
result.setDataType(data.getDataType());
result.setPrimaryKey(data.isPrimaryKey());
result.setGenerated(data.isGenerated());
result.setCaseSensitive(data.isCaseSensitive());
result.setVisible(data.isVisible());
result.setUnsigned(data.isUnsigned());
result.setNullable(data.isNullable());
return result;
}

@Override
public ShardingSphereColumn swapToObject(final YamlShardingSphereColumn yamlConfig) {
return new ShardingSphereColumn(yamlConfig.getName(), yamlConfig.getDataType(),
yamlConfig.isPrimaryKey(), yamlConfig.isGenerated(), yamlConfig.isCaseSensitive(), yamlConfig.isVisible(), yamlConfig.isUnsigned(), yamlConfig.isNullable());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* 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.infra.yaml.schema.swapper;

import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereConstraint;
import org.apache.shardingsphere.infra.util.yaml.swapper.YamlConfigurationSwapper;
import org.apache.shardingsphere.infra.yaml.schema.pojo.YamlShardingSphereConstraint;

/**
* YAML constraint swapper.
*/
public final class YamlConstraintSwapper implements YamlConfigurationSwapper<YamlShardingSphereConstraint, ShardingSphereConstraint> {

@Override
public YamlShardingSphereConstraint swapToYamlConfiguration(final ShardingSphereConstraint data) {
YamlShardingSphereConstraint result = new YamlShardingSphereConstraint();
result.setName(data.getName());
result.setReferencedTableName(data.getReferencedTableName());
return result;
}

@Override
public ShardingSphereConstraint swapToObject(final YamlShardingSphereConstraint yamlConfig) {
return new ShardingSphereConstraint(yamlConfig.getName(), yamlConfig.getReferencedTableName());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* 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.infra.yaml.schema.swapper;

import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereIndex;
import org.apache.shardingsphere.infra.util.yaml.swapper.YamlConfigurationSwapper;
import org.apache.shardingsphere.infra.yaml.schema.pojo.YamlShardingSphereIndex;

/**
* YAML index swapper.
*/
public final class YamlIndexSwapper implements YamlConfigurationSwapper<YamlShardingSphereIndex, ShardingSphereIndex> {

@Override
public YamlShardingSphereIndex swapToYamlConfiguration(final ShardingSphereIndex data) {
YamlShardingSphereIndex result = new YamlShardingSphereIndex();
result.setName(data.getName());
result.getColumns().addAll(data.getColumns());
result.setUnique(data.isUnique());
return result;
}

@Override
public ShardingSphereIndex swapToObject(final YamlShardingSphereIndex yamlConfig) {
ShardingSphereIndex result = new ShardingSphereIndex(yamlConfig.getName());
result.getColumns().addAll(yamlConfig.getColumns());
result.setUnique(yamlConfig.isUnique());
return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,88 +38,50 @@
*/
public final class YamlTableSwapper implements YamlConfigurationSwapper<YamlShardingSphereTable, ShardingSphereTable> {

private final YamlColumnSwapper columnSwapper = new YamlColumnSwapper();

private final YamlIndexSwapper indexSwapper = new YamlIndexSwapper();

private final YamlConstraintSwapper constraintSwapper = new YamlConstraintSwapper();

@Override
public YamlShardingSphereTable swapToYamlConfiguration(final ShardingSphereTable table) {
YamlShardingSphereTable result = new YamlShardingSphereTable();
result.setColumns(swapYamlColumns(table.getColumnValues()));
result.setIndexes(swapYamlIndexes(table.getIndexValues()));
result.setConstraints(swapYamlConstraints(table.getConstraintValues()));
result.setName(table.getName());
result.setColumns(swapToYamlColumns(table.getColumnValues()));
result.setIndexes(swapToYamlIndexes(table.getIndexValues()));
result.setConstraints(swapToYamlConstraints(table.getConstraintValues()));
result.setType(table.getType());
return result;
}

@Override
public ShardingSphereTable swapToObject(final YamlShardingSphereTable yamlConfig) {
return new ShardingSphereTable(
yamlConfig.getName(), swapColumns(yamlConfig.getColumns()), swapIndexes(yamlConfig.getIndexes()), swapConstraints(yamlConfig.getConstraints()), yamlConfig.getType());
}

private Collection<ShardingSphereConstraint> swapConstraints(final Map<String, YamlShardingSphereConstraint> constraints) {
return null == constraints ? Collections.emptyList() : constraints.values().stream().map(this::swapConstraint).collect(Collectors.toList());
}

private ShardingSphereConstraint swapConstraint(final YamlShardingSphereConstraint constraint) {
return new ShardingSphereConstraint(constraint.getName(), constraint.getReferencedTableName());
private Map<String, YamlShardingSphereColumn> swapToYamlColumns(final Collection<ShardingSphereColumn> columns) {
return columns.stream().collect(Collectors.toMap(key -> key.getName().toLowerCase(), columnSwapper::swapToYamlConfiguration, (oldValue, currentValue) -> oldValue, LinkedHashMap::new));
}

private Collection<ShardingSphereIndex> swapIndexes(final Map<String, YamlShardingSphereIndex> indexes) {
return null == indexes ? Collections.emptyList() : indexes.values().stream().map(this::swapIndex).collect(Collectors.toList());
}

private ShardingSphereIndex swapIndex(final YamlShardingSphereIndex index) {
ShardingSphereIndex result = new ShardingSphereIndex(index.getName());
result.getColumns().addAll(index.getColumns());
result.setUnique(index.isUnique());
return result;
private Map<String, YamlShardingSphereIndex> swapToYamlIndexes(final Collection<ShardingSphereIndex> indexes) {
return indexes.stream().collect(Collectors.toMap(key -> key.getName().toLowerCase(), indexSwapper::swapToYamlConfiguration, (oldValue, currentValue) -> oldValue, LinkedHashMap::new));
}

private Collection<ShardingSphereColumn> swapColumns(final Map<String, YamlShardingSphereColumn> indexes) {
return null == indexes ? Collections.emptyList() : indexes.values().stream().map(this::swapColumn).collect(Collectors.toList());
private Map<String, YamlShardingSphereConstraint> swapToYamlConstraints(final Collection<ShardingSphereConstraint> constrains) {
return constrains.stream().collect(Collectors.toMap(key -> key.getName().toLowerCase(), constraintSwapper::swapToYamlConfiguration, (oldValue, currentValue) -> oldValue, LinkedHashMap::new));
}

private ShardingSphereColumn swapColumn(final YamlShardingSphereColumn column) {
return new ShardingSphereColumn(column.getName(), column.getDataType(), column.isPrimaryKey(), column.isGenerated(), column.isCaseSensitive(), column.isVisible(), column.isUnsigned(),
column.isNullable());
}

private Map<String, YamlShardingSphereConstraint> swapYamlConstraints(final Collection<ShardingSphereConstraint> constrains) {
return constrains.stream().collect(Collectors.toMap(key -> key.getName().toLowerCase(), this::swapYamlConstraint, (oldValue, currentValue) -> oldValue, LinkedHashMap::new));
}

private YamlShardingSphereConstraint swapYamlConstraint(final ShardingSphereConstraint constraint) {
YamlShardingSphereConstraint result = new YamlShardingSphereConstraint();
result.setName(constraint.getName());
result.setReferencedTableName(constraint.getReferencedTableName());
return result;
}

private Map<String, YamlShardingSphereIndex> swapYamlIndexes(final Collection<ShardingSphereIndex> indexes) {
return indexes.stream().collect(Collectors.toMap(key -> key.getName().toLowerCase(), this::swapYamlIndex, (oldValue, currentValue) -> oldValue, LinkedHashMap::new));
@Override
public ShardingSphereTable swapToObject(final YamlShardingSphereTable yamlConfig) {
return new ShardingSphereTable(yamlConfig.getName(),
swapToColumns(yamlConfig.getColumns()), swapToIndexes(yamlConfig.getIndexes()), swapToConstraints(yamlConfig.getConstraints()), yamlConfig.getType());
}

private YamlShardingSphereIndex swapYamlIndex(final ShardingSphereIndex index) {
YamlShardingSphereIndex result = new YamlShardingSphereIndex();
result.setName(index.getName());
result.getColumns().addAll(index.getColumns());
result.setUnique(index.isUnique());
return result;
private Collection<ShardingSphereColumn> swapToColumns(final Map<String, YamlShardingSphereColumn> columns) {
return null == columns ? Collections.emptyList() : columns.values().stream().map(columnSwapper::swapToObject).collect(Collectors.toList());
}

private Map<String, YamlShardingSphereColumn> swapYamlColumns(final Collection<ShardingSphereColumn> columns) {
return columns.stream().collect(Collectors.toMap(key -> key.getName().toLowerCase(), this::swapYamlColumn, (oldValue, currentValue) -> oldValue, LinkedHashMap::new));
private Collection<ShardingSphereIndex> swapToIndexes(final Map<String, YamlShardingSphereIndex> indexes) {
return null == indexes ? Collections.emptyList() : indexes.values().stream().map(indexSwapper::swapToObject).collect(Collectors.toList());
}

private YamlShardingSphereColumn swapYamlColumn(final ShardingSphereColumn column) {
YamlShardingSphereColumn result = new YamlShardingSphereColumn();
result.setName(column.getName());
result.setCaseSensitive(column.isCaseSensitive());
result.setGenerated(column.isGenerated());
result.setPrimaryKey(column.isPrimaryKey());
result.setDataType(column.getDataType());
result.setVisible(column.isVisible());
result.setUnsigned(column.isUnsigned());
result.setNullable(column.isNullable());
return result;
private Collection<ShardingSphereConstraint> swapToConstraints(final Map<String, YamlShardingSphereConstraint> constraints) {
return null == constraints ? Collections.emptyList() : constraints.values().stream().map(constraintSwapper::swapToObject).collect(Collectors.toList());
}
}

0 comments on commit cb70d6b

Please sign in to comment.