Column.java

  1. /*
  2.  *
  3.  * The DbUnit Database Testing Framework
  4.  * Copyright (C)2002-2004, DbUnit.org
  5.  *
  6.  * This library is free software; you can redistribute it and/or
  7.  * modify it under the terms of the GNU Lesser General Public
  8.  * License as published by the Free Software Foundation; either
  9.  * version 2.1 of the License, or (at your option) any later version.
  10.  *
  11.  * This library is distributed in the hope that it will be useful,
  12.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14.  * Lesser General Public License for more details.
  15.  *
  16.  * You should have received a copy of the GNU Lesser General Public
  17.  * License along with this library; if not, write to the Free Software
  18.  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  19.  *
  20.  */

  21. package org.dbunit.dataset;

  22. import org.slf4j.Logger;
  23. import org.slf4j.LoggerFactory;

  24. import org.dbunit.dataset.datatype.DataType;

  25. import java.sql.DatabaseMetaData;

  26. /**
  27.  * Represents a table column.
  28.  *
  29.  * @author Manuel Laflamme
  30.  * @author Last changed by: $Author$
  31.  * @version $Revision$ $Date$
  32.  * @since 1.0 (Feb 17, 2002)
  33.  */
  34. public class Column
  35. {

  36.     /**
  37.      * Logger for this class
  38.      */
  39.     private static final Logger logger = LoggerFactory.getLogger(Column.class);

  40.     /**
  41.      * Indicates that the column might not allow <code>NULL</code> values.
  42.      */
  43.     public static final Nullable NO_NULLS = new Nullable("noNulls");
  44.     /**
  45.      * Indicates that the column definitely allows <code>NULL</code> values.
  46.      */
  47.     public static final Nullable NULLABLE = new Nullable("nullable");
  48.     /**
  49.      * Indicates that the nullability of columns is unknown.
  50.      */
  51.     public static final Nullable NULLABLE_UNKNOWN = new Nullable("nullableUnknown");

  52.     private final String _columnName;
  53.     private final DataType _dataType;
  54.     private final String _sqlTypeName;
  55.     private final Nullable _nullable;
  56.     private final String _defaultValue;
  57.     private final String _remarks;
  58.     private final AutoIncrement _autoIncrement;
  59.     private final Boolean _generatedColumn;

  60.     /**
  61.      * Creates a Column object. This constructor set nullable to true.
  62.      *
  63.      * @param columnName the column name
  64.      * @param dataType the data type
  65.      */
  66.     public Column(String columnName, DataType dataType)
  67.     {
  68.         this(columnName, dataType, NULLABLE_UNKNOWN);
  69.     }

  70.     /**
  71.      * Creates a Column object.
  72.      */
  73.     public Column(String columnName, DataType dataType, Nullable nullable)
  74.     {
  75.         this(columnName, dataType, dataType.toString(), nullable, null);
  76.     }

  77.     /**
  78.      * Creates a Column object.
  79.      */
  80.     public Column(String columnName, DataType dataType, String sqlTypeName,
  81.             Nullable nullable)
  82.     {
  83.         this(columnName, dataType, sqlTypeName, nullable, null);
  84.     }

  85.     /**
  86.      * Creates a Column object.
  87.      * @param columnName The name of the column
  88.      * @param dataType The DbUnit {@link DataType} of the column
  89.      * @param sqlTypeName The SQL name of the column which comes from the JDBC driver.
  90.      * See value 'TYPE_NAME' in {@link DatabaseMetaData#getColumns(String, String, String, String)}
  91.      * @param nullable whether or not the column is nullable
  92.      * @param defaultValue The default value on the DB for this column. Can be <code>null</code>.
  93.      */
  94.     public Column(String columnName, DataType dataType, String sqlTypeName,
  95.             Nullable nullable, String defaultValue)
  96.     {
  97.         this(columnName, dataType, sqlTypeName, nullable, defaultValue, null, null);
  98.     }

  99.     /**
  100.      * Creates a Column object.
  101.      * @param columnName The name of the column
  102.      * @param dataType The DbUnit {@link DataType} of the column
  103.      * @param sqlTypeName The SQL name of the column which comes from the JDBC driver.
  104.      * See value 'TYPE_NAME' in {@link DatabaseMetaData#getColumns(String, String, String, String)}
  105.      * @param nullable whether or not the column is nullable
  106.      * @param defaultValue The default value on the DB for this column. Can be <code>null</code>.
  107.      * @param remarks The remarks on the DB for this column. Can be <code>null</code>.
  108.      * @param autoIncrement The auto increment setting for this column. Can be <code>null</code>.
  109.      */
  110.     public Column(String columnName, DataType dataType, String sqlTypeName,
  111.             Nullable nullable, String defaultValue, String remarks, AutoIncrement autoIncrement)
  112.     {
  113.         this(columnName, dataType, sqlTypeName, nullable, defaultValue, remarks, autoIncrement, null);
  114.     }

  115.     /**
  116.      * Creates a Column object.
  117.      * @param columnName The name of the column
  118.      * @param dataType The DbUnit {@link DataType} of the column
  119.      * @param sqlTypeName The SQL name of the column which comes from the JDBC driver.
  120.      * See value 'TYPE_NAME' in {@link DatabaseMetaData#getColumns(String, String, String, String)}
  121.      * @param nullable whether or not the column is nullable
  122.      * @param defaultValue The default value on the DB for this column. Can be <code>null</code>.
  123.      * @param remarks The remarks on the DB for this column. Can be <code>null</code>.
  124.      * @param autoIncrement The auto increment setting for this column. Can be <code>null</code>.
  125.      * @param generatedColumn Whether this column is a generated column. Can be <code>null</code>.
  126.      */
  127.     public Column(String columnName, DataType dataType, String sqlTypeName,
  128.             Nullable nullable, String defaultValue, String remarks, AutoIncrement autoIncrement,
  129.             Boolean generatedColumn)
  130.     {
  131.         _columnName = columnName;
  132.         _dataType = dataType;
  133.         _sqlTypeName = sqlTypeName;
  134.         _nullable = nullable;
  135.         _defaultValue = defaultValue;
  136.         _remarks = remarks;
  137.         _autoIncrement = autoIncrement;
  138.         _generatedColumn = generatedColumn;
  139.     }

  140.     public boolean hasDefaultValue()
  141.     {
  142.         return _defaultValue != null;
  143.     }
  144.    
  145.     public boolean isNotNullable()
  146.     {
  147.         return _nullable== Column.NO_NULLS;
  148.     }
  149.    
  150.     /**
  151.      * Returns this column name.
  152.      */
  153.     public String getColumnName()
  154.     {
  155.         return _columnName;
  156.     }

  157.     /**
  158.      * Returns this column data type.
  159.      */
  160.     public DataType getDataType()
  161.     {
  162.         return _dataType;
  163.     }

  164.     /**
  165.      * Returns this column sql data type name.
  166.      */
  167.     public String getSqlTypeName()
  168.     {
  169.         return _sqlTypeName;
  170.     }

  171.     /**
  172.      * Returns <code>true</code> if this column is nullable.
  173.      */
  174.     public Nullable getNullable()
  175.     {
  176.         return _nullable;
  177.     }

  178.     /**
  179.      * @return The default value the database uses for this column
  180.      * if not specified in the insert column list
  181.      */
  182.     public String getDefaultValue()
  183.     {
  184.         return _defaultValue;
  185.     }
  186.    
  187.     /**
  188.      * @return The remarks set on the database for this column
  189.      * @since 2.4.3
  190.      */
  191.     public String getRemarks()
  192.     {
  193.         return _remarks;
  194.     }
  195.    
  196.     /**
  197.      * @return The auto-increment property for this column
  198.      * @since 2.4.3
  199.      */
  200.     public AutoIncrement getAutoIncrement()
  201.     {
  202.         return _autoIncrement;
  203.     }
  204.    
  205.     /**
  206.      * @return Whether the column is a generated column
  207.      * @since 3.0.0
  208.      */
  209.     public Boolean getGeneratedColumn()
  210.     {
  211.         return _generatedColumn;
  212.     }
  213.    
  214.     /**
  215.      * Returns the appropriate Nullable constant according specified JDBC
  216.      * DatabaseMetaData constant.
  217.      *
  218.      * @param nullable one of the following constants
  219.      * {@link java.sql.DatabaseMetaData#columnNoNulls},
  220.      * {@link java.sql.DatabaseMetaData#columnNullable},
  221.      * {@link java.sql.DatabaseMetaData#columnNullableUnknown}
  222.      */
  223.     public static Nullable nullableValue(int nullable)
  224.     {
  225.         if(logger.isDebugEnabled())
  226.             logger.debug("nullableValue(nullable={}) - start", String.valueOf(nullable));

  227.         switch (nullable)
  228.         {
  229.             case DatabaseMetaData.columnNoNulls:
  230.                 return NO_NULLS;

  231.             case DatabaseMetaData.columnNullable:
  232.                 return NULLABLE;

  233.             case DatabaseMetaData.columnNullableUnknown:
  234.                 return NULLABLE_UNKNOWN;

  235.             default:
  236.                 throw new IllegalArgumentException("Unknown constant value "
  237.                         + nullable);
  238.         }
  239.     }

  240.     /**
  241.      * Returns the appropriate Nullable constant.
  242.      *
  243.      * @param nullable <code>true</code> if null is allowed
  244.      */
  245.     public static Nullable nullableValue(boolean nullable)
  246.     {
  247.         if(logger.isDebugEnabled())
  248.             logger.debug("nullableValue(nullable={}) - start", String.valueOf(nullable));
  249.        
  250.         return nullable ? NULLABLE : NO_NULLS;
  251.     }
  252.    
  253.     /**
  254.      * Converts a DatabaseMetaData boolean string to a Boolean object.
  255.      * @param value The string to convert
  256.      * @return True if string is "YES", false if string is "NO", null otherwise
  257.      */
  258.     public static Boolean convertMetaDataBoolean(String value)
  259.     {
  260.         if ("YES".equalsIgnoreCase(value)) {
  261.             return true;
  262.         } else if ("NO".equalsIgnoreCase(value)) {
  263.             return false;
  264.         } else {
  265.             return null;
  266.         }
  267.     }

  268.     ////////////////////////////////////////////////////////////////////////////
  269.     // Object class

  270.     public String toString()
  271.     {
  272.         return "(" + _columnName + ", " + _dataType + ", " + _nullable + ")";
  273.     }

  274.     public boolean equals(Object o)
  275.     {
  276.         logger.debug("equals(o={}) - start", o);

  277.         if (this == o) return true;
  278.         if (!(o instanceof Column)) return false;

  279.         final Column column = (Column)o;

  280.         if (!_columnName.equals(column._columnName)) return false;
  281.         if (!_dataType.equals(column._dataType)) return false;
  282.         if (!_nullable.equals(column._nullable)) return false;
  283.         if (!_sqlTypeName.equals(column._sqlTypeName)) return false;
  284.        
  285.         // Default value is nullable
  286.         if (_defaultValue==null){
  287.             if(column._defaultValue!=null)
  288.                 return false;
  289.         }
  290.         else{
  291.             if(!_defaultValue.equals(column._defaultValue))
  292.                 return false;
  293.         }

  294.         return true;
  295.     }

  296.     public int hashCode()
  297.     {
  298.         int result;
  299.         result = _columnName.hashCode();
  300.         result = 29 * result + _dataType.hashCode();
  301.         result = 29 * result + _sqlTypeName.hashCode();
  302.         result = 29 * result + _nullable.hashCode();
  303.         result = 29 * result + (_defaultValue==null? 0 : _defaultValue.hashCode());
  304.         return result;
  305.     }

  306.     /**
  307.      * Specifies nullable usage.
  308.      *
  309.      * @author Manuel Laflamme
  310.      * @author Last changed by: $Author$
  311.      * @version $Revision$ $Date$
  312.      * @since Feb 17, 2002
  313.      * @see Column
  314.      */
  315.     public static class Nullable
  316.     {

  317.         private final String _name;

  318.         private Nullable(String name)
  319.         {
  320.             _name = name;
  321.         }

  322.         ////////////////////////////////////////////////////////////////////////////
  323.         // Object class

  324.         public String toString()
  325.         {
  326.             return _name;
  327.         }
  328.     }
  329.    
  330.    
  331.     /**
  332.      * Enumeration for valid auto-increment values provided by JDBC driver implementations.
  333.      *
  334.      * @author gommma
  335.      * @author Last changed by: $Author$
  336.      * @version $Revision$ $Date$
  337.      * @since 2.4.3
  338.      * @see Column
  339.      */
  340.     public static class AutoIncrement
  341.     {
  342.         public static final AutoIncrement YES = new AutoIncrement("YES");
  343.         public static final AutoIncrement NO = new AutoIncrement("NO");
  344.         public static final AutoIncrement UNKNOWN = new AutoIncrement("UNKNOWN");
  345.        
  346.         /**
  347.          * Logger for this class
  348.          */
  349.         private static final Logger LOGGER = LoggerFactory.getLogger(AutoIncrement.class);

  350.         private final String key;
  351.         private AutoIncrement(String key)
  352.         {
  353.             this.key = key;
  354.         }
  355.        
  356.         public String getKey()
  357.         {
  358.             return key;
  359.         }

  360.         /**
  361.          * Searches the enumeration type for the given String provided by the JDBC driver.
  362.          * <p>
  363.          * If the parameter <code>autoIncrementValue</code>
  364.          * <ul>
  365.          * <li>equalsIgnoreCase &quot;YES&quot; or equals &quot;1&quot; then {@link AutoIncrement#YES} is returned</li>
  366.          * <li></li>
  367.          * </ul>
  368.          * </p>
  369.          * @param isAutoIncrement The String from the JDBC driver.
  370.          * @return The enumeration
  371.          */
  372.         public static AutoIncrement autoIncrementValue(String isAutoIncrement)
  373.         {
  374.             if(LOGGER.isDebugEnabled())
  375.                 logger.debug("autoIncrementValue(isAutoIncrement={}) - start", isAutoIncrement);
  376.            
  377.             AutoIncrement result = AutoIncrement.UNKNOWN;
  378.            
  379.             if(isAutoIncrement != null)
  380.             {
  381.                 if(isAutoIncrement.equalsIgnoreCase("YES") || isAutoIncrement.equals("1"))
  382.                 {
  383.                     result = AutoIncrement.YES;
  384.                 }
  385.                 else if(isAutoIncrement.equalsIgnoreCase("NO") || isAutoIncrement.equals("0"))
  386.                 {
  387.                     result = AutoIncrement.NO;
  388.                 }
  389.             }
  390.             return result;
  391.         }


  392.         public String toString()
  393.         {
  394.             return "autoIncrement=" + key;
  395.         }
  396.     }

  397. }