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.PreparedStatement; |
9 | import java.sql.ResultSet; |
10 | import java.sql.SQLException; |
11 | |
12 | import org.h2.engine.Session; |
13 | import org.h2.message.DbException; |
14 | import org.h2.result.Row; |
15 | import org.h2.result.SearchRow; |
16 | import org.h2.table.Column; |
17 | import org.h2.table.TableLink; |
18 | import org.h2.value.DataType; |
19 | import org.h2.value.Value; |
20 | |
21 | /** |
22 | * The cursor implementation for the linked index. |
23 | */ |
24 | public class LinkedCursor implements Cursor { |
25 | |
26 | private final TableLink tableLink; |
27 | private final PreparedStatement prep; |
28 | private final String sql; |
29 | private final Session session; |
30 | private final ResultSet rs; |
31 | private Row current; |
32 | |
33 | LinkedCursor(TableLink tableLink, ResultSet rs, Session session, |
34 | String sql, PreparedStatement prep) { |
35 | this.session = session; |
36 | this.tableLink = tableLink; |
37 | this.rs = rs; |
38 | this.sql = sql; |
39 | this.prep = prep; |
40 | } |
41 | |
42 | @Override |
43 | public Row get() { |
44 | return current; |
45 | } |
46 | |
47 | @Override |
48 | public SearchRow getSearchRow() { |
49 | return current; |
50 | } |
51 | |
52 | @Override |
53 | public boolean next() { |
54 | try { |
55 | boolean result = rs.next(); |
56 | if (!result) { |
57 | rs.close(); |
58 | tableLink.reusePreparedStatement(prep, sql); |
59 | current = null; |
60 | return false; |
61 | } |
62 | } catch (SQLException e) { |
63 | throw DbException.convert(e); |
64 | } |
65 | current = tableLink.getTemplateRow(); |
66 | for (int i = 0; i < current.getColumnCount(); i++) { |
67 | Column col = tableLink.getColumn(i); |
68 | Value v = DataType.readValue(session, rs, i + 1, col.getType()); |
69 | current.setValue(i, v); |
70 | } |
71 | return true; |
72 | } |
73 | |
74 | @Override |
75 | public boolean previous() { |
76 | throw DbException.throwInternalError(); |
77 | } |
78 | |
79 | } |