db-torque-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From tfisc...@apache.org
Subject svn commit: r1333015 - in /db/torque/torque4/trunk: torque-templates/src/main/java/org/apache/torque/templates/transformer/om/ torque-templates/src/main/java/org/apache/torque/templates/transformer/sql/ torque-templates/src/main/resources/org/apache/to...
Date Wed, 02 May 2012 12:36:03 GMT
Author: tfischer
Date: Wed May  2 12:36:03 2012
New Revision: 1333015

URL: http://svn.apache.org/viewvc?rev=1333015&view=rev
Log:
TORQUE-142 honor default value CURRENT_DATE, CURRENT_TIME and CURRENT_TIMESTAMP for dates

Modified:
    db/torque/torque4/trunk/torque-templates/src/main/java/org/apache/torque/templates/transformer/om/OMColumnTransformer.java
    db/torque/torque4/trunk/torque-templates/src/main/java/org/apache/torque/templates/transformer/sql/SQLTransformer.java
    db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/peer/impl/base/buildColumnValues.vm
    db/torque/torque4/trunk/torque-test/src/main/schema/default-value-schema.xml
    db/torque/torque4/trunk/torque-test/src/main/schema/test-schema.xml
    db/torque/torque4/trunk/torque-test/src/main/schema/types-schema.xml
    db/torque/torque4/trunk/torque-test/src/test/java/org/apache/torque/generated/peer/DateTest.java
    db/torque/torque4/trunk/torque-test/src/test/java/org/apache/torque/generated/peer/UseDefaultValueFromDatabaseTest.java

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=1333015&r1=1333014&r2=1333015&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
Wed May  2 12:36:03 2012
@@ -608,6 +608,20 @@ public class OMColumnTransformer
             {
                 fieldDefaultValue = "Long.valueOf(" + defaultValue + "L)";
             }
