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.value.identifier;
19  
20  import com.google.common.base.Strings;
21  import lombok.EqualsAndHashCode;
22  import lombok.Getter;
23  import lombok.RequiredArgsConstructor;
24  import lombok.ToString;
25  import org.apache.shardingsphere.infra.database.core.metadata.database.enums.QuoteCharacter;
26  import org.apache.shardingsphere.sql.parser.sql.common.util.SQLUtils;
27  import org.apache.shardingsphere.sql.parser.sql.common.value.ValueASTNode;
28  
29  /**
30   * Identifier value.
31   */
32  @RequiredArgsConstructor
33  @Getter
34  @EqualsAndHashCode
35  @ToString
36  public final class IdentifierValue implements ValueASTNode<String> {
37      
38      private final String value;
39      
40      private final QuoteCharacter quoteCharacter;
41      
42      public IdentifierValue(final String text) {
43          this(SQLUtils.getExactlyValue(text), QuoteCharacter.getQuoteCharacter(text));
44      }
45      
46      public IdentifierValue(final String text, final String reservedCharacters) {
47          this(SQLUtils.getExactlyValue(text, reservedCharacters), QuoteCharacter.getQuoteCharacter(text));
48      }
49      
50      /**
51       * Get value with quote characters, i.e. `table1` or `field1`
52       *
53       * @return value with quote characters
54       */
55      public String getValueWithQuoteCharacters() {
56          return null == value ? "" : quoteCharacter.wrap(value);
57      }
58      
59      /**
60       * Get quoted content.
61       *
62       * @param text text
63       * @return quote content
64       */
65      public static String getQuotedContent(final String text) {
66          if (Strings.isNullOrEmpty(text)) {
67              return text;
68          }
69          QuoteCharacter quoteCharacter = QuoteCharacter.getQuoteCharacter(text);
70          if (QuoteCharacter.NONE == quoteCharacter) {
71              return text.trim();
72          }
73          return text.substring(1, text.length() - 1);
74      }
75  }