1 5 package org.h2.value; 6 7 import java.sql.PreparedStatement ; 8 import java.sql.ResultSet ; 9 import java.sql.ResultSetMetaData ; 10 import java.sql.SQLException ; 11 12 import org.h2.message.Message; 13 import org.h2.tools.SimpleResultSet; 14 15 public class ValueResultSet extends Value { 16 17 private ResultSet result; 18 19 public static ValueResultSet get(ResultSet rs) throws SQLException { 20 ValueResultSet val = new ValueResultSet(); 21 val.result = rs; 22 return val; 23 } 24 25 public static ValueResultSet getCopy(ResultSet rs, int maxrows) throws SQLException { 26 ValueResultSet val = new ValueResultSet(); 27 ResultSetMetaData meta = rs.getMetaData(); 28 int columnCount = meta.getColumnCount(); 29 SimpleResultSet simple = new SimpleResultSet(); 30 val.result = simple; 31 for(int i=0; i<columnCount; i++) { 32 String name = meta.getColumnLabel(i+1); 33 int sqlType = meta.getColumnType(i+1); 34 int precision = meta.getPrecision(i+1); 35 int scale = meta.getScale(i+1); 36 simple.addColumn(name, sqlType, precision, scale); 37 } 38 for(int i=0; i<maxrows && rs.next(); i++) { 39 Object [] list = new Object [columnCount]; 40 for(int j=0; j<columnCount; j++) { 41 list[j] = rs.getObject(j+1); 42 } 43 simple.addRow(list); 44 } 45 return val; 46 } 47 48 public int getType() { 49 return Value.RESULT_SET; 50 } 51 52 public long getPrecision() { 53 return 0; 54 } 55 56 public int getDisplaySize() { 57 return 100; 59 } 60 61 public String getString() throws SQLException { 62 StringBuffer buff = new StringBuffer (); 63 buff.append("("); 64 result.beforeFirst(); 65 ResultSetMetaData meta = result.getMetaData(); 66 int columnCount = meta.getColumnCount(); 67 for(int i=0; result.next(); i++) { 68 if(i>0) { 69 buff.append(", "); 70 } 71 buff.append('('); 72 for(int j=0; j<columnCount; j++) { 73 if (j > 0) { 74 buff.append(", "); 75 } 76 int t = DataType.convertSQLTypeToValueType(meta.getColumnType(j + 1)); 77 Value v = DataType.readValue(null, result, j+1, t); 78 buff.append(v.getString()); 79 } 80 buff.append(')'); 81 } 82 buff.append(")"); 83 result.beforeFirst(); 84 return buff.toString(); 85 } 86 87 protected int compareSecure(Value v, CompareMode mode) throws SQLException { 88 throw Message.getUnsupportedException(); 89 } 90 91 protected boolean isEqual(Value v) { 92 return false; 93 } 94 95 public Object getObject() throws SQLException { 96 return result; 97 } 98 99 public ResultSet getResultSet() { 100 return result; 101 } 102 103 public void set(PreparedStatement prep, int parameterIndex) throws SQLException { 104 throw Message.getUnsupportedException(); 105 } 106 107 public String getSQL() { 108 return ""; 109 } 110 111 } 112 | Popular Tags |