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.expr.simple;
19  
20  import lombok.EqualsAndHashCode;
21  import lombok.Getter;
22  import lombok.RequiredArgsConstructor;
23  import lombok.Setter;
24  import org.apache.shardingsphere.sql.parser.sql.common.enums.ParameterMarkerType;
25  import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.item.ProjectionSegment;
26  import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.AliasAvailable;
27  import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.AliasSegment;
28  import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.ParameterMarkerSegment;
29  import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.bounded.ColumnSegmentBoundedInfo;
30  import org.apache.shardingsphere.sql.parser.sql.common.value.identifier.IdentifierValue;
31  
32  import java.util.Optional;
33  
34  /**
35   * Parameter marker expression segment.
36   */
37  @RequiredArgsConstructor
38  @Getter
39  @EqualsAndHashCode(exclude = "boundedInfo")
40  public class ParameterMarkerExpressionSegment implements SimpleExpressionSegment, ProjectionSegment, AliasAvailable, ParameterMarkerSegment {
41      
42      private final int startIndex;
43      
44      private final int stopIndex;
45      
46      private final int parameterMarkerIndex;
47      
48      private final ParameterMarkerType parameterMarkerType;
49      
50      @Setter
51      private AliasSegment alias;
52      
53      @Setter
54      private ColumnSegmentBoundedInfo boundedInfo;
55      
56      public ParameterMarkerExpressionSegment(final int startIndex, final int stopIndex, final int parameterMarkerIndex) {
57          this.startIndex = startIndex;
58          this.stopIndex = stopIndex;
59          this.parameterMarkerIndex = parameterMarkerIndex;
60          parameterMarkerType = ParameterMarkerType.QUESTION;
61      }
62      
63      @Override
64      public String getColumnLabel() {
65          return getAliasName().orElse(String.valueOf(parameterMarkerIndex));
66      }
67      
68      @Override
69      public Optional<String> getAliasName() {
70          return null == alias ? Optional.empty() : Optional.ofNullable(alias.getIdentifier().getValue());
71      }
72      
73      @Override
74      public Optional<IdentifierValue> getAlias() {
75          return Optional.ofNullable(alias).map(AliasSegment::getIdentifier);
76      }
77      
78      @Override
79      public int getParameterIndex() {
80          return parameterMarkerIndex;
81      }
82      
83      @Override
84      public ColumnSegmentBoundedInfo getBoundedInfo() {
85          return Optional.ofNullable(boundedInfo).orElseGet(() -> new ColumnSegmentBoundedInfo(new IdentifierValue("")));
86      }
87      
88      @Override
89      public int getStopIndex() {
90          return null == alias ? stopIndex : alias.getStopIndex();
91      }
92      
93      @Override
94      public String getText() {
95          return parameterMarkerType.getMarker();
96      }
97      
98      /**
99       * Get alias segment.
100      * 
101      * @return alias segment
102      */
103     public Optional<AliasSegment> getAliasSegment() {
104         return Optional.ofNullable(alias);
105     }
106 }