Search in sources :

Example 11 with MemoryResultSet

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

the class ResultSetTest method testWSResultSet1.

public void testWSResultSet1() {
    MemoryMetaData metadata = new MemoryMetaData(new String[][] { { "col1", "col2" } }, null);
    metadata.setColumnTypes(new String[] { "string", "int" });
    MemoryResultSet data = new MemoryResultSet(metadata);
    data.addRow(new Object[] { "a", new Integer(1) });
    data.addRow(new Object[] { "b", new Integer(2) });
    data.addRow(new Object[] { "c", new Integer(3) });
    MarshallableResultSet result = new MarshallableResultSet();
    result.setResultSet(data);
    validate(result);
}
Also used : MarshallableResultSet(org.pentaho.commons.connection.marshal.MarshallableResultSet) MemoryMetaData(org.pentaho.commons.connection.memory.MemoryMetaData) MemoryResultSet(org.pentaho.commons.connection.memory.MemoryResultSet)

Example 12 with MemoryResultSet

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

the class KettleComponent method registerAsStepListener.

private boolean registerAsStepListener(String stepName, Trans trans) throws KettleComponentException {
    boolean success = false;
    try {
        if (trans != null) {
            List<StepMetaDataCombi> stepList = trans.getSteps();
            // find the specified step
            for (StepMetaDataCombi step : stepList) {
                if (step.stepname.equals(stepName)) {
                    if (ComponentBase.debug) {
                        // $NON-NLS-1$
                        debug(Messages.getInstance().getString("Kettle.DEBUG_FOUND_STEP_IMPORTER"));
                    }
                    // this is the step we are looking for
                    if (ComponentBase.debug) {
                        // $NON-NLS-1$
                        debug(Messages.getInstance().getString("Kettle.DEBUG_GETTING_STEP_METADATA"));
                    }
                    RowMetaInterface row = trans.getTransMeta().getStepFields(stepName);
                    // create the metadata that the Pentaho result sets need
                    String[] fieldNames = row.getFieldNames();
                    String[][] columns = new String[1][fieldNames.length];
                    for (int column = 0; column < fieldNames.length; column++) {
                        columns[0][column] = fieldNames[column];
                    }
                    if (ComponentBase.debug) {
                        // $NON-NLS-1$
                        debug(Messages.getInstance().getString("Kettle.DEBUG_CREATING_RESULTSET_METADATA"));
                    }
                    MemoryMetaData metaData = new MemoryMetaData(columns, null);
                    results = new MemoryResultSet(metaData);
                    errorResults = new MemoryResultSet(metaData);
                    // add ourself as a row listener
                    step.step.addRowListener(this);
                    success = true;
                    break;
                }
            }
        }
    } catch (Exception e) {
        throw new KettleComponentException(Messages.getInstance().getString("Kettle.ERROR_0027_ERROR_INIT_STEP", stepName), // $NON-NLS-1$
        e);
    }
    return success;
}
Also used : StepMetaDataCombi(org.pentaho.di.trans.step.StepMetaDataCombi) RowMetaInterface(org.pentaho.di.core.row.RowMetaInterface) MemoryMetaData(org.pentaho.commons.connection.memory.MemoryMetaData) KettleException(org.pentaho.di.core.exception.KettleException) UnknownParamException(org.pentaho.di.core.parameters.UnknownParamException) KettleValueException(org.pentaho.di.core.exception.KettleValueException) KettleStepException(org.pentaho.di.core.exception.KettleStepException) MemoryResultSet(org.pentaho.commons.connection.memory.MemoryResultSet)

Example 13 with MemoryResultSet

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

the class SQLDdlComponent method doQuery.

@Override
public IPentahoResultSet doQuery(final SQLConnection sqlConnection, final String query, boolean forwardOnlyResultset) throws Exception {
    MemoryResultSet resultSet = null;
    int n = ((SQLConnection) connection).execute(query);
    Object[][] columnHeaders = new Object[1][1];
    // $NON-NLS-1$
    columnHeaders[0][0] = "result";
    IPentahoMetaData metadata = new MemoryMetaData(columnHeaders, null);
    resultSet = new MemoryResultSet(metadata);
    Object[] rowObjects = new Object[1];
    rowObjects[0] = new Integer(n);
    resultSet.addRow(rowObjects);
    return resultSet;
}
Also used : SQLConnection(org.pentaho.platform.plugin.services.connections.sql.SQLConnection) MemoryMetaData(org.pentaho.commons.connection.memory.MemoryMetaData) IPentahoMetaData(org.pentaho.commons.connection.IPentahoMetaData) MemoryResultSet(org.pentaho.commons.connection.memory.MemoryResultSet)

