This is an automated email from the ASF dual-hosted git repository. struberg pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/openjpa.git
commit 3b4c2e6f427de1f6f1e21a9c5a0cca4e5730167e Author: Mark Struberg <[email protected]> AuthorDate: Thu Nov 1 14:20:49 2018 +0100 OPENJPA-2755 OPENJPA-2555 use scale to set Date fractions --- .../openjpa/jdbc/schema/DynamicSchemaFactory.java | 2 +- .../apache/openjpa/jdbc/sql/MySQLDictionary.java | 2 +- .../persistence/kernel/TestDateQueries.java | 27 +++++++++++ .../kernel/common/apps/AllFieldTypesTest.java | 53 ++++++++++++++++++++++ 4 files changed, 82 insertions(+), 2 deletions(-) diff --git a/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/schema/DynamicSchemaFactory.java b/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/schema/DynamicSchemaFactory.java index f6b3c92..d0e3129 100644 --- a/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/schema/DynamicSchemaFactory.java +++ b/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/schema/DynamicSchemaFactory.java @@ -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; } diff --git a/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/MySQLDictionary.java b/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/MySQLDictionary.java index 580fa8b..4d061f2 100644 --- a/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/MySQLDictionary.java +++ b/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/sql/MySQLDictionary.java @@ -99,7 +99,7 @@ public class MySQLDictionary supportsSelectStartIndex = true; supportsSelectEndIndex = true; - + datePrecision = MICRO; concatenateFunction = "CONCAT({0},{1})"; diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/kernel/TestDateQueries.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/kernel/TestDateQueries.java index 4ff28de..7677340 100644 --- a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/kernel/TestDateQueries.java +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/kernel/TestDateQueries.java @@ -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()); diff --git a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/kernel/common/apps/AllFieldTypesTest.java b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/kernel/common/apps/AllFieldTypesTest.java index 00bca61..eb99c3c 100644 --- a/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/kernel/common/apps/AllFieldTypesTest.java +++ b/openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/kernel/common/apps/AllFieldTypesTest.java @@ -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();
