1 19 20 package org.apache.cayenne.access.jdbc; 21 22 import java.sql.ResultSet ; 23 import java.sql.ResultSetMetaData ; 24 import java.sql.SQLException ; 25 import java.util.Map ; 26 27 import org.apache.cayenne.CayenneRuntimeException; 28 import org.apache.cayenne.access.types.ExtendedType; 29 import org.apache.cayenne.access.types.ExtendedTypeMap; 30 31 37 public class RowDescriptor { 39 40 protected ColumnDescriptor[] columns; 41 protected ExtendedType[] converters; 42 43 46 protected RowDescriptor() { 47 48 } 49 50 53 public RowDescriptor(ColumnDescriptor[] columns, ExtendedTypeMap types) { 54 this.columns = columns; 55 indexTypes(types); 56 } 57 58 61 public RowDescriptor(ResultSet resultSet, ExtendedTypeMap types) { 62 this(resultSet, types, null); 63 } 64 65 70 public RowDescriptor(ResultSet resultSet, ExtendedTypeMap types, Map javaTypeOverrides) { 71 72 initFromResultSet(resultSet); 73 74 if (javaTypeOverrides != null) { 75 overrideJavaTypes(javaTypeOverrides); 76 } 77 78 indexTypes(types); 79 } 80 81 86 public void forceUpperCaseColumnNames() { 87 for(int i = 0; i < columns.length; i++) { 88 columns[i].setLabel(columns[i].getLabel().toUpperCase()); 89 } 90 } 91 92 97 public void forceLowerCaseColumnNames() { 98 for(int i = 0; i < columns.length; i++) { 99 columns[i].setLabel(columns[i].getLabel().toLowerCase()); 100 } 101 } 102 103 106 protected void indexTypes(ExtendedTypeMap types) { 107 this.converters = new ExtendedType[columns.length]; 108 for (int i = 0; i < columns.length; i++) { 109 converters[i] = types.getRegisteredType(columns[i].getJavaClass()); 110 } 111 } 112 113 116 protected void initFromResultSet(ResultSet resultSet) { 117 try { 118 ResultSetMetaData md = resultSet.getMetaData(); 119 int len = md.getColumnCount(); 120 if (len == 0) { 121 throw new CayenneRuntimeException("No columns in ResultSet."); 122 } 123 124 this.columns = new ColumnDescriptor[len]; 125 126 for (int i = 0; i < len; i++) { 127 columns[i] = new ColumnDescriptor(md, i + 1); 128 } 129 } 130 catch (SQLException sqex) { 131 throw new CayenneRuntimeException("Error reading metadata.", sqex); 132 } 133 } 134 135 139 protected void overrideJavaTypes(Map overrides) { 140 141 for (int i = 0; i < columns.length; i++) { 142 String type = (String ) overrides.get(columns[i].getName()); 143 144 if (type != null) { 145 columns[i].setJavaClass(type); 146 } 147 } 148 } 149 150 153 public int getWidth() { 154 return columns.length; 155 } 156 157 160 public ColumnDescriptor[] getColumns() { 161 return columns; 162 } 163 164 167 public ExtendedType[] getConverters() { 168 return converters; 169 } 170 } 171 | Popular Tags |