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.column;
19  
20  import lombok.Getter;
21  import lombok.Setter;
22  import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.ExpressionSegment;
23  import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.ParenthesesSegment;
24  import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.OwnerAvailable;
25  import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.OwnerSegment;
26  import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.bounded.ColumnSegmentBoundedInfo;
27  import org.apache.shardingsphere.sql.parser.sql.common.value.identifier.IdentifierValue;
28  
29  import java.util.LinkedList;
30  import java.util.List;
31  import java.util.Optional;
32  import java.util.stream.Collectors;
33  
34  /**
35   * Column segment.
36   */
37  @Getter
38  @Setter
39  public final class ColumnSegment implements ExpressionSegment, OwnerAvailable {
40      
41      private final int startIndex;
42      
43      private final int stopIndex;
44      
45      private final IdentifierValue identifier;
46      
47      private List<IdentifierValue> nestedObjectAttributes;
48      
49      private OwnerSegment owner;
50      
51      private ColumnSegmentBoundedInfo columnBoundedInfo;
52      
53      private ColumnSegmentBoundedInfo otherUsingColumnBoundedInfo;
54      
55      private boolean isVariable;
56      
57      private List<ParenthesesSegment> parentheses = new LinkedList<>();
58      
59      public ColumnSegment(final int startIndex, final int stopIndex, final IdentifierValue identifier) {
60          this.startIndex = startIndex;
61          this.stopIndex = stopIndex;
62          this.identifier = identifier;
63          columnBoundedInfo = new ColumnSegmentBoundedInfo(identifier);
64      }
65      
66      /**
67       * Get qualified name with quote characters.
68       * i.e. `field1`, `table1`, field1, table1, `table1`.`field1`, `table1`.field1, table1.`field1` or table1.field1
69       *
70       * @return qualified name with quote characters
71       */
72      public String getQualifiedName() {
73          String column = identifier.getValueWithQuoteCharacters();
74          if (null != nestedObjectAttributes && !nestedObjectAttributes.isEmpty()) {
75              column = String.join(".", column, nestedObjectAttributes.stream().map(IdentifierValue::getValueWithQuoteCharacters).collect(Collectors.joining(".")));
76          }
77          return null == owner ? column : String.join(".", owner.getIdentifier().getValueWithQuoteCharacters(), column);
78      }
79      
80      /**
81       * Get expression.
82       * 
83       * @return expression
84       */
85      public String getExpression() {
86          String column = identifier.getValue();
87          if (null != nestedObjectAttributes && !nestedObjectAttributes.isEmpty()) {
88              column = String.join(".", column, nestedObjectAttributes.stream().map(IdentifierValue::getValue).collect(Collectors.joining(".")));
89          }
90          return null == owner ? column : String.join(".", owner.getIdentifier().getValue(), column);
91      }
92      
93      @Override
94      public Optional<OwnerSegment> getOwner() {
95          return Optional.ofNullable(owner);
96      }
97      
98      @Override
99      public String getText() {
100         return getExpression();
101     }
102 }