| 1 |
/* |
| 2 |
* Copyright 2004-2023 H2 Group. Multiple-Licensed under the MPL 2.0, |
| 3 |
* and the EPL 1.0 (https://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.ResultInterface; |
| 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 query expression index. |
| 18 |
*/ |
| 19 |
public class QueryExpressionCursor implements Cursor { |
| 20 |
|
| 21 |
private final Table table; |
| 22 |
private final QueryExpressionIndex index; |
| 23 |
private final ResultInterface result; |
| 24 |
private final SearchRow first, last; |
| 25 |
private Row current; |
| 26 |
|
| 27 |
public QueryExpressionCursor(QueryExpressionIndex index, ResultInterface result, SearchRow first, SearchRow last) { |
| 28 |
this.table = index.getTable(); |
| 29 |
this.index = index; |
| 30 |
this.result = result; |
| 31 |
this.first = first; |
| 32 |
this.last = last; |
| 33 |
} |
| 34 |
|
| 35 |
@Override |
| 36 |
public Row get() { |
| 37 |
return current; |
| 38 |
} |
| 39 |
|
| 40 |
@Override |
| 41 |
public SearchRow getSearchRow() { |
| 42 |
return current; |
| 43 |
} |
| 44 |
|
| 45 |
@Override |
| 46 |
public boolean next() { |
| 47 |
while (true) { |
| 48 |
boolean res = result.next(); |
| 49 |
if (!res) { |
| 50 |
if (index.isRecursive()) { |
| 51 |
result.reset(); |
| 52 |
} else { |
| 53 |
result.close(); |
| 54 |
} |
| 55 |
current = null; |
| 56 |
return false; |
| 57 |
} |
| 58 |
current = table.getTemplateRow(); |
| 59 |
Value[] values = result.currentRow(); |
| 60 |
for (int i = 0, len = current.getColumnCount(); i < len; i++) { |
| 61 |
Value v = i < values.length ? values[i] : ValueNull.INSTANCE; |
| 62 |
current.setValue(i, v); |
| 63 |
} |
| 64 |
int comp; |
| 65 |
if (first != null) { |
| 66 |
comp = index.compareRows(current, first); |
| 67 |
if (comp < 0) { |
| 68 |
continue; |
| 69 |
} |
| 70 |
} |
| 71 |
if (last != null) { |
| 72 |
comp = index.compareRows(current, last); |
| 73 |
if (comp > 0) { |
| 74 |
continue; |
| 75 |
} |
| 76 |
} |
| 77 |
return true; |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
@Override |
| 82 |
public boolean previous() { |
| 83 |
throw DbException.getInternalError(toString()); |
| 84 |
} |
| 85 |
|
| 86 |
} |