MsSqlDataTypeFactory.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.ext.mssql;

  22. import java.sql.Types;
  23. import java.util.Arrays;
  24. import java.util.Collection;

  25. import org.dbunit.dataset.datatype.DataType;
  26. import org.dbunit.dataset.datatype.DataTypeException;
  27. import org.dbunit.dataset.datatype.DefaultDataTypeFactory;
  28. import org.slf4j.Logger;
  29. import org.slf4j.LoggerFactory;

  30. /**
  31.  * Specialized factory that recognizes MS SQL Server data types.
  32.  *
  33.  * @author Manuel Laflamme
  34.  * @since May 19, 2003
  35.  * @version $Revision$
  36.  */
  37. public class MsSqlDataTypeFactory extends DefaultDataTypeFactory
  38. {

  39.     /**
  40.      * Logger for this class
  41.      */
  42.     private static final Logger logger = LoggerFactory.getLogger(MsSqlDataTypeFactory.class);
  43.     /**
  44.      * Database product names supported.
  45.      */
  46.     private static final Collection DATABASE_PRODUCTS = Arrays.asList(new String[] {"mssql", "Microsoft SQL Server"});
  47.    
  48.     private static final DateTimeOffsetType DATE_TIME_OFFSET_TYPE = new DateTimeOffsetType();

  49.     public static final int NCHAR = -8;
  50.     public static final int NVARCHAR = -9;
  51.     public static final int NTEXT = -10;
  52.     public static final int NTEXT_MSSQL_2005 = -16;
  53.    
  54.     /**
  55.      * @see org.dbunit.dataset.datatype.IDbProductRelatable#getValidDbProducts()
  56.      */
  57.     public Collection getValidDbProducts()
  58.     {
  59.       return DATABASE_PRODUCTS;
  60.     }

  61.     public DataType createDataType(int sqlType, String sqlTypeName) throws DataTypeException
  62.     {
  63.         if(logger.isDebugEnabled())
  64.             logger.debug("createDataType(sqlType={}, sqlTypeName={}) - start", String.valueOf(sqlType), sqlTypeName);

  65.         // TODO : Process MS SQL Server custom datatype here
  66.         if(sqlType == Types.CHAR)
  67.         {
  68.             if (UniqueIdentifierType.UNIQUE_IDENTIFIER_TYPE.equals(sqlTypeName))
  69.             {
  70.                 return new UniqueIdentifierType();
  71.             }
  72.         }

  73.         switch(sqlType) {
  74.             case NCHAR: return DataType.CHAR; // nchar
  75.             case NVARCHAR: return DataType.VARCHAR; // nvarchar
  76.             case NTEXT: return DataType.LONGVARCHAR; // ntext
  77.             case NTEXT_MSSQL_2005: return DataType.LONGVARCHAR; // ntext
  78.             case DateTimeOffsetType.TYPE: return DATE_TIME_OFFSET_TYPE;
  79.             default: return super.createDataType(sqlType, sqlTypeName);
  80.         }
  81.     }
  82. }