KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > h2 > value > ValueResultSet


1 /*
2  * Copyright 2004-2006 H2 Group. Licensed under the H2 License, Version 1.0 (http://h2database.com/html/license.html).
3  * Initial Developer: H2 Group
4  */

5 package org.h2.value;
6
7 import java.sql.PreparedStatement JavaDoc;
8 import java.sql.ResultSet JavaDoc;
9 import java.sql.ResultSetMetaData JavaDoc;
10 import java.sql.SQLException JavaDoc;
11
12 import org.h2.message.Message;
13 import org.h2.tools.SimpleResultSet;
14
15 public class ValueResultSet extends Value {
16
17     private ResultSet JavaDoc result;
18     
19     public static ValueResultSet get(ResultSet JavaDoc rs) throws SQLException JavaDoc {
20         ValueResultSet val = new ValueResultSet();
21         val.result = rs;
22         return val;
23     }
24     
25     public static ValueResultSet getCopy(ResultSet JavaDoc rs, int maxrows) throws SQLException JavaDoc {
26         ValueResultSet val = new ValueResultSet();
27         ResultSetMetaData JavaDoc 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 JavaDoc 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 JavaDoc[] list = new Object JavaDoc[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         // it doesn't make sense to calculate this
58
return 100;
59     }
60
61     public String JavaDoc getString() throws SQLException JavaDoc {
62         StringBuffer JavaDoc buff = new StringBuffer JavaDoc();
63         buff.append("(");
64         result.beforeFirst();
65         ResultSetMetaData JavaDoc 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 JavaDoc {
88         throw Message.getUnsupportedException();
89     }
90
91     protected boolean isEqual(Value v) {
92         return false;
93     }
94
95     public Object JavaDoc getObject() throws SQLException JavaDoc {
96         return result;
97     }
98     
99     public ResultSet JavaDoc getResultSet() {
100         return result;
101     }
102
103     public void set(PreparedStatement JavaDoc prep, int parameterIndex) throws SQLException JavaDoc {
104         throw Message.getUnsupportedException();
105     }
106
107     public String JavaDoc getSQL() {
108         return "";
109     }
110
111 }
112
Popular Tags