View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.shardingsphere.infra.yaml.data.swapper;
19  
20  import lombok.RequiredArgsConstructor;
21  import org.apache.shardingsphere.infra.metadata.statistics.ShardingSphereRowData;
22  import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereColumn;
23  import org.apache.shardingsphere.infra.util.yaml.swapper.YamlConfigurationSwapper;
24  import org.apache.shardingsphere.infra.yaml.data.pojo.YamlShardingSphereRowData;
25  
26  import java.math.BigDecimal;
27  import java.sql.Types;
28  import java.util.Collection;
29  import java.util.Collections;
30  import java.util.LinkedList;
31  import java.util.List;
32  
33  /**
34   * YAML ShardingSphere row data swapper.
35   */
36  @RequiredArgsConstructor
37  public final class YamlShardingSphereRowDataSwapper implements YamlConfigurationSwapper<YamlShardingSphereRowData, ShardingSphereRowData> {
38      
39      private final List<ShardingSphereColumn> columns;
40      
41      @Override
42      public YamlShardingSphereRowData swapToYamlConfiguration(final ShardingSphereRowData data) {
43          YamlShardingSphereRowData result = new YamlShardingSphereRowData();
44          Collection<Object> rowData = null == data.getRows() ? Collections.emptyList() : data.getRows();
45          List<Object> yamlRowData = new LinkedList<>();
46          int count = 0;
47          for (Object each : rowData) {
48              yamlRowData.add(convertDataType(each, columns.get(count++).getDataType()));
49          }
50          result.setRows(yamlRowData);
51          result.setUniqueKey(data.getUniqueKey());
52          return result;
53      }
54      
55      private Object convertDataType(final Object data, final int dataType) {
56          if (Types.DECIMAL == dataType || Types.BIGINT == dataType) {
57              return null == data ? null : data.toString();
58          }
59          // TODO use general type convertor
60          return data;
61      }
62      
63      @Override
64      public ShardingSphereRowData swapToObject(final YamlShardingSphereRowData yamlConfig) {
65          Collection<Object> yamlRow = null == yamlConfig.getRows() ? Collections.emptyList() : yamlConfig.getRows();
66          List<Object> rowData = new LinkedList<>();
67          int count = 0;
68          for (Object each : yamlRow) {
69              ShardingSphereColumn column = columns.get(count++);
70              rowData.add(convertByDataType(each, column.getDataType()));
71          }
72          return new ShardingSphereRowData(yamlConfig.getUniqueKey(), rowData);
73      }
74      
75      private Object convertByDataType(final Object data, final int dataType) {
76          if (null == data) {
77              return null;
78          }
79          if (Types.DECIMAL == dataType) {
80              return new BigDecimal(data.toString());
81          }
82          if (Types.BIGINT == dataType) {
83              return Long.valueOf(data.toString());
84          }
85          if (Types.REAL == dataType || Types.FLOAT == dataType) {
86              return Float.parseFloat(data.toString());
87          }
88          // TODO use general type convertor
89          return data;
90      }
91  }