Search in sources :

Example 1 with ResultSetJDBC

use of org.apache.jena.sdb.sql.ResultSetJDBC in project jena by apache.

the class sdbsql method execOneSQL.

private void execOneSQL(String sqlStmt) {
    if (isVerbose()) {
        System.out.print(sqlStmt);
        if (!sqlStmt.endsWith("\n"))
            System.out.println();
    }
    if (sqlStmt.startsWith("@"))
        sqlStmt = FileManager.get().readWholeFileAsUTF8(sqlStmt.substring(1));
    getModTime().startTimer();
    long queryTime = 0;
    ResultSetJDBC rs = null;
    try {
        rs = getModStore().getConnection().exec(sqlStmt);
        queryTime = getModTime().readTimer();
        if (rs == null)
            System.out.println("Executed with no errors or results");
        else {
            if (isQuiet())
                RS.consume(rs.get());
            else
                RS.printResultSet(rs.get());
        }
    } catch (SQLException ex) {
        System.err.println("SQL Exception: " + ex.getMessage());
        throw new TerminationException(9);
    } finally {
        RS.close(rs);
    }
    long time = getModTime().endTimer();
    long fmtTime = time - queryTime;
    if (getModTime().timingEnabled()) {
        String totalTimeStr = getModTime().timeStr(time);
        String queryTimeStr = getModTime().timeStr(queryTime);
        String fmtTimeStr = getModTime().timeStr(fmtTime);
        System.out.printf("Query: %s (query %s, formatting %s)\n", totalTimeStr, queryTimeStr, fmtTimeStr);
    }
}
Also used : TerminationException(jena.cmd.TerminationException) SQLException(java.sql.SQLException) ResultSetJDBC(org.apache.jena.sdb.sql.ResultSetJDBC)

Example 2 with ResultSetJDBC

use of org.apache.jena.sdb.sql.ResultSetJDBC in project jena by apache.

the class TupleTable method getDesc.

private static TableDesc getDesc(Store store, String tableName) {
    ResultSetJDBC tableData = null;
    List<String> colVars = new ArrayList<String>();
    try {
        // Need to portable get the column names.
        tableData = store.getConnection().execQuery("SELECT * FROM " + tableName);
        java.sql.ResultSetMetaData meta = tableData.get().getMetaData();
        int N = meta.getColumnCount();
        for (int i = 1; i <= N; i++) {
            String colName = meta.getColumnName(i);
            colVars.add(colName);
        }
        return new TableDesc(tableName, colVars);
    } catch (SQLException ex) {
        throw new SDBExceptionSQL(ex);
    } finally {
        RS.close(tableData);
    }
}
Also used : SDBExceptionSQL(org.apache.jena.sdb.sql.SDBExceptionSQL) SQLException(java.sql.SQLException) ResultSetJDBC(org.apache.jena.sdb.sql.ResultSetJDBC) ArrayList(java.util.ArrayList)

Example 3 with ResultSetJDBC

use of org.apache.jena.sdb.sql.ResultSetJDBC in project jena by apache.

the class TupleTable method iterator.

public QueryIterator iterator() {
    SDBRequest request = new SDBRequest(store, null);
    String tableName = desc.getTableName();
    SQLBridge b = store.getSQLBridgeFactory().create(request, sqlTable, vars);
    b.build();
    try {
        String sqlStr = store.getSQLGenerator().generateSQL(request, b.getSqlNode());
        //System.out.println(sqlStr) ;
        ResultSetJDBC tableData = store.getConnection().execQuery(sqlStr);
        ExecutionContext execCxt = new ExecutionContext(new Context(), null, null, null);
        return b.assembleResults(tableData, BindingRoot.create(), execCxt);
    } catch (SQLException ex) {
        throw new SDBExceptionSQL(ex);
    }
}
Also used : Context(org.apache.jena.sparql.util.Context) ExecutionContext(org.apache.jena.sparql.engine.ExecutionContext) ExecutionContext(org.apache.jena.sparql.engine.ExecutionContext) SDBExceptionSQL(org.apache.jena.sdb.sql.SDBExceptionSQL) SQLException(java.sql.SQLException) ResultSetJDBC(org.apache.jena.sdb.sql.ResultSetJDBC) SDBRequest(org.apache.jena.sdb.core.SDBRequest)

