001: /*
002: * This is free software, licensed under the Gnu Public License (GPL)
003: * get a copy from <http://www.gnu.org/licenses/gpl.html>
004: */
005: package henplus.plugins.tablediff;
006:
007: import henplus.HenPlus;
008: import henplus.Command;
009: import henplus.sqlmodel.ColumnFkInfo;
010: import henplus.sqlmodel.ColumnPkInfo;
011: import henplus.util.ListMap;
012: import henplus.view.Column;
013: import henplus.view.ColumnMetaData;
014: import henplus.view.ExtendedColumn;
015: import henplus.view.ExtendedTableRenderer;
016:
017: import java.util.ArrayList;
018: import java.util.Iterator;
019: import java.util.List;
020: import java.util.SortedSet;
021:
022: /**
023: * <p>Title: ResultTablePrinter</p>
024: * <p>Description:<br>
025: * Created on: 24.07.2003</p>
026: * @version $Id: ResultTablePrinter.java,v 1.6 2005/06/18 04:58:13 hzeller Exp $
027: * @author <a href="mailto:martin.grotzke@javakaffee.de">Martin Grotzke</a>
028: */
029: public final class ResultTablePrinter {
030:
031: private final static ColumnMetaData[] DESC_META;
032: static {
033: DESC_META = new ColumnMetaData[8];
034: DESC_META[0] = new ColumnMetaData("status",
035: ColumnMetaData.ALIGN_CENTER);
036: DESC_META[1] = new ColumnMetaData("#",
037: ColumnMetaData.ALIGN_RIGHT);
038: DESC_META[2] = new ColumnMetaData("column");
039: DESC_META[3] = new ColumnMetaData("type");
040: DESC_META[4] = new ColumnMetaData("null");
041: DESC_META[5] = new ColumnMetaData("default");
042: DESC_META[6] = new ColumnMetaData("pk");
043: DESC_META[7] = new ColumnMetaData("fk");
044: }
045:
046: private static final String STAT_MODIFIED_ORG = "M-";
047: private static final String STAT_MODIFIED_NEW = "M+";
048: private static final String STAT_REMOVED = "-";
049: private static final String STAT_ADDED = "+";
050: private static final String YES = "YES";
051: private static final String NO = "NO";
052:
053: public static int printResult(TableDiffResult result) {
054: /*
055: * if all columns belong to the same table name, then don't
056: * report it. A different table name may only occur in rare
057: * circumstance like object oriented databases.
058: */
059: //boolean allSameTableName = true;
060: /*
061: * build up actual describe table.
062: */
063: final List rows = new ArrayList();
064: if (result != null) {
065: // first, print removed columns
066: SortedSet removed = result.getRemovedColumns();
067: if (removed != null) {
068: // ExtendedColumn header = new ExtendedColumn("Removed Columns", 8, ExtendedColumn.ALIGN_CENTER);
069: // rows.add(header);
070: appendLines(STAT_REMOVED, rows, removed);
071: }
072: // then, print added columns
073: SortedSet added = result.getAddedColumns();
074: if (added != null) {
075: appendLines(STAT_ADDED, rows, added);
076: }
077: // at last, print modified columns
078: ListMap modified = result.getModifiedColumns();
079: if (modified != null) {
080: appendModified(rows, modified);
081: }
082: }
083:
084: /*
085: * we render the table now, since we only know know, whether we
086: * will show the first column or not.
087: */
088: ExtendedTableRenderer table = new ExtendedTableRenderer(
089: DESC_META, HenPlus.out());
090: Iterator it = rows.iterator();
091: while (it.hasNext()) {
092: table.addRow((Column[]) it.next());
093: }
094: table.closeTable();
095: return Command.SUCCESS;
096: }
097:
098: private static void appendLines(String symbol, List rows,
099: SortedSet rowSet) {
100: final Iterator iter = rowSet.iterator();
101: while (iter.hasNext()) {
102: henplus.sqlmodel.Column col = (henplus.sqlmodel.Column) iter
103: .next();
104:
105: Column[] row = new Column[8];
106: row[0] = new Column(symbol);
107: row[1] = new Column(col.getPosition());
108: row[2] = new Column(col.getName());
109: String type = extractType(col);
110: row[3] = new Column(type);
111: row[4] = new Column(col.isNullable() ? YES : NO);
112:
113: String defaultVal = col.getDefault();
114: // oracle appends newline to default values for some reason.
115: row[5] = new Column(((defaultVal != null) ? defaultVal
116: .trim() : null));
117:
118: // String pkdesc = (String)pks.get(colname);
119: row[6] = new Column(getPkDesc(col));
120: // String fkdesc = (String)fks.get(colname);
121: row[7] = new Column(getFkDesc(col));
122: rows.add(row);
123: }
124: }
125:
126: private static String extractType(henplus.sqlmodel.Column col) {
127: String type = col.getType();
128: int colSize = col.getSize();
129: if (colSize > 0) {
130: StringBuffer sb = new StringBuffer(type);
131: sb.append("(").append(colSize).append(")");
132: type = sb.toString();
133: }
134: return type;
135: }
136:
137: private static void appendModified(List rows, ListMap modified) {
138: final Iterator iter = modified.keysListIterator();
139: while (iter.hasNext()) {
140: henplus.sqlmodel.Column org = (henplus.sqlmodel.Column) iter
141: .next();
142: henplus.sqlmodel.Column mod = (henplus.sqlmodel.Column) modified
143: .get(org);
144:
145: ExtendedColumn[] orgView = new ExtendedColumn[8];
146: ExtendedColumn[] modView = new ExtendedColumn[8];
147:
148: orgView[0] = new ExtendedColumn(STAT_MODIFIED_ORG,
149: DESC_META[0].getAlignment());
150: modView[0] = new ExtendedColumn(STAT_MODIFIED_NEW,
151: DESC_META[0].getAlignment());
152:
153: // if this was modified it doesn't matter
154: orgView[1] = new ExtendedColumn(org.getPosition(),
155: DESC_META[1].getAlignment());
156: modView[1] = new ExtendedColumn(mod.getPosition(),
157: DESC_META[1].getAlignment());
158:
159: // this should not differ
160: orgView[2] = new ExtendedColumn(org.getName(), DESC_META[2]
161: .getAlignment());
162: modView[2] = new ExtendedColumn(mod.getName(), DESC_META[2]
163: .getAlignment());
164:
165: String orgType = extractType(org);
166: String modType = extractType(mod);
167: orgView[3] = new ExtendedColumn(orgType, DESC_META[3]
168: .getAlignment());
169: modView[3] = new ExtendedColumn(modType, DESC_META[3]
170: .getAlignment());
171: if (!modType.equals(orgType))
172: markAsChanged(modView[3]);
173:
174: orgView[4] = new ExtendedColumn(
175: org.isNullable() ? YES : NO, DESC_META[4]
176: .getAlignment());
177: modView[4] = new ExtendedColumn(
178: mod.isNullable() ? YES : NO, DESC_META[4]
179: .getAlignment());
180: if (org.isNullable() != mod.isNullable())
181: markAsChanged(modView[4]);
182:
183: // System.out.println("default: " + org.getDefault());
184: String orgDefaultVal = (org.getDefault() != null) ? org
185: .getDefault().trim() : null;
186: // oracle appends newline to default values for some reason.
187: orgView[5] = new ExtendedColumn(orgDefaultVal, DESC_META[5]
188: .getAlignment());
189:
190: String modDefaultVal = (mod.getDefault() != null) ? mod
191: .getDefault().trim() : null;
192: modView[5] = new ExtendedColumn(modDefaultVal, DESC_META[5]
193: .getAlignment());
194: if (orgDefaultVal != null
195: && !orgDefaultVal.equals(modDefaultVal)
196: || orgDefaultVal == null && modDefaultVal != null) {
197: markAsChanged(modView[5]);
198: }
199:
200: // primary key
201: String pkDescOrg = getPkDesc(org);
202: String pkDescMod = getPkDesc(mod);
203: orgView[6] = new ExtendedColumn(pkDescOrg, DESC_META[6]
204: .getAlignment());
205: modView[6] = new ExtendedColumn(pkDescMod, DESC_META[6]
206: .getAlignment());
207: // check if one of the cols has to be marked as changed
208: if (org.isPartOfPk() && !mod.isPartOfPk()) {
209: markAsChanged(orgView[6]);
210: } else if (!org.isPartOfPk() && mod.isPartOfPk()) {
211: markAsChanged(modView[6]);
212: } else if (org.isPartOfPk() && mod.isPartOfPk()) {
213: // compare values of pk names
214: if (org.getPkInfo().getPkName() != null
215: && !org.getPkInfo().getPkName().equals(
216: mod.getPkInfo().getPkName())) {
217: markAsChanged(modView[6]);
218: }
219: }
220:
221: // foreign key
222: String fkDescOrg = getFkDesc(org);
223: String fkDescMod = getFkDesc(mod);
224: orgView[7] = new ExtendedColumn(fkDescOrg, DESC_META[7]
225: .getAlignment());
226: modView[7] = new ExtendedColumn(fkDescMod, DESC_META[7]
227: .getAlignment());
228: // check if one of the cols has to be marked as changed
229: if (org.isForeignKey() && !mod.isForeignKey()) {
230: markAsChanged(orgView[7]);
231: } else if (!org.isForeignKey() && mod.isForeignKey()) {
232: markAsChanged(modView[7]);
233: } else if (org.isForeignKey() && mod.isForeignKey()) {
234: // compare values of pk names
235: if (!org.getFkInfo().equals(mod.getFkInfo())) {
236: markAsChanged(modView[7]);
237: }
238: }
239:
240: rows.add(orgView);
241: rows.add(modView);
242: }
243: }
244:
245: private static void markAsChanged(ExtendedColumn col) {
246: col.setBoldRequested(true);
247: }
248:
249: private static String getPkDesc(henplus.sqlmodel.Column col) {
250: String pkDesc = "";
251:
252: if (col.isPartOfPk()) {
253: ColumnPkInfo pkInfo = col.getPkInfo();
254: if (pkInfo.getColumnIndex() == 1) {
255: pkDesc = (pkInfo.getPkName() != null) ? pkInfo
256: .getPkName() : "*";
257: } else { // the pk index is greater than 1
258: pkDesc = (pkInfo.getPkName() != null) ? pkInfo
259: .getPkName() : "*";
260: pkDesc = new StringBuffer(pkDesc).append("{").append(
261: pkInfo.getColumnIndex()).append("}").toString();
262: }
263: }
264:
265: return pkDesc;
266: }
267:
268: private static String getFkDesc(henplus.sqlmodel.Column col) {
269: String fkDesc = "";
270:
271: ColumnFkInfo fkInfo = col.getFkInfo();
272: if (fkInfo != null) {
273: StringBuffer sb = new StringBuffer();
274: if (fkInfo.getFkName() != null) {
275: sb.append(fkInfo.getFkName()).append("\n -> ");
276: } else {
277: sb.append(" -> ");
278: }
279: sb.append(fkInfo.getPkTable()).append("(").append(
280: fkInfo.getPkColumn()).append(")");
281: fkDesc = sb.toString();
282: }
283:
284: return fkDesc;
285: }
286:
287: }
|