Search in sources :

Example 6 with DBTable

use of fr.lirmm.graphik.graal.store.rdbms.util.DBTable in project graal by graphik-team.

the class AdHocRdbmsStore method getPredicateTableIfExist.

@Override
protected DBTable getPredicateTableIfExist(Predicate predicate) throws AtomSetException {
    DBTable table = null;
    try {
        this.getPredicateTableStatement.setString(1, predicate.getIdentifier().toString());
        this.getPredicateTableStatement.setInt(2, predicate.getArity());
        ResultSet results = this.getPredicateTableStatement.executeQuery();
        if (results.next()) {
            String predicateTableName = results.getString("predicate_table_name");
            table = this.getDriver().getTable(predicateTableName);
        }
        results.close();
    } catch (SQLException e) {
        throw new AtomSetException(e);
    }
    return table;
}
Also used : DBTable(fr.lirmm.graphik.graal.store.rdbms.util.DBTable) SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) AtomSetException(fr.lirmm.graphik.graal.api.core.AtomSetException)

Example 7 with DBTable

use of fr.lirmm.graphik.graal.store.rdbms.util.DBTable in project graal by graphik-team.

the class AdHocRdbmsStore method add.

// /////////////////////////////////////////////////////////////////////////
// PROTECTED METHODS
// /////////////////////////////////////////////////////////////////////////
/**
 * @param statement
 * @param atom
 * @throws AtomSetException
 */
@Override
protected Statement add(Statement statement, Atom atom) throws AtomSetException {
    if (!this.check(atom)) {
        // FIXME say why
        throw new UnsupportedAtomTypeException("");
    }
    try {
        for (Term t : atom.getTerms()) {
            if (this.getTerm(t.getLabel()) == null) {
                // FIXME Quick fix for
                // VARIABLE and
                // CONSTANT with same
                // label conflict
                this.add(statement, t);
            }
        }
        DBTable table = this.createPredicateTableIfNotExist(atom.getPredicate());
        Map<String, String> data = new TreeMap<String, String>();
        int i = -1;
        for (Term t : atom.getTerms()) {
            ++i;
            data.put("term" + i, '\'' + StringUtils.addSlashes(t.getIdentifier().toString()) + '\'');
        }
        String query = this.getDriver().getInsertOrIgnoreQuery(table, data);
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug(atom.toString() + " : " + query.toString());
        }
        statement.addBatch(query);
    } catch (SQLException e) {
        throw new AtomSetException(e);
    }
    return statement;
}
Also used : UnsupportedAtomTypeException(fr.lirmm.graphik.graal.api.core.UnsupportedAtomTypeException) DBTable(fr.lirmm.graphik.graal.store.rdbms.util.DBTable) SQLException(java.sql.SQLException) AtomSetException(fr.lirmm.graphik.graal.api.core.AtomSetException) Term(fr.lirmm.graphik.graal.api.core.Term) TreeMap(java.util.TreeMap)

Example 8 with DBTable

use of fr.lirmm.graphik.graal.store.rdbms.util.DBTable 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 9 with DBTable

use of fr.lirmm.graphik.graal.store.rdbms.util.DBTable in project graal by graphik-team.

the class AdHocConjunctiveQueryTranslator method translateContainsQuery.

@Override
public SQLQuery translateContainsQuery(Atom atom) throws AtomSetException {
    DBTable table = this.store.getPredicateTable(atom.getPredicate());
    if (table == null) {
        return SQLQuery.hasSchemaErrorInstance();
    }
    Term term;
    int termIndex = -1;
    StringBuilder query = new StringBuilder("SELECT 1 FROM ");
    query.append(table.getName());
    query.append(" WHERE ");
    Iterator<Term> terms = atom.getTerms().iterator();
    // TODO: FIX THIS => if arity = 0 -> crash ?!
    term = terms.next();
    ++termIndex;
    query.append("term").append(termIndex).append(" = \'").append(term.getIdentifier()).append('\'');
    while (terms.hasNext()) {
        term = terms.next();
        ++termIndex;
        query.append(" AND ").append(AbstractRdbmsConjunctiveQueryTranslator.PREFIX_TERM_FIELD).append(termIndex).append(" = \'").append(term.getIdentifier()).append('\'');
    }
    query.append(" LIMIT 1;");
    return new SQLQuery(query.toString());
}
Also used : DBTable(fr.lirmm.graphik.graal.store.rdbms.util.DBTable) Term(fr.lirmm.graphik.graal.api.core.Term) SQLQuery(fr.lirmm.graphik.graal.store.rdbms.util.SQLQuery)

Example 10 with DBTable

use of fr.lirmm.graphik.graal.store.rdbms.util.DBTable in project graal by graphik-team.

the class AdHocConjunctiveQueryTranslator method translate.

// /////////////////////////////////////////////////////////////////////////
// PUBLIC METHODS
// /////////////////////////////////////////////////////////////////////////
@Override
public SQLQuery translate(ConjunctiveQuery cquery) throws AtomSetException {
    if (cquery.getAtomSet().isEmpty()) {
        return SQLQuery.emptyInstance();
    }
    AtomSet atomSet = cquery.getAtomSet();
    StringBuilder fields = new StringBuilder();
    StringBuilder tables = new StringBuilder();
    StringBuilder where = new StringBuilder();
    HashMap<Atom, String> tableNames = new HashMap<Atom, String>();
    HashMap<String, String> lastOccurrence = new HashMap<String, String>();
    ArrayList<String> constants = new ArrayList<String>();
    ArrayList<String> equivalences = new ArrayList<String>();
    TreeMap<Term, String> columns = new TreeMap<Term, String>();
    int count = -1;
    CloseableIterator<Atom> it = atomSet.iterator();
    try {
        while (it.hasNext()) {
            Atom atom = it.next();
            String tableName = "atom" + ++count;
            tableNames.put(atom, tableName);
        }
        // Create WHERE clause
        it = atomSet.iterator();
        while (it.hasNext()) {
            Atom atom = it.next();
            String currentAtom = tableNames.get(atom) + ".";
            int position = 0;
            for (Term term : atom.getTerms()) {
                String thisTerm = currentAtom + AbstractRdbmsConjunctiveQueryTranslator.PREFIX_TERM_FIELD + position;
                if (term.isConstant()) {
                    constants.add(thisTerm + " = '" + term.getIdentifier().toString() + "'");
                } else {
                    if (lastOccurrence.containsKey(term.getIdentifier().toString())) {
                        equivalences.add(lastOccurrence.get(term.getIdentifier().toString()) + " = " + thisTerm);
                    }
                    lastOccurrence.put(term.getIdentifier().toString(), thisTerm);
                    if (cquery.getAnswerVariables().contains(term))
                        columns.put(term, thisTerm + " as " + term.getIdentifier().toString());
                }
                ++position;
            }
        }
    } catch (IteratorException e) {
        throw new AtomSetException(e);
    }
    for (String equivalence : equivalences) {
        if (where.length() != 0)
            where.append(" AND ");
        where.append(equivalence);
    }
    for (String constant : constants) {
        if (where.length() != 0)
            where.append(" AND ");
        where.append(constant);
    }
    // Create FROM clause
    DBTable table = null;
    for (Map.Entry<Atom, String> entries : tableNames.entrySet()) {
        if (tables.length() != 0)
            tables.append(", ");
        table = store.getPredicateTableIfExist(entries.getKey().getPredicate());
        if (table == null)
            return SQLQuery.hasSchemaErrorInstance();
        else
            tables.append(table.getName());
        tables.append(" as ");
        tables.append(entries.getValue());
    }
    // Create SELECT clause
    for (Term t : cquery.getAnswerVariables()) {
        if (fields.length() != 0)
            fields.append(", ");
        if (t.isConstant()) {
            fields.append("'");
            fields.append(t.getIdentifier());
            fields.append("'");
        } else {
            fields.append(columns.get(t));
        }
    }
    StringBuilder query = new StringBuilder("SELECT DISTINCT ");
    if (fields.length() > 0)
        query.append(fields);
    else
        query.append("1");
    query.append(" FROM ");
    if (tables.length() > 0)
        query.append(tables);
    else
        query.append(AdHocRdbmsStore.TEST_TABLE_NAME);
    if (where.length() > 0)
        query.append(" WHERE ").append(where);
    if (LOGGER.isDebugEnabled())
        LOGGER.debug("Generated SQL query :" + cquery + " --> " + query.toString());
    return new SQLQuery(query.toString());
}
Also used : IteratorException(fr.lirmm.graphik.util.stream.IteratorException) DBTable(fr.lirmm.graphik.graal.store.rdbms.util.DBTable) HashMap(java.util.HashMap) AtomSet(fr.lirmm.graphik.graal.api.core.AtomSet) ArrayList(java.util.ArrayList) Term(fr.lirmm.graphik.graal.api.core.Term) TreeMap(java.util.TreeMap) SQLQuery(fr.lirmm.graphik.graal.store.rdbms.util.SQLQuery) Atom(fr.lirmm.graphik.graal.api.core.Atom) AtomSetException(fr.lirmm.graphik.graal.api.core.AtomSetException) HashMap(java.util.HashMap) TreeMap(java.util.TreeMap) Map(java.util.Map)

Aggregations

DBTable (fr.lirmm.graphik.graal.store.rdbms.util.DBTable)17 AtomSetException (fr.lirmm.graphik.graal.api.core.AtomSetException)10 Term (fr.lirmm.graphik.graal.api.core.Term)9 SQLQuery (fr.lirmm.graphik.graal.store.rdbms.util.SQLQuery)9 SQLException (java.sql.SQLException)9 DBColumn (fr.lirmm.graphik.graal.store.rdbms.util.DBColumn)7 TreeMap (java.util.TreeMap)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 UnsupportedAtomTypeException (fr.lirmm.graphik.graal.api.core.UnsupportedAtomTypeException)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 ConjunctiveQuery (fr.lirmm.graphik.graal.api.core.ConjunctiveQuery)1 InMemoryAtomSet (fr.lirmm.graphik.graal.api.core.InMemoryAtomSet)1 ConversionException (fr.lirmm.graphik.util.stream.converter.ConversionException)1 ResultSet (java.sql.ResultSet)1 Statement (java.sql.Statement)1 LinkedList (java.util.LinkedList)1