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