/*
* (c) Copyright 2006 Hewlett-Packard Development Company, LP
* All rights reserved.
* [See end of file]
*/
package com.hp.hpl.squirrelrdf.rdb;
import java.sql.SQLException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.hp.hpl.jena.query.Query;
import com.hp.hpl.jena.query.engine.QueryIterator;
import com.hp.hpl.jena.query.engine1.ExecutionContext;
import com.hp.hpl.jena.query.engine1.Plan;
import com.hp.hpl.jena.query.engine1.PlanElement;
import com.hp.hpl.jena.query.engine1.PlanVisitor;
import com.hp.hpl.jena.query.engine1.PlanVisitorBase;
import com.hp.hpl.jena.query.engine1.PlanWalker;
import com.hp.hpl.jena.query.engine1.QueryEngine;
import com.hp.hpl.jena.query.engine1.QueryIterNullIterator;
import com.hp.hpl.jena.query.engine1.compiler.PlanBasicPattern;
import com.hp.hpl.jena.query.engine1.compiler.PlanElementBase;
import com.hp.hpl.jena.query.engine1.compiler.PlanGroup;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.squirrelrdf.querymap.exceptions.ConfigException;
import com.hp.hpl.squirrelrdf.querymap.exceptions.InconsistentException;
import com.hp.hpl.squirrelrdf.querymap.exceptions.UnanswerableException;
/**
* SQLQueryEngine
*
* An ARQ query engine, with minor modifications to hook in RDF2SQL.
*
* @author Damian Steer <pldms@mac.com>
*/
public class SQLQueryEngine extends QueryEngine
{
final static Log log = LogFactory.getLog(SQLQueryEngine.class);
private RDF2SQLConfig mapper;
/**
* @param Query A query
* @param configModel An RDF2SQL configuration
* @throws ConfigException When the configuration is malformed
*/
public SQLQueryEngine(Query arg0, Model configModel) throws ConfigException
{
super(arg0);
this.mapper = new RDF2SQLConfig(configModel);
}
/**
* Close this engine. Cleans up open database connections.
* @throws SQLException
*/
@Override
public void close()
{
super.close();
try
{
mapper.close();
}
catch (SQLException e)
{
log.error("Error closing: ", e);
}
}
/*
* walk through the plan, and replace the bits we want to handle.
*/
protected PlanElement queryPlanHook(Plan plan, PlanElement planElt)
{
PlanWalker.walk(new SQLPlanVisitor(), planElt);
return planElt;
}
/*
* This does the pottering through the plan
*/
class SQLPlanVisitor extends PlanVisitorBase
{
/*
* We start replacing at PlanGroups, since here we can
* remove and add bits
*/
@SuppressWarnings("unchecked")
public void visit(PlanGroup planElt)
{
Object[] elements = planElt.getPlanElements().toArray();
for (int i = 0; i < elements.length; i++)
{
// Aha, something to replace...
if (elements[i] instanceof PlanBasicPattern)
{
PlanBasicPattern pbp = (PlanBasicPattern) elements[i];
planElt.getPlanElements().remove(i);
// Warning supressed
planElt.getPlanElements().add(i, new SQLPattern(pbp.getPlan(), pbp));
}
}
}
}
/**
* This class replaces PlanBasicPatterns (although we keep the
* original around). RDF2SQL can answer these things.
*/
class SQLPattern extends PlanElementBase
{
PlanBasicPattern pattern;
protected SQLPattern(Plan p, PlanBasicPattern pattern)
{
super(p);
this.pattern = pattern;
}
@SuppressWarnings("unchecked")
public QueryIterator build(QueryIterator arg0, ExecutionContext arg1)
{
ProcessedQuery queryP;
try {
queryP = mapper.createQuery(pattern.getPattern());
return queryP.execute(arg0, arg1);
} catch (InconsistentException e) {
log.warn("Query failed due to inconsistency: " + e.getMessage());
} catch (UnanswerableException e) {
log.error("Query is not answerable: " + e.getMessage());
} catch (SQLException e) {
log.error("SQL problem: ", e);
}
return new QueryIterNullIterator(arg1);
}
public void visit(PlanVisitor arg0) { }
}
}
/*
* (c) Copyright 2006 Hewlett-Packard Development Company, LP
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/