1 package org.apache.ojb.broker.metadata.torque; 2 3 17 18 import org.apache.ojb.broker.metadata.FieldDescriptor; 19 20 import java.sql.Types ; 21 22 public class TorqueFieldGenerator { 23 24 private boolean ignoreAutoIncrement; 25 26 public TorqueFieldGenerator(boolean ignoreAutoIncrement) { 27 this.ignoreAutoIncrement = ignoreAutoIncrement; 28 } 29 30 public void generateFieldDescriptors(FieldDescriptor[] descriptors, StringBuffer buffer) { 31 if (descriptors != null) { 32 for (int i = 0; i < descriptors.length; i++) { 33 FieldDescriptor descriptor = descriptors[i]; 34 generateFieldDescriptor(descriptor, descriptor.getColumnName(), descriptor.isRequired(), descriptor.isAutoIncrement(), descriptor.isPrimaryKey(), buffer); 35 } 36 } 37 } 38 39 public void generateMappingFieldDescriptors(FieldDescriptor[] descriptors, StringBuffer buffer) { 40 if (descriptors != null) { 41 for (int i = 0; i < descriptors.length; i++) { 42 FieldDescriptor descriptor = descriptors[i]; 43 StringBuffer fieldBuffer = new StringBuffer (); 44 generateFieldDescriptor(descriptor, descriptor.getColumnName(), true, false, true, fieldBuffer); 45 buffer.append(fieldBuffer); 46 } 47 } 48 } 49 50 private void generateFieldDescriptor(FieldDescriptor descriptor, String fieldName, boolean isRequired, boolean isAutoIncrement, boolean isPrimaryKey, StringBuffer buffer) { 51 buffer.append(" <column name=\""); 52 buffer.append(fieldName); 53 buffer.append("\" required=\""); 54 buffer.append(isRequired || isPrimaryKey); 55 if (!this.ignoreAutoIncrement) { 56 buffer.append("\" autoIncrement=\""); 57 buffer.append(isAutoIncrement); 58 } 59 buffer.append("\" primaryKey=\""); 60 buffer.append(isPrimaryKey); 61 buffer.append("\" type=\""); 62 buffer.append(descriptor.getColumnType()); 63 64 generateConstraints(descriptor, buffer); 65 66 buffer.append("\"/>\n"); 67 } 68 69 private void generateConstraints(FieldDescriptor descriptor, StringBuffer buffer) { 70 int jdbcType = descriptor.getJdbcType().getType(); 71 72 switch (jdbcType) { 73 case Types.BIGINT: 74 case Types.DECIMAL: 75 case Types.DOUBLE: 76 case Types.FLOAT: 77 case Types.NUMERIC: 79 case Types.REAL: 80 case Types.SMALLINT: 81 case Types.TINYINT: 82 if (descriptor.isPrecisionSpecified()) { 83 buffer.append("\" precision=\""); 84 buffer.append(descriptor.getPrecision()); 85 } 86 if (descriptor.isScaleSpecified()) { 87 buffer.append("\" scale=\""); 88 buffer.append(descriptor.getScale()); 89 } 90 break; 91 default: 92 if (descriptor.isLengthSpecified()) { 93 buffer.append("\" size=\""); 94 buffer.append(descriptor.getLength()); 95 } 96 } 97 } 98 99 } 100 | Popular Tags |