+            else if (JavaType.DATE == javaType)
+            {
+                if ("CURRENT_DATE".equalsIgnoreCase(defaultValue)
+                        || "CURRENT_TIME".equalsIgnoreCase(defaultValue)
+                        || "CURRENT_TIMESTAMP".equalsIgnoreCase(defaultValue))
+                {
+                    fieldDefaultValue = "null";
+                }
+                else
+                {
+                    fieldDefaultValue = "new " + javaType.getFullClassName()
+                            + "(" + defaultValue + ")";
+                }
+            }
             else if (primitiveFieldType)
             {
                 fieldDefaultValue = defaultValue;

Modified: db/torque/torque4/trunk/torque-templates/src/main/java/org/apache/torque/templates/transformer/sql/SQLTransformer.java
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-templates/src/main/java/org/apache/torque/templates/transformer/sql/SQLTransformer.java?rev=1333015&r1=1333014&r2=1333015&view=diff
==============================================================================
--- db/torque/torque4/trunk/torque-templates/src/main/java/org/apache/torque/templates/transformer/sql/SQLTransformer.java
(original)
+++ db/torque/torque4/trunk/torque-templates/src/main/java/org/apache/torque/templates/transformer/sql/SQLTransformer.java
Wed May  2 12:36:03 2012
@@ -297,7 +297,14 @@ public class SQLTransformer implements S
         {
             resultList.add("default");
 
-            if (TypeMap.isTextType(schemaType))
+            if ((SchemaType.DATE == schemaType
+                    || SchemaType.TIME == schemaType
+                    || SchemaType.TIMESTAMP == schemaType)
+                 && sqlType.getDefaultValue().startsWith("CURRENT_"))
+            {
+                resultList.add(sqlType.getDefaultValue());
+            }
+            else if (TypeMap.isTextType(schemaType))
             {
                 resultList.add(platform.quoteAndEscape(
                         sqlType.getDefaultValue()));

Modified: db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/peer/impl/base/buildColumnValues.vm
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/peer/impl/base/buildColumnValues.vm?rev=1333015&r1=1333014&r2=1333015&view=diff
==============================================================================
--- db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/peer/impl/base/buildColumnValues.vm
(original)
+++ db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/peer/impl/base/buildColumnValues.vm
Wed May  2 12:36:03 2012
@@ -57,9 +57,11 @@
     #if ("boolean" == $columnFieldType)
         if (!${field}.isNew())
     #elseif ("true" == $primitive || $defaultValue == "null")
-        if (!${field}.isNew() || ${field}.$getter() != $defaultValue)
+        if (!${field}.isNew() 
+            || ${field}.$getter() != $defaultValue)
     #else
-        if (!${field}.isNew() || !${defaultValue}.equals(${field}.$getter()))
+        if (!${field}.isNew() 
+            || !${defaultValue}.equals(${field}.$getter()))
     #end
         {
     #if ($columnType == "DATE" && $columnFieldType == "java.util.Date")

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=1333015&r1=1333014&r2=1333015&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 Wed May 
2 12:36:03 2012
@@ -21,18 +21,18 @@
 <database name="@DATABASE_DEFAULT@"
     defaultIdMethod="@DATABASE_ID_METHOD@"
     package="org.apache.torque.test.ext"
-    xmlns="http://db.apache.org/torque/4.0/templates/database"
-    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
-    xsi:schemaLocation="http://db.apache.org/torque/4.0/templates/database 
-        http://db.apache.org/torque/4.0/templates/database-strict.xsd">
+    xmlns="http://db.apache.org/torque/4.0/templates/database">
 
   <table name="default_values">
+  
+    <domain name="myTimestamp" type="TIMESTAMP" />
     <column
       name="id"
       required="true"
       primaryKey="true"
       type="INTEGER"
     />
+    <!-- attribute defaultValue is the java default  -->
     <column
       name="o_integer_default_from_db"
       required="true"
@@ -40,6 +40,7 @@
       javaType="object"
       useDatabaseDefaultValue="true"
       default="2"
+      defaultValue="null"
       description="Object integer field which uses the default value from the database."
     />
     <column
@@ -57,6 +58,7 @@
       javaType="primitive"
       useDatabaseDefaultValue="true"
       default="4"
+      defaultValue="0"
     />
     <column
       name="p_int_with_default"
@@ -66,4 +68,72 @@
       default="5"
     />
   </table>
+
+  <table name="CURRENT_DATE_TABLE" 
+       description="Table to test the CURRENT_DATE function">
+    <column
+      name="ID"
+      required="true"
+      primaryKey="true"
+      type="INTEGER"
+    />
+    <column
+      name="PAYLOAD"
+      required="false"
+      type="INTEGER"
+    />
+    <column
+      name="CURRENT_DATE_VALUE"
+      required="true"
+      type="DATE"
+      default="CURRENT_DATE"
+      useDatabaseDefaultValue="true"
+    />
+  </table>
+
+  <table name="CURRENT_TIME_TABLE" 
+       description="Table to test the CURRENT_TIME function">
+    <column
+      name="ID"
+      required="true"
+      primaryKey="true"
+      type="INTEGER"
+    />
+    <column
+      name="PAYLOAD"
+      required="false"
+      type="INTEGER"
+    />
+    <column
+      name="CURRENT_TIME_VALUE"
+      required="true"
+      type="TIME"
+      default="CURRENT_TIME"
+      useDatabaseDefaultValue="true"
+    />
+  </table>
+
+  <table name="CURRENT_TIMESTAMP_TABLE" 
+       description="Table to test the CURRENT_TIMESTAMP function">
+    <column
+      name="ID"
+      required="true"
+      primaryKey="true"
+      type="INTEGER"
+    />
+    <column
+      name="PAYLOAD"
+      required="false"
+      type="INTEGER"
+    />
+    <column
+      name="CURRENT_TIMESTAMP_VALUE"
+      required="true"
+      type="TIMESTAMP"
+      domain="myTimestamp"
+      default="CURRENT_TIMESTAMP"
+      useDatabaseDefaultValue="true"
+    />
+  </table>
+
 </database>

Modified: db/torque/torque4/trunk/torque-test/src/main/schema/test-schema.xml
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-test/src/main/schema/test-schema.xml?rev=1333015&r1=1333014&r2=1333015&view=diff
==============================================================================
--- db/torque/torque4/trunk/torque-test/src/main/schema/test-schema.xml (original)
+++ db/torque/torque4/trunk/torque-test/src/main/schema/test-schema.xml Wed May  2 12:36:03
2012
@@ -196,32 +196,6 @@
     <column name="name" size="100" type="VARCHAR"/>
   </table>
 
-  <!-- TRQS263 -->
-  <table name="DATE_TIME_TIMESTAMP" description="Table to test Date formats">
-    <column
-      name="DATE_TEST_ID"
-      required="true"
-      primaryKey="true"
-      type="INTEGER"
-    />
-    <column
-      name="DATE_VALUE"
-      required="true"
-      type="DATE"
-    />
-    <column
-      name="TIME_VALUE"
-      required="true"
-      type="TIME"
-    />
-    <column
-      name="TIMESTAMP_VALUE"
-      required="true"
-      type="TIMESTAMP"
-    />
-  </table>
-
-
   <table name="INHERITANCE_TEST" description="Table to test inheritance with mapped keys">
     <column
       name="INHERITANCE_TEST"

Modified: db/torque/torque4/trunk/torque-test/src/main/schema/types-schema.xml
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-test/src/main/schema/types-schema.xml?rev=1333015&r1=1333014&r2=1333015&view=diff
==============================================================================
--- db/torque/torque4/trunk/torque-test/src/main/schema/types-schema.xml (original)
+++ db/torque/torque4/trunk/torque-test/src/main/schema/types-schema.xml Wed May  2 12:36:03
2012
@@ -49,4 +49,28 @@
     <column name="bchar_object_value" type="BOOLEANCHAR" javaType="object"/>
   </table>
 
+  <table name="DATE_TIME_TIMESTAMP" description="Table to test Date formats">
+    <column
+      name="ID"
+      required="true"
+      primaryKey="true"
+      type="INTEGER"
+    />
+    <column
+      name="DATE_VALUE"
+      required="true"
+      type="DATE"
+    />
+    <column
+      name="TIME_VALUE"
+      required="true"
+      type="TIME"
+    />
+    <column
+      name="TIMESTAMP_VALUE"
+      required="true"
+      type="TIMESTAMP"
+    />
+  </table>
+
 </database>

Modified: db/torque/torque4/trunk/torque-test/src/test/java/org/apache/torque/generated/peer/DateTest.java
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-test/src/test/java/org/apache/torque/generated/peer/DateTest.java?rev=1333015&r1=1333014&r2=1333015&view=diff
==============================================================================
--- db/torque/torque4/trunk/torque-test/src/test/java/org/apache/torque/generated/peer/DateTest.java
(original)
+++ db/torque/torque4/trunk/torque-test/src/test/java/org/apache/torque/generated/peer/DateTest.java
Wed May  2 12:36:03 2012
@@ -183,9 +183,7 @@ public class DateTest extends BaseDataba
     }
 
     /**
-     * Checks that Criteria.CURRENT_DATE works (i.e. does not throw
-     * an exception).
-     * Other behaviour is difficult to test as database implementations differ.
+     * Checks that Criteria.CURRENT_DATE is larger than a past date value.
      *
      * @throws TorqueException if a problem occurs.
      */
@@ -198,15 +196,19 @@ public class DateTest extends BaseDataba
                     + "is not supported by MSSQL");
             return;
         }
-        Criteria c = new Criteria().where(
-                DateTimeTimestampPeer.DATE_VALUE, Criteria.CURRENT_DATE);
-        DateTimeTimestampPeer.doSelect(c);
+        fillDateTimeTimestampWithPastEntry();
+
+        Criteria criteria = new Criteria().where(
+                DateTimeTimestampPeer.DATE_VALUE,
+                Criteria.CURRENT_DATE,
+                Criteria.GREATER_EQUAL);
+        List<DateTimeTimestamp> result
+                = DateTimeTimestampPeer.doSelect(criteria);
+        assertEquals(0, result.size());
     }
 
     /**
-     * Checks that Criteria.CURRENT_TIME works (i.e. does not throw
-     * an exception).
-     * Other behaviour is difficult to test as database implementations differ.
+     * Checks that Criteria.CURRENT_TIME is larger than a past date value.
      *
      * @throws TorqueException if a problem occurs.
      */
