PostgresqlDataTypeFactory.java

  1. /*
  2.  *
  3.  * The DbUnit Database Testing Framework
  4.  * Copyright (C)2002-2009, 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.postgresql;

  22. import org.dbunit.dataset.datatype.DataType;
  23. import org.dbunit.dataset.datatype.DataTypeException;
  24. import org.dbunit.dataset.datatype.DefaultDataTypeFactory;
  25. import org.slf4j.Logger;
  26. import org.slf4j.LoggerFactory;

  27. import java.sql.Types;
  28. import java.util.Arrays;
  29. import java.util.Collection;

  30. /**
  31.  * Specialized factory that recognizes Postgresql data types.
  32.  * <p>
  33.  * Derived from work by manuel.laflamme
  34.  * </p>
  35.  *
  36.  * @author Jarvis Cochrane (jarvis@cochrane.com.au)
  37.  * @author manuel.laflamme
  38.  * @author Martin Gollogly (zemertz@gmail.com)
  39.  * @since 2.4.5 (Apr 27, 2009)
  40.  */
  41. public class PostgresqlDataTypeFactory extends DefaultDataTypeFactory
  42. {
  43.     /**
  44.      * Logger for this class
  45.      */
  46.     private static final Logger logger =
  47.             LoggerFactory.getLogger(PostgresqlDataTypeFactory.class);
  48.     /**
  49.      * Database product names supported.
  50.      */
  51.     private static final Collection DATABASE_PRODUCTS =
  52.             Arrays.asList(new String[] {"PostgreSQL"});

  53.     /**
  54.      * @see org.dbunit.dataset.datatype.IDbProductRelatable#getValidDbProducts()
  55.      */
  56.     @Override
  57.     public Collection getValidDbProducts()
  58.     {
  59.         return DATABASE_PRODUCTS;
  60.     }

  61.     public static Collection getDatabaseProducts()
  62.     {
  63.         return DATABASE_PRODUCTS;
  64.     }

  65.     @Override
  66.     public DataType createDataType(final int sqlType, final String sqlTypeName)
  67.             throws DataTypeException
  68.     {
  69.         logger.debug("createDataType(sqlType={}, sqlTypeName={})",
  70.                 String.valueOf(sqlType), sqlTypeName);

  71.         if (sqlType == Types.OTHER)
  72.         {
  73.             // Treat Postgresql UUID types as VARCHARS
  74.             if ("uuid".equals(sqlTypeName))
  75.             {
  76.                 return new UuidType();
  77.             } else if ("interval".equals(sqlTypeName))
  78.             {
  79.                 return new IntervalType();
  80.             } else if ("inet".equals(sqlTypeName))
  81.             {
  82.                 return new InetType();
  83.             } else if ("geometry".equals(sqlTypeName))
  84.             {
  85.                 return new GeometryType();
  86.             } else if ("citext".equals(sqlTypeName))
  87.             {
  88.                 return new CitextType();
  89.             } else
  90.             {
  91.                 // Finally check whether the user defined a custom datatype
  92.                 if (isEnumType(sqlTypeName))
  93.                 {
  94.                     logger.debug(
  95.                             "Custom enum type used for sqlTypeName {} (sqlType '{}')",
  96.                             sqlTypeName, sqlType);
  97.                     return new GenericEnumType(sqlTypeName);
  98.                 }
  99.             }
  100.         } else if (sqlType == Types.BIGINT && "oid".equals(sqlTypeName)) {
  101.             return new PostgreSQLOidDataType();
  102.         }

  103.         return super.createDataType(sqlType, sqlTypeName);
  104.     }

  105.     /**
  106.      * Returns a data type for the given sql type name if the user wishes one.
  107.      * <b>Designed to be overridden by custom implementations extending this
  108.      * class.</b> Override this method if you have a custom enum type in the
  109.      * database and want to map it via dbunit.
  110.      *
  111.      * @param sqlTypeName
  112.      *            The sql type name for which users can specify a custom data
  113.      *            type.
  114.      * @return <code>null</code> if the given type name is not a custom type
  115.      *         which is the default implementation.
  116.      * @since 2.4.6
  117.      */
  118.     public boolean isEnumType(final String sqlTypeName)
  119.     {
  120.         return false;
  121.     }
  122. }