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

schemacrawler.crawl.RoutineExtRetriever 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://sourceforge.net/projects/schemacrawler
 * Copyright (c) 2000-2014, Sualeh Fatehi.
 *
 * This library is free software; you can redistribute it and/or modify it under the terms
 * of the GNU Lesser General Public License as published by the Free Software Foundation;
 * either version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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.
 * See the GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License along with this
 * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
 * Boston, MA 02111-1307, USA.
 *
 */

package schemacrawler.crawl;


import static sf.util.DatabaseUtility.executeSql;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;

import schemacrawler.schema.RoutineBodyType;
import schemacrawler.schemacrawler.InformationSchemaViews;

/**
 * A retriever that uses database metadata to get the extended details
 * about the database routines.
 *
 * @author Sualeh Fatehi
 */
final class RoutineExtRetriever
  extends AbstractRetriever
{

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

  RoutineExtRetriever(final RetrieverConnection retrieverConnection,
                      final MutableCatalog catalog)
    throws SQLException
  {
    super(retrieverConnection, catalog);
  }

  /**
   * Retrieves a routine definitions from the database.
   *
   * @throws SQLException
   *         On a SQL exception
   */
  void retrieveRoutineInformation()
    throws SQLException
  {
    final InformationSchemaViews informationSchemaViews = getRetrieverConnection()
      .getInformationSchemaViews();
    if (!informationSchemaViews.hasRoutinesSql())
    {
      LOGGER.log(Level.FINE,
                 "Routine definition SQL statement was not provided");
      return;
    }
    final String routineDefinitionsSql = informationSchemaViews
      .getRoutinesSql();

    final Connection connection = getDatabaseConnection();
    try (final Statement statement = connection.createStatement();
        final MetadataResultSet results = new MetadataResultSet(executeSql(statement,
                                                                           routineDefinitionsSql)))
    {
      while (results.next())
      {
        final String catalogName = quotedName(results
          .getString("ROUTINE_CATALOG"));
        final String schemaName = quotedName(results
          .getString("ROUTINE_SCHEMA"));
        final String routineName = quotedName(results.getString("ROUTINE_NAME"));
        final String specificName = quotedName(results
          .getString("SPECIFIC_NAME"));

        final MutableRoutine routine = lookupRoutine(catalogName,
                                                     schemaName,
                                                     routineName,
                                                     specificName);
        if (routine != null)
        {
          LOGGER.log(Level.FINER, "Retrieving routine information: "
                                  + routineName);
          final RoutineBodyType routineBodyType = results
            .getEnum("ROUTINE_BODY", RoutineBodyType.unknown);
          final String definition = results.getString("ROUTINE_DEFINITION");

          routine.setRoutineBodyType(routineBodyType);
          routine.appendDefinition(definition);

          routine.addAttributes(results.getAttributes());
        }
      }
    }
    catch (final Exception e)
    {
      LOGGER.log(Level.WARNING, "Could not retrieve routines", e);
    }
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy