Last active
February 22, 2021 23:27
-
-
Save sualeh/57159a2d44439c90f16b1db399421284 to your computer and use it in GitHub Desktop.
SchemaCrawler Issue #239
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import static sf.util.Utility.isBlank; | |
import static us.fatehi.commandlineparser.CommandLineUtility.applyApplicationLogLevel; | |
import static us.fatehi.commandlineparser.CommandLineUtility.logSystemClasspath; | |
import static us.fatehi.commandlineparser.CommandLineUtility.logSystemProperties; | |
import java.nio.file.Path; | |
import java.nio.file.Paths; | |
import java.sql.Connection; | |
import java.sql.SQLException; | |
import java.util.logging.Level; | |
import java.util.regex.Pattern; | |
import javax.sql.DataSource; | |
import schemacrawler.schemacrawler.DatabaseConnectionOptions; | |
import schemacrawler.schemacrawler.RegularExpressionInclusionRule; | |
import schemacrawler.schemacrawler.RegularExpressionRule; | |
import schemacrawler.schemacrawler.SchemaCrawlerException; | |
import schemacrawler.schemacrawler.SchemaCrawlerOptions; | |
import schemacrawler.schemacrawler.SchemaCrawlerOptionsBuilder; | |
import schemacrawler.schemacrawler.SchemaRetrievalOptions; | |
import schemacrawler.schemacrawler.SchemaRetrievalOptionsBuilder; | |
import schemacrawler.server.oracle.OracleDatabaseConnector; | |
import schemacrawler.tools.executable.SchemaCrawlerExecutable; | |
import schemacrawler.tools.options.InfoLevel; | |
import schemacrawler.tools.options.OutputOptions; | |
import schemacrawler.tools.options.OutputOptionsBuilder; | |
import schemacrawler.tools.options.TextOutputFormat; | |
public final class Issue239 | |
{ | |
public static void main(final String[] args) | |
throws Exception | |
{ | |
// Turn application logging on by applying the correct log level | |
applyApplicationLogLevel(Level.OFF); | |
// Log system properties and classpath | |
logSystemProperties(); | |
logSystemClasspath(); | |
final Path filePath = getOutputFile(args); | |
final TextOutputFormat textOutputFormat = TextOutputFormat.text; | |
final Pattern schemaInclusionList = Pattern.compile(".*"); | |
final Pattern schemaExclusionList = Pattern.compile(""); | |
final Pattern tableInclusionList = Pattern.compile(".*"); | |
final InfoLevel schemaInfoLevel = InfoLevel.maximum; | |
final SchemaCrawlerOptionsBuilder schemaCrawlerOptionsBuilder = SchemaCrawlerOptionsBuilder | |
.builder().withSchemaInfoLevel(schemaInfoLevel.buildSchemaInfoLevel()) | |
.includeSchemas(new RegularExpressionRule(schemaInclusionList, | |
schemaExclusionList)) | |
.includeAllRoutines() | |
.includeTables(new RegularExpressionInclusionRule(tableInclusionList)); | |
final SchemaCrawlerOptions schemaCrawlerOptions = schemaCrawlerOptionsBuilder | |
.toOptions(); | |
final Connection connection = getConnection(); | |
final OracleDatabaseConnector databaseConnector = new OracleDatabaseConnector(); | |
final SchemaRetrievalOptionsBuilder schemaRetrievalOptionsBuilder = databaseConnector | |
.getSchemaRetrievalOptionsBuilder(connection); | |
final SchemaRetrievalOptions schemaRetrievalOptions = schemaRetrievalOptionsBuilder | |
.toOptions(); | |
final OutputOptions outputOptions = OutputOptionsBuilder | |
.newOutputOptions(textOutputFormat, filePath); | |
final String command = "schema"; | |
final SchemaCrawlerExecutable executable = new SchemaCrawlerExecutable(command); | |
executable.setSchemaCrawlerOptions(schemaCrawlerOptions); | |
executable.setSchemaRetrievalOptions(schemaRetrievalOptions); | |
executable.setOutputOptions(outputOptions); | |
executable.setConnection(connection); | |
executable.execute(); | |
System.out.println("Created output file, " + filePath); | |
} | |
private static Connection getConnection() | |
throws SchemaCrawlerException, SQLException | |
{ | |
final String connectionUrl = "jdbc:hsqldb:hsql://localhost:9001/schemacrawler"; | |
final DataSource dataSource = new DatabaseConnectionOptions(connectionUrl); | |
return dataSource.getConnection("sa", ""); | |
} | |
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.text"; | |
} | |
final Path outputFile = Paths.get(outputfile).toAbsolutePath().normalize(); | |
return outputFile; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment