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.mysql;
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 MySql data types.
35 *
36 * @author manuel.laflamme
37 * @author Last changed by: $Author: gommma $
38 * @version $Revision: 1031 $ $Date: 2009-09-12 17:13:43 +0200 (sab, 12 set 2009) $
39 * @since 1.5 (Sep 3, 2003)
40 */
41 public class MySqlDataTypeFactory extends DefaultDataTypeFactory
42 {
43 public static final String UNSIGNED_SUFFIX = " UNSIGNED";
44 public static final String SQL_TYPE_NAME_TINYINT_UNSIGNED = "TINYINT" + UNSIGNED_SUFFIX;
45
46 /**
47 * Logger for this class
48 */
49 private static final Logger logger = LoggerFactory.getLogger(MySqlDataTypeFactory.class);
50 /**
51 * Database product names supported.
52 */
53 private static final Collection DATABASE_PRODUCTS = Arrays.asList(new String[] {"mysql"});
54 /**
55 * @see org.dbunit.dataset.datatype.IDbProductRelatable#getValidDbProducts()
56 */
57 public Collection getValidDbProducts()
58 {
59 return DATABASE_PRODUCTS;
60 }
61
62 public DataType createDataType(int sqlType, String sqlTypeName) throws DataTypeException
63 {
64 if(logger.isDebugEnabled())
65 logger.debug("createDataType(sqlType={}, sqlTypeName={}) - start", String.valueOf(sqlType), sqlTypeName);
66
67 if (sqlType == Types.OTHER)
68 {
69 // CLOB
70 if ("longtext".equalsIgnoreCase(sqlTypeName))
71 {
72 return DataType.CLOB;
73 }
74 // MySQL 5.0 Boolean
75 else if("bit".equalsIgnoreCase(sqlTypeName))
76 {
77 return DataType.BOOLEAN;
78 }
79 else if("point".equalsIgnoreCase(sqlTypeName))
80 {
81 return DataType.BINARY;
82 }
83 }
84
85 // Special handling for "TINYINT UNSIGNED"
86 if(SQL_TYPE_NAME_TINYINT_UNSIGNED.equalsIgnoreCase(sqlTypeName)){
87 return DataType.TINYINT; // It is a bit of a waste here - we could better use a "Short" instead of an "Integer" type
88 }
89
90 // If we have an unsigned datatype check for some specialties
91 // See http://dev.mysql.com/doc/refman/5.0/en/connector-j-reference-type-conversions.html
92 if(sqlTypeName.endsWith(UNSIGNED_SUFFIX)) {
93 if(sqlType == Types.INTEGER) {
94 return DataType.BIGINT;
95 }
96 }
97
98 return super.createDataType(sqlType, sqlTypeName);
99 }
100 }