📄 pset_reifstore_rdb.java
字号:
/*
(c) Copyright 2003, 2004, 2005, 2006, 2007 Hewlett-Packard Development Company, LP
[See end of file]
*/
package com.hp.hpl.jena.db.impl;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;
import com.hp.hpl.jena.graph.*;
import com.hp.hpl.jena.shared.*;
import com.hp.hpl.jena.graph.impl.LiteralLabel;
import com.hp.hpl.jena.util.iterator.ExtendedIterator;
import com.hp.hpl.jena.util.iterator.Map1;
import com.hp.hpl.jena.db.RDFRDBException;
import com.hp.hpl.jena.vocabulary.RDF;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
//=======================================================================
/**
* Handles Physical storage for implementing SpecializedGraphs.
* Different PSet classes are needed for different databases and different
* layout schemes.
* <p>
* This class is a base implemention from which database-specific
* drivers can inherit. It is not generic in the sense that it will work
* on any minimal SQL store and so should be treated as if it were
* an abstract class.
* <p>The SQL statements which implement each of the functions are
* loaded in a separate file etc/[layout]_[database].sql from the classpath.
* See {@link SQLCache SQLCache documentation} for more information on the
* format of this file.
*
* Based on Driver* classes by Dave Reynolds.
*
* @author <a href="mailto:harumi.kuno@hp.com">Harumi Kuno</a>
* @version $Revision: 1.28 $ on $Date: 2007/01/02 11:50:44 $
*/
public class PSet_ReifStore_RDB extends PSet_TripleStore_RDB {
//=======================================================================
// Internal variables
protected static Log logger = LogFactory.getLog( PSet_ReifStore_RDB.class );
//=======================================================================
// Constructors and accessors
/**
* Constructor.
*/
public PSet_ReifStore_RDB() {
}
//=======================================================================
// Database operations
public void storeReifStmt(Node n, Triple t, IDBID my_GID) {
storeTripleAR(t, my_GID, n, true, false, null);
}
public void deleteReifStmt(Node n, Triple t, IDBID my_GID) {
deleteTripleAR(t, my_GID, n, false, null);
}
/* (non-Javadoc)
* @see com.hp.hpl.jena.db.impl.IPSet#find(com.hp.hpl.jena.graph.TripleMatch, com.hp.hpl.jena.db.impl.IDBID)
*/
public ResultSetReifIterator findReifStmt(
Node stmtURI,
boolean hasType,
IDBID graphID, boolean getTriples) {
String astName = getTblName();
String gid = graphID.getID().toString();
ResultSetReifIterator result = new ResultSetReifIterator(this, getTriples, graphID);
PreparedStatement ps = null;
boolean objIsBlankOrURI = false;
int args = 1;
String stmtStr;
boolean findAll = (stmtURI == null) || stmtURI.equals(Node.ANY);
boolean notFound = false;
if ( findAll )
stmtStr = hasType ? "selectReifiedT" : "selectReified";
else
stmtStr = hasType ? "selectReifiedNT" : "selectReifiedN";
try {
ps = m_sql.getPreparedSQLStatement(stmtStr, getTblName());
if (!findAll) {
String stmt_uri = m_driver.nodeToRDBString(stmtURI, false);
if ( stmt_uri == null ) notFound = true;
else ps.setString(args++, stmt_uri);
}
if (hasType)
ps.setString(args++, "T");
ps.setString(args++, gid);
} catch (Exception e) {
notFound = true;
logger.warn( "Getting prepared statement for " + stmtStr + " Caught exception ", e);
throw new JenaException("Exception during database access", e); // Rethrow in case there is a recovery option
}
if ( notFound )
result.close();
else {
try {
m_sql.executeSQL(ps, stmtStr, result);
} catch (Exception e) {
logger.debug( "find encountered exception ", e);
throw new JenaException("Exception during database access", e); // Rethrow in case there is a recovery option
}
}
return result;
}
public ResultSetReifIterator findReifTripleMatch(
TripleMatch t,
IDBID graphID) {
String astName = getTblName();
String gid = graphID.getID().toString();
ResultSetReifIterator result = new ResultSetReifIterator(this, true, graphID);
PreparedStatement ps = null;
String stmtStr = "*findReif ";
boolean gotStmt = false;
boolean gotPred = false;
boolean gotObj = false;
boolean objIsStmt = false;
char reifProp = ' ';
String objNode = null;
boolean done = false;
int argc = 1;
Node stmtURI = t.getMatchSubject();
Node obj = t.getMatchObject();
Node pred = t.getMatchPredicate();
if ( (stmtURI != null) && !stmtURI.equals(Node.ANY) ) {
gotStmt = true;
stmtStr += "N";
}
if ( (pred != null) && !pred.equals(Node.ANY) ) {
gotPred = true;
if ( pred.equals(RDF.Nodes.subject) ) reifProp = 'S';
else if ( pred.equals(RDF.Nodes.predicate) ) reifProp = 'P';
else if ( pred.equals(RDF.Nodes.object) ) reifProp = 'O';
else if ( pred.equals(RDF.Nodes.type) ) reifProp = 'T';
else done = true;
stmtStr += ("P" + reifProp);
}
if ( (obj != null) && !obj.equals(Node.ANY) ) {
gotObj = true;
stmtStr += "O";
if ( obj.equals(RDF.Nodes.Statement) ) {
objIsStmt = true;
stmtStr += "C";
} else if ( reifProp == 'T' )
// reifier only stores patterns like (-, rdf:type, rdf:Statement)
done = true;
}
if ( done == false ) try {
ps = m_sql.getPreparedSQLStatement(stmtStr, getTblName());
ps.setString(argc++, gid);
if ( gotStmt ) {
String stmtNode = m_driver.nodeToRDBString(stmtURI, false);
if ( stmtNode == null ) done = true;
else ps.setString(argc++, stmtNode);
}
if ( gotObj ) {
// no arguments in case match is <-,rdf:type,rdf:Statement>
if ( !(gotPred && objIsStmt) ) {
objNode = m_driver.nodeToRDBString(obj, false);
ps.setString(argc++,objNode);
if ( gotPred == false ) {
// if no predicate, object value could be in subj, pred or obj column
ps.setString(argc++,objNode);
ps.setString(argc++,objNode);
}
}
}
} catch (Exception e) {
done = true;
logger.warn( "Getting prepared statement for " + stmtStr + " Caught exception ", e);
throw new JenaException("Exception during database access", e); // Rethrow in case there is a recovery option
}
if ( done ) {
result.close();
} else {
if ( gotPred ) {
result.close();
result = new ResultSetReifIterator(this, reifProp, graphID);
} else if ( gotObj ) {
result.close();
result = new ResultSetReifIterator(this, obj, graphID);
}
try {
m_sql.executeSQL(ps, stmtStr, result);
} catch (Exception e) {
logger.debug( "find encountered exception ", e);
throw new JenaException("Exception during database access", e); // Rethrow in case there is a recovery option
}
}
return result;
}
/*
* (non-Javadoc)
* return all nodes which reify the triple as a statement. no
* need to do distinct here since we only return nodes for reified statments.
*/
public ExtendedIterator findReifStmtURIByTriple(Triple t, IDBID my_GID) {
String stmtStr = null;
int argc = 1;
PreparedStatement ps = null;
ResultSetIterator result = new ResultSetNodeIterator();
boolean notFound = false;
stmtStr = "selectReifNode";
stmtStr += (t == null) ? "T" : "SPOT";
try {
ps = m_sql.getPreparedSQLStatement(stmtStr, getTblName());
ps.clearParameters();
if (t != null) {
String argStr;
argStr = m_driver.nodeToRDBString(t.getSubject(),false);
if ( argStr == null ) notFound = true;
else ps.setString(argc++, argStr);
argStr = m_driver.nodeToRDBString(t.getPredicate(),false);
if ( argStr == null ) notFound = true;
else ps.setString(argc++, argStr);
argStr = m_driver.nodeToRDBString(t.getObject(),false);
if ( argStr == null ) notFound = true;
else ps.setString(argc++, argStr);
}
ps.setString(argc, my_GID.getID().toString());
} catch (Exception e) {
notFound = true;
logger.warn( "Getting prepared statement for " + stmtStr + " Caught exception ", e);
throw new JenaException("Exception during database access", e); // Rethrow in case there is a recovery option
}
// find on object field
if ( notFound )
result.close();
else {
try {
m_sql.executeSQL(ps, stmtStr, result);
} catch (Exception e) {
logger.debug("find encountered exception ", e);
throw new JenaException("Exception during database access", e); // Rethrow in case there is a recovery option
}
}
return result.mapWith(new MapResultSetToNode());
}
private class MapResultSetToNode implements Map1 {
/* (non-Javadoc)
* @see com.hp.hpl.jena.util.iterator.Map1#map1(java.lang.Object)
*/
public Object map1(Object o) {
// TODO Auto-generated method stub
List l = (List) o;
// TODO we have a BUG here somewhere, hence the message.
String s = null;
Object n = l.get(0);
if ( (n instanceof String) || (n instanceof Byte) )
s = (String) n;
else
throw new JenaException( "String required: " + l.get(0).getClass() + " " + l.get(0) );
Node r = m_driver.RDBStringToNode(s);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -