You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@turbine.apache.org by Bill Schneider <bs...@vecna.com> on 2002/02/25 16:30:37 UTC

[PATCH] TORQUE: unique/index: default name generation

I made this patch to help solve a problem i was having with unnamed 
unique constraints.  When you create a unique constraint with no 
supplied name (<unique><unique-column ...></unique>), the resulting SQL 
will not parse for Oracle because Oracle demands named constraints and 
the SQL contains "$unique.Name" because unique.getName() is null.

It looks like the same thing goes for indexes.

Probably this is happening because the particular constructor for Index 
which sets a default name, if necessary, was not ever getting called.

It might make more sense to call createName() from loadFromXML, rather 
than generating the name lazily, but I wasn't sure if you had all the 
other relevant info loaded at that point.

-- Bill

Index: src/java/org/apache/torque/engine/database/model/Index.java
===================================================================
RCS file: 
/home/cvspublic/jakarta-turbine-torque/src/java/org/apache/torque/engine/database/model/Index.java,v
retrieving revision 1.16
diff -u -r1.16 Index.java
--- src/java/org/apache/torque/engine/database/model/Index.java	8 Nov 2001 17:20:52 -0000	1.16
+++ src/java/org/apache/torque/engine/database/model/Index.java	25 Feb 2002 15:20:01 -0000
@@ -106,14 +106,7 @@
          {
              this.indexColumns = indexColumns;

-            List inputs = new ArrayList(4);
-            inputs.add(table.getDatabase());
-            inputs.add(table.getName());
-            inputs.add("I");
-            // ASSUMPTION: This Index not yet added to the list.
-            inputs.add(new Integer(table.getIndices().length + 1));
-            indexName = NameFactory.generateName
-                (NameFactory.CONSTRAINT_GENERATOR, inputs);
+            createName();

              if (DEBUG)
              {
@@ -129,6 +122,26 @@
          }
      }

+    private void createName() throws TorqueException
+    {
+        Table table = getTable();
+        List inputs = new ArrayList(4);
+        inputs.add(table.getDatabase());
+        inputs.add(table.getName());
+        if (isUnique())
+        {
+            inputs.add("U");
+        }
+        else
+        {
+            inputs.add("I");
+        }
+        // ASSUMPTION: This Index not yet added to the list.
+        inputs.add(new Integer(table.getIndices().length + 1));
+        indexName = NameFactory.generateName
+          (NameFactory.CONSTRAINT_GENERATOR, inputs);
+    }
+
      /**
       * Imports index from an XML specification
       */
@@ -168,6 +181,18 @@
       */
      public String getName()
      {
+        if (indexName == null)
+        {
+            try
+            {
+                // generate an index name if we don't have a supplied one
+                createName();
+            }
+            catch (TorqueException e)
+            {
+                // still no name
+            }
+        }
          return indexName;
      }


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>