All Downloads are FREE. Search and download functionalities are using the official Maven repository.

schemacrawler.crawl.SchemaRetriever Maven / Gradle / Ivy

Go to download

SchemaCrawler is an open-source Java API that makes working with database metadata as easy as working with plain old Java objects. SchemaCrawler is also a database schema discovery and comprehension, and schema documentation tool. You can search for database schema objects using regular expressions, and output the schema and data in a readable text format. The output is designed to be diff-ed against other database schemas.

There is a newer version: 16.22.2
Show newest version
/*
========================================================================
SchemaCrawler
http://www.schemacrawler.com
Copyright (c) 2000-2016, Sualeh Fatehi .
All rights reserved.
------------------------------------------------------------------------

SchemaCrawler is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

SchemaCrawler and the accompanying materials are made available under
the terms of the Eclipse Public License v1.0, GNU General Public License
v3 or GNU Lesser General Public License v3.

You may elect to redistribute this code under any of these licenses.

The Eclipse Public License is available at:
http://www.eclipse.org/legal/epl-v10.html

The GNU General Public License v3 and the GNU Lesser General Public
License v3 are available at:
http://www.gnu.org/licenses/

========================================================================
*/

package schemacrawler.crawl;


import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;

import schemacrawler.filter.InclusionRuleFilter;
import schemacrawler.schema.Schema;
import schemacrawler.schema.SchemaReference;
import schemacrawler.schemacrawler.InclusionRule;
import schemacrawler.schemacrawler.InformationSchemaViews;
import schemacrawler.schemacrawler.SchemaCrawlerOptions;
import schemacrawler.utility.Query;
import sf.util.DatabaseUtility;
import sf.util.StringFormat;

