LinkedCursor.java 2.0 KB
Newer Older
1
/*
2
 * Copyright 2004-2018 H2 Group. Multiple-Licensed under the MPL 2.0,
3
 * and the EPL 1.0 (http://h2database.com/html/license.html).
4 5 6 7
 * Initial Developer: H2 Group
 */
package org.h2.index;

8
import java.sql.PreparedStatement;
9 10 11
import java.sql.ResultSet;
import java.sql.SQLException;
import org.h2.engine.Session;
12
import org.h2.message.DbException;
13 14 15
import org.h2.result.Row;
import org.h2.result.SearchRow;
import org.h2.table.Column;
16
import org.h2.table.TableLink;
17 18 19 20 21 22 23 24
import org.h2.value.DataType;
import org.h2.value.Value;

/**
 * The cursor implementation for the linked index.
 */
public class LinkedCursor implements Cursor {

25 26 27 28 29
    private final TableLink tableLink;
    private final PreparedStatement prep;
    private final String sql;
    private final Session session;
    private final ResultSet rs;
30 31
    private Row current;

32 33
    LinkedCursor(TableLink tableLink, ResultSet rs, Session session,
            String sql, PreparedStatement prep) {
34
        this.session = session;
35
        this.tableLink = tableLink;
36
        this.rs = rs;
37 38 39 40
        this.sql = sql;
        this.prep = prep;
    }

41
    @Override
42 43 44 45
    public Row get() {
        return current;
    }

46
    @Override
47 48 49 50
    public SearchRow getSearchRow() {
        return current;
    }

51
    @Override
52 53 54 55 56 57 58 59 60 61 62
    public boolean next() {
        try {
            boolean result = rs.next();
            if (!result) {
                rs.close();
                tableLink.reusePreparedStatement(prep, sql);
                current = null;
                return false;
            }
        } catch (SQLException e) {
            throw DbException.convert(e);
63
        }
64
        current = tableLink.getTemplateRow();
65
        for (int i = 0; i < current.getColumnCount(); i++) {
66
            Column col = tableLink.getColumn(i);
67 68 69 70 71 72
            Value v = DataType.readValue(session, rs, i + 1, col.getType());
            current.setValue(i, v);
        }
        return true;
    }

73
    @Override
74
    public boolean previous() {
75
        throw DbException.throwInternalError(toString());
76 77 78
    }

}