1
2
3
4
5
6
7 package org.asyrinx.joey.gen.model.rdb;
8
9 import java.util.ArrayList;
10 import java.util.Iterator;
11 import java.util.List;
12
13 import org.asyrinx.brownie.core.lang.NumberUtils;
14 import org.asyrinx.joey.gen.jdbc.JdbcUtils;
15 import org.asyrinx.joey.gen.model.Element;
16
17 /***
18 * @author akima
19 */
20 public class Column extends Element {
21
22 /***
23 */
24 public Column() {
25 this(null, null, null);
26 }
27
28 /***
29 * @param table
30 * @param name
31 * @param type
32 */
33 public Column(Table parent, String name, String type) {
34 super(parent, name);
35 this.type = type;
36 }
37
38 /***
39 * @param table
40 * @param name
41 * @param type
42 * @param size
43 */
44 public Column(Table parent, String name, String type, String size) {
45 this(parent, name, type, size, false, false);
46 }
47
48 /***
49 * @param table
50 * @param name
51 * @param type
52 * @param size
53 * @param required
54 */
55 public Column(Table parent, String name, String type, String size, boolean required) {
56 this(parent, name, type, size, required, false, null);
57 }
58
59 /***
60 * @param table
61 * @param name
62 * @param type
63 * @param size
64 * @param required
65 * @param primaryKey
66 */
67 public Column(Table parent, String name, String type, String size, boolean required, boolean primaryKey) {
68 this(parent, name, type, size, required, primaryKey, null);
69 }
70
71 /***
72 * @param table
73 * @param name
74 * @param type
75 * @param size
76 * @param required
77 * @param primaryKey
78 * @param defaultValue
79 */
80 public Column(Table parent, String name, String type, String size, boolean required, boolean primaryKey,
81 String defaultValue) {
82 super(parent, name);
83 this.type = type;
84 this.size = size;
85 this.required = required;
86 this.primaryKey = primaryKey;
87 this.defaultValue = defaultValue;
88 }
89
90
91
92
93
94
95 public Table getParent() {
96 return (Table) super.getParentElement();
97 }
98
99 private String type = null;
100
101 private String size = null;
102
103 private String decimalSize = null;
104
105 private boolean required = false;
106
107 private boolean primaryKey = false;
108
109 private String defaultValue = null;
110
111 private String enum = null;
112
113 private boolean autoIncrement = false;
114
115 private String idMethod = null;
116
117 private String fk = null;
118
119 private String indexed = null;
120
121 private boolean extended = false;
122
123 /***
124 * @return Returns the defaultValue.
125 */
126 public String getDefault() {
127 return getDefaultValue();
128 }
129
130 public int getSizeAsInt() {
131 return NumberUtils.toInt(getSize(), 0);
132 }
133
134 public int getDeciamlSizeAsInt() {
135 return NumberUtils.toInt(getDecimalSize(), 0);
136 }
137
138 /***
139 * @param defaultValue
140 * The defaultValue to set.
141 */
142 public void setDefault(String defaultValue) {
143 this.setDefaultValue(defaultValue);
144 }
145
146 /***
147 * @return Returns the defaultValue.
148 */
149 public String getDefaultValue() {
150 return defaultValue;
151 }
152
153 /***
154 * @param defaultValue
155 * The defaultValue to set.
156 */
157 public void setDefaultValue(String defaultValue) {
158 this.defaultValue = defaultValue;
159 }
160
161 /***
162 * @return Returns the primaryKey.
163 */
164 public boolean isPrimaryKey() {
165 return primaryKey;
166 }
167
168 /***
169 * @param primaryKey
170 * The primaryKey to set.
171 */
172 public void setPrimaryKey(boolean primaryKey) {
173 this.primaryKey = primaryKey;
174 }
175
176 /***
177 * @return Returns the required.
178 */
179 public boolean isRequired() {
180 return required;
181 }
182
183 /***
184 * @param required
185 * The required to set.
186 */
187 public void setRequired(boolean required) {
188 this.required = required;
189 }
190
191 /***
192 * @return Returns the size.
193 */
194 public String getSize() {
195 return size;
196 }
197
198 /***
199 * @param size
200 * The size to set.
201 */
202 public void setSize(String size) {
203 this.size = size;
204 }
205
206 /***
207 * @return Returns the type.
208 */
209 public String getType() {
210 return type;
211 }
212
213 /***
214 * @param type
215 * The type to set.
216 */
217 public void setType(String type) {
218 this.type = type;
219 }
220
221 public int getJdbcType() {
222 return JdbcUtils.toJdbcType(this.getType());
223 }
224
225 /***
226 * @return Returns the enum.
227 */
228 public String getEnum() {
229 return enum;
230 }
231
232 /***
233 * @param enum
234 * The enum to set.
235 */
236 public void setEnum(String enum) {
237 this.enum = enum;
238 }
239
240 public RdbEnumeration getEnumeration() {
241 final Table table = getParent();
242 if (table == null)
243 return null;
244 final Database database = table.getParent();
245 return database.getEnumerations().getEnumeration(getEnum());
246 }
247
248 /***
249 * @return Returns the autoIncrement.
250 */
251 public boolean isAutoIncrement() {
252 return autoIncrement;
253 }
254
255 /***
256 * @param autoIncrement
257 * The autoIncrement to set.
258 */
259 public void setAutoIncrement(boolean autoIncrement) {
260 this.autoIncrement = autoIncrement;
261 }
262
263 /***
264 * @return Returns the idMethod.
265 */
266 public String getIdMethod() {
267 return idMethod;
268 }
269
270 /***
271 * @param idMethod
272 * The idMethod to set.
273 */
274 public void setIdMethod(String idMethod) {
275 this.idMethod = idMethod;
276 }
277
278 /***
279 * @return Returns the fk.
280 */
281 public String getFk() {
282 return fk;
283 }
284
285 /***
286 * @param fk
287 * The fk to set.
288 */
289 public void setFk(String fk) {
290 this.fk = fk;
291 }
292
293 /***
294 * @return Returns the indexed.
295 */
296 public String getIndexed() {
297 return indexed;
298 }
299
300 /***
301 * @param indexed
302 * The indexed to set.
303 */
304 public void setIndexed(String indexed) {
305 this.indexed = indexed;
306 }
307
308 /***
309 * @return Returns the extended.
310 */
311 public boolean isExtended() {
312 return extended;
313 }
314
315 /***
316 * @param extended
317 * The extended to set.
318 */
319 public void setExtended(boolean extended) {
320 this.extended = extended;
321 }
322
323 public List getForeignKeysContainsAsLocal() {
324 final List result = new ArrayList();
325 final Table table = this.getParent();
326 if (table == null)
327 return result;
328 for (Iterator i = table.getForeignKeys().iterator(); i.hasNext();) {
329 final ForeignKey fk = (ForeignKey) i.next();
330 if (fk.containsAsLocal(this))
331 result.add(fk);
332 }
333 return result;
334 }
335
336 public String getDecimalSize() {
337 return decimalSize;
338 }
339
340 public void setDecimalSize(String decimalSize) {
341 this.decimalSize = decimalSize;
342 }
343 }