Example 14 with MemoryResultSet

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

the class SQLExecute method runSqlQuery.

protected boolean runSqlQuery(final SQLConnection conn, String rawQuery, final boolean live) {
    SqlExecuteAction sqlExecuteAction = (SqlExecuteAction) getActionDefinition();
    boolean executed = false;
    boolean continueOnException = sqlExecuteAction.getContinueOnException().getBooleanValue(false);
    String[] columnHeaders = new String[] { // $NON-NLS-1$
    Messages.getInstance().getString("SQLExecute.USER_AFFECTED_ROWS_COLUMN_NAME"), // $NON-NLS-1$
    Messages.getInstance().getString("SQLExecute.USER_AFFECTED_ROW_STATUS") };
    MemoryMetaData metaData = new MemoryMetaData(new String[][] { columnHeaders }, null);
    // $NON-NLS-1$ //$NON-NLS-2$
    metaData.setColumnTypes(new String[] { "int", "string" });
    MemoryResultSet affectedRowsResultSet = new MemoryResultSet(metaData);
    // $NON-NLS-1$
    String successMsg = Messages.getInstance().getString("SQLExecute.USER_SUCCESS");
    // $NON-NLS-1$
    String failMsg = Messages.getInstance().getString("SQLExecute.USER_FAILED");
    try {
        if (conn == null) {
            // $NON-NLS-1$
            error(Messages.getInstance().getErrorString("SQLBaseComponent.ERROR_0007_NO_CONNECTION"));
            return false;
        }
        if (!conn.initialized()) {
            // $NON-NLS-1$
            error(Messages.getInstance().getErrorString("SQLBaseComponent.ERROR_0007_NO_CONNECTION"));
            return false;
        }
        if (sqlExecuteAction.getForceSingleStatement().getBooleanValue(false)) {
            // Forces original execution path.
            // 
            // This execution path should be used if the query
            // has a semi-colon in the text of the SQL statement.
            // 
            // This is a legitimate condition if there is (for example)
            // a statement with a where-clause that has a semi-colon.
            // 
            // e.g.: UPDATE sometable SET somecolumn='val1;val2' WHERE somecolumn='val3;val4'
            // 
            // In this case, using StringTokenizer on semi-colon will result in multiple un-executable
            // statements - the whole thing will fail.
            // 
            // This is (arguably) unlikely, but it is possible. That's why I've chosen to make sure
            // that there is a mechanism for instating the old behavior.
            // 
            String query = applyInputsToFormat(rawQuery);
            if (ComponentBase.debug) {
                // $NON-NLS-1$
                debug(Messages.getInstance().getString("SQLBaseComponent.DEBUG_RUNNING_QUERY", query));
            }
            int affectedRows = conn.execute(query);
            executed = true;
            affectedRowsResultSet.addRow(new Object[] { new Integer(affectedRows), successMsg });
        } else {
            // 
            // Multiple statement execute support provided by contribution from Melanie Crouch
            // 
            rawQuery = SQLExecute.removeLineTerminators(rawQuery.trim()).toString();
            // tokenize the rawQuery passed into method to find if there are multiple updates to be executed.
            StringTokenizer st = // $NON-NLS-1$
            new StringTokenizer(rawQuery, sqlExecuteAction.getMultiStatementSeparator().getStringValue(";"));
            while (st.hasMoreTokens()) {
                // set rawQuery equal to the nextToken.
                rawQuery = st.nextToken();
                String query = applyInputsToFormat(rawQuery.trim());
                if (ComponentBase.debug) {
                    // $NON-NLS-1$
                    debug(Messages.getInstance().getString("SQLBaseComponent.DEBUG_RUNNING_QUERY", query));
                }
                try {
                    int affectedRows = conn.execute(query);
                    // Normally, we'd check to see if the execution resulted in
                    // some updated rows.
                    affectedRowsResultSet.addRow(new Object[] { new Integer(affectedRows), successMsg });
                    executed = true;
                    // $NON-NLS-1$
                    debug(Messages.getInstance().getString("SQLBaseComponent.DEBUG_UPDATED_QUERY", query));
                } catch (SQLException e) {
                    error(Messages.getInstance().getErrorString("SQLBaseComponent.ERROR_0006_EXECUTE_FAILED", // $NON-NLS-1$ //$NON-NLS-2$
                    getActionName() + " : " + e.getLocalizedMessage()));
                    executed = continueOnException;
                    if (!continueOnException) {
                        break;
                    }
                    addErrorCode(affectedRowsResultSet, e, failMsg);
                }
            }
        // end while tokenizer
        }
        if (getResultOutputName() != null) {
            setOutputValue(this.getResultOutputName(), affectedRowsResultSet);
        }
    } catch (SQLException e) {
        error(Messages.getInstance().getErrorString("SQLBaseComponent.ERROR_0006_EXECUTE_FAILED", // $NON-NLS-1$ //$NON-NLS-2$
        getActionName() + " : " + e.getLocalizedMessage()));
        executed = continueOnException;
        addErrorCode(affectedRowsResultSet, e, e.getLocalizedMessage());
    } finally {
        if (connectionOwner) {
            conn.close();
        }
    }
    return executed;
}
Also used : StringTokenizer(java.util.StringTokenizer) SQLException(java.sql.SQLException) MemoryMetaData(org.pentaho.commons.connection.memory.MemoryMetaData) SqlExecuteAction(org.pentaho.actionsequence.dom.actions.SqlExecuteAction) MemoryResultSet(org.pentaho.commons.connection.memory.MemoryResultSet)

