/*
 * 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.schema.builder;

import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.apache.shardingsphere.infra.metadata.schema.ShardingSphereSchema;
import org.apache.shardingsphere.infra.rule.ShardingSphereRule;
import org.apache.shardingsphere.infra.rule.type.TableContainedRule;

import java.sql.SQLException;

/**
 * Schema builder.
 */
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class SchemaBuilder {
    
    /**
     * Build ShardingSphere schema.
     * 
     * @param materials schema builder materials
     * @return ShardingSphere schema
     * @throws SQLException SQL exception
     */
    public static ShardingSphereSchema build(final SchemaBuilderMaterials materials) throws SQLException {
        ShardingSphereSchema result = new ShardingSphereSchema();
        for (ShardingSphereRule rule : materials.getRules()) {
            if (rule instanceof TableContainedRule) {
                for (String table : ((TableContainedRule) rule).getTables()) {
                    if (!result.containsTable(table)) {
                        TableMetaDataBuilder.build(table, materials).ifPresent(optional -> result.put(table, optional));
                    }
                }
            }
        }
        return result;
    }
}

@@ -999,16 +999,184 @@
 Schema;%0A
+import org.apache.shardingsphere.infra.metadata.schema.builder.loader.SchemaMetaDataLoader;%0Aimport org.apache.shardingsphere.infra.metadata.schema.model.TableMetaData;%0A
 import o
@@ -1231,16 +1231,16 @@
 reRule;%0A
-
 import o
@@ -1316,25 +1316,143 @@
 java
-.sql.SQLException
+x.sql.DataSource;%0Aimport java.sql.SQLException;%0Aimport java.util.Collection;%0Aimport java.util.LinkedHashSet;%0Aimport java.util.Map.Entry
 ;%0A%0A/
@@ -2346,32 +2346,32 @@
 %7D%0A            %7D%0A
-
         %7D%0A      
@@ -2356,32 +2356,1167 @@
     %7D%0A        %7D%0A
+        appendRemainTables(materials, result);%0A        return result;%0A    %7D%0A    %0A    private static void appendRemainTables(final SchemaBuilderMaterials materials, final ShardingSphereSchema schema) throws SQLException %7B%0A        Collection%3CString%3E tableNames = new LinkedHashSet%3C%3E();%0A        for (Entry%3CString, DataSource%3E entry: materials.getDataSourceMap().entrySet()) %7B%0A            tableNames.addAll(SchemaMetaDataLoader.loadAllTableNames(entry.getValue(), materials.getDatabaseType()));%0A        %7D%0A        tableNames.removeAll(getExistedTables(materials.getRules(), schema));%0A        for (String each : tableNames) %7B%0A            schema.put(each, new TableMetaData());%0A        %7D%0A    %7D%0A    %0A    private static Collection%3CString%3E getExistedTables(final Collection%3CShardingSphereRule%3E rules, final ShardingSphereSchema schema) %7B%0A        Collection%3CString%3E result = new LinkedHashSet%3C%3E();%0A        for (ShardingSphereRule each : rules) %7B%0A            if (each instanceof TableContainedRule) %7B%0A                result.addAll(((TableContainedRule) each).getTables());%0A            %7D%0A        %7D%0A        result.addAll(schema.getAllTableNames());%0A
         return r

Add default tables into ShardingSphere metadata (#8341)