Skip to content
Advertisement

How to generate .dot file using Schemacrawler

Using schemcrawler I’ve generated html file

public final class ExecutableExample {

public static void main(final String[] args) throws Exception {

    // Set log level
    new LoggingConfig(Level.OFF);
    final LimitOptionsBuilder limitOptionsBuilder = LimitOptionsBuilder.builder()
            .includeSchemas(new IncludeAll())
            .includeTables(new IncludeAll());
    final LoadOptionsBuilder loadOptionsBuilder =
            LoadOptionsBuilder.builder()
                    // Set what details are required in the schema - this affects the
                    // time taken to crawl the schema
                    .withSchemaInfoLevel(SchemaInfoLevelBuilder.standard());
    final SchemaCrawlerOptions options =
            SchemaCrawlerOptionsBuilder.newSchemaCrawlerOptions()
                    .withLimitOptions(limitOptionsBuilder.toOptions())
                    .withLoadOptions(loadOptionsBuilder.toOptions());

    final Path outputFile = getOutputFile(args);
    final OutputOptions outputOptions =
            OutputOptionsBuilder.newOutputOptions(TextOutputFormat.html, outputFile);
    final String command = "schema";

    try (Connection connection = getConnection()) {
        final SchemaCrawlerExecutable executable = new SchemaCrawlerExecutable(command);
        executable.setSchemaCrawlerOptions(options);
        executable.setOutputOptions(outputOptions);
        executable.setConnection(connection);
        executable.execute();
    }

    System.out.println("Created output file, " + outputFile);
}

private static Connection getConnection() {
    final String connectionUrl = "jdbc:postgresql://localhost:5433/table_accounts";
    final DatabaseConnectionSource dataSource = new DatabaseConnectionSource(connectionUrl);
    dataSource.setUserCredentials(new SingleUseUserCredentials("postgres", "new_password"));
    return dataSource.get();
}

private static Path getOutputFile(final String[] args) {
    final String outputfile;
    if (args != null && args.length > 0 && !isBlank(args[0])) {
        outputfile = args[0];
    } else {
        outputfile = "./schemacrawler_output.html";
    }
    final Path outputFile = Paths.get(outputfile).toAbsolutePath().normalize();
    return outputFile;
}

But I want to have an output in .dot file that contains diagram, node, graph, edge etc.. So how can I do it using my code or maybe some another way to do it with Java?

Advertisement

Answer

Simply change the output format from TextOutputFormat.html to DiagramOutputFormat.scdot.

Sualeh Fatehi, SchemaCrawler

8 People found this is helpful
Advertisement