1 21 22 package org.apache.derby.impl.sql; 23 24 import org.apache.derby.iapi.sql.ResultColumnDescriptor; 25 import org.apache.derby.iapi.sql.ResultDescription; 26 27 import org.apache.derby.iapi.services.sanity.SanityManager; 28 29 import org.apache.derby.iapi.services.io.StoredFormatIds; 30 import org.apache.derby.iapi.services.io.FormatIdUtil; 31 import org.apache.derby.iapi.services.io.Formatable; 32 33 import java.io.ObjectOutput ; 34 import java.io.ObjectInput ; 35 import java.io.IOException ; 36 44 public final class GenericResultDescription 45 implements ResultDescription, Formatable 46 { 47 48 61 62 private ResultColumnDescriptor[] columns; 63 private String statementType; 64 65 68 public GenericResultDescription() 69 { 70 } 71 72 78 public GenericResultDescription(ResultColumnDescriptor[] columns, 79 String statementType) 80 { 81 this.columns = columns; 82 this.statementType = statementType; 83 } 84 85 91 public GenericResultDescription 92 ( 93 ResultDescription rd, 94 int[] theCols 95 ) 96 { 97 if (SanityManager.DEBUG) 98 { 99 SanityManager.ASSERT(theCols != null, "theCols argument to GenericResultDescription is null"); 100 } 101 102 this.columns = new ResultColumnDescriptor[theCols.length]; 103 for (int i = 0; i < theCols.length; i++) 104 { 105 columns[i] = rd.getColumnDescriptor(theCols[i]); 106 } 107 this.statementType = rd.getStatementType(); 108 } 109 110 116 public String getStatementType() { 117 return statementType; 118 } 119 120 123 public int getColumnCount() 124 { 125 return (columns == null) ? 0 : columns.length; 126 } 127 128 public ResultColumnDescriptor[] getColumnInfo() { 129 return columns; 130 } 131 132 136 public ResultColumnDescriptor getColumnDescriptor(int position) { 137 return columns[position-1]; 138 } 139 140 152 public ResultDescription truncateColumns(int truncateFrom) 153 { 154 if (SanityManager.DEBUG) 155 { 156 if (!(truncateFrom > 0 && columns != null)) 157 { 158 SanityManager.THROWASSERT("bad truncate value: "+truncateFrom+" is too low"); 159 } 160 if (truncateFrom > columns.length) 161 { 162 SanityManager.THROWASSERT("bad truncate value: "+truncateFrom+" is too high"); 163 } 164 } 165 ResultColumnDescriptor[] newColumns = new ResultColumnDescriptor[truncateFrom-1]; 166 System.arraycopy(columns, 0, newColumns, 0, newColumns.length); 167 return new GenericResultDescription(newColumns, statementType); 168 } 169 170 171 183 public void writeExternal(ObjectOutput out) throws IOException 184 { 185 int len = (columns == null) ? 0 : columns.length; 186 187 out.writeObject(statementType); 188 out.writeInt(len); 189 while(len-- > 0) 190 { 191 197 if (!(columns[len] instanceof 198 GenericColumnDescriptor)) 199 { 200 columns[len] = new GenericColumnDescriptor(columns[len]); 201 } 202 out.writeObject(columns[len]); 203 } 204 } 205 206 214 public void readExternal(ObjectInput in) 215 throws IOException , ClassNotFoundException 216 { 217 int len; 218 219 columns = null; 220 statementType = (String )in.readObject(); 221 len = in.readInt(); 222 if (len > 0) 223 { 224 columns = new GenericColumnDescriptor[len]; 225 while(len-- > 0) 226 { 227 columns[len] = (ResultColumnDescriptor)in.readObject(); 228 } 229 } 230 } 231 232 237 public int getTypeFormatId() { return StoredFormatIds.GENERIC_RESULT_DESCRIPTION_V01_ID; } 238 239 240 241 public String toString() 242 { 243 if (SanityManager.DEBUG) 244 { 245 StringBuffer colStr = new StringBuffer (); 246 for (int i = 0; i < columns.length; i++) 247 { 248 colStr.append("column["+i+"]\n"); 249 colStr.append(columns[i].toString()); 250 } 251 return "GenericResultDescription\n" + 252 "\tStatementType = "+statementType+"\n" + 253 "\tCOLUMNS\n" + colStr.toString(); 254 } 255 else 256 { 257 return ""; 258 } 259 } 260 } 261 262 | Popular Tags |