@@ -220,9 +222,39 @@ public class DateTest extends BaseDataba
                     + "is not supported by MSSQL and Oracle");
             return;
         }
-        Criteria c = new Criteria().where(
-                DateTimeTimestampPeer.TIME_VALUE, Criteria.CURRENT_TIME);
-        DateTimeTimestampPeer.doSelect(c);
+        fillDateTimeTimestampWithPastEntry();
+        Criteria criteria = new Criteria().where(
+                DateTimeTimestampPeer.TIME_VALUE,
+                Criteria.CURRENT_TIME,
+                Criteria.GREATER_EQUAL);
+        List<DateTimeTimestamp> result
+                = DateTimeTimestampPeer.doSelect(criteria);
+        assertEquals(0, result.size());
+    }
+
+    /**
+     * Checks that Criteria.CURRENT_TIMESTAMP is larger than a past date value.
+     *
+     * @throws TorqueException if a problem occurs.
+     */
+    public void testCurrentTimestamp() throws TorqueException
+    {
+        if (defaultAdapter instanceof DBMSSQL)
+        {
+            log.warn("testCurrentTimestamp(): "
+                    + Criteria.CURRENT_TIMESTAMP
+                    + "is not supported by MSSQL");
+            return;
+        }
+        fillDateTimeTimestampWithPastEntry();
+
+        Criteria criteria = new Criteria().where(
+                DateTimeTimestampPeer.TIMESTAMP_VALUE,
+                Criteria.CURRENT_TIMESTAMP,
+                Criteria.GREATER_EQUAL);
+        List<DateTimeTimestamp> result
+                = DateTimeTimestampPeer.doSelect(criteria);
+        assertEquals(0, result.size());
     }
 
     /**
@@ -502,12 +534,7 @@ public class DateTest extends BaseDataba
      */
     private void cleanDateTimeTimestampTable() throws TorqueException
     {
-        Criteria criteria = new Criteria();
-        criteria.where(
-                DateTimeTimestampPeer.DATE_TEST_ID,
-                (Long) null,
-                Criteria.NOT_EQUAL);
-        DateTimeTimestampPeer.doDelete(criteria);
+        DateTimeTimestampPeer.doDelete(new Criteria());
     }
 
     private boolean timestampHasMillisecondAccuracy()
