Skip to content

Commit

Permalink
It turns out that the "anomaly" of nullable primary keys that must be…
Browse files Browse the repository at this point in the history
… unique is actually in the SQL standard and isn't an anomaly at all.
  • Loading branch information
johncurrier committed Jun 27, 2014
1 parent 81b2020 commit b1521aa
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 75 deletions.
31 changes: 8 additions & 23 deletions src/main/java/net/sourceforge/schemaspy/DbAnalyzer.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* This file is a part of the SchemaSpy project (http://schemaspy.sourceforge.net).
* Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011 John Currier
* Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2014 John Currier
*
* SchemaSpy is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
Expand Down Expand Up @@ -35,19 +35,20 @@
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Pattern;

import net.sourceforge.schemaspy.model.ForeignKeyConstraint;
import net.sourceforge.schemaspy.model.ImpliedForeignKeyConstraint;
import net.sourceforge.schemaspy.model.RailsForeignKeyConstraint;
import net.sourceforge.schemaspy.model.Table;
import net.sourceforge.schemaspy.model.TableColumn;
import net.sourceforge.schemaspy.model.TableIndex;
import net.sourceforge.schemaspy.util.Inflection;

public class DbAnalyzer {
public static List<ImpliedForeignKeyConstraint> getImpliedConstraints(Collection<Table> tables) {
List<TableColumn> columnsWithoutParents = new ArrayList<TableColumn>();
Map<TableColumn, Table> keyedTablesByPrimary = new TreeMap<TableColumn, Table>(new Comparator<TableColumn>() {
public int compare(TableColumn column1, TableColumn column2) {
@Override
public int compare(TableColumn column1, TableColumn column2) {
int rc = column1.getName().compareToIgnoreCase(column2.getName());
if (rc == 0) {
if (column1.getType() != null && column2.getType() != null)
Expand Down Expand Up @@ -190,24 +191,6 @@ public static List<Table> getOrphans(Collection<Table> tables) {
return sortTablesByName(orphans);
}

/**
* Return a list of <code>TableColumn</code>s that are both nullable
* and have an index that specifies that they must be unique (a rather strange combo).
*/
public static List<TableColumn> getMustBeUniqueNullableColumns(Collection<Table> tables) {
List<TableColumn> uniqueNullables = new ArrayList<TableColumn>();

for (Table table : tables) {
for (TableIndex index : table.getIndexes()) {
if (index.isUniqueNullable()) {
uniqueNullables.addAll(index.getColumns());
}
}
}

return sortColumnsByTable(uniqueNullables);
}

/**
* Return a list of <code>Table</code>s that have neither an index nor a primary key.
*/
Expand Down Expand Up @@ -279,7 +262,8 @@ public static List<Table> getTablesWithOneColumn(Collection<Table> tables) {

public static List<Table> sortTablesByName(List<Table> tables) {
Collections.sort(tables, new Comparator<Table>() {
public int compare(Table table1, Table table2) {
@Override
public int compare(Table table1, Table table2) {
return table1.compareTo(table2);
}
});
Expand All @@ -289,7 +273,8 @@ public int compare(Table table1, Table table2) {

public static List<TableColumn> sortColumnsByTable(List<TableColumn> columns) {
Collections.sort(columns, new Comparator<TableColumn>() {
public int compare(TableColumn column1, TableColumn column2) {
@Override
public int compare(TableColumn column1, TableColumn column2) {
int rc = column1.getTable().compareTo(column2.getTable());
if (rc == 0)
rc = column1.getName().compareToIgnoreCase(column2.getName());
Expand Down
26 changes: 3 additions & 23 deletions src/main/java/net/sourceforge/schemaspy/model/TableIndex.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* This file is a part of the SchemaSpy project (http://schemaspy.sourceforge.net).
* Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010 John Currier
* Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2014 John Currier
*
* SchemaSpy is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
Expand Down Expand Up @@ -110,27 +110,6 @@ public List<TableColumn> getColumns() {
return Collections.unmodifiableList(columns);
}

/**
* Yes, we had a project that had columns defined as both 'nullable' and 'must be unique'.
*
* @return boolean
*/
public boolean isUniqueNullable() {
if (!isUnique())
return false;

// if all of the columns specified by the Unique Index are nullable
// then return true, otherwise false
boolean allNullable = true;
for (TableColumn column : getColumns()) {
allNullable = column != null && column.isNullable();
if (!allNullable)
break;
}

return allNullable;
}

/**
* @param column
* @return
Expand All @@ -143,7 +122,8 @@ public boolean isAscending(TableColumn column) {
* @param object
* @return
*/
public int compareTo(TableIndex other) {
@Override
public int compareTo(TableIndex other) {
if (isPrimaryKey() && !other.isPrimaryKey())
return -1;
if (!isPrimaryKey() && other.isPrimaryKey())
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* This file is a part of the SchemaSpy project (http://schemaspy.sourceforge.net).
* Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011 John Currier
* Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2014 John Currier
*
* SchemaSpy is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
Expand All @@ -23,6 +23,7 @@
import java.util.Collection;
import java.util.HashSet;
import java.util.List;

import net.sourceforge.schemaspy.DbAnalyzer;
import net.sourceforge.schemaspy.model.Database;
import net.sourceforge.schemaspy.model.ForeignKeyConstraint;
Expand Down Expand Up @@ -58,7 +59,6 @@ public void write(Database database, Collection<Table> tables, List<? extends Fo
writeHeader(database, out);
writeImpliedConstraints(impliedConstraints, out);
writeTablesWithoutIndexes(DbAnalyzer.getTablesWithoutIndexes(new HashSet<Table>(tables)), out);
writeUniqueNullables(DbAnalyzer.getMustBeUniqueNullableColumns(new HashSet<Table>(tables)), out);
writeTablesWithOneColumn(DbAnalyzer.getTablesWithOneColumn(tables), out);
writeTablesWithIncrementingColumnNames(DbAnalyzer.getTablesWithIncrementingColumnNames(tables), out);
writeDefaultNullStrings(DbAnalyzer.getDefaultNullStringColumns(new HashSet<Table>(tables)), out);
Expand Down Expand Up @@ -135,13 +135,6 @@ private void writeImpliedConstraints(List<? extends ForeignKeyConstraint> implie
out.writeln("<p></li>");
}

private void writeUniqueNullables(List<TableColumn> uniqueNullables, LineWriter out) throws IOException {
out.writeln("<li>");
out.writeln("<b>Columns that are flagged as both 'nullable' and 'must be unique':</b>");
writeColumnBasedAnomaly(uniqueNullables, out);
out.writeln("<p></li>");
}

private void writeTablesWithoutIndexes(List<Table> unindexedTables, LineWriter out) throws IOException {
out.writeln("<li>");
out.writeln("<b>Tables without indexes:</b>");
Expand Down
21 changes: 1 addition & 20 deletions src/main/java/net/sourceforge/schemaspy/view/HtmlTablePage.java
Original file line number Diff line number Diff line change
Expand Up @@ -341,17 +341,9 @@ private void writeIndexes(Table table, LineWriter out) throws IOException {
boolean showId = table.getId() != null;
Set<TableIndex> indexes = table.getIndexes();
if (indexes != null && !indexes.isEmpty()) {
// see if we've got any strangeness so we can have the correct number of colgroups
boolean containsAnomalies = false;
for (TableIndex index : indexes) {
containsAnomalies = index.isUniqueNullable();
if (containsAnomalies)
break;
}

out.writeln("<div class='indent'>");
out.writeln("<b>Indexes:</b>");
out.writeln("<table class='dataTable' border='1' rules='groups'><colgroup><colgroup><colgroup><colgroup>" + (showId ? "<colgroup>" : "") + (containsAnomalies ? "<colgroup>" : ""));
out.writeln("<table class='dataTable' border='1' rules='groups'><colgroup><colgroup><colgroup><colgroup>" + (showId ? "<colgroup>" : ""));
out.writeln("<thead>");
out.writeln(" <tr>");
if (showId)
Expand All @@ -360,8 +352,6 @@ private void writeIndexes(Table table, LineWriter out) throws IOException {
out.writeln(" <th>Type</th>");
out.writeln(" <th>Sort</th>");
out.writeln(" <th class='constraint' style='text-align:left;'>Constraint Name</th>");
if (containsAnomalies)
out.writeln(" <th>Anomalies</th>");
out.writeln(" </tr>");
out.writeln("</thead>");
out.writeln("<tbody>");
Expand Down Expand Up @@ -407,15 +397,6 @@ private void writeIndexes(Table table, LineWriter out) throws IOException {
out.write(" <td class='constraint' style='text-align:left;'>");
out.write(index.getName());
out.writeln("</td>");

if (index.isUniqueNullable()) {
if (index.getColumns().size() == 1)
out.writeln(" <td class='detail'>This unique column is also nullable</td>");
else
out.writeln(" <td class='detail'>These unique columns are also nullable</td>");
} else if (containsAnomalies) {
out.writeln(" <td>&nbsp;</td>");
}
out.writeln(" </tr>");
}
out.writeln("</tbody>");
Expand Down

0 comments on commit b1521aa

Please sign in to comment.