KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > continuent > sequoia > controller > sql > schema > DatabaseColumn


1 /**
2  * Sequoia: Database clustering technology.
3  * Copyright (C) 2002-2004 French National Institute For Research In Computer
4  * Science And Control (INRIA).
5  * Copyright (C) 2005 AmicoSoft, Inc. dba Emic Networks
6  * Contact: sequoia@continuent.org
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  * Initial developer(s): Emmanuel Cecchet.
21  * Contributor(s): Julie Marguerite.
22  */

23
24 package org.continuent.sequoia.controller.sql.schema;
25
26 import java.io.Serializable JavaDoc;
27 import java.sql.Types JavaDoc;
28
29 /**
30  * A <code>DatabaseColumn</code> represents a column of a database table. It
31  * is composed of a name, type (not used yet) and a boolean indicated whether or
32  * not rows are unique or not (like primary keys or columns created explicitely
33  * with the <code>UNIQUE</code> keyword).
34  *
35  * @author <a HREF="mailto:Emmanuel.Cecchet@inria.fr">Emmanuel Cecchet</a>
36  * @author <a HREF="mailto:Julie.Marguerite@inria.fr">Julie Marguerite</a>
37  * @version 1.0
38  */

39 public class DatabaseColumn implements Serializable JavaDoc
40 {
41   private static final long serialVersionUID = 1118853825798791836L;
42
43   /** Column name. */
44   private String JavaDoc name;
45
46   /**
47    * <code>true</code> if this column has a <code>UNIQUE</code> constraint
48    * (like primary keys for example).
49    */

50   private boolean isUnique;
51
52   /** Type of the column (<code>VARCHAR</code>,<code>TEXT</code>, ...). */
53   private int type;
54
55   /**
56    * Creates a new <code>DatabaseColumn</code> instance.
57    *
58    * @param name name of the column
59    * @param isUnique <code>true</code> if this column has a
60    * <code>UNIQUE</code> constraint
61    */

62   public DatabaseColumn(String JavaDoc name, boolean isUnique)
63   {
64     this(name, isUnique, Types.NULL);
65   }
66
67   /**
68    * Creates a new <code>DatabaseColumn</code> instance.
69    *
70    * @param name name of the column
71    * @param isUnique <code>true</code> if this column has a
72    * <code>UNIQUE</code> constraint
73    * @param type type of the column (<code>VARCHAR</code>,<code>TEXT</code>,
74    * ...)
75    */

76   public DatabaseColumn(String JavaDoc name, boolean isUnique, int type)
77   {
78     if (name == null)
79       throw new IllegalArgumentException JavaDoc(
80           "Illegal null column name in DatabaseColumn constructor");
81
82     this.name = name;
83     this.isUnique = isUnique;
84     this.type = type;
85   }
86
87   /**
88    * Gets the column name.
89    *
90    * @return a <code>String</code> value.
91    */

92   public String JavaDoc getName()
93   {
94     return name;
95   }
96
97   /**
98    * Returns the column type according to <code>java.sql.Types</code>.
99    *
100    * @return the column type. Returns <code>Types.NULL</code> if the type is
101    * not set.
102    * @see java.sql.Types
103    */

104   public int getType()
105   {
106     return type;
107   }
108
109   /**
110    * Tests if the column has a <code>UNIQUE</code> constraint (like primary
111    * keys for example).
112    *
113    * @return <code>true</code> if the column has a <code>UNIQUE</code>
114    * constraint
115    */

116   public boolean isUnique()
117   {
118     return isUnique;
119   }
120
121   /**
122    * Sets the type value.
123    *
124    * @param type The type to set.
125    */

126   public final void setType(int type)
127   {
128     this.type = type;
129   }
130
131   /**
132    * Sets the value of {@link #isUnique}.
133    *
134    * @param bool <code>true</code> if the column has a <code>UNIQUE</code>
135    * constraint (like primary keys for example).
136    */

137   public void setIsUnique(boolean bool)
138   {
139     isUnique = bool;
140   }
141
142   /**
143    * Two <code>DatabaseColumn</code> are considered equal if they have the
144    * same name and type and if they are both unique or both non unique.
145    *
146    * @param other the object to compare with
147    * @return <code>true</code> if the columns are equal
148    */

149   public boolean equals(Object JavaDoc other)
150   {
151     if ((other == null) || !(other instanceof DatabaseColumn))
152       return false;
153
154     DatabaseColumn c = (DatabaseColumn) other;
155     return (isUnique == c.isUnique()) && name.equals(c.getName())
156         && (type == c.getType());
157   }
158
159   /**
160    * This function is the same as equal but ignores the column type.
161    *
162    * @param other the object to compare with
163    * @return true if the columns are equal ignoring their type.
164    * @see #equals(Object)
165    */

166   public boolean equalsIgnoreType(Object JavaDoc other)
167   {
168     if ((other == null) || !(other instanceof DatabaseColumn))
169       return false;
170
171     DatabaseColumn c = (DatabaseColumn) other;
172     return (isUnique == c.isUnique()) && name.equals(c.getName());
173   }
174
175   /**
176    * @see java.lang.Object#toString()
177    */

178   public String JavaDoc toString()
179   {
180     return name;
181   }
182 }
183
Popular Tags