001: /*
002: * Copyright Aduna (http://www.aduna-software.com/) (c) 2008.
003: *
004: * Licensed under the Aduna BSD-style license.
005: */
006: package org.openrdf.sail.rdbms.iteration;
007:
008: import java.sql.PreparedStatement;
009: import java.sql.ResultSet;
010: import java.sql.SQLException;
011: import java.util.Collection;
012:
013: import org.openrdf.model.Value;
014: import org.openrdf.query.BindingSet;
015: import org.openrdf.query.QueryEvaluationException;
016: import org.openrdf.query.algebra.evaluation.QueryBindingSet;
017: import org.openrdf.sail.rdbms.RdbmsValueFactory;
018: import org.openrdf.sail.rdbms.algebra.ColumnVar;
019: import org.openrdf.sail.rdbms.exceptions.RdbmsQueryEvaluationException;
020: import org.openrdf.sail.rdbms.iteration.base.RdbmIterationBase;
021: import org.openrdf.sail.rdbms.model.RdbmsResource;
022: import org.openrdf.sail.rdbms.model.RdbmsValue;
023: import org.openrdf.sail.rdbms.schema.IdSequence;
024: import org.openrdf.sail.rdbms.schema.ValueTable;
025:
026: /**
027: * Converts a {@link ResultSet} into a {@link BindingSet} in an iteration.
028: *
029: * @author James Leigh
030: *
031: */
032: public class RdbmsBindingIteration extends
033: RdbmIterationBase<BindingSet, QueryEvaluationException> {
034: private BindingSet bindings;
035: private Collection<ColumnVar> projections;
036: private RdbmsValueFactory vf;
037: private IdSequence ids;
038:
039: public RdbmsBindingIteration(PreparedStatement stmt)
040: throws SQLException {
041: super (stmt);
042: }
043:
044: public void setBindings(BindingSet bindings) {
045: this .bindings = bindings;
046: }
047:
048: public void setProjections(Collection<ColumnVar> proj) {
049: this .projections = proj;
050: }
051:
052: public void setValueFactory(RdbmsValueFactory vf) {
053: this .vf = vf;
054: }
055:
056: public void setIdSequence(IdSequence ids) {
057: this .ids = ids;
058: }
059:
060: @Override
061: protected BindingSet convert(ResultSet rs) throws SQLException {
062: QueryBindingSet result = new QueryBindingSet(bindings);
063: for (ColumnVar var : projections) {
064: String name = var.getName();
065: if (var != null && !result.hasBinding(name)) {
066: Value value = var.getValue();
067: if (value == null) {
068: value = createValue(rs, var.getIndex() + 1);
069: }
070: if (value != null) {
071: result.addBinding(var.getName(), value);
072: }
073: }
074: }
075: return result;
076: }
077:
078: @Override
079: protected QueryEvaluationException convertSQLException(
080: SQLException e) {
081: return new RdbmsQueryEvaluationException(e);
082: }
083:
084: private RdbmsResource createResource(ResultSet rs, int index)
085: throws SQLException {
086: Number id = ids.idOf(rs.getLong(index));
087: if (id.longValue() == ValueTable.NIL_ID)
088: return null;
089: return vf.getRdbmsResource(id, rs.getString(index + 1));
090: }
091:
092: private RdbmsValue createValue(ResultSet rs, int index)
093: throws SQLException {
094: Number id = ids.idOf(rs.getLong(index));
095: if (ids.isLiteral(id)) {
096: String label = rs.getString(index + 1);
097: String language = rs.getString(index + 2);
098: String datatype = rs.getString(index + 3);
099: return vf.getRdbmsLiteral(id, label, language, datatype);
100: }
101: return createResource(rs, index);
102: }
103:
104: }
|