Пример #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();
      }
    }
  }
Пример #2
0
  private void createIndices(final MutableTable table, final MetadataResultSet results)
      throws SQLException {
    try {
      while (results.next()) {
        // "TABLE_CAT", "TABLE_SCHEM", "TABLE_NAME"
        String indexName = quotedName(results.getString("INDEX_NAME"));
        if (Utility.isBlank(indexName)) {
          indexName = UNKNOWN;
        }
        LOGGER.log(
            Level.FINER, String.format("Retrieving index: %s.%s", table.getFullName(), indexName));
        final String columnName = quotedName(results.getString("COLUMN_NAME"));
        if (Utility.isBlank(columnName)) {
          continue;
        }

        MutableIndex index = table.getIndex(indexName);
        if (index == null) {
          index = new MutableIndex(table, indexName);
          table.addIndex(index);
        }

        final boolean uniqueIndex = !results.getBoolean("NON_UNIQUE");
        final int type = results.getInt("TYPE", IndexType.unknown.getId());
        final int ordinalPosition = results.getInt("ORDINAL_POSITION", 0);
        final IndexColumnSortSequence sortSequence =
            IndexColumnSortSequence.valueOfFromCode(results.getString("ASC_OR_DESC"));
        final int cardinality = results.getInt("CARDINALITY", 0);
        final int pages = results.getInt("PAGES", 0);

        final MutableColumn column = table.getColumn(columnName);
        if (column != null) {
          column.setPartOfUniqueIndex(uniqueIndex);
          final MutableIndexColumn indexColumn = new MutableIndexColumn(index, column);
          indexColumn.setIndexOrdinalPosition(ordinalPosition);
          indexColumn.setSortSequence(sortSequence);
          //
          index.addColumn(indexColumn);
          index.setUnique(uniqueIndex);
          index.setType(IndexType.valueOf(type));
          index.setCardinality(cardinality);
          index.setPages(pages);
          index.addAttributes(results.getAttributes());
        }
      }
    } finally {
      results.close();
    }
  }