-
Notifications
You must be signed in to change notification settings - Fork 6.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add SameEncryptorComparator (#32268)
* Add SameEncryptorComparator * Add SameEncryptorComparator * Add SameEncryptorComparator
- Loading branch information
Showing
7 changed files
with
187 additions
and
76 deletions.
There are no files selected for viewing
69 changes: 69 additions & 0 deletions
69
...phere/encrypt/rewrite/token/acrosstable/InsertSelectColumnsSameEncryptorUsageChecker.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/* | ||
* 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.rewrite.token.acrosstable; | ||
|
||
import lombok.AccessLevel; | ||
import lombok.NoArgsConstructor; | ||
import org.apache.shardingsphere.encrypt.rule.EncryptRule; | ||
import org.apache.shardingsphere.encrypt.spi.EncryptAlgorithm; | ||
import org.apache.shardingsphere.infra.binder.context.segment.select.projection.Projection; | ||
import org.apache.shardingsphere.infra.binder.context.segment.select.projection.impl.ColumnProjection; | ||
import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.column.ColumnSegment; | ||
import org.apache.shardingsphere.sql.parser.statement.core.segment.generic.bound.ColumnSegmentBoundInfo; | ||
import org.apache.shardingsphere.sql.parser.statement.core.value.identifier.IdentifierValue; | ||
|
||
import java.util.Collection; | ||
import java.util.Iterator; | ||
|
||
/** | ||
* Insert select columns same encryptor usage checker. | ||
*/ | ||
@NoArgsConstructor(access = AccessLevel.PRIVATE) | ||
public final class InsertSelectColumnsSameEncryptorUsageChecker { | ||
|
||
/** | ||
* Judge whether all insert select columns use same encryptor or not. | ||
* | ||
* @param insertColumns insert columns | ||
* @param projections projections | ||
* @param encryptRule encrypt rule | ||
* @return same or different encryptors are using | ||
*/ | ||
public static boolean isSame(final Collection<ColumnSegment> insertColumns, final Collection<Projection> projections, final EncryptRule encryptRule) { | ||
Iterator<ColumnSegment> insertColumnsIterator = insertColumns.iterator(); | ||
Iterator<Projection> projectionIterator = projections.iterator(); | ||
while (insertColumnsIterator.hasNext()) { | ||
ColumnSegment columnSegment = insertColumnsIterator.next(); | ||
EncryptAlgorithm columnEncryptor = encryptRule.findQueryEncryptor( | ||
columnSegment.getColumnBoundInfo().getOriginalTable().getValue(), columnSegment.getColumnBoundInfo().getOriginalColumn().getValue()).orElse(null); | ||
Projection projection = projectionIterator.next(); | ||
ColumnSegmentBoundInfo columnBoundInfo = getColumnSegmentBoundInfo(projection); | ||
EncryptAlgorithm projectionEncryptor = encryptRule.findQueryEncryptor(columnBoundInfo.getOriginalTable().getValue(), columnBoundInfo.getOriginalColumn().getValue()).orElse(null); | ||
if (!SameEncryptorComparator.isSame(columnEncryptor, projectionEncryptor)) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
|
||
private static ColumnSegmentBoundInfo getColumnSegmentBoundInfo(final Projection projection) { | ||
return projection instanceof ColumnProjection | ||
? new ColumnSegmentBoundInfo(null, null, ((ColumnProjection) projection).getOriginalTable(), ((ColumnProjection) projection).getOriginalColumn()) | ||
: new ColumnSegmentBoundInfo(new IdentifierValue(projection.getColumnLabel())); | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
...dingsphere/encrypt/rewrite/token/acrosstable/JoinConditionsSameEncryptorUsageChecker.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/* | ||
* 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.rewrite.token.acrosstable; | ||
|
||
import lombok.AccessLevel; | ||
import lombok.NoArgsConstructor; | ||
import org.apache.shardingsphere.encrypt.rule.EncryptRule; | ||
import org.apache.shardingsphere.encrypt.spi.EncryptAlgorithm; | ||
import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.column.ColumnSegment; | ||
import org.apache.shardingsphere.sql.parser.statement.core.segment.dml.expr.BinaryOperationExpression; | ||
import org.apache.shardingsphere.sql.parser.statement.core.segment.generic.bound.ColumnSegmentBoundInfo; | ||
|
||
import java.util.Collection; | ||
|
||
/** | ||
* Join conditions same encryptor usage checker. | ||
*/ | ||
@NoArgsConstructor(access = AccessLevel.PRIVATE) | ||
public final class JoinConditionsSameEncryptorUsageChecker { | ||
|
||
/** | ||
* Judge whether all join conditions use same encryptor or not. | ||
* | ||
* @param joinConditions join conditions | ||
* @param encryptRule encrypt rule | ||
* @return same or different encryptors are using | ||
*/ | ||
public static boolean isSame(final Collection<BinaryOperationExpression> joinConditions, final EncryptRule encryptRule) { | ||
for (BinaryOperationExpression each : joinConditions) { | ||
if (!(each.getLeft() instanceof ColumnSegment) || !(each.getRight() instanceof ColumnSegment)) { | ||
continue; | ||
} | ||
ColumnSegmentBoundInfo leftColumnInfo = ((ColumnSegment) each.getLeft()).getColumnBoundInfo(); | ||
EncryptAlgorithm leftColumnEncryptor = encryptRule.findQueryEncryptor(leftColumnInfo.getOriginalTable().getValue(), leftColumnInfo.getOriginalColumn().getValue()).orElse(null); | ||
ColumnSegmentBoundInfo rightColumnInfo = ((ColumnSegment) each.getRight()).getColumnBoundInfo(); | ||
EncryptAlgorithm rightColumnEncryptor = encryptRule.findQueryEncryptor(rightColumnInfo.getOriginalTable().getValue(), rightColumnInfo.getOriginalColumn().getValue()).orElse(null); | ||
if (!SameEncryptorComparator.isSame(leftColumnEncryptor, rightColumnEncryptor)) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
.../org/apache/shardingsphere/encrypt/rewrite/token/acrosstable/SameEncryptorComparator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/* | ||
* 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.rewrite.token.acrosstable; | ||
|
||
import lombok.AccessLevel; | ||
import lombok.NoArgsConstructor; | ||
import org.apache.shardingsphere.encrypt.spi.EncryptAlgorithm; | ||
|
||
/** | ||
* Same encryptor comparator. | ||
*/ | ||
@NoArgsConstructor(access = AccessLevel.PRIVATE) | ||
public final class SameEncryptorComparator { | ||
|
||
/** | ||
* Compare whether to same encryptor. | ||
* | ||
* @param encryptor1 encryptor 1 to be compared | ||
* @param encryptor2 encryptor 2 to be compared | ||
* @return same or different encryptors | ||
*/ | ||
public static boolean isSame(final EncryptAlgorithm encryptor1, final EncryptAlgorithm encryptor2) { | ||
if (null == encryptor1 && null == encryptor2) { | ||
return true; | ||
} | ||
if (null != encryptor1 && null != encryptor2) { | ||
return encryptor1.getType().equals(encryptor2.getType()) && encryptor1.equals(encryptor2); | ||
} | ||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters