File D:\maven-qcr-plugin-testsunitaires\qcr4java\h2-1.3.170-sources\target\site\qc\lecode\value\ValueResultSet.java.html

1 /*
2 * Copyright 2004-2011 H2 Group. Multiple-Licensed under the H2 License,
3 * Version 1.0, and under the Eclipse Public License, Version 1.0
4 * (http://h2database.com/html/license.html).
5 * Initial Developer: H2 Group
6 */
7 package org.h2.value;
8
9 import java.sql.PreparedStatement;
10 import java.sql.ResultSet;
11 import java.sql.ResultSetMetaData;
12 import java.sql.SQLException;
13 import org.h2.message.DbException;
14 import org.h2.tools.SimpleResultSet;
15 import org.h2.util.StatementBuilder;
16
17 /**
18 * Implementation of the RESULT_SET data type.
19 */
20 public class ValueResultSet extends Value {
21
22 private final ResultSet result;
23
24 private ValueResultSet(ResultSet rs) {
25 this.result = rs;
26 }
27
28 /**
29 * Create a result set value for the given result set.
30 * The result set will be wrapped.
31 *
32 * @param rs the result set
33 * @return the value
34 */
35 public static ValueResultSet get(ResultSet rs) {
36 ValueResultSet val = new ValueResultSet(rs);
37 return val;
38 }
39
40 /**
41 * Create a result set value for the given result set. The result set will
42 * be fully read in memory. The original result set is not closed.
43 *
44 * @param rs the result set
45 * @param maxrows the maximum number of rows to read (0 to just read the
46 * meta data)
47 * @return the value
48 */
49 public static ValueResultSet getCopy(ResultSet rs, int maxrows) {
50 try {
51 ResultSetMetaData meta = rs.getMetaData();
52 int columnCount = meta.getColumnCount();
53 SimpleResultSet simple = new SimpleResultSet();
54 simple.setAutoClose(false);
55 ValueResultSet val = new ValueResultSet(simple);
56 for (int i = 0; i < columnCount; i++) {
57 String name = meta.getColumnLabel(i + 1);
58 int sqlType = meta.getColumnType(i + 1);
59 int precision = meta.getPrecision(i + 1);
60 int scale = meta.getScale(i + 1);
61 simple.addColumn(name, sqlType, precision, scale);
62 }
63 for (int i = 0; i < maxrows && rs.next(); i++) {
64 Object[] list = new Object[columnCount];
65 for (int j = 0; j < columnCount; j++) {
66 list[j] = rs.getObject(j + 1);
67 }
68 simple.addRow(list);
69 }
70 return val;
71 } catch (SQLException e) {
72 throw DbException.convert(e);
73 }
74 }
75
76 public int getType() {
77 return Value.RESULT_SET;
78 }
79
80 public long getPrecision() {
81 return Integer.MAX_VALUE;
82 }
83
84 public int getDisplaySize() {
85 // it doesn't make sense to calculate it
86 return Integer.MAX_VALUE;
87 }
88
89 public String getString() {
90 try {
91 StatementBuilder buff = new StatementBuilder("(");
92 result.beforeFirst();
93 ResultSetMetaData meta = result.getMetaData();
94 int columnCount = meta.getColumnCount();
95 for (int i = 0; result.next(); i++) {
96 if (i > 0) {
97 buff.append(", ");
98 }
99 buff.append('(');
100 buff.resetCount();
101 for (int j = 0; j < columnCount; j++) {
102 buff.appendExceptFirst(", ");
103 int t = DataType.convertSQLTypeToValueType(meta.getColumnType(j + 1));
104 Value v = DataType.readValue(null, result, j+1, t);
105 buff.append(v.getString());
106 }
107 buff.append(')');
108 }
109 result.beforeFirst();
110 return buff.append(')').toString();
111 } catch (SQLException e) {
112 throw DbException.convert(e);
113 }
114 }
115
116 protected int compareSecure(Value v, CompareMode mode) {
117 return this == v ? 0 : super.toString().compareTo(v.toString());
118 }
119
120 public boolean equals(Object other) {
121 return other == this;
122 }
123
124 public int hashCode() {
125 return 0;
126 }
127
128 public Object getObject() {
129 return result;
130 }
131
132 public ResultSet getResultSet() {
133 return result;
134 }
135
136 public void set(PreparedStatement prep, int parameterIndex) {
137 throw throwUnsupportedExceptionForType("PreparedStatement.set");
138 }
139
140 public String getSQL() {
141 return "";
142 }
143
144 public Value convertPrecision(long precision, boolean force) {
145 if (!force) {
146 return this;
147 }
148 return ValueResultSet.get(new SimpleResultSet());
149 }
150
151 }
152