Search in sources :

Example 11 with MemoryMetaData

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

the class XQueryIT method testIterators.

public void testIterators() {
    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));
    assertEquals("a", data.next()[0]);
    assertEquals("b", data.next()[0]);
    assertEquals("c", data.next()[0]);
    assertNull(data.next());
    data.beforeFirst();
    assertEquals("a", data.next()[0]);
    assertEquals("b", data.next()[0]);
    assertEquals("c", data.next()[0]);
    assertNull(data.next());
    data.close();
    assertEquals("a", data.next()[0]);
    assertEquals("b", data.next()[0]);
    assertEquals("c", data.next()[0]);
    assertNull(data.next());
    data.closeConnection();
    assertEquals("a", data.next()[0]);
    assertEquals("b", data.next()[0]);
    assertEquals("c", data.next()[0]);
    assertNull(data.next());
    data.dispose();
    assertEquals("a", data.next()[0]);
    assertEquals("b", data.next()[0]);
    assertEquals("c", data.next()[0]);
    assertNull(data.next());
}
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)

Example 12 with MemoryMetaData

use of org.pentaho.commons.connection.memory.MemoryMetaData in project pdi-platform-plugin by pentaho.

the class PdiActionTest method testTransformationInjector.

@Test
public void testTransformationInjector() throws Exception {
    String[][] columnNames = { { "REGION", "DEPARTMENT", "POSITIONTITLE" } };
    MemoryMetaData metadata = new MemoryMetaData(columnNames, null);
    MemoryResultSet rowsIn = new MemoryResultSet(metadata);
    rowsIn.addRow(new Object[] { "abc", "123", "bogus" });
    rowsIn.addRow(new Object[] { "region2", "Sales", "bad" });
    rowsIn.addRow(new Object[] { "Central", "Sales", "test title" });
    rowsIn.addRow(new Object[] { "Central", "xyz", "bad" });
    PdiAction component = new PdiAction();
    component.setRepositoryName(KettleFileRepositoryMeta.REPOSITORY_TYPE_ID);
    component.setDirectory(SOLUTION_REPOSITORY);
    component.setTransformation("/org/pentaho/platform/plugin/kettle/PdiActionTest_testTransformationInjector.ktr");
    component.setInjectorRows(rowsIn);
    component.setInjectorStep("Injector");
    component.setMonitorStep("Output");
    component.execute();
    assertEquals(1, component.getTransformationOutputRowsCount());
    assertEquals(0, component.getTransformationOutputErrorRowsCount());
    IPentahoResultSet rows = component.getTransformationOutputRows();
    assertNotNull(rows);
    assertEquals(1, rows.getRowCount());
    assertEquals("Central", rows.getValueAt(0, 0));
    assertEquals("Sales", rows.getValueAt(0, 1));
    assertEquals("test title", rows.getValueAt(0, 2));
    assertEquals("Hello, test title", rows.getValueAt(0, 3));
    rows = component.getTransformationOutputErrorRows();
    assertNotNull(rows);
    String log = component.getLog();
    assertTrue(log.contains("Injector"));
    assertTrue(log.contains("R=1"));
    assertTrue(log.contains("Filter rows"));
    assertTrue(log.contains("W=1"));
    assertTrue(log.contains("Java Script Value"));
    assertTrue(log.contains("W=1"));
    assertTrue(log.contains("Output"));
    assertTrue(log.contains("W=4"));
}
Also used : IPentahoResultSet(org.pentaho.commons.connection.IPentahoResultSet) MemoryMetaData(org.pentaho.commons.connection.memory.MemoryMetaData) MemoryResultSet(org.pentaho.commons.connection.memory.MemoryResultSet) Test(org.junit.Test)

Example 13 with MemoryMetaData

use of org.pentaho.commons.connection.memory.MemoryMetaData in project data-access by pentaho.

the class DatasourceServiceHelper method getSerializeableResultSet.

