1 | /* |
2 | * Copyright 2004-2014 H2 Group. Multiple-Licensed under the MPL 2.0, |
3 | * and the EPL 1.0 (http://h2database.com/html/license.html). |
4 | * Initial Developer: H2 Group |
5 | */ |
6 | package org.h2.index; |
7 | |
8 | import java.sql.ResultSet; |
9 | import java.sql.ResultSetMetaData; |
10 | import java.sql.SQLException; |
11 | import org.h2.engine.Session; |
12 | import org.h2.message.DbException; |
13 | import org.h2.result.Row; |
14 | import org.h2.result.SearchRow; |
15 | import org.h2.value.DataType; |
16 | import org.h2.value.Value; |
17 | |
18 | /** |
19 | * A cursor for a function that returns a JDBC result set. |
20 | */ |
21 | public class FunctionCursorResultSet implements Cursor { |
22 | |
23 | private final Session session; |
24 | private final ResultSet result; |
25 | private final ResultSetMetaData meta; |
26 | private Value[] values; |
27 | private Row row; |
28 | |
29 | FunctionCursorResultSet(Session session, ResultSet result) { |
30 | this.session = session; |
31 | this.result = result; |
32 | try { |
33 | this.meta = result.getMetaData(); |
34 | } catch (SQLException e) { |
35 | throw DbException.convert(e); |
36 | } |
37 | } |
38 | |
39 | @Override |
40 | public Row get() { |
41 | if (values == null) { |
42 | return null; |
43 | } |
44 | if (row == null) { |
45 | row = new Row(values, 1); |
46 | } |
47 | return row; |
48 | } |
49 | |
50 | @Override |
51 | public SearchRow getSearchRow() { |
52 | return get(); |
53 | } |
54 | |
55 | @Override |
56 | public boolean next() { |
57 | row = null; |
58 | try { |
59 | if (result != null && result.next()) { |
60 | int columnCount = meta.getColumnCount(); |
61 | values = new Value[columnCount]; |
62 | for (int i = 0; i < columnCount; i++) { |
63 | int type = DataType.getValueTypeFromResultSet(meta, i + 1); |
64 | values[i] = DataType.readValue(session, result, i + 1, type); |
65 | } |
66 | } else { |
67 | values = null; |
68 | } |
69 | } catch (SQLException e) { |
70 | throw DbException.convert(e); |
71 | } |
72 | return values != null; |
73 | } |
74 | |
75 | @Override |
76 | public boolean previous() { |
77 | throw DbException.throwInternalError(); |
78 | } |
79 | |
80 | } |