001: /**
002: * Sequoia: Database clustering technology.
003: * Copyright (C) 2002-2004 French National Institute For Research In Computer
004: * Science And Control (INRIA).
005: * Copyright (C) 2005 AmicoSoft, Inc. dba Emic Networks
006: * Contact: sequoia@continuent.org
007: *
008: * Licensed under the Apache License, Version 2.0 (the "License");
009: * you may not use this file except in compliance with the License.
010: * You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing, software
015: * distributed under the License is distributed on an "AS IS" BASIS,
016: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: * See the License for the specific language governing permissions and
018: * limitations under the License.
019: *
020: * Initial developer(s): Emmanuel Cecchet.
021: * Contributor(s): Julie Marguerite.
022: */package org.continuent.sequoia.common.sql.schema;
023:
024: import java.io.Serializable;
025: import java.sql.Types;
026:
027: import org.continuent.sequoia.common.xml.DatabasesXmlTags;
028:
029: /**
030: * A <code>DatabaseColumn</code> represents a column of a database table. It
031: * is composed of a name, type (not used yet) and a boolean indicated whether or
032: * not rows are unique or not (like primary keys or columns created explicitely
033: * with the <code>UNIQUE</code> keyword).
034: *
035: * @author <a href="mailto:Emmanuel.Cecchet@inria.fr">Emmanuel Cecchet</a>
036: * @author <a href="mailto:Julie.Marguerite@inria.fr">Julie Marguerite</a>
037: * @version 1.0
038: */
039: public class DatabaseColumn implements Serializable {
040: private static final long serialVersionUID = 1118853825798791836L;
041:
042: /** Column name. */
043: private String name;
044:
045: /**
046: * <code>true</code> if this column has a <code>UNIQUE</code> constraint
047: * (like primary keys for example).
048: */
049: private boolean isUnique;
050:
051: /** Type of the column (<code>VARCHAR</code>,<code>TEXT</code>, ...). */
052: private int type;
053:
054: /**
055: * Creates a new <code>DatabaseColumn</code> instance.
056: *
057: * @param name name of the column
058: * @param isUnique <code>true</code> if this column has a
059: * <code>UNIQUE</code> constraint
060: */
061: public DatabaseColumn(String name, boolean isUnique) {
062: this (name, isUnique, Types.NULL);
063: }
064:
065: /**
066: * Creates a new <code>DatabaseColumn</code> instance.
067: *
068: * @param name name of the column
069: * @param isUnique <code>true</code> if this column has a
070: * <code>UNIQUE</code> constraint
071: * @param type type of the column (<code>VARCHAR</code>,<code>TEXT</code>,
072: * ...)
073: */
074: public DatabaseColumn(String name, boolean isUnique, int type) {
075: if (name == null)
076: throw new IllegalArgumentException(
077: "Illegal null column name in DatabaseColumn constructor");
078:
079: this .name = name;
080: this .isUnique = isUnique;
081: this .type = type;
082: }
083:
084: /**
085: * Gets the column name.
086: *
087: * @return a <code>String</code> value.
088: */
089: public String getName() {
090: return name;
091: }
092:
093: /**
094: * Returns the column type according to <code>java.sql.Types</code>.
095: *
096: * @return the column type. Returns <code>Types.NULL</code> if the type is
097: * not set.
098: * @see java.sql.Types
099: */
100: public int getType() {
101: return type;
102: }
103:
104: /**
105: * Tests if the column has a <code>UNIQUE</code> constraint (like primary
106: * keys for example).
107: *
108: * @return <code>true</code> if the column has a <code>UNIQUE</code>
109: * constraint
110: */
111: public boolean isUnique() {
112: return isUnique;
113: }
114:
115: /**
116: * Sets the type value.
117: *
118: * @param type The type to set.
119: */
120: public final void setType(int type) {
121: this .type = type;
122: }
123:
124: /**
125: * Sets the value of {@link #isUnique}.
126: *
127: * @param bool <code>true</code> if the column has a <code>UNIQUE</code>
128: * constraint (like primary keys for example).
129: */
130: public void setIsUnique(boolean bool) {
131: isUnique = bool;
132: }
133:
134: /**
135: * Two <code>DatabaseColumn</code> are considered equal if they have the
136: * same name and type and if they are both unique or both non unique.
137: *
138: * @param other the object to compare with
139: * @return <code>true</code> if the columns are equal
140: */
141: public boolean equals(Object other) {
142: if ((other == null) || !(other instanceof DatabaseColumn))
143: return false;
144:
145: DatabaseColumn c = (DatabaseColumn) other;
146: return (isUnique == c.isUnique()) && name.equals(c.getName())
147: && (type == c.getType());
148: }
149:
150: /**
151: * This function is the same as equal but ignores the column type.
152: *
153: * @param other the object to compare with
154: * @return true if the columns are equal ignoring their type.
155: * @see #equals(Object)
156: */
157: public boolean equalsIgnoreType(Object other) {
158: if ((other == null) || !(other instanceof DatabaseColumn))
159: return false;
160:
161: DatabaseColumn c = (DatabaseColumn) other;
162: return (isUnique == c.isUnique()) && name.equals(c.getName());
163: }
164:
165: /**
166: * Get xml information about this column.
167: *
168: * @return xml formatted information on this database column.
169: */
170: public String getXml() {
171: StringBuffer info = new StringBuffer();
172: info.append("<" + DatabasesXmlTags.ELT_DatabaseColumn + " "
173: + DatabasesXmlTags.ATT_columnName + "=\"" + name
174: + "\" " + DatabasesXmlTags.ATT_isUnique + "=\""
175: + isUnique + "\">");
176: info.append("</" + DatabasesXmlTags.ELT_DatabaseColumn + ">");
177: return info.toString();
178: }
179:
180: /**
181: * @see java.lang.Object#toString()
182: */
183: public String toString() {
184: return name;
185: }
186: }
|