01: /*
02: * Copyright 2006 Le Duc Bao, Ralf Joachim
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
05: * use this file except in compliance with the License. You may obtain a copy of
06: * the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13: * License for the specific language governing permissions and limitations under
14: * the License.
15: */
16: package org.castor.ddlgen.typeinfo;
17:
18: import org.castor.ddlgen.DDLGenConfiguration;
19: import org.castor.ddlgen.schemaobject.Field;
20:
21: /**
22: * Final TypeInfo for types having one optional length parameter.
23: *
24: * @author <a href="mailto:leducbao AT gmail DOT com">Le Duc Bao</a>
25: * @author <a href="mailto:ralf DOT joachim AT syscon DOT eu">Ralf Joachim</a>
26: * @version $Revision: 5951 $ $Date: 2006-04-25 16:09:10 -0600 (Tue, 25 Apr 2006) $
27: * @since 1.1
28: */
29: public final class OptionalLengthType extends AbstractType {
30: //--------------------------------------------------------------------------
31:
32: /** Default length parameter from ddl.properties file. Will be used if no specific
33: * length is specified at field mapping. */
34: private final Integer _defaultLength;
35:
36: /** Post fix to be appended after type. */
37: private final String _postfix;
38:
39: //--------------------------------------------------------------------------
40:
41: /**
42: * Construct a new TypeInfo instance with given JDBC type, SQL type and Configuration.
43: *
44: * @param jdbcType The JDBC type.
45: * @param sqlType The SQL type.
46: * @param conf The configuration to get default parameter values from.
47: */
48: public OptionalLengthType(final String jdbcType,
49: final String sqlType, final DDLGenConfiguration conf) {
50: this (jdbcType, sqlType, "", conf);
51: }
52:
53: /**
54: * Construct a new TypeInfo instance with given JDBC type, SQL type and Configuration.
55: *
56: * @param jdbcType The JDBC type.
57: * @param sqlType The SQL type.
58: * @param postfix Post fix to be appended after type.
59: * @param conf The configuration to get default parameter values from.
60: */
61: public OptionalLengthType(final String jdbcType,
62: final String sqlType, final String postfix,
63: final DDLGenConfiguration conf) {
64: super (jdbcType, sqlType);
65:
66: String param = PARAM_PREFIX + jdbcType + PARAM_POSTFIX_LENGTH;
67: _defaultLength = conf.getInteger(param);
68: _postfix = postfix;
69: }
70:
71: //--------------------------------------------------------------------------
72:
73: /**
74: * {@inheritDoc}
75: */
76: public String toDDL(final Field field) {
77: Integer length = field.getLength();
78: if (length == null) {
79: length = _defaultLength;
80: }
81:
82: StringBuffer sb = new StringBuffer();
83: sb.append(getSqlType());
84: if (length != null) {
85: sb.append('(').append(length).append(')');
86: }
87: sb.append(_postfix);
88:
89: return sb.toString();
90: }
91:
92: //--------------------------------------------------------------------------
93: }
|