001: package simpleorm.quickstart;
002:
003: import java.io.IOException;
004: import java.io.Writer;
005: import java.util.Vector;
006:
007: import org.apache.commons.sql.model.Column;
008: import org.apache.commons.sql.model.Table;
009: import simpleorm.quickstart.AParam;
010:
011: /**
012: * @author <a href="mailto:richard.schmidt@inform6.com">Richard Schmidt</a>
013: *
014: */
015: final class ColumnGenerate {
016: Table table;
017: Column column;
018: INiceNameFormatter niceName;
019: String sFieldName;
020: String niceColumnName;
021:
022: ColumnGenerate(Table table, Column column,
023: INiceNameFormatter niceName) {
024: this .table = table;
025: this .column = column;
026: this .niceName = niceName;
027:
028: sFieldName = jdbcTypeToSField(column);
029: niceColumnName = niceName.niceNameForColumn(table.getName(),
030: column.getName());
031: }
032:
033: public boolean isPrimary() {
034:
035: return column.isPrimaryKey();
036: }
037:
038: public void outputStaticFields(Writer out) throws IOException {
039:
040: out.write(" public static final " + sFieldName + " "
041: + niceColumnName + " =\n");
042:
043: out.write(" new " + sFieldName + "(meta, \""
044: + column.getName() + "\"");
045:
046: if (sFieldName.equalsIgnoreCase("SFieldBigDecimal")) {
047: out.write(", " + column.getSize() + ", "
048: + column.getScale());
049: } else if (sFieldName.equalsIgnoreCase("SFieldString")) {
050: out.write(", " + column.getSize());
051: }
052:
053: // required and/or primary key?
054: String parameters = "";
055: String comma = "";
056:
057: if (column.isPrimaryKey()) {
058: parameters += (comma + "SFD_PRIMARY_KEY");
059: comma = ", ";
060: }
061:
062: if (column.isRequired()) {
063: parameters += (comma + "SFD_MANDATORY");
064: comma = ", ";
065: }
066: if (column.isAutoIncrement()) {
067: parameters += (comma + "SFD_GENERATED_KEY");
068: comma = ", ";
069: }
070:
071: if (parameters.length() != 0) {
072: out.write(",\n new SPropertyValue[] { "
073: + parameters + " }");
074: }
075:
076: out.write(");\n\n");
077: }
078:
079: /**
080: * Generate something like<br>
081: public long getfldIdBirdEvent(){
082: return this.getLong( fldIdBirdEvent);
083: }<br>
084: public void setfldIdBirdEvent( long value){
085: this.setLong( fldIdBirdEvent, value);
086: }
087: */
088: public void outputGettersAndSetters(Writer out) throws IOException {
089: String type = sFieldToPrimative(jdbcTypeToSField(column));
090: String typeUpper = type.substring(0, 1).toUpperCase()
091: + type.substring(1);
092:
093: out.write(" public " + type + " get_" + niceColumnName
094: + "(){ return get" + typeUpper + "(" + niceColumnName
095: + ");}\n");
096:
097: out.write(" public void set_" + niceColumnName + "( " + type
098: + " value){" + "set" + typeUpper + "( "
099: + niceColumnName + ",value);}\n\n");
100: }
101:
102: private static String sFieldToPrimative(String sField) {
103: if (sField.equalsIgnoreCase("SFieldDouble")) {
104: return "BigDecimal";
105: } else if (sField.equalsIgnoreCase("SFieldString")) {
106: return "String";
107: } else if (sField.equalsIgnoreCase("SFieldBigDecimal")) {
108: return "BigDecimal";
109: } else if (sField.equalsIgnoreCase("SFieldDate")) {
110: return "Date";
111: } else if (sField.equalsIgnoreCase("SFieldTimestamp")) {
112: return "Date";
113: } else if (sField.equalsIgnoreCase("SFieldInteger")) {
114: return "int";
115: } else if (sField.equalsIgnoreCase("SFieldLong")) {
116: return "long";
117: } else {
118: return " {Code not implemented " + sField + "}";
119: }
120: }
121:
122: private static String sFieldToPrimativeObject(String sField) {
123: if (sField.equalsIgnoreCase("SFieldInteger")) {
124: return "Integer";
125: } else if (sField.equalsIgnoreCase("SFieldLong")) {
126: return "Long";
127: } else {
128: return null;
129: }
130: }
131:
132: /**
133: * Mappings from Sybase to SimpleORM types.
134: */
135: private static String jdbcTypeToSField(Column _column) {
136: String datatype = _column.getType();
137:
138: if (datatype.equalsIgnoreCase("DOUBLE PRECIS")
139: || datatype.equalsIgnoreCase("DOUBLE")) {
140: return "SFieldDouble";
141: }
142:
143: if (datatype.equalsIgnoreCase("VARCHAR")
144: || datatype.equalsIgnoreCase("LONGVARCHAR")
145: || datatype.equalsIgnoreCase("LONGVARBINARY")) {
146: return "SFieldString";
147: }
148:
149: if (datatype.equalsIgnoreCase("NUMERIC")
150: || datatype.equalsIgnoreCase("DECIMAL")) {
151: if (_column.getScale() == 0) {
152: if (_column.getPrecisionRadix() <= 9) {
153: return "SFieldInteger";
154: } else if (_column.getPrecisionRadix() <= 20) {
155: return "SFieldLong";
156: }
157: }
158:
159: return "SFieldBigDecimal";
160: }
161:
162: if (datatype.equalsIgnoreCase("IMAGE")) {
163: return "SFieldString";
164: }
165:
166: if (datatype.equalsIgnoreCase("DATETIME")) {
167: return "SFieldDate";
168: }
169:
170: if (datatype.equalsIgnoreCase("CHAR")) {
171: return "SFieldString";
172: }
173:
174: if (datatype.equalsIgnoreCase("SMALLINT")) {
175: return "SFieldInteger";
176: }
177:
178: if (datatype.equalsIgnoreCase("BIT")) {
179: return "SFieldInteger"; //should be something else but ...
180: }
181:
182: if (datatype.equalsIgnoreCase("INT")
183: || datatype.equalsIgnoreCase("INTEGER")) {
184: if (_column.getSize() > 9) {
185: return "SFieldLong";
186: } else {
187: return "SFieldInteger";
188: }
189: }
190:
191: if (datatype.equalsIgnoreCase("TEXT")) {
192: return "SFieldString";
193: }
194:
195: if (datatype.equalsIgnoreCase("TINYINT")) {
196: return "SFieldInteger";
197: }
198:
199: if (datatype.equalsIgnoreCase("SMALLDATETIME")) {
200: return "SFieldDate";
201: }
202:
203: if (datatype.equalsIgnoreCase("TIMESTAMP")) {
204: return "SFieldTimestamp";
205: }
206:
207: //VARBINARY to Timestamp - are you sure
208: if (datatype.equalsIgnoreCase("VARBINARY")) {
209: return "SFieldTimestamp";
210: }
211:
212: //map a Float to double.
213: if (datatype.equalsIgnoreCase("FLOAT")) {
214: return "SFieldDouble";
215: }
216:
217: if (datatype.equalsIgnoreCase("DATE")) {
218: return "SFieldDate";
219: } else {
220: return datatype.substring(0, 1).toUpperCase()
221: + datatype.substring(1).toLowerCase();
222: }
223: }
224:
225: /**example: "int fldEmpleeID"*/
226: public AParam getParameters() {
227:
228: //not a foreign key
229: String name = "_" + niceColumnName;
230: String type = sFieldToPrimative(jdbcTypeToSField(column));
231:
232: String methodParam = type + " " + name;
233: String objectParam = name;
234: if (sFieldToPrimativeObject(jdbcTypeToSField(column)) != null) {
235: objectParam = "new "
236: + sFieldToPrimativeObject(jdbcTypeToSField(column))
237: + "( " + name + ")";
238: }
239: ;
240: return new AParam(name, type, methodParam, objectParam);
241:
242: }
243:
244: }
|