You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by GitBox <gi...@apache.org> on 2021/11/08 15:53:58 UTC

[GitHub] [shardingsphere] cheese8 commented on a change in pull request #13486: fixbug: Support not nested subquery (from Projections, TableSegment and Expression) on SelectStatement

cheese8 commented on a change in pull request #13486:
URL: https://github.com/apache/shardingsphere/pull/13486#discussion_r744859685



##########
File path: shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/impl/EncryptProjectionTokenGenerator.java
##########
@@ -67,63 +71,96 @@ protected boolean isGenerateSQLTokenForEncrypt(final SQLStatementContext sqlStat
     @Override
     public Collection<SubstitutableColumnNameToken> generateSQLTokens(final SQLStatementContext sqlStatementContext) {
         Collection<SubstitutableColumnNameToken> result = new LinkedHashSet<>();
+        Map<String, Map<String, Map<String, String>>> rewriteMetaDataMap = new HashMap<>();
         if (sqlStatementContext instanceof InsertStatementContext) {
-            result.addAll(generateSQLTokens(((InsertStatementContext) sqlStatementContext).getInsertSelectContext().getSelectStatementContext(), true));
+            result.addAll(generateSQLTokens(((InsertStatementContext) sqlStatementContext).getInsertSelectContext().getSelectStatementContext(), Optional.empty(), SubqueryEnum.InsertSelectSubquery,
+                    rewriteMetaDataMap));
         }
         if (sqlStatementContext instanceof SelectStatementContext) {
-            result.addAll(generateSQLTokens((SelectStatementContext) sqlStatementContext, false));
+            SelectStatementContext selectStatementContext = (SelectStatementContext) sqlStatementContext;
+            if (selectStatementContext.isContainsSubquery()) {
+                SubqueryExtractUtil.getSubqueryTableSegmentsFromTableSegment(selectStatementContext.getSqlStatement().getFrom()).forEach(each -> result.addAll(generateSQLTokens(
+                        new SelectStatementContext(selectStatementContext.getMetaDataMap(), selectStatementContext.getParameters(), each.getSubquery().getSelect(),
+                                selectStatementContext.getSchemaName()), each.getAlias(), SubqueryEnum.NestedProjectionTabsegmentSubquery, rewriteMetaDataMap)));
+                SubqueryExtractUtil.getSubquerySegmentsFromProjections(selectStatementContext.getSqlStatement().getProjections()).forEach(each -> result.addAll(generateSQLTokens(
+                        new SelectStatementContext(selectStatementContext.getMetaDataMap(), selectStatementContext.getParameters(), each.getSelect(),
+                                selectStatementContext.getSchemaName()), Optional.empty(), SubqueryEnum.ProjectionSubqery, rewriteMetaDataMap)));
+                selectStatementContext.getWhere().ifPresent(where -> SubqueryExtractUtil.getSubquerySegmentsFromExpression(where.getExpr()).forEach(each -> result.addAll(generateSQLTokens(
+                        new SelectStatementContext(selectStatementContext.getMetaDataMap(), selectStatementContext.getParameters(), each.getSelect(),
+                                selectStatementContext.getSchemaName()), Optional.empty(), SubqueryEnum.ExpressionSubqery, rewriteMetaDataMap))));
+            }
+            result.addAll(generateSQLTokens(selectStatementContext, Optional.empty(), SubqueryEnum.None, rewriteMetaDataMap));
         }
         return result;
     }
-    
-    private Collection<SubstitutableColumnNameToken> generateSQLTokens(final SelectStatementContext selectStatementContext, final boolean insertSelect) {
+
+    private Collection<SubstitutableColumnNameToken> generateSQLTokens(final SelectStatementContext selectStatementContext, final Optional<String> alias,
+                                                                       final SubqueryEnum subqueryEnum, final Map<String, Map<String, Map<String, String>>> rewriteMetaDataMap) {
         Collection<SubstitutableColumnNameToken> result = new LinkedHashSet<>();
         ProjectionsSegment projectionsSegment = selectStatementContext.getSqlStatement().getProjections();
         for (String each : selectStatementContext.getTablesContext().getTableNames()) {
-            getEncryptRule().findEncryptTable(each).map(optional -> generateSQLTokens(projectionsSegment, each, selectStatementContext, optional, insertSelect)).ifPresent(result::addAll);
+            getEncryptRule().findEncryptTable(each).map(optional -> generateSQLTokens(projectionsSegment, each, selectStatementContext, optional, alias, subqueryEnum,
+                    rewriteMetaDataMap)).ifPresent(result::addAll);
         }
+        selectStatementContext.setRewriteMetaDataMap(rewriteMetaDataMap);

Review comment:
       set rewriteMetaDataMap and get it to rewrite related column in Projections and Expressions.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@shardingsphere.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org