[#926] AbstractRecord.into() fails to convert java.sql.Date into java.util.Date

[#928] Add DataTypeException extending DataAccessException in case something went wrong when converting data types
This commit is contained in:
Lukas Eder 2011-11-11 19:59:20 +00:00
parent 942861daaf
commit 3e1d77c6b2
5 changed files with 117 additions and 22 deletions

View File

@ -56,6 +56,9 @@ public class BookWithAnnotations {
@Column(name = "FIRST_NAME")
public String firstName;
@Column(name = "DATE_OF_BIRTH")
public java.util.Date dateOfBirth;
// Members without annotations
// ---------------------------
public int id3;

View File

@ -41,15 +41,17 @@ package org.jooq.test;
*/
public class BookWithoutAnnotations {
public Integer id;
public int id2;
public int ID;
public String title;
public String firstName;
public String firstName2;
public String lastName;
public String lastName2;
public String LAST_NAME;
public Integer id;
public int id2;
public int ID;
public String title;
public String firstName;
public String firstName2;
public String lastName;
public String lastName2;
public String LAST_NAME;
public java.util.Date DATE_OF_BIRTH;
public java.sql.Date dateOfBirth;
public void setId(int id) {
id2 = id;

View File

@ -1642,6 +1642,15 @@ public abstract class jOOQAbstractTest<
assertEquals(new Date(1), SQLDataType.DATE.convert(new Timestamp(1)));
assertEquals(new Time(1), SQLDataType.TIME.convert(new Timestamp(1)));
assertEquals(new Timestamp(1), SQLDataType.TIMESTAMP.convert(new Timestamp(1)));
// [#926] Some additional date conversion checks
A author = create().newRecord(TAuthor());
author.setValue(TAuthor_DATE_OF_BIRTH(), new Date(1));
assertEquals(new Date(1), author.getValue(TAuthor_DATE_OF_BIRTH(), Date.class));
assertEquals(new Time(1), author.getValue(TAuthor_DATE_OF_BIRTH(), Time.class));
assertEquals(new Timestamp(1), author.getValue(TAuthor_DATE_OF_BIRTH(), Timestamp.class));
assertEquals(new java.util.Date(1), author.getValue(TAuthor_DATE_OF_BIRTH(), java.util.Date.class));
}
@Test
@ -2673,7 +2682,8 @@ public abstract class jOOQAbstractTest<
TBook_ID(),
TBook_TITLE(),
TAuthor_FIRST_NAME(),
TAuthor_LAST_NAME())
TAuthor_LAST_NAME(),
TAuthor_DATE_OF_BIRTH())
.from(TBook())
.join(TAuthor()).on(TBook_AUTHOR_ID().equal(TAuthor_ID()))
.orderBy(TBook_ID())
@ -2754,7 +2764,8 @@ public abstract class jOOQAbstractTest<
TBook_ID(),
TBook_TITLE(),
TAuthor_FIRST_NAME(),
TAuthor_LAST_NAME())
TAuthor_LAST_NAME(),
TAuthor_DATE_OF_BIRTH())
.from(TBook())
.join(TAuthor()).on(TBook_AUTHOR_ID().equal(TAuthor_ID()))
.orderBy(TBook_ID())
@ -2824,7 +2835,8 @@ public abstract class jOOQAbstractTest<
TBook_ID(),
TBook_TITLE(),
TAuthor_FIRST_NAME(),
TAuthor_LAST_NAME())
TAuthor_LAST_NAME(),
TAuthor_DATE_OF_BIRTH())
.from(TBook())
.join(TAuthor()).on(TBook_AUTHOR_ID().equal(TAuthor_ID()))
.orderBy(TBook_ID())

View File

@ -0,0 +1,71 @@
/**
* Copyright (c) 2009-2011, Lukas Eder, lukas.eder@gmail.com
* All rights reserved.
*
* This software is licensed to you under the Apache License, Version 2.0
* (the "License"); You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* . Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* . Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* . Neither the name "jOOQ" nor the names of its contributors may be
* used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
package org.jooq.exception;
/**
* An error occurred while handling data types. This typically happens when
* converting types
*
* @author Lukas Eder
*/
public class DataTypeException extends DataAccessException {
/**
* Generated UID
*/
private static final long serialVersionUID = -6460945824599280420L;
/**
* Constructor for DataAccessException.
*
* @param message the detail message
*/
public DataTypeException(String message) {
super(message);
}
/**
* Constructor for DataAccessException.
*
* @param message the detail message
* @param cause the root cause (usually from using a underlying data access
* API such as JDBC)
*/
public DataTypeException(String message, Throwable cause) {
super(message, cause);
}
}

View File

@ -47,7 +47,7 @@ import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.jooq.exception.SQLDialectNotSupportedException;
import org.jooq.exception.DataTypeException;
/**
* Utility methods for type conversions
@ -234,18 +234,25 @@ final class TypeUtils {
}
// Date types can be converted among each other
else if (toClass == Date.class && java.util.Date.class.isAssignableFrom(fromClass)) {
return (T) new Date(((java.util.Date) from).getTime());
}
else if (toClass == Time.class && java.util.Date.class.isAssignableFrom(fromClass)) {
return (T) new Time(((java.util.Date) from).getTime());
}
else if (toClass == Timestamp.class && java.util.Date.class.isAssignableFrom(fromClass)) {
return (T) new Timestamp(((java.util.Date) from).getTime());
else if (java.util.Date.class.isAssignableFrom(fromClass)) {
long time = ((java.util.Date) from).getTime();
if (toClass == Date.class) {
return (T) new Date(time);
}
else if (toClass == Time.class) {
return (T) new Time(time);
}
else if (toClass == Timestamp.class) {
return (T) new Timestamp(time);
}
else if (toClass == java.util.Date.class) {
return (T) new java.util.Date(time);
}
}
}
throw new SQLDialectNotSupportedException("Cannot convert from " + from + " to " + toClass);
throw new DataTypeException("Cannot convert from " + from + " to " + toClass);
}
/**