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

schemacrawler.tools.traversal.SchemaTraverser 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.tools.traversal;


import static java.util.Objects.requireNonNull;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import schemacrawler.schema.Catalog;
import schemacrawler.schema.ColumnDataType;
import schemacrawler.schema.NamedObject;
import schemacrawler.schema.Routine;
import schemacrawler.schema.Sequence;
import schemacrawler.schema.Synonym;
import schemacrawler.schema.Table;
import schemacrawler.schemacrawler.SchemaCrawlerException;
import schemacrawler.utility.NamedObjectSort;

public class SchemaTraverser
{

  private Catalog catalog;
  private SchemaTraversalHandler handler;
  private Comparator tablesComparator;
  private Comparator routinesComparator;

  public SchemaTraverser()
  {
    tablesComparator = NamedObjectSort.natural;
    routinesComparator = NamedObjectSort.natural;
  }

  public Catalog getCatalog()
  {
    return catalog;
  }

  public SchemaTraversalHandler getHandler()
  {
    return handler;
  }

  public Comparator getRoutinesComparator()
  {
    return routinesComparator;
  }

  public Comparator getTablesComparator()
  {
    return tablesComparator;
  }

  public void setCatalog(final Catalog catalog)
  {
    this.catalog = requireNonNull(catalog, "No catalog provided");
  }

  public void setHandler(final SchemaTraversalHandler handler)
  {
    this.handler = requireNonNull(handler, "No handler provided");
  }

  public void setRoutinesComparator(final Comparator routinesComparator)
  {
    this.routinesComparator = requireNonNull(routinesComparator);
  }

  public void setTablesComparator(final Comparator tablesComparator)
  {
    this.tablesComparator = requireNonNull(tablesComparator);
  }

  public final void traverse()
    throws SchemaCrawlerException
  {

    final Collection columnDataTypes = catalog
      .getColumnDataTypes();
    final Collection tables = catalog.getTables();
    final Collection routines = catalog.getRoutines();
    final Collection synonyms = catalog.getSynonyms();
    final Collection sequences = catalog.getSequences();

    handler.begin();

    handler.handleHeaderStart();
    handler.handle(catalog.getCrawlInfo());
    handler.handleHeaderEnd();

    if (!tables.isEmpty())
    {

      handler.handleTablesStart();

      final List tablesList = new ArrayList<>(tables);
      Collections.sort(tablesList, tablesComparator);
      for (final Table table: tablesList)
      {
        handler.handle(table);
      }

      handler.handleTablesEnd();
    }

    if (!routines.isEmpty())
    {
      handler.handleRoutinesStart();

      final List routinesList = new ArrayList<>(routines);
      Collections.sort(routinesList, routinesComparator);
      for (final Routine routine: routinesList)
      {
        handler.handle(routine);
      }

      handler.handleRoutinesEnd();
    }

    if (!sequences.isEmpty())
    {
      handler.handleSequencesStart();
      for (final Sequence sequence: sequences)
      {
        handler.handle(sequence);
      }
      handler.handleSequencesEnd();
    }

    if (!synonyms.isEmpty())
    {
      handler.handleSynonymsStart();
      for (final Synonym synonym: synonyms)
      {
        handler.handle(synonym);
      }
      handler.handleSynonymsEnd();
    }

    if (!columnDataTypes.isEmpty())
    {
      handler.handleColumnDataTypesStart();
      for (final ColumnDataType columnDataType: columnDataTypes)
      {
        handler.handle(columnDataType);
      }
      handler.handleColumnDataTypesEnd();
    }

    handler.handleInfoStart();
    handler.handle(catalog.getSchemaCrawlerInfo());
    handler.handle(catalog.getDatabaseInfo());
    handler.handle(catalog.getJdbcDriverInfo());
    handler.handleInfoEnd();

    handler.end();
  }

}