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