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;
}
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;
}
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);
}
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);
}
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;
}
Aggregations