KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > hp > hpl > jena > db > impl > ResultSetTripleIterator


1 /*
2  * (c) Copyright 2002, 2003, 2004, 2005 Hewlett-Packard Development Company, LP
3  * All rights reserved.
4  *
5  *
6  */

7
8 //=======================================================================
9
// Package
10
package com.hp.hpl.jena.db.impl;
11
12 //=======================================================================
13
// Imports
14
import java.sql.*;
15
16 import com.hp.hpl.jena.db.RDFRDBException;
17 import com.hp.hpl.jena.graph.*;
18
19 import org.apache.commons.logging.Log;
20 import org.apache.commons.logging.LogFactory;
21
22 //=======================================================================
23
/**
24 * Version of ResultSetIterator that extracts database rows as Triples.
25 *
26 * @author hkuno. Based on ResultSetResource Iterator, by Dave Reynolds, HPLabs, Bristol <a HREF="mailto:der@hplb.hpl.hp.com">Dave Reynolds</a>
27 * @version $Revision: 1.9 $ on $Date: 2005/02/21 12:03:11 $
28 */

29 public class ResultSetTripleIterator extends ResultSetIterator {
30
31     /** The rdf model in which to instantiate any resources */
32     protected IDBID m_graphID;
33
34     /** The database driver, used to access namespace and resource caches */
35     protected IPSet m_pset;
36     
37     /** Holds the current row as a triple */
38     protected Triple m_triple;
39     
40     /** True if iterating over reified statements */
41     protected boolean m_isReif;
42     
43     /** Statement URI if iterating over reified statements */
44     protected Node m_stmtURI;
45     
46     /** HasType flag if iterating over reified statements */
47     protected boolean m_hasType;
48
49     static protected Log logger = LogFactory.getLog( ResultSetTripleIterator.class );
50     
51     // Constructor
52
public ResultSetTripleIterator(IPSet p, IDBID graphID) {
53         m_pset = p;
54         setGraphID(graphID);
55         m_isReif = false;
56     }
57
58     // Constructor for iterating over reified statements
59
public ResultSetTripleIterator(IPSet p, boolean isReif, IDBID graphID) {
60         m_pset = p;
61         setGraphID(graphID);
62         m_isReif = isReif;
63     }
64     
65     /**
66      * Set m_graphID.
67      * @param gid is the id of the graph associated with this iterator.
68      */

69     public void setGraphID(IDBID gid) {
70         m_graphID = gid;
71     }
72     
73     /**
74      * Reset an existing iterator to scan a new result set.
75      * @param resultSet the result set being iterated over
76      * @param sourceStatement The source Statement to be cleaned up when the iterator finishes - return it to cache or close it if no cache
77      * @param cache The originating SQLcache to return the statement to, can be null
78      * @param opname The name of the original operation that lead to this statement, can be null if SQLCache is null
79      */

80     public void reset(ResultSet resultSet, PreparedStatement sourceStatement, SQLCache cache, String JavaDoc opname) {
81         super.reset(resultSet, sourceStatement, cache, opname);
82         m_triple = null;
83     }
84
85     /**
86      * Extract the current row into a triple.
87      * Requires the row to be of the form:
88      * subject URI (String)
89      * predicate URI (String)
90      * object URI (String)
91      * object value (String)
92      * Object literal id (Object)
93      *
94      * The object of the triple can be either a URI, a simple literal (in
95      * which case it will just have an object value, or a complex literal
96      * (in which case both the object value and the object literal id
97      * columns may be populated.
98      */

99     protected void extractRow() throws SQLException {
100         int rx = 1;
101         ResultSet rs = m_resultSet;
102         String JavaDoc subj = rs.getString(1);
103         String JavaDoc pred = rs.getString(2);
104         String JavaDoc obj = rs.getString(3);
105
106         if ( m_isReif ) {
107             m_stmtURI = m_pset.driver().RDBStringToNode(rs.getString(4));
108             m_hasType = rs.getString(5).equals("T");
109         }
110         
111         Triple t = null;
112         
113         try {
114         t = m_pset.extractTripleFromRowData(subj, pred, obj);
115         } catch (RDFRDBException e) {
116             logger.debug("Extracting triple from row encountered exception: ", e);
117         }
118         
119         m_triple = t;
120         
121     }
122     
123         /**
124          * Return the current row, which should have already been extracted.
125          */

126         protected Object JavaDoc getRow() {
127             return m_triple;
128         }
129         
130         /**
131         * Return the current row, which should have already been extracted.
132         */

133         protected Node getStmtURI() {
134             return m_stmtURI;
135         }
136         
137         /**
138         * Return the current row, which should have already been extracted.
139         */

140         protected boolean getHasType() {
141             return m_hasType;
142         }
143         
144         /**
145         * Delete the current row, which should have already been extracted.
146         * Should only be used (carefully and) internally by db layer.
147         */

148         protected void deleteRow() {
149             try {
150                 m_resultSet.deleteRow();
151             } catch (SQLException e) {
152                 throw new RDFRDBException("Internal sql error", e);
153             }
154         }
155         
156     
157         /**
158          * Remove the current triple from the data store.
159          */

160         public void remove() {
161             if (m_triple == null)
162                   throw new IllegalStateException JavaDoc();
163             m_pset.deleteTriple(m_triple, m_graphID);
164         }
165
166 } // End class
167

168 /*
169  * (c) Copyright 2002, 2003, 2004, 2005 Hewlett-Packard Development Company, LP
170  * All rights reserved.
171  *
172  * Redistribution and use in source and binary forms, with or without
173  * modification, are permitted provided that the following conditions
174  * are met:
175  * 1. Redistributions of source code must retain the above copyright
176  * notice, this list of conditions and the following disclaimer.
177  * 2. Redistributions in binary form must reproduce the above copyright
178  * notice, this list of conditions and the following disclaimer in the
179  * documentation and/or other materials provided with the distribution.
180  * 3. The name of the author may not be used to endorse or promote products
181  * derived from this software without specific prior written permission.
182
183  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
184  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
185  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
186  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
187  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
188  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
189  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
190  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
191  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
192  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
193  */

194
195
Popular Tags