1 5 6 package com.hp.hpl.jena.db.impl; 7 8 import java.sql.PreparedStatement ; 9 import java.sql.SQLException ; 10 import java.util.List ; 11 12 import com.hp.hpl.jena.graph.*; 13 import com.hp.hpl.jena.shared.*; 14 import com.hp.hpl.jena.graph.impl.LiteralLabel; 15 import com.hp.hpl.jena.util.iterator.ExtendedIterator; 16 import com.hp.hpl.jena.util.iterator.Map1; 17 import com.hp.hpl.jena.db.RDFRDBException; 18 import com.hp.hpl.jena.db.impl.SpecializedGraphReifier_RDB.StmtMask; 19 20 import com.hp.hpl.jena.vocabulary.RDF; 21 22 import org.apache.commons.logging.Log; 23 import org.apache.commons.logging.LogFactory; 24 25 45 46 public class PSet_ReifStore_RDB extends PSet_TripleStore_RDB { 47 48 51 protected static Log logger = LogFactory.getLog( PSet_ReifStore_RDB.class ); 52 53 56 59 public PSet_ReifStore_RDB() { 60 } 61 62 64 65 67 public void storeReifStmt(Node n, Triple t, IDBID my_GID) { 68 storeTripleAR(t, my_GID, n, true, false, null); 69 } 70 71 public void deleteReifStmt(Node n, Triple t, IDBID my_GID) { 72 deleteTripleAR(t, my_GID, n, false, null); 73 } 74 75 78 public ResultSetReifIterator findReifStmt( 79 Node stmtURI, 80 boolean hasType, 81 IDBID graphID, boolean getTriples) { 82 String astName = getTblName(); 83 String gid = graphID.getID().toString(); 84 ResultSetReifIterator result = new ResultSetReifIterator(this, getTriples, graphID); 85 86 PreparedStatement ps = null; 87 88 boolean objIsBlankOrURI = false; 89 int args = 1; 90 String stmtStr; 91 boolean findAll = (stmtURI == null) || stmtURI.equals(Node.ANY); 92 boolean notFound = false; 93 94 if ( findAll ) 95 stmtStr = hasType ? "selectReifiedT" : "selectReified"; 96 else 97 stmtStr = hasType ? "selectReifiedNT" : "selectReifiedN"; 98 try { 99 ps = m_sql.getPreparedSQLStatement(stmtStr, getTblName()); 100 101 if (!findAll) { 102 String stmt_uri = m_driver.nodeToRDBString(stmtURI, false); 103 if ( stmt_uri == null ) notFound = true; 104 else ps.setString(args++, stmt_uri); 105 } 106 if (hasType) 107 ps.setString(args++, "T"); 108 109 ps.setString(args++, gid); 110 111 } catch (Exception e) { 112 notFound = true; 113 logger.warn( "Getting prepared statement for " + stmtStr + " Caught exception ", e); 114 throw new JenaException("Exception during database access", e); } 116 117 if ( notFound ) 118 result.close(); 119 else { 120 try { 121 m_sql.executeSQL(ps, stmtStr, result); 122 } catch (Exception e) { 123 logger.debug( "find encountered exception ", e); 124 throw new JenaException("Exception during database access", e); } 126 } 127 return result; 128 } 129 130 public ResultSetReifIterator findReifTripleMatch( 131 TripleMatch t, 132 IDBID graphID) { 133 String astName = getTblName(); 134 String gid = graphID.getID().toString(); 135 ResultSetReifIterator result = new ResultSetReifIterator(this, true, graphID); 136 137 PreparedStatement ps = null; 138 139 String stmtStr = "*findReif "; 140 boolean gotStmt = false; 141 boolean gotPred = false; 142 boolean gotObj = false; 143 boolean objIsStmt = false; 144 char reifProp = ' '; 145 boolean done = false; 146 int argc = 1; 147 148 Node stmtURI = t.getMatchSubject(); 149 Node obj = t.getMatchObject(); 150 Node pred = t.getMatchPredicate(); 151 152 if ( (stmtURI != null) && !stmtURI.equals(Node.ANY) ) { 153 gotStmt = true; 154 stmtStr += "N"; 155 } 156 if ( (pred != null) && !pred.equals(Node.ANY) ) { 157 gotPred = true; 158 if ( pred.equals(RDF.Nodes.subject) ) reifProp = 'S'; 159 else if ( pred.equals(RDF.Nodes.predicate) ) reifProp = 'P'; 160 else if ( pred.equals(RDF.Nodes.object) ) reifProp = 'O'; 161 else if ( pred.equals(RDF.Nodes.type) ) reifProp = 'T'; 162 else done = true; 163 stmtStr += ("P" + reifProp); 164 } 165 if ( (obj != null) && !obj.equals(Node.ANY) ) { 166 gotObj = true; 167 stmtStr += "O"; 168 if ( obj.equals(RDF.Nodes.Statement) ) { 169 objIsStmt = true; 170 stmtStr += "C"; 171 } else if ( reifProp == 'T' ) 172 done = true; 174 } 175 176 if ( done == false ) try { 177 ps = m_sql.getPreparedSQLStatement(stmtStr, getTblName()); 178 ps.setString(argc++, gid); 179 if ( gotStmt ) { 180 String stmtNode = m_driver.nodeToRDBString(stmtURI, false); 181 if ( stmtNode == null ) done = true; 182 else ps.setString(argc++, stmtNode); 183 } 184 if ( gotObj ) { 185 if ( !(gotPred && objIsStmt) ) { 187 String objNode = m_driver.nodeToRDBString(obj, false); 188 ps.setString(argc++,objNode); 189 if ( gotPred == false ) { 190 ps.setString(argc++,objNode); 192 ps.setString(argc++,objNode); 193 } 194 } 195 } 196 197 } catch (Exception e) { 198 done = true; 199 logger.warn( "Getting prepared statement for " + stmtStr + " Caught exception ", e); 200 throw new JenaException("Exception during database access", e); } 202 203 if ( done ) 204 result.close(); 205 else { 206 try { 207 m_sql.executeSQL(ps, stmtStr, result); 208 } catch (Exception e) { 209 logger.debug( "find encountered exception ", e); 210 throw new JenaException("Exception during database access", e); } 212 } 213 return result; 214 } 215 216 217 218 223 224 public ExtendedIterator findReifStmtURIByTriple(Triple t, IDBID my_GID) { 225 String stmtStr = null; 226 int argc = 1; 227 PreparedStatement ps = null; 228 ResultSetIterator result = new ResultSetIterator(); 229 boolean notFound = false; 230 231 stmtStr = "selectReifNode"; 232 stmtStr += (t == null) ? "T" : "SPOT"; 233 234 try { 235 ps = m_sql.getPreparedSQLStatement(stmtStr, getTblName()); 236 ps.clearParameters(); 237 238 if (t != null) { 239 String argStr; 240 argStr = m_driver.nodeToRDBString(t.getSubject(),false); 241 if ( argStr == null ) notFound = true; 242 else ps.setString(argc++, argStr); 243 argStr = m_driver.nodeToRDBString(t.getPredicate(),false); 244 if ( argStr == null ) notFound = true; 245 else ps.setString(argc++, argStr); 246 argStr = m_driver.nodeToRDBString(t.getObject(),false); 247 if ( argStr == null ) notFound = true; 248 else ps.setString(argc++, argStr); 249 } 250 251 ps.setString(argc, my_GID.getID().toString()); 252 } catch (Exception e) { 253 notFound = true; 254 logger.warn( "Getting prepared statement for " + stmtStr + " Caught exception ", e); 255 throw new JenaException("Exception during database access", e); } 257 258 if ( notFound ) 260 result.close(); 261 else { 262 try { 263 m_sql.executeSQL(ps, stmtStr, result); 264 } catch (Exception e) { 265 logger.debug("find encountered exception ", e); 266 throw new JenaException("Exception during database access", e); } 268 } 269 return result.mapWith(new MapResultSetToNode()); 270 } 271 272 private class MapResultSetToNode implements Map1 { 273 274 277 public Object map1(Object o) { 278 List l = (List ) o; 280 if (l.get(0) instanceof String ) {} else 282 throw new JenaException( "String required: " + l.get(0).getClass() + " " + l.get(0) ); 283 Node n = m_driver.RDBStringToNode((String ) l.get(0)); 284 return n; 285 } 286 287 } 288 289 292 293 public ExtendedIterator findReifNodes(Node stmtURI, IDBID graphID) { 294 String astName = getTblName(); 295 String gid = graphID.getID().toString(); 296 ResultSetIterator result = new ResultSetIterator(); 297 int argc = 1; 298 PreparedStatement ps = null; 299 boolean notFound = false; 300 301 String stmtStr = 302 stmtURI == null ? "selectReifNode" : "selectReifNodeN"; 303 try { 304 ps = m_sql.getPreparedSQLStatement(stmtStr, getTblName()); 305 306 if (stmtURI != null) { 307 String stmt_uri = m_driver.nodeToRDBString(stmtURI,false); 308 if ( stmtURI == null ) notFound = true; 309 else ps.setString(argc++, stmt_uri); 310 } 311 312 ps.setString(argc, gid); 313 314 } catch (Exception e) { 315 notFound = true; 316 logger.warn( "Getting prepared statement for " + stmtStr + " Caught exception ", e); 317 throw new JenaException("Exception during database access", e); } 319 320 if ( notFound ) 321 result.close(); 322 else try { 323 result = m_sql.executeSQL(ps, stmtStr, result); 324 } catch (Exception e) { 325 logger.debug("find encountered exception ", e); 326 throw new JenaException("Exception during database access", e); } 328 return result; 329 } 330 331 public void storeFrag( 332 Node stmtURI, 333 Triple frag, 334 StmtMask fragMask, 335 IDBID my_GID) { 336 Node subj = fragMask.hasSubj() ? frag.getObject() : Node.NULL; 337 Node prop = fragMask.hasPred() ? frag.getObject() : Node.NULL; 338 Node obj = fragMask.hasObj() ? frag.getObject() : Node.NULL; 339 Triple t = Triple.create(subj, prop, obj); 340 storeTripleAR(t, my_GID, stmtURI, fragMask.hasType(), false, null); 341 } 342 343 public void updateOneFrag( 344 Node stmtURI, 345 Triple frag, 346 StmtMask fragMask, 347 boolean nullify, 348 IDBID my_GID) { 349 350 String stmtStr = null; 351 Node val = null; 352 int argc = 1; 353 String argStr; 354 355 if ( !fragMask.hasOneBit() ) 356 throw new JenaException("Reification can only update one column"); 357 PreparedStatement ps = null; 358 359 if ( fragMask.hasSubj() ) { 360 stmtStr = "updateReifiedS"; 361 if ( !nullify ) val = frag.getObject(); 362 } else if ( fragMask.hasPred() ) { 363 stmtStr = "updateReifiedP"; 364 if ( !nullify ) val = frag.getObject(); 365 } else if ( fragMask.hasObj() ) { 366 stmtStr = "updateReifiedO"; 367 if ( !nullify ) val = frag.getObject(); 368 } else if ( fragMask.hasType() ) { 369 stmtStr = "updateReifiedT"; 370 } 371 372 try { 373 ps = m_sql.getPreparedSQLStatement(stmtStr, getTblName()); 374 ps.clearParameters(); 375 if ( fragMask.hasSubj() || fragMask.hasPred() || fragMask.hasObj() ) { 376 if (nullify) 377 ps.setNull(argc++,java.sql.Types.VARCHAR); 378 else { 379 argStr = m_driver.nodeToRDBString(val,true); 380 if ( argStr == null ) 381 throw new RDFRDBException("Invalid update argument: " + val.toString()); 382 ps.setString(argc++,argStr); 383 } 384 } else { 385 if ( nullify ) 387 ps.setString(argc++," "); else 389 ps.setString(argc++,"T"); 390 } 391 argStr = m_driver.nodeToRDBString(stmtURI,true); 392 if ( argStr == null ) 393 throw new RDFRDBException("Invalid update statement URI: " + stmtURI.toString()); 394 ps.setString(argc++,argStr); 395 396 ps.setString(argc++,my_GID.getID().toString()); 397 } catch (Exception e) { 398 logger.warn( "Getting prepared statement for " + stmtStr + " Caught exception ", e); 399 throw new JenaException("Exception during database access", e); } 401 try { 402 ps.executeUpdate(); 403 } catch (SQLException e1) { 404 logger.error("SQLException caught during reification update" + e1.getErrorCode(), e1); 405 throw new JenaException("Exception during database access", e1); } 407 } 408 409 public void nullifyFrag(Node stmtURI, StmtMask fragMask, IDBID my_GID) { 410 updateOneFrag(stmtURI,null,fragMask,true,my_GID); 411 } 412 413 public void updateFrag( 414 Node stmtURI, 415 Triple frag, 416 StmtMask fragMask, 417 IDBID my_GID) { 418 updateOneFrag(stmtURI,frag,fragMask,false,my_GID); 419 } 420 421 public ResultSetReifIterator findFrag( 422 Node stmtURI, 423 Triple frag, 424 StmtMask fragMask, 425 IDBID my_GID) { 426 427 String stmtStr = null; 428 Node val = null; 429 int argc = 1; 430 ResultSetReifIterator result = 431 new ResultSetReifIterator(this, true, my_GID); 432 boolean notFound = false; 433 String argStr; 434 435 Node_Literal litNode = null; 436 LiteralLabel ll = null; 437 String lval = null; 438 boolean litIsPlain = false; 439 boolean objIsURI = false; 440 441 442 if ( !fragMask.hasOneBit() ) 443 throw new JenaException("Reification can only find one column"); 444 PreparedStatement ps = null; 445 446 val = frag.getObject(); 447 if ( fragMask.hasSubj() ) { 448 stmtStr = "selectReifiedNS"; 449 } else if ( fragMask.hasPred() ) { 450 stmtStr = "selectReifiedNP"; 451 } else if ( fragMask.hasObj() ) { 452 stmtStr = "selectReifiedNO"; 453 } else if ( fragMask.hasType() ) { 454 stmtStr = "selectReifiedNT"; 455 } 456 457 try { 458 ps = m_sql.getPreparedSQLStatement(stmtStr, getTblName()); 459 ps.clearParameters(); 460 argStr = m_driver.nodeToRDBString(stmtURI,false); 461 if ( argStr == null ) notFound = true; 462 else ps.setString(argc++,argStr); 463 if ( fragMask.hasSubj() || fragMask.hasPred() || fragMask.hasObj()) { 464 argStr = m_driver.nodeToRDBString(val,false); 465 if ( argStr == null ) notFound = true; 466 else ps.setString(argc++,argStr); 467 } else { 468 ps.setString(argc++,"T"); 470 } 471 ps.setString(argc,my_GID.getID().toString()); 472 473 } catch (Exception e) { 474 logger.warn( "Getting prepared statement for " + stmtStr + " Caught exception ", e); 475 } 476 477 if ( notFound ) 478 result.close(); 479 else 480 try { 481 m_sql.executeSQL(ps, stmtStr, result); 482 } catch (Exception e) { 483 logger.debug("find encountered exception ", e); 484 } 485 return result; 486 } 487 488 public void deleteFrag( 489 Triple frag, 490 StmtMask fragMask, 491 IDBID my_GID) { 492 493 if ( !fragMask.hasOneBit() ) 494 throw new JenaException("Can only delete one fragment"); 495 int argc = 1; 496 497 PreparedStatement ps = null; 498 String stmtStr = "deleteReified"; 499 if ( fragMask.hasSubj() ) 500 stmtStr += "S"; 501 else if ( fragMask.hasPred() ) 502 stmtStr += "P"; 503 else if ( fragMask.hasObj() ) 504 stmtStr += "O"; 505 else if ( fragMask.hasType() ) 506 stmtStr += "T"; 507 else 508 throw new JenaException("Unspecified reification fragment in deleteFrag"); 509 510 Node val = frag.getObject(); 511 String argStr = m_driver.nodeToRDBString(val,false); 512 Node stmtURI = frag.getSubject(); 513 String uriStr = m_driver.nodeToRDBString(stmtURI,false); 514 515 try { 516 ps = m_sql.getPreparedSQLStatement(stmtStr, getTblName()); 517 ps.clearParameters(); 518 519 if ( fragMask.hasSubj() ) 520 ps.setString(argc++,argStr); 521 else if ( fragMask.hasPred() ) 522 ps.setString(argc++,argStr); 523 else if ( fragMask.hasObj() ) 524 ps.setString(argc++,argStr); 525 ps.setString(argc++,my_GID.getID().toString()); 526 ps.setString(argc,uriStr); 527 528 } catch (Exception e) { 529 logger.warn( "Getting prepared statement for " + stmtStr + " Caught exception ", e); 530 } 531 try { 532 ps.executeUpdate(); 533 } catch (Exception e) { 534 logger.debug("deleteFrag encountered exception ", e); 535 } 536 return; 537 } 538 } 539 565 566 | Popular Tags |