Skip to content

Commit

Permalink
Remove MaskAlgorithmMetaData
Browse files Browse the repository at this point in the history
  • Loading branch information
terrymanu committed Mar 19, 2024
1 parent b8cac4c commit 3c146b8
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 145 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import org.apache.shardingsphere.infra.merge.engine.decorator.ResultDecoratorEngine;
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import org.apache.shardingsphere.mask.constant.MaskOrder;
import org.apache.shardingsphere.mask.merge.dql.MaskAlgorithmMetaData;
import org.apache.shardingsphere.mask.merge.dql.MaskDQLResultDecorator;
import org.apache.shardingsphere.mask.rule.MaskRule;

Expand All @@ -38,9 +37,7 @@ public final class MaskResultDecoratorEngine implements ResultDecoratorEngine<Ma
@Override
public Optional<ResultDecorator<MaskRule>> newInstance(final ShardingSphereDatabase database,
final MaskRule maskRule, final ConfigurationProperties props, final SQLStatementContext sqlStatementContext) {
return sqlStatementContext instanceof SelectStatementContext
? Optional.of(new MaskDQLResultDecorator(new MaskAlgorithmMetaData(maskRule, (SelectStatementContext) sqlStatementContext)))
: Optional.empty();
return sqlStatementContext instanceof SelectStatementContext ? Optional.of(new MaskDQLResultDecorator(maskRule, (SelectStatementContext) sqlStatementContext)) : Optional.empty();
}

