예제 #1
0
  void retrieveTables(
      final String catalogName,
      final String schemaName,
      final String tableNamePattern,
      final TableType[] tableTypes,
      final InclusionRule tableInclusionRule)
      throws SQLException {
    if (tableInclusionRule == null || tableInclusionRule.equals(InclusionRule.EXCLUDE_ALL)) {
      return;
    }

    MetadataResultSet results = null;
    try {
      results =
          new MetadataResultSet(
              getMetaData()
                  .getTables(
                      unquotedName(catalogName),
                      unquotedName(schemaName),
                      tableNamePattern,
                      TableType.toStrings(tableTypes)));

      while (results.next()) {
        // "TABLE_CAT", "TABLE_SCHEM"
        final String tableName = quotedName(results.getString("TABLE_NAME"));
        LOGGER.log(Level.FINER, String.format("Retrieving table: %s.%s", schemaName, tableName));
        final TableType tableType = results.getEnum("TABLE_TYPE", TableType.unknown);
        final String remarks = results.getString("REMARKS");

        final MutableSchema schema = lookupSchema(catalogName, schemaName);
        if (schema == null) {
          LOGGER.log(
              Level.FINE, String.format("Cannot find schema, %s.%s", catalogName, schemaName));
          continue;
        }

        final MutableTable table;
        if (tableType == TableType.view) {
          table = new MutableView(schema, tableName);
        } else {
          table = new MutableTable(schema, tableName);
        }
        if (tableInclusionRule.include(table.getFullName())) {
          table.setType(tableType);
          table.setRemarks(remarks);

          schema.addTable(table);
        }
      }
    } finally {
      if (results != null) {
        results.close();
      }
    }
  }
 @Override
 public boolean include(final N namedObject) {
   if (namedObject == null) {
     return false;
   }
   // Schema names may be null
   if (namedObject.getFullName() == null) {
     return false;
   }
   return inclusionRule.include(namedObject.getFullName());
 }
  /**
   * Retrieves a list of schemas from the database, for the table specified.
   *
   * @param tableSort TODO
   * @param table Catalog for which data is required.
   * @throws SQLException On a SQL exception
   */
  void retrieveSchemas(final InclusionRule schemaInclusionRule) throws SQLException {
    final MetadataResultSet results =
        new MetadataResultSet(getRetrieverConnection().getMetaData().getSchemas());
    try {
      final boolean supportsCatalogs = getRetrieverConnection().isSupportsCatalogs();
      while (results.next()) {
        final String catalogName;
        if (supportsCatalogs) {
          catalogName = results.getString("TABLE_CATALOG");
        } else {
          catalogName = null;
        }
        final String schemaName = results.getString("TABLE_SCHEM");
        LOGGER.log(Level.FINER, String.format("Retrieving schema: %s.%s", catalogName, schemaName));

        final MutableCatalog[] catalogs;
        final MutableCatalog catalog = database.getCatalog(catalogName);
        if (catalog != null) {
          catalogs = new MutableCatalog[] {catalog};
        } else {
          final Catalog[] databaseCatalogs = database.getCatalogs();
          catalogs = new MutableCatalog[databaseCatalogs.length];
          for (int i = 0; i < databaseCatalogs.length; i++) {
            catalogs[i] = (MutableCatalog) databaseCatalogs[i];
          }
        }

        for (final MutableCatalog currentCatalog : catalogs) {
          final MutableSchema schema = new MutableSchema(currentCatalog, schemaName);
          final String schemaFullName = schema.getFullName();
          if (schemaInclusionRule.include(schemaFullName)) {
            currentCatalog.addSchema(schema);
          }
        }
      }
    } finally {
      results.close();
    }

    for (final Catalog catalog : database.getCatalogs()) {
      if (catalog.getSchemas().length == 0) {
        final MutableCatalog mutableCatalog = (MutableCatalog) catalog;
        final MutableSchema schema = new MutableSchema(mutableCatalog, null);
        mutableCatalog.addSchema(schema);
      }
    }
  }
예제 #4
0
  void retrieveColumns(final MutableTable table, final InclusionRule columnInclusionRule)
      throws SQLException {
    MetadataResultSet results = null;
    try {
      results =
          new MetadataResultSet(
              getMetaData()
                  .getColumns(
                      unquotedName(table.getSchema().getCatalogName()),
                      unquotedName(table.getSchema().getSchemaName()),
                      unquotedName(table.getName()),
                      null));

      while (results.next()) {
        // Get the "COLUMN_DEF" value first as it the Oracle drivers
        // don't handle it properly otherwise.
        // http://issues.apache.org/jira/browse/DDLUTILS-29?page=all
        final String defaultValue = results.getString("COLUMN_DEF");
        //
        final String columnCatalogName = quotedName(results.getString("TABLE_CAT"));
        final String schemaName = quotedName(results.getString("TABLE_SCHEM"));
        final String tableName = quotedName(results.getString("TABLE_NAME"));
        final String columnName = quotedName(results.getString("COLUMN_NAME"));
        LOGGER.log(Level.FINER, String.format("Retrieving column: %s.%s", tableName, columnName));

        MutableColumn column;

        column = lookupOrCreateColumn(table, columnName, false /* add */);
        final String columnFullName = column.getFullName();
        // Note: If the table name contains an underscore character,
        // this is a wildcard character. We need to do another check to
        // see if the table name matches.
        if (columnInclusionRule.include(columnFullName)
            && table.getName().equals(tableName)
            && belongsToSchema(table, columnCatalogName, schemaName)) {
          column = lookupOrCreateColumn(table, columnName, true /* add */);

          final int ordinalPosition = results.getInt("ORDINAL_POSITION", 0);
          final int dataType = results.getInt("DATA_TYPE", 0);
          final String typeName = results.getString("TYPE_NAME");
          final int size = results.getInt("COLUMN_SIZE", 0);
          final int decimalDigits = results.getInt("DECIMAL_DIGITS", 0);
          final boolean isNullable =
              results.getInt("NULLABLE", DatabaseMetaData.columnNullableUnknown)
                  == DatabaseMetaData.columnNullable;
          final String remarks = results.getString("REMARKS");

          column.setOrdinalPosition(ordinalPosition);
          column.setType(
              lookupOrCreateColumnDataType((MutableSchema) table.getSchema(), dataType, typeName));
          column.setSize(size);
          column.setDecimalDigits(decimalDigits);
          column.setRemarks(remarks);
          column.setNullable(isNullable);
          if (defaultValue != null) {
            column.setDefaultValue(defaultValue);
          }

          column.addAttributes(results.getAttributes());

          table.addColumn(column);
        }
      }
    } catch (final SQLException e) {
      throw new SchemaCrawlerSQLException("Could not retrieve columns for table " + table, e);
    } finally {
      if (results != null) {
        results.close();
      }
    }
  }