From torque-dev-return-11450-apmail-db-torque-dev-archive=db.apache.org@db.apache.org Fri Jul 20 04:32:05 2012 Return-Path: X-Original-To: apmail-db-torque-dev-archive@www.apache.org Delivered-To: apmail-db-torque-dev-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 1DFC1D6DE for ; Fri, 20 Jul 2012 04:32:05 +0000 (UTC) Received: (qmail 30088 invoked by uid 500); 20 Jul 2012 04:32:04 -0000 Delivered-To: apmail-db-torque-dev-archive@db.apache.org Received: (qmail 30067 invoked by uid 500); 20 Jul 2012 04:32:03 -0000 Mailing-List: contact torque-dev-help@db.apache.org; run by ezmlm Precedence: bulk List-Unsubscribe: List-Help: List-Post: List-Id: "Apache Torque Developers List" Reply-To: "Apache Torque Developers List" Delivered-To: mailing list torque-dev@db.apache.org Received: (qmail 30045 invoked by uid 500); 20 Jul 2012 04:32:03 -0000 Received: (qmail 30041 invoked by uid 99); 20 Jul 2012 04:32:03 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 20 Jul 2012 04:32:03 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=5.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 20 Jul 2012 04:31:53 +0000 Received: from eris.apache.org (localhost [127.0.0.1]) by eris.apache.org (Postfix) with ESMTP id 347BA2388860; Fri, 20 Jul 2012 04:31:32 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r1363653 - in /db/torque/torque4/trunk: torque-templates/src/main/java/org/apache/torque/templates/platform/ torque-templates/src/main/java/org/apache/torque/templates/transformer/om/ torque-templates/src/main/java/org/apache/torque/templat... Date: Fri, 20 Jul 2012 04:31:31 -0000 To: torque-commits@db.apache.org From: tfischer@apache.org X-Mailer: svnmailer-1.0.8-patched Message-Id: <20120720043132.347BA2388860@eris.apache.org> Author: tfischer Date: Fri Jul 20 04:31:30 2012 New Revision: 1363653 URL: http://svn.apache.org/viewvc?rev=1363653&view=rev Log: TORQUE-217 Date default values should be possible For now only tested on hsqldb and derby Added: db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/dbObject/base/getCurrentDate.vm db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/dbObject/base/getCurrentTime.vm db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/dbObject/base/getCurrentTimestamp.vm db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/dbObject/base/imports.vm db/torque/torque4/trunk/torque-test/src/test/java/org/apache/torque/generated/peer/DefaultValuesFromDatabaseTest.java - copied, changed from r1356516, db/torque/torque4/trunk/torque-test/src/test/java/org/apache/torque/generated/peer/UseDefaultValueFromDatabaseTest.java db/torque/torque4/trunk/torque-test/src/test/java/org/apache/torque/generated/peer/DefaultValuesFromJavaTest.java - copied, changed from r1356516, db/torque/torque4/trunk/torque-test/src/test/java/org/apache/torque/generated/peer/DefaultValueTest.java Removed: db/torque/torque4/trunk/torque-test/src/test/java/org/apache/torque/generated/peer/DefaultValueTest.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/platform/Platform.java db/torque/torque4/trunk/torque-templates/src/main/java/org/apache/torque/templates/platform/PlatformDefaultImpl.java db/torque/torque4/trunk/torque-templates/src/main/java/org/apache/torque/templates/platform/PlatformHsqldbImpl.java db/torque/torque4/trunk/torque-templates/src/main/java/org/apache/torque/templates/platform/PlatformMssqlImpl.java db/torque/torque4/trunk/torque-templates/src/main/java/org/apache/torque/templates/platform/PlatformMysqlImpl.java db/torque/torque4/trunk/torque-templates/src/main/java/org/apache/torque/templates/platform/PlatformOracleImpl.java 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/om/OMTableAndViewTransformer.java db/torque/torque4/trunk/torque-templates/src/main/java/org/apache/torque/templates/transformer/om/TableAttributeName.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/outlets/dbObject.xml db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/dbObject/base/baseDbObject.vm 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/platform/Platform.java URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-templates/src/main/java/org/apache/torque/templates/platform/Platform.java?rev=1363653&r1=1363652&r2=1363653&view=diff ============================================================================== --- db/torque/torque4/trunk/torque-templates/src/main/java/org/apache/torque/templates/platform/Platform.java (original) +++ db/torque/torque4/trunk/torque-templates/src/main/java/org/apache/torque/templates/platform/Platform.java Fri Jul 20 04:31:30 2012 @@ -19,6 +19,8 @@ package org.apache.torque.templates.plat * under the License. */ +import java.util.Date; + import org.apache.torque.templates.typemapping.SchemaType; import org.apache.torque.templates.typemapping.SqlType; @@ -98,4 +100,32 @@ public interface Platform * @return the escaped String, not null. */ String quoteAndEscape(String value); + + /** + * Formats the given date as date string which is parseable by the database. + * + * @param date the date to format. + * + * @return the date string, inclusive string escaping. + */ + String getDateString(Date date); + + /** + * Formats the given date as time string which is parseable by the database. + * + * @param date the date to format. + * + * @return the time string, inclusive string escaping. + */ + String getTimeString(Date date); + + /** + * Formats the given date as timestamp string which is parseable + * by the database. + * + * @param date the date to format. + * + * @return the timestamp string, inclusive string escaping. + */ + String getTimestampString(Date date); } Modified: db/torque/torque4/trunk/torque-templates/src/main/java/org/apache/torque/templates/platform/PlatformDefaultImpl.java URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-templates/src/main/java/org/apache/torque/templates/platform/PlatformDefaultImpl.java?rev=1363653&r1=1363652&r2=1363653&view=diff ============================================================================== --- db/torque/torque4/trunk/torque-templates/src/main/java/org/apache/torque/templates/platform/PlatformDefaultImpl.java (original) +++ db/torque/torque4/trunk/torque-templates/src/main/java/org/apache/torque/templates/platform/PlatformDefaultImpl.java Fri Jul 20 04:31:30 2012 @@ -19,9 +19,12 @@ package org.apache.torque.templates.plat * under the License. */ +import java.text.SimpleDateFormat; import java.util.Collections; +import java.util.Date; import java.util.HashMap; import java.util.Map; +import java.util.TimeZone; import org.apache.commons.lang.StringUtils; import org.apache.torque.templates.typemapping.SchemaType; @@ -36,6 +39,9 @@ import org.apache.torque.templates.typem */ public class PlatformDefaultImpl implements Platform { + /** The date format for formatting database timestamps. */ + private static String TIMESTAMP_FORMAT = "''yyyy-MM-dd HH:mm:ss''"; + /** * Maps the Torque schema types to sql types. */ @@ -179,4 +185,30 @@ public class PlatformDefaultImpl impleme { return true; } + + /** + * {@inheritDoc} + */ + public String getDateString(Date date) + { + return getTimestampString(date); + } + + /** + * {@inheritDoc} + */ + public String getTimeString(Date date) + { + return getTimestampString(date); + } + + /** + * {@inheritDoc} + */ + public String getTimestampString(Date date) + { + SimpleDateFormat dateFormat = new SimpleDateFormat(TIMESTAMP_FORMAT); + dateFormat.setTimeZone(TimeZone.getTimeZone("GMT")); + return dateFormat.format(date); + } } Modified: db/torque/torque4/trunk/torque-templates/src/main/java/org/apache/torque/templates/platform/PlatformHsqldbImpl.java URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-templates/src/main/java/org/apache/torque/templates/platform/PlatformHsqldbImpl.java?rev=1363653&r1=1363652&r2=1363653&view=diff ============================================================================== --- db/torque/torque4/trunk/torque-templates/src/main/java/org/apache/torque/templates/platform/PlatformHsqldbImpl.java (original) +++ db/torque/torque4/trunk/torque-templates/src/main/java/org/apache/torque/templates/platform/PlatformHsqldbImpl.java Fri Jul 20 04:31:30 2012 @@ -19,6 +19,10 @@ package org.apache.torque.templates.plat * under the License. */ +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.TimeZone; + import org.apache.torque.templates.typemapping.SchemaType; import org.apache.torque.templates.typemapping.SqlType; @@ -30,6 +34,12 @@ import org.apache.torque.templates.typem */ public class PlatformHsqldbImpl extends PlatformDefaultImpl { + /** The date format for formatting database dates. */ + private static String DATE_FORMAT = "''yyyy-MM-dd''"; + + /** The date format for formatting database times. */ + private static String TIME_FORMAT = "''HH:mm:ss''"; + /** * Default constructor. */ @@ -56,6 +66,7 @@ public class PlatformHsqldbImpl extends * @return The RDBMS-specific SQL fragment for autoincrement. * @see Platform#getAutoIncrement() */ + @Override public String getAutoIncrement() { return "GENERATED BY DEFAULT AS IDENTITY (START WITH 1)"; @@ -68,13 +79,38 @@ public class PlatformHsqldbImpl extends * @return false. * @see Platform#createNotNullBeforeAutoincrement() */ + @Override public boolean createNotNullBeforeAutoincrement() { return false; } + @Override protected boolean escapeBackslashes() { return false; } + + /** + * {@inheritDoc} + */ + @Override + public String getDateString(Date date) + { + SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT); + dateFormat.setTimeZone(TimeZone.getTimeZone("GMT")); + return dateFormat.format(date); + } + + /** + * {@inheritDoc} + */ + @Override + public String getTimeString(Date date) + { + SimpleDateFormat dateFormat = new SimpleDateFormat(TIME_FORMAT); + dateFormat.setTimeZone(TimeZone.getTimeZone("GMT")); + return dateFormat.format(date); + } + } Modified: db/torque/torque4/trunk/torque-templates/src/main/java/org/apache/torque/templates/platform/PlatformMssqlImpl.java URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-templates/src/main/java/org/apache/torque/templates/platform/PlatformMssqlImpl.java?rev=1363653&r1=1363652&r2=1363653&view=diff ============================================================================== --- db/torque/torque4/trunk/torque-templates/src/main/java/org/apache/torque/templates/platform/PlatformMssqlImpl.java (original) +++ db/torque/torque4/trunk/torque-templates/src/main/java/org/apache/torque/templates/platform/PlatformMssqlImpl.java Fri Jul 20 04:31:30 2012 @@ -19,6 +19,10 @@ package org.apache.torque.templates.plat * under the License. */ +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.TimeZone; + import org.apache.torque.templates.typemapping.SchemaType; import org.apache.torque.templates.typemapping.SqlType; @@ -31,6 +35,9 @@ import org.apache.torque.templates.typem */ public class PlatformMssqlImpl extends PlatformDefaultImpl { + /** The date format for formatting database dates. */ + private static String DATE_FORMAT = "''yyyyMMdd HH:mm:ss''"; + /** * Default constructor. */ @@ -88,13 +95,26 @@ public class PlatformMssqlImpl extends P * allowed (as recomended by Microsoft). * @see Platform#getNullString(boolean) */ + @Override public String getNullString(boolean notNull) { return (notNull ? "NOT NULL" : "NULL"); } + @Override protected boolean escapeBackslashes() { return false; } + + /** + * {@inheritDoc} + */ + @Override + public String getDateString(Date date) + { + SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT); + dateFormat.setTimeZone(TimeZone.getTimeZone("GMT")); + return dateFormat.format(date); + } } Modified: db/torque/torque4/trunk/torque-templates/src/main/java/org/apache/torque/templates/platform/PlatformMysqlImpl.java URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-templates/src/main/java/org/apache/torque/templates/platform/PlatformMysqlImpl.java?rev=1363653&r1=1363652&r2=1363653&view=diff ============================================================================== --- db/torque/torque4/trunk/torque-templates/src/main/java/org/apache/torque/templates/platform/PlatformMysqlImpl.java (original) +++ db/torque/torque4/trunk/torque-templates/src/main/java/org/apache/torque/templates/platform/PlatformMysqlImpl.java Fri Jul 20 04:31:30 2012 @@ -19,6 +19,10 @@ package org.apache.torque.templates.plat * under the License. */ +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.TimeZone; + import org.apache.torque.templates.typemapping.SchemaType; import org.apache.torque.templates.typemapping.SqlType; @@ -30,6 +34,9 @@ import org.apache.torque.templates.typem */ public class PlatformMysqlImpl extends PlatformDefaultImpl { + /** The date format for formatting database dates. */ + private static String DATE_FORMAT = "''yyyyMMddHHmmss''"; + /** * Default constructor. */ @@ -73,6 +80,7 @@ public class PlatformMysqlImpl extends P /** * @see Platform#getAutoIncrement() */ + @Override public String getAutoIncrement() { return "AUTO_INCREMENT"; @@ -81,6 +89,7 @@ public class PlatformMysqlImpl extends P /** * @see Platform#hasSize(String) */ + @Override public boolean hasSize(String sqlType) { return !("MEDIUMTEXT".equals(sqlType) || "LONGTEXT".equals(sqlType) @@ -88,8 +97,20 @@ public class PlatformMysqlImpl extends P || "LONGBLOB".equals(sqlType)); } + @Override protected boolean escapeBackslashes() { return true; } + + /** + * {@inheritDoc} + */ + @Override + public String getDateString(Date date) + { + SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT); + dateFormat.setTimeZone(TimeZone.getTimeZone("GMT")); + return dateFormat.format(date); + } } Modified: db/torque/torque4/trunk/torque-templates/src/main/java/org/apache/torque/templates/platform/PlatformOracleImpl.java URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-templates/src/main/java/org/apache/torque/templates/platform/PlatformOracleImpl.java?rev=1363653&r1=1363652&r2=1363653&view=diff ============================================================================== --- db/torque/torque4/trunk/torque-templates/src/main/java/org/apache/torque/templates/platform/PlatformOracleImpl.java (original) +++ db/torque/torque4/trunk/torque-templates/src/main/java/org/apache/torque/templates/platform/PlatformOracleImpl.java Fri Jul 20 04:31:30 2012 @@ -19,6 +19,10 @@ package org.apache.torque.templates.plat * under the License. */ +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.TimeZone; + import org.apache.commons.lang.StringUtils; import org.apache.torque.templates.typemapping.SchemaType; import org.apache.torque.templates.typemapping.SqlType; @@ -31,6 +35,9 @@ import org.apache.torque.templates.typem */ public class PlatformOracleImpl extends PlatformDefaultImpl { + /** The date format for formatting database dates. */ + private static String DATE_FORMAT = "''dd-MM-yyyy HH:mm:ss''"; + /** * Default constructor. */ @@ -95,11 +102,13 @@ public class PlatformOracleImpl extends /** * @see Platform#getAutoIncrement() */ + @Override public String getAutoIncrement() { return ""; } + @Override protected boolean escapeBackslashes() { return true; @@ -116,6 +125,7 @@ public class PlatformOracleImpl extends * This implementation always returns the empty string. * */ + @Override public String getSizeSuffix(String sqlType) { if ("VARCHAR2".equals(sqlType)) @@ -124,4 +134,15 @@ public class PlatformOracleImpl extends } return StringUtils.EMPTY; } + + /** + * {@inheritDoc} + */ + @Override + public String getDateString(Date date) + { + SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT); + dateFormat.setTimeZone(TimeZone.getTimeZone("GMT")); + return dateFormat.format(date); + } } 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=1363653&r1=1363652&r2=1363653&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 Jul 20 04:31:30 2012 @@ -19,9 +19,13 @@ package org.apache.torque.templates.tran * under the License. */ +import java.text.ParseException; +import java.text.SimpleDateFormat; import java.util.Collections; +import java.util.Date; import java.util.HashSet; import java.util.Set; +import java.util.TimeZone; import org.apache.commons.lang.StringUtils; import org.apache.torque.generator.control.ControllerState; @@ -57,6 +61,28 @@ public class OMColumnTransformer /** Names which cannot be used as constants for column names. */ private static final Set RESERVED_CONSTANT_NAMES; + /** The Date format for Dates in Default values. */ + private static final String DEFAULT_DATE_FORMAT = "yyyy-MM-dd HH:mm:ss.S"; + + /** Constant for the CURRENT_DATE default value for Dates. */ + static final String CURRENT_DATE = "CURRENT_DATE"; + + /** Constant for the CURRENT_TIME default value for Dates. */ + static final String CURRENT_TIME = "CURRENT_TIME"; + + /** Constant for the CURRENT_TIMESTAMP default value for Dates. */ + static final String CURRENT_TIMESTAMP = "CURRENT_TIMESTAMP"; + + /** Constant for the getDefaultDate method name. */ + static final String GET_DEFAULT_DATE_METHOD_NAME = "getDefaultDate"; + + /** Constant for the getDefaultTime method name. */ + static final String GET_DEFAULT_TIME_METHOD_NAME = "getDefaultTime"; + + /** Constant for the getDefaultTimestamp method name. */ + static final String GET_DEFAULT_TIMESTAMP_METHOD_NAME + = "getDefaultTimestamp"; + /** Prevents reserved java words. */ private static WrapReservedJavaWords reservedJavaWordsWrapper = new WrapReservedJavaWords(); @@ -607,7 +633,10 @@ public class OMColumnTransformer columnElement.getAttribute( TorqueSchemaAttributeName.USE_DATABASE_DEFAULT_VALUE)); fieldDefaultValue = getDefaultValueWithDefaultSet( - javaType, defaultAttributeValue, useDatabaseDefaultValue); + javaType, + defaultAttributeValue, + useDatabaseDefaultValue, + columnElement); } else { @@ -626,13 +655,19 @@ public class OMColumnTransformer * @param defaultValue The default value from the schema. * @param useDatabaseDefaultValue whether the database default value should * be used. + * @param columnElement the current column element for which + * the default value should be calculated. * * @return The java default value. + * + * @throws SourceTransformerException if an illegal default value is used. */ protected String getDefaultValueWithDefaultSet( - JavaType javaType, - String defaultValue, - boolean useDatabaseDefaultValue) + JavaType javaType, + String defaultValue, + boolean useDatabaseDefaultValue, + SourceElement columnElement) + throws SourceTransformerException { boolean primitiveFieldType = javaType.isPrimitive(); String fieldDefaultValue; @@ -688,9 +723,9 @@ public class OMColumnTransformer } else if (JavaType.DATE == javaType) { - if ("CURRENT_DATE".equalsIgnoreCase(defaultValue) - || "CURRENT_TIME".equalsIgnoreCase(defaultValue) - || "CURRENT_TIMESTAMP".equalsIgnoreCase(defaultValue)) + if (CURRENT_DATE.equalsIgnoreCase(defaultValue) + || CURRENT_TIME.equalsIgnoreCase(defaultValue) + || CURRENT_TIMESTAMP.equalsIgnoreCase(defaultValue)) { if (useDatabaseDefaultValue) { @@ -702,14 +737,74 @@ public class OMColumnTransformer { // the database does not provide a default so use // java current time. - fieldDefaultValue = "new " + javaType.getFullClassName() - + "()"; + if (CURRENT_DATE.equalsIgnoreCase(defaultValue)) + { + String methodName; + if (columnElement.getParent().getAttribute( + TableAttributeName.GET_DEFAULT_DATE_METHOD_NAME) + != null) + { + methodName = columnElement.getParent().getAttribute( + TableAttributeName.GET_DEFAULT_DATE_METHOD_NAME) + .toString(); + } + else + { + methodName = GET_DEFAULT_DATE_METHOD_NAME; + } + fieldDefaultValue = methodName + "()"; + } + else if (CURRENT_TIME.equalsIgnoreCase(defaultValue)) + { + String methodName; + if (columnElement.getParent().getAttribute( + TableAttributeName.GET_DEFAULT_TIME_METHOD_NAME) + != null) + { + methodName = columnElement.getParent().getAttribute( + TableAttributeName.GET_DEFAULT_TIME_METHOD_NAME) + .toString(); + } + else + { + methodName = GET_DEFAULT_TIME_METHOD_NAME; + } + fieldDefaultValue = methodName + "()"; + } + else + { + String methodName; + if (columnElement.getParent().getAttribute( + TableAttributeName.GET_DEFAULT_TIMESTAMP_METHOD_NAME) + != null) + { + methodName = columnElement.getParent().getAttribute( + TableAttributeName.GET_DEFAULT_TIMESTAMP_METHOD_NAME) + .toString(); + } + else + { + methodName = GET_DEFAULT_TIMESTAMP_METHOD_NAME; + } + fieldDefaultValue = methodName + "()"; + } } } else { - fieldDefaultValue = "new " + javaType.getFullClassName() - + "(" + defaultValue + ")"; + if (useDatabaseDefaultValue) + { + // if the database default value is used, do not use + // current time in java as it might be different + // and have a custom format. + fieldDefaultValue = "null"; + } + else + { + fieldDefaultValue = "new Date(" + + getDefaultValueAsDate(defaultValue).getTime() + + "L)";; + } } } else if (primitiveFieldType) @@ -726,6 +821,35 @@ public class OMColumnTransformer } /** + * Parses the default value String as Date. + * + * @param defaultValue the String to parse. + * @return the parsed date. + * + * @throws SourceTransformerException if the date cannot be parsed. + */ + public static Date getDefaultValueAsDate(String defaultValue) + throws SourceTransformerException + { + try + { + SimpleDateFormat dateFormat + = new SimpleDateFormat(DEFAULT_DATE_FORMAT); + dateFormat.setTimeZone(TimeZone.getTimeZone("GMT")); + return dateFormat.parse(defaultValue); + } + catch (ParseException e) + { + throw new SourceTransformerException( + "The default value " + + defaultValue + + " does not match the format String " + + DEFAULT_DATE_FORMAT + + " for date values"); + } + } + + /** * Calculates the java default value of a column in case a default value * is not set. * Modified: db/torque/torque4/trunk/torque-templates/src/main/java/org/apache/torque/templates/transformer/om/OMTableAndViewTransformer.java URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-templates/src/main/java/org/apache/torque/templates/transformer/om/OMTableAndViewTransformer.java?rev=1363653&r1=1363652&r2=1363653&view=diff ============================================================================== --- db/torque/torque4/trunk/torque-templates/src/main/java/org/apache/torque/templates/transformer/om/OMTableAndViewTransformer.java (original) +++ db/torque/torque4/trunk/torque-templates/src/main/java/org/apache/torque/templates/transformer/om/OMTableAndViewTransformer.java Fri Jul 20 04:31:30 2012 @@ -19,9 +19,17 @@ package org.apache.torque.templates.tran * under the License. */ +import static org.apache.torque.templates.transformer.om.OMColumnTransformer.CURRENT_DATE; +import static org.apache.torque.templates.transformer.om.OMColumnTransformer.CURRENT_TIME; +import static org.apache.torque.templates.transformer.om.OMColumnTransformer.CURRENT_TIMESTAMP; +import static org.apache.torque.templates.transformer.om.OMColumnTransformer.GET_DEFAULT_DATE_METHOD_NAME; +import static org.apache.torque.templates.transformer.om.OMColumnTransformer.GET_DEFAULT_TIME_METHOD_NAME; +import static org.apache.torque.templates.transformer.om.OMColumnTransformer.GET_DEFAULT_TIMESTAMP_METHOD_NAME; + import java.io.InputStreamReader; import java.util.List; +import org.apache.commons.lang.ObjectUtils; import org.apache.commons.lang.StringUtils; import org.apache.torque.generator.control.ControllerState; import org.apache.torque.generator.processor.string.Camelbacker; @@ -69,6 +77,7 @@ public class OMTableAndViewTransformer e } + @Override public SourceElement transform( SourceElement tableElement, ControllerState controllerState) @@ -109,6 +118,8 @@ public class OMTableAndViewTransformer e columnPosition++; } + setCreateDefaultDateMethodsAttributes(tableElement); + referencingForeignKeyTableTransformer.transform( tableElement, controllerState); @@ -496,4 +507,52 @@ public class OMTableAndViewTransformer e "toSave"); } } + + protected void setCreateDefaultDateMethodsAttributes( + SourceElement tableElement) + { + for (SourceElement column : tableElement.getChildren( + TorqueSchemaElementName.COLUMN)) + { + if ("java.util.Date".equals( + column.getAttribute(JavaFieldAttributeName.FIELD_TYPE))) + { + String defaultValue = ObjectUtils.toString(column.getAttribute( + TorqueSchemaAttributeName.DEFAULT)); + if (CURRENT_DATE.equalsIgnoreCase(defaultValue)) + { + if (tableElement.getAttribute( + TableAttributeName.GET_DEFAULT_DATE_METHOD_NAME) + == null) + { + tableElement.setAttribute( + TableAttributeName.GET_DEFAULT_DATE_METHOD_NAME, + GET_DEFAULT_DATE_METHOD_NAME); + } + } + else if (CURRENT_TIME.equalsIgnoreCase(defaultValue)) + { + if (tableElement.getAttribute( + TableAttributeName.GET_DEFAULT_TIME_METHOD_NAME) + == null) + { + tableElement.setAttribute( + TableAttributeName.GET_DEFAULT_TIME_METHOD_NAME, + GET_DEFAULT_TIME_METHOD_NAME); + } + } + else if (CURRENT_TIMESTAMP.equalsIgnoreCase(defaultValue)) + { + if (tableElement.getAttribute( + TableAttributeName.GET_DEFAULT_TIMESTAMP_METHOD_NAME) + == null) + { + tableElement.setAttribute( + TableAttributeName.GET_DEFAULT_TIMESTAMP_METHOD_NAME, + GET_DEFAULT_TIMESTAMP_METHOD_NAME); + } + } + } + } + } } Modified: db/torque/torque4/trunk/torque-templates/src/main/java/org/apache/torque/templates/transformer/om/TableAttributeName.java URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-templates/src/main/java/org/apache/torque/templates/transformer/om/TableAttributeName.java?rev=1363653&r1=1363652&r2=1363653&view=diff ============================================================================== --- db/torque/torque4/trunk/torque-templates/src/main/java/org/apache/torque/templates/transformer/om/TableAttributeName.java (original) +++ db/torque/torque4/trunk/torque-templates/src/main/java/org/apache/torque/templates/transformer/om/TableAttributeName.java Fri Jul 20 04:31:30 2012 @@ -90,7 +90,25 @@ public enum TableAttributeName implement /** * The variable name in which the object to save is stored, if any. */ - SAVE_METHOD_TO_SAVE_VARIABLE("saveMethodToSaveVariable"); + SAVE_METHOD_TO_SAVE_VARIABLE("saveMethodToSaveVariable"), + + /** + * The variable name in which the name of the getDefaultDate method + * us defined, is it should be generated. + */ + GET_DEFAULT_DATE_METHOD_NAME("getDefaultDateMethodName"), + + /** + * The variable name in which the name of the getDefaultTime method + * us defined, is it should be generated. + */ + GET_DEFAULT_TIME_METHOD_NAME("getDefaultTimeMethodName"), + + /** + * The variable name in which the name of the getDefaultTimestamp method + * us defined, is it should be generated. + */ + GET_DEFAULT_TIMESTAMP_METHOD_NAME("getDefaultTimestampMethodName"); /** The name of the source element attribute, not null. */ 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=1363653&r1=1363652&r2=1363653&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 Fri Jul 20 04:31:30 2012 @@ -20,6 +20,7 @@ package org.apache.torque.templates.tran */ import java.util.ArrayList; +import java.util.Date; import java.util.List; import org.apache.commons.lang.ObjectUtils; @@ -37,6 +38,7 @@ import org.apache.torque.templates.platf import org.apache.torque.templates.platform.PlatformFactory; import org.apache.torque.templates.transformer.CollectAttributeSetTrueTransformer; import org.apache.torque.templates.transformer.SchemaTypeHelper; +import org.apache.torque.templates.transformer.om.OMColumnTransformer; import org.apache.torque.templates.transformer.om.TableChildElementName; import org.apache.torque.templates.typemapping.SchemaType; import org.apache.torque.templates.typemapping.SqlType; @@ -299,10 +301,30 @@ public class SQLTransformer implements S if ((SchemaType.DATE == schemaType || SchemaType.TIME == schemaType - || SchemaType.TIMESTAMP == schemaType) - && sqlType.getDefaultValue().startsWith("CURRENT_")) + || SchemaType.TIMESTAMP == schemaType)) { - resultList.add(sqlType.getDefaultValue()); + if (sqlType.getDefaultValue().startsWith("CURRENT_")) + { + resultList.add(sqlType.getDefaultValue()); + } + else + { + Date defaultDate + = OMColumnTransformer.getDefaultValueAsDate( + sqlType.getDefaultValue()); + if (SchemaType.DATE == schemaType) { + resultList.add(platform.getDateString(defaultDate)); + } + else if (SchemaType.TIME == schemaType) + { + resultList.add(platform.getTimeString(defaultDate)); + } + else + { + resultList.add(platform.getTimestampString( + defaultDate)); + } + } } else if (TypeMap.isTextType(schemaType)) { Modified: db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/outlets/dbObject.xml URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/outlets/dbObject.xml?rev=1363653&r1=1363652&r2=1363653&view=diff ============================================================================== --- db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/outlets/dbObject.xml (original) +++ db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/outlets/dbObject.xml Fri Jul 20 04:31:30 2012 @@ -41,13 +41,16 @@ - + + + + @@ -150,6 +153,18 @@ + + + + + + + + + @@ -240,6 +255,11 @@ path="dbObject/base/bean/objectBeanImports.vm"> + + + @@ -405,6 +425,21 @@ path="dbObject/base/isCacheOnSave.vm"> + + + + + + + + + Modified: db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/dbObject/base/baseDbObject.vm URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/dbObject/base/baseDbObject.vm?rev=1363653&r1=1363652&r2=1363653&view=diff ============================================================================== --- db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/dbObject/base/baseDbObject.vm (original) +++ db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/dbObject/base/baseDbObject.vm Fri Jul 20 04:31:30 2012 @@ -27,59 +27,9 @@ ## package $baseDbObjectPackage; -import java.io.Serializable; -import java.sql.Connection; -import java.util.ArrayList; -import java.util.Collections; -import java.util.Date; -import java.util.List; - -#if ($torqueGen.booleanOption("torque.addIntakeRetrievable")) -import $torqueGen.option("torque.retrievableInterface"); -#end -#if (!$torqueGen.booleanOption("torque.om.complexObjectModel")) -import org.apache.torque.Torque; -#end -import org.apache.torque.TorqueException; -import org.apache.torque.map.TableMap; -import org.apache.torque.om.ComboKey; -import org.apache.torque.om.DateKey; -import org.apache.torque.om.NumberKey; -import org.apache.torque.om.ObjectKey; -import org.apache.torque.om.SimpleKey; -import org.apache.torque.om.StringKey; -import org.apache.torque.om.BooleanKey; -#if ($addSaveMethods == true && $saveMethodsInDbObjects == "true") -import org.apache.torque.om.Persistent; -#end -#if ($torqueGen.booleanOption("torque.om.addGetByNameMethods")) -import org.apache.torque.om.ColumnAccessByName; -#end## -import $torqueGen.option("torque.om.criteriaClass"); -#if ($addSaveMethods == true && $saveMethodsInDbObjects == "true") -import org.apache.torque.util.Transaction; -#end -import org.apache.commons.lang.ObjectUtils; - - -#if ($complexObjectModel) -#foreach ($col in $table.Columns) - #if ($col.isForeignKey()) - #set ( $tblFK = $table.Database.getTable($col.RelatedTableName) ) - #if ($tblFK.Interface && $tblFK.Interface.indexOf('.') != -1) -import $tblFK.Interface; - #end - #end -#end -#end $torqueGen.mergepoint("imports")## -#if ($torqueGen.booleanOption("torque.om.generateBeans")) -$torqueGen.mergepoint("objectBeanImports") -#end -#if ($peerPackage != $dbObjectPackage) -import ${peerPackage}.${peerClassName}; -#end -$torqueGen.mergepoint("classJavadoc") +$torqueGen.mergepoint("classJavadoc")## +$torqueGen.mergepoint("classAnnotations")## #if ($baseClass == "") #set ($extendsBaseClass = "" ) #else @@ -160,6 +110,9 @@ $torqueGen.mergepoint("objectBeanMethods #if (${useManagers} == "true") $torqueGen.mergepoint("isCacheOnSave") #end +$torqueGen.mergepoint("getCurrentDate")## +$torqueGen.mergepoint("getCurrentTime")## +$torqueGen.mergepoint("getCurrentTimestamp")## $torqueGen.mergepoint("toString") $torqueGen.mergepoint("equalsHashCode") Added: db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/dbObject/base/getCurrentDate.vm URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/dbObject/base/getCurrentDate.vm?rev=1363653&view=auto ============================================================================== --- db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/dbObject/base/getCurrentDate.vm (added) +++ db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/dbObject/base/getCurrentDate.vm Fri Jul 20 04:31:30 2012 @@ -0,0 +1,48 @@ +## 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. +## +###### +## +## version $Id: baseDbObject.vm 1337528 2012-05-12 12:55:09Z tfischer $ +## +## This template creates the getCurrentDate method for setting CURRENT_DATE +## default values. +## +## The template expects as input a "table" element from the torque schema +## which was processed by the OMTransformer. +## +## The template expects as input a "table" element from the torque schema +## which was processed by the OMTransformer. +## +#set ($getDefaultDateMethodName = ${torqueGen.getAttribute("getDefaultDateMethodName")}) +#if (${getDefaultDateMethodName}) + /** + * Returns the current java date for use as default value. + * + * @return the current java date. + */ + protected Date ${getDefaultDateMethodName}() + { + GregorianCalendar calendar = new GregorianCalendar(); + calendar.set(GregorianCalendar.HOUR_OF_DAY, 0); + calendar.set(GregorianCalendar.MINUTE, 0); + calendar.set(GregorianCalendar.SECOND, 0); + calendar.set(GregorianCalendar.MILLISECOND, 0); + return calendar.getTime(); + } + +#end \ No newline at end of file Added: db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/dbObject/base/getCurrentTime.vm URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/dbObject/base/getCurrentTime.vm?rev=1363653&view=auto ============================================================================== --- db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/dbObject/base/getCurrentTime.vm (added) +++ db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/dbObject/base/getCurrentTime.vm Fri Jul 20 04:31:30 2012 @@ -0,0 +1,42 @@ +## 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. +## +###### +## +## version $Id: baseDbObject.vm 1337528 2012-05-12 12:55:09Z tfischer $ +## +## This template creates the getCurrentTime method for setting CURRENT_TIME +## default values. +## +## The template expects as input a "table" element from the torque schema +## which was processed by the OMTransformer. +## +#set ($getDefaultTimeMethodName = ${torqueGen.getAttribute("getDefaultTimeMethodName")}) +#if (${getDefaultTimeMethodName}) + /** + * Returns the current java time for use as default value. + * + * @return the current java time. + */ + protected Date ${getDefaultTimeMethodName}() + { + GregorianCalendar calendar = new GregorianCalendar(); + calendar.set(1970,01,01); + return calendar.getTime(); + } + + #end \ No newline at end of file Added: db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/dbObject/base/getCurrentTimestamp.vm URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/dbObject/base/getCurrentTimestamp.vm?rev=1363653&view=auto ============================================================================== --- db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/dbObject/base/getCurrentTimestamp.vm (added) +++ db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/dbObject/base/getCurrentTimestamp.vm Fri Jul 20 04:31:30 2012 @@ -0,0 +1,40 @@ +## 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. +## +###### +## +## version $Id: baseDbObject.vm 1337528 2012-05-12 12:55:09Z tfischer $ +## +## This template creates the getCurrentTimestamp method for setting +## CURRENT_TIMESTAMP default values. +## +## The template expects as input a "table" element from the torque schema +## which was processed by the OMTransformer. +## +#set ($getDefaultTimestampMethodName = ${torqueGen.getAttribute("getDefaultTimestampMethodName")}) +#if (${getDefaultTimestampMethodName}) + /** + * Returns the current java timestamp for use as default value. + * + * @return the current java timestamp. + */ + protected Date ${getDefaultTimestampMethodName}() + { + return new Date(); + } + +#end \ No newline at end of file Added: db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/dbObject/base/imports.vm URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/dbObject/base/imports.vm?rev=1363653&view=auto ============================================================================== --- db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/dbObject/base/imports.vm (added) +++ db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/om/templates/dbObject/base/imports.vm Fri Jul 20 04:31:30 2012 @@ -0,0 +1,80 @@ +## 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. +## +###### +## +## version $Id: baseDbObject.vm 1337528 2012-05-12 12:55:09Z tfischer $ +## +## This template creates the imports for a database object. +## +## The template expects as input a "table" element from the torque schema +## which was processed by the OMTransformer. +## +import java.io.Serializable; +import java.sql.Connection; +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.Collections; +import java.util.Date; +import java.util.GregorianCalendar; +import java.util.List; + +#if ($torqueGen.booleanOption("torque.addIntakeRetrievable")) +import $torqueGen.option("torque.retrievableInterface"); +#end +#if (!$torqueGen.booleanOption("torque.om.complexObjectModel")) +import org.apache.torque.Torque; +#end +import org.apache.torque.TorqueException; +import org.apache.torque.map.TableMap; +import org.apache.torque.om.ComboKey; +import org.apache.torque.om.DateKey; +import org.apache.torque.om.NumberKey; +import org.apache.torque.om.ObjectKey; +import org.apache.torque.om.SimpleKey; +import org.apache.torque.om.StringKey; +import org.apache.torque.om.BooleanKey; +#if ($addSaveMethods == true && $saveMethodsInDbObjects == "true") +import org.apache.torque.om.Persistent; +#end +#if ($torqueGen.booleanOption("torque.om.addGetByNameMethods")) +import org.apache.torque.om.ColumnAccessByName; +#end## +import $torqueGen.option("torque.om.criteriaClass"); +#if ($addSaveMethods == true && $saveMethodsInDbObjects == "true") +import org.apache.torque.util.Transaction; +#end +import org.apache.commons.lang.ObjectUtils; + + +#if ($complexObjectModel) +#foreach ($col in $table.Columns) + #if ($col.isForeignKey()) + #set ( $tblFK = $table.Database.getTable($col.RelatedTableName) ) + #if ($tblFK.Interface && $tblFK.Interface.indexOf('.') != -1) +import $tblFK.Interface; + #end + #end +#end +#end +$torqueGen.mergepoint("imports")## +#if ($torqueGen.booleanOption("torque.om.generateBeans")) +$torqueGen.mergepoint("objectBeanImports") +#end +#if ($peerPackage != $dbObjectPackage) +import ${peerPackage}.${peerClassName}; +#end 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=1363653&r1=1363652&r2=1363653&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 Jul 20 04:31:30 2012 @@ -25,47 +25,139 @@ - +
- + + + + + + + + + + +
+ + + + + + + + +
@@ -133,30 +225,4 @@ useDatabaseDefaultValue="true" /> - - - - - - -
Copied: db/torque/torque4/trunk/torque-test/src/test/java/org/apache/torque/generated/peer/DefaultValuesFromDatabaseTest.java (from r1356516, 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/DefaultValuesFromDatabaseTest.java?p2=db/torque/torque4/trunk/torque-test/src/test/java/org/apache/torque/generated/peer/DefaultValuesFromDatabaseTest.java&p1=db/torque/torque4/trunk/torque-test/src/test/java/org/apache/torque/generated/peer/UseDefaultValueFromDatabaseTest.java&r1=1356516&r2=1363653&rev=1363653&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/DefaultValuesFromDatabaseTest.java Fri Jul 20 04:31:30 2012 @@ -5,7 +5,11 @@ import java.sql.ResultSet; import java.sql.Statement; import java.sql.Time; import java.sql.Timestamp; +import java.sql.Types; +import java.text.ParseException; +import java.text.SimpleDateFormat; import java.util.Date; +import java.util.GregorianCalendar; import java.util.List; import org.apache.commons.logging.Log; @@ -25,8 +29,10 @@ import org.apache.torque.test.CurrentTim 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.test.DatabaseDefaultValues; +import org.apache.torque.test.DatabaseDefaultValuesPeer; +import org.apache.torque.util.ColumnValues; +import org.apache.torque.util.JdbcTypedValue; /* * Licensed to the Apache Software Foundation (ASF) under one @@ -52,21 +58,48 @@ import org.apache.torque.test.DefaultVal * * @version $Id$ */ -public class UseDefaultValueFromDatabaseTest extends BaseDatabaseTestCase +public class DefaultValuesFromDatabaseTest extends BaseDatabaseTestCase { private static Log log - = LogFactory.getLog(UseDefaultValueFromDatabaseTest.class); + = LogFactory.getLog(DefaultValuesFromDatabaseTest.class); + + /** The default date format. */ + private static String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss"; /** * Checks the java default values. */ - public void testNewObjectJavaDefault() throws Exception + public void testJavaDefault() throws Exception { - DefaultValues defaultValues = new DefaultValues(); - assertEquals(null, defaultValues.getOIntegerDefaultFromDb()); - assertEquals(new Integer(3), defaultValues.getOIntegerWithDefault()); - assertEquals(0, defaultValues.getPIntDefaultFromDb()); - assertEquals(5, defaultValues.getPIntWithDefault()); + DatabaseDefaultValues databaseDefaultValues + = new DatabaseDefaultValues(); + assertEquals(Integer.valueOf(2), databaseDefaultValues.getOInteger()); + assertEquals(4, databaseDefaultValues.getPInt()); + assertEquals("Default!", databaseDefaultValues.getVarcharField()); + // For date values, java fields are null if the default is read + // from the database. This is because different databases + // handle dates differently so it is difficult to predict + // from java side what the database value will be. + assertEquals(null, databaseDefaultValues.getDateField()); + assertEquals(null, databaseDefaultValues.getTimeField()); + assertEquals(null, databaseDefaultValues.getTimestampField()); + } + + /** + * Checks that the buildColumnValues for an unchanged object + * does not contain the values with database default. + */ + public void testGetColumnValuesNewUnchangedObject() throws Exception + { + DatabaseDefaultValues databaseDefaultValues + = new DatabaseDefaultValues(); + ColumnValues columnValues + = DatabaseDefaultValuesPeer.buildColumnValues( + databaseDefaultValues); + assertEquals(1, columnValues.size()); + assertEquals( + columnValues.get(DatabaseDefaultValuesPeer.NORMAL_PAYLOAD), + new JdbcTypedValue(0, Types.INTEGER)); } /** @@ -74,29 +107,39 @@ public class UseDefaultValueFromDatabase */ public void testNewObjectDatabaseDefault() throws Exception { - DefaultValuesPeer.doDelete(new Criteria()); - DefaultValues defaultValues = new DefaultValues(); + DatabaseDefaultValuesPeer.doDelete(new Criteria()); + DatabaseDefaultValues databaseDefaultValues + = new DatabaseDefaultValues(); - defaultValues.save(); + databaseDefaultValues.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()); + assertEquals(Integer.valueOf(2), databaseDefaultValues.getOInteger()); + assertEquals(4, databaseDefaultValues.getPInt()); + assertEquals("Default!", databaseDefaultValues.getVarcharField()); + assertEquals(null, databaseDefaultValues.getDateField()); + assertEquals(null, databaseDefaultValues.getTimeField()); + assertEquals(null, databaseDefaultValues.getTimestampField()); // re-loading should give the database default values - List defaultValuesList - = DefaultValuesPeer.doSelect(new Criteria()); - assertEquals(1, defaultValuesList.size()); - DefaultValues defaultValuesSaved = defaultValuesList.get(0); + List databaseDefaultValuesList + = DatabaseDefaultValuesPeer.doSelect(new Criteria()); + assertEquals(1, databaseDefaultValuesList.size()); + DatabaseDefaultValues databaseDefaultValuesSaved + = databaseDefaultValuesList.get(0); assertEquals( new Integer(2), - defaultValuesSaved.getOIntegerDefaultFromDb()); - assertEquals( - new Integer(3), - defaultValuesSaved.getOIntegerWithDefault()); - assertEquals(4, defaultValuesSaved.getPIntDefaultFromDb()); - assertEquals(5, defaultValuesSaved.getPIntWithDefault()); + databaseDefaultValuesSaved.getOInteger()); + assertEquals(4, databaseDefaultValuesSaved.getPInt()); + assertEquals("Default!", databaseDefaultValuesSaved.getVarcharField()); + assertEquals(toString(doSelect(toDate("2010-09-08 00:00:00"), + java.sql.Date.class)), + toString(databaseDefaultValuesSaved.getDateField())); + assertEquals(toString(doSelect(toDate("1970-01-01 10:20:30"), + java.sql.Time.class)), + toString(databaseDefaultValuesSaved.getTimeField())); + assertEquals(toString(doSelect(toDate("2010-09-08 11:12:13"), + java.sql.Timestamp.class)), + toString(databaseDefaultValuesSaved.getTimestampField())); } /** @@ -105,54 +148,114 @@ public class UseDefaultValueFromDatabase */ public void testNewObjectChangedValue() throws Exception { - DefaultValuesPeer.doDelete(new Criteria()); - DefaultValues defaultValues = new DefaultValues(); - defaultValues.setOIntegerDefaultFromDb(6); - defaultValues.setOIntegerWithDefault(7); - defaultValues.setPIntDefaultFromDb(8); - defaultValues.setPIntWithDefault(9); + DatabaseDefaultValuesPeer.doDelete(new Criteria()); + DatabaseDefaultValues databaseDefaultValues + = new DatabaseDefaultValues(); + databaseDefaultValues.setOInteger(1); + databaseDefaultValues.setPInt(3); + databaseDefaultValues.setVarcharField("Changed!"); + databaseDefaultValues.setDateField( + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss") + .parse("2000-01-02 00:00:00")); + databaseDefaultValues.setTimeField( + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss") + .parse("1970-01-01 11:21:31")); + databaseDefaultValues.setTimestampField( + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss") + .parse("2010-09-08 12:13:14")); - defaultValues.save(); + databaseDefaultValues.save(); - List defaultValuesList - = DefaultValuesPeer.doSelect(new Criteria()); - assertEquals(1, defaultValuesList.size()); - DefaultValues defaultValuesSaved = defaultValuesList.get(0); + // saved object should stay the same + assertEquals(Integer.valueOf(1), databaseDefaultValues.getOInteger()); + assertEquals(3, databaseDefaultValues.getPInt()); + assertEquals("Changed!", databaseDefaultValues.getVarcharField()); + assertEquals( + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss") + .parse("2000-01-02 00:00:00"), + databaseDefaultValues.getDateField()); + assertEquals( + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss") + .parse("1970-01-01 11:21:31"), + databaseDefaultValues.getTimeField()); assertEquals( - new Integer(6), - defaultValuesSaved.getOIntegerDefaultFromDb()); + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss") + .parse("2010-09-08 12:13:14"), + databaseDefaultValues.getTimestampField()); + List defaultValuesList + = DatabaseDefaultValuesPeer.doSelect(new Criteria()); + assertEquals(1, defaultValuesList.size()); + DatabaseDefaultValues databaseDefaultValuesSaved + = defaultValuesList.get(0); assertEquals( - new Integer(7), - defaultValuesSaved.getOIntegerWithDefault()); - assertEquals(8, defaultValuesSaved.getPIntDefaultFromDb()); - assertEquals(9, defaultValuesSaved.getPIntWithDefault()); + new Integer(1), + databaseDefaultValuesSaved.getOInteger()); + assertEquals(3, databaseDefaultValuesSaved.getPInt()); + assertEquals("Changed!", databaseDefaultValuesSaved.getVarcharField()); + assertEquals(toString(doSelect(toDate("2000-01-02 00:00:00"), + java.sql.Date.class)), + toString(databaseDefaultValuesSaved.getDateField())); + assertEquals(toString(doSelect(toDate("1970-01-01 11:21:31"), + java.sql.Time.class)), + toString(databaseDefaultValuesSaved.getTimeField())); + assertEquals(toString(doSelect(toDate("2010-09-08 12:13:14"), + java.sql.Timestamp.class)), + toString(databaseDefaultValuesSaved.getTimestampField())); } /** - * Tests that values are saved if they are not equal to the default value - * on a new object. + * Checks the second save also saves unchanged values. */ - public void testNotNewObjectChangedValue() throws Exception + public void testNotNewObjectSavesUnchangedValues() throws Exception { - DefaultValuesPeer.doDelete(new Criteria()); - DefaultValues defaultValues = new DefaultValues(); - defaultValues.save(); - - defaultValues.save(); + DatabaseDefaultValuesPeer.doDelete(new Criteria()); + DatabaseDefaultValues databaseDefaultValues + = new DatabaseDefaultValues(); + databaseDefaultValues.save(); + // modify object in db so we can check that the unchanged values + // are saved + List databaseDefaultValuesList + = DatabaseDefaultValuesPeer.doSelect(new Criteria()); + assertEquals(1, databaseDefaultValuesList.size()); + DatabaseDefaultValues changedValuesInDatabase + = databaseDefaultValuesList.get(0); + changedValuesInDatabase.setOInteger(1); + changedValuesInDatabase.setPInt(3); + changedValuesInDatabase.setVarcharField("Changed!"); + changedValuesInDatabase.setDateField( + new GregorianCalendar(1990, 2, 4).getTime()); + changedValuesInDatabase.setTimeField(new Date(2500L)); + changedValuesInDatabase.setTimestampField( + new GregorianCalendar(1990, 2, 4).getTime()); + changedValuesInDatabase.save(); + databaseDefaultValues.setModified(true); // second save behaves differently because object is not new any more - List defaultValuesList - = DefaultValuesPeer.doSelect(new Criteria()); - assertEquals(1, defaultValuesList.size()); - DefaultValues defaultValuesSaved = defaultValuesList.get(0); + // unchanged values should also be saved + databaseDefaultValues.save(); + + // saved object should stay the same + assertEquals(Integer.valueOf(2), databaseDefaultValues.getOInteger()); + assertEquals(4, databaseDefaultValues.getPInt()); + assertEquals("Default!", databaseDefaultValues.getVarcharField()); + assertEquals(null, databaseDefaultValues.getDateField()); + assertEquals(null, databaseDefaultValues.getTimeField()); + assertEquals(null, databaseDefaultValues.getTimestampField()); + // re-loading should give the unchanged values + // (changes in the db were overwritten by the second save) + databaseDefaultValuesList + = DatabaseDefaultValuesPeer.doSelect(new Criteria()); + assertEquals(1, databaseDefaultValuesList.size()); + DatabaseDefaultValues databaseDefaultValuesSaved + = databaseDefaultValuesList.get(0); assertEquals( new Integer(2), - defaultValuesSaved.getOIntegerDefaultFromDb()); - assertEquals( - new Integer(3), - defaultValuesSaved.getOIntegerWithDefault()); - assertEquals(4, defaultValuesSaved.getPIntDefaultFromDb()); - assertEquals(5, defaultValuesSaved.getPIntWithDefault()); + databaseDefaultValuesSaved.getOInteger()); + assertEquals(4, databaseDefaultValuesSaved.getPInt()); + assertEquals("Default!", databaseDefaultValuesSaved.getVarcharField()); + assertEquals(null, databaseDefaultValuesSaved.getDateField()); + assertEquals(null, databaseDefaultValuesSaved.getTimeField()); + assertEquals(null, databaseDefaultValuesSaved.getTimestampField()); } /** @@ -254,6 +357,45 @@ public class UseDefaultValueFromDatabase currentTimestampAfter)); } + private Date doSelect(Date toSelect, Class classToSelect) + throws Exception + { + String dateFormat; + if (defaultAdapter instanceof OracleAdapter) + { + dateFormat = "''dd-MM-yyyy HH:mm:ss''"; + } + else if (defaultAdapter instanceof MysqlAdapter) + { + dateFormat = "''yyyyMMddHHmmss''"; + } + else if (defaultAdapter instanceof MssqlAdapter) + { + dateFormat = "''yyyyMMdd HH:mm:ss''"; + } + else if (defaultAdapter instanceof HsqldbAdapter) + { + if (classToSelect == java.sql.Date.class) + { + dateFormat = "''yyyy-MM-dd''"; + } + else if (classToSelect == java.sql.Time.class) + { + dateFormat = "''HH:mm:ss''"; + } + else + { + dateFormat = "''yyyy-MM-dd HH:mm:ss''"; + } + } + else + { + dateFormat = "''yyyy-MM-dd HH:mm:ss''"; + } + String dateString = new SimpleDateFormat(dateFormat).format(toSelect); + return doSelect(dateString, classToSelect); + } + private Date doSelect(String toSelect, Class classToSelect) throws Exception { @@ -367,9 +509,19 @@ public class UseDefaultValueFromDatabase { if (defaultAdapter instanceof PostgresAdapter) { - log.warn("Timezone is buggy in CUTTENT_TIME in Postgres"); + log.warn("Timezone is buggy in CURRENT_TIME in Postgres"); return true; } return false; } + + private static Date toDate(String toConvert) throws ParseException + { + return new SimpleDateFormat(DATE_FORMAT).parse(toConvert); + } + + private static String toString(Date toConvert) + { + return new SimpleDateFormat(DATE_FORMAT).format(toConvert); + } } Copied: db/torque/torque4/trunk/torque-test/src/test/java/org/apache/torque/generated/peer/DefaultValuesFromJavaTest.java (from r1356516, 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/DefaultValuesFromJavaTest.java?p2=db/torque/torque4/trunk/torque-test/src/test/java/org/apache/torque/generated/peer/DefaultValuesFromJavaTest.java&p1=db/torque/torque4/trunk/torque-test/src/test/java/org/apache/torque/generated/peer/DefaultValueTest.java&r1=1356516&r2=1363653&rev=1363653&view=diff ============================================================================== --- db/torque/torque4/trunk/torque-test/src/test/java/org/apache/torque/generated/peer/DefaultValueTest.java (original) +++ db/torque/torque4/trunk/torque-test/src/test/java/org/apache/torque/generated/peer/DefaultValuesFromJavaTest.java Fri Jul 20 04:31:30 2012 @@ -1,9 +1,12 @@ package org.apache.torque.generated.peer; +import java.text.SimpleDateFormat; import java.util.Date; +import java.util.GregorianCalendar; +import java.util.TimeZone; import org.apache.torque.BaseDatabaseTestCase; -import org.apache.torque.test.JavaDefaultTable; +import org.apache.torque.test.JavaDefaultValues; /* * Licensed to the Apache Software Foundation (ASF) under one @@ -29,8 +32,11 @@ import org.apache.torque.test.JavaDefaul * * @version $Id: UseDefaultValueFromDatabaseTest.java 1333015 2012-05-02 12:36:03Z tfischer $ */ -public class DefaultValueTest extends BaseDatabaseTestCase +public class DefaultValuesFromJavaTest extends BaseDatabaseTestCase { + /** The default date format. */ + private static String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss"; + /** * Checks that if CURRENT_DATE is used as default value * then an object is constructed with current java time. @@ -39,46 +45,156 @@ public class DefaultValueTest extends Ba */ public void testCurrentDateAsJavaDefault() throws Exception { + GregorianCalendar currentCalendarBefore = new GregorianCalendar(); + currentCalendarBefore.set(GregorianCalendar.HOUR_OF_DAY, 0); + currentCalendarBefore.set(GregorianCalendar.MINUTE, 0); + currentCalendarBefore.set(GregorianCalendar.SECOND, 0); + currentCalendarBefore.set(GregorianCalendar.MILLISECOND, 0); + JavaDefaultValues javaDefaultValues = new JavaDefaultValues(); + GregorianCalendar currentCalendarAfter = new GregorianCalendar(); + currentCalendarAfter.set(GregorianCalendar.HOUR_OF_DAY, 0); + currentCalendarAfter.set(GregorianCalendar.MINUTE, 0); + currentCalendarAfter.set(GregorianCalendar.SECOND, 0); + currentCalendarAfter.set(GregorianCalendar.MILLISECOND, 0); + + assertFalse( + "currentDate should be >= currentDateBefore", + javaDefaultValues.getCurrentDateValue().before( + currentCalendarBefore.getTime())); + assertFalse( + "currentDate should be <= currentDateAfter", + javaDefaultValues.getCurrentDateValue().after( + currentCalendarAfter.getTime())); + } + + /** + * Checks that if CURRENT_TIME is used as default value + * then an object is constructed with current java time. + * + * @throws Exception if an error occurs. + */ + public void testCurrentTimeAsJavaDefault() throws Exception + { + GregorianCalendar currentCalendarBefore = new GregorianCalendar(); + currentCalendarBefore.set(1970, 1, 1); + JavaDefaultValues javaDefaultValues = new JavaDefaultValues(); + GregorianCalendar currentCalendarAfter = new GregorianCalendar(); + currentCalendarAfter.set(1970, 1, 1); + assertFalse( + "currentTime should be >= currentCalendarBefore", + javaDefaultValues.getCurrentTimeValue().before( + currentCalendarBefore.getTime())); + assertFalse( + "currentTime should be <= currentDateAfter", + javaDefaultValues.getCurrentTimeValue().after( + currentCalendarAfter.getTime())); + } + + /** + * Checks that if CURRENT_TIMESTAMP is used as default value + * then an object is constructed with current java time. + * + * @throws Exception if an error occurs. + */ + public void testCurrentTimestampAsJavaDefault() throws Exception + { Date currentDateBefore = new Date(); - JavaDefaultTable javaDefaultTable - = new JavaDefaultTable(); + JavaDefaultValues javaDefaultValues = new JavaDefaultValues(); Date currentDateAfter = new Date(); assertFalse( - "currentDate should be >= currentDateBefore", - javaDefaultTable.getCurrentDateValue().before( + "currentTime should be >= currentDateBefore", + javaDefaultValues.getCurrentTimestampValue().before( currentDateBefore)); assertFalse( - "currentDate should be <= currentDateAfter", - javaDefaultTable.getCurrentDateValue().after( + "currentTime should be <= currentDateAfter", + javaDefaultValues.getCurrentTimestampValue().after( currentDateAfter)); } /** - * Checks that we can set a default value to an Integer column. + * Checks that we can set a java default value to an Integer column. * * @throws Exception if an error occurs. */ public void testIntegerDefault() throws Exception { - JavaDefaultTable javaDefaultTable - = new JavaDefaultTable(); + JavaDefaultValues javaDefaultValues = new JavaDefaultValues(); + + assertEquals( + Integer.valueOf(2), + javaDefaultValues.getOInteger()); + } + + /** + * Checks that we can set a java default value to an int column. + * + * @throws Exception if an error occurs. + */ + public void testIntDefault() throws Exception + { + JavaDefaultValues javaDefaultValues = new JavaDefaultValues(); - assertEquals(27, javaDefaultTable.getIntegerValue()); + assertEquals(4, javaDefaultValues.getPInt()); } /** - * Checks that we can set a default value to an Varchar column. + * Checks that we can set a java default value to an Varchar column. * * @throws Exception if an error occurs. */ public void testVarcharDefault() throws Exception { - JavaDefaultTable javaDefaultTable - = new JavaDefaultTable(); + JavaDefaultValues javaDefaultValues = new JavaDefaultValues(); + + assertEquals("Default!", javaDefaultValues.getVarcharField()); + } + + + /** + * Checks that we can set a default value to an Date column. + * + * @throws Exception if an error occurs. + */ + public void testDateDefault() throws Exception + { + JavaDefaultValues javaDefaultValues = new JavaDefaultValues(); + + assertEquals("2010-09-08 00:00:00", + toString(javaDefaultValues.getDateField())); + } + + /** + * Checks that we can set a default value to an Date column. + * + * @throws Exception if an error occurs. + */ + public void testTimeDefault() throws Exception + { + JavaDefaultValues javaDefaultValues = new JavaDefaultValues(); - assertEquals("Default", javaDefaultTable.getVarcharValue()); + assertEquals("1970-01-01 10:20:30", + toString(javaDefaultValues.getTimeField())); } + /** + * Checks that we can set a default value to an Date column. + * + * @throws Exception if an error occurs. + */ + public void testTimestampDefault() throws Exception + { + JavaDefaultValues javaDefaultValues = new JavaDefaultValues(); + assertEquals( + "2010-09-08 11:12:13", + toString(javaDefaultValues.getTimestampField())); + } + + private static String toString(Date toConvert) + { + SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT); + dateFormat.setTimeZone(TimeZone.getTimeZone("GMT")); + return dateFormat.format(toConvert); + } } --------------------------------------------------------------------- To unsubscribe, e-mail: torque-dev-unsubscribe@db.apache.org For additional commands, e-mail: torque-dev-help@db.apache.org