@Override
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.infra.binder.context.statement.SQLStatementContext;
import org.apache.shardingsphere.infra.binder.context.statement.dml.SelectStatementContext;
import org.apache.shardingsphere.infra.executor.sql.execute.result.query.QueryResult;
import org.apache.shardingsphere.infra.merge.engine.decorator.ResultDecorator;
import org.apache.shardingsphere.infra.merge.result.MergedResult;
Expand All @@ -31,15 +32,17 @@
@RequiredArgsConstructor
public final class MaskDQLResultDecorator implements ResultDecorator<MaskRule> {

private final MaskAlgorithmMetaData metaData;
private final MaskRule maskRule;

private final SelectStatementContext selectStatementContext;

@Override
public MergedResult decorate(final QueryResult queryResult, final SQLStatementContext sqlStatementContext, final MaskRule rule) {
return new MaskMergedResult(metaData, new TransparentMergedResult(queryResult));
return new MaskMergedResult(maskRule, selectStatementContext, new TransparentMergedResult(queryResult));
}

@Override
public MergedResult decorate(final MergedResult mergedResult, final SQLStatementContext sqlStatementContext, final MaskRule rule) {
return new MaskMergedResult(metaData, mergedResult);
return new MaskMergedResult(maskRule, selectStatementContext, mergedResult);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@
package org.apache.shardingsphere.mask.merge.dql;

import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.infra.binder.context.segment.select.projection.impl.ColumnProjection;
import org.apache.shardingsphere.infra.binder.context.statement.dml.SelectStatementContext;
import org.apache.shardingsphere.infra.merge.result.MergedResult;
import org.apache.shardingsphere.mask.rule.MaskRule;
import org.apache.shardingsphere.mask.spi.MaskAlgorithm;

import java.io.InputStream;
Expand All @@ -33,7 +36,9 @@
@RequiredArgsConstructor
public final class MaskMergedResult implements MergedResult {

private final MaskAlgorithmMetaData metaData;
private final MaskRule maskRule;

private final SelectStatementContext selectStatementContext;

private final MergedResult mergedResult;

Expand All @@ -45,7 +50,11 @@ public boolean next() throws SQLException {
@SuppressWarnings({"rawtypes", "unchecked"})
@Override
public Object getValue(final int columnIndex, final Class<?> type) throws SQLException {
Optional<MaskAlgorithm> maskAlgorithm = metaData.findMaskAlgorithm(columnIndex);
Optional<ColumnProjection> columnProjection = selectStatementContext.getProjectionsContext().findColumnProjection(columnIndex);
if (!columnProjection.isPresent()) {
return mergedResult.getValue(columnIndex, type);
}
Optional<MaskAlgorithm> maskAlgorithm = maskRule.findAlgorithm(columnProjection.get().getOriginalTable().getValue(), columnProjection.get().getName().getValue());
if (!maskAlgorithm.isPresent()) {
return mergedResult.getValue(columnIndex, type);
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

package org.apache.shardingsphere.mask.merge.dql;

import org.apache.shardingsphere.infra.binder.context.statement.dml.SelectStatementContext;
import org.apache.shardingsphere.mask.rule.MaskRule;
import org.apache.shardingsphere.infra.binder.context.statement.SQLStatementContext;
import org.apache.shardingsphere.infra.executor.sql.execute.result.query.QueryResult;
Expand All @@ -35,7 +36,7 @@ class MaskDQLResultDecoratorTest {
void assertDecorateQueryResult() throws SQLException {
QueryResult queryResult = mock(QueryResult.class);
when(queryResult.next()).thenReturn(true);
MaskDQLResultDecorator decorator = new MaskDQLResultDecorator(mock(MaskAlgorithmMetaData.class));
MaskDQLResultDecorator decorator = new MaskDQLResultDecorator(mock(MaskRule.class), mock(SelectStatementContext.class));
MergedResult actual = decorator.decorate(queryResult, mock(SQLStatementContext.class), mock(MaskRule.class));
assertTrue(actual.next());
}
Expand All @@ -44,7 +45,7 @@ void assertDecorateQueryResult() throws SQLException {
void assertDecorateMergedResult() throws SQLException {
MergedResult mergedResult = mock(MergedResult.class);
when(mergedResult.next()).thenReturn(true);
MaskDQLResultDecorator decorator = new MaskDQLResultDecorator(mock(MaskAlgorithmMetaData.class));
MaskDQLResultDecorator decorator = new MaskDQLResultDecorator(mock(MaskRule.class), mock(SelectStatementContext.class));
MergedResult actual = decorator.decorate(mergedResult, mock(SQLStatementContext.class), mock(MaskRule.class));
assertTrue(actual.next());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@

package org.apache.shardingsphere.mask.merge.dql;

import org.apache.shardingsphere.infra.binder.context.segment.select.projection.impl.ColumnProjection;
import org.apache.shardingsphere.infra.binder.context.statement.dml.SelectStatementContext;
import org.apache.shardingsphere.infra.merge.result.MergedResult;
import org.apache.shardingsphere.mask.rule.MaskRule;
import org.apache.shardingsphere.mask.spi.MaskAlgorithm;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
Expand All @@ -34,55 +37,68 @@
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

@ExtendWith(MockitoExtension.class)
class MaskMergedResultTest {

@Mock
private MaskAlgorithmMetaData metaData;

@Mock
private MergedResult mergedResult;

@Test
void assertNext() throws SQLException {
assertFalse(new MaskMergedResult(metaData, mergedResult).next());
assertFalse(new MaskMergedResult(mock(MaskRule.class), mock(SelectStatementContext.class), mergedResult).next());
}

@Test
void assertGetValue() throws SQLException {
when(mergedResult.getValue(1, Object.class)).thenReturn("VALUE");
assertThat(new MaskMergedResult(mockMaskRule(), mockSelectStatementContext(), mergedResult).getValue(1, String.class), is("MASK_VALUE"));
}

@SuppressWarnings("unchecked")
private MaskRule mockMaskRule() {
MaskAlgorithm<String, String> maskAlgorithm = mock(MaskAlgorithm.class);
when(maskAlgorithm.mask("VALUE")).thenReturn("MASK_VALUE");
when(metaData.findMaskAlgorithm(1)).thenReturn(Optional.of(maskAlgorithm));
assertThat(new MaskMergedResult(metaData, mergedResult).getValue(1, String.class), is("MASK_VALUE"));
MaskRule result = mock(MaskRule.class);
when(result.findAlgorithm("tbl", "col")).thenReturn(Optional.of(maskAlgorithm));
return result;
}

private SelectStatementContext mockSelectStatementContext() {
ColumnProjection columnProjection = mock(ColumnProjection.class, RETURNS_DEEP_STUBS);
when(columnProjection.getOriginalTable().getValue()).thenReturn("tbl");
when(columnProjection.getName().getValue()).thenReturn("col");
SelectStatementContext result = mock(SelectStatementContext.class, RETURNS_DEEP_STUBS);
when(result.getProjectionsContext().findColumnProjection(1)).thenReturn(Optional.of(columnProjection));
return result;
}

@Test
void assertGetCalendarValue() throws SQLException {
Calendar calendar = Calendar.getInstance();
when(mergedResult.getCalendarValue(1, Date.class, calendar)).thenReturn(new Date(0L));
assertThat(new MaskMergedResult(metaData, mergedResult).getCalendarValue(1, Date.class, calendar), is(new Date(0L)));
assertThat(new MaskMergedResult(mock(MaskRule.class), mock(SelectStatementContext.class), mergedResult).getCalendarValue(1, Date.class, calendar), is(new Date(0L)));
}

@Test
void assertGetInputStream() throws SQLException {
InputStream inputStream = mock(InputStream.class);
when(mergedResult.getInputStream(1, "asc")).thenReturn(inputStream);
assertThat(new MaskMergedResult(metaData, mergedResult).getInputStream(1, "asc"), is(inputStream));
assertThat(new MaskMergedResult(mock(MaskRule.class), mock(SelectStatementContext.class), mergedResult).getInputStream(1, "asc"), is(inputStream));
}

@Test
void assertGetCharacterStream() throws SQLException {
Reader reader = mock(Reader.class);
when(mergedResult.getCharacterStream(1)).thenReturn(reader);
assertThat(new MaskMergedResult(metaData, mergedResult).getCharacterStream(1), is(reader));
assertThat(new MaskMergedResult(mock(MaskRule.class), mock(SelectStatementContext.class), mergedResult).getCharacterStream(1), is(reader));
}

@Test
void assertWasNull() throws SQLException {
assertFalse(new MaskMergedResult(metaData, mergedResult).wasNull());
assertFalse(new MaskMergedResult(mock(MaskRule.class), mock(SelectStatementContext.class), mergedResult).wasNull());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -169,4 +169,18 @@ private boolean isContainsLastInsertIdProjection(final Collection<Projection> pr
}
return false;
}

/**
* Find column projection.
*
* @param columnIndex column index
* @return found column projection
*/
public Optional<ColumnProjection> findColumnProjection(final int columnIndex) {
if (expandProjections.size() < columnIndex) {
return Optional.empty();
}
Projection projection = expandProjections.get(columnIndex - 1);
return projection instanceof ColumnProjection ? Optional.of((ColumnProjection) projection) : Optional.empty();
}
}

0 comments on commit 3c146b8

Please sign in to comment.