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 org.h2.message.DbException; |
9 | import org.h2.result.LocalResult; |
10 | import org.h2.result.Row; |
11 | import org.h2.result.SearchRow; |
12 | import org.h2.table.Table; |
13 | import org.h2.value.Value; |
14 | import org.h2.value.ValueNull; |
15 | |
16 | /** |
17 | * The cursor implementation of a view index. |
18 | */ |
19 | public class ViewCursor implements Cursor { |
20 | |
21 | private final Table table; |
22 | private final ViewIndex index; |
23 | private final LocalResult result; |
24 | private final SearchRow first, last; |
25 | private Row current; |
26 | |
27 | ViewCursor(ViewIndex index, LocalResult result, SearchRow first, |
28 | SearchRow last) { |
29 | this.table = index.getTable(); |
30 | this.index = index; |
31 | this.result = result; |
32 | this.first = first; |
33 | this.last = last; |
34 | } |
35 | |
36 | @Override |
37 | public Row get() { |
38 | return current; |
39 | } |
40 | |
41 | @Override |
42 | public SearchRow getSearchRow() { |
43 | return current; |
44 | } |
45 | |
46 | @Override |
47 | public boolean next() { |
48 | while (true) { |
49 | boolean res = result.next(); |
50 | if (!res) { |
51 | if (index.isRecursive()) { |
52 | result.reset(); |
53 | } else { |
54 | result.close(); |
55 | } |
56 | current = null; |
57 | return false; |
58 | } |
59 | current = table.getTemplateRow(); |
60 | Value[] values = result.currentRow(); |
61 | for (int i = 0, len = current.getColumnCount(); i < len; i++) { |
62 | Value v = i < values.length ? values[i] : ValueNull.INSTANCE; |
63 | current.setValue(i, v); |
64 | } |
65 | int comp; |
66 | if (first != null) { |
67 | comp = index.compareRows(current, first); |
68 | if (comp < 0) { |
69 | continue; |
70 | } |
71 | } |
72 | if (last != null) { |
73 | comp = index.compareRows(current, last); |
74 | if (comp > 0) { |
75 | continue; |
76 | } |
77 | } |
78 | return true; |
79 | } |
80 | } |
81 | |
82 | @Override |
83 | public boolean previous() { |
84 | throw DbException.throwInternalError(); |
85 | } |
86 | |
87 | } |