db-torque-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From tfisc...@apache.org
Subject svn commit: r997971 - in /db/torque/torque4/trunk/torque-templates/src/main/java/org/apache/torque/templates/transformer: om/mapInit/MapInitDatabaseTransformer.java sql/SQLTransformer.java sql/SqlAttributeName.java
Date Fri, 17 Sep 2010 04:12:50 GMT
Author: tfischer
Date: Fri Sep 17 04:12:50 2010
New Revision: 997971

URL: http://svn.apache.org/viewvc?rev=997971&view=rev
Log:
- javadoc
- moved attribute names to separate enum
- removed unnecessary throws clauses

Added:
    db/torque/torque4/trunk/torque-templates/src/main/java/org/apache/torque/templates/transformer/sql/SqlAttributeName.java
Modified:
    db/torque/torque4/trunk/torque-templates/src/main/java/org/apache/torque/templates/transformer/om/mapInit/MapInitDatabaseTransformer.java
    db/torque/torque4/trunk/torque-templates/src/main/java/org/apache/torque/templates/transformer/sql/SQLTransformer.java

Modified: db/torque/torque4/trunk/torque-templates/src/main/java/org/apache/torque/templates/transformer/om/mapInit/MapInitDatabaseTransformer.java
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-templates/src/main/java/org/apache/torque/templates/transformer/om/mapInit/MapInitDatabaseTransformer.java?rev=997971&r1=997970&r2=997971&view=diff
==============================================================================
--- db/torque/torque4/trunk/torque-templates/src/main/java/org/apache/torque/templates/transformer/om/mapInit/MapInitDatabaseTransformer.java
(original)
+++ db/torque/torque4/trunk/torque-templates/src/main/java/org/apache/torque/templates/transformer/om/mapInit/MapInitDatabaseTransformer.java
Fri Sep 17 04:12:50 2010
@@ -31,10 +31,22 @@ import org.apache.torque.templates.Torqu
 import org.apache.torque.templates.TorqueSchemaElementName;
 import org.apache.torque.templates.transformer.om.DatabaseAttributeName;
 
