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.mask.merge.dql;
19  
20  import org.apache.shardingsphere.infra.binder.context.statement.type.dml.SelectStatementContext;
21  import org.apache.shardingsphere.infra.merge.result.MergedResult;
22  import org.apache.shardingsphere.infra.merge.result.impl.decorator.DecoratorMergedResult;
23  import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
24  import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
25  import org.apache.shardingsphere.mask.rule.MaskRule;
26  import org.apache.shardingsphere.mask.spi.MaskAlgorithm;
27  import org.apache.shardingsphere.sql.parser.statement.core.segment.generic.bound.ColumnSegmentBoundInfo;
28  
29  import java.sql.SQLException;
30  import java.util.Optional;
31  
32  /**
33   * Merged result for mask.
34   */
35  public final class MaskMergedResult extends DecoratorMergedResult {
36      
37      private final ShardingSphereDatabase database;
38      
39      private final ShardingSphereMetaData metaData;
40      
41      private final SelectStatementContext selectStatementContext;
42      
43      public MaskMergedResult(final ShardingSphereDatabase database, final ShardingSphereMetaData metaData, final SelectStatementContext selectStatementContext, final MergedResult mergedResult) {
44          super(mergedResult);
45          this.database = database;
46          this.metaData = metaData;
47          this.selectStatementContext = selectStatementContext;
48      }
49      
50      @SuppressWarnings({"rawtypes", "unchecked"})
51      @Override
52      public Object getValue(final int columnIndex, final Class<?> type) throws SQLException {
53          Optional<ColumnSegmentBoundInfo> columnSegmentBoundInfo = selectStatementContext.findColumnBoundInfo(columnIndex);
54          if (!columnSegmentBoundInfo.isPresent()) {
55              return getMergedResult().getValue(columnIndex, type);
56          }
57          String originalTableName = columnSegmentBoundInfo.get().getOriginalTable().getValue();
58          String originalColumnName = columnSegmentBoundInfo.get().getOriginalColumn().getValue();
59          ShardingSphereDatabase database = metaData.containsDatabase(columnSegmentBoundInfo.get().getOriginalDatabase().getValue())
60                  ? metaData.getDatabase(columnSegmentBoundInfo.get().getOriginalDatabase().getValue())
61                  : this.database;
62          Optional<MaskRule> rule = database.getRuleMetaData().findSingleRule(MaskRule.class);
63          if (!rule.isPresent() || !rule.get().findMaskTable(originalTableName).map(optional -> optional.findAlgorithm(originalColumnName).isPresent()).orElse(false)) {
64              return getMergedResult().getValue(columnIndex, type);
65          }
66          Optional<MaskAlgorithm> maskAlgorithm = rule.get().findMaskTable(originalTableName).flatMap(optional -> optional.findAlgorithm(originalColumnName));
67          if (!maskAlgorithm.isPresent()) {
68              return getMergedResult().getValue(columnIndex, type);
69          }
70          Object originalValue = getMergedResult().getValue(columnIndex, Object.class);
71          return null == originalValue ? null : maskAlgorithm.get().mask(originalValue);
72      }
73  }