View Javadoc

1   package org.apache.torque.engine.database.model;
2   
3   /*
4    * Copyright 2001-2005 The Apache Software Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License")
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  import java.util.List;
20  
21  import org.apache.commons.logging.Log;
22  import org.apache.commons.logging.LogFactory;
23  
24  import org.apache.torque.engine.EngineException;
25  
26  /***
27   * A <code>NameGenerator</code> implementation for table-specific
28   * constraints.  Conforms to the maximum column name length for the
29   * type of database in use.
30   *
31   * @author <a href="mailto:dlr@finemaltcoding.com>Daniel Rall</a>
32   * @version $Id: ConstraintNameGenerator.java 239624 2005-08-24 12:18:03Z henning $
33   */
34  public class ConstraintNameGenerator implements NameGenerator
35  {
36      /*** Logging class from commons.logging */
37      private static Log log = LogFactory.getLog(ConstraintNameGenerator.class);
38  
39      /***
40       * First element of <code>inputs</code> should be of type {@link
41       * org.apache.torque.engine.database.model.Database}, second
42       * should be a table name, third is the type identifier (spared if
43       * trimming is necessary due to database type length constraints),
44       * and the fourth is a <code>Integer</code> indicating the number
45       * of this contraint.
46       *
47       * @see org.apache.torque.engine.database.model.NameGenerator
48       */
49      public String generateName(List inputs)
50          throws EngineException
51      {
52          StringBuffer name = new StringBuffer();
53          Database db = (Database) inputs.get(0);
54          name.append((String) inputs.get(1));
55          String namePostfix = (String) inputs.get(2);
56          String constraintNbr = inputs.get(3).toString();
57  
58          // Calculate maximum RDBMS-specific column character limit.
59          int maxBodyLength = -1;
60          try
61          {
62              int maxColumnNameLength = db.getPlatform().getMaxColumnNameLength();
63              maxBodyLength = (maxColumnNameLength - namePostfix.length()
64                      - constraintNbr.length() - 2);
65  
66              if (log.isDebugEnabled())
67              {
68                  log.debug("maxColumnNameLength=" + maxColumnNameLength
69                          + " maxBodyLength=" + maxBodyLength);
70              }
71          }
72          catch (NumberFormatException maxLengthUnknown)
73          {
74          }
75  
76          // Do any necessary trimming.
77          if (maxBodyLength != -1 && name.length() > maxBodyLength)
78          {
79              name.setLength(maxBodyLength);
80          }
81  
82          name.append(STD_SEPARATOR_CHAR).append(namePostfix)
83              .append(STD_SEPARATOR_CHAR).append(constraintNbr);
84  
85          return name.toString();
86      }
87  }