final class SchemaRetriever
  extends AbstractRetriever
{

  private static final Logger LOGGER = Logger
    .getLogger(SchemaRetriever.class.getName());

  private final boolean supportsCatalogs;
  private final boolean supportsSchemas;

  SchemaRetriever(final RetrieverConnection retrieverConnection,
                  final MutableCatalog catalog,
                  final SchemaCrawlerOptions options)
    throws SQLException
  {
    super(retrieverConnection, catalog, options);

    supportsCatalogs = retrieverConnection.isSupportsCatalogs();
    supportsSchemas = retrieverConnection.isSupportsSchemas();
  }

  /**
   * Retrieves a list of schemas from the database.
   *
   * @param schemaInclusionRule
   *        Schema inclusion rule
   * @throws SQLException
   *         On an exception
   */
  void retrieveSchemas(final InclusionRule schemaInclusionRule)
    throws SQLException
  {
    final InclusionRuleFilter schemaFilter = new InclusionRuleFilter<>(schemaInclusionRule,
                                                                               true);

    if (schemaFilter.isExcludeAll())
    {
      return;
    }

    final Set schemaRefs;

    // Prefer to retrieve schemas from the INFORMATION_SCHEMA views
    schemaRefs = retrieveAllSchemasFromInformationSchemaViews();
    if (schemaRefs.isEmpty())
    {
      schemaRefs.addAll(retrieveAllSchemas());
    }

    // Filter out schemas
    for (final Iterator iterator = schemaRefs
      .iterator(); iterator.hasNext();)
    {
      final SchemaReference schemaRef = iterator.next();
      if (!schemaFilter.test(schemaRef))
      {
        LOGGER.log(Level.FINER,
                   new StringFormat("Dropping schema, since schema is excluded, %s",
                                    schemaRef.getFullName()));
        iterator.remove();
        // continue
      }
    }

    // Create schemas for the catalogs, as well as create the schema
    // reference cache
    schemaRefs.stream().forEach(schemaRef -> catalog.addSchema(schemaRef));

    // Add an empty schema reference for databases that do not support
    // neither catalogs nor schemas
    if (!supportsCatalogs && !supportsSchemas)
    {
      catalog.addSchema(new SchemaReference(null, null));
    }

  }

  /**
   * Retrieves all catalog names.
   *
   * @return All catalog names in the database
   */
  private Set retrieveAllCatalogs()
  {
    LOGGER.log(Level.INFO, "Retrieving all catalogs");

    final Set catalogNames = new HashSet<>();

    if (supportsCatalogs)
    {
      try
      {
        final List metaDataCatalogNames = DatabaseUtility
          .readResultsVector(getMetaData().getCatalogs());
        for (final String catalogName: metaDataCatalogNames)
        {
          catalogNames.add(quotedName(catalogName));
        }
      }
      catch (final SQLException e)
      {
        LOGGER.log(Level.WARNING, e.getMessage(), e);
      }
      LOGGER.log(Level.FINER,
                 new StringFormat("Retrieved catalogs, %s", catalogNames));
    }

    return catalogNames;
  }

  private Set retrieveAllSchemas()
    throws SQLException
  {
    LOGGER.log(Level.INFO, "Retrieving all schemas");

    final Set schemaRefs = new HashSet<>();
    final Set allCatalogNames = retrieveAllCatalogs();
    if (supportsSchemas)
    {
      try (final MetadataResultSet results = new MetadataResultSet(getMetaData()
        .getSchemas());)
      {
        results.setDescription("retrieveAllSchemas");
        while (results.next())
        {
          final String catalogName;
          if (supportsCatalogs)
          {
            catalogName = quotedName(results.getString("TABLE_CATALOG"));
          }
          else
          {
            catalogName = null;
          }
          final String schemaName = quotedName(results
            .getString("TABLE_SCHEM"));
          LOGGER.log(Level.FINER,
                     new StringFormat("Retrieving schema: %s --> %s",
                                      catalogName,
                                      schemaName));
          if (catalogName == null)
          {
            if (allCatalogNames.isEmpty())
            {
              schemaRefs.add(new SchemaReference(null, schemaName));
            }
            else
            {
              for (final String expectedCatalogName: allCatalogNames)
              {
                schemaRefs
                  .add(new SchemaReference(expectedCatalogName, schemaName));
              }
            }
          }
          else
          {
            schemaRefs.add(new SchemaReference(catalogName, schemaName));
          }
        }
      }
    }
    else
    {
      for (final String catalogName: allCatalogNames)
      {
        LOGGER.log(Level.FINER,
                   new StringFormat("Retrieving schema: %s --> %s",
                                    catalogName,
                                    null));
        schemaRefs.add(new SchemaReference(catalogName, null));
      }
    }
    return schemaRefs;
  }

  private Set retrieveAllSchemasFromInformationSchemaViews()
    throws SQLException
  {
    final Set schemaRefs = new HashSet<>();

    final InformationSchemaViews informationSchemaViews = getRetrieverConnection()
      .getInformationSchemaViews();
    if (!informationSchemaViews.hasSchemataSql())
    {
      LOGGER.log(Level.FINE, "Schemata SQL statement was not provided");
      return schemaRefs;
    }
    final Query schemataSql = informationSchemaViews.getSchemataSql();

    final Connection connection = getDatabaseConnection();

    try (final Statement statement = connection.createStatement();
        final MetadataResultSet results = new MetadataResultSet(schemataSql,
                                                                statement,
                                                                getSchemaInclusionRule());)
    {
      results.setDescription("retrieveAllSchemasFromInformationSchemaViews");
      while (results.next())
      {
        final String catalogName = quotedName(results
          .getString("CATALOG_NAME"));
        final String schemaName = quotedName(results.getString("SCHEMA_NAME"));
        LOGGER.log(Level.FINER,
                   new StringFormat("Retrieving schema: %s --> %s",
                                    catalogName,
                                    schemaName));
        schemaRefs.add(new SchemaReference(catalogName, schemaName));
      }
    }
    catch (final Exception e)
    {
      LOGGER.log(Level.WARNING, "Could not retrieve schemas", e);
    }

    return schemaRefs;
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy