View Javadoc
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  
23  import org.dbunit.dataset.datatype.DataType;
24  import org.dbunit.dataset.datatype.DataTypeException;
25  import org.dbunit.dataset.datatype.DefaultDataTypeFactory;
26  import org.slf4j.Logger;
27  import org.slf4j.LoggerFactory;
28  
29  import java.sql.Types;
30  import java.util.Arrays;
31  import java.util.Collection;
32  
33  /**
34   * Specialized factory that recognizes Postgresql data types.
35   * <p>
36   * Derived from work by manuel.laflamme
37   * </p>
38   *
39   * @author Jarvis Cochrane (jarvis@cochrane.com.au)
40   * @author manuel.laflamme
41   * @author Martin Gollogly (zemertz@gmail.com)
42   * @since 2.4.5 (Apr 27, 2009)
43   */
44  public class PostgresqlDataTypeFactory extends DefaultDataTypeFactory
45  {
46      /**
47       * Logger for this class
48       */
49      private static final Logger logger =
50              LoggerFactory.getLogger(PostgresqlDataTypeFactory.class);
51      /**
52       * Database product names supported.
53       */
54      private static final Collection DATABASE_PRODUCTS =
55              Arrays.asList(new String[] {"PostgreSQL"});
56  
57      /**
58       * @see org.dbunit.dataset.datatype.IDbProductRelatable#getValidDbProducts()
59       */
60      @Override
61      public Collection getValidDbProducts()
62      {
63          return DATABASE_PRODUCTS;
64      }
65  
66      public static Collection getDatabaseProducts()
67      {
68          return DATABASE_PRODUCTS;
69      }
70  
71      @Override
72      public DataType createDataType(final int sqlType, final String sqlTypeName)
73              throws DataTypeException
74      {
75          logger.debug("createDataType(sqlType={}, sqlTypeName={})",
76                  String.valueOf(sqlType), sqlTypeName);
77  
78          if (sqlType == Types.OTHER)
79          {
80              // Treat Postgresql UUID types as VARCHARS
81              if ("uuid".equals(sqlTypeName))
82              {
83                  return new UuidType();
84              } else if ("interval".equals(sqlTypeName))
85              {
86                  return new IntervalType();
87              } else if ("inet".equals(sqlTypeName))
88              {
89                  return new InetType();
90              } else if ("geometry".equals(sqlTypeName))
91              {
92                  return new GeometryType();
93              } else if ("citext".equals(sqlTypeName))
94              {
95                  return new CitextType();
96              } else
97              {
98                  // Finally check whether the user defined a custom datatype
99                  if (isEnumType(sqlTypeName))
100                 {
101                     if (logger.isDebugEnabled())
102                     {
103                         logger.debug(
104                                 "Custom enum type used for sqlTypeName {} (sqlType '{}')",
105                                 new Object[] {sqlTypeName,
106                                         new Integer(sqlType)});
107                     }
108                     return new GenericEnumType(sqlTypeName);
109                 }
110             }
111         } else if (sqlType == Types.BIGINT && "oid".equals(sqlTypeName)) {
112             return new PostgreSQLOidDataType();
113         }
114 
115         return super.createDataType(sqlType, sqlTypeName);
116     }
117 
118     /**
119      * Returns a data type for the given sql type name if the user wishes one.
120      * <b>Designed to be overridden by custom implementations extending this
121      * class.</b> Override this method if you have a custom enum type in the
122      * database and want to map it via dbunit.
123      *
124      * @param sqlTypeName
125      *            The sql type name for which users can specify a custom data
126      *            type.
127      * @return <code>null</code> if the given type name is not a custom type
128      *         which is the default implementation.
129      * @since 2.4.6
130      */
131     public boolean isEnumType(final String sqlTypeName)
132     {
133         return false;
134     }
135 }