001: package net.sourceforge.squirrel_sql.plugins.dbdiff;
002:
003: /*
004: * Copyright (C) 2007 Rob Manning
005: * manningr@users.sourceforge.net
006: *
007: * This library is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation; either
010: * version 2.1 of the License, or (at your option) any later version.
011: *
012: * This library is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this library; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020: */
021: import net.sourceforge.squirrel_sql.fw.sql.TableColumnInfo;
022:
023: /**
024: * This class is responsible for identifying and storing differences between
025: * columns in a table.
026: *
027: * @author manningr
028: */
029: public class ColumnDifference {
030:
031: private String tableName = "";
032: private String columnName = "";
033:
034: private int col1Type = -1;
035: private int col2Type = -1;
036:
037: private int col1Length = 0;
038: private int col2Length = 0;
039:
040: private boolean col1IsNullable = false;
041: private boolean col2IsNullable = false;
042:
043: private boolean col1Exists = true;
044: private boolean col2Exists = true;
045:
046: private String col1remarks = "";
047: private String col2remarks = "";
048:
049: public ColumnDifference() {
050: }
051:
052: public void setColumns(TableColumnInfo c1, TableColumnInfo c2) {
053: if (c1 == null || c2 == null) {
054: throw new IllegalArgumentException("c1, c2 cannot be null");
055: }
056: if (!c1.getTableName().equals(c2.getTableName())) {
057: throw new IllegalArgumentException(
058: "Columns to be compared must be from the same table");
059: }
060: if (!c1.getColumnName().equals(c2.getColumnName())) {
061: throw new IllegalArgumentException(
062: "Columns to be compared must have the same column name");
063: }
064: setColumn1(c1);
065: setColumn2(c2);
066: }
067:
068: public void setColumn1(TableColumnInfo c1) {
069: col1Type = c1.getDataType();
070: col1Length = c1.getColumnSize();
071: col1IsNullable = c1.isNullable().equalsIgnoreCase("NO") ? false
072: : true;
073: tableName = c1.getTableName();
074: columnName = c1.getColumnName();
075: col1remarks = c1.getRemarks();
076: }
077:
078: public void setColumn2(TableColumnInfo c2) {
079: col2Type = c2.getDataType();
080: col2Length = c2.getColumnSize();
081: col2IsNullable = c2.isNullable().equalsIgnoreCase("NO") ? false
082: : true;
083: tableName = c2.getTableName();
084: columnName = c2.getColumnName();
085: col2remarks = c2.getRemarks();
086: }
087:
088: public int getCol1Type() {
089: return col1Type;
090: }
091:
092: public int getCol1Length() {
093: return col1Length;
094: }
095:
096: public boolean col1AllowsNull() {
097: return col1IsNullable;
098: }
099:
100: public int getCol2Type() {
101: return col2Type;
102: }
103:
104: public int getCol2Length() {
105: return col2Length;
106: }
107:
108: public boolean col2AllowsNull() {
109: return col2IsNullable;
110: }
111:
112: public String getCol1Remarks() {
113: return col1remarks;
114: }
115:
116: public String getCol2Remarks() {
117: return col2remarks;
118: }
119:
120: /**
121: * Returns a boolean indicating whether or not the two columns are different
122: * in any aspect.
123: *
124: * @return
125: */
126: public boolean execute() {
127: if (!col1Exists || !col2Exists) {
128: return true;
129: }
130: if (col1Type != col2Type) {
131: return true;
132: }
133: if (col1Length != col2Length) {
134: return true;
135: }
136: if (col1IsNullable != col2IsNullable) {
137: return true;
138: }
139: if (!remarksEqual()) {
140: return true;
141: }
142: return false;
143: }
144:
145: /**
146: * @param tableName the tableName to set
147: */
148: public void setTableName(String tableName) {
149: this .tableName = tableName;
150: }
151:
152: /**
153: * @return the tableName
154: */
155: public String getTableName() {
156: return tableName;
157: }
158:
159: /**
160: * @param columnName the columnName to set
161: */
162: public void setColumnName(String columnName) {
163: this .columnName = columnName;
164: }
165:
166: /**
167: * @return the columnName
168: */
169: public String getColumnName() {
170: return columnName;
171: }
172:
173: /**
174: * @param col1Exists the col1Exists to set
175: */
176: public void setCol1Exists(boolean col1Exists) {
177: this .col1Exists = col1Exists;
178: }
179:
180: /**
181: * @return the col1Exists
182: */
183: public boolean isCol1Exists() {
184: return col1Exists;
185: }
186:
187: /**
188: * @param col2Exists the col2Exists to set
189: */
190: public void setCol2Exists(boolean col2Exists) {
191: this .col2Exists = col2Exists;
192: }
193:
194: /**
195: * @return the col2Exists
196: */
197: public boolean isCol2Exists() {
198: return col2Exists;
199: }
200:
201: public boolean remarksEqual() {
202: if (col1remarks == null && col2remarks == null) {
203: return true;
204: }
205: if ((col1remarks == null && col2remarks != null)
206: || (col1remarks != null && col2remarks == null)) {
207: return false;
208: }
209: return col1remarks.equals(col2remarks);
210: }
211:
212: public boolean typesEqual() {
213: return col1Type == col2Type;
214: }
215:
216: public boolean lengthsEqual() {
217: return col1Length == col2Length;
218: }
219:
220: public boolean nullableEqual() {
221: return col1IsNullable == col2IsNullable;
222: }
223:
224: public String toString() {
225: StringBuilder result = new StringBuilder();
226: result.append("TABLE: ");
227: result.append(tableName);
228: result.append(" COLUMN: ");
229: result.append(columnName);
230: result.append("\n");
231: result.append("source colType: ");
232: result.append(col1Type);
233: result.append("\n");
234:
235: result.append("dest colType: ");
236: result.append(col2Type);
237: result.append("\n");
238:
239: result.append("source colLength: ");
240: result.append(col1Length);
241: result.append("\n");
242:
243: result.append("dest colLength: ");
244: result.append(col2Length);
245: result.append("\n");
246:
247: result.append("source IsNullable: ");
248: result.append(col1IsNullable);
249: result.append("\n");
250:
251: result.append("dest IsNullable: ");
252: result.append(col2IsNullable);
253: result.append("\n");
254:
255: return result.toString();
256: }
257: }
|