From be84cea942e6f3e2415308d4aa2c99ff00e091f2 Mon Sep 17 00:00:00 2001 From: Sualeh Fatehi Date: Fri, 14 Jul 2023 15:32:40 -0400 Subject: [PATCH] Pipe hide options into the formatters --- .../options/BaseSchemaTextOptionsBuilder.java | 64 +++++++++++++++++++ .../schema/options/SchemaTextOptions.java | 19 ++++-- .../formatter/schema/SchemaListFormatter.java | 54 +++++++++++++++- .../formatter/schema/SchemaTextFormatter.java | 32 ++++++++++ 4 files changed, 164 insertions(+), 5 deletions(-) diff --git a/schemacrawler-text/src/main/java/schemacrawler/tools/command/text/schema/options/BaseSchemaTextOptionsBuilder.java b/schemacrawler-text/src/main/java/schemacrawler/tools/command/text/schema/options/BaseSchemaTextOptionsBuilder.java index b0a208b45f..536b29bbb7 100644 --- a/schemacrawler-text/src/main/java/schemacrawler/tools/command/text/schema/options/BaseSchemaTextOptionsBuilder.java +++ b/schemacrawler-text/src/main/java/schemacrawler/tools/command/text/schema/options/BaseSchemaTextOptionsBuilder.java @@ -36,6 +36,11 @@ import static schemacrawler.tools.command.text.schema.options.HideDatabaseObjectNamesType.hideTableConstraintNames; import static schemacrawler.tools.command.text.schema.options.HideDatabaseObjectNamesType.hideTriggerNames; import static schemacrawler.tools.command.text.schema.options.HideDatabaseObjectNamesType.hideWeakAssociationNames; +import static schemacrawler.tools.command.text.schema.options.HideDatabaseObjectsType.hideRoutines; +import static schemacrawler.tools.command.text.schema.options.HideDatabaseObjectsType.hideSchemas; +import static schemacrawler.tools.command.text.schema.options.HideDatabaseObjectsType.hideSequences; +import static schemacrawler.tools.command.text.schema.options.HideDatabaseObjectsType.hideSynonyms; +import static schemacrawler.tools.command.text.schema.options.HideDatabaseObjectsType.hideTables; import static schemacrawler.tools.command.text.schema.options.HideDependantDatabaseObjectsType.hideAlternateKeys; import static schemacrawler.tools.command.text.schema.options.HideDependantDatabaseObjectsType.hideForeignKeys; import static schemacrawler.tools.command.text.schema.options.HideDependantDatabaseObjectsType.hideIndexes; @@ -74,10 +79,12 @@ public abstract class BaseSchemaTextOptionsBuilder< protected boolean isShowOrdinalNumbers; protected boolean isShowStandardColumnTypeNames; protected boolean isHideTableRowCounts; + protected final Map hideDatabaseObjects; protected final Map hideDependantDatabaseObjects; protected final Map hideNames; public BaseSchemaTextOptionsBuilder() { + hideDatabaseObjects = new EnumMap<>(HideDatabaseObjectsType.class); hideDependantDatabaseObjects = new EnumMap<>(HideDependantDatabaseObjectsType.class); hideNames = new EnumMap<>(HideDatabaseObjectNamesType.class); } @@ -99,6 +106,10 @@ public B fromConfig(final Config config) { config.getBooleanValue(SC_SORT_ALPHABETICALLY_TABLE_FOREIGNKEYS); isAlphabeticalSortForIndexes = config.getBooleanValue(SC_SORT_ALPHABETICALLY_TABLE_INDEXES); + for (final HideDatabaseObjectsType databaseObjectsType : HideDatabaseObjectsType.values()) { + final boolean booleanValue = config.getBooleanValue(databaseObjectsType.getKey()); + hideDatabaseObjects.put(databaseObjectsType, booleanValue); + } for (final HideDependantDatabaseObjectsType databaseObjectsType : HideDependantDatabaseObjectsType.values()) { final boolean booleanValue = config.getBooleanValue(databaseObjectsType.getKey()); @@ -132,6 +143,9 @@ public B fromOptions(final O options) { isAlphabeticalSortForForeignKeys = options.isAlphabeticalSortForForeignKeys(); isAlphabeticalSortForIndexes = options.isAlphabeticalSortForIndexes(); + for (final HideDatabaseObjectsType databaseObjectsType : HideDatabaseObjectsType.values()) { + hideDatabaseObjects.put(databaseObjectsType, options.is(databaseObjectsType)); + } for (final HideDependantDatabaseObjectsType databaseObjectsType : HideDependantDatabaseObjectsType.values()) { hideDependantDatabaseObjects.put(databaseObjectsType, options.is(databaseObjectsType)); @@ -254,6 +268,15 @@ public final B noRoutineParameters(final boolean value) { return (B) this; } + public final B noRoutines() { + return noRoutines(true); + } + + public final B noRoutines(final boolean value) { + hideDatabaseObjects.put(hideRoutines, value); + return (B) this; + } + public final B noRoutineSpecificNames() { return noRoutineSpecificNames(true); } @@ -263,6 +286,33 @@ public final B noRoutineSpecificNames(final boolean value) { return (B) this; } + public final B noSchemas() { + return noSchemas(true); + } + + public final B noSchemas(final boolean value) { + hideDatabaseObjects.put(hideSchemas, value); + return (B) this; + } + + public final B noSequences() { + return noSequences(true); + } + + public final B noSequences(final boolean value) { + hideDatabaseObjects.put(hideSequences, value); + return (B) this; + } + + public final B noSynonyms() { + return noSynonyms(true); + } + + public final B noSynonyms(final boolean value) { + hideDatabaseObjects.put(hideSynonyms, value); + return (B) this; + } + public final B noTableColumns() { return noTableColumns(true); } @@ -281,6 +331,15 @@ public final B noTableConstraints(final boolean value) { return (B) this; } + public final B noTables() { + return noTables(true); + } + + public final B noTables(final boolean value) { + hideDatabaseObjects.put(hideTables, value); + return (B) this; + } + public final B noTriggerNames() { return noTriggerNames(true); } @@ -383,6 +442,11 @@ public Config toConfig() { config.put(SC_SORT_ALPHABETICALLY_TABLE_FOREIGNKEYS, isAlphabeticalSortForForeignKeys); config.put(SC_SORT_ALPHABETICALLY_TABLE_INDEXES, isAlphabeticalSortForIndexes); + for (final HideDatabaseObjectsType databaseObjectsType : HideDatabaseObjectsType.values()) { + config.put( + databaseObjectsType.getKey(), + hideDatabaseObjects.getOrDefault(databaseObjectsType, false)); + } for (final HideDependantDatabaseObjectsType databaseObjectsType : HideDependantDatabaseObjectsType.values()) { config.put( diff --git a/schemacrawler-text/src/main/java/schemacrawler/tools/command/text/schema/options/SchemaTextOptions.java b/schemacrawler-text/src/main/java/schemacrawler/tools/command/text/schema/options/SchemaTextOptions.java index f9fb66cb7c..2b220ae042 100644 --- a/schemacrawler-text/src/main/java/schemacrawler/tools/command/text/schema/options/SchemaTextOptions.java +++ b/schemacrawler-text/src/main/java/schemacrawler/tools/command/text/schema/options/SchemaTextOptions.java @@ -40,7 +40,8 @@ public class SchemaTextOptions extends BaseTextOptions { private final boolean isShowOrdinalNumbers; private final boolean isShowStandardColumnTypeNames; private final boolean isHideTableRowCounts; - private final Map hideDatabaseObjects; + private final Map hideDatabaseObjects; + private final Map hideDependantDatabaseObjects; private final Map hideNames; protected SchemaTextOptions( @@ -54,10 +55,16 @@ protected SchemaTextOptions( isShowStandardColumnTypeNames = builder.isShowStandardColumnTypeNames; isHideTableRowCounts = builder.isHideTableRowCounts; - hideDatabaseObjects = new EnumMap<>(HideDependantDatabaseObjectsType.class); + hideDatabaseObjects = new EnumMap<>(HideDatabaseObjectsType.class); + for (final HideDatabaseObjectsType databaseObjectsType : HideDatabaseObjectsType.values()) { + hideDatabaseObjects.put( + databaseObjectsType, + builder.hideDatabaseObjects.getOrDefault(databaseObjectsType, false)); + } + hideDependantDatabaseObjects = new EnumMap<>(HideDependantDatabaseObjectsType.class); for (final HideDependantDatabaseObjectsType databaseObjectsType : HideDependantDatabaseObjectsType.values()) { - hideDatabaseObjects.put( + hideDependantDatabaseObjects.put( databaseObjectsType, builder.hideDependantDatabaseObjects.getOrDefault(databaseObjectsType, false)); } @@ -73,10 +80,14 @@ public boolean is(final HideDatabaseObjectNamesType key) { return hideNames.getOrDefault(key, false); } - public boolean is(final HideDependantDatabaseObjectsType key) { + public boolean is(final HideDatabaseObjectsType key) { return hideDatabaseObjects.getOrDefault(key, false); } + public boolean is(final HideDependantDatabaseObjectsType key) { + return hideDependantDatabaseObjects.getOrDefault(key, false); + } + public boolean isAlphabeticalSortForForeignKeys() { return isAlphabeticalSortForForeignKeys; } diff --git a/schemacrawler-text/src/main/java/schemacrawler/tools/text/formatter/schema/SchemaListFormatter.java b/schemacrawler-text/src/main/java/schemacrawler/tools/text/formatter/schema/SchemaListFormatter.java index 6ea0d64236..e270e5a63c 100644 --- a/schemacrawler-text/src/main/java/schemacrawler/tools/text/formatter/schema/SchemaListFormatter.java +++ b/schemacrawler-text/src/main/java/schemacrawler/tools/text/formatter/schema/SchemaListFormatter.java @@ -28,8 +28,11 @@ package schemacrawler.tools.text.formatter.schema; +import static schemacrawler.tools.command.text.schema.options.HideDatabaseObjectsType.hideRoutines; +import static schemacrawler.tools.command.text.schema.options.HideDatabaseObjectsType.hideSequences; +import static schemacrawler.tools.command.text.schema.options.HideDatabaseObjectsType.hideSynonyms; +import static schemacrawler.tools.command.text.schema.options.HideDatabaseObjectsType.hideTables; import static us.fatehi.utility.Utility.isBlank; - import schemacrawler.schema.ColumnDataType; import schemacrawler.schema.CrawlInfo; import schemacrawler.schema.DatabaseInfo; @@ -40,6 +43,7 @@ import schemacrawler.schema.Synonym; import schemacrawler.schema.Table; import schemacrawler.schemacrawler.Identifiers; +import schemacrawler.tools.command.text.schema.options.HideDatabaseObjectsType; import schemacrawler.tools.command.text.schema.options.SchemaTextDetailType; import schemacrawler.tools.command.text.schema.options.SchemaTextOptions; import schemacrawler.tools.options.OutputOptions; @@ -140,6 +144,10 @@ public void handle(final JdbcDriverInfo driverInfo) { /** {@inheritDoc} */ @Override public void handle(final Routine routine) { + if (routine == null || options.is(hideRoutines)) { + return; + } + final String routineTypeDetail = String.format("%s, %s", routine.getRoutineType(), routine.getReturnType()); final String routineName = quoteName(routine); @@ -152,6 +160,10 @@ public void handle(final Routine routine) { /** {@inheritDoc} */ @Override public void handle(final Sequence sequence) { + if (sequence == null || options.is(hideSequences)) { + return; + } + final String sequenceName = quoteName(sequence); final String sequenceType = "[sequence]"; @@ -162,6 +174,10 @@ public void handle(final Sequence sequence) { /** {@inheritDoc} */ @Override public void handle(final Synonym synonym) { + if (synonym == null || options.is(hideSynonyms)) { + return; + } + final String synonymName = quoteName(synonym); final String synonymType = "[synonym]"; @@ -171,6 +187,10 @@ public void handle(final Synonym synonym) { @Override public void handle(final Table table) { + if (options.is(hideTables)) { + return; + } + final String tableName = quoteName(table); final String tableType = "[" + table.getTableType() + "]"; @@ -213,12 +233,20 @@ public void handleInfoStart() { /** {@inheritDoc} */ @Override public void handleRoutinesEnd() { + if (options.is(hideRoutines)) { + return; + } + formattingHelper.writeObjectEnd(); } /** {@inheritDoc} */ @Override public void handleRoutinesStart() { + if (options.is(hideRoutines)) { + return; + } + formattingHelper.writeHeader(DocumentHeaderType.subTitle, "Routines"); formattingHelper.writeObjectStart(); @@ -227,12 +255,20 @@ public void handleRoutinesStart() { /** {@inheritDoc} */ @Override public void handleSequencesEnd() { + if (options.is(hideSequences)) { + return; + } + formattingHelper.writeObjectEnd(); } /** {@inheritDoc} */ @Override public void handleSequencesStart() { + if (options.is(hideSequences)) { + return; + } + formattingHelper.writeHeader(DocumentHeaderType.subTitle, "Sequences"); formattingHelper.writeObjectStart(); @@ -241,12 +277,20 @@ public void handleSequencesStart() { /** {@inheritDoc} */ @Override public void handleSynonymsEnd() { + if (options.is(hideSynonyms)) { + return; + } + formattingHelper.writeObjectEnd(); } /** {@inheritDoc} */ @Override public void handleSynonymsStart() { + if (options.is(HideDatabaseObjectsType.hideSynonyms)) { + return; + } + formattingHelper.writeHeader(DocumentHeaderType.subTitle, "Synonyms"); formattingHelper.writeObjectStart(); @@ -255,12 +299,20 @@ public void handleSynonymsStart() { /** {@inheritDoc} */ @Override public void handleTablesEnd() { + if (options.is(hideTables)) { + return; + } + formattingHelper.writeObjectEnd(); } /** {@inheritDoc} */ @Override public void handleTablesStart() { + if (options.is(hideTables)) { + return; + } + formattingHelper.writeHeader(DocumentHeaderType.subTitle, "Tables"); formattingHelper.writeObjectStart(); diff --git a/schemacrawler-text/src/main/java/schemacrawler/tools/text/formatter/schema/SchemaTextFormatter.java b/schemacrawler-text/src/main/java/schemacrawler/tools/text/formatter/schema/SchemaTextFormatter.java index d108eb6462..7353345264 100644 --- a/schemacrawler-text/src/main/java/schemacrawler/tools/text/formatter/schema/SchemaTextFormatter.java +++ b/schemacrawler-text/src/main/java/schemacrawler/tools/text/formatter/schema/SchemaTextFormatter.java @@ -40,6 +40,10 @@ import static schemacrawler.tools.command.text.schema.options.HideDatabaseObjectNamesType.hideTableConstraintNames; import static schemacrawler.tools.command.text.schema.options.HideDatabaseObjectNamesType.hideTriggerNames; import static schemacrawler.tools.command.text.schema.options.HideDatabaseObjectNamesType.hideWeakAssociationNames; +import static schemacrawler.tools.command.text.schema.options.HideDatabaseObjectsType.hideRoutines; +import static schemacrawler.tools.command.text.schema.options.HideDatabaseObjectsType.hideSequences; +import static schemacrawler.tools.command.text.schema.options.HideDatabaseObjectsType.hideSynonyms; +import static schemacrawler.tools.command.text.schema.options.HideDatabaseObjectsType.hideTables; import static schemacrawler.tools.command.text.schema.options.HideDependantDatabaseObjectsType.hideAlternateKeys; import static schemacrawler.tools.command.text.schema.options.HideDependantDatabaseObjectsType.hideForeignKeys; import static schemacrawler.tools.command.text.schema.options.HideDependantDatabaseObjectsType.hideIndexes; @@ -141,6 +145,10 @@ public void handle(final ColumnDataType columnDataType) { /** {@inheritDoc} */ @Override public void handle(final Routine routine) { + if (routine == null || options.is(hideRoutines)) { + return; + } + final String routineTypeDetail = String.format("%s, %s", routine.getRoutineType(), routine.getReturnType()); final String routineName = quoteName(routine); @@ -202,6 +210,10 @@ public void handle(final Sequence sequence) { /** {@inheritDoc} */ @Override public void handle(final Synonym synonym) { + if (synonym == null || options.is(hideSynonyms)) { + return; + } + final String synonymName = quoteName(synonym); final String synonymType = "[synonym]"; @@ -229,6 +241,10 @@ public void handle(final Synonym synonym) { @Override public void handle(final Table table) { + if (table == null || options.is(hideTables)) { + return; + } + final String tableName = quoteName(table); final String tableType = "[" + table.getTableType() + "]"; @@ -290,6 +306,10 @@ public void handleRoutinesEnd() { /** {@inheritDoc} */ @Override public void handleRoutinesStart() { + if (options.is(hideRoutines)) { + return; + } + formattingHelper.writeHeader(DocumentHeaderType.subTitle, "Routines"); } @@ -302,6 +322,10 @@ public void handleSequencesEnd() { /** {@inheritDoc} */ @Override public void handleSequencesStart() { + if (options.is(hideSequences)) { + return; + } + formattingHelper.writeHeader(DocumentHeaderType.subTitle, "Sequences"); } @@ -314,6 +338,10 @@ public void handleSynonymsEnd() { /** {@inheritDoc} */ @Override public void handleSynonymsStart() { + if (options.is(hideSynonyms)) { + return; + } + formattingHelper.writeHeader(DocumentHeaderType.subTitle, "Synonyms"); } @@ -326,6 +354,10 @@ public void handleTablesEnd() { /** {@inheritDoc} */ @Override public void handleTablesStart() { + if (options.is(hideTables)) { + return; + } + formattingHelper.writeHeader(DocumentHeaderType.subTitle, "Tables"); }