1 package pl.aislib.tools.mapping.db;
2
3 import java.sql.DatabaseMetaData;
4 import java.sql.Types;
5
6 import org.jdom.CDATA;
7 import org.jdom.Element;
8
9 /***
10 * @author Micha? Ja?tak, AIS.PL
11 * @author Milosz Tylenda, AIS.PL
12 * @version $Revision: 1.2 $
13 */
14 public class Column {
15
16 private Integer cnColumnSize;
17 private Integer cnDecimalDigits;
18 private int cnNullable;
19 private int cnNumPrecRadix;
20 private int cnOrdinalPosition;
21 private short cnDataType;
22 private String cnCatalog;
23 private String cnClassName;
24 private String cnColumnDef;
25 private String cnName;
26 private String cnRemarks;
27 private String cnSchema;
28 private String cnTableName;
29 private String cnTypeName;
30
31 /***
32 *
33 */
34 protected Column(String tName) {
35 cnName = tName;
36 }
37
38 /***
39 *
40 */
41 public String getName() {
42 return ((cnName != null) ? new String (cnName) : cnName);
43 }
44
45 /***
46 *
47 */
48 public void setCatalog(String tCatalog) {
49 cnCatalog = tCatalog;
50 }
51
52 /***
53 *
54 */
55 public void setClassName(String tClassName) {
56 cnClassName = tClassName;
57 }
58
59 /***
60 *
61 */
62 public void setColumnDef(String tColumnDef) {
63 cnColumnDef = tColumnDef;
64 }
65
66 /***
67 *
68 */
69 public void setColumnSize(int tColumnSize) {
70 cnColumnSize = new Integer(tColumnSize);
71 }
72
73 /***
74 *
75 */
76 public void setDataType(short tDataType) {
77 cnDataType = tDataType;
78 }
79
80 /***
81 *
82 */
83 public void setDecimalDigits(int tDecimalDigits) {
84 cnDecimalDigits = new Integer(tDecimalDigits);
85 }
86
87 /***
88 *
89 */
90 public void setNullable(int tNullable) {
91 cnNullable = tNullable;
92 }
93
94 /***
95 *
96 */
97 public void setNumPrecRadix(int tNumPrecRadix) {
98 cnNumPrecRadix = tNumPrecRadix;
99 }
100
101 /***
102 *
103 */
104 public void setOrdinalPosition(int tOrdinalPosition) {
105 cnOrdinalPosition = tOrdinalPosition;
106 }
107
108 /***
109 *
110 */
111 public void setSchema(String tSchema) {
112 cnSchema = tSchema;
113 }
114
115 /***
116 *
117 */
118 public void setTableName(String tTableName) {
119 cnTableName = tTableName;
120 }
121
122 /***
123 *
124 */
125 public void setTypeName(String tTypeName) {
126 cnTypeName = tTypeName;
127 }
128
129 /***
130 *
131 */
132 private String firstCharToUpper(String source) {
133 if (source.length() == 0) {
134 return source;
135 }
136 return source.substring (0, 1).toUpperCase ().concat (source.substring (1));
137 }
138
139 /***
140 *
141 */
142 private String dropUnderscores(String name) {
143
144 String source = new String (name);
145 StringBuffer result = new StringBuffer ();
146
147 int idx = 0;
148 int vSize = source.length ();
149
150 while (idx < vSize) {
151 int spaceIdx = source.indexOf ('_', idx);
152 if (spaceIdx >= 0) { result.append (firstCharToUpper (source.substring (idx, spaceIdx))); }
153 else {
154 result.append (firstCharToUpper (source.substring (idx)));
155 spaceIdx = vSize;
156 }
157 idx = spaceIdx + 1;
158 }
159 return new String (result);
160 }
161
162 /***
163 *
164 */
165 private String canonizeFieldName(String name) {
166
167 String result = dropUnderscores (name);
168 return result.substring(0,1).toLowerCase().concat (result.substring(1));
169 }
170
171 /***
172 * FIXME
173 */
174 public String toString() {
175 return new String ("Column:\n" + cnName + "(" + cnCatalog + ", " + cnSchema
176 + ", " + cnTypeName + ")\n");
177 }
178
179 /***
180 * FIXME
181 */
182 public Element toXML () {
183
184 Element result = new Element ("column");
185 result.setAttribute ("name", cnName);
186 result.setAttribute ("java-name", canonizeFieldName (cnName.toLowerCase ()));
187 result.setAttribute ("order", "" + cnOrdinalPosition);
188 result.setAttribute ("db-type", cnTypeName);
189 if (cnColumnSize != null) {
190 result.setAttribute("column-size", "" + cnColumnSize.intValue());
191 }
192 if (cnDecimalDigits != null) {
193 result.setAttribute("decimal-digits", "" + cnDecimalDigits.intValue());
194 }
195
196 if (cnRemarks != null) {
197 Element remarks = new Element ("remarks");
198 remarks.addContent (new CDATA (cnRemarks));
199 result.addContent (remarks);
200 }
201
202 if (cnColumnDef != null) {
203 result.setAttribute ("default-value", cnColumnDef);
204 }
205
206 String notNull = null;
207 if (cnNullable == DatabaseMetaData.columnNoNulls) { notNull = "true"; }
208 if (cnNullable == DatabaseMetaData.columnNullable) { notNull = "false"; }
209 if (notNull != null) {
210 result.setAttribute ("not-null", notNull);
211 }
212
213 if (cnClassName != null) {
214 String shortName = cnClassName.substring (cnClassName.lastIndexOf ('.') + 1);
215 result.setAttribute ("class-name", cnClassName);
216 result.setAttribute ("class-shortcut", shortName);
217 }
218
219 String sqlType = cnTypeName;
220 switch (cnDataType) {
221 case Types.CHAR:
222 case Types.DATE:
223 case Types.LONGVARCHAR:
224 case Types.TIMESTAMP:
225 case Types.VARCHAR:
226 if (cnColumnSize != null) {
227 sqlType = sqlType.concat ("(" + cnColumnSize.intValue() + ")");
228 }
229 break;
230
231 case Types.BIGINT:
232 case Types.DECIMAL:
233 case Types.DOUBLE:
234 case Types.FLOAT:
235 case Types.INTEGER:
236 case Types.NUMERIC:
237 if ((cnColumnSize != null) && (cnDecimalDigits != null)) {
238 sqlType = sqlType.concat ("(" + cnColumnSize + ", " + cnDecimalDigits + ")");
239 }
240 break;
241 }
242 result.setAttribute ("sql-type", sqlType);
243 return result;
244 }
245
246 }
247
248 /***
249 * $Log: Column.java,v $
250 * Revision 1.2 2004/06/24 05:48:28 milosz
251 * firstCharToUpper: fixed to deal with empty Strings - no more Exceptions when column names contain consecutive underscores; one unused method removed.
252 *
253 */