Search in sources :

Example 61 with IPentahoResultSet

use of org.pentaho.commons.connection.IPentahoResultSet in project pentaho-platform by pentaho.

the class SQLConnection method executeQuery.

/**
 * Executes the specified query with the defined parameters
 *
 * @param query
 *          the query to be executed
 * @param scrollType
 * @param concur
 * @return the result set of data for the query
 * @throws SQLException
 *           indicates an error running the query
 * @throws InterruptedException
 *           indicates the query took longer than allowable by the query timeout
 * @throws PentahoSystemException
 */
public IPentahoResultSet executeQuery(final String query, final int scrollType, final int concur) throws SQLException, InterruptedException, PentahoSystemException {
    if (this.getReadOnly()) {
        try {
            nativeConnection.setReadOnly(true);
        } catch (Exception ignored) {
        // ignored
        }
    }
    // Create a statement for a scrollable resultset.
    Statement stmt = null;
    ResultSet resultSet = null;
    try {
        stmt = nativeConnection.createStatement(scrollType, concur);
        stmts.add(stmt);
        enhanceStatement(stmt);
        setStatementLimitations(stmt);
        if (logger != null && logger.getLoggingLevel() == ILogger.DEBUG) {
            // $NON-NLS-1$
            logger.debug("SQLConnection.executeQuery:" + query);
        }
        resultSet = stmt.executeQuery(query);
    } catch (Exception e) {
        // on this connection, then try to fix it up...
        if ((scrollType == ResultSet.TYPE_SCROLL_INSENSITIVE) && (isFallBackToNonscrollableOnError())) {
            // FORCE forward only
            stmt = nativeConnection.createStatement(ResultSet.TYPE_FORWARD_ONLY, concur);
            stmts.add(stmt);
            enhanceStatement(stmt);
            setStatementLimitations(stmt);
            if (logger != null && logger.getLoggingLevel() == ILogger.DEBUG) {
                // $NON-NLS-1$
                logger.debug("SQLConnection.executeQuery(e):" + query);
            }
            resultSet = stmt.executeQuery(query);
            setForcedForwardOnly(true);
        }
    }
    sqlResultSet = new SQLResultSet(resultSet, this);
    // add to list of resultsets for cleanup later.
    resultSets.add(sqlResultSet);
    lastQuery = query;
    return sqlResultSet;
}
Also used : PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) ResultSet(java.sql.ResultSet) IPentahoResultSet(org.pentaho.commons.connection.IPentahoResultSet) ObjectFactoryException(org.pentaho.platform.api.engine.ObjectFactoryException) SQLException(java.sql.SQLException) PentahoSystemException(org.pentaho.platform.api.engine.PentahoSystemException)

Example 62 with IPentahoResultSet

use of org.pentaho.commons.connection.IPentahoResultSet in project pentaho-platform by pentaho.

the class SQLConnection method prepareAndExecuteQuery.

public IPentahoResultSet prepareAndExecuteQuery(final String query, final List parameters, final int scrollType, final int concur) throws SQLException {
    if (this.getReadOnly()) {
        try {
            nativeConnection.setReadOnly(true);
        } catch (Exception ignored) {
        // ignored
        }
    }
    // Create a prepared statement
    PreparedStatement pStmt = null;
    ResultSet resultSet = null;
    try {
        if (logger != null && logger.getLoggingLevel() == ILogger.DEBUG) {
            // $NON-NLS-1$
            logger.debug("SQLConnection.prepareAndExecuteQuery:" + query);
        }
        pStmt = nativeConnection.prepareStatement(query, scrollType, concur);
        // add to stmts list for closing when connection closes
        stmts.add(pStmt);
        enhanceStatement(pStmt);
        setStatementLimitations(pStmt);
        for (int i = 0; i < parameters.size(); i++) {
            pStmt.setObject(i + 1, parameters.get(i));
        }
        resultSet = pStmt.executeQuery();
    } catch (Exception e) {
        // attempt to remove the offending statement...
        stmts.remove(pStmt);
        if ((scrollType == ResultSet.TYPE_SCROLL_INSENSITIVE) && (isFallBackToNonscrollableOnError())) {
            // FORCE forward only
            if (logger != null && logger.getLoggingLevel() == ILogger.DEBUG) {
                // $NON-NLS-1$
                logger.debug("SQLConnection.prepareAndExecuteQuery(e):" + query);
            }
            pStmt = nativeConnection.prepareStatement(query, ResultSet.TYPE_FORWARD_ONLY, concur);
            // add to stmts list for closing when connection closes
            stmts.add(pStmt);
            enhanceStatement(pStmt);
            setStatementLimitations(pStmt);
            for (int i = 0; i < parameters.size(); i++) {
                pStmt.setObject(i + 1, parameters.get(i));
            }
            resultSet = pStmt.executeQuery();
            setForcedForwardOnly(true);
        }
    }
    sqlResultSet = new SQLResultSet(resultSet, this);
    // add to list of resultsets for cleanup later.
    resultSets.add(sqlResultSet);
    lastQuery = query;
    return sqlResultSet;
}
Also used : ResultSet(java.sql.ResultSet) IPentahoResultSet(org.pentaho.commons.connection.IPentahoResultSet) PreparedStatement(java.sql.PreparedStatement) ObjectFactoryException(org.pentaho.platform.api.engine.ObjectFactoryException) SQLException(java.sql.SQLException) PentahoSystemException(org.pentaho.platform.api.engine.PentahoSystemException)

