You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by du...@apache.org on 2022/11/14 09:20:45 UTC

[shardingsphere] branch master updated: Add pg_class and pg_namespace data collector. (#22166)

This is an automated email from the ASF dual-hosted git repository.

duanzhengqiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git


The following commit(s) were added to refs/heads/master by this push:
     new 4314c687415 Add pg_class and pg_namespace data collector. (#22166)
4314c687415 is described below

commit 4314c6874156a15094488e646ed74cbf3f2b1820
Author: Chuxin Chen <ch...@qq.com>
AuthorDate: Mon Nov 14 17:20:36 2022 +0800

    Add pg_class and pg_namespace data collector. (#22166)
    
    * Add pg_class and pg_namespace data collector.
    
    * Fix resultSet not closed.
---
 .../collector/tables/PgClassTableCollector.java    | 88 ++++++++++++++++++++++
 .../tables/PgNamespaceTableCollector.java          | 82 ++++++++++++++++++++
 .../YamlShardingSphereTableDataSwapper.java        | 14 +++-
 ...data.data.collector.ShardingSphereDataCollector | 19 +++++
 4 files changed, 201 insertions(+), 2 deletions(-)

diff --git a/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/data/collector/tables/PgClassTableCollector.java b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/data/collector/tables/PgClassTableCollector.java
new file mode 100644
index 00000000000..a7135444d6e
--- /dev/null
+++ b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/data/collector/tables/PgClassTableCollector.java
@@ -0,0 +1,88 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shardingsphere.infra.metadata.data.collector.tables;
+
+import org.apache.shardingsphere.infra.metadata.data.ShardingSphereRowData;
+import org.apache.shardingsphere.infra.metadata.data.ShardingSphereTableData;
+import org.apache.shardingsphere.infra.metadata.data.collector.ShardingSphereDataCollector;
+import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
+import org.apache.shardingsphere.infra.metadata.database.schema.decorator.model.ShardingSphereTable;
+
+import javax.sql.DataSource;
+import java.math.BigInteger;
+import java.sql.Connection;
+import java.sql.ResultSet;
+import java.sql.ResultSetMetaData;
+import java.sql.SQLException;
+import java.sql.Statement;
+import java.sql.Types;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Optional;
+
+/**
+ * Table pg_class data collector.
+ */
+public final class PgClassTableCollector implements ShardingSphereDataCollector {
+    
+    private static final String PG_CLASS = "pg_class";
+    
+    private static final String SELECT_SQL = "SELECT oid, relname, relnamespace, reltype, reloftype, relowner, relam, relfilenode, reltablespace, relpages, reltuples, relallvisible, reltoastrelid,"
+            + "relhasindex, relisshared, relpersistence, relkind, relnatts, relchecks, relhasrules, relhastriggers, relhassubclass, relrowsecurity, relforcerowsecurity, relispopulated, relreplident,"
+            + "relispartition, relrewrite, relfrozenxid, relminmxid, relacl, reloptions, relpartbound FROM pg_catalog.pg_class "
+            + "WHERE relkind IN ('r','v','m','S','L','f','e','o','') AND relname NOT LIKE 'matviewmap\\_%' AND relname NOT LIKE 'mlog\\_%' "
+            + "AND pg_catalog.pg_table_is_visible(oid);";
+    
+    @Override
+    public Optional<ShardingSphereTableData> collect(final ShardingSphereDatabase shardingSphereDatabase, final ShardingSphereTable table) throws SQLException {
+        ShardingSphereTableData result = new ShardingSphereTableData(PG_CLASS, new ArrayList<>(table.getColumns().values()));
+        for (DataSource each : shardingSphereDatabase.getResourceMetaData().getDataSources().values()) {
+            try (
+                    Connection connection = each.getConnection();
+                    Statement statement = connection.createStatement();
+                    ResultSet resultSet = statement.executeQuery(SELECT_SQL)) {
+                ResultSetMetaData metaData = resultSet.getMetaData();
+                while (resultSet.next()) {
+                    List<Object> rows = new ArrayList<>(metaData.getColumnCount());
+                    for (int i = 1; i <= metaData.getColumnCount(); i++) {
+                        rows.add(convertIfNecessary(resultSet.getObject(i), metaData.getColumnType(i)));
+                    }
+                    ShardingSphereRowData rowData = new ShardingSphereRowData(rows);
+                    result.getRows().add(rowData);
+                }
+            }
+        }
+        return Optional.of(result);
+    }
+    
+    // TODO extract to util
+    private Object convertIfNecessary(final Object data, final int dataType) {
+        if (Types.ARRAY == dataType || Types.OTHER == dataType) {
+            return null == data ? null : data.toString();
+        }
+        if (Types.BIGINT == dataType) {
+            return null == data ? null : new BigInteger(data.toString());
+        }
+        return data;
+    }
+    
+    @Override
+    public String getType() {
+        return PG_CLASS;
+    }
+}
diff --git a/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/data/collector/tables/PgNamespaceTableCollector.java b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/data/collector/tables/PgNamespaceTableCollector.java
new file mode 100644
index 00000000000..a0863b4f7dd
--- /dev/null
+++ b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/data/collector/tables/PgNamespaceTableCollector.java
@@ -0,0 +1,82 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shardingsphere.infra.metadata.data.collector.tables;
+
+import org.apache.shardingsphere.infra.metadata.data.ShardingSphereRowData;
+import org.apache.shardingsphere.infra.metadata.data.ShardingSphereTableData;
+import org.apache.shardingsphere.infra.metadata.data.collector.ShardingSphereDataCollector;
+import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
+import org.apache.shardingsphere.infra.metadata.database.schema.decorator.model.ShardingSphereTable;
+
+import javax.sql.DataSource;
+import java.math.BigInteger;
+import java.sql.Connection;
+import java.sql.ResultSet;
+import java.sql.ResultSetMetaData;
+import java.sql.SQLException;
+import java.sql.Statement;
+import java.sql.Types;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Optional;
+
+/**
+ * Table pg_namespace data collector.
+ */
+public final class PgNamespaceTableCollector implements ShardingSphereDataCollector {
+    
+    private static final String PG_NAMESPACE = "pg_namespace";
+    
+    @Override
+    public Optional<ShardingSphereTableData> collect(final ShardingSphereDatabase shardingSphereDatabase, final ShardingSphereTable table) throws SQLException {
+        ShardingSphereTableData result = new ShardingSphereTableData(PG_NAMESPACE, new ArrayList<>(table.getColumns().values()));
+        for (DataSource each : shardingSphereDatabase.getResourceMetaData().getDataSources().values()) {
+            try (
+                    Connection connection = each.getConnection();
+                    Statement statement = connection.createStatement();
+                    ResultSet resultSet = statement.executeQuery("SELECT oid, nspname, nspowner, nspacl FROM pg_catalog.pg_namespace")) {
+                ResultSetMetaData metaData = resultSet.getMetaData();
+                while (resultSet.next()) {
+                    List<Object> rows = new ArrayList<>(metaData.getColumnCount());
+                    for (int i = 1; i <= metaData.getColumnCount(); i++) {
+                        rows.add(convertIfNecessary(resultSet.getObject(i), metaData.getColumnType(i)));
+                    }
+                    ShardingSphereRowData rowData = new ShardingSphereRowData(rows);
+                    result.getRows().add(rowData);
+                }
+            }
+        }
+        return Optional.of(result);
+    }
+    
+    // TODO extract to util
+    private Object convertIfNecessary(final Object data, final int dataType) {
+        if (Types.ARRAY == dataType || Types.OTHER == dataType) {
+            return null == data ? null : data.toString();
+        }
+        if (Types.BIGINT == dataType) {
+            return null == data ? null : new BigInteger(data.toString());
+        }
+        return data;
+    }
+    
+    @Override
+    public String getType() {
+        return PG_NAMESPACE;
+    }
+}
diff --git a/infra/common/src/main/java/org/apache/shardingsphere/infra/yaml/data/swapper/YamlShardingSphereTableDataSwapper.java b/infra/common/src/main/java/org/apache/shardingsphere/infra/yaml/data/swapper/YamlShardingSphereTableDataSwapper.java
index 5c277d7f7c3..e104a7b3784 100644
--- a/infra/common/src/main/java/org/apache/shardingsphere/infra/yaml/data/swapper/YamlShardingSphereTableDataSwapper.java
+++ b/infra/common/src/main/java/org/apache/shardingsphere/infra/yaml/data/swapper/YamlShardingSphereTableDataSwapper.java
@@ -26,6 +26,7 @@ import org.apache.shardingsphere.infra.yaml.data.pojo.YamlShardingSphereTableDat
 import org.apache.shardingsphere.infra.yaml.schema.pojo.YamlShardingSphereColumn;
 
 import java.math.BigDecimal;
+import java.math.BigInteger;
 import java.sql.Types;
 import java.util.Collections;
 import java.util.LinkedList;
@@ -62,8 +63,8 @@ public final class YamlShardingSphereTableDataSwapper implements YamlConfigurati
     }
     
     private Object convertDataType(final Object data, final int dataType) {
-        if (Types.DECIMAL == dataType) {
-            return data.toString();
+        if (Types.DECIMAL == dataType || Types.BIGINT == dataType) {
+            return null == data ? null : data.toString();
         }
         // TODO use general type convertor
         return data;
@@ -105,9 +106,18 @@ public final class YamlShardingSphereTableDataSwapper implements YamlConfigurati
     }
     
     private Object convertByDataType(final Object data, final int dataType) {
+        if (null == data) {
+            return null;
+        }
         if (Types.DECIMAL == dataType) {
             return new BigDecimal(data.toString());
         }
+        if (Types.BIGINT == dataType) {
+            return new BigInteger(data.toString());
+        }
+        if (Types.REAL == dataType || Types.FLOAT == dataType) {
+            return Float.parseFloat(data.toString());
+        }
         // TODO use general type convertor
         return data;
     }
diff --git a/infra/common/src/main/resources/META-INF/services/org.apache.shardingsphere.infra.metadata.data.collector.ShardingSphereDataCollector b/infra/common/src/main/resources/META-INF/services/org.apache.shardingsphere.infra.metadata.data.collector.ShardingSphereDataCollector
new file mode 100644
index 00000000000..8aba2f9c26e
--- /dev/null
+++ b/infra/common/src/main/resources/META-INF/services/org.apache.shardingsphere.infra.metadata.data.collector.ShardingSphereDataCollector
@@ -0,0 +1,19 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License.  You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+org.apache.shardingsphere.infra.metadata.data.collector.tables.PgNamespaceTableCollector
+org.apache.shardingsphere.infra.metadata.data.collector.tables.PgClassTableCollector