1 19 20 package org.netbeans.modules.db.test.jdbcstub; 21 22 import java.sql.SQLException ; 23 import java.util.HashMap ; 24 import java.util.Iterator ; 25 import java.util.List ; 26 import java.util.Map ; 27 import org.netbeans.modules.j2ee.persistence.editor.completion.*; 28 import org.netbeans.test.stub.api.StubDelegate; 29 30 34 public class ResultSetImpl extends StubDelegate { 35 36 private List columns; 37 private Map names2iterators = new HashMap (); 38 private Map names2values; 40 public ResultSetImpl(List columns) { 41 this.columns = columns; 42 43 for (Iterator it = columns.iterator(); it.hasNext();) { 44 List column = (List )it.next(); 45 Iterator columnIterator = column.iterator(); 46 String columnName = columnIterator.next().toString(); 47 names2iterators.put(columnName, columnIterator); 48 } 49 } 50 51 public boolean next() { 52 if (names2values != null) { 53 names2values.clear(); 54 } else { 55 names2values = new HashMap (); 56 } 57 58 Iterator it = names2iterators.entrySet().iterator(); 59 if (!it.hasNext()) { 60 return false; 61 } 62 63 while (it.hasNext()) { 64 Map.Entry entry = (Map.Entry )it.next(); 65 String columnName = (String )entry.getKey(); 66 Iterator columnIterator = (Iterator )entry.getValue(); 67 68 if (!columnIterator.hasNext()) { 69 return false; 70 } 71 72 Object value = columnIterator.next(); 73 names2values.put(columnName, value); 74 } 75 76 return true; 77 } 78 79 public Object getObject(String columnName) throws SQLException { 80 if (names2values == null) { 81 throw new SQLException ("The next() method has not been called yet"); 82 } 83 if (!names2values.containsKey(columnName)) { 84 throw new SQLException ("Unknown column name " + columnName + "."); 85 } 86 return names2values.get(columnName); 87 } 88 89 public short getShort(String columnName) throws SQLException { 90 Object value = getObject(columnName); 91 if (value instanceof Short ) { 92 return ((Short )value).shortValue(); 93 } else { 94 throw new SQLException (value + "is not a short."); 95 } 96 } 97 98 public int getInt(String columnName) throws SQLException { 99 Object value = getObject(columnName); 100 if (value instanceof Integer ){ 101 return ((Integer )value).intValue(); 102 } else { 103 throw new SQLException (value + " is not an int."); 104 } 105 } 106 107 public boolean getBoolean(String columnName) throws SQLException { 108 Object value = getObject(columnName); 109 if (value instanceof Boolean ) { 110 return ((Boolean )value).booleanValue(); 111 } else { 112 throw new SQLException (value + " is not a boolean."); 113 } 114 } 115 116 public String getString(String columnName) throws SQLException { 117 Object value = getObject(columnName); 118 return value != null ? value.toString() : null; 119 } 120 121 public void close() { 122 } 123 } 124 | Popular Tags |