%line | %branch | |||||||||
---|---|---|---|---|---|---|---|---|---|---|
org.apache.torque.engine.database.model.Domain |
|
|
1 | package org.apache.torque.engine.database.model; |
|
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 | import org.apache.torque.engine.platform.Platform; |
|
24 | import org.xml.sax.Attributes; |
|
25 | ||
26 | /** |
|
27 | * A Class for holding data about a column used in an Application. |
|
28 | * |
|
29 | * @author <a href="mailto:mpoeschl@marmot.at>Martin Poeschl</a> |
|
30 | * @version $Id: Domain.java 473814 2006-11-11 22:30:30Z tv $ |
|
31 | */ |
|
32 | public class Domain |
|
33 | { |
|
34 | private String name; |
|
35 | private String description; |
|
36 | private String size; |
|
37 | private String scale; |
|
38 | /** type as defined in schema.xml */ |
|
39 | private SchemaType torqueType; |
|
40 | private String sqlType; |
|
41 | private String defaultValue; |
|
42 | ||
43 | /** |
|
44 | * Creates a new instance with a <code>null</code> name. |
|
45 | */ |
|
46 | public Domain() |
|
47 | 21852 | { |
48 | 21852 | this.name = null; |
49 | 21852 | } |
50 | ||
51 | /** |
|
52 | * Creates a new Domain and set the name |
|
53 | * |
|
54 | * @param name column name |
|
55 | */ |
|
56 | public Domain(String name) |
|
57 | 0 | { |
58 | 0 | this.name = name; |
59 | 0 | } |
60 | ||
61 | /** |
|
62 | * Creates a new Domain and set the name |
|
63 | */ |
|
64 | public Domain(SchemaType type) |
|
65 | 5400 | { |
66 | 5400 | this.name = null; |
67 | 5400 | this.torqueType = type; |
68 | 5400 | this.sqlType = type.getName(); |
69 | 5400 | } |
70 | ||
71 | /** |
|
72 | * Creates a new Domain and set the name |
|
73 | */ |
|
74 | public Domain(SchemaType type, String sqlType) |
|
75 | 1440 | { |
76 | 1440 | this.name = null; |
77 | 1440 | this.torqueType = type; |
78 | 1440 | this.sqlType = sqlType; |
79 | 1440 | } |
80 | ||
81 | /** |
|
82 | * Creates a new Domain and set the name |
|
83 | */ |
|
84 | public Domain(SchemaType type, String sqlType, String size, String scale) |
|
85 | 60 | { |
86 | 60 | this.name = null; |
87 | 60 | this.torqueType = type; |
88 | 60 | this.sqlType = sqlType; |
89 | 60 | this.size = size; |
90 | 60 | this.scale = scale; |
91 | 60 | } |
92 | ||
93 | /** |
|
94 | * Creates a new Domain and set the name |
|
95 | */ |
|
96 | public Domain(SchemaType type, String sqlType, String size) |
|
97 | 12 | { |
98 | 12 | this.name = null; |
99 | 12 | this.torqueType = type; |
100 | 12 | this.sqlType = sqlType; |
101 | 12 | this.size = size; |
102 | 12 | } |
103 | ||
104 | public Domain(Domain domain) |
|
105 | 41820 | { |
106 | 41820 | copy(domain); |
107 | 41820 | } |
108 | ||
109 | public void copy(Domain domain) |
|
110 | { |
|
111 | 42276 | this.defaultValue = domain.getDefaultValue(); |
112 | 42276 | this.description = domain.getDescription(); |
113 | 42276 | this.name = domain.getName(); |
114 | 42276 | this.scale = domain.getScale(); |
115 | 42276 | this.size = domain.getSize(); |
116 | 42276 | this.sqlType = domain.getSqlType(); |
117 | 42276 | this.torqueType = domain.getType(); |
118 | 42276 | } |
119 | ||
120 | /** |
|
121 | * Imports a column from an XML specification |
|
122 | */ |
|
123 | public void loadFromXML(Attributes attrib, Platform platform) |
|
124 | { |
|
125 | 456 | SchemaType schemaType = SchemaType.getEnum(attrib.getValue("type")); |
126 | 456 | copy(platform.getDomainForSchemaType(schemaType)); |
127 | //Name |
|
128 | 456 | name = attrib.getValue("name"); |
129 | //Default column value. |
|
130 | 456 | defaultValue = attrib.getValue("default"); |
131 | 456 | size = attrib.getValue("size"); |
132 | 456 | scale = attrib.getValue("scale"); |
133 | ||
134 | 456 | description = attrib.getValue("description"); |
135 | 456 | } |
136 | ||
137 | /** |
|
138 | * @return Returns the description. |
|
139 | */ |
|
140 | public String getDescription() |
|
141 | { |
|
142 | 42288 | return description; |
143 | } |
|
144 | ||
145 | /** |
|
146 | * @param description The description to set. |
|
147 | */ |
|
148 | public void setDescription(String description) |
|
149 | { |
|
150 | 0 | this.description = description; |
151 | 0 | } |
152 | ||
153 | /** |
|
154 | * @return Returns the name. |
|
155 | */ |
|
156 | public String getName() |
|
157 | { |
|
158 | 42732 | return name; |
159 | } |
|
160 | ||
161 | /** |
|
162 | * @param name The name to set. |
|
163 | */ |
|
164 | public void setName(String name) |
|
165 | { |
|
166 | 0 | this.name = name; |
167 | 0 | } |
168 | ||
169 | /** |
|
170 | * @return Returns the scale. |
|
171 | */ |
|
172 | public String getScale() |
|
173 | { |
|
174 | 63804 | return scale; |
175 | } |
|
176 | ||
177 | /** |
|
178 | * @param scale The scale to set. |
|
179 | */ |
|
180 | public void setScale(String scale) |
|
181 | { |
|
182 | 0 | this.scale = scale; |
183 | 0 | } |
184 | ||
185 | /** |
|
186 | * Replaces the size if the new value is not null. |
|
187 | * |
|
188 | * @param value The size to set. |
|
189 | */ |
|
190 | public void replaceScale(String value) |
|
191 | { |
|
192 | 21336 | this.scale = StringUtils.defaultString(value, getScale()); |
193 | 21336 | } |
194 | ||
195 | /** |
|
196 | * @return Returns the size. |
|
197 | */ |
|
198 | public String getSize() |
|
199 | { |
|
200 | 63864 | return size; |
201 | } |
|
202 | ||
203 | /** |
|
204 | * @param size The size to set. |
|
205 | */ |
|
206 | public void setSize(String size) |
|
207 | { |
|
208 | 12 | this.size = size; |
209 | 12 | } |
210 | ||
211 | /** |
|
212 | * Replaces the size if the new value is not null. |
|
213 | * |
|
214 | * @param value The size to set. |
|
215 | */ |
|
216 | public void replaceSize(String value) |
|
217 | { |
|
218 | 21336 | this.size = StringUtils.defaultString(value, getSize()); |
219 | 21336 | } |
220 | ||
221 | /** |
|
222 | * @return Returns the torqueType. |
|
223 | */ |
|
224 | public SchemaType getType() |
|
225 | { |
|
226 | 43896 | return torqueType; |
227 | } |
|
228 | ||
229 | /** |
|
230 | * @param torqueType The torqueType to set. |
|
231 | */ |
|
232 | public void setType(SchemaType torqueType) |
|
233 | { |
|
234 | 24 | this.torqueType = torqueType; |
235 | 24 | } |
236 | ||
237 | /** |
|
238 | * @param torqueType The torqueType to set. |
|
239 | */ |
|
240 | public void setType(String torqueType) |
|
241 | { |
|
242 | 0 | this.torqueType = SchemaType.getEnum(torqueType); |
243 | 0 | } |
244 | ||
245 | /** |
|
246 | * Replaces the default value if the new value is not null. |
|
247 | * |
|
248 | * @param value The defaultValue to set. |
|
249 | */ |
|
250 | public void replaceType(String value) |
|
251 | { |
|
252 | 0 | this.torqueType = SchemaType.getEnum( |
253 | StringUtils.defaultString(value, getType().getName())); |
|
254 | 0 | } |
255 | ||
256 | /** |
|
257 | * @return Returns the defaultValue. |
|
258 | */ |
|
259 | public String getDefaultValue() |
|
260 | { |
|
261 | 64368 | return defaultValue; |
262 | } |
|
263 | ||
264 | /** |
|
265 | * Return a string that will give this column a default value. |
|
266 | * @deprecated |
|
267 | */ |
|
268 | public String getDefaultSetting() |
|
269 | { |
|
270 | 0 | StringBuffer dflt = new StringBuffer(0); |
271 | 0 | if (getDefaultValue() != null) |
272 | { |
|
273 | 0 | dflt.append("default "); |
274 | 0 | if (TypeMap.isTextType(getType())) |
275 | { |
|
276 | // TODO: Properly SQL-escape the text. |
|
277 | 0 | dflt.append('\'').append(getDefaultValue()).append('\''); |
278 | } |
|
279 | else |
|
280 | { |
|
281 | 0 | dflt.append(getDefaultValue()); |
282 | } |
|
283 | } |
|
284 | 0 | return dflt.toString(); |
285 | } |
|
286 | ||
287 | /** |
|
288 | * @param defaultValue The defaultValue to set. |
|
289 | */ |
|
290 | public void setDefaultValue(String defaultValue) |
|
291 | { |
|
292 | 0 | this.defaultValue = defaultValue; |
293 | 0 | } |
294 | ||
295 | /** |
|
296 | * Replaces the default value if the new value is not null. |
|
297 | * |
|
298 | * @param value The defaultValue to set. |
|
299 | */ |
|
300 | public void replaceDefaultValue(String value) |
|
301 | { |
|
302 | 21336 | this.defaultValue = StringUtils.defaultString(value, getDefaultValue()); |
303 | 21336 | } |
304 | ||
305 | /** |
|
306 | * @return Returns the sqlType. |
|
307 | */ |
|
308 | public String getSqlType() |
|
309 | { |
|
310 | 43692 | return sqlType; |
311 | } |
|
312 | ||
313 | /** |
|
314 | * @param sqlType The sqlType to set. |
|
315 | */ |
|
316 | public void setSqlType(String sqlType) |
|
317 | { |
|
318 | 0 | this.sqlType = sqlType; |
319 | 0 | } |
320 | ||
321 | /** |
|
322 | * Return the size and scale in brackets for use in an sql schema. |
|
323 | * |
|
324 | * @return size and scale or an empty String if there are no values |
|
325 | * available. |
|
326 | */ |
|
327 | public String printSize() |
|
328 | { |
|
329 | 720 | if (size != null && scale != class="keyword">null) |
330 | { |
|
331 | 396 | return '(' + size + ',' + scale + ')'; |
332 | } |
|
333 | 324 | else if (size != null) |
334 | { |
|
335 | 60 | return '(' + size + ')'; |
336 | } |
|
337 | else |
|
338 | { |
|
339 | 264 | return ""; |
340 | } |
|
341 | } |
|
342 | ||
343 | } |
This report is generated by jcoverage, Maven and Maven JCoverage Plugin. |