OPENJPA-2849 proper handling of different Date types

This commit is contained in:
Mark Struberg 2021-05-07 23:07:41 +02:00
parent 477d73a996
commit 25af7c35ee
3 changed files with 211 additions and 160 deletions

View File

@ -132,6 +132,11 @@ abstract class UnaryOp
throws SQLException { throws SQLException {
Class<?> type = getType(); Class<?> type = getType();
int typeCode = type != null ? JavaTypes.getTypeCode(type) : JavaSQLTypes.JDBC_DEFAULT; int typeCode = type != null ? JavaTypes.getTypeCode(type) : JavaSQLTypes.JDBC_DEFAULT;
if (typeCode == JavaTypes.DATE) {
// further clarify which date exactly
typeCode = JavaSQLTypes.getDateTypeCode(type);
}
Object value = res.getObject(this, typeCode, null); Object value = res.getObject(this, typeCode, null);
if (value == null) { if (value == null) {
if (nullableValue(ctx, state)) { // OPENJPA-1794 if (nullableValue(ctx, state)) { // OPENJPA-1794

View File

@ -18,6 +18,8 @@
*/ */
package org.apache.openjpa.jira1794; package org.apache.openjpa.jira1794;
import java.util.Date;
import javax.persistence.Entity; import javax.persistence.Entity;
import javax.persistence.GeneratedValue; import javax.persistence.GeneratedValue;
import javax.persistence.Id; import javax.persistence.Id;
@ -48,6 +50,10 @@ public class AggEntity {
private String stringVal; private String stringVal;
private java.util.Date utilDate;
private java.sql.Date sqlDate;
public void setId(int id) { public void setId(int id) {
this.id = id; this.id = id;
} }
@ -144,6 +150,22 @@ public class AggEntity {
return stringVal; return stringVal;
} }
public Date getUtilDate() {
return utilDate;
}
public void setUtilDate(Date utilDate) {
this.utilDate = utilDate;
}
public java.sql.Date getSqlDate() {
return sqlDate;
}
public void setSqlDate(java.sql.Date sqlDate) {
this.sqlDate = sqlDate;
}
public void init() { public void init() {
setPshortVal((short) 1); setPshortVal((short) 1);
setShortVal((short) 1); setShortVal((short) 1);
@ -156,5 +178,7 @@ public class AggEntity {
setFloatVal(1f); setFloatVal(1f);
setPfloatVal(1f); setPfloatVal(1f);
setStringVal("1"); setStringVal("1");
setUtilDate(new java.util.Date());
setSqlDate(new java.sql.Date(getUtilDate().getTime()));
} }
} }

View File

@ -75,6 +75,9 @@ public class TestAggregateFunctions extends SingleEMFTestCase {
verifyResult(em, stringAggregateFunctions, verifyResult(em, stringAggregateFunctions,
new String[] { "ae.stringVal" }, true, true); new String[] { "ae.stringVal" }, true, true);
verifyResult(em, stringAggregateFunctions,
new String[]{"ae.utilDate", "ae.sqlDate"}, true, true);
// Add a row to the table and re-test // Add a row to the table and re-test
AggEntity ae = new AggEntity(); AggEntity ae = new AggEntity();
ae.init(); ae.init();
@ -82,6 +85,8 @@ public class TestAggregateFunctions extends SingleEMFTestCase {
em.persist(ae); em.persist(ae);
em.getTransaction().commit(); em.getTransaction().commit();
verifyResult(em, stringAggregateFunctions, "ae.sqlDate", java.sql.Date.class);
// Verify all numeric types for all aggregate functions return a // Verify all numeric types for all aggregate functions return a
// non-null value when there is a query result // non-null value when there is a query result
verifyResult(em, numericAggregateFunctions, numericAttributes, false); verifyResult(em, numericAggregateFunctions, numericAttributes, false);
@ -90,6 +95,8 @@ public class TestAggregateFunctions extends SingleEMFTestCase {
verifyResult(em, stringAggregateFunctions, verifyResult(em, stringAggregateFunctions,
new String[] { "ae.stringVal" }, false); new String[] { "ae.stringVal" }, false);
verifyResult(em, stringAggregateFunctions, "ae.utilDate", java.util.Date.class);
em.close(); em.close();
} }
@ -241,6 +248,21 @@ public class TestAggregateFunctions extends SingleEMFTestCase {
} }
} }
private <T> void verifyResult(EntityManager em, String[] aggregates,
String attr, Class<T> expectedType) {
for (String func : aggregates) {
// JPQL with aggregate and aggregate in subselect
String sql = "SELECT " + func + "(" + attr + ")"
+ " FROM AggEntity ae WHERE " + attr + " <= "
+ "(SELECT " + func + "("
+ attr.replaceFirst("^ae.", "ae2.")
+ ") FROM AggEntity ae2)";
TypedQuery<T> q = em.createQuery(sql, expectedType);
T intance = q.getSingleResult();
assertEquals(intance.getClass(), expectedType);
}
}
private void verifyQueryResult(Query q, boolean emptyRs) { private void verifyQueryResult(Query q, boolean emptyRs) {
verifyQueryResult(q, emptyRs, false); verifyQueryResult(q, emptyRs, false);
} }