@@ -524,4 +551,23 @@ public class DateTest extends BaseDataba
         }
         return true;
     }
+
+    /**
+     * Creates exactly one entry in the DATE_TIME_TIMESTAMP table which
+     * has dates in the past.
+     *
+     * @throws TorqueException if the data cannot be deleted or saved.
+     */
+    private void fillDateTimeTimestampWithPastEntry() throws TorqueException
+    {
+        DateTimeTimestampPeer.doDelete(new Criteria());
+        DateTimeTimestamp dateTimeTimestamp = new DateTimeTimestamp();
+        dateTimeTimestamp.setDateValue(
+                new GregorianCalendar(2010, 1, 1).getTime());
+        dateTimeTimestamp.setTimeValue(
+                new GregorianCalendar(2010, 1, 1).getTime());
+        dateTimeTimestamp.setTimestampValue(
+                new GregorianCalendar(2010, 1, 1).getTime());
+        dateTimeTimestamp.save();
+    }
 }

Modified: db/torque/torque4/trunk/torque-test/src/test/java/org/apache/torque/generated/peer/UseDefaultValueFromDatabaseTest.java
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-test/src/test/java/org/apache/torque/generated/peer/UseDefaultValueFromDatabaseTest.java?rev=1333015&r1=1333014&r2=1333015&view=diff
==============================================================================
--- db/torque/torque4/trunk/torque-test/src/test/java/org/apache/torque/generated/peer/UseDefaultValueFromDatabaseTest.java
(original)
+++ db/torque/torque4/trunk/torque-test/src/test/java/org/apache/torque/generated/peer/UseDefaultValueFromDatabaseTest.java
Wed May  2 12:36:03 2012
@@ -1,12 +1,32 @@
 package org.apache.torque.generated.peer;
 
-import java.sql.Types;
+import java.sql.Connection;
+import java.sql.ResultSet;
+import java.sql.Statement;
+import java.sql.Time;
+import java.sql.Timestamp;
+import java.util.Date;
+import java.util.List;
 
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
 import org.apache.torque.BaseDatabaseTestCase;
