Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor formatters to make methods more readable #1762

Merged
merged 1 commit into from
Nov 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,10 @@ public void handleTablesStart() {
// No output required
}

private String arrowhead(final ForeignKeyCardinality connectivity) {
private String arrowheadFk(final ForeignKeyCardinality connectivity) {
if (!options.isShowForeignKeyCardinality()) {
return "none";
}
switch (connectivity) {
case zero_one:
return "teeodot";
Expand All @@ -262,6 +265,26 @@ private String arrowhead(final ForeignKeyCardinality connectivity) {
}
}

private String arrowheadPk() {
final String pkSymbol;
if (options.isShowPrimaryKeyCardinality()) {
pkSymbol = "teetee";
} else {
pkSymbol = "none";
}
return pkSymbol;
}

private String columnReferenceLineStyle(final boolean isForeignKey) {
final String style;
if (isForeignKey) {
style = "solid";
} else {
style = "dashed";
}
return style;
}

private String[] getPortIds(final Column column, final boolean isNewNode) {
final String[] portIds = new String[2];

Expand Down Expand Up @@ -363,27 +386,9 @@ private String printColumnReference(
final String[] fkPortIds =
getPortIds(foreignKeyColumn, isFkColumnFiltered || !isFkColumnSignificant);

final DiagramOptions diagramOptions = options;
final String pkSymbol;
if (diagramOptions.isShowPrimaryKeyCardinality()) {
pkSymbol = "teetee";
} else {
pkSymbol = "none";
}

final String fkSymbol;
if (diagramOptions.isShowForeignKeyCardinality()) {
fkSymbol = arrowhead(fkCardinality);
} else {
fkSymbol = "none";
}

final String style;
if (isForeignKey) {
style = "solid";
} else {
style = "dashed";
}
final String pkSymbol = arrowheadPk();
final String fkSymbol = arrowheadFk(fkCardinality);
final String lineStyle = columnReferenceLineStyle(isForeignKey);

final String associationName;
if (isForeignKey && options.is(hideForeignKeyNames)
Expand All @@ -403,7 +408,7 @@ private String printColumnReference(

return String.format(
" %s:w -> %s:e [label=<%s> style=\"%s\" dir=\"both\" arrowhead=\"%s\" arrowtail=\"%s\"];%n",
fkPortIds[0], pkPortIds[1], label, style, pkSymbol, fkSymbol);
fkPortIds[0], pkPortIds[1], label, lineStyle, pkSymbol, fkSymbol);
}

private void printForeignKeys(final Table table) {
Expand Down
13 changes: 13 additions & 0 deletions schemacrawler-docker-compose/mcsbart.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
services:

sqlserver:
image: mcsbart_1
container_name: sqlserver
ports:
- target: 1433
published: 1433
protocol: tcp
mode: host
environment:
ACCEPT_EULA: "Y"
SA_PASSWORD: Schem#Crawl3r
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,54 @@ public void handleTablesStart() {
formattingHelper.writeHeader(DocumentHeaderType.subTitle, "Tables");
}

private List<TableConstraint> filterPrintableConstraints(
final Collection<TableConstraint> constraintsCollection) {
final EnumSet<TableConstraintType> printableConstraints =
EnumSet.of(TableConstraintType.check, TableConstraintType.unique);

final List<TableConstraint> constraints = new ArrayList<>();
for (final TableConstraint constraint : constraintsCollection) {
// 1. There is no point in showing a constraint if there is no information
// about the constrained columns, and the name is hidden
final List<TableConstraintColumn> constrainedColumns = constraint.getConstrainedColumns();
final boolean hasNoNameOrColumns =
options.is(hideTableConstraintNames)
&& constrainedColumns.isEmpty()
&& !constraint.hasRemarks();
// 2. Print only check constraints and unique constraints
final boolean isNotPkOrFk = printableConstraints.contains(constraint.getType());
// Keep only constraints that should be printed
if (!hasNoNameOrColumns && isNotPkOrFk) {
constraints.add(constraint);
}
}
return constraints;
}

private String makeFkRuleString(final ForeignKey foreignKey) {
String updateRuleString = "";
final ForeignKeyUpdateRule updateRule = foreignKey.getUpdateRule();
if (updateRule != null && updateRule != ForeignKeyUpdateRule.unknown) {
updateRuleString = ", on update " + updateRule.toString();
}

String deleteRuleString = "";
final ForeignKeyUpdateRule deleteRule = foreignKey.getDeleteRule();
if (deleteRule != null && deleteRule != ForeignKeyUpdateRule.unknown) {
deleteRuleString = ", on delete " + deleteRule.toString();
}

final String ruleString;
if (deleteRule != null
&& updateRule == deleteRule
&& updateRule != ForeignKeyUpdateRule.unknown) {
ruleString = ", with " + deleteRule.toString();
} else {
ruleString = updateRuleString + deleteRuleString;
}
return ruleString;
}

private void printAlternateKeys(final Table table) {
if (table == null || options.is(hideAlternateKeys)) {
LOGGER.log(Level.FINER, "Not showing alternate keys");
Expand Down Expand Up @@ -571,27 +619,7 @@ private void printForeignKeys(final Table table) {
for (final ForeignKey foreignKey : foreignKeys) {
if (foreignKey != null) {
final String name = identifiers.quoteName(foreignKey);

String updateRuleString = "";
final ForeignKeyUpdateRule updateRule = foreignKey.getUpdateRule();
if (updateRule != null && updateRule != ForeignKeyUpdateRule.unknown) {
updateRuleString = ", on update " + updateRule.toString();
}

String deleteRuleString = "";
final ForeignKeyUpdateRule deleteRule = foreignKey.getDeleteRule();
if (deleteRule != null && deleteRule != ForeignKeyUpdateRule.unknown) {
deleteRuleString = ", on delete " + deleteRule.toString();
}

final String ruleString;
if (deleteRule != null
&& updateRule == deleteRule
&& updateRule != ForeignKeyUpdateRule.unknown) {
ruleString = ", with " + deleteRule.toString();
} else {
ruleString = updateRuleString + deleteRuleString;
}
final String ruleString = makeFkRuleString(foreignKey);

formattingHelper.writeEmptyRow();

Expand Down Expand Up @@ -872,50 +900,22 @@ private void printTableConstraints(final Collection<TableConstraint> constraints
return;
}

final EnumSet<TableConstraintType> printableConstraints =
EnumSet.of(TableConstraintType.check, TableConstraintType.unique);

final List<TableConstraint> constraints = new ArrayList<>();
for (final TableConstraint constraint : constraintsCollection) {
if (printableConstraints.contains(constraint.getType())) {
constraints.add(constraint);
}
}
final List<TableConstraint> constraints = filterPrintableConstraints(constraintsCollection);
if (constraints.isEmpty()) {
return;
}

Collections.sort(
constraints, NamedObjectSort.getNamedObjectSort(options.isAlphabeticalSortForIndexes()));

// There is no point in showing a constraint if there is no information
// about the constrained columns, and the name is hidden
boolean canDisplayTableConstraints = false;
for (final TableConstraint constraint : constraints) {
if (constraint == null) {
continue;
}
final List<TableConstraintColumn> constrainedColumns = constraint.getConstrainedColumns();
final boolean cannotDisplayTableConstraints =
(options.is(hideTableConstraintNames)
&& constrainedColumns.isEmpty()
&& !constraint.hasRemarks());
if (!cannotDisplayTableConstraints) {
canDisplayTableConstraints = true;
break;
}
}
if (!canDisplayTableConstraints) {
return;
}

formattingHelper.writeEmptyRow();
formattingHelper.writeWideRow("Table Constraints", "section");

for (final TableConstraint constraint : constraints) {
if (constraint == null) {
continue;
}

final String constraintName;
if (!options.is(hideTableConstraintNames)) {
LOGGER.log(
Expand All @@ -925,23 +925,14 @@ private void printTableConstraints(final Collection<TableConstraint> constraints
} else {
constraintName = "";
}

final List<TableConstraintColumn> constrainedColumns = constraint.getConstrainedColumns();
if (options.is(hideTableConstraintNames)
&& constrainedColumns.isEmpty()
&& !constraint.hasRemarks()) {
// There is no point in showing a constraint if there is no information
// about the constrained columns, and the name is hidden
continue;
}

final String constraintType = constraint.getType().getValue().toLowerCase();
final String constraintDetails = "[" + constraintType + " constraint]";
formattingHelper.writeEmptyRow();
formattingHelper.writeNameRow(constraintName, constraintDetails);

printRemarks(constraint);
if (!isBrief()) {
final List<TableConstraintColumn> constrainedColumns = constraint.getConstrainedColumns();
printTableColumns(constrainedColumns, false);
}
printDependantObjectDefinition(constraint);
Expand Down Expand Up @@ -985,7 +976,7 @@ private void printTriggers(final Collection<Trigger> triggers) {
if (timingBuffer.length() > 0) {
timingBuffer.append(SPACE);
}
for (EventManipulationType eventManipulationType : eventManipulationTypes) {
for (final EventManipulationType eventManipulationType : eventManipulationTypes) {
timingBuffer.append(eventManipulationType);
if (eventManipulationTypes.indexOf(eventManipulationType)
< eventManipulationTypes.size() - 1) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import java.io.IOException;
import java.io.Reader;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.SQLWarning;
import java.sql.Statement;
import java.util.ArrayList;
Expand Down Expand Up @@ -102,7 +103,6 @@ public void run() {
final List<String> sqlList = readSql(new BufferedReader(scriptReader));
for (final Iterator<String> iterator = sqlList.iterator(); iterator.hasNext(); ) {
sql = iterator.next();
statement.clearWarnings();
try {
if (Pattern.matches("\\s+", sql)) {
continue;
Expand All @@ -111,15 +111,7 @@ public void run() {
LOGGER.log(Level.INFO, "\n" + sql);
}

final boolean hasResults = statement.execute(sql);
if (hasResults) {
throw new SQLWarning(String.format("Results not expected from SQL%n%s%n", sql));
}

final SQLWarning warnings = statement.getWarnings();
if (warnings != null && !warnings.getMessage().startsWith("Can't drop database")) {
throw warnings;
}
executeSql(sql, statement);

if (!connection.getAutoCommit()) {
connection.commit();
Expand All @@ -144,6 +136,19 @@ public void run() {
}
}

private void executeSql(final String sql, final Statement statement) throws SQLException {
final boolean hasResults = statement.execute(sql);
if (hasResults) {
throw new SQLWarning(String.format("Results not expected from SQL%n%s%n", sql));
}

final SQLWarning warnings = statement.getWarnings();
statement.clearWarnings();
if (warnings != null && !warnings.getMessage().startsWith("Can't drop database")) {
throw warnings;
}
}

private Throwable getCause(final Throwable e) {
Throwable cause;
Throwable result = e;
Expand Down
Loading