-
Notifications
You must be signed in to change notification settings - Fork 306
/
HtmlTablePage.java
228 lines (191 loc) · 9.25 KB
/
HtmlTablePage.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/*
* This file is a part of the SchemaSpy project (http://schemaspy.org).
* 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
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* SchemaSpy is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
package org.schemaspy.view;
import org.schemaspy.Config;
import org.schemaspy.model.*;
import org.schemaspy.util.DiagramUtil;
import org.schemaspy.util.Dot;
import org.schemaspy.util.LineWriter;
import org.schemaspy.util.Markdown;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.*;
/**
* The page that contains the details of a specific table or view
*
* @author John Currier
*/
public class HtmlTablePage extends HtmlFormatter {
private static final HtmlTablePage instance = new HtmlTablePage();
/**
* Singleton: Don't allow instantiation
*/
private HtmlTablePage() {
}
/**
* Singleton accessor
*
* @return the singleton instance
*/
public static HtmlTablePage getInstance() {
return instance;
}
public WriteStats write(Database db, Table table, File outputDir, WriteStats stats) throws IOException {
writeMainTable(db, table, outputDir, stats);
return stats;
}
public void writeMainTable(Database db, Table table, File outputDir, WriteStats stats) throws IOException {
Set<TableColumn> primaries = new HashSet<>(table.getPrimaryColumns());
Set<TableColumn> indexes = new HashSet<>();
Set<MustacheTableColumn> tableColumns = new LinkedHashSet<>();
Set<MustacheTableIndex> indexedColumns = new LinkedHashSet<>();
Set<TableIndex> sortIndexes = new TreeSet<>(table.getIndexes()); // sort primary keys first
for (TableIndex index : sortIndexes) {
indexes.addAll(index.getColumns());
indexedColumns.add(new MustacheTableIndex(index));
}
for (TableColumn column : table.getColumns()) {
tableColumns.add(new MustacheTableColumn(column, indexes, getPathToRoot()));
}
HashMap<String, Object> scopes = new HashMap<>();
scopes.put("table", table);
scopes.put("comments", Markdown.toHtml(table.getComments(), getPathToRoot()));
scopes.put("primaries", primaries);
scopes.put("columns", tableColumns);
scopes.put("indexes", indexedColumns);
List<MustacheTableDiagram> diagrams = new ArrayList<>();
Object graphvizExists = generateDiagrams(table, stats, outputDir, diagrams);
String graphvizVersion = Dot.getInstance().getSupportedVersions().substring(4);
scopes.put("graphvizExists", graphvizExists);
scopes.put("graphvizVersion", graphvizVersion);
scopes.put("diagrams", diagrams);
scopes.put("sqlCode", sqlCode(table));
scopes.put("references", sqlReferences(table, db));
scopes.put("diagramExists", DiagramUtil.diagramExists(diagrams));
scopes.put("indexExists", indexExists(table, indexedColumns));
scopes.put("definitionExists", definitionExists(table));
System.out.println("Table -> "+table.getName());
MustacheWriter mw = new MustacheWriter(outputDir, scopes, getPathToRoot(), db.getName(), false);
mw.write("tables/table.html", Markdown.pagePath(table.getName()), "table.js");
}
private Set<Table> sqlReferences(Table table, Database db) {
Set<Table> references = null;
if (table.isView() && table.getViewDefinition() != null) {
DefaultSqlFormatter formatter = new DefaultSqlFormatter();
references = formatter.getReferencedTables(table.getViewDefinition(), db);
}
return references;
}
private String sqlCode(Table table) {
return table.getViewDefinition() != null ? table.getViewDefinition().trim() : "";
}
private Object indexExists(Table table, Set<MustacheTableIndex> indexedColumns) {
Object exists = null;
if (!table.isView() && !indexedColumns.isEmpty()) {
exists = new Object();
}
return exists;
}
private Object definitionExists(Table table) {
Object exists = null;
if (table.isView() && table.getViewDefinition() != null) {
exists = new Object();
}
return exists;
}
/**
* Generate the .dot file(s) to represent the specified table's relationships.
* <p>
* Generates a <TABLENAME>.dot if the table has real relatives.
* <p>
* Also generates a <TABLENAME>.implied2degrees.dot if the table has implied relatives within
* two degrees of separation.
*
* @param table Table
* @param diagramDir File
* @return boolean <code>true</code> if the table has implied relatives within two
* degrees of separation.
* @throws IOException
*/
private boolean generateDots(Table table, File diagramDir, WriteStats stats, File outputDir) throws IOException {
Dot dot = Dot.getInstance();
String extension = dot == null ? Config.getInstance().getImageFormat() : dot.getFormat();
File oneDegreeDotFile = new File(diagramDir, table.getName() + ".1degree.dot");
File oneDegreeDiagramFile = new File(diagramDir, table.getName() + ".1degree." + extension);
File twoDegreesDotFile = new File(diagramDir, table.getName() + ".2degrees.dot");
File twoDegreesDiagramFile = new File(diagramDir, table.getName() + ".2degrees." + extension);
File oneImpliedDotFile = new File(diagramDir, table.getName() + ".implied1degrees.dot");
File oneImpliedDiagramFile = new File(diagramDir, table.getName() + ".implied1degrees." + extension);
File twoImpliedDotFile = new File(diagramDir, table.getName() + ".implied2degrees.dot");
File twoImpliedDiagramFile = new File(diagramDir, table.getName() + ".implied2degrees." + extension);
// delete before we start because we'll use the existence of these files to determine
// if they should be turned into pngs & presented
Files.deleteIfExists(oneDegreeDotFile.toPath());
Files.deleteIfExists(oneDegreeDiagramFile.toPath());
Files.deleteIfExists(twoDegreesDotFile.toPath());
Files.deleteIfExists(twoDegreesDiagramFile.toPath());
Files.deleteIfExists(oneImpliedDotFile.toPath());
Files.deleteIfExists(oneImpliedDiagramFile.toPath());
Files.deleteIfExists(twoImpliedDotFile.toPath());
Files.deleteIfExists(twoImpliedDiagramFile.toPath());
if (table.getMaxChildren() + table.getMaxParents() > 0) {
Set<ForeignKeyConstraint> impliedConstraints;
DotFormatter formatter = DotFormatter.getInstance();
LineWriter dotOut = new LineWriter(oneDegreeDotFile, Config.DOT_CHARSET);
WriteStats oneStats = new WriteStats(stats);
formatter.writeRealRelationships(table, false, oneStats, dotOut, outputDir);
dotOut.close();
dotOut = new LineWriter(twoDegreesDotFile, Config.DOT_CHARSET);
WriteStats twoStats = new WriteStats(stats);
impliedConstraints = formatter.writeRealRelationships(table, true, twoStats, dotOut, outputDir);
dotOut.close();
if (oneStats.getNumTablesWritten() + oneStats.getNumViewsWritten() == twoStats.getNumTablesWritten() + twoStats.getNumViewsWritten()) {
Files.deleteIfExists(twoDegreesDotFile.toPath()); // no different than before, so don't show it
}
if (!impliedConstraints.isEmpty()) {
dotOut = new LineWriter(oneImpliedDotFile, Config.DOT_CHARSET);
formatter.writeAllRelationships(table, false, stats, dotOut, outputDir);
dotOut.close();
dotOut = new LineWriter(twoImpliedDotFile, Config.DOT_CHARSET);
formatter.writeAllRelationships(table, true, stats, dotOut, outputDir);
dotOut.close();
return true;
}
}
return false;
}
private Object generateDiagrams(Table table, WriteStats stats, File outputDir, List<MustacheTableDiagram> diagrams) throws IOException {
Object graphviz = new Object();
File diagramsDir = new File(outputDir, "diagrams");
generateDots(table, diagramsDir, stats, outputDir);
if (table.getMaxChildren() + table.getMaxParents() > 0) {
if (HtmlTableDiagrammer.getInstance().write(table, diagramsDir, diagrams)) {
//writeExcludedColumns(stats.getExcludedColumns(), table, html);
} else {
graphviz = null;
}
}
return graphviz;
}
@Override
protected String getPathToRoot() {
return "../";
}
}