1 21 22 package org.apache.derby.catalog; 23 24 import java.sql.Types ; 25 import java.lang.reflect.*; 26 import java.sql.ResultSetMetaData ; 27 import java.sql.SQLException ; 28 import java.sql.DatabaseMetaData ; 29 import org.apache.derby.catalog.TypeDescriptor; 30 31 import org.apache.derby.iapi.types.DataTypeDescriptor; 32 import org.apache.derby.iapi.types.DataTypeUtilities; 33 import org.apache.derby.iapi.sql.ResultColumnDescriptor; 34 import org.apache.derby.impl.jdbc.EmbedResultSetMetaData; 35 import org.apache.derby.catalog.types.RoutineAliasInfo; 36 37 import org.apache.derby.shared.common.reference.JDBC40Translation; 38 84 85 public class GetProcedureColumns extends org.apache.derby.vti.VTITemplate 86 { 87 private boolean isFunction; 88 private int translate(int val) { 89 if (!isFunction) { return val; } 90 switch (val) { 91 case DatabaseMetaData.procedureColumnUnknown: 92 return JDBC40Translation.FUNCTION_PARAMETER_UNKNOWN; 93 case DatabaseMetaData.procedureColumnIn: 94 return JDBC40Translation.FUNCTION_PARAMETER_IN; 95 case DatabaseMetaData.procedureColumnInOut: 96 return JDBC40Translation.FUNCTION_PARAMETER_INOUT; 97 case DatabaseMetaData.procedureColumnOut: 98 return JDBC40Translation.FUNCTION_PARAMETER_OUT; 99 case DatabaseMetaData.procedureColumnReturn: 100 return JDBC40Translation.FUNCTION_RETURN; 101 default: 102 return JDBC40Translation.FUNCTION_PARAMETER_UNKNOWN; 103 } 104 } 105 106 private boolean isProcedure; 107 private RoutineAliasInfo procedure; 109 private int paramCursor; 110 private short method_count; 111 private short param_number; 112 113 private TypeDescriptor sqlType; 114 private String columnName; 115 private short columnType; 116 private final short nullable; 117 118 public ResultSetMetaData getMetaData() 119 { 120 return metadata; 121 } 122 123 public GetProcedureColumns(AliasInfo aliasInfo, String aliasType) throws SQLException 130 { 131 if (aliasInfo != null) { 133 isProcedure = aliasType.equals("P"); 134 isFunction = aliasType.equals("F"); 135 procedure = (RoutineAliasInfo) aliasInfo; 136 method_count = (short) procedure.getParameterCount(); 137 } 138 if (aliasType == null) { 139 nullable = 0; 140 return; 141 } 142 143 if (isFunction) { 144 nullable = (short) JDBC40Translation.FUNCTION_NULLABLE; 145 sqlType = procedure.getReturnType(); 146 columnName = ""; columnType = (short) JDBC40Translation.FUNCTION_RETURN; 148 paramCursor = -2; 149 return; 150 } 151 nullable = (short) DatabaseMetaData.procedureNullable; 152 153 paramCursor = -1; 154 } 155 156 public boolean next() throws SQLException { 157 if (++paramCursor >= procedure.getParameterCount()) 158 return false; 159 160 if (paramCursor > -1) { 161 sqlType = procedure.getParameterTypes()[paramCursor]; 162 columnName = procedure.getParameterNames()[paramCursor]; 163 columnType = 164 (short)translate(procedure.getParameterModes()[paramCursor]); 165 } 166 param_number = (short) paramCursor; 167 return true; 168 } 169 170 public String getString(int column) throws SQLException 177 { 178 switch (column) 179 { 180 case 1: return columnName; 182 183 case 4: return sqlType.getTypeName(); 185 186 case 10: return null; 188 189 default: 190 return super.getString(column); } 192 } 193 194 public int getInt(int column) throws SQLException 201 { 202 switch (column) 203 { 204 case 3: if (sqlType != null) { 206 return sqlType.getJDBCTypeId(); 207 } 208 return java.sql.Types.JAVA_OBJECT; 209 210 case 5: if (sqlType != null) 212 { 213 int type = sqlType.getJDBCTypeId(); 214 if (DataTypeDescriptor.isNumericType(type)) 215 return sqlType.getPrecision(); 216 else if (type == Types.DATE || type == Types.TIME 217 || type == Types.TIMESTAMP) 218 return DataTypeUtilities.getColumnDisplaySize(type, -1); 219 else 220 return sqlType.getMaximumWidth(); 221 } 222 223 return 0; 225 226 case 6: if (sqlType != null) 228 return sqlType.getMaximumWidthInBytes(); 229 230 return 0; 232 233 default: 234 return super.getInt(column); } 236 } 237 238 public short getShort(int column) throws SQLException 245 { 246 switch (column) 247 { 248 case 2: return columnType; 250 251 case 7: if (sqlType != null) 253 return (short)sqlType.getScale(); 254 255 return 0; 257 258 case 8: if (sqlType != null) 260 { 261 int sqlTypeID = sqlType.getJDBCTypeId(); 262 if (sqlTypeID == java.sql.Types.REAL || 263 sqlTypeID == java.sql.Types.FLOAT || 264 sqlTypeID == java.sql.Types.DOUBLE) 265 { 266 return 2; 267 } 268 return 10; 269 } 270 271 return 0; 273 274 case 9: return nullable; 277 278 case 11: return method_count; 280 281 case 12: return param_number; 283 284 default: 285 return super.getShort(column); } 287 } 288 289 public void close() 290 { 291 } 292 293 296 private static final ResultColumnDescriptor[] columnInfo = { 297 298 EmbedResultSetMetaData.getResultColumnDescriptor("COLUMN_NAME", Types.VARCHAR, false, 128), 299 EmbedResultSetMetaData.getResultColumnDescriptor("COLUMN_TYPE", Types.SMALLINT, false), 300 EmbedResultSetMetaData.getResultColumnDescriptor("DATA_TYPE", Types.INTEGER, false), 301 EmbedResultSetMetaData.getResultColumnDescriptor("TYPE_NAME", Types.VARCHAR, false, 22), 302 EmbedResultSetMetaData.getResultColumnDescriptor("PRECISION", Types.INTEGER, false), 303 EmbedResultSetMetaData.getResultColumnDescriptor("LENGTH", Types.INTEGER, false), 304 EmbedResultSetMetaData.getResultColumnDescriptor("SCALE", Types.SMALLINT, false), 305 306 EmbedResultSetMetaData.getResultColumnDescriptor("RADIX", Types.SMALLINT, false), 307 EmbedResultSetMetaData.getResultColumnDescriptor("NULLABLE", Types.SMALLINT, false), 308 EmbedResultSetMetaData.getResultColumnDescriptor("REMARKS", Types.VARCHAR, true, 22), 309 EmbedResultSetMetaData.getResultColumnDescriptor("METHOD_ID", Types.SMALLINT, false), 310 EmbedResultSetMetaData.getResultColumnDescriptor("PARAMETER_ID", Types.SMALLINT, false), 311 }; 312 private static final ResultSetMetaData metadata = new EmbedResultSetMetaData(columnInfo); 313 } 314 | Popular Tags |