+import org.apache.torque.Torque;
+import org.apache.torque.adapter.DBDerby;
+import org.apache.torque.adapter.DBHsqldb;
+import org.apache.torque.adapter.DBMM;
+import org.apache.torque.adapter.DBMSSQL;
+import org.apache.torque.adapter.DBOracle;
+import org.apache.torque.adapter.DBPostgres;
+import org.apache.torque.criteria.Criteria;
+import org.apache.torque.test.CurrentDateTable;
+import org.apache.torque.test.CurrentDateTablePeer;
+import org.apache.torque.test.CurrentTimeTable;
+import org.apache.torque.test.CurrentTimeTablePeer;
+import org.apache.torque.test.CurrentTimestampTable;
+import org.apache.torque.test.CurrentTimestampTablePeer;
 import org.apache.torque.test.DefaultValues;
 import org.apache.torque.test.DefaultValuesPeer;
-import org.apache.torque.util.ColumnValues;
-import org.apache.torque.util.JdbcTypedValue;
 
 /*
  * Licensed to the Apache Software Foundation (ASF) under one
@@ -29,120 +49,327 @@ import org.apache.torque.util.JdbcTypedV
 
 /**
  * Tests whether the useDatabaseDefaultValue attribute works.
- * It is difficult to have database default values for the different
- * databases, so the test checks the results of the buildColumnValues
- * method of the peers.
  *
  * @version $Id$
  */
 public class UseDefaultValueFromDatabaseTest extends BaseDatabaseTestCase
 {
+    private static Log log
+            = LogFactory.getLog(UseDefaultValueFromDatabaseTest.class);
+
     /**
-     * Tests whether the buildColumnValues adds column values
-     * for columns which have no database default on a new object.
+     * Checks the java default values.
      */
-    public void testJavaDefaultValueOnNewObject() throws Exception
+    public void testNewObjectJavaDefault() throws Exception
     {
         DefaultValues defaultValues = new DefaultValues();
-        ColumnValues columnValues
-                = DefaultValuesPeer.buildColumnValues(defaultValues);
-        assertEquals(
-                new JdbcTypedValue(3, Types.INTEGER),
-                columnValues.get(DefaultValuesPeer.O_INTEGER_WITH_DEFAULT));
-        assertEquals(
-                new JdbcTypedValue(5, Types.INTEGER),
-                columnValues.get(DefaultValuesPeer.P_INT_WITH_DEFAULT));
+        assertEquals(null, defaultValues.getOIntegerDefaultFromDb());
+        assertEquals(new Integer(3), defaultValues.getOIntegerWithDefault());
+        assertEquals(0, defaultValues.getPIntDefaultFromDb());
+        assertEquals(5, defaultValues.getPIntWithDefault());
     }
 
     /**
-     * Tests whether the buildColumnValues does not add column values
-     * for columns which use the database default value on a new object.
+     * Checks the values in the database if a new object is saved.
      */
-    public void testDatabaseDefaultValueOnNewObject() throws Exception
+    public void testNewObjectDatabaseDefault() throws Exception
     {
+        DefaultValuesPeer.doDelete(new Criteria());
         DefaultValues defaultValues = new DefaultValues();
-        ColumnValues columnValues
-                = DefaultValuesPeer.buildColumnValues(defaultValues);
-        assertNull(
-                columnValues.get(DefaultValuesPeer.O_INTEGER_DEFAULT_FROM_DB));
-        assertNull(
-                columnValues.get(DefaultValuesPeer.P_INT_DEFAULT_FROM_DB));
+
+        defaultValues.save();
+
+        // saved object should stay the same
+        assertEquals(null, defaultValues.getOIntegerDefaultFromDb());
+        assertEquals(new Integer(3), defaultValues.getOIntegerWithDefault());
+        assertEquals(0, defaultValues.getPIntDefaultFromDb());
+        assertEquals(5, defaultValues.getPIntWithDefault());
+        // re-loading should give the database default values
+        List<DefaultValues> defaultValuesList
+                = DefaultValuesPeer.doSelect(new Criteria());
+        assertEquals(1, defaultValuesList.size());
+        DefaultValues defaultValuesSaved = defaultValuesList.get(0);
+        assertEquals(
+                new Integer(2),
+                defaultValuesSaved.getOIntegerDefaultFromDb());
+        assertEquals(
+                new Integer(3),
+                defaultValuesSaved.getOIntegerWithDefault());
+        assertEquals(4, defaultValuesSaved.getPIntDefaultFromDb());
+        assertEquals(5, defaultValuesSaved.getPIntWithDefault());
     }
 
     /**
-     * Tests whether the buildColumnValues adds column values
-     * for columns with modified value which have no database default
-     *  on a new object.
+     * Tests that values are saved if they are not equal to the default value
+     * on a new object.
      */
-    public void testJavaDefaultValueOnNewObjectChangedValue() throws Exception
+    public void testNewObjectChangedValue() throws Exception
     {
+        DefaultValuesPeer.doDelete(new Criteria());
         DefaultValues defaultValues = new DefaultValues();
-        defaultValues.setOIntegerWithDefault(6);
-        defaultValues.setPIntWithDefault(7);
-        ColumnValues columnValues
-                = DefaultValuesPeer.buildColumnValues(defaultValues);
+        defaultValues.setOIntegerDefaultFromDb(6);
+        defaultValues.setOIntegerWithDefault(7);
+        defaultValues.setPIntDefaultFromDb(8);
+        defaultValues.setPIntWithDefault(9);
+
+        defaultValues.save();
+
+        List<DefaultValues> defaultValuesList
+                = DefaultValuesPeer.doSelect(new Criteria());
+        assertEquals(1, defaultValuesList.size());
+        DefaultValues defaultValuesSaved = defaultValuesList.get(0);
         assertEquals(
-                new JdbcTypedValue(6, Types.INTEGER),
-                columnValues.get(DefaultValuesPeer.O_INTEGER_WITH_DEFAULT));
+                new Integer(6),
+                defaultValuesSaved.getOIntegerDefaultFromDb());
         assertEquals(
-                new JdbcTypedValue(7, Types.INTEGER),
-                columnValues.get(DefaultValuesPeer.P_INT_WITH_DEFAULT));
+                new Integer(7),
+                defaultValuesSaved.getOIntegerWithDefault());
+        assertEquals(8, defaultValuesSaved.getPIntDefaultFromDb());
+        assertEquals(9, defaultValuesSaved.getPIntWithDefault());
     }
 
     /**
-     * Tests whether the buildColumnValues adds column values
-     * for columns with modified value
-     * which use the database default value on a new object.
+     * Tests that values are saved if they are not equal to the default value
+     * on a new object.
      */
