View Javadoc
1   /*
2    * This file is a part of the SchemaSpy project (http://schemaspy.sourceforge.net).
3    * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010 John Currier
4    *
5    * SchemaSpy is free software; you can redistribute it and/or
6    * modify it under the terms of the GNU Lesser General Public
7    * License as published by the Free Software Foundation; either
8    * version 2.1 of the License, or (at your option) any later version.
9    *
10   * SchemaSpy is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13   * Lesser General Public License for more details.
14   *
15   * You should have received a copy of the GNU Lesser General Public
16   * License along with this library; if not, write to the Free Software
17   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
18   */
19  package net.sourceforge.schemaspy.model;
20  
21  import java.sql.ResultSet;
22  import java.sql.SQLException;
23  import java.util.ArrayList;
24  import java.util.Collections;
25  import java.util.List;
26  
27  public class TableIndex implements Comparable<TableIndex> {
28      private final String name;
29      private final boolean isUnique;
30      private Object id;
31      private boolean isPrimary;
32      private final List<TableColumn> columns = new ArrayList<TableColumn>();
33      private final List<Boolean> columnsAscending = new ArrayList<Boolean>(); // for whether colums are ascending order
34  
35      /**
36       * @param rs
37       * @throws java.sql.SQLException
38       */
39      public TableIndex(ResultSet rs) throws SQLException {
40          name = rs.getString("INDEX_NAME");
41          isUnique = !rs.getBoolean("NON_UNIQUE");
42      }
43  
44      public void setId(Object id) {
45          this.id = id;
46      }
47  
48      public Object getId() {
49          return id;
50      }
51  
52      public String getName() {
53          return name;
54      }
55  
56      void addColumn(TableColumn column, String sortOrder) {
57          if (column != null) {
58              columns.add(column);
59              columnsAscending.add(Boolean.valueOf(sortOrder == null || sortOrder.equals("A")));
60          }
61      }
62  
63      /**
64       * @return
65       */
66      public String getType() {
67          if (isPrimaryKey())
68              return "Primary key";
69          if (isUnique())
70              return "Must be unique";
71          return "Performance";
72      }
73  
74      /**
75       * @return
76       */
77      public boolean isPrimaryKey() {
78          return isPrimary;
79      }
80  
81      /**
82       * @param isPrimaryKey
83       */
84      public void setIsPrimaryKey(boolean isPrimaryKey) {
85          isPrimary = isPrimaryKey;
86      }
87  
88      /**
89       * @return
90       */
91      public boolean isUnique() {
92          return isUnique;
93      }
94  
95      /**
96       * @return
97       */
98      public String getColumnsAsString() {
99          StringBuilder buf = new StringBuilder();
100 
101         for (TableColumn column : columns) {
102             if (buf.length() > 0)
103                 buf.append(" + ");
104             buf.append(column);
105         }
106         return buf.toString();
107     }
108 
109     public List<TableColumn> getColumns() {
110         return Collections.unmodifiableList(columns);
111     }
112 
113     /**
114      * Yes, we had a project that had columns defined as both 'nullable' and 'must be unique'.
115      *
116      * @return boolean
117      */
118     public boolean isUniqueNullable() {
119         if (!isUnique())
120             return false;
121 
122         // if all of the columns specified by the Unique Index are nullable
123         // then return true, otherwise false
124         boolean allNullable = true;
125         for (TableColumn column : getColumns()) {
126             allNullable = column != null && column.isNullable();
127             if (!allNullable)
128                 break;
129         }
130 
131         return allNullable;
132     }
133 
134     /**
135      * @param column
136      * @return
137      */
138     public boolean isAscending(TableColumn column) {
139         return columnsAscending.get(columns.indexOf(column)).booleanValue();
140     }
141 
142     /**
143      */
144     public int compareTo(TableIndex other) {
145         if (isPrimaryKey() && !other.isPrimaryKey())
146             return -1;
147         if (!isPrimaryKey() && other.isPrimaryKey())
148             return 1;
149 
150         Object thisId = getId();
151         Object otherId = other.getId();
152         if (thisId == null || otherId == null)
153             return getName().compareToIgnoreCase(other.getName());
154         if (thisId instanceof Number)
155             return ((Number)thisId).intValue() - ((Number)otherId).intValue();
156         return thisId.toString().compareToIgnoreCase(otherId.toString());
157     }
158 }