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: SqlType.java 1333879 2012-05-04 11:13:01Z tfischer $
50 */
51 public class SqlType
52 {
53 /**
54 * The default size for the columns with this type.
55 */
56 private String size;
57
58 /**
59 * The default scale for the columns with this type.
60 */
61 private String scale;
62
63 /**
64 * The default "default value" for the columns with this type.
65 . */
66 private String defaultValue;
67
68 /**
69 * The SQL expression for the type name, or null if unknown.
70 */
71 private String sqlTypeName;
72
73 /**
74 * Creates a new SqlType with the given SQL Type.
75 * Size, scale and defaultValue are set to null.
76 *
77 * @param sqlType the SQL name of the SQL type, or null.
78 */
79 public SqlType(String sqlTypeName)
80 {
81 this.sqlTypeName = sqlTypeName;
82 }
83
84 /**
85 * Creates a new SqlType with null scale and null default value.
86 *
87 * @param sqlTypeName the SQL name of the SQL type, not null.
88 * @param size the default size of the columns with this SQL type.
89 * Can be overridden in the column definition.
90 *
91 * @throws NullPointerException if sqlTypeName is null.
92 */
93 public SqlType(
94 String sqlTypeName,
95 String size)
96 {
97 this(sqlTypeName);
98 this.size = size;
99 }
100
101 /**
102 * Creates a new SqlType with null default value.
103 *
104 * @param sqlTypeName the SQL name of the SQL type, not null.
105 * @param size the default size of the columns with this SQL type.
106 * Can be overridden in the column definition.
107 * @param scale the default scale of the columns with this SQL type.
108 * Can be overridden in the column definition.
109 *
110 * @throws NullPointerException if sqlTypeName is null.
111 */
112 public SqlType(
113 String sqlTypeName,
114 String size,
115 String scale)
116 {
117 this(sqlTypeName, size);
118 this.scale = scale;
119 }
120
121 /**
122 * Creates a new SqlType.
123 *
124 * @param sqlTypeName the SQL name of the SQL type, not null.
125 * @param size the default size of the columns with this SQL type.
126 * Can be overridden in the column definition.
127 * @param scale the default scale of the columns with this SQL type.
128 * Can be overridden in the column definition.
129 * @param defaultValue the default "default value" of the columns with this
130 * SQL type. Can be overridden in the column definition.
131 *
132 * @throws NullPointerException if sqlTypeName is null.
133 */
134 public SqlType(
135 String sqlTypeName,
136 String size,
137 String scale,
138 String defaultValue)
139 {
140 this(sqlTypeName, size, scale);
141 this.defaultValue = defaultValue;
142 }
143
144 /**
145 * Creates a new SqlType by copying another sql type.
146 *
147 * @param sqlType the SQL type, to copy, not null.
148 * @param size the default size of the columns with this SQL type.
149 * @param scale the default scale of the columns with this SQL type.
150 ยดยด * @param defaultValue the default "default value" of the columns with this
151 * SQL type.
152 *
153 * @throws NullPointerException if sqlType is null.
154 */
155 public SqlType(
156 SqlType sqlType,
157 String size,
158 String scale,
159 String defaultValue)
160 {
161 this(sqlType.getSqlTypeName());
162 if (size != null)
163 {
164 this.size = size;
165 }
166 else
167 {
168 this.size = sqlType.getSize();
169 }
170 if (scale != null)
171 {
172 this.scale = scale;
173 }
174 else
175 {
176 this.scale = sqlType.getScale();
177 }
178 if (defaultValue != null)
179 {
180 this.defaultValue = defaultValue;
181 }
182 else
183 {
184 this.defaultValue = sqlType.getDefaultValue();
185 }
186 }
187
188 /**
189 * @return Returns the scale.
190 */
191 public String getScale()
192 {
193 return scale;
194 }
195
196 /**
197 * @return Returns the size.
198 */
199 public String getSize()
200 {
201 return size;
202 }
203
204 /**
205 * @return Returns the defaultValue.
206 */
207 public String getDefaultValue()
208 {
209 return defaultValue;
210 }
211
212 /**
213 * Returns the SQL type name.
214 *
215 * @return The SQL type name for this column type, or null if the type
216 * is not set.
217 */
218 public String getSqlTypeName()
219 {
220 return sqlTypeName;
221 }
222
223 /**
224 * Return the size and scale in brackets for use in an SQL script.
225 *
226 * @return size and scale or an empty String if there are no values
227 * available.
228 */
229 public String printSize(String sizeSuffix)
230 {
231 if (StringUtils.isNotBlank(size) && StringUtils.isNotBlank(scale))
232 {
233 return '(' + size + sizeSuffix + ',' + scale + ')';
234 }
235 else if (StringUtils.isNotBlank(size))
236 {
237 return '(' + size + sizeSuffix + ')';
238 }
239 else
240 {
241 return "";
242 }
243 }
244 }