Example 63 with IPentahoResultSet

use of org.pentaho.commons.connection.IPentahoResultSet in project pentaho-platform by pentaho.

the class ResultSetCompareComponentTest method execute_fails_without_validation.

@Test
public void execute_fails_without_validation() {
    ResultSetCompareComponent rscc = createResultSetCompareComponent();
    IPentahoResultSet rs = Mockito.mock(IPentahoResultSet.class);
    ResultSetCompareAction resultSetCompareAction = createResultSetCompareAction(rs, rs, 0, false, true);
    rscc.setActionDefinition(resultSetCompareAction);
    int actualExecuteResult = rscc.execute();
    assertNotEquals(IRuntimeContext.RUNTIME_STATUS_SUCCESS, actualExecuteResult);
}
Also used : IPentahoResultSet(org.pentaho.commons.connection.IPentahoResultSet) ResultSetCompareAction(org.pentaho.actionsequence.dom.actions.ResultSetCompareAction) Test(org.junit.Test)

Example 64 with IPentahoResultSet

use of org.pentaho.commons.connection.IPentahoResultSet in project pentaho-platform by pentaho.

the class ResultSetCompareComponentTest method execute_fails_when_resultSets_have_different_values_in_compareColumn.

@Test
public void execute_fails_when_resultSets_have_different_values_in_compareColumn() {
    ResultSetCompareComponent rscc = createResultSetCompareComponent();
    IPentahoResultSet rs1 = createResultSet(new String[][] { { FIRST_ROW }, { SECOND_ROW } });
    IPentahoResultSet rs2 = createResultSet(new String[][] { { FIRST_ROW }, { "SECOND" } });
    ResultSetCompareAction resultSetCompareAction = createResultSetCompareAction(rs1, rs2, 0, false, true);
    rscc.setActionDefinition(resultSetCompareAction);
    rscc.validate();
    int actualExecuteResult = rscc.execute();
    assertEquals(IRuntimeContext.RUNTIME_STATUS_FAILURE, actualExecuteResult);
}
Also used : IPentahoResultSet(org.pentaho.commons.connection.IPentahoResultSet) ResultSetCompareAction(org.pentaho.actionsequence.dom.actions.ResultSetCompareAction) Test(org.junit.Test)

Example 65 with IPentahoResultSet

use of org.pentaho.commons.connection.IPentahoResultSet in project pentaho-platform by pentaho.

the class ResultSetCompareComponentTest method createResultSet.

private static IPentahoResultSet createResultSet(int columnCount, int rowCount, Object resultSetElement) {
    IPentahoResultSet rs = Mockito.mock(IPentahoResultSet.class);
    when(rs.getColumnCount()).thenReturn(columnCount);
    when(rs.getRowCount()).thenReturn(rowCount);
    when(rs.getValueAt(anyInt(), anyInt())).thenReturn(resultSetElement);
    return rs;
}
Also used : IPentahoResultSet(org.pentaho.commons.connection.IPentahoResultSet)

Aggregations

IPentahoResultSet (org.pentaho.commons.connection.IPentahoResultSet)113 Test (org.junit.Test)26 Node (org.dom4j.Node)18 ArrayList (java.util.ArrayList)17 IPentahoMetaData (org.pentaho.commons.connection.IPentahoMetaData)12 Iterator (java.util.Iterator)10 List (java.util.List)10 IPentahoConnection (org.pentaho.commons.connection.IPentahoConnection)10 IActionParameter (org.pentaho.platform.api.engine.IActionParameter)10 IPentahoSession (org.pentaho.platform.api.engine.IPentahoSession)10 OutputStream (java.io.OutputStream)9 ResultSetCompareAction (org.pentaho.actionsequence.dom.actions.ResultSetCompareAction)8 IRuntimeContext (org.pentaho.platform.api.engine.IRuntimeContext)8 XQConnection (org.pentaho.platform.plugin.services.connections.xquery.XQConnection)8 SQLException (java.sql.SQLException)7 HashMap (java.util.HashMap)6 StandaloneSession (org.pentaho.platform.engine.core.system.StandaloneSession)6 Map (java.util.Map)5 Set (java.util.Set)5 IPreparedComponent (org.pentaho.platform.api.data.IPreparedComponent)5