schemacrawler.crawl.SchemaRetriever Maven / Gradle / Ivy
/*
========================================================================
SchemaCrawler
http://www.schemacrawler.com
Copyright (c) 2000-2020, 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 static schemacrawler.schemacrawler.InformationSchemaKey.SCHEMATA;
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 schemacrawler.SchemaCrawlerLogger;
import schemacrawler.filter.InclusionRuleFilter;
import schemacrawler.inclusionrule.InclusionRule;
import schemacrawler.schema.Schema;
import schemacrawler.schemacrawler.InformationSchemaViews;
import schemacrawler.schemacrawler.Query;
import schemacrawler.schemacrawler.SchemaCrawlerOptions;
import schemacrawler.schemacrawler.SchemaReference;
import us.fatehi.utility.DatabaseUtility;
import us.fatehi.utility.string.StringFormat;
final class SchemaRetriever
extends AbstractRetriever
{
private static final SchemaCrawlerLogger LOGGER =
SchemaCrawlerLogger.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("Excluding schema <%s>", schemaRef));
iterator.remove();
// continue
}
}
// Create schemas for the catalogs, as well as create the schema
// reference cache
for (final SchemaReference schemaRef : schemaRefs)
{
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
{
int numCatalogs = 0;
final List metaDataCatalogNames =
DatabaseUtility.readResultsVector(getMetaData().getCatalogs());
for (final String catalogName : metaDataCatalogNames)
{
numCatalogs = numCatalogs + 1;
catalogNames.add(catalogName);
}
LOGGER.log(Level.INFO,
new StringFormat("Processed %d catalogs", numCatalogs));
}
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)
{
int numSchemas = 0;
try (
final MetadataResultSet results = new MetadataResultSet(getMetaData().getSchemas())
)
{
results.setDescription("retrieveAllSchemas");
while (results.next())
{
numSchemas = numSchemas + 1;
final String catalogName =
normalizeCatalogName(results.getString("TABLE_CATALOG"));
final String schemaName = 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));
}
}
}
LOGGER.log(Level.INFO,
new StringFormat("Processed %d schemas", numSchemas));
}
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.hasQuery(SCHEMATA))
{
LOGGER.log(Level.FINE, "Schemata SQL statement was not provided");
return schemaRefs;
}
final Query schemataSql = informationSchemaViews.getQuery(SCHEMATA);
final Connection connection = getDatabaseConnection();
try (
final Statement statement = connection.createStatement();
final MetadataResultSet results = new MetadataResultSet(schemataSql,
statement,
getSchemaInclusionRule())
)
{
results.setDescription("retrieveAllSchemasFromInformationSchemaViews");
int numSchemas = 0;
while (results.next())
{
numSchemas = numSchemas + 1;
final String catalogName = results.getString("CATALOG_NAME");
final String schemaName = results.getString("SCHEMA_NAME");
LOGGER.log(Level.FINER,
new StringFormat("Retrieving schema: %s --> %s",
catalogName,
schemaName));
schemaRefs.add(new SchemaReference(catalogName, schemaName));
}
LOGGER.log(Level.INFO,
new StringFormat("Processed %d schemas", numSchemas));
}
catch (final Exception e)
{
LOGGER.log(Level.WARNING, "Could not retrieve schemas", e);
}
return schemaRefs;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy