Search in sources :

Example 1 with DBColumn

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

the class AbstractRdbmsDriver method getColumns.

@Override
public List<DBColumn> getColumns(String tableName) throws SQLException {
    tableName = this.formatIdentifier(tableName);
    ArrayList<DBColumn> list = new ArrayList<DBColumn>();
    DatabaseMetaData metaData = this.getConnection().getMetaData();
    ResultSet res = metaData.getColumns(null, null, tableName, null);
    while (res.next()) {
        String name = res.getString("COLUMN_NAME");
        int type = res.getInt("DATA_TYPE");
        list.add(new DBColumn(name, type));
    }
    return list;
}
Also used : ArrayList(java.util.ArrayList) ResultSet(java.sql.ResultSet) DBColumn(fr.lirmm.graphik.graal.store.rdbms.util.DBColumn) DatabaseMetaData(java.sql.DatabaseMetaData)

Example 2 with DBColumn

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

the class AdHocRdbmsStore method add.

// /////////////////////////////////////////////////////////////////////////
// PRIVATE METHODS
// /////////////////////////////////////////////////////////////////////////
private void add(Statement statement, Term term) throws AtomSetException {
    try {
        List<DBColumn> cols = TERMS_TABLE.getColumns();
        Map<String, String> data = new TreeMap<String, String>();
        data.put(cols.get(0).getName(), '\'' + StringUtils.addSlashes(term.getIdentifier().toString()) + '\'');
        data.put(cols.get(1).getName(), '\'' + getType(term) + '\'');
        String query = this.getDriver().getInsertOrIgnoreQuery(TERMS_TABLE, data);
        statement.executeUpdate(query);
    } catch (SQLException e) {
        throw new AtomSetException("Error during insertion of a term: " + term, e);
    }
}
Also used : SQLException(java.sql.SQLException) AtomSetException(fr.lirmm.graphik.graal.api.core.AtomSetException) DBColumn(fr.lirmm.graphik.graal.store.rdbms.util.DBColumn) TreeMap(java.util.TreeMap)

Example 3 with DBColumn

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

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

the class AbstractRdbmsConjunctiveQueryTranslator method translateCreateTable.

@Override
public String translateCreateTable(DBTable table) {
    StringBuilder primaryKey = new StringBuilder("PRIMARY KEY (");
    StringBuilder query = new StringBuilder("CREATE TABLE ");
    query.append(table.getName());
    query.append(" (");
    boolean first = true;
    for (DBColumn col : table.getColumns()) {
        if (!first) {
            query.append(", ");
            primaryKey.append(", ");
        }
        query.append(col.getName()).append(" varchar(" + AbstractRdbmsStore.VARCHAR_SIZE + ")");
        primaryKey.append(col.getName());
        first = false;
    }
    primaryKey.append(")");
    query.append(',');
    query.append(primaryKey);
    query.append(");");
    return query.toString();
}
Also used : DBColumn(fr.lirmm.graphik.graal.store.rdbms.util.DBColumn)

Example 5 with DBColumn

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

the class NaturalRDBMSStore method add.

// /////////////////////////////////////////////////////////////////////////
// PROTECTED AND PRIVATE METHODS
// /////////////////////////////////////////////////////////////////////////
@Override
protected Statement add(Statement statement, Atom atom) throws AtomSetException {
    if (!this.check(atom)) {
        // FIXME say why
        throw new UnsupportedAtomTypeException("");
    }
    try {
        DBTable table = this.createPredicateTableIfNotExist(atom.getPredicate());
        Iterator<DBColumn> cols = table.getColumns().iterator();
        Map<String, String> data = new TreeMap<String, String>();
        for (Term t : atom.getTerms()) {
            DBColumn col = cols.next();
            data.put(col.getName(), this.getConjunctiveQueryTranslator().formatFromColumnType(col, t));
        }
        String query = this.getDriver().getInsertOrIgnoreQuery(table, data);
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug(atom.toString() + " : " + query);
        }
        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) DBColumn(fr.lirmm.graphik.graal.store.rdbms.util.DBColumn) Term(fr.lirmm.graphik.graal.api.core.Term) TreeMap(java.util.TreeMap)

Aggregations

DBColumn (fr.lirmm.graphik.graal.store.rdbms.util.DBColumn)11 DBTable (fr.lirmm.graphik.graal.store.rdbms.util.DBTable)7 Term (fr.lirmm.graphik.graal.api.core.Term)5 SQLQuery (fr.lirmm.graphik.graal.store.rdbms.util.SQLQuery)5 AtomSetException (fr.lirmm.graphik.graal.api.core.AtomSetException)4 SQLException (java.sql.SQLException)4 TreeMap (java.util.TreeMap)3 ArrayList (java.util.ArrayList)2 Atom (fr.lirmm.graphik.graal.api.core.Atom)1 AtomSet (fr.lirmm.graphik.graal.api.core.AtomSet)1 UnsupportedAtomTypeException (fr.lirmm.graphik.graal.api.core.UnsupportedAtomTypeException)1 IteratorException (fr.lirmm.graphik.util.stream.IteratorException)1 ConversionException (fr.lirmm.graphik.util.stream.converter.ConversionException)1 DatabaseMetaData (java.sql.DatabaseMetaData)1 ResultSet (java.sql.ResultSet)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1