-    public void testDatabaseDefaultValueOnNewObjectChangedValue()
-            throws Exception
+    public void testNotNewObjectChangedValue() throws Exception
     {
+        DefaultValuesPeer.doDelete(new Criteria());
         DefaultValues defaultValues = new DefaultValues();
-        defaultValues.setOIntegerDefaultFromDb(6);
-        defaultValues.setPIntDefaultFromDb(7);
-        ColumnValues columnValues
-                = DefaultValuesPeer.buildColumnValues(defaultValues);
+        defaultValues.save();
+
+        defaultValues.save();
+
+        // second save behaves differently because object is not new any more
+        List<DefaultValues> defaultValuesList
+                = DefaultValuesPeer.doSelect(new Criteria());
+        assertEquals(1, defaultValuesList.size());
+        DefaultValues defaultValuesSaved = defaultValuesList.get(0);
         assertEquals(
-                new JdbcTypedValue(6, Types.INTEGER),
-                columnValues.get(DefaultValuesPeer.O_INTEGER_DEFAULT_FROM_DB));
+                new Integer(2),
+                defaultValuesSaved.getOIntegerDefaultFromDb());
         assertEquals(
-                new JdbcTypedValue(7, Types.INTEGER),
-                columnValues.get(DefaultValuesPeer.P_INT_DEFAULT_FROM_DB));
+                new Integer(3),
+                defaultValuesSaved.getOIntegerWithDefault());
+        assertEquals(4, defaultValuesSaved.getPIntDefaultFromDb());
+        assertEquals(5, defaultValuesSaved.getPIntWithDefault());
     }
 
     /**
-     * Tests whether the buildColumnValues adds column values
-     * for columns which have no database default on a not new object.
+     * Checks that if CURRENT_DATE is used as default value
+     * then the database saves the current date.
+     *
+     * @throws Exception if an error occurs.
      */
