001: /*
002: * Copyright 2004-2008 H2 Group. Licensed under the H2 License, Version 1.0
003: * (http://h2database.com/html/license.html).
004: * Initial Developer: H2 Group
005: */
006: package org.h2.value;
007:
008: import java.sql.PreparedStatement;
009: import java.sql.ResultSet;
010: import java.sql.ResultSetMetaData;
011: import java.sql.SQLException;
012:
013: import org.h2.message.Message;
014: import org.h2.tools.SimpleResultSet;
015:
016: /**
017: * Implementation of the RESULT_SET data type.
018: */
019: public class ValueResultSet extends Value {
020:
021: private final ResultSet result;
022:
023: private ValueResultSet(ResultSet rs) {
024: this .result = rs;
025: }
026:
027: public static ValueResultSet get(ResultSet rs) throws SQLException {
028: ValueResultSet val = new ValueResultSet(rs);
029: return val;
030: }
031:
032: public static ValueResultSet getCopy(ResultSet rs, int maxrows)
033: throws SQLException {
034: ResultSetMetaData meta = rs.getMetaData();
035: int columnCount = meta.getColumnCount();
036: SimpleResultSet simple = new SimpleResultSet();
037: ValueResultSet val = new ValueResultSet(simple);
038: for (int i = 0; i < columnCount; i++) {
039: String name = meta.getColumnLabel(i + 1);
040: int sqlType = meta.getColumnType(i + 1);
041: int precision = meta.getPrecision(i + 1);
042: int scale = meta.getScale(i + 1);
043: simple.addColumn(name, sqlType, precision, scale);
044: }
045: for (int i = 0; i < maxrows && rs.next(); i++) {
046: Object[] list = new Object[columnCount];
047: for (int j = 0; j < columnCount; j++) {
048: list[j] = rs.getObject(j + 1);
049: }
050: simple.addRow(list);
051: }
052: return val;
053: }
054:
055: public int getType() {
056: return Value.RESULT_SET;
057: }
058:
059: public long getPrecision() {
060: return 0;
061: }
062:
063: public int getDisplaySize() {
064: // it doesn't make sense to calculate it
065: return Integer.MAX_VALUE;
066: }
067:
068: public String getString() {
069: try {
070: StringBuffer buff = new StringBuffer();
071: buff.append("(");
072: result.beforeFirst();
073: ResultSetMetaData meta = result.getMetaData();
074: int columnCount = meta.getColumnCount();
075: for (int i = 0; result.next(); i++) {
076: if (i > 0) {
077: buff.append(", ");
078: }
079: buff.append('(');
080: for (int j = 0; j < columnCount; j++) {
081: if (j > 0) {
082: buff.append(", ");
083: }
084: int t = DataType.convertSQLTypeToValueType(meta
085: .getColumnType(j + 1));
086: Value v = DataType
087: .readValue(null, result, j + 1, t);
088: buff.append(v.getString());
089: }
090: buff.append(')');
091: }
092: buff.append(")");
093: result.beforeFirst();
094: return buff.toString();
095: } catch (SQLException e) {
096: throw Message.convertToInternal(e);
097: }
098: }
099:
100: protected int compareSecure(Value v, CompareMode mode)
101: throws SQLException {
102: throw Message.getUnsupportedException();
103: }
104:
105: public boolean equals(Object other) {
106: return other == this ;
107: }
108:
109: public int hashCode() {
110: return 0;
111: }
112:
113: public Object getObject() {
114: return result;
115: }
116:
117: public ResultSet getResultSet() {
118: return result;
119: }
120:
121: public void set(PreparedStatement prep, int parameterIndex)
122: throws SQLException {
123: throw Message.getUnsupportedException();
124: }
125:
126: public String getSQL() {
127: return "";
128: }
129:
130: }
|