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.sqlfederation.optimizer.converter.statement.merge;
19  
20  import org.apache.calcite.sql.SqlMerge;
21  import org.apache.calcite.sql.SqlNode;
22  import org.apache.calcite.sql.SqlNodeList;
23  import org.apache.calcite.sql.SqlUpdate;
24  import org.apache.calcite.sql.parser.SqlParserPos;
25  import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.assignment.ColumnAssignmentSegment;
26  import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.column.ColumnSegment;
27  import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.ExpressionSegment;
28  import org.apache.shardingsphere.sql.parser.sql.common.statement.dml.MergeStatement;
29  import org.apache.shardingsphere.sql.parser.sql.common.statement.dml.UpdateStatement;
30  import org.apache.shardingsphere.sqlfederation.optimizer.converter.segment.expression.ExpressionConverter;
31  import org.apache.shardingsphere.sqlfederation.optimizer.converter.segment.expression.impl.ColumnConverter;
32  import org.apache.shardingsphere.sqlfederation.optimizer.converter.segment.from.TableConverter;
33  import org.apache.shardingsphere.sqlfederation.optimizer.converter.segment.where.WhereConverter;
34  import org.apache.shardingsphere.sqlfederation.optimizer.converter.statement.SQLStatementConverter;
35  
36  import java.util.List;
37  import java.util.stream.Collectors;
38  
39  /**
40   * Merge statement converter.
41   */
42  public final class MergeStatementConverter implements SQLStatementConverter<MergeStatement, SqlNode> {
43      
44      @Override
45      public SqlNode convert(final MergeStatement mergeStatement) {
46          SqlNode targetTable = TableConverter.convert(mergeStatement.getTarget()).orElseThrow(IllegalStateException::new);
47          SqlNode condition = ExpressionConverter.convert(mergeStatement.getExpression().getExpr()).orElseThrow(IllegalStateException::new);
48          SqlNode sourceTable = TableConverter.convert(mergeStatement.getSource()).orElseThrow(IllegalStateException::new);
49          SqlUpdate sqlUpdate = mergeStatement.getUpdate().map(this::convertUpdate).orElse(null);
50          return new SqlMerge(SqlParserPos.ZERO, targetTable, condition, sourceTable, sqlUpdate, null, null, null);
51      }
52      
53      private SqlUpdate convertUpdate(final UpdateStatement updateStatement) {
54          SqlNode table = TableConverter.convert(updateStatement.getTable()).orElse(SqlNodeList.EMPTY);
55          SqlNode condition = updateStatement.getWhere().flatMap(WhereConverter::convert).orElse(null);
56          SqlNodeList columns = new SqlNodeList(SqlParserPos.ZERO);
57          SqlNodeList expressions = new SqlNodeList(SqlParserPos.ZERO);
58          for (ColumnAssignmentSegment each : updateStatement.getAssignmentSegment().orElseThrow(IllegalStateException::new).getAssignments()) {
59              columns.addAll(convertColumn(each.getColumns()));
60              expressions.add(convertExpression(each.getValue()));
61          }
62          return new SqlUpdate(SqlParserPos.ZERO, table, columns, expressions, condition, null, null);
63      }
64      
65      private List<SqlNode> convertColumn(final List<ColumnSegment> columnSegments) {
66          return columnSegments.stream().map(each -> ColumnConverter.convert(each).orElseThrow(IllegalStateException::new)).collect(Collectors.toList());
67      }
68      
69      private SqlNode convertExpression(final ExpressionSegment expressionSegment) {
70          return ExpressionConverter.convert(expressionSegment).orElseThrow(IllegalStateException::new);
71      }
72  }