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.sql.parser.sql.common.segment.dml.pagination.rownum;
19  
20  import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.BinaryOperationExpression;
21  import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.ExpressionSegment;
22  import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.simple.LiteralExpressionSegment;
23  import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.simple.ParameterMarkerExpressionSegment;
24  import java.util.List;
25  
26  /**
27   * Row number value segment for expression.
28   */
29  public final class ExpressionRowNumberValueSegment extends RowNumberValueSegment {
30      
31      private final ExpressionSegment expressionSegment;
32      
33      public ExpressionRowNumberValueSegment(final int startIndex, final int stopIndex, final ExpressionSegment expressionSegment, final boolean boundOpened) {
34          super(startIndex, stopIndex, boundOpened);
35          this.expressionSegment = expressionSegment;
36      }
37      
38      /**
39       * Get value.
40       *
41       * @param params parameters
42       * @return value
43       */
44      public Long getValue(final List<Object> params) {
45          return getValueFromExpression(expressionSegment, params);
46      }
47      
48      private Long getValueFromExpression(final ExpressionSegment expressionSegment, final List<Object> params) {
49          if (expressionSegment instanceof ParameterMarkerExpressionSegment) {
50              return null == params || params.isEmpty() ? 0L : Long.parseLong(params.get(((ParameterMarkerExpressionSegment) expressionSegment).getParameterMarkerIndex()).toString());
51          }
52          if (expressionSegment instanceof BinaryOperationExpression) {
53              return getValueFromBinaryOperationExpression((BinaryOperationExpression) expressionSegment, params);
54          }
55          if (expressionSegment instanceof LiteralExpressionSegment) {
56              return Long.parseLong(expressionSegment.getText());
57          }
58          throw new UnsupportedOperationException(String.format("Unsupported expression: %s in page expression", expressionSegment.getClass().getName()));
59      }
60      
61      private Long getValueFromBinaryOperationExpression(final BinaryOperationExpression binaryOperationExpression, final List<Object> params) {
62          String operator = binaryOperationExpression.getOperator();
63          Long leftValue = getValueFromExpression(binaryOperationExpression.getLeft(), params);
64          Long rightValue = getValueFromExpression(binaryOperationExpression.getRight(), params);
65          switch (operator) {
66              case "+":
67                  return leftValue + rightValue;
68              case "-":
69                  return leftValue - rightValue;
70              case "*":
71                  return leftValue * rightValue;
72              case "/":
73                  return leftValue / rightValue;
74              default:
75                  throw new UnsupportedOperationException(String.format("Unsupported operator: %s in page expression", operator));
76          }
77      }
78  }