KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > cjdbc > common > sql > schema > DatabaseColumn


1 /**
2  * C-JDBC: Clustered JDBC.
3  * Copyright (C) 2002-2005 French National Institute For Research In Computer
4  * Science And Control (INRIA).
5  * Contact: c-jdbc@objectweb.org
6  *
7  * This library is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as published by the
9  * Free Software Foundation; either version 2.1 of the License, or any later
10  * version.
11  *
12  * This library is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
15  * for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this library; if not, write to the Free Software Foundation,
19  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
20  *
21  * Initial developer(s): Emmanuel Cecchet.
22  * Contributor(s): Julie Marguerite.
23  */

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

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

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

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

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

95   public String JavaDoc getName()
96   {
97     return name;
98   }
99
100   /**
101    * Tests if the column has a <code>UNIQUE</code> constraint (like primary
102    * keys for example).
103    *
104    * @return <code>true</code> if the column has a <code>UNIQUE</code>
105    * constraint
106    */

107   public boolean isUnique()
108   {
109     return isUnique;
110   }
111
112   /**
113    * Sets the value of {@link #isUnique}.
114    *
115    * @param bool <code>true</code> if the column has a <code>UNIQUE</code>
116    * constraint (like primary keys for example).
117    */

118   public void setIsUnique(boolean bool)
119   {
120     isUnique = bool;
121   }
122
123   /**
124    * Returns the column type according to <code>java.sql.Types</code>.
125    *
126    * @return the column type. Returns <code>Types.NULL</code> if the type is
127    * not set.
128    * @see java.sql.Types
129    */

130   public int getType()
131   {
132     return type;
133   }
134
135   /**
136    * Two <code>DatabaseColumn</code> are considered equal if they have the
137    * same name and type and if they are both unique or both non unique.
138    *
139    * @param other the object to compare with
140    * @return <code>true</code> if the columns are equal
141    */

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

159   public boolean equalsIgnoreType(Object JavaDoc other)
160   {
161     if ((other == null) || !(other instanceof DatabaseColumn))
162       return false;
163
164     DatabaseColumn c = (DatabaseColumn) other;
165     return (isUnique == c.isUnique()) && name.equals(c.getName());
166   }
167
168   /**
169    * Get xml information about this column.
170    *
171    * @return xml formatted information on this database column.
172    */

173   public String JavaDoc getXml()
174   {
175     StringBuffer JavaDoc info = new StringBuffer JavaDoc();
176     info.append("<" + DatabasesXmlTags.ELT_DatabaseColumn + " "
177         + DatabasesXmlTags.ATT_columnName + "=\"" + name + "\" "
178         + DatabasesXmlTags.ATT_isUnique + "=\"" + isUnique + "\">");
179     info.append("</" + DatabasesXmlTags.ELT_DatabaseColumn + ">");
180     return info.toString();
181   }
182 }
183
Popular Tags