db-torque-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From tfisc...@apache.org
Subject svn commit: r1333900 - in /db/torque/torque4/trunk: torque-templates/src/main/java/org/apache/torque/templates/transformer/om/ torque-test/src/main/schema/ torque-test/src/test/java/org/apache/torque/generated/peer/
Date Fri, 04 May 2012 12:16:20 GMT
Author: tfischer
Date: Fri May  4 12:16:19 2012
New Revision: 1333900

URL: http://svn.apache.org/viewvc?rev=1333900&view=rev
Log:
TORQUE-142: Use current java date for CURRENT_DATE, CURRENT_TIME and CURRENT_TIMESTAMP default
values if useDatabaseDefaultValue is false

Added:
    db/torque/torque4/trunk/torque-test/src/test/java/org/apache/torque/generated/peer/DefaultValueTest.java
Modified:
    db/torque/torque4/trunk/torque-templates/src/main/java/org/apache/torque/templates/transformer/om/OMColumnTransformer.java
    db/torque/torque4/trunk/torque-test/src/main/schema/default-value-schema.xml

Modified: db/torque/torque4/trunk/torque-templates/src/main/java/org/apache/torque/templates/transformer/om/OMColumnTransformer.java
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-templates/src/main/java/org/apache/torque/templates/transformer/om/OMColumnTransformer.java?rev=1333900&r1=1333899&r2=1333900&view=diff
==============================================================================
--- db/torque/torque4/trunk/torque-templates/src/main/java/org/apache/torque/templates/transformer/om/OMColumnTransformer.java
(original)
+++ db/torque/torque4/trunk/torque-templates/src/main/java/org/apache/torque/templates/transformer/om/OMColumnTransformer.java
Fri May  4 12:16:19 2012
@@ -603,8 +603,11 @@ public class OMColumnTransformer
         String fieldDefaultValue;
         if (defaultAttributeValue != null)
         {
+            boolean useDatabaseDefaultValue = "true".equals(
+                    columnElement.getAttribute(
+                        TorqueSchemaAttributeName.USE_DATABASE_DEFAULT_VALUE));
             fieldDefaultValue = getDefaultValueWithDefaultSet(
-                    javaType, defaultAttributeValue);
+                    javaType, defaultAttributeValue, useDatabaseDefaultValue);
         }
         else
         {
@@ -621,12 +624,15 @@ public class OMColumnTransformer
      *
      * @param javaType The java type of the column.
      * @param defaultValue The default value from the schema.
+     * @param useDatabaseDefaultValue whether the database default value should
+     *        be used.
      *
      * @return The java default value.
      */
     protected String getDefaultValueWithDefaultSet(
             JavaType javaType,
-            String defaultValue)
+            String defaultValue,
+            boolean useDatabaseDefaultValue)
     {
         boolean primitiveFieldType = javaType.isPrimitive();
         String fieldDefaultValue;
@@ -686,7 +692,19 @@ public class OMColumnTransformer
                     || "CURRENT_TIME".equalsIgnoreCase(defaultValue)
                     || "CURRENT_TIMESTAMP".equalsIgnoreCase(defaultValue))
             {
-                fieldDefaultValue = "null";
+                if (useDatabaseDefaultValue)
+                {
+                    // if the database default value is used do not use
+                    // current time in java as it might be different
+                    fieldDefaultValue = "null";
+                }
+                else
+                {
+                    // the database does not provide a default so use
+                    // java current time.
+                    fieldDefaultValue = "new " + javaType.getFullClassName()
+                            + "()";
+                }
             }
             else
             {

Modified: db/torque/torque4/trunk/torque-test/src/main/schema/default-value-schema.xml
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-test/src/main/schema/default-value-schema.xml?rev=1333900&r1=1333899&r2=1333900&view=diff
==============================================================================
--- db/torque/torque4/trunk/torque-test/src/main/schema/default-value-schema.xml (original)
+++ db/torque/torque4/trunk/torque-test/src/main/schema/default-value-schema.xml Fri May 
4 12:16:19 2012
@@ -70,7 +70,7 @@
   </table>
 
   <table name="CURRENT_DATE_TABLE" 
-       description="Table to test the CURRENT_DATE function">
+       description="Table which has a column which uses CURRENT_DATE from the database as
default">
     <column
       name="ID"
       required="true"
@@ -134,4 +134,29 @@
     />
   </table>
 
+  <table name="JAVA_DEFAULT_TABLE" 
+       description="Table to test java default values">
+    <column
+      name="ID"
+      required="true"
+      primaryKey="true"
+      type="INTEGER"
+    />
+    <column
+      name="CURRENT_DATE_VALUE"
+      type="DATE"
+      default="CURRENT_DATE"
+    />
+    <column
+      name="INTEGER_VALUE"
+      type="INTEGER"
+      default="27"
+    />
+    <column
+      name="VARCHAR_VALUE"
+      type="VARCHAR"
+      size="20"
+      default="Default"
+    />
+  </table>
 </database>

Added: db/torque/torque4/trunk/torque-test/src/test/java/org/apache/torque/generated/peer/DefaultValueTest.java
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-test/src/test/java/org/apache/torque/generated/peer/DefaultValueTest.java?rev=1333900&view=auto
==============================================================================
--- db/torque/torque4/trunk/torque-test/src/test/java/org/apache/torque/generated/peer/DefaultValueTest.java
(added)
+++ db/torque/torque4/trunk/torque-test/src/test/java/org/apache/torque/generated/peer/DefaultValueTest.java
Fri May  4 12:16:19 2012
@@ -0,0 +1,84 @@
+package org.apache.torque.generated.peer;
+
+import java.util.Date;
+
+import org.apache.torque.BaseDatabaseTestCase;
+import org.apache.torque.test.JavaDefaultTable;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+/**
+ * Tests that the default attribute works for columns.
+ *
+ * @version $Id: UseDefaultValueFromDatabaseTest.java 1333015 2012-05-02 12:36:03Z tfischer
$
+ */
+public class DefaultValueTest extends BaseDatabaseTestCase
+{
+    /**
+     * Checks that if CURRENT_DATE is used as default value
+     * then an object is constructed with current java time.
+     *
+     * @throws Exception if an error occurs.
+     */
+    public void testCurrentDateAsJavaDefault() throws Exception
+    {
+        Date currentDateBefore = new Date();
+        JavaDefaultTable javaDefaultTable
+                = new JavaDefaultTable();
+        Date currentDateAfter = new Date();
+
+        assertFalse(
+                "currentDate should be >= currentDateBefore",
+                javaDefaultTable.getCurrentDateValue().before(
+                        currentDateBefore));
+        assertFalse(
+                "currentDate should be <= currentDateAfter",
+                javaDefaultTable.getCurrentDateValue().after(
+                        currentDateAfter));
+    }
+
+    /**
+     * Checks that we can set a default value to an Integer column.
+     *
+     * @throws Exception if an error occurs.
+     */
+    public void testIntegerDefault() throws Exception
+    {
+        JavaDefaultTable javaDefaultTable
+                = new JavaDefaultTable();
+
+        assertEquals(27, javaDefaultTable.getIntegerValue());
+    }
+
+    /**
+     * Checks that we can set a default value to an Varchar column.
+     *
+     * @throws Exception if an error occurs.
+     */
+    public void testVarcharDefault() throws Exception
+    {
+        JavaDefaultTable javaDefaultTable
+                = new JavaDefaultTable();
+
+        assertEquals("Default", javaDefaultTable.getVarcharValue());
+    }
+
+
+}



---------------------------------------------------------------------
To unsubscribe, e-mail: torque-dev-unsubscribe@db.apache.org
For additional commands, e-mail: torque-dev-help@db.apache.org


Mime
View raw message