public static SerializedResultSet getSerializeableResultSet(String connectionName, String query, int rowLimit, IPentahoSession session) throws DatasourceServiceException {
    SerializedResultSet serializedResultSet = null;
    SQLConnection sqlConnection = null;
    try {
        sqlConnection = (SQLConnection) PentahoConnectionFactory.getConnection(IPentahoConnection.SQL_DATASOURCE, connectionName, PentahoSessionHolder.getSession(), null);
        sqlConnection.setMaxRows(rowLimit);
        sqlConnection.setReadOnly(true);
        IPentahoResultSet resultSet = sqlConnection.executeQuery(query);
        // $NON-NLS-1$
        logger.debug("ResultSet is not scrollable. Copying into memory");
        if (!resultSet.isScrollable()) {
            resultSet = convertToMemoryResultSet(resultSet);
        }
        MarshallableResultSet marshallableResultSet = new MarshallableResultSet();
        marshallableResultSet.setResultSet(resultSet);
        IPentahoMetaData ipmd = resultSet.getMetaData();
        int[] columnTypes = null;
        if (ipmd instanceof SQLMetaData) {
            SQLMetaData smd = (SQLMetaData) ipmd;
            columnTypes = smd.getJDBCColumnTypes();
        } else if (ipmd instanceof MemoryMetaData) {
            MemoryMetaData mmd = (MemoryMetaData) ipmd;
            String[] columnTypesAsString = mmd.getColumnTypes();
            columnTypes = new int[columnTypesAsString.length];
            for (int i = 0; i < columnTypesAsString.length; i++) {
                columnTypes[i] = Integer.parseInt(columnTypesAsString[i]);
            }
        }
        if (columnTypes != null) {
            // Hack warning - get JDBC column types
            // TODO: Need to generalize this amongst all IPentahoResultSets
            List<List<String>> data = new ArrayList<List<String>>();
            for (MarshallableRow row : marshallableResultSet.getRows()) {
                String[] rowData = row.getCell();
                List<String> rowDataList = new ArrayList<String>(rowData.length);
                for (int j = 0; j < rowData.length; j++) {
                    rowDataList.add(rowData[j]);
                }
                data.add(rowDataList);
            }
            serializedResultSet = new SerializedResultSet(columnTypes, marshallableResultSet.getColumnNames().getColumnName(), data);
        }
    } catch (Exception e) {
        logger.error(Messages.getErrorString("DatasourceServiceHelper.ERROR_0001_QUERY_VALIDATION_FAILED", e.getLocalizedMessage()), // $NON-NLS-1$
        e);
        throw new DatasourceServiceException(Messages.getErrorString("DatasourceServiceHelper.ERROR_0001_QUERY_VALIDATION_FAILED", e.getLocalizedMessage()), // $NON-NLS-1$
        e);
    } finally {
        if (sqlConnection != null) {
            sqlConnection.close();
        }
    }
    return serializedResultSet;
}
Also used : MarshallableResultSet(org.pentaho.commons.connection.marshal.MarshallableResultSet) SQLConnection(org.pentaho.platform.plugin.services.connections.sql.SQLConnection) ArrayList(java.util.ArrayList) SerializedResultSet(org.pentaho.platform.dataaccess.datasource.beans.SerializedResultSet) MemoryMetaData(org.pentaho.commons.connection.memory.MemoryMetaData) IPentahoMetaData(org.pentaho.commons.connection.IPentahoMetaData) DatasourceServiceException(org.pentaho.platform.dataaccess.datasource.wizard.service.DatasourceServiceException) ModelerException(org.pentaho.agilebi.modeler.ModelerException) SQLException(java.sql.SQLException) SQLMetaData(org.pentaho.platform.plugin.services.connections.sql.SQLMetaData) IPentahoResultSet(org.pentaho.commons.connection.IPentahoResultSet) MarshallableRow(org.pentaho.commons.connection.marshal.MarshallableRow) ArrayList(java.util.ArrayList) List(java.util.List) DatasourceServiceException(org.pentaho.platform.dataaccess.datasource.wizard.service.DatasourceServiceException)

Example 14 with MemoryMetaData

use of org.pentaho.commons.connection.memory.MemoryMetaData in project data-access by pentaho.

the class DatasourceServiceHelper method convertToMemoryResultSet.

/**
 * Convert the live result set to memory result set.
 *
 * @param resultSet
 * @return
 */
