EncryptColumnsMergedResult.java 5.4 KB
Newer Older
T
terrymanu 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*
 * 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.encrypt.merge.dal.impl;

20
import com.google.common.base.Preconditions;
21
import org.apache.shardingsphere.encrypt.metadata.EncryptColumnMetaData;
22
import org.apache.shardingsphere.infra.metadata.schema.model.physical.PhysicalColumnMetaData;
23
import org.apache.shardingsphere.infra.metadata.schema.ShardingSphereSchema;
24 25
import org.apache.shardingsphere.infra.binder.statement.SQLStatementContext;
import org.apache.shardingsphere.infra.binder.type.TableAvailable;
26
import org.apache.shardingsphere.infra.merge.result.MergedResult;
T
terrymanu 已提交
27 28 29 30 31

import java.io.InputStream;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
import java.util.Calendar;
32 33 34
import java.util.Collection;
import java.util.LinkedList;
import java.util.Map.Entry;
L
Liang Zhang 已提交
35
import java.util.Optional;
36
import java.util.stream.Collectors;
T
terrymanu 已提交
37 38

/**
39
 * Encrypt column merged result.
T
terrymanu 已提交
40
 */
41
public abstract class EncryptColumnsMergedResult implements MergedResult {
T
terrymanu 已提交
42
    
43
    private final ShardingSphereSchema schema;
T
terrymanu 已提交
44
    
45
    private final String tableName;
T
terrymanu 已提交
46
    
47 48
    protected EncryptColumnsMergedResult(final SQLStatementContext sqlStatementContext, final ShardingSphereSchema schema) {
        this.schema = schema;
49 50
        Preconditions.checkState(sqlStatementContext instanceof TableAvailable && 1 == ((TableAvailable) sqlStatementContext).getAllTables().size());
        tableName = ((TableAvailable) sqlStatementContext).getAllTables().iterator().next().getTableName().getIdentifier().getValue();
T
terrymanu 已提交
51 52 53
    }
    
    @Override
54
    public final boolean next() throws SQLException { 
T
terrymanu 已提交
55
        boolean hasNext = nextValue();
T
terrymanu 已提交
56
        if (hasNext && getTableEncryptColumnMetaDataList().isEmpty()) {
T
terrymanu 已提交
57 58 59 60 61 62
            return true;
        }
        if (!hasNext) {
            return false;
        }
        String columnName = getOriginalValue(1, String.class).toString();
63
        while (getAssistedQueryColumns().contains(columnName) || getPlainColumns().contains(columnName)) {
T
terrymanu 已提交
64 65 66 67
            hasNext = nextValue();
            if (!hasNext) {
                return false;
            }
68
            columnName = getOriginalValue(1, String.class).toString();
T
terrymanu 已提交
69 70 71 72
        }
        return true;
    }
    
73
    private Collection<String> getAssistedQueryColumns() {
T
terrymanu 已提交
74
        return getTableEncryptColumnMetaDataList().stream().map(EncryptColumnMetaData::getAssistedQueryColumnName)
75 76 77 78
                .collect(Collectors.toList());
    }
    
    private Collection<String> getPlainColumns() {
T
terrymanu 已提交
79
        return getTableEncryptColumnMetaDataList().stream().map(EncryptColumnMetaData::getPlainColumnName)
80 81 82
                .collect(Collectors.toList());
    }
    
T
terrymanu 已提交
83
    private Collection<EncryptColumnMetaData> getTableEncryptColumnMetaDataList() {
84
        Collection<EncryptColumnMetaData> result = new LinkedList<>();
85
        for (Entry<String, PhysicalColumnMetaData> entry : schema.get(tableName).getColumns().entrySet()) {
86 87 88 89 90 91 92
            if (entry.getValue() instanceof EncryptColumnMetaData) {
                result.add((EncryptColumnMetaData) entry.getValue());
            }
        }
        return result;
    }
    
T
terrymanu 已提交
93 94 95
    @Override
    public final Object getValue(final int columnIndex, final Class<?> type) throws SQLException {
        if (1 == columnIndex) {
L
Liang Zhang 已提交
96
            String columnName = getOriginalValue(1, type).toString();
97
            Optional<String> logicColumn = getLogicColumnOfCipher(columnName);
98
            return logicColumn.orElse(columnName);
T
terrymanu 已提交
99 100 101 102
        }
        return getOriginalValue(columnIndex, type);
    }
    
103
    private Optional<String> getLogicColumnOfCipher(final String cipherColumn) {
104
        for (Entry<String, PhysicalColumnMetaData> entry : schema.get(tableName).getColumns().entrySet()) {
105 106 107 108 109 110 111 112 113 114
            if (entry.getValue() instanceof EncryptColumnMetaData) {
                EncryptColumnMetaData encryptColumnMetaData = (EncryptColumnMetaData) entry.getValue();
                if (encryptColumnMetaData.getCipherColumnName().equalsIgnoreCase(cipherColumn)) {
                    return Optional.of(entry.getKey());
                }
            }
        }
        return Optional.empty();
    }
    
T
terrymanu 已提交
115 116
    @Override
    public final Object getCalendarValue(final int columnIndex, final Class<?> type, final Calendar calendar) throws SQLException {
L
Liang Zhang 已提交
117
        throw new SQLFeatureNotSupportedException("");
T
terrymanu 已提交
118 119 120 121
    }
    
    @Override
    public final InputStream getInputStream(final int columnIndex, final String type) throws SQLException {
L
Liang Zhang 已提交
122
        throw new SQLFeatureNotSupportedException("");
T
terrymanu 已提交
123 124 125 126 127 128
    }
    
    protected abstract boolean nextValue() throws SQLException;
    
    protected abstract Object getOriginalValue(int columnIndex, Class<?> type) throws SQLException;
}