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.update;
19  
20  import org.apache.calcite.sql.SqlNode;
21  import org.apache.calcite.sql.SqlNodeList;
22  import org.apache.calcite.sql.SqlOrderBy;
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.segment.dml.pagination.limit.LimitSegment;
29  import org.apache.shardingsphere.sql.parser.sql.common.statement.dml.UpdateStatement;
30  import org.apache.shardingsphere.sql.parser.sql.dialect.handler.dml.UpdateStatementHandler;
31  import org.apache.shardingsphere.sqlfederation.optimizer.converter.segment.expression.ExpressionConverter;
32  import org.apache.shardingsphere.sqlfederation.optimizer.converter.segment.expression.impl.ColumnConverter;
33  import org.apache.shardingsphere.sqlfederation.optimizer.converter.segment.from.TableConverter;
34  import org.apache.shardingsphere.sqlfederation.optimizer.converter.segment.limit.PaginationValueSQLConverter;
35  import org.apache.shardingsphere.sqlfederation.optimizer.converter.segment.orderby.OrderByConverter;
36  import org.apache.shardingsphere.sqlfederation.optimizer.converter.segment.where.WhereConverter;
37  import org.apache.shardingsphere.sqlfederation.optimizer.converter.statement.SQLStatementConverter;
38  
39  import java.util.List;
40  import java.util.Optional;
41  import java.util.stream.Collectors;
42  
43  /**
44   * Update statement converter.
45   */
46  public final class UpdateStatementConverter implements SQLStatementConverter<UpdateStatement, SqlNode> {
47      
48      @Override
49      public SqlNode convert(final UpdateStatement updateStatement) {
50          SqlUpdate sqlUpdate = convertUpdate(updateStatement);
51          SqlNodeList orderBy = UpdateStatementHandler.getOrderBySegment(updateStatement).flatMap(OrderByConverter::convert).orElse(SqlNodeList.EMPTY);
52          Optional<LimitSegment> limit = UpdateStatementHandler.getLimitSegment(updateStatement);
53          if (limit.isPresent()) {
54              SqlNode offset = limit.get().getOffset().flatMap(PaginationValueSQLConverter::convert).orElse(null);
55              SqlNode rowCount = limit.get().getRowCount().flatMap(PaginationValueSQLConverter::convert).orElse(null);
56              return new SqlOrderBy(SqlParserPos.ZERO, sqlUpdate, orderBy, offset, rowCount);
57          }
58          return orderBy.isEmpty() ? sqlUpdate : new SqlOrderBy(SqlParserPos.ZERO, sqlUpdate, orderBy, null, null);
59      }
60      
61      private SqlUpdate convertUpdate(final UpdateStatement updateStatement) {
62          SqlNode table = TableConverter.convert(updateStatement.getTable()).orElseThrow(IllegalStateException::new);
63          SqlNode condition = updateStatement.getWhere().flatMap(WhereConverter::convert).orElse(null);
64          SqlNodeList columns = new SqlNodeList(SqlParserPos.ZERO);
65          SqlNodeList expressions = new SqlNodeList(SqlParserPos.ZERO);
66          for (ColumnAssignmentSegment each : updateStatement.getAssignmentSegment().orElseThrow(IllegalStateException::new).getAssignments()) {
67              columns.addAll(convertColumn(each.getColumns()));
68              expressions.add(convertExpression(each.getValue()));
69          }
70          return new SqlUpdate(SqlParserPos.ZERO, table, columns, expressions, condition, null, null);
71      }
72      
73      private List<SqlNode> convertColumn(final List<ColumnSegment> columnSegments) {
74          return columnSegments.stream().map(each -> ColumnConverter.convert(each).orElseThrow(IllegalStateException::new)).collect(Collectors.toList());
75      }
76      
77      private SqlNode convertExpression(final ExpressionSegment expressionSegment) {
78          return ExpressionConverter.convert(expressionSegment).orElseThrow(IllegalStateException::new);
79      }
80  }