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.sqlfederation.optimizer.planner.util;
19  
20  import lombok.AccessLevel;
21  import lombok.NoArgsConstructor;
22  import org.apache.calcite.schema.SchemaPlus;
23  import org.apache.calcite.schema.impl.ScalarFunctionImpl;
24  import org.apache.shardingsphere.infra.autogen.version.ShardingSphereVersion;
25  
26  /**
27   * SQL federation function utility class.
28   */
29  @NoArgsConstructor(access = AccessLevel.PRIVATE)
30  public final class SQLFederationFunctionUtils {
31      
32      private static final int DEFAULT_PASSWORD_DEADLINE = 90;
33      
34      private static final int DEFAULT_PASSWORD_NOTIFY_TIME = 7;
35      
36      /**
37       * Registry user defined function.
38       * 
39       * @param schemaName schema name
40       * @param schemaPlus schema plus
41       */
42      public static void registryUserDefinedFunction(final String schemaName, final SchemaPlus schemaPlus) {
43          schemaPlus.add("version", ScalarFunctionImpl.create(SQLFederationFunctionUtils.class, "version"));
44          schemaPlus.add("opengauss_version", ScalarFunctionImpl.create(SQLFederationFunctionUtils.class, "openGaussVersion"));
45          schemaPlus.add("gs_password_deadline", ScalarFunctionImpl.create(SQLFederationFunctionUtils.class, "gsPasswordDeadline"));
46          schemaPlus.add("intervaltonum", ScalarFunctionImpl.create(SQLFederationFunctionUtils.class, "intervalToNum"));
47          schemaPlus.add("gs_password_notifyTime", ScalarFunctionImpl.create(SQLFederationFunctionUtils.class, "gsPasswordNotifyTime"));
48          schemaPlus.add("pg_catalog.gs_password_deadline", ScalarFunctionImpl.create(SQLFederationFunctionUtils.class, "gsPasswordDeadline"));
49          schemaPlus.add("pg_catalog.intervaltonum", ScalarFunctionImpl.create(SQLFederationFunctionUtils.class, "intervalToNum"));
50          schemaPlus.add("pg_catalog.gs_password_notifyTime", ScalarFunctionImpl.create(SQLFederationFunctionUtils.class, "gsPasswordNotifyTime"));
51          if ("pg_catalog".equalsIgnoreCase(schemaName)) {
52              schemaPlus.add("pg_catalog.pg_table_is_visible", ScalarFunctionImpl.create(SQLFederationFunctionUtils.class, "pgTableIsVisible"));
53              schemaPlus.add("pg_catalog.pg_get_userbyid", ScalarFunctionImpl.create(SQLFederationFunctionUtils.class, "pgGetUserById"));
54          }
55      }
56      
57      /**
58       * Mock pg_table_is_visible function.
59       *
60       * @param oid oid
61       * @return true
62       */
63      @SuppressWarnings("unused")
64      public static boolean pgTableIsVisible(final Long oid) {
65          return true;
66      }
67      
68      /**
69       * Mock pg_get_userbyid function.
70       * 
71       * @param oid oid
72       * @return user name
73       */
74      @SuppressWarnings("unused")
75      public static String pgGetUserById(final Long oid) {
76          return "mock user";
77      }
78      
79      /**
80       * Get version of ShardingSphere-Proxy.
81       *
82       * @return version message
83       */
84      public static String version() {
85          return "ShardingSphere-Proxy " + ShardingSphereVersion.VERSION + ("-" + ShardingSphereVersion.BUILD_GIT_COMMIT_ID_ABBREV) + (ShardingSphereVersion.BUILD_GIT_DIRTY ? "-dirty" : "");
86      }
87      
88      /**
89       * Get version of ShardingSphere-Proxy for openGauss.
90       *
91       * @return version message
92       */
93      @SuppressWarnings("unused")
94      public static String openGaussVersion() {
95          return ShardingSphereVersion.VERSION;
96      }
97      
98      /**
99       * The type interval is not supported in standard JDBC.
100      * Indicates the number of remaining days before the password of the current user expires.
101      *
102      * @return 90 days
103      */
104     @SuppressWarnings("unused")
105     public static int gsPasswordDeadline() {
106         return DEFAULT_PASSWORD_DEADLINE;
107     }
108     
109     /**
110      * The type interval is not supported in standard JDBC.
111      * Convert interval to num.
112      *
113      * @param result result
114      * @return result
115      */
116     @SuppressWarnings("unused")
117     public static int intervalToNum(final int result) {
118         return result;
119     }
120     
121     /**
122      * Specifies the number of days prior to password expiration that a user will receive a reminder.
123      *
124      * @return 7 days
125      */
126     @SuppressWarnings("unused")
127     public static int gsPasswordNotifyTime() {
128         return DEFAULT_PASSWORD_NOTIFY_TIME;
129     }
130 }