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();
      }
    }
  }
示例#2
0
  void retrieveTables(
      final Schema schema,
      final String tableNamePattern,
      final Collection<String> tableTypes,
      final InclusionRule tableInclusionRule)
      throws SQLException {
    requireNonNull(schema, "No schema provided");

    final InclusionRuleFilter<Table> tableFilter =
        new InclusionRuleFilter<>(tableInclusionRule, false);
    if (tableFilter.isExcludeAll()) {
      LOGGER.log(Level.INFO, "Not retrieving tables, since this was not requested");
      return;
    }

    final Optional<Schema> schemaOptional = catalog.lookupSchema(schema.getFullName());
    if (!schemaOptional.isPresent()) {
      LOGGER.log(
          Level.INFO,
          new StringFormat(
              "Cannot locate schema, so not retrieving tables for schema: %s", schema));
      return;
    }

    LOGGER.log(Level.INFO, new StringFormat("Retrieving tables for schema: %s", schema));

    final TableTypes supportedTableTypes = getRetrieverConnection().getTableTypes();
    final String[] filteredTableTypes = supportedTableTypes.filterUnknown(tableTypes);
    LOGGER.log(
        Level.FINER,
        new StringFormat(
            "Retrieving table types: %s",
            filteredTableTypes == null ? "<<all>>" : Arrays.asList(filteredTableTypes)));

    final String catalogName = schema.getCatalogName();
    final String schemaName = schema.getName();

    try (final MetadataResultSet results =
        new MetadataResultSet(
            getMetaData()
                .getTables(
                    unquotedName(catalogName),
                    unquotedName(schemaName),
                    tableNamePattern,
                    filteredTableTypes)); ) {
      results.setDescription("retrieveTables");
      while (results.next()) {
        // "TABLE_CAT", "TABLE_SCHEM"
        final String tableName = quotedName(results.getString("TABLE_NAME"));
        LOGGER.log(Level.FINE, String.format("Retrieving table: %s.%s", schema, tableName));
        final String tableTypeString = results.getString("TABLE_TYPE");
        final String remarks = results.getString("REMARKS");

        final TableType tableType =
            supportedTableTypes.lookupTableType(tableTypeString).orElse(TableType.UNKNOWN);
        if (tableType.equals(TableType.UNKNOWN)) {
          LOGGER.log(
              Level.FINE,
              new StringFormat(
                  "Unknown table type, %s, for %s.%s", tableTypeString, schema, tableName));
        }

        final MutableTable table;
        if (tableType.isView()) {
          table = new MutableView(schema, tableName);
        } else {
          table = new MutableTable(schema, tableName);
        }
        if (tableFilter.test(table)) {
          table.setTableType(tableType);
          table.setRemarks(remarks);

          catalog.addTable(table);
        }
      }
    }
  }