View Javadoc

1   package org.apache.torque.engine.database.model;
2   
3   /*
4    * Copyright 2001-2004 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,v 1.2.2.2 2004/05/20 04:34:15 seade Exp $
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 =
63                  Integer.parseInt(db.getProperty("maxColumnNameLength"));
64              maxBodyLength = (maxColumnNameLength - namePostfix.length()
65                      - constraintNbr.length() - 2);
66  
67              if (log.isDebugEnabled())
68              {
69                  log.debug("maxColumnNameLength=" + maxColumnNameLength
70                          + " maxBodyLength=" + maxBodyLength);
71              }
72          }
73          catch (EngineException e)
74          {
75              log.error(e.getMessage(), e);
76          }
77          catch (NumberFormatException maxLengthUnknown)
78          {
79          }
80  
81          // Do any necessary trimming.
82          if (maxBodyLength != -1 && name.length() > maxBodyLength)
83          {
84              name.setLength(maxBodyLength);
85          }
86  
87          name.append(STD_SEPARATOR_CHAR).append(namePostfix)
88              .append(STD_SEPARATOR_CHAR).append(constraintNbr);
89  
90          return name.toString();
91      }
92  }