private static IPentahoResultSet convertToMemoryResultSet(IPentahoResultSet resultSet) throws SQLException {
    MemoryResultSet cachedResultSet = null;
    try {
        IPentahoMetaData meta = resultSet.getMetaData();
        Object[][] columnHeaders = meta.getColumnHeaders();
        MemoryMetaData cachedMetaData = new MemoryMetaData(columnHeaders, null);
        String[] colTypesAsString;
        // If the IPentahoMetaData is an instanceof SQLMetaData then get the column types from the metadata
        if (meta instanceof SQLMetaData) {
            SQLMetaData sqlMeta = (SQLMetaData) meta;
            // Column Types in SQLMetaData are int. MemoryMetaData stores column types as string. So we will store them
            // as string in MemoryMetaData
            int[] colTypes = sqlMeta.getJDBCColumnTypes();
            colTypesAsString = new String[colTypes.length];
            for (int i = 0; i < colTypes.length; i++) {
                colTypesAsString[i] = Integer.toString(colTypes[i]);
            }
            cachedMetaData.setColumnTypes(colTypesAsString);
        }
        cachedResultSet = new MemoryResultSet(cachedMetaData);
        Object[] rowObjects = resultSet.next();
        while (rowObjects != null) {
            cachedResultSet.addRow(rowObjects);
            rowObjects = resultSet.next();
        }
    } finally {
        resultSet.close();
    }
    return cachedResultSet;
}
Also used : SQLMetaData(org.pentaho.platform.plugin.services.connections.sql.SQLMetaData) MemoryMetaData(org.pentaho.commons.connection.memory.MemoryMetaData) IPentahoMetaData(org.pentaho.commons.connection.IPentahoMetaData) MemoryResultSet(org.pentaho.commons.connection.memory.MemoryResultSet)

Example 15 with MemoryMetaData

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

the class HQLResultSet method memoryCopy.

public IPentahoResultSet memoryCopy() {
    try {
        IPentahoMetaData localMetadata = getMetaData();
        Object[][] columnHeaders = localMetadata.getColumnHeaders();
        MemoryMetaData cachedMetaData = new MemoryMetaData(columnHeaders, null);
        MemoryResultSet cachedResultSet = new MemoryResultSet(cachedMetaData);
        Object[] rowObjects = next();
        while (rowObjects != null) {
            cachedResultSet.addRow(rowObjects);
            rowObjects = next();
        }
        return cachedResultSet;
    } finally {
        close();
    }
}
Also used : MemoryMetaData(org.pentaho.commons.connection.memory.MemoryMetaData) IPentahoMetaData(org.pentaho.commons.connection.IPentahoMetaData) MemoryResultSet(org.pentaho.commons.connection.memory.MemoryResultSet)

Aggregations

MemoryMetaData (org.pentaho.commons.connection.memory.MemoryMetaData)29 MemoryResultSet (org.pentaho.commons.connection.memory.MemoryResultSet)24 IPentahoMetaData (org.pentaho.commons.connection.IPentahoMetaData)9 JavaScriptResultSet (org.pentaho.platform.plugin.services.connections.javascript.JavaScriptResultSet)8 ArrayList (java.util.ArrayList)4 Date (java.util.Date)3 List (java.util.List)3 Test (org.junit.Test)3 IPentahoResultSet (org.pentaho.commons.connection.IPentahoResultSet)3 SQLException (java.sql.SQLException)2 NativeArray (org.mozilla.javascript.NativeArray)2 MarshallableResultSet (org.pentaho.commons.connection.marshal.MarshallableResultSet)2 RowMetaInterface (org.pentaho.di.core.row.RowMetaInterface)2 StepMetaDataCombi (org.pentaho.di.trans.step.StepMetaDataCombi)2 SQLConnection (org.pentaho.platform.plugin.services.connections.sql.SQLConnection)2 SQLMetaData (org.pentaho.platform.plugin.services.connections.sql.SQLMetaData)2 SimpleDateFormat (java.text.SimpleDateFormat)1 HashMap (java.util.HashMap)1 Iterator (java.util.Iterator)1 Map (java.util.Map)1