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