1 56 package org.objectstyle.cayenne.access.jdbc; 57 58 import java.sql.ResultSet ; 59 import java.sql.ResultSetMetaData ; 60 import java.sql.SQLException ; 61 import java.util.Map ; 62 63 import org.objectstyle.cayenne.CayenneRuntimeException; 64 import org.objectstyle.cayenne.access.types.ExtendedType; 65 import org.objectstyle.cayenne.access.types.ExtendedTypeMap; 66 67 73 public class RowDescriptor { 75 76 protected ColumnDescriptor[] columns; 77 protected ExtendedType[] converters; 78 79 82 protected RowDescriptor() { 83 84 } 85 86 89 public RowDescriptor(ColumnDescriptor[] columns, ExtendedTypeMap types) { 90 this.columns = columns; 91 indexTypes(types); 92 } 93 94 97 public RowDescriptor(ResultSet resultSet, ExtendedTypeMap types) { 98 this(resultSet, types, null); 99 } 100 101 106 public RowDescriptor(ResultSet resultSet, ExtendedTypeMap types, Map javaTypeOverrides) { 107 108 initFromResultSet(resultSet); 109 110 if (javaTypeOverrides != null) { 111 overrideJavaTypes(javaTypeOverrides); 112 } 113 114 indexTypes(types); 115 } 116 117 120 protected void indexTypes(ExtendedTypeMap types) { 121 this.converters = new ExtendedType[columns.length]; 122 for (int i = 0; i < columns.length; i++) { 123 converters[i] = types.getRegisteredType(columns[i].getJavaClass()); 124 } 125 } 126 127 130 protected void initFromResultSet(ResultSet resultSet) { 131 try { 132 ResultSetMetaData md = resultSet.getMetaData(); 133 int len = md.getColumnCount(); 134 if (len == 0) { 135 throw new CayenneRuntimeException("No columns in ResultSet."); 136 } 137 138 this.columns = new ColumnDescriptor[len]; 139 140 for (int i = 0; i < len; i++) { 141 columns[i] = new ColumnDescriptor(md, i + 1); 142 } 143 } 144 catch (SQLException sqex) { 145 throw new CayenneRuntimeException("Error reading metadata.", sqex); 146 } 147 } 148 149 153 protected void overrideJavaTypes(Map overrides) { 154 155 for (int i = 0; i < columns.length; i++) { 156 String type = (String ) overrides.get(columns[i].getName()); 157 158 if (type != null) { 159 columns[i].setJavaClass(type); 160 } 161 } 162 } 163 164 167 public int getWidth() { 168 return columns.length; 169 } 170 171 174 public ColumnDescriptor[] getColumns() { 175 return columns; 176 } 177 178 181 public ExtendedType[] getConverters() { 182 return converters; 183 } 184 } | Popular Tags |