[#930] Support converting date time types to java.util.Calendar

This commit is contained in:
Lukas Eder 2011-11-11 21:14:33 +00:00
parent 3e1d77c6b2
commit c2ca42f5de
4 changed files with 121 additions and 0 deletions

View File

@ -0,0 +1,87 @@
/**
* 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.test;
import java.util.Calendar;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
/**
* @author Lukas Eder
*/
@Entity
public class DatesWithAnnotations {
@Column(name = "DATE_OF_BIRTH")
public Calendar calOfBirth;
public Calendar cal1;
public Calendar cal2;
@Column(name = "DATE_OF_BIRTH")
public Date dateOfBirth;
public Date date1;
public Date date2;
@Column(name = "DATE_OF_BIRTH")
public void setCal(Calendar calendar) {
cal1 = calendar;
}
public void setC(Calendar calendar) {
cal2 = calendar;
}
@Column(name = "DATE_OF_BIRTH")
public Calendar getC() {
return cal2;
}
@Column(name = "DATE_OF_BIRTH")
public void setDate(Date ated) {
date1 = ated;
}
public void setD(Date date) {
date2 = date;
}
@Column(name = "DATE_OF_BIRTH")
public Date getD() {
return date2;
}
}

View File

@ -71,6 +71,7 @@ import java.sql.Timestamp;
import java.util.AbstractList;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
@ -1647,10 +1648,14 @@ public abstract class jOOQAbstractTest<
A author = create().newRecord(TAuthor());
author.setValue(TAuthor_DATE_OF_BIRTH(), new Date(1));
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(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));
assertEquals(calendar, author.getValue(TAuthor_DATE_OF_BIRTH(), Calendar.class));
}
@Test
@ -2746,6 +2751,23 @@ public abstract class jOOQAbstractTest<
fail();
}
catch (FetchIntoException expected) {}
// [#930] Calendar/Date conversion checks
List<DatesWithAnnotations> calendars =
create().select(TAuthor_DATE_OF_BIRTH())
.from(TAuthor())
.orderBy(TAuthor_ID())
.fetchInto(DatesWithAnnotations.class);
assertEquals(2, calendars.size());
for (int index : asList(0, 1)) {
assertEquals(calendars.get(index).cal1, calendars.get(index).cal2);
assertEquals(calendars.get(index).cal1, calendars.get(index).calOfBirth);
assertEquals(calendars.get(index).date1, calendars.get(index).date2);
assertEquals(calendars.get(index).date1, calendars.get(index).dateOfBirth);
assertEquals(calendars.get(index).cal1.getTime(), calendars.get(index).date1);
}
}
@Test

View File

@ -46,6 +46,7 @@ import java.util.Collection;
import java.util.List;
import javax.persistence.Column;
import javax.persistence.Entity;
import org.jooq.ArrayRecord;
import org.jooq.Configuration;
@ -332,6 +333,11 @@ final class JooqUtil {
* or methods
*/
static final boolean hasColumnAnnotations(Class<?> type) {
// An entity usually has @Column annotations, too
if (type.getAnnotation(Entity.class) != null) {
return true;
}
for (java.lang.reflect.Field member : type.getFields()) {
if (member.getAnnotation(Column.class) != null) {
return true;

View File

@ -43,6 +43,7 @@ import java.sql.Time;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
@ -249,6 +250,11 @@ final class TypeUtils {
else if (toClass == java.util.Date.class) {
return (T) new java.util.Date(time);
}
else if (toClass == Calendar.class) {
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(time);
return (T) calendar;
}
}
}