Search in sources :

Example 1 with SQLQuery

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;
}
Also used : SQLException(java.sql.SQLException) Statement(java.sql.Statement) ResultSet(java.sql.ResultSet) AtomSetException(fr.lirmm.graphik.graal.api.core.AtomSetException) SQLQuery(fr.lirmm.graphik.graal.store.rdbms.util.SQLQuery)

Example 2 with SQLQuery

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);
}
Also used : DBTable(fr.lirmm.graphik.graal.store.rdbms.util.DBTable) SQLQuery(fr.lirmm.graphik.graal.store.rdbms.util.SQLQuery)

Example 3 with SQLQuery

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());
}
Also used : DBTable(fr.lirmm.graphik.graal.store.rdbms.util.DBTable) DBColumn(fr.lirmm.graphik.graal.store.rdbms.util.DBColumn) Term(fr.lirmm.graphik.graal.api.core.Term) SQLQuery(fr.lirmm.graphik.graal.store.rdbms.util.SQLQuery)

Example 4 with SQLQuery

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;
}
Also used : HomomorphismException(fr.lirmm.graphik.graal.api.homomorphism.HomomorphismException) RdbmsConjunctiveQueryTranslator(fr.lirmm.graphik.graal.store.rdbms.RdbmsConjunctiveQueryTranslator) SQLQuery(fr.lirmm.graphik.graal.store.rdbms.util.SQLQuery) ConjunctiveQuery(fr.lirmm.graphik.graal.api.core.ConjunctiveQuery) HomomorphismException(fr.lirmm.graphik.graal.api.homomorphism.HomomorphismException)

Example 5 with SQLQuery

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;
}
Also used : RuleApplicationException(fr.lirmm.graphik.graal.api.forward_chaining.RuleApplicationException) SQLException(java.sql.SQLException) Statement(java.sql.Statement) AtomSetException(fr.lirmm.graphik.graal.api.core.AtomSetException) SQLQuery(fr.lirmm.graphik.graal.store.rdbms.util.SQLQuery)

Aggregations

SQLQuery (fr.lirmm.graphik.graal.store.rdbms.util.SQLQuery)12 DBTable (fr.lirmm.graphik.graal.store.rdbms.util.DBTable)9 Term (fr.lirmm.graphik.graal.api.core.Term)7 AtomSetException (fr.lirmm.graphik.graal.api.core.AtomSetException)6 DBColumn (fr.lirmm.graphik.graal.store.rdbms.util.DBColumn)5 SQLException (java.sql.SQLException)4 Atom (fr.lirmm.graphik.graal.api.core.Atom)3 IteratorException (fr.lirmm.graphik.util.stream.IteratorException)3 AtomSet (fr.lirmm.graphik.graal.api.core.AtomSet)2 ConjunctiveQuery (fr.lirmm.graphik.graal.api.core.ConjunctiveQuery)2 Statement (java.sql.Statement)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 TreeMap (java.util.TreeMap)2 InMemoryAtomSet (fr.lirmm.graphik.graal.api.core.InMemoryAtomSet)1 RuleApplicationException (fr.lirmm.graphik.graal.api.forward_chaining.RuleApplicationException)1 HomomorphismException (fr.lirmm.graphik.graal.api.homomorphism.HomomorphismException)1 RdbmsConjunctiveQueryTranslator (fr.lirmm.graphik.graal.store.rdbms.RdbmsConjunctiveQueryTranslator)1 ResultSet (java.sql.ResultSet)1