001: /*
002: * (c) Copyright 2002, 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
003: * All rights reserved.
004: *
005: *
006: */
007:
008: //=======================================================================
009: // Package
010: package com.hp.hpl.jena.db.impl;
011:
012: //=======================================================================
013: // Imports
014: import java.sql.*;
015:
016: import com.hp.hpl.jena.db.RDFRDBException;
017: import com.hp.hpl.jena.graph.*;
018:
019: import org.apache.commons.logging.Log;
020: import org.apache.commons.logging.LogFactory;
021:
022: //=======================================================================
023: /**
024: * Version of ResultSetIterator that extracts database rows as Triples.
025: *
026: * @author hkuno. Based on ResultSetResource Iterator, by Dave Reynolds, HPLabs, Bristol <a href="mailto:der@hplb.hpl.hp.com">Dave Reynolds</a>
027: * @version $Revision: 1.12 $ on $Date: 2008/01/02 12:08:24 $
028: */
029: public class ResultSetTripleIterator extends ResultSetIterator {
030:
031: /** The rdf model in which to instantiate any resources */
032: protected IDBID m_graphID;
033:
034: /** The database driver, used to access namespace and resource caches */
035: protected IPSet m_pset;
036:
037: /** Holds the current row as a triple */
038: protected Triple m_triple;
039:
040: /** True if iterating over reified statements */
041: protected boolean m_isReif;
042:
043: /** Statement URI if iterating over reified statements */
044: protected Node m_stmtURI;
045:
046: /** HasType flag if iterating over reified statements */
047: protected boolean m_hasType;
048:
049: static protected Log logger = LogFactory
050: .getLog(ResultSetTripleIterator.class);
051:
052: // Constructor
053: public ResultSetTripleIterator(IPSet p, IDBID graphID) {
054: m_pset = p;
055: setGraphID(graphID);
056: m_isReif = false;
057: }
058:
059: // Constructor for iterating over reified statements
060: public ResultSetTripleIterator(IPSet p, boolean isReif,
061: IDBID graphID) {
062: m_pset = p;
063: setGraphID(graphID);
064: m_isReif = isReif;
065: }
066:
067: /**
068: * Set m_graphID.
069: * @param gid is the id of the graph associated with this iterator.
070: */
071: public void setGraphID(IDBID gid) {
072: m_graphID = gid;
073: }
074:
075: /**
076: * Reset an existing iterator to scan a new result set.
077: * @param resultSet the result set being iterated over
078: * @param sourceStatement The source Statement to be cleaned up when the iterator finishes - return it to cache or close it if no cache
079: * @param cache The originating SQLcache to return the statement to, can be null
080: * @param opname The name of the original operation that lead to this statement, can be null if SQLCache is null
081: */
082: public void reset(ResultSet resultSet,
083: PreparedStatement sourceStatement, SQLCache cache,
084: String opname) {
085: super .reset(resultSet, sourceStatement, cache, opname);
086: m_triple = null;
087: }
088:
089: /**
090: * Extract the current row into a triple.
091: * Requires the row to be of the form:
092: * subject URI (String)
093: * predicate URI (String)
094: * object URI (String)
095: * object value (String)
096: * Object literal id (Object)
097: *
098: * The object of the triple can be either a URI, a simple literal (in
099: * which case it will just have an object value, or a complex literal
100: * (in which case both the object value and the object literal id
101: * columns may be populated.
102: */
103: protected void extractRow() throws SQLException {
104: int rx = 1;
105: ResultSet rs = m_resultSet;
106: String subj = rs.getString(1);
107: String pred = rs.getString(2);
108: String obj = rs.getString(3);
109:
110: if (m_isReif) {
111: m_stmtURI = m_pset.driver()
112: .RDBStringToNode(rs.getString(4));
113: m_hasType = rs.getString(5).equals("T");
114: }
115:
116: Triple t = null;
117:
118: try {
119: t = m_pset.extractTripleFromRowData(subj, pred, obj);
120: } catch (RDFRDBException e) {
121: logger
122: .debug(
123: "Extracting triple from row encountered exception: ",
124: e);
125: }
126:
127: m_triple = t;
128:
129: }
130:
131: /**
132: * Return the current row, which should have already been extracted.
133: */
134: protected Object getRow() {
135: return m_triple;
136: }
137:
138: /**
139: * Return the current row, which should have already been extracted.
140: */
141: protected Node getStmtURI() {
142: return m_stmtURI;
143: }
144:
145: /**
146: * Return the current row, which should have already been extracted.
147: */
148: protected boolean getHasType() {
149: return m_hasType;
150: }
151:
152: /**
153: * Delete the current row, which should have already been extracted.
154: * Should only be used (carefully and) internally by db layer.
155: */
156: protected void deleteRow() {
157: try {
158: m_resultSet.deleteRow();
159: } catch (SQLException e) {
160: throw new RDFRDBException("Internal sql error", e);
161: }
162: }
163:
164: /**
165: * Remove the current triple from the data store.
166: */
167: public void remove() {
168: if (m_triple == null)
169: throw new IllegalStateException();
170: m_pset.deleteTriple(m_triple, m_graphID);
171: }
172:
173: } // End class
174:
175: /*
176: * (c) Copyright 2002, 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
177: * All rights reserved.
178: *
179: * Redistribution and use in source and binary forms, with or without
180: * modification, are permitted provided that the following conditions
181: * are met:
182: * 1. Redistributions of source code must retain the above copyright
183: * notice, this list of conditions and the following disclaimer.
184: * 2. Redistributions in binary form must reproduce the above copyright
185: * notice, this list of conditions and the following disclaimer in the
186: * documentation and/or other materials provided with the distribution.
187: * 3. The name of the author may not be used to endorse or promote products
188: * derived from this software without specific prior written permission.
189:
190: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
191: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
192: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
193: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
194: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
195: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
196: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
197: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
198: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
199: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
200: */
|