1 7 8 package com.hp.hpl.jena.db.impl; 11 12 import java.sql.*; 15 16 import com.hp.hpl.jena.db.RDFRDBException; 17 import com.hp.hpl.jena.graph.*; 18 import com.hp.hpl.jena.shared.JenaException; 19 import com.hp.hpl.jena.vocabulary.RDF; 20 21 import org.apache.commons.logging.Log; 22 import org.apache.commons.logging.LogFactory; 23 24 31 public class ResultSetReifIterator extends ResultSetIterator { 32 33 34 protected IDBID m_graphID; 35 36 37 protected IPSet m_pset; 38 39 40 protected Node m_subjNode; 41 protected Node m_predNode; 42 protected Node m_objNode; 43 44 45 protected Node m_stmtURI; 46 47 48 protected boolean m_hasType; 49 50 52 protected boolean m_getTriples; 53 54 55 protected int m_fragCount; 56 57 58 protected int m_fragRem; 59 60 61 protected int m_nextFrag; 62 63 64 static protected Log logger = LogFactory.getLog( ResultSetReifIterator.class ); 65 66 public ResultSetReifIterator(IPSet p, boolean getTriples, IDBID graphID) { 68 m_pset = p; 69 setGraphID(graphID); 70 m_getTriples = getTriples; 71 } 72 73 77 public void setGraphID(IDBID gid) { 78 m_graphID = gid; 79 } 80 81 88 public void reset(ResultSet resultSet, PreparedStatement sourceStatement, SQLCache cache, String opname) { 89 super.reset(resultSet, sourceStatement, cache, opname); 90 } 91 92 106 protected void extractRow() throws SQLException { 107 int rx = 1; 108 ResultSet rs = m_resultSet; 109 String subj = rs.getString(1); 110 String pred = rs.getString(2); 111 String obj = rs.getString(3); 112 113 m_stmtURI = m_pset.driver().RDBStringToNode(rs.getString(4)); 114 m_hasType = rs.getString(5).equals("T"); 115 116 m_fragRem = m_hasType ? 1 : 0; 117 if ( subj == null ) { 118 m_subjNode = Node.NULL; 119 } else { 120 m_subjNode = m_pset.driver().RDBStringToNode(subj); 121 m_fragRem++; 122 } 123 if ( pred == null ) { 124 m_predNode = Node.NULL; 125 } else { 126 m_predNode = m_pset.driver().RDBStringToNode(pred); 127 m_fragRem++; 128 } 129 if ( obj == null ) { 130 m_objNode = Node.NULL; 131 } else { 132 m_objNode = m_pset.driver().RDBStringToNode(obj); 133 m_fragRem++; 134 } 135 m_nextFrag = 0; 136 m_fragCount = m_fragRem; 137 } 138 139 142 protected Object getRow() { 143 Triple t = null; 144 145 if ( m_getTriples == true ) { 146 if ( m_nextFrag == 0) { 147 if ( !m_subjNode.equals(Node.NULL) ) { 148 t = Triple.create(m_stmtURI,RDF.Nodes.subject,m_subjNode); 149 m_fragRem--; 150 } else 151 m_nextFrag++; 152 } 153 if ( m_nextFrag == 1) { 154 if ( !m_predNode.equals(Node.NULL) ) { 155 t = Triple.create(m_stmtURI,RDF.Nodes.predicate,m_predNode); 156 m_fragRem--; 157 } else 158 m_nextFrag++; 159 } 160 if ( m_nextFrag == 2) { 161 if ( !m_objNode.equals(Node.NULL) ) { 162 t = Triple.create(m_stmtURI,RDF.Nodes.object,m_objNode); 163 m_fragRem--; 164 } else 165 m_nextFrag++; 166 } 167 if ( m_nextFrag >= 3) { 168 if ( m_hasType ) { 169 t = Triple.create(m_stmtURI,RDF.Nodes.type,RDF.Nodes.Statement); 170 m_fragRem--; 171 } else 172 throw new JenaException("Reified triple not found"); 173 } 174 m_nextFrag++; 175 if ( m_fragRem > 0 ) 176 m_prefetched = true; 177 178 } else { 179 t = Triple.create(m_subjNode, m_predNode, m_objNode); 180 } 181 182 return t; 183 } 184 185 188 protected boolean hasSubj() { 189 return m_subjNode != Node.NULL; 190 } 191 192 195 protected boolean hasPred() { 196 return m_predNode != Node.NULL; 197 } 198 199 202 protected boolean hasObj() { 203 return m_objNode != Node.NULL; 204 } 205 206 209 protected boolean hasType() { 210 return m_hasType; 211 } 212 213 216 protected int getFragCount() { 217 return m_fragCount; 218 } 219 220 223 protected Node getStmtURI() { 224 return m_stmtURI; 225 } 226 227 230 protected boolean getHasType() { 231 return m_hasType; 232 } 233 234 238 protected void deleteRow() { 239 try { 240 m_resultSet.deleteRow(); 241 } catch (SQLException e) { 242 throw new RDFRDBException("Internal sql error", e); 243 } 244 } 245 246 247 } 249 275 276 | Popular Tags |