-    public void testJavaDefaultValueOnNotNewObject() throws Exception
+    public void testCurrentDateAsDefault() throws Exception
     {
-        DefaultValues defaultValues = new DefaultValues();
-        defaultValues.setNew(false);
-        ColumnValues columnValues
-                = DefaultValuesPeer.buildColumnValues(defaultValues);
-        assertEquals(
-                new JdbcTypedValue(3, Types.INTEGER),
-                columnValues.get(DefaultValuesPeer.O_INTEGER_WITH_DEFAULT));
-        assertEquals(
-                new JdbcTypedValue(5, Types.INTEGER),
-                columnValues.get(DefaultValuesPeer.P_INT_WITH_DEFAULT));
+        if (!canUseCurrentDateAsDefaultForDate())
+        {
+            return;
+        }
+        CurrentDateTablePeer.doDelete(new Criteria());
+        CurrentDateTable currentDate = new CurrentDateTable();
+
+        Date currentDateBefore = doSelect("CURRENT_DATE", java.sql.Date.class);
+        currentDate.save();
+        Date currentDateAfter = doSelect("CURRENT_DATE", java.sql.Date.class);
+
+        List<CurrentDateTable> dbStateList
+                = CurrentDateTablePeer.doSelect(new Criteria());
+        assertEquals(1, dbStateList.size());
+        CurrentDateTable dbState = dbStateList.get(0);
+        assertFalse(
+                "currentDate should be >= currentDateBefore",
+                dbState.getCurrentDateValue().before(currentDateBefore));
+        assertFalse(
+                "currentDate should be <= currentDateAfter",
+                dbState.getCurrentDateValue().after(currentDateAfter));
     }
 
     /**
-     * Tests whether the buildColumnValues adds column values
-     * for columns which use the database default value on a not new object.
+     * Checks that if CURRENT_TIME is used as default value
+     * then the database saves the current date.
+     *
+     * @throws Exception if an error occurs.
      */
