1 package org.apache.torque.templates.typemapping;
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 org.apache.commons.lang.StringUtils;
23
24 /*
25 * Licensed to the Apache Software Foundation (ASF) under one
26 * or more contributor license agreements. See the NOTICE file
27 * distributed with this work for additional information
28 * regarding copyright ownership. The ASF licenses this file
29 * to you under the Apache License, Version 2.0 (the
30 * "License"); you may not use this file except in compliance
31 * with the License. You may obtain a copy of the License at
32 *
33 * http://www.apache.org/licenses/LICENSE-2.0
34 *
35 * Unless required by applicable law or agreed to in writing,
36 * software distributed under the License is distributed on an
37 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
38 * KIND, either express or implied. See the License for the
39 * specific language governing permissions and limitations
40 * under the License.
41 */
42
43
44 /**
45 * The SQL type data for a column. Can contain additional information such as
46 * default size, scale and defaultValue.
47 * This class is immutable.
48 *
49 * @version $Id: SqlTypeWithJdbcType.java 1331196 2012-04-27 02:56:12Z tfischer $
50 */
51 public class SqlTypeWithJdbcType
52 {
53 /**
54 * The default size for the columns with this type
55 * (can be overridden in the column definition).
56 */
57 private String size;
58
59 /**
60 * The default scale for the columns with this type
61 * (can be overridden in the column definition).
62 */
63 private String scale;
64
65 /**
66 * The default "default value" for the columns with this type
67 * (can be overridden in the column definition).
68 */
69 private String defaultValue;
70
71 /**
72 * The SQL expression for the type name, not null.
73 */
74 private String sqlTypeName;
75
76 /**
77 * The jdbc type as in <code>java.sql.types</code>
78 */
79 private int jdbcType;
80
81 /**
82 * Creates a new SqlType with the given SQL Type.
83 * Size, scale and defaultValue are set to null.
84 *
85 * @param sqlType the SQL name of the SQL type, not null.
86 * @param jdbcType the jdbc type as in <code>java.sql.types</code>.
87 *
88 * @throws NullPointerException if sqlTypeName is null.
89 */
90 public SqlTypeWithJdbcType(String sqlTypeName, int jdbcType)
91 {
92 if (sqlTypeName == null)
93 {
94 throw new NullPointerException("sqlTypeName must not be null");
95 }
96 this.sqlTypeName = sqlTypeName;
97 this.jdbcType = jdbcType;
98 }
99
100 /**
101 * Creates a new SqlType with null scale and null default value.
102 *
103 * @param sqlTypeName the SQL name of the SQL type, not null.
104 * @param jdbcType the jdbc type as in <code>java.sql.types</code>.
105 * @param size the default size of the columns with this SQL type.
106 * Can be overridden in the column definition.
107 *
108 * @throws NullPointerException if sqlTypeName is null.
109 */
110 public SqlTypeWithJdbcType(
111 String sqlTypeName,
112 int jdbcType,
113 String size)
114 {
115 this(sqlTypeName, jdbcType);
116 this.size = size;
117 }
118
119 /**
120 * Creates a new SqlType with null default value.
121 *
122 * @param sqlTypeName the SQL name of the SQL type, not null.
123 * @param jdbcType the jdbc type as in <code>java.sql.types</code>.
124 * @param size the default size of the columns with this SQL type.
125 * Can be overridden in the column definition.
126 * @param scale the default scale of the columns with this SQL type.
127 * Can be overridden in the column definition.
128 *
129 * @throws NullPointerException if sqlTypeName is null.
130 */
131 public SqlTypeWithJdbcType(
132 String sqlTypeName,
133 int jdbcType,
134 String size,
135 String scale)
136 {
137 this(sqlTypeName, jdbcType, size);
138 this.scale = scale;
139 }
140
141 /**
142 * Creates a new SqlType.
143 *
144 * @param sqlTypeName the SQL name of the SQL type, not null.
145 * @param jdbcType the jdbc type as in <code>java.sql.types</code>.
146 * @param size the default size of the columns with this SQL type.
147 * Can be overridden in the column definition.
148 * @param scale the default scale of the columns with this SQL type.
149 * Can be overridden in the column definition.
150 * @param defaultValue the default "default value" of the columns with this
151 * SQL type. Can be overridden in the column definition.
152 *
153 * @throws NullPointerException if sqlTypeName is null.
154 */
155 public SqlTypeWithJdbcType(
156 String sqlTypeName,
157 int jdbcType,
158 String size,
159 String scale,
160 String defaultValue)
161 {
162 this(sqlTypeName, jdbcType, size, scale);
163 this.defaultValue = defaultValue;
164 }
165
166 /**
167 * Creates a new SqlType by copying another sql type.
168 *
169 * @param sqlType the SQL type, to copy, not null.
170 * @param size the default size of the columns with this SQL type.
171 * Can be overridden in the column definition.
172 * If null, the size from sqlType is used.
173 * @param scale the default scale of the columns with this SQL type.
174 * Can be overridden in the column definition.
175 * If null, the scale from sqlType is used.
176 * @param defaultValue the default "default value" of the columns with this
177 * SQL type. Can be overridden in the column definition.
178 * If null, the defaultValue from sqlType is used.
179 *
180 * @throws NullPointerException if sqlType is null.
181 */
182 public SqlTypeWithJdbcType(
183 SqlTypeWithJdbcType sqlType,
184 String size,
185 String scale,
186 String defaultValue)
187 {
188 this(sqlType.getSqlTypeName(), sqlType.getJdbcType());
189 if (size != null)
190 {
191 this.size = size;
192 }
193 else
194 {
195 this.size = sqlType.getSize();
196 }
197 if (scale != null)
198 {
199 this.scale = scale;
200 }
201 else
202 {
203 this.scale = sqlType.getScale();
204 }
205 if (defaultValue != null)
206 {
207 this.defaultValue = defaultValue;
208 }
209 else
210 {
211 this.defaultValue = sqlType.getDefaultValue();
212 }
213 }
214
215 /**
216 * @return Returns the scale.
217 */
218 public String getScale()
219 {
220 return scale;
221 }
222
223 /**
224 * @return Returns the size.
225 */
226 public String getSize()
227 {
228 return size;
229 }
230
231 /**
232 * @return Returns the defaultValue.
233 */
234 public String getDefaultValue()
235 {
236 return defaultValue;
237 }
238
239 /**
240 * @return Returns the SQL type name for this column type.
241 */
242 public String getSqlTypeName()
243 {
244 return sqlTypeName;
245 }
246
247 /**
248 * @return Returns the jdbc type tas in <code>java.sql.types</code>.
249 */
250 public int getJdbcType()
251 {
252 return jdbcType;
253 }
254
255 /**
256 * Return the size and scale in brackets for use in an SQL script.
257 *
258 * @return size and scale or an empty String if there are no values
259 * available.
260 */
261 public String printSize()
262 {
263 if (StringUtils.isNotBlank(size) && StringUtils.isNotBlank(scale))
264 {
265 return '(' + size + ',' + scale + ')';
266 }
267 else if (StringUtils.isNotBlank(size))
268 {
269 return '(' + size + ')';
270 }
271 else
272 {
273 return "";
274 }
275 }
276 }