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
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 473814 2006-11-11 22:30:30Z tv $
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
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
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 }