From b1521aa3d309e6bec966705bfd05b29f3dc4710f Mon Sep 17 00:00:00 2001 From: johncurrier Date: Fri, 27 Jun 2014 20:47:09 +0000 Subject: [PATCH] It turns out that the "anomaly" of nullable primary keys that must be unique is actually in the SQL standard and isn't an anomaly at all. --- .../net/sourceforge/schemaspy/DbAnalyzer.java | 31 +++++-------------- .../schemaspy/model/TableIndex.java | 26 ++-------------- .../schemaspy/view/HtmlAnomaliesPage.java | 11 ++----- .../schemaspy/view/HtmlTablePage.java | 21 +------------ 4 files changed, 14 insertions(+), 75 deletions(-) diff --git a/src/main/java/net/sourceforge/schemaspy/DbAnalyzer.java b/src/main/java/net/sourceforge/schemaspy/DbAnalyzer.java index 5917592..8268faf 100644 --- a/src/main/java/net/sourceforge/schemaspy/DbAnalyzer.java +++ b/src/main/java/net/sourceforge/schemaspy/DbAnalyzer.java @@ -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 @@ -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 getImpliedConstraints(Collection tables) { List columnsWithoutParents = new ArrayList(); Map keyedTablesByPrimary = new TreeMap(new Comparator() { - 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) @@ -190,24 +191,6 @@ public static List
getOrphans(Collection
tables) { return sortTablesByName(orphans); } - /** - * Return a list of TableColumns that are both nullable - * and have an index that specifies that they must be unique (a rather strange combo). - */ - public static List getMustBeUniqueNullableColumns(Collection
tables) { - List uniqueNullables = new ArrayList(); - - for (Table table : tables) { - for (TableIndex index : table.getIndexes()) { - if (index.isUniqueNullable()) { - uniqueNullables.addAll(index.getColumns()); - } - } - } - - return sortColumnsByTable(uniqueNullables); - } - /** * Return a list of Tables that have neither an index nor a primary key. */ @@ -279,7 +262,8 @@ public static List
getTablesWithOneColumn(Collection
tables) { public static List
sortTablesByName(List
tables) { Collections.sort(tables, new Comparator
() { - public int compare(Table table1, Table table2) { + @Override + public int compare(Table table1, Table table2) { return table1.compareTo(table2); } }); @@ -289,7 +273,8 @@ public int compare(Table table1, Table table2) { public static List sortColumnsByTable(List columns) { Collections.sort(columns, new Comparator() { - 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()); diff --git a/src/main/java/net/sourceforge/schemaspy/model/TableIndex.java b/src/main/java/net/sourceforge/schemaspy/model/TableIndex.java index 2a9afe8..28524fb 100755 --- a/src/main/java/net/sourceforge/schemaspy/model/TableIndex.java +++ b/src/main/java/net/sourceforge/schemaspy/model/TableIndex.java @@ -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 @@ -110,27 +110,6 @@ public List 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 @@ -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()) diff --git a/src/main/java/net/sourceforge/schemaspy/view/HtmlAnomaliesPage.java b/src/main/java/net/sourceforge/schemaspy/view/HtmlAnomaliesPage.java index 7faac2c..ee4a56d 100755 --- a/src/main/java/net/sourceforge/schemaspy/view/HtmlAnomaliesPage.java +++ b/src/main/java/net/sourceforge/schemaspy/view/HtmlAnomaliesPage.java @@ -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 @@ -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; @@ -58,7 +59,6 @@ public void write(Database database, Collection
tables, List(tables)), out); - writeUniqueNullables(DbAnalyzer.getMustBeUniqueNullableColumns(new HashSet
(tables)), out); writeTablesWithOneColumn(DbAnalyzer.getTablesWithOneColumn(tables), out); writeTablesWithIncrementingColumnNames(DbAnalyzer.getTablesWithIncrementingColumnNames(tables), out); writeDefaultNullStrings(DbAnalyzer.getDefaultNullStringColumns(new HashSet
(tables)), out); @@ -135,13 +135,6 @@ private void writeImpliedConstraints(List implie out.writeln("

"); } - private void writeUniqueNullables(List uniqueNullables, LineWriter out) throws IOException { - out.writeln("

  • "); - out.writeln("Columns that are flagged as both 'nullable' and 'must be unique':"); - writeColumnBasedAnomaly(uniqueNullables, out); - out.writeln("

  • "); - } - private void writeTablesWithoutIndexes(List
    unindexedTables, LineWriter out) throws IOException { out.writeln("
  • "); out.writeln("Tables without indexes:"); diff --git a/src/main/java/net/sourceforge/schemaspy/view/HtmlTablePage.java b/src/main/java/net/sourceforge/schemaspy/view/HtmlTablePage.java index ddfc164..c87747a 100755 --- a/src/main/java/net/sourceforge/schemaspy/view/HtmlTablePage.java +++ b/src/main/java/net/sourceforge/schemaspy/view/HtmlTablePage.java @@ -341,17 +341,9 @@ private void writeIndexes(Table table, LineWriter out) throws IOException { boolean showId = table.getId() != null; Set 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("
    "); out.writeln("Indexes:"); - out.writeln("
  • " + (showId ? "" : "") + (containsAnomalies ? "" : "")); + out.writeln("
    " + (showId ? "" : "")); out.writeln(""); out.writeln(" "); if (showId) @@ -360,8 +352,6 @@ private void writeIndexes(Table table, LineWriter out) throws IOException { out.writeln(" "); out.writeln(" "); out.writeln(" "); - if (containsAnomalies) - out.writeln(" "); out.writeln(" "); out.writeln(""); out.writeln(""); @@ -407,15 +397,6 @@ private void writeIndexes(Table table, LineWriter out) throws IOException { out.write(" "); - - if (index.isUniqueNullable()) { - if (index.getColumns().size() == 1) - out.writeln(" "); - else - out.writeln(" "); - } else if (containsAnomalies) { - out.writeln(" "); - } out.writeln(" "); } out.writeln("");
    TypeSortConstraint NameAnomalies
    "); out.write(index.getName()); out.writeln("This unique column is also nullableThese unique columns are also nullable