Example 15 with MemoryResultSet

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

the class XQueryIT method testAddRow.

public void testAddRow() {
    MemoryMetaData metadata = new MemoryMetaData(new String[][] { { "col1", "col2" } }, null);
    MemoryResultSet dataSet = new MemoryResultSet(metadata);
    JavaScriptResultSet data = new JavaScriptResultSet();
    data.setResultSet(dataSet);
    data.addRow(new Object[] { "a", new Integer(1) });
    data.addRow(new Object[] { "b", new Integer(2) });
    data.addRow(new Object[] { "c", new Integer(3) });
    assertEquals(3, data.getRowCount());
    assertEquals("a", data.getValueAt(0, 0));
    assertEquals(1, data.getValueAt(0, 1));
    assertEquals("b", data.getValueAt(1, 0));
    assertEquals(2, data.getValueAt(1, 1));
    assertEquals("c", data.getValueAt(2, 0));
    assertEquals(3, data.getValueAt(2, 1));
}
Also used : JavaScriptResultSet(org.pentaho.platform.plugin.services.connections.javascript.JavaScriptResultSet) MemoryMetaData(org.pentaho.commons.connection.memory.MemoryMetaData) MemoryResultSet(org.pentaho.commons.connection.memory.MemoryResultSet)

Aggregations

MemoryResultSet (org.pentaho.commons.connection.memory.MemoryResultSet)31 MemoryMetaData (org.pentaho.commons.connection.memory.MemoryMetaData)24 IPentahoMetaData (org.pentaho.commons.connection.IPentahoMetaData)7 JavaScriptResultSet (org.pentaho.platform.plugin.services.connections.javascript.JavaScriptResultSet)7 Date (java.util.Date)3 ArrayList (java.util.ArrayList)2 List (java.util.List)2 RowMetaInterface (org.pentaho.di.core.row.RowMetaInterface)2 StepMetaDataCombi (org.pentaho.di.trans.step.StepMetaDataCombi)2 SQLException (java.sql.SQLException)1 HashMap (java.util.HashMap)1 Iterator (java.util.Iterator)1 Map (java.util.Map)1 StringTokenizer (java.util.StringTokenizer)1 MimeHeaders (javax.xml.soap.MimeHeaders)1 Name (javax.xml.soap.Name)1 Node (javax.xml.soap.Node)1 SOAPBody (javax.xml.soap.SOAPBody)1 SOAPConnection (javax.xml.soap.SOAPConnection)1 SOAPElement (javax.xml.soap.SOAPElement)1