-    public void testDatabaseDefaultValueOnNotNewObject() throws Exception
+    public void testCurrentTimeAsDefault() throws Exception
     {
-        DefaultValues defaultValues = new DefaultValues();
-        defaultValues.setNew(false);
-        ColumnValues columnValues
-                = DefaultValuesPeer.buildColumnValues(defaultValues);
-        assertEquals(
-                new JdbcTypedValue(2, Types.INTEGER),
-                columnValues.get(DefaultValuesPeer.O_INTEGER_DEFAULT_FROM_DB));
-        assertEquals(
-                new JdbcTypedValue(4, Types.INTEGER),
-                columnValues.get(DefaultValuesPeer.P_INT_DEFAULT_FROM_DB));
+        if (!canUseCurrentTimeAsDefaultForTime())
+        {
+            return;
+        }
+        CurrentTimeTablePeer.doDelete(new Criteria());
+        CurrentTimeTable currentTime = new CurrentTimeTable();
+
+        Date currentDateBefore = doSelect("CURRENT_TIME", Time.class);
+        currentTime.save();
+        Date currentDateAfter = doSelect("CURRENT_TIME", Time.class);
+
+        List<CurrentTimeTable> dbStateList
+                = CurrentTimeTablePeer.doSelect(new Criteria());
+        assertEquals(1, dbStateList.size());
+        CurrentTimeTable dbState = dbStateList.get(0);
+        if (timezoneBuggyInCurrentTime())
+        {
+            return;
+        }
+        assertFalse(
+                "currentDate should be >= currentDateBefore",
+                dbState.getCurrentTimeValue().before(currentDateBefore));
+        assertFalse(
+                "currentDate should be <= currentDateAfter",
+                dbState.getCurrentTimeValue().after(currentDateAfter));
+    }
+
+    /**
+     * Checks that if CURRENT_TIMESTAMP is used as default value
+     * then the database saves the current date.
+     *
+     * @throws Exception if an error occurs.
+     */
+    public void testCurrentTimestampAsDefault() throws Exception
+    {
+        if (!canUseCurrentTimestampAsDefaultForTimestamp())
+        {
+            return;
+        }
+        CurrentTimestampTablePeer.doDelete(new Criteria());
+        CurrentTimestampTable currentTimestamp = new CurrentTimestampTable();
+
+        Date currentTimestampBefore = doSelect("CURRENT_TIMESTAMP", Timestamp.class);
+        currentTimestamp.save();
+        Date currentTimestampAfter = doSelect("CURRENT_TIMESTAMP", Timestamp.class);
+
+        List<CurrentTimestampTable> dbStateList
+                = CurrentTimestampTablePeer.doSelect(new Criteria());
+        assertEquals(1, dbStateList.size());
+        CurrentTimestampTable dbState = dbStateList.get(0);
+        assertFalse(
+                "currentDate should be >= currentDateBefore",
+                dbState.getCurrentTimestampValue().before(
+                        currentTimestampBefore));
+        assertFalse(
+                "currentDate should be <= currentDateAfter",
+                dbState.getCurrentTimestampValue().after(
+                        currentTimestampAfter));
+    }
+
+    private Date doSelect(String toSelect, Class<?> classToSelect)
+            throws Exception
+    {
+        String sql;
+        if (defaultAdapter instanceof DBOracle)
+        {
+            sql = "select " + toSelect + " from dual";
+        }
+        else if (defaultAdapter instanceof DBDerby)
+        {
+            sql = "values(" + toSelect + ")";
+        }
+        else if (defaultAdapter instanceof DBHsqldb)
+        {
+            sql = "call " + toSelect;
+        }
+        else
+        {
+            sql = "select " + toSelect;
+        }
+        Connection connection = null;
+        try
+        {
+            connection = Torque.getConnection();
+            Statement statement = connection.createStatement();
+            ResultSet resultSet = statement.executeQuery(sql);
+            if (!resultSet.next())
+            {
+                fail("Statement " + sql + " returned 0 rows");
+            }
+            Date result;
+            if (Time.class == classToSelect)
+            {
+                result= resultSet.getTime(1);
+            }
+            else if (java.sql.Date.class == classToSelect)
+            {
+                result= resultSet.getDate(1);
+            }
+            else if (Timestamp.class == classToSelect)
+            {
+                result= resultSet.getTimestamp(1);
+            }
+            else
+            {
+                throw new IllegalArgumentException("unknown classToSelect "
+                        + classToSelect.getName());
+            }
+            if (resultSet.next())
+            {
+                fail("Statement " + sql + " returned more than 1 row");
+            }
+            return result;
+        }
+        finally
+        {
+            if (connection != null)
+            {
+                try
+                {
+                    connection.close();
+                }
+                catch (Exception e)
+                {
+                    // ignore
+                }
+            }
+        }
+    }
+
+    private static boolean canUseCurrentDateAsDefaultForDate()
+    {
+        if (defaultAdapter instanceof DBMM
+                || defaultAdapter instanceof DBMSSQL)
+        {
+            log.warn("canUseCurrentDateAsDefaultForDate(): "
+                    + "CURRENT_DATE cannot be used as default value "
+                    + "for Date columns for MySQL and MSSQL");
+            return false;
+        }
+        return true;
+    }
+
+    private static boolean canUseCurrentTimeAsDefaultForTime()
+    {
+        if (defaultAdapter instanceof DBMM
+                || defaultAdapter instanceof DBOracle
+                || defaultAdapter instanceof DBMSSQL)
+        {
+            log.warn("canUseCurrentTimeAsDefaultForTime(): "
+                    + "CURRENT_TIME cannot be used as default value "
+                    + "for Time columns for MySQL, Oracle and MSSQL");
+            return false;
+        }
+        return true;
+    }
+
+    private static boolean canUseCurrentTimestampAsDefaultForTimestamp()
+    {
+        if (defaultAdapter instanceof DBMM)
+        {
+            log.warn("canUseCurrentDateAsDefaultForDate(): "
+                    + "CURRENT_TIMESTAMP cannot be used as default value "
+                    + "for Timestamp columns for MySQL");
+            return false;
+        }
+        return true;
+    }
+
+    private static boolean timezoneBuggyInCurrentTime()
+    {
+        if (defaultAdapter instanceof DBPostgres)
+        {
+            log.warn("Timezone is buggy in CUTTENT_TIME in Postgres");
+            return true;
+        }
+        return false;
     }
 }



---------------------------------------------------------------------
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