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.executor.utils;
19  
20  import lombok.AccessLevel;
21  import lombok.NoArgsConstructor;
22  import org.apache.shardingsphere.authority.rule.AuthorityRule;
23  import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
24  import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
25  import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereSchema;
26  import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereTable;
27  import org.apache.shardingsphere.infra.metadata.statistics.ShardingSphereRowData;
28  import org.apache.shardingsphere.infra.metadata.statistics.ShardingSphereTableData;
29  import org.apache.shardingsphere.infra.metadata.user.ShardingSphereUser;
30  import org.apache.shardingsphere.sqlfederation.executor.constant.EnumerableConstants;
31  
32  import java.util.Arrays;
33  import java.util.Collection;
34  import java.util.Map;
35  
36  /**
37   * Statistics assemble utils.
38   */
39  @NoArgsConstructor(access = AccessLevel.PRIVATE)
40  public final class StatisticsAssembleUtils {
41      
42      /**
43       * Assemble table data.
44       * 
45       * @param table table
46       * @param metaData meta data
47       * @return ShardingSphere table data
48       */
49      public static ShardingSphereTableData assembleTableData(final ShardingSphereTable table, final ShardingSphereMetaData metaData) {
50          // TODO move this logic to ShardingSphere statistics
51          ShardingSphereTableData result = new ShardingSphereTableData(table.getName());
52          if (EnumerableConstants.PG_DATABASE.equalsIgnoreCase(table.getName())) {
53              assembleOpenGaussDatabaseData(result, metaData.getDatabases().values());
54          } else if (EnumerableConstants.PG_TABLES.equalsIgnoreCase(table.getName())) {
55              for (ShardingSphereDatabase each : metaData.getDatabases().values()) {
56                  assembleOpenGaussTableData(result, each.getSchemas());
57              }
58          } else if (EnumerableConstants.PG_ROLES.equalsIgnoreCase(table.getName())) {
59              assembleOpenGaussRoleData(result, metaData);
60          }
61          return result;
62      }
63      
64      private static void assembleOpenGaussDatabaseData(final ShardingSphereTableData tableData, final Collection<ShardingSphereDatabase> databases) {
65          for (ShardingSphereDatabase each : databases) {
66              Object[] rows = new Object[15];
67              rows[0] = each.getName();
68              rows[11] = EnumerableConstants.DAT_COMPATIBILITY;
69              tableData.getRows().add(new ShardingSphereRowData(Arrays.asList(rows)));
70          }
71      }
72      
73      private static void assembleOpenGaussTableData(final ShardingSphereTableData tableData, final Map<String, ShardingSphereSchema> schemas) {
74          for (Map.Entry<String, ShardingSphereSchema> entry : schemas.entrySet()) {
75              for (String each : entry.getValue().getAllTableNames()) {
76                  Object[] rows = new Object[10];
77                  rows[0] = entry.getKey();
78                  rows[1] = each;
79                  tableData.getRows().add(new ShardingSphereRowData(Arrays.asList(rows)));
80              }
81          }
82      }
83      
84      private static void assembleOpenGaussRoleData(final ShardingSphereTableData tableData, final ShardingSphereMetaData metaData) {
85          for (ShardingSphereUser each : metaData.getGlobalRuleMetaData().getSingleRule(AuthorityRule.class).getConfiguration().getUsers()) {
86              Object[] rows = new Object[27];
87              rows[0] = each.getGrantee().getUsername();
88              tableData.getRows().add(new ShardingSphereRowData(Arrays.asList(rows)));
89          }
90      }
91  }