Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.apache.shardingsphere.database.connector.core.metadata.database.metadata.DialectDatabaseMetaData;
import org.apache.shardingsphere.database.connector.core.metadata.database.metadata.option.schema.DialectSchemaOption;
import org.apache.shardingsphere.database.connector.core.metadata.database.system.SystemDatabase;
import org.apache.shardingsphere.database.connector.core.metadata.detector.SystemTableEngine;
import org.apache.shardingsphere.database.connector.core.type.DatabaseType;
import org.apache.shardingsphere.database.connector.core.type.DatabaseTypeRegistry;

Expand Down Expand Up @@ -115,15 +116,11 @@ private Collection<String> loadTableNames(final Connection connection, final Str
try (ResultSet resultSet = connection.getMetaData().getTables(connection.getCatalog(), schemaName, null, tableTypes)) {
while (resultSet.next()) {
String table = resultSet.getString(TABLE_NAME);
if (!isSystemTable(table) && !excludedTables.contains(table)) {
if (!SystemTableEngine.isSystemTable(databaseType, table) && !excludedTables.contains(table)) {
result.add(table);
}
}
}
return result;
}

private boolean isSystemTable(final String table) {
return table.contains("$") || table.contains("/") || table.contains("##");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* 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.database.connector.core.metadata.detector;

import org.apache.shardingsphere.database.connector.core.spi.DatabaseTypedSPI;

/**
* Rule to judge whether table is system table for specific database type.
*/
public interface DialectSystemTableRule extends DatabaseTypedSPI {

/**
* Judge whether the table is a system table.
*
* @param tableName table name
* @return {@code true} if the table is a system table; otherwise {@code false}
*/
boolean isSystemTable(String tableName);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/*
* 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.database.connector.core.metadata.detector;

import com.cedarsoftware.util.CaseInsensitiveMap;
import com.cedarsoftware.util.CaseInsensitiveSet;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.apache.shardingsphere.infra.util.directory.ClasspathResourceDirectoryReader;

import java.util.Collection;
import java.util.Collections;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
* System table detector.
*/
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class SystemTableDetector {

private static final Map<String, Map<String, Collection<String>>> DATABASE_TYPE_SCHEMA_TABLE_MAP;

private static final String COMMON = "common";

static {
try (Stream<String> resourceNameStream = ClasspathResourceDirectoryReader.read("schema")) {
DATABASE_TYPE_SCHEMA_TABLE_MAP = resourceNameStream.filter(each -> each.endsWith(".yaml"))
.map(resourceName -> resourceName.split("/"))
.filter(path -> 4 == path.length)
.collect(Collectors.groupingBy(path -> path[1], CaseInsensitiveMap::new,
Collectors.groupingBy(path -> path[2], CaseInsensitiveMap::new,
Collectors.mapping(path -> StringUtils.removeEnd(path[3], ".yaml"),
Collectors.toCollection(CaseInsensitiveSet::new)))));
}
}

/**
* Judge whether the given database type contains system tables.
*
* @param databaseType database type
* @return {@code true} if the given database type contains system tables, otherwise {@code false}
*/
public static boolean hasSystemTables(final String databaseType) {
return !DATABASE_TYPE_SCHEMA_TABLE_MAP.getOrDefault(databaseType, Collections.emptyMap()).isEmpty();
}

/**
* Judge whether current table is system table or not.
*
* @param databaseType database type
* @param schema schema
* @param tableName table name
* @return whether current table is system table or not
*/
public static boolean isSystemTable(final String databaseType, final String schema, final String tableName) {
Map<String, Collection<String>> schemaTableMap = DATABASE_TYPE_SCHEMA_TABLE_MAP.getOrDefault(databaseType, Collections.emptyMap());
Map<String, Collection<String>> commonTableMap = DATABASE_TYPE_SCHEMA_TABLE_MAP.getOrDefault(COMMON, Collections.emptyMap());
if (null == schema) {
return schemaTableMap.values().stream().anyMatch(each -> each.contains(tableName)) || commonTableMap.values().stream().anyMatch(each -> each.contains(tableName));
}
return schemaTableMap.getOrDefault(schema, Collections.emptyList()).contains(tableName) || commonTableMap.getOrDefault(schema, Collections.emptyList()).contains(tableName);
}

/**
* Judge whether current table is system table or not.
*
* @param databaseType database type
* @param schema schema
* @param tableNames table names
* @return whether current table is system table or not
*/
public static boolean isSystemTable(final String databaseType, final String schema, final Collection<String> tableNames) {
Collection<String> databaseTypeTables = Optional.ofNullable(DATABASE_TYPE_SCHEMA_TABLE_MAP.get(databaseType)).map(schemas -> schemas.get(schema)).orElse(Collections.emptyList());
Collection<String> commonTables = Optional.ofNullable(DATABASE_TYPE_SCHEMA_TABLE_MAP.get(COMMON)).map(schemas -> schemas.get(schema)).orElse(Collections.emptyList());
for (String each : tableNames) {
if (!databaseTypeTables.contains(each) && !commonTables.contains(each)) {
return false;
}
}
return true;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* 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.database.connector.core.metadata.detector;

import org.apache.shardingsphere.database.connector.core.spi.DatabaseTypedSPILoader;
import org.apache.shardingsphere.database.connector.core.type.DatabaseType;

/**
* System table engine.
*/
public final class SystemTableEngine {

/**
* Judge whether the table is system table or not.
*
* @param databaseType database type
* @param tableName table name
* @return whether the table is system table or not
*/
public static boolean isSystemTable(final DatabaseType databaseType, final String tableName) {
return DatabaseTypedSPILoader.findService(DialectSystemTableRule.class, databaseType)
.map(rule -> rule.isSystemTable(tableName))
.orElseGet(() -> SystemTableDetector.hasSystemTables(databaseType.getType())
? SystemTableDetector.isSystemTable(databaseType.getType(), null, tableName)
: tableName.contains("$") || tableName.contains("/") || tableName.contains("##"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* 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.database.connector.core.metadata.detector;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

class SystemTableDetectorTest {

@Test
void assertIsSystemTable() {
assertTrue(SystemTableDetector.isSystemTable("PostgreSQL", "pg_catalog", "pg_aggregate"));
assertTrue(SystemTableDetector.isSystemTable("PostgreSQL", "information_schema", "domain_udt_usage"));
assertFalse(SystemTableDetector.isSystemTable("PostgreSQL", "public", "t_order"));
assertTrue(SystemTableDetector.isSystemTable("MySQL", "information_schema", "applicable_roles"));
assertTrue(SystemTableDetector.isSystemTable("MySQL", "performance_schema", "accounts"));
assertTrue(SystemTableDetector.isSystemTable("MySQL", "mysql", "columns_priv"));
assertTrue(SystemTableDetector.isSystemTable("MySQL", "sys", "host_summary_by_stages"));
assertFalse(SystemTableDetector.isSystemTable("MySQL", "app", "t_order"));
assertTrue(SystemTableDetector.isSystemTable("openGauss", "pg_catalog", "get_global_prepared_xacts"));
assertTrue(SystemTableDetector.isSystemTable("openGauss", "information_schema", "_pg_foreign_data_wrappers"));
assertFalse(SystemTableDetector.isSystemTable("openGauss", "public", "t_order"));
assertTrue(SystemTableDetector.isSystemTable("PostgreSQL", "shardingsphere", "cluster_information"));
assertTrue(SystemTableDetector.isSystemTable("Firebird", "system_tables", "mon$attachments"));
assertFalse(SystemTableDetector.isSystemTable("Firebird", "system_tables", "nonexistent"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* 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.database.connector.core.metadata.detector;

import org.junit.jupiter.api.Test;
import org.apache.shardingsphere.database.connector.core.type.DatabaseType;
import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

class SystemTableEngineTest {

private final DatabaseType databaseType = TypedSPILoader.getService(DatabaseType.class, "FIXTURE");

@Test
void assertIsSystemTable() {
assertTrue(SystemTableEngine.isSystemTable(databaseType, "pg$foo"));
assertFalse(SystemTableEngine.isSystemTable(databaseType, "t_order"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#
# 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.
#

name: MON$ATTACHMENTS
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#
# 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.
#

name: APPLICABLE_ROLES
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#
# 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.
#

name: columns_priv
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#
# 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.
#

name: accounts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#
# 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.
#

name: host_summary_by_stages
Loading