Example 4 with ResultSetJDBC

use of org.apache.jena.sdb.sql.ResultSetJDBC in project jena by apache.

the class TupleLoaderOneHash method insertNode.

@Override
public SqlConstant insertNode(Node node) throws SQLException {
    int typeId = NodeLayout2.nodeToType(node);
    String lex = NodeLayout2.nodeToLex(node);
    String lang = "";
    String datatype = "";
    if (node.isLiteral()) {
        lang = node.getLiteralLanguage();
        datatype = node.getLiteralDatatypeURI();
        if (NodeUtils.isSimpleString(node) || NodeUtils.isLangString(node))
            datatype = "";
    }
    long hash = NodeLayout2.hash(lex, lang, datatype, typeId);
    // Existance check
    String sqlStmtTest = strjoinNL("SELECT hash FROM " + TableDescNodes.name(), "WHERE hash = " + hash);
    ResultSetJDBC rsx = null;
    try {
        rsx = connection().execQuery(sqlStmtTest);
        ResultSet rs = rsx.get();
        boolean b = rs.next();
        if (b)
            // Exists
            return new SqlConstant(hash);
    } finally {
        RS.close(rsx);
    }
    String sqlStmt = strjoinNL("INSERT INTO " + TableDescNodes.name() + "(hash,lex,lang,datatype,type) VALUES", "  (" + hash + ", ", "   " + SQLUtils.quoteStr(lex) + ", ", "   " + SQLUtils.quoteStr(lang) + ", ", "   " + SQLUtils.quoteStr(datatype) + ", ", "   " + typeId, ")");
    connection().execUpdate(sqlStmt);
    return new SqlConstant(hash);
}
Also used : SqlConstant(org.apache.jena.sdb.core.sqlexpr.SqlConstant) ResultSetJDBC(org.apache.jena.sdb.sql.ResultSetJDBC) ResultSet(java.sql.ResultSet)

Example 5 with ResultSetJDBC

use of org.apache.jena.sdb.sql.ResultSetJDBC in project jena by apache.

the class SDB_QC method exec.

// ---- Execute an OpSQL.
public static QueryIterator exec(OpSQL opSQL, SDBRequest request, Binding binding, ExecutionContext execCxt) {
    String sqlStmtStr = toSqlString(opSQL, request);
    if (PrintSQL)
        System.out.println(sqlStmtStr);
    String str = null;
    if (execCxt != null)
        str = execCxt.getContext().getAsString(SDB.jdbcFetchSize);
    int fetchSize = SDBConstants.jdbcFetchSizeOff;
    if (str != null)
        try {
            fetchSize = Integer.parseInt(str);
        } catch (NumberFormatException ex) {
            log.warn("Bad number for fetch size: " + str);
        }
    try {
        ResultSetJDBC jdbcResultSet = request.getStore().getConnection().execQuery(sqlStmtStr, fetchSize);
        try {
            // And check this is called once per SQL.
            if (opSQL.getBridge() == null)
                log.error("Null bridge");
            return opSQL.getBridge().assembleResults(jdbcResultSet, binding, execCxt);
        } finally {
            // ResultSet closed inside assembleResults or by the iterator returned.
            jdbcResultSet = null;
        }
    } catch (SQLException ex) {
        throw new SDBExceptionSQL("SQLException in executing SQL statement", ex);
    }
}
Also used : SDBExceptionSQL(org.apache.jena.sdb.sql.SDBExceptionSQL) SQLException(java.sql.SQLException) ResultSetJDBC(org.apache.jena.sdb.sql.ResultSetJDBC)

Aggregations

ResultSetJDBC (org.apache.jena.sdb.sql.ResultSetJDBC)12 SQLException (java.sql.SQLException)11 SDBExceptionSQL (org.apache.jena.sdb.sql.SDBExceptionSQL)8 ResultSet (java.sql.ResultSet)6 ArrayList (java.util.ArrayList)1 TerminationException (jena.cmd.TerminationException)1 SDBException (org.apache.jena.sdb.SDBException)1 SDBRequest (org.apache.jena.sdb.core.SDBRequest)1 SqlConstant (org.apache.jena.sdb.core.sqlexpr.SqlConstant)1 ExecutionContext (org.apache.jena.sparql.engine.ExecutionContext)1 Context (org.apache.jena.sparql.util.Context)1