OPENJPA-2755 OPENJPA-2555 use scale to set Date fractions

This commit is contained in:
Mark Struberg 2018-11-01 14:20:49 +01:00
parent 9461ffdfcd
commit 3b4c2e6f42
4 changed files with 82 additions and 2 deletions

View File

@ -251,7 +251,7 @@ public class DynamicSchemaFactory
setSize(size);
if (typeName != null)
setTypeIdentifier(DBIdentifier.newColumnDefinition(typeName));
if (decimals >= 0)
if (decimals != 0)
setDecimalDigits(decimals);
return true;
}

View File

@ -99,7 +99,7 @@ public class MySQLDictionary
supportsSelectStartIndex = true;
supportsSelectEndIndex = true;
datePrecision = MICRO;
concatenateFunction = "CONCAT({0},{1})";

View File

@ -27,6 +27,7 @@
*/
package org.apache.openjpa.persistence.kernel;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Date;
@ -71,6 +72,15 @@ public class TestDateQueries extends BaseKernelTest {
startTx(_pm);
AllFieldTypesTest test = new AllFieldTypesTest();
test.setTestDate(_date);
// prepare scale test fields
Timestamp tst = new Timestamp(1000000000L);
tst.setNanos(123456789);
test.setTestDateMaxScale(tst);
test.setTestDateScale0(tst);
test.setTestDateScale3(tst);
test.setTestDateScale6(tst);
_pm.persist(test);
test = new AllFieldTypesTest();
@ -81,6 +91,8 @@ public class TestDateQueries extends BaseKernelTest {
test.setTestDate(_after);
_pm.persist(test);
endTx(_pm);
_pm.clear();
}
public void testEquals() {
@ -95,6 +107,21 @@ public class TestDateQueries extends BaseKernelTest {
assertEquals(2, vals.size());
}
public void testDateScale() {
Timestamp referenceTst = new Timestamp(1000000000L);
Collection vals = executeQuery("testDate = :date");
AllFieldTypesTest aft = (AllFieldTypesTest) vals.iterator().next();
assertNotNull(aft);
long time = aft.getTestDateMaxScale().getTime();
long nanos = aft.getTestDateMaxScale().getNanos();
// cut of the ms
assertEquals(referenceTst, time - (time%1000));
}
public void testBefore() {
Collection vals = executeQuery("testDate < :date");
assertEquals(1, vals.size());

View File

@ -20,10 +20,12 @@ package org.apache.openjpa.persistence.kernel.common.apps;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.sql.Timestamp;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
@ -55,10 +57,28 @@ public class AllFieldTypesTest {
private char testchar;
private String testString;
private String testBigString;
@Temporal(TemporalType.DATE)
private Date testDate;
@Temporal(TemporalType.DATE)
private Calendar testCalendar;
@Temporal(TemporalType.TIMESTAMP)
@Column(scale=-1)
private Timestamp testDateScale0;
@Temporal(TemporalType.TIMESTAMP)
@Column(scale=3)
private Timestamp testDateScale3;
@Temporal(TemporalType.TIMESTAMP)
@Column(scale=6)
private Timestamp testDateScale6;
@Temporal(TemporalType.TIMESTAMP)
private Timestamp testDateMaxScale;
private Object testObject;
private BigInteger testBigInteger;
private BigDecimal testBigDecimal;
@ -188,6 +208,39 @@ public class AllFieldTypesTest {
this.testBigDecimal = testBigDecimal;
}
public Timestamp getTestDateScale0() {
return testDateScale0;
}
public void setTestDateScale0(Timestamp testDateScale0) {
this.testDateScale0 = testDateScale0;
}
public Timestamp getTestDateScale3() {
return testDateScale3;
}
public void setTestDateScale3(Timestamp testDateScale3) {
this.testDateScale3 = testDateScale3;
}
public Timestamp getTestDateScale6() {
return testDateScale6;
}
public void setTestDateScale6(Timestamp testDateScale6) {
this.testDateScale6 = testDateScale6;
}
public Timestamp getTestDateMaxScale() {
return testDateMaxScale;
}
public void setTestDateMaxScale(Timestamp testDateMaxScale) {
this.testDateMaxScale = testDateMaxScale;
}
public void randomize(boolean objects, boolean blobs) {
testint = AbstractTestCase.randomInt().intValue();
testlong = AbstractTestCase.randomLong().longValue();