1 package org.apache.torque.engine.database.model;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
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
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 }