Main Page | Packages | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | Related Pages

DatabaseColumn.java

00001 /**
00002  * C-JDBC: Clustered JDBC.
00003  * Copyright (C) 2002-2004 French National Institute For Research In Computer
00004  * Science And Control (INRIA).
00005  * Contact: c-jdbc@objectweb.org
00006  * 
00007  * This library is free software; you can redistribute it and/or modify it
00008  * under the terms of the GNU Lesser General Public License as published by the
00009  * Free Software Foundation; either version 2.1 of the License, or any later
00010  * version.
00011  * 
00012  * This library is distributed in the hope that it will be useful, but WITHOUT
00013  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00014  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
00015  * for more details.
00016  * 
00017  * You should have received a copy of the GNU Lesser General Public License
00018  * along with this library; if not, write to the Free Software Foundation,
00019  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
00020  *
00021  * Initial developer(s): Emmanuel Cecchet.
00022  * Contributor(s): Julie Marguerite.
00023  */
00024 
00025 package org.objectweb.cjdbc.common.sql.schema;
00026 
00027 import java.io.Serializable;
00028 import java.sql.Types;
00029 
00030 import org.objectweb.cjdbc.common.xml.DatabasesXmlTags;
00031 
00032 /**
00033  * A <code>DatabaseColumn</code> represents a column of a database table. It
00034  * is composed of a name, type (not used yet) and a boolean indicated whether
00035  * or not rows are unique or not (like primary keys or columns created
00036  * explicitely with the <code>UNIQUE</code> keyword).
00037  * 
00038  * @author <a href="mailto:Emmanuel.Cecchet@inria.fr">Emmanuel Cecchet</a>
00039  * @author <a href="mailto:Julie.Marguerite@inria.fr">Julie Marguerite</a>
00040  * @version 1.0
00041  */
00042 public class DatabaseColumn implements Serializable
00043 {
00044   /** Column name. */
00045   private String name;
00046 
00047   /**
00048    * <code>true</code> if this column has a <code>UNIQUE</code> constraint
00049    * (like primary keys for example).
00050    */
00051   private boolean isUnique;
00052 
00053   /** Type of the column (<code>VARCHAR</code>,<code>TEXT</code>, ...). */
00054   private int type;
00055 
00056   /**
00057    * Creates a new <code>DatabaseColumn</code> instance.
00058    * 
00059    * @param name name of the column
00060    * @param isUnique <code>true</code> if this column has a <code>UNIQUE</code>
00061    *          constraint
00062    */
00063   public DatabaseColumn(String name, boolean isUnique)
00064   {
00065     this(name, isUnique, Types.NULL);
00066   }
00067 
00068   /**
00069    * Creates a new <code>DatabaseColumn</code> instance.
00070    * 
00071    * @param name name of the column
00072    * @param isUnique <code>true</code> if this column has a <code>UNIQUE</code>
00073    *          constraint
00074    * @param type type of the column (<code>VARCHAR</code>,<code>TEXT</code>,
00075    *          ...)
00076    */
00077   public DatabaseColumn(String name, boolean isUnique, int type)
00078   {
00079     if (name == null)
00080       throw new IllegalArgumentException("Illegal null column name in DatabaseColumn constructor");
00081 
00082     this.name = name;
00083     this.isUnique = isUnique;
00084     this.type = type;
00085   }
00086 
00087   /**
00088    * Gets the column name.
00089    * 
00090    * @return a <code>String</code> value.
00091    */
00092   public String getName()
00093   {
00094     return name;
00095   }
00096 
00097   /**
00098    * Tests if the column has a <code>UNIQUE</code> constraint (like primary
00099    * keys for example).
00100    * 
00101    * @return <code>true</code> if the column has a <code>UNIQUE</code>
00102    *         constraint
00103    */
00104   public boolean isUnique()
00105   {
00106     return isUnique;
00107   }
00108 
00109   /**
00110    * Sets the value of {@link #isUnique}.
00111    * 
00112    * @param bool <code>true</code> if the column has a <code>UNIQUE</code>
00113    *          constraint (like primary keys for example).
00114    */
00115   public void setIsUnique(boolean bool)
00116   {
00117     isUnique = bool;
00118   }
00119 
00120   /**
00121    * Returns the column type according to <code>java.sql.Types</code>.
00122    * 
00123    * @return the column type. Returns <code>Types.NULL</code> if the type is
00124    *         not set.
00125    * @see java.sql.Types
00126    */
00127   public int getType()
00128   {
00129     return type;
00130   }
00131 
00132   /**
00133    * Two <code>DatabaseColumn</code> are considered equal if they have the
00134    * same name and type and if they are both unique or both non unique.
00135    * 
00136    * @param other the object to compare with
00137    * @return <code>true</code> if the columns are equal
00138    */
00139   public boolean equals(Object other)
00140   {
00141     if ((other == null) || !(other instanceof DatabaseColumn))
00142       return false;
00143 
00144     DatabaseColumn c = (DatabaseColumn) other;
00145     return (isUnique == c.isUnique())
00146       && name.equals(c.getName())
00147       && (type == c.getType());
00148   }
00149 
00150   /**
00151    * This function is the same as equal but ignores the column type.
00152    * 
00153    * @param other the object to compare with
00154    * @return true if the columns are equal ignoring their type.
00155    * @see #equals(Object)
00156    */
00157   public boolean equalsIgnoreType(Object other)
00158   {
00159     if ((other == null) || !(other instanceof DatabaseColumn))
00160       return false;
00161 
00162     DatabaseColumn c = (DatabaseColumn) other;
00163     return (isUnique == c.isUnique()) && name.equals(c.getName());
00164   }
00165 
00166   /**
00167    * Get xml information about this column.
00168    * 
00169    * @return xml formatted information on this database column.
00170    */
00171   public String getXml()
00172   {
00173     StringBuffer info = new StringBuffer();
00174     info.append(
00175       "<"
00176         + DatabasesXmlTags.ELT_DatabaseColumn
00177         + " "
00178         + DatabasesXmlTags.ATT_columnName
00179         + "=\""
00180         + name
00181         + "\" "
00182         + DatabasesXmlTags.ATT_isUnique
00183         + "=\""
00184         + isUnique
00185         + "\">");
00186     info.append("</" + DatabasesXmlTags.ELT_DatabaseColumn + ">");
00187     return info.toString();
00188   }
00189 }

Generated on Mon Apr 11 22:01:31 2005 for C-JDBC by  doxygen 1.3.9.1