01: /*
02: * Copyright Aduna (http://www.aduna-software.com/) (c) 2008.
03: *
04: * Licensed under the Aduna BSD-style license.
05: */
06: package org.openrdf.sail.rdbms.iteration;
07:
08: import java.sql.PreparedStatement;
09: import java.sql.ResultSet;
10: import java.sql.SQLException;
11:
12: import org.openrdf.sail.SailException;
13: import org.openrdf.sail.rdbms.RdbmsValueFactory;
14: import org.openrdf.sail.rdbms.exceptions.RdbmsException;
15: import org.openrdf.sail.rdbms.iteration.base.RdbmIterationBase;
16: import org.openrdf.sail.rdbms.model.RdbmsResource;
17: import org.openrdf.sail.rdbms.model.RdbmsStatement;
18: import org.openrdf.sail.rdbms.model.RdbmsURI;
19: import org.openrdf.sail.rdbms.model.RdbmsValue;
20: import org.openrdf.sail.rdbms.schema.IdSequence;
21: import org.openrdf.sail.rdbms.schema.ValueTable;
22:
23: /**
24: * Converts a {@link ResultSet} into a {@link RdbmsStatement} in an iteration.
25: *
26: * @author James Leigh
27: *
28: */
29: public class RdbmsStatementIteration extends
30: RdbmIterationBase<RdbmsStatement, SailException> {
31: private RdbmsValueFactory vf;
32: private IdSequence ids;
33:
34: public RdbmsStatementIteration(RdbmsValueFactory vf,
35: PreparedStatement stmt, IdSequence ids) throws SQLException {
36: super (stmt);
37: this .vf = vf;
38: this .ids = ids;
39: }
40:
41: @Override
42: protected RdbmsStatement convert(ResultSet rs) throws SQLException {
43: RdbmsResource ctx = createResource(rs, 1);
44: RdbmsResource subj = createResource(rs, 3);
45: RdbmsURI pred = (RdbmsURI) createResource(rs, 5);
46: RdbmsValue obj = createValue(rs, 7);
47: return new RdbmsStatement(subj, pred, obj, ctx);
48: }
49:
50: @Override
51: protected RdbmsException convertSQLException(SQLException e) {
52: return new RdbmsException(e);
53: }
54:
55: private RdbmsResource createResource(ResultSet rs, int index)
56: throws SQLException {
57: Number id = ids.idOf(rs.getLong(index));
58: if (id.longValue() == ValueTable.NIL_ID)
59: return null;
60: String stringValue = rs.getString(index + 1);
61: return vf.getRdbmsResource(id, stringValue);
62: }
63:
64: private RdbmsValue createValue(ResultSet rs, int index)
65: throws SQLException {
66: Number id = ids.idOf(rs.getLong(index));
67: if (ids.isLiteral(id)) {
68: String label = rs.getString(index + 1);
69: String datatype = rs.getString(index + 2);
70: String language = rs.getString(index + 3);
71: return vf.getRdbmsLiteral(id, label, language, datatype);
72: }
73: return createResource(rs, index);
74: }
75:
76: }
|