001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.dbschema;
043:
044: import java.sql.Types;
045:
046: import org.netbeans.modules.dbschema.util.SQLTypeUtil;
047:
048: /** Describes a column in a table.
049: */
050: public class ColumnElement extends DBMemberElement {
051: /** Create a new column element represented in memory.
052: */
053: public ColumnElement() {
054: this (new Memory(), null);
055: }
056:
057: /** Creates a new column element.
058: * @param impl the pluggable implementation
059: * @param declaringTable declaring table of this column, or <code>null</code>
060: */
061: public ColumnElement(Impl impl, TableElement declaringTable) {
062: super (impl, declaringTable);
063: }
064:
065: /** Indicates whether some other object is "equal to" this one.
066: * @param obj the reference object with which to compare.
067: * @return true if this object is the same as the obj argument; false otherwise.
068: */
069: public boolean equals(Object obj) {
070: Integer iThis, iArg;
071:
072: if (!(obj instanceof ColumnElement))
073: return false;
074:
075: ColumnElement ce = (ColumnElement) obj;
076: if (!getName().getFullName().equals(ce.getName().getFullName()))
077: return false;
078:
079: if (getType() != ce.getType())
080: return false;
081:
082: if (isNullable() != ce.isNullable())
083: return false;
084:
085: // handle length
086: iThis = getLength();
087: iArg = ce.getLength();
088: if (iThis != null ^ iArg != null)
089: // return false, if one length is null and the other is not null
090: return false;
091:
092: if (iThis != null && iArg != null && iThis.compareTo(iArg) != 0)
093: // return false, if both lengths are defined but do not compare equal
094: return false;
095:
096: // handle scale
097: iThis = getScale();
098: iArg = ce.getScale();
099: if (iThis != null ^ iArg != null)
100: // return false, if one scale is null and the other is not null
101: return false;
102:
103: if (iThis != null && iArg != null && iThis.compareTo(iArg) != 0)
104: // return false, if both scales are defined but do not compare equal
105: return false;
106:
107: // handle precision
108: iThis = getPrecision();
109: iArg = ce.getPrecision();
110: if (iThis != null ^ iArg != null)
111: // return false, if one precision is null and the other is not null
112: return false;
113:
114: if (iThis != null && iArg != null && iThis.compareTo(iArg) != 0)
115: // return false, if both precisions are defined but do not compare equal
116: return false;
117:
118: return true;
119: }
120:
121: /** Clone the column element.
122: * @return a new element that has the same values as the original but is represented in memory
123: */
124: public Object clone() {
125: return new ColumnElement(new Memory(this ), null);
126: }
127:
128: /** Returns the implementation for the column.
129: * @return implementation for the column
130: */
131: final Impl getColumnImpl() {
132: return (Impl) getElementImpl();
133: }
134:
135: /** Get the value type of the column.
136: * @return the type
137: */
138: public int getType() {
139: return getColumnImpl().getType();
140: }
141:
142: /** Set the value type of the column.
143: * @param type the type
144: * @throws DBException if impossible
145: */
146: public void setType(int type) throws DBException {
147: getColumnImpl().setType(type);
148: }
149:
150: //convenience methods
151:
152: /** Returns whether the data type is numeric.
153: * @return true if tha data type is numeric; false otherwise.
154: */
155: public boolean isNumericType() {
156: return SQLTypeUtil.isNumeric(getType());
157: }
158:
159: /** Returns whether the data type is character.
160: * @return true if tha data type is character; false otherwise.
161: */
162: public boolean isCharacterType() {
163: return SQLTypeUtil.isCharacter(getType());
164: }
165:
166: /** Returns whether the data type is blob type.
167: * @return true if tha data type is blob type; false otherwise.
168: */
169: public boolean isBlobType() {
170: return SQLTypeUtil.isBlob(getType());
171: }
172:
173: //end convenience methods
174:
175: /** Returns whether the column is nullable.
176: * @return a flag representing whether the column is nullable
177: */
178: public boolean isNullable() {
179: return getColumnImpl().isNullable();
180: }
181:
182: /** Set whether the column is nullable.
183: * @param flag flag representing whether the column is nullable
184: * @throws DBException if impossible
185: */
186: public void setNullable(boolean flag) throws DBException {
187: getColumnImpl().setNullable(flag);
188: }
189:
190: /** Get the length of the column - for character type fields only.
191: * @return the length, <code>null</code> if it is not a character type
192: * field or there is no length.
193: */
194: public Integer getLength() {
195: if (isCharacterType() || isBlobType())
196: return getColumnImpl().getLength();
197: else
198: return null;
199: }
200:
201: /** Set the length of the column - for character type fields only.
202: * @param length the length for the column if it a character type
203: * @throws DBException if impossible
204: */
205: public void setLength(Integer length) throws DBException {
206: if (isCharacterType() || isBlobType())
207: getColumnImpl().setLength(length);
208: }
209:
210: /** Get the precision of the column - for numeric type fields only.
211: * @return the precision, <code>null</code> if it is not a numeric type
212: * field or there is no precision.
213: */
214: public Integer getPrecision() {
215: if (isNumericType())
216: return getColumnImpl().getPrecision();
217: else
218: return null;
219: }
220:
221: /** Set the precision of the column - for numeric type fields only.
222: * @param precision the precision for the column if it a numeric type
223: * @throws DBException if impossible
224: */
225: public void setPrecision(Integer precision) throws DBException {
226: if (isNumericType())
227: getColumnImpl().setPrecision(precision);
228: }
229:
230: /** Get the scale of the column - for numeric type fields only.
231: * @return the scale, <code>null</code> if it is not a numeric type
232: * field or there is no scale.
233: */
234: public Integer getScale() {
235: if (isNumericType())
236: return getColumnImpl().getScale();
237: else
238: return null;
239: }
240:
241: /** Set the scale of the column - for numeric type fields only.
242: * @param scale the scale for the column if it a numeric type
243: * @throws DBException if impossible
244: */
245: public void setScale(Integer scale) throws DBException {
246: if (isNumericType())
247: getColumnImpl().setScale(scale);
248: }
249:
250: /** Returns a string representation of the object.
251: * @return a string representation of the object.
252: */
253: public String toString() {
254: return getName().toString();
255: }
256:
257: /** Implementation of a column element.
258: * @see ColumnElement
259: */
260: public interface Impl extends DBMemberElement.Impl {
261: /** Get the value type of the column.
262: * @return the type
263: */
264: public int getType();
265:
266: /** Set the value type of the column.
267: * @param type the type
268: * @throws DBException if impossible
269: */
270: public void setType(int type) throws DBException;
271:
272: /** Returns whether the column is nullable.
273: * @return a flag representing whether the column is nullable
274: */
275: public boolean isNullable();
276:
277: /** Set whether the column is nullable.
278: * @param flag flag representing whether the column is nullable
279: * @throws DBException if impossible
280: */
281: public void setNullable(boolean flag) throws DBException;
282:
283: /** Get the length of the column - for character type fields only.
284: * @return the length, <code>null</code> if it is not a character type
285: * field or there is no length.
286: */
287: public Integer getLength();
288:
289: /** Set the length of the column - for character type fields only.
290: * @param length the length for the column if it a character type
291: * @throws DBException if impossible
292: */
293: public void setLength(Integer length) throws DBException;
294:
295: /** Get the precision of the column - for numeric type fields only.
296: * @return the precision, <code>null</code> if it is not a numeric type
297: * field or there is no precision.
298: */
299: public Integer getPrecision();
300:
301: /** Set the precision of the column - for numeric type fields only.
302: * @param precision the precision for the column if it a numeric type
303: * @throws DBException if impossible
304: */
305: public void setPrecision(Integer precision) throws DBException;
306:
307: /** Get the scale of the column - for numeric type fields only.
308: * @return the scale, <code>null</code> if it is not a numeric type
309: * field or there is no scale.
310: */
311: public Integer getScale();
312:
313: /** Set the scale of the column - for numeric type fields only.
314: * @param scale the scale for the column if it a numeric type
315: * @throws DBException if impossible
316: */
317: public void setScale(Integer scale) throws DBException;
318: }
319:
320: static class Memory extends DBMemberElement.Memory implements Impl {
321: /** Type of column */
322: private int _type;
323:
324: /** Nullability flag */
325: private boolean _nullable;
326:
327: /** Length of column */
328: private Integer _length;
329:
330: /** Precision of column */
331: private Integer _precision;
332:
333: /** Scale of column */
334: private Integer _scale;
335:
336: /** Default constructor.
337: */
338: Memory() {
339: super ();
340: _type = Types.NULL;
341: }
342:
343: /** Copy constructor.
344: * @param column the object from which to read values
345: */
346: Memory(ColumnElement column) {
347: super (column);
348: _type = column.getType();
349: _nullable = column.isNullable();
350: _length = column.getLength();
351: _precision = column.getPrecision();
352: _scale = column.getScale();
353: }
354:
355: /** Type of the column.
356: * @return the type
357: */
358: public int getType() {
359: return _type;
360: }
361:
362: /** Setter for type of the column.
363: * @param type the column type
364: */
365: public void setType(int type) {
366: int old = _type;
367:
368: _type = type;
369: firePropertyChange(PROP_TYPE, new Integer(old),
370: new Integer(type));
371: }
372:
373: /** Returns whether the column is nullable.
374: * @return a flag representing whether the column is nullable
375: */
376: public boolean isNullable() {
377: return _nullable;
378: }
379:
380: /** Set whether the column is nullable.
381: * @param flag flag representing whether the column is nullable
382: * @throws DBException if impossible
383: */
384: public void setNullable(boolean flag) throws DBException {
385: boolean old = _nullable;
386:
387: _nullable = flag;
388: firePropertyChange(PROP_NULLABLE, Boolean.valueOf(old),
389: Boolean.valueOf(flag));
390: }
391:
392: /** Get the length of the column - for character type fields only.
393: * @return the length, <code>null</code> if it is not a character type
394: * field or there is no length.
395: */
396: public Integer getLength() {
397: return _length;
398: }
399:
400: /** Set the length of the column - for character type fields only.
401: * @param length the length for the column if it a character type
402: * @throws DBException if impossible
403: */
404: public void setLength(Integer length) throws DBException {
405: Integer old = _length;
406:
407: _length = length;
408: firePropertyChange(PROP_LENGTH, old, length);
409: }
410:
411: /** Get the precision of the column - for numeric type fields only.
412: * @return the precision, <code>null</code> if it is not a numeric type
413: * field or there is no precision.
414: */
415: public Integer getPrecision() {
416: return _precision;
417: }
418:
419: /** Set the precision of the column - for numeric type fields only.
420: * @param precision the precision for the column if it a numeric type
421: * @throws DBException if impossible
422: */
423: public void setPrecision(Integer precision) throws DBException {
424: Integer old = _precision;
425:
426: _precision = precision;
427: firePropertyChange(PROP_PRECISION, old, precision);
428: }
429:
430: /** Get the scale of the column - for numeric type fields only.
431: * @return the scale, <code>null</code> if it is not a numeric type
432: * field or there is no scale.
433: */
434: public Integer getScale() {
435: return _scale;
436: }
437:
438: /** Set the scale of the column - for numeric type fields only.
439: * @param scale the scale for the column if it a numeric type
440: * @throws DBException if impossible
441: */
442: public void setScale(Integer scale) throws DBException {
443: Integer old = _scale;
444:
445: _scale = scale;
446: firePropertyChange(PROP_SCALE, old, scale);
447: }
448: }
449: }
|