1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.javagen.agile.db.model;
17
18 import javax.xml.bind.annotation.XmlAttribute;
19 import javax.xml.bind.annotation.XmlRootElement;
20 import javax.xml.bind.annotation.XmlTransient;
21
22 import org.javagen.agile.core.annotation.DefaultValue;
23 import org.javagen.agile.core.model.AbstractModel;
24 import org.javagen.agile.core.model.Model;
25
26 @XmlRootElement
27 public class Column extends AbstractModel {
28
29 public static final String DEFAULT_MODEL_TYPE = "COLUMN";
30
31 protected DbType dbType;
32 /*** Database-specific type if different from standard SQL type (i.o. DbType). */
33 protected String dbTypeName;
34 protected Boolean notNull;
35 protected Boolean unique;
36 protected Object defaultValue;
37 protected Integer columnSize;
38 protected Integer scale;
39 protected Boolean autoIncrement;
40
41 public Column() {
42 super();
43 this.setModelType(DEFAULT_MODEL_TYPE);
44 }
45
46 public Column(Table parentTable) {
47 this();
48 this.setParentModel(parentTable);
49 }
50
51 public Column(String name) {
52 this();
53 this.setName(name);
54 }
55
56 @XmlTransient
57 public Table getParentTable() {
58 return (Table)getParentModel();
59 }
60
61 /*** is this a primary key? */
62 public boolean isKey() { return false; }
63
64 /*** is this a foreign key? */
65 public boolean isForeignKey() { return get(ColumnReference.DEFAULT_MODEL_TYPE)!=null; }
66
67 /***
68 * columnSize for varchar types or precision for numeric/decimal types.
69 */
70 @XmlAttribute
71 public Integer getColumnSize() {
72 return columnSize;
73 }
74
75 public void setColumnSize(Integer columnSize) {
76 this.columnSize = columnSize;
77 }
78
79 @XmlAttribute
80 public DbType getDbType() {
81 return dbType;
82 }
83
84 public String getDbTypeAsString() {
85 return dbType.toString();
86 }
87
88 public void setDbType(DbType dbType) {
89 this.dbType = dbType;
90 }
91
92
93
94
95
96
97
98
99
100 @XmlAttribute
101 @DefaultValue("false")
102 public Boolean getNotNull() {
103 return notNull;
104 }
105
106 public void setNotNull(Boolean notNull) {
107 this.notNull = notNull;
108 }
109
110 /***
111 * Number of decimal places allowed in numeric/decimal types.
112 */
113 @XmlAttribute
114 public Integer getScale() {
115 return scale;
116 }
117
118 public void setScale(Integer scale) {
119 this.scale = scale;
120 }
121
122 @XmlAttribute
123 @DefaultValue("false")
124 public Boolean getUnique() {
125 return unique;
126 }
127
128 public void setUnique(Boolean unique) {
129 this.unique = unique;
130 }
131
132 /***
133 * Database vender-specific type if different from generic <code>DbType</code>.
134 * For example <code>VARCHAR</code> is often <code>VARCHAR2</code> on Oracle.
135 * @return
136 */
137 @XmlAttribute
138 public String getDbTypeName() {
139 return dbTypeName;
140 }
141
142 public void setDbTypeName(String dbTypeName) {
143 this.dbTypeName = dbTypeName;
144 }
145
146 public Object getDefaultValue() {
147 return defaultValue;
148 }
149
150 public void setDefaultValue(Object defaultValue) {
151 this.defaultValue = defaultValue;
152 }
153
154 @XmlAttribute
155 @DefaultValue("false")
156 public Boolean getAutoIncrement() {
157 return autoIncrement;
158 }
159
160 public void setAutoIncrement(Boolean autoSequence) {
161 this.autoIncrement = autoSequence;
162 }
163
164 @Override
165 public void copyTo(Model targetModel) {
166 Column target = (Column)targetModel;
167 super.copyTo(target);
168 if (this.getColumnSize()!=null)
169 target.setColumnSize(this.getColumnSize());
170 if (this.getDbType()!=null)
171 target.setDbType(this.getDbType());
172 if (this.getDbTypeName()!=null)
173 target.setDbTypeName(this.getDbTypeName());
174 if (this.getDefaultValue()!=null)
175 target.setDefaultValue(this.getDefaultValue());
176 if (this.getNotNull()!=null)
177 target.setNotNull(this.getNotNull());
178 if (this.getUnique()!=null)
179 target.setUnique(this.getUnique());
180 if (this.getScale()!=null)
181 target.setScale(this.getScale());
182 if (this.getAutoIncrement()!=null)
183 target.setAutoIncrement(this.getAutoIncrement());
184 }
185
186 @Override
187 public String toString() {
188 StringBuilder sb = new StringBuilder("Column{");
189 if (name!=null)
190 sb.append("name=").append(name).append(", ");
191 if (dbType!=null)
192 sb.append("dbType=").append(dbType.name()).append(", ");
193 if (notNull!=null)
194 sb.append("notNull=").append(notNull).append(", ");
195 if (unique!=null)
196 sb.append("unique=").append(unique).append(", ");
197 if (defaultValue!=null)
198 sb.append("defaultValue=").append(defaultValue).append(", ");
199 if (columnSize!=null)
200 sb.append("columnSize=").append(columnSize).append(", ");
201 if (scale!=null)
202 sb.append("scale=").append(scale).append(", ");
203 if (autoIncrement!=null)
204 sb.append("autoIncrement=").append(autoIncrement).append(", ");
205 return sb.append("]").toString();
206 }
207
208 @Override
209 public int hashCode() {
210 int result = 37;
211 result = 37*result + (this.dbType==null ? 0 : this.dbType.hashCode());
212 result = 37*result + (this.dbTypeName==null ? 0 : this.dbTypeName.hashCode());
213 result = 37*result + (Boolean.TRUE.equals(this.notNull) ? 1 : 0);
214 result = 37*result + (Boolean.TRUE.equals(this.unique) ? 1 : 0);
215 result = 37*result + (this.defaultValue==null ? 0 : this.defaultValue.hashCode());
216 result = 37*result + (this.columnSize==null ? 0 : this.columnSize.hashCode());
217 result = 37*result + (this.scale==null ? 0 : this.scale.hashCode());
218 result = 37*result + (Boolean.TRUE.equals(this.autoIncrement) ? 1 : 0);
219 return result;
220 }
221
222 }