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.infra.binder.context.segment.select.projection.util;
19  
20  import lombok.AccessLevel;
21  import lombok.NoArgsConstructor;
22  import org.apache.shardingsphere.infra.database.core.metadata.database.enums.QuoteCharacter;
23  import org.apache.shardingsphere.infra.database.core.type.DatabaseType;
24  import org.apache.shardingsphere.infra.database.opengauss.type.OpenGaussDatabaseType;
25  import org.apache.shardingsphere.infra.database.oracle.type.OracleDatabaseType;
26  import org.apache.shardingsphere.infra.database.postgresql.type.PostgreSQLDatabaseType;
27  import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.item.SubqueryProjectionSegment;
28  import org.apache.shardingsphere.sql.parser.sql.common.value.identifier.IdentifierValue;
29  
30  /**
31   * Projection utility class.
32   */
33  @NoArgsConstructor(access = AccessLevel.PRIVATE)
34  public final class ProjectionUtils {
35      
36      /**
37       * Get column label from alias.
38       * 
39       * @param alias alias
40       * @param databaseType database type
41       * @return column label
42       */
43      public static String getColumnLabelFromAlias(final IdentifierValue alias, final DatabaseType databaseType) {
44          return getIdentifierValueByDatabaseType(alias, databaseType);
45      }
46      
47      private static String getIdentifierValueByDatabaseType(final IdentifierValue identifierValue, final DatabaseType databaseType) {
48          if (QuoteCharacter.NONE != identifierValue.getQuoteCharacter()) {
49              return identifierValue.getValue();
50          }
51          if (databaseType instanceof PostgreSQLDatabaseType || databaseType instanceof OpenGaussDatabaseType) {
52              return identifierValue.getValue().toLowerCase();
53          }
54          if (databaseType instanceof OracleDatabaseType) {
55              return identifierValue.getValue().toUpperCase();
56          }
57          return identifierValue.getValue();
58      }
59      
60      /**
61       * Get column name from column.
62       *
63       * @param columnName column name
64       * @param databaseType database type
65       * @return column name
66       */
67      public static String getColumnNameFromColumn(final IdentifierValue columnName, final DatabaseType databaseType) {
68          return getIdentifierValueByDatabaseType(columnName, databaseType);
69      }
70      
71      /**
72       * Get column name from function.
73       * 
74       * @param functionName function name
75       * @param functionExpression function expression
76       * @param databaseType database type
77       * @return column name
78       */
79      public static String getColumnNameFromFunction(final String functionName, final String functionExpression, final DatabaseType databaseType) {
80          if (databaseType instanceof PostgreSQLDatabaseType || databaseType instanceof OpenGaussDatabaseType) {
81              return functionName.toLowerCase();
82          }
83          if (databaseType instanceof OracleDatabaseType) {
84              return functionExpression.replace(" ", "").toUpperCase();
85          }
86          return functionExpression;
87      }
88      
89      /**
90       * Get column name from expression.
91       * 
92       * @param expression expression
93       * @param databaseType database type
94       * @return column name
95       */
96      public static String getColumnNameFromExpression(final String expression, final DatabaseType databaseType) {
97          if (databaseType instanceof PostgreSQLDatabaseType || databaseType instanceof OpenGaussDatabaseType) {
98              return "?column?";
99          }
100         if (databaseType instanceof OracleDatabaseType) {
101             return expression.replace(" ", "").toUpperCase();
102         }
103         return expression;
104     }
105     
106     /**
107      * Get column name from subquery segment.
108      * 
109      * @param subquerySegment subquery segment
110      * @param databaseType database type
111      * @return column name
112      */
113     public static String getColumnNameFromSubquery(final SubqueryProjectionSegment subquerySegment, final DatabaseType databaseType) {
114         // TODO support postgresql subquery projection
115         if (databaseType instanceof OracleDatabaseType) {
116             return subquerySegment.getText().replace(" ", "").toUpperCase();
117         }
118         return subquerySegment.getText();
119     }
120 }