public static void main(final String[] args) throws Exception {
    // Create a database connection
    final DataSource dataSource =
        new DatabaseConnectionOptions(
            "org.hsqldb.jdbcDriver", "jdbc:hsqldb:hsql://localhost:9001/schemacrawler");
    final Connection connection = dataSource.getConnection("sa", "");

    // Create the options
    final SchemaCrawlerOptions options = new SchemaCrawlerOptions();
    // Set what details are required in the schema - this affects the
    // time taken to crawl the schema
    options.setSchemaInfoLevel(SchemaInfoLevel.standard());
    options.setRoutineInclusionRule(new InclusionRule(InclusionRule.NONE, InclusionRule.ALL));
    options.setSchemaInclusionRule(new InclusionRule("PUBLIC.BOOKS", InclusionRule.NONE));

    // Get the schema definition
    final Database database = SchemaCrawlerUtility.getDatabase(connection, options);

    for (final Schema schema : database.getSchemas()) {
      System.out.println(schema);
      for (final Table table : database.getTables(schema)) {
        System.out.print("o--> " + table);
        if (table instanceof View) {
          System.out.println(" (VIEW)");
        } else {
          System.out.println();
        }

        for (final Column column : table.getColumns()) {
          System.out.println("     o--> " + column + " (" + column.getColumnDataType() + ")");
        }
      }
    }
  }
Beispiel #2
0
  private void analyseSchema() {
    try {
      final String jdbcUrl = m_options.m_jdbcUrl;
      final String schemaName = m_options.m_schemaName;

      System.out.printf("Analysing schema [%s] at [%s]...%n", schemaName, jdbcUrl);
      final long msecStart = System.currentTimeMillis();

      // Create a database connection
      final DataSource dataSource = new DatabaseConnectionOptions(jdbcUrl);
      final Connection connection =
          dataSource.getConnection(m_options.m_username, m_options.m_password);

      // Create the options
      final SchemaCrawlerOptions options = new SchemaCrawlerOptions();

      // Set what details are required in the schema - this affects the
      // time taken to crawl the schema
      options.setSchemaInfoLevel(SchemaInfoLevel.standard());
      options.setSchemaInclusionRule(new RegularExpressionInclusionRule(schemaName));

      final InclusionRule tableRule = new RegularExpressionExclusionRule(".+[\\$/].+");

      final InclusionRule procRule =
          s -> {
            // System.out.println (s);
            final String[] a =
                new String[] {
                  ".*" + "\\$" + ".*",
                  ".*" + "_RETRIGGER_EXPRESSION" + ".*",
                  ".*" + "BITOR" + ".*",
                  ".*" + "CENTRED" + ".*",
                  ".*" + "COMPLEMENT" + ".*",
                  ".*" + "D2" + ".*",
                  ".*" + "DDMMYYYYHHMMMSS" + ".*",
                  ".*" + "DOW" + ".*",
                  ".*" + "DSM" + ".*",
                  ".*" + "ENABLE" + ".*",
                  ".*" + "F2" + ".*",
                  ".*" + "GET_LAST_CHANGE_NUMBER" + ".*",
                  ".*" + "GROOM" + ".*",
                  ".*" + "IS_EVEN" + ".*",
                  ".*" + "IS_ODD" + ".*",
                  ".*" + "LEFT_PADDED" + ".*",
                  ".*" + "LOGICAL_" + ".*",
                  ".*" + "M2" + ".*",
                  ".*" + "MD5" + ".*",
                  ".*" + "MERGE_OSS_TRIP_LOCATION" + ".*",
                  ".*" + "MODF" + ".*",
                  ".*" + "NEXT_TIME_ABSOLUTE" + ".*",
                  ".*" + "NEXT_TIME_RELATIVE" + ".*",
                  ".*" + "RESERVE_FARM_STATUS" + ".*",
                  ".*" + "RIGHT_PADDED" + ".*",
                  ".*" + "S2" + ".*",
                  ".*" + "SCHEDULE_JOB" + ".*",
                  ".*" + "SNDF" + ".*",
                  ".*" + "SSM" + ".*",
                  ".*" + "XOR" + ".*",
                  ".*" + "XSD_TIMESTAMP" + ".*",
                  ".*" + "//dummy//" + ".*"
                };
            return !HcUtil.containsWildcard(a, s);
          };
      options.setTableInclusionRule(tableRule);

      options.setRoutineInclusionRule(procRule); // new ExcludeAll ()

      // Get the schema definition
      final Catalog catalog = SchemaCrawlerUtility.getCatalog(connection, options);
      final long msecDuration = System.currentTimeMillis() - msecStart;

      final Collection<Schema> schemas = catalog.getSchemas();
      ThreadContext.assertFault(
          schemas.size() == 1, "Expected one schema, got %s [%s]", schemas.size(), schemas.size());

      final Schema[] a = schemas.toArray(new Schema[schemas.size()]);
      final Schema schema = a[0];

      final DatabaseInfo di = catalog.getDatabaseInfo();

      System.out.printf(
          "  catalog[%s] database[%s] version[%s]%n",
          schema.getFullName(), di.getProductName(), di.getProductVersion());
      System.out.printf("  %s tables%n", catalog.getTables(schema).size());
      System.out.printf("  %s stored procedures%n", catalog.getRoutines(schema).size());
      System.out.printf("...analysed in %s seconds%n", msecDuration / 1_000L);

      outputSchemaXml(catalog, schema);
      System.out.printf("Schema file [%s] created.%n", m_options.m_outputFilename);
    } catch (final SchemaCrawlerException | SQLException e) {
      // Propagate exception as unchecked fault up to the fault barrier.
      ThreadContext.throwFault(e);
    }
  }