+/**
+ * A transformer providing extra attributes for the genreration of the
+ * mapInit classes.
+ *
+ * @version $Id: $
+ */
 public class MapInitDatabaseTransformer  extends AttributeTransformer
 {
+    /** The camelbacker for creating the MapInit class name. */
     private static Camelbacker camelbacker = new Camelbacker();
 
+    /**
+     * Constructor.
+     *
+     * @throws SourceTransformerException if the attribute map is malformed.
+     */
     public MapInitDatabaseTransformer() throws SourceTransformerException
     {
         super(new InputStreamReader(

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=997971&r1=997970&r2=997971&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 Sep 17 04:12:50 2010
@@ -43,34 +43,10 @@ import org.apache.torque.templates.typem
 import org.apache.torque.templates.typemapping.TypeMap;
 
 /**
- * Transforms the tables in the OM model.
+ * Transforms the tables in the OM model for sql generation.
  */
 public class SQLTransformer implements SourceTransformer
 {
-    private static final String PRIMARY_KEY_COLUMN_NAMES_ATTRIBUTE
-            = "primaryKeyColumnNames";
-
-    private static final String UNIQUE_COLUMN_NAMES_ATTRIBUTE
-            = "uniqueColumnNames";
-
-    private static final String INDEX_COLUMN_NAMES_ATTRIBUTE
-            = "indexColumnNames";
-
-    private static final String LOCAL_COLUMN_NAMES_ATTRIBUTE
-            = "localColumnNames";
-
-    private static final String FOREIGN_COLUMN_NAMES_ATTRIBUTE
-            = "foreignColumnNames";
-
-    private static final String PRIMARY_KEY_CONSTRAINT_NAME_ATTRIBUTE
-            = "primaryKeyConstraintName";
-
-    private static final String SEQUENCE_NAME_ATTRIBUTE
-            = "sequenceName";
-
-    private static final String DDL_SQL_ATTRIBUTE
-            = "ddlSql";
-
     private CollectAttributeSetTrueTransformer collectAttributeSetTrueTransformer
             = new CollectAttributeSetTrueTransformer();
 
@@ -84,7 +60,7 @@ public class SQLTransformer implements S
         if (databaseOption == null)
         {
             throw new SourceTransformerException(
-                    "Option torque:database must be set");
+                    "Option torque.database must be set");
         }
 
         List<SourceElement> tableElements = databaseElement.getChildren(
@@ -121,7 +97,7 @@ public class SQLTransformer implements S
         }
         String tableName = (String) tableElement.getAttribute(
                 TorqueSchemaAttributeName.NAME.getName());
-        if (tableElement.getAttribute(PRIMARY_KEY_CONSTRAINT_NAME_ATTRIBUTE)
+        if (tableElement.getAttribute(SqlAttributeName.PRIMARY_KEY_CONSTRAINT_NAME)
                 == null)
         {
             // TODO: Column length from database descriptor
@@ -129,10 +105,10 @@ public class SQLTransformer implements S
                 (tableName.length() > 27 ? tableName.substring(0, 26) : tableName)
                   + "_PK";
             tableElement.setAttribute(
-                    PRIMARY_KEY_CONSTRAINT_NAME_ATTRIBUTE,
+                    SqlAttributeName.PRIMARY_KEY_CONSTRAINT_NAME,
                     primaryKeyConstraintName);
         }
-        if (tableElement.getAttribute(SEQUENCE_NAME_ATTRIBUTE)
+        if (tableElement.getAttribute(SqlAttributeName.SEQUENCE_NAME)
                 == null)
         {
             // TODO: Sequence name length from database descriptor
@@ -140,7 +116,7 @@ public class SQLTransformer implements S
                 (tableName.length() > 27 ? tableName.substring(0, 26) : tableName)
                   + "_SEQ";
             tableElement.setAttribute(
-                    SEQUENCE_NAME_ATTRIBUTE,
+                    SqlAttributeName.SEQUENCE_NAME,
                     sequenceName);
         }
 
@@ -167,7 +143,7 @@ public class SQLTransformer implements S
             firstPk = false;
         }
         tableElement.setAttribute(
-                PRIMARY_KEY_COLUMN_NAMES_ATTRIBUTE,
+                SqlAttributeName.PRIMARY_KEY_COLUMN_NAMES,
                 primaryKeyColumnNames.toString());
 
         // unique
@@ -190,7 +166,7 @@ public class SQLTransformer implements S
                     TorqueSchemaElementName.UNIQUE_COLUMN.getName(),
                     TorqueSchemaAttributeName.NAME.getName());
             uniqueElement.setAttribute(
-                    UNIQUE_COLUMN_NAMES_ATTRIBUTE,
+                    SqlAttributeName.UNIQUE_COLUMN_NAMES,
                     uniqueColumnNames);
             ++uniqueIndex;
         }
@@ -204,7 +180,7 @@ public class SQLTransformer implements S
                     TorqueSchemaElementName.INDEX_COLUMN.getName(),
                     TorqueSchemaAttributeName.NAME.getName());
             indexElement.setAttribute(
-                    INDEX_COLUMN_NAMES_ATTRIBUTE,
+                    SqlAttributeName.INDEX_COLUMN_NAMES,
                     indexColumnNames);
         }
 
@@ -228,20 +204,41 @@ public class SQLTransformer implements S
         }
     }
 
+    /**
+     * Enriches the column elements with additional attributes
+     * needed for SQL generation.
+     *
+     * @param columnElement the element to enrich, not null.
+     * @param controllerState the current controller state, not null.
+     *
+     * @throws SourceTransformerException if the name or type attributes
+     *         are not set or if the type is unknown.
+     */
     private void transformColumn(
             SourceElement columnElement,
             ControllerState controllerState)
         throws SourceTransformerException
     {
         String sql = (String) columnElement.getAttribute(
-                DDL_SQL_ATTRIBUTE);
+                SqlAttributeName.DDL_SQL);
         if (sql == null)
         {
             sql = getSql(columnElement, controllerState);
-            columnElement.setAttribute(DDL_SQL_ATTRIBUTE, sql);
+            columnElement.setAttribute(SqlAttributeName.DDL_SQL, sql);
         }
     }
 
+    /**
+     * Creates the SQL for defining a column.
+     *
+     * @param columnElement the column element for which the SQL
+     *        should be created, not null.
+     * @param controllerState the current controller state, not null.
+     *
+     * @return the SQL for defining the column, not null.
+     *
+     * @throws SourceTransformerException
+     */
     private String getSql(
             SourceElement columnElement,
             ControllerState controllerState)
@@ -344,11 +341,17 @@ public class SQLTransformer implements S
         return StringUtils.join(resultList.iterator(), ' ');
     }
 
+    /**
+     * Sets additional attributes on foreign key elements.
+     *
+     * @param foreignKeyElement the foreign key element to enrich, not null.
+     * @param controllerState the current controller state, not null.
+     * @param fkIndex the number of the foreign-key element in the table.
+     */
     private void transformForeignKey(
             SourceElement foreignKeyElement,
             ControllerState controllerState,
             int fkIndex)
