use of fr.lirmm.graphik.graal.store.rdbms.util.SQLQuery in project graal by graphik-team.
the class AbstractRdbmsStore method contains.
@Override
public boolean contains(Atom atom) throws AtomSetException {
if (!this.check(atom)) {
return false;
}
Statement statement = this.createStatement();
SQLQuery query = this.getConjunctiveQueryTranslator().translateContainsQuery(atom);
boolean res = false;
if (!query.hasSchemaError()) {
ResultSet results;
try {
statement = this.createStatement();
results = statement.executeQuery(query.toString());
if (results.next()) {
res = true;
}
results.close();
} catch (SQLException e) {
if (statement != null) {
try {
statement.close();
} catch (SQLException sqlEx) {
}
}
throw new AtomSetException("Error during check contains atom: " + atom, e);
}
}
return res;
}
use of fr.lirmm.graphik.graal.store.rdbms.util.SQLQuery in project graal by graphik-team.
the class AdHocConjunctiveQueryTranslator method translateTermsByPredicatePositionQuery.
@Override
public SQLQuery translateTermsByPredicatePositionQuery(Predicate p, int position) throws AtomSetException {
DBTable table = this.store.getPredicateTable(p);
if (table == null) {
return SQLQuery.hasSchemaErrorInstance();
}
String query = String.format(TERMS_BY_PREDICATE_POSITION_FORMAT, position, table.getName(), position);
return new SQLQuery(query);
}
use of fr.lirmm.graphik.graal.store.rdbms.util.SQLQuery in project graal by graphik-team.
the class AbstractRdbmsConjunctiveQueryTranslator method translateRemove.
// /////////////////////////////////////////////////////////////////////////
// PUBLIC METHODS
// /////////////////////////////////////////////////////////////////////////
@Override
public SQLQuery translateRemove(Atom atom) throws AtomSetException {
DBTable table = this.store.getPredicateTableIfExist(atom.getPredicate());
if (table == null)
return SQLQuery.hasSchemaErrorInstance();
StringBuilder query = new StringBuilder("DELETE FROM ");
query.append(table);
query.append(" WHERE ");
List<DBColumn> columns = table.getColumns();
int termIndex = 0;
for (Term t : atom.getTerms()) {
if (termIndex != 0) {
query.append(" and ");
}
query.append(columns.get(termIndex).getName()).append(" = '").append(t.getLabel()).append("'");
++termIndex;
}
return new SQLQuery(query.toString());
}
use of fr.lirmm.graphik.graal.store.rdbms.util.SQLQuery in project graal by graphik-team.
the class SqlUCQHomomorphism method preprocessing.
private static SQLQuery preprocessing(UnionOfConjunctiveQueries queries, RdbmsStore store) throws HomomorphismException {
boolean emptyQuery = false;
CloseableIterator<ConjunctiveQuery> it = queries.iterator();
StringBuilder ucq = new StringBuilder();
RdbmsConjunctiveQueryTranslator translator = store.getConjunctiveQueryTranslator();
try {
if (!it.hasNext())
return SQLQuery.hasSchemaErrorInstance();
while (it.hasNext()) {
SQLQuery query = translator.translate(it.next());
if (!query.hasSchemaError()) {
if (ucq.length() > 0)
ucq.append("\nUNION\n");
ucq.append(query.toString());
} else if (query.isEmpty()) {
emptyQuery = true;
}
}
} catch (Exception e) {
throw new HomomorphismException("Error during query translation to SQL", e);
}
SQLQuery query = new SQLQuery(ucq.toString());
if (query.isEmpty() && !emptyQuery) {
return SQLQuery.hasSchemaErrorInstance();
}
return query;
}
use of fr.lirmm.graphik.graal.store.rdbms.util.SQLQuery in project graal by graphik-team.
the class SQLRuleApplier method apply.
// //////////////////////////////////////////////////////////////////////////
// METHODS
// //////////////////////////////////////////////////////////////////////////
@Override
public boolean apply(Rule rule, RdbmsStore store) throws RuleApplicationException {
boolean returnValue = false;
if (rule.getExistentials().isEmpty()) {
Statement statement = null;
try {
statement = store.getDriver().createStatement();
Iterator<SQLQuery> sqlQueries = store.getConjunctiveQueryTranslator().translate(rule);
while (sqlQueries.hasNext()) {
SQLQuery query = sqlQueries.next();
if (!query.hasSchemaError())
statement.addBatch(query.toString());
}
int[] res = statement.executeBatch();
for (int i = 0; i < res.length; ++i) {
if (res[i] > 0) {
returnValue = true;
break;
}
}
} catch (AtomSetException e) {
throw new RuleApplicationException("An error has been occured during rule application.", e);
} catch (SQLException e) {
throw new RuleApplicationException("An error has been occured during rule application.", e);
} finally {
if (statement != null) {
try {
statement.getConnection().commit();
statement.close();
} catch (SQLException e) {
}
}
}
} else {
returnValue = this.fallback.apply(rule, store);
}
return returnValue;
}
Aggregations