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 lombok.RequiredArgsConstructor;
21  import org.apache.shardingsphere.infra.binder.context.segment.select.projection.impl.ColumnProjection;
22  import org.apache.shardingsphere.infra.binder.context.statement.dml.SelectStatementContext;
23  import org.apache.shardingsphere.infra.merge.result.MergedResult;
24  import org.apache.shardingsphere.mask.rule.MaskRule;
25  import org.apache.shardingsphere.mask.rule.MaskTable;
26  import org.apache.shardingsphere.mask.spi.MaskAlgorithm;
27  
28  import java.io.InputStream;
29  import java.io.Reader;
30  import java.sql.SQLException;
31  import java.util.Calendar;
32  import java.util.Optional;
33  
34  /**
35   * Merged result for mask.
36   */
37  @RequiredArgsConstructor
38  public final class MaskMergedResult implements MergedResult {
39      
40      private final MaskRule maskRule;
41      
42      private final SelectStatementContext selectStatementContext;
43      
44      private final MergedResult mergedResult;
45      
46      @Override
47      public boolean next() throws SQLException {
48          return mergedResult.next();
49      }
50      
51      @SuppressWarnings({"rawtypes", "unchecked"})
52      @Override
53      public Object getValue(final int columnIndex, final Class<?> type) throws SQLException {
54          Optional<ColumnProjection> columnProjection = selectStatementContext.getProjectionsContext().findColumnProjection(columnIndex);
55          if (!columnProjection.isPresent()) {
56              return mergedResult.getValue(columnIndex, type);
57          }
58          Optional<MaskTable> maskTable = maskRule.findMaskTable(columnProjection.get().getOriginalTable().getValue());
59          if (!maskTable.isPresent()) {
60              return mergedResult.getValue(columnIndex, type);
61          }
62          Optional<MaskAlgorithm> maskAlgorithm = maskTable.get().findAlgorithm(columnProjection.get().getName().getValue());
63          if (!maskAlgorithm.isPresent()) {
64              return mergedResult.getValue(columnIndex, type);
65          }
66          Object originalValue = mergedResult.getValue(columnIndex, Object.class);
67          return null == originalValue ? null : maskAlgorithm.get().mask(originalValue);
68      }
69      
70      @Override
71      public Object getCalendarValue(final int columnIndex, final Class<?> type, final Calendar calendar) throws SQLException {
72          return mergedResult.getCalendarValue(columnIndex, type, calendar);
73      }
74      
75      @Override
76      public InputStream getInputStream(final int columnIndex, final String type) throws SQLException {
77          return mergedResult.getInputStream(columnIndex, type);
78      }
79      
80      @Override
81      public Reader getCharacterStream(final int columnIndex) throws SQLException {
82          return mergedResult.getCharacterStream(columnIndex);
83      }
84      
85      @Override
86      public boolean wasNull() throws SQLException {
87          return mergedResult.wasNull();
88      }
89  }