-        throws SourceTransformerException
     {
         if (foreignKeyElement.getAttribute(
                     TorqueSchemaAttributeName.NAME.getName())
@@ -365,17 +368,32 @@ public class SQLTransformer implements S
                 TorqueSchemaElementName.REFERENCE.getName(),
                 TorqueSchemaAttributeName.LOCAL.getName());
         foreignKeyElement.setAttribute(
-                LOCAL_COLUMN_NAMES_ATTRIBUTE,
+                SqlAttributeName.LOCAL_COLUMN_NAMES,
                 localColumnsNames);
         String foreignColumnsNames = collectAttributes(
                 foreignKeyElement,
                 TorqueSchemaElementName.REFERENCE.getName(),
                 TorqueSchemaAttributeName.FOREIGN.getName());
         foreignKeyElement.setAttribute(
-                FOREIGN_COLUMN_NAMES_ATTRIBUTE,
+                SqlAttributeName.FOREIGN_COLUMN_NAMES,
                 foreignColumnsNames);
     }
 
+    /**
+     * Collects attribute values in a comma-separated string.
+     * The elements on which the attribute values are read can be reached
+     * from <code>rootElement</code> via the path <code>sourcePath</code>;
+     * and on these elements, the attributes with name
+     * <code>attributeName</code> are collected.
+     *
+     * @param rootElement the base element from where to start, not null.
+     * @param sourcePath the path from <code>rootElement</code> to the elements
+     *        on which to collect the attributes.
+     * @param attributeName the name of the attributes to collect.
+     *
+     * @return a comma-separated (", ") String containing all the matching
+     *         attribute values.
+     */
     private String collectAttributes(
             SourceElement rootElement,
             String sourcePath,
@@ -384,15 +402,17 @@ public class SQLTransformer implements S
         StringBuilder result = new StringBuilder();
         List<SourceElement> toCollectFrom = SourcePath.getElements(
                 rootElement, sourcePath);
-        boolean firstElement = true;
         for (SourceElement sourceElement : toCollectFrom)
         {
-            if (!firstElement)
+            Object attributeValue = sourceElement.getAttribute(attributeName);
+            if (attributeValue != null)
             {
-                result.append(", ");
+                if (result.length() > 0)
+                {
+                    result.append(", ");
+                }
+                result.append(sourceElement.getAttribute(attributeName));
             }
-            result.append(sourceElement.getAttribute(attributeName));
-            firstElement = false;
         }
         return result.toString();
     }

Added: db/torque/torque4/trunk/torque-templates/src/main/java/org/apache/torque/templates/transformer/sql/SqlAttributeName.java
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-templates/src/main/java/org/apache/torque/templates/transformer/sql/SqlAttributeName.java?rev=997971&view=auto
==============================================================================
--- db/torque/torque4/trunk/torque-templates/src/main/java/org/apache/torque/templates/transformer/sql/SqlAttributeName.java
(added)
+++ db/torque/torque4/trunk/torque-templates/src/main/java/org/apache/torque/templates/transformer/sql/SqlAttributeName.java
Fri Sep 17 04:12:50 2010
@@ -0,0 +1,80 @@
+package org.apache.torque.templates.transformer.sql;
+
+/*
+ * 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.
+ */
+
+import org.apache.torque.generator.source.SourceAttributeName;
+
+/**
+ * Contains the attributes created by the SQLTransformer.
+ */
+public enum SqlAttributeName implements SourceAttributeName
+{
+    /**
+     * The attribute contains all primary key columns
+     * in a comma-separated String.
+     */
+    PRIMARY_KEY_COLUMN_NAMES("primaryKeyColumnNames"),
+
+    /**
+     * The attribute contains all unique columns in a comma-separated String.
+     */
+    UNIQUE_COLUMN_NAMES("uniqueColumnNames"),
+
+    INDEX_COLUMN_NAMES("indexColumnNames"),
+
+    LOCAL_COLUMN_NAMES("localColumnNames"),
+
+    FOREIGN_COLUMN_NAMES("foreignColumnNames"),
+
+    PRIMARY_KEY_CONSTRAINT_NAME("primaryKeyConstraintName"),
+
+    SEQUENCE_NAME("sequenceName"),
+
+    DDL_SQL("ddlSql");
+
+    /** The name of the source element attribute, not null. */
+    private String name;
+
+    /**
+     * Constructor.
+     *
+     * @param name the name of the source element attribute, not null.
+     */
+    private SqlAttributeName(String name)
+    {
+        this.name = name;
+    }
+
+    /**
+     * Returns the name of the referenced source element attribute.
+     *
+     * @return the name of the referenced source element attribute.
+     */
+    public String getName()
+    {
+        return name;
+    }
+
+    @Override
+    public String toString()
+    {
+        return name;
+    }
+}



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