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