Search in sources :

Example 1 with MarshallableRow

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

the class ResultSetTest method validate.

private void validate(MarshallableResultSet result) {
    MarshallableColumnNames colNames = result.getColumnNames();
    assertNotNull(colNames);
    String[] cols = colNames.getColumnName();
    assertNotNull(cols);
    assertEquals(2, cols.length);
    assertEquals("col1", cols[0]);
    assertEquals("col2", cols[1]);
    MarshallableColumnTypes colTypes = result.getColumnTypes();
    assertNotNull(colTypes);
    String[] types = colTypes.getColumnType();
    assertNotNull(types);
    assertEquals(2, types.length);
    assertEquals("string", types[0]);
    assertEquals("integer", types[1]);
    MarshallableRow[] rows = result.getRows();
    assertNotNull(rows);
    assertEquals(3, rows.length);
    MarshallableRow row = rows[0];
    assertNotNull(row);
    String[] cells = row.getCell();
    assertEquals(2, cells.length);
    assertEquals("a", cells[0]);
    assertEquals("1", cells[1]);
    row = rows[1];
    assertNotNull(row);
    cells = row.getCell();
    assertEquals(2, cells.length);
    assertEquals("b", cells[0]);
    assertEquals("2", cells[1]);
    row = rows[2];
    assertNotNull(row);
    cells = row.getCell();
    assertEquals(2, cells.length);
    assertEquals("c", cells[0]);
    assertEquals("3", cells[1]);
}
Also used : MarshallableRow(org.pentaho.commons.connection.marshal.MarshallableRow) MarshallableColumnNames(org.pentaho.commons.connection.marshal.MarshallableColumnNames) MarshallableColumnTypes(org.pentaho.commons.connection.marshal.MarshallableColumnTypes)

Example 2 with MarshallableRow

use of org.pentaho.commons.connection.marshal.MarshallableRow in project data-access by pentaho.

the class DatasourceInMemoryServiceHelper method getSerializeableResultSet.

public static SerializedResultSet getSerializeableResultSet(String connectionName, String query, int rowLimit, IPentahoSession session) throws DatasourceServiceException {
    SerializedResultSet serializedResultSet = null;
    SQLConnection sqlConnection = null;
    try {
        sqlConnection = getConnection(connectionName);
        sqlConnection.setMaxRows(rowLimit);
        sqlConnection.setReadOnly(true);
        IPentahoResultSet resultSet = sqlConnection.executeQuery(query);
        MarshallableResultSet marshallableResultSet = new MarshallableResultSet();
        marshallableResultSet.setResultSet(resultSet);
        IPentahoMetaData ipmd = resultSet.getMetaData();
        if (ipmd instanceof SQLMetaData) {
            // Hack warning - get JDBC column types
            // TODO: Need to generalize this amongst all IPentahoResultSets
            SQLMetaData smd = (SQLMetaData) ipmd;
            int[] columnTypes = smd.getJDBCColumnTypes();
            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("DatasourceInMemoryServiceHelper.ERROR_0005_QUERY_VALIDATION_FAILED", e.getLocalizedMessage()), // $NON-NLS-1$
        e);
        throw new DatasourceServiceException(Messages.getErrorString("DatasourceInMemoryServiceHelper.ERROR_0005_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) IPentahoMetaData(org.pentaho.commons.connection.IPentahoMetaData) DatasourceServiceException(org.pentaho.platform.dataaccess.datasource.wizard.service.DatasourceServiceException) SQLException(java.sql.SQLException) DatabaseDialectException(org.pentaho.database.DatabaseDialectException) ConnectionServiceException(org.pentaho.platform.dataaccess.datasource.wizard.service.ConnectionServiceException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) 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 3 with MarshallableRow

use of org.pentaho.commons.connection.marshal.MarshallableRow 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 4 with MarshallableRow

use of org.pentaho.commons.connection.marshal.MarshallableRow in project data-access by pentaho.

the class MetadataServiceTest method testDoQuery.

@Test
public void testDoQuery() {
    when(metadataService.doQuery(any(Query.class), anyInt())).thenCallRealMethod();
    when(metadataService.doXmlQuery(anyString(), any(Integer.class))).thenCallRealMethod();
    when(metadataServiceUtil.convertQuery(any(Query.class))).thenCallRealMethod();
    when(metadataServiceUtil.getCategory(anyString(), any(LogicalModel.class))).thenCallRealMethod();
    MarshallableResultSet marshallableResultSet = getMarshallableResultSet();
    when(metadataService.getMarshallableResultSet()).thenReturn(marshallableResultSet);
    Query query = buildQuery();
    MarshallableResultSet marshallableResultSetReturned = metadataService.doQuery(query, ROWS);
    MarshallableRow[] rows = marshallableResultSetReturned.getRows();
    String[] cell = rows[0].getCell();
    MarshallableColumnNames columnNames = marshallableResultSetReturned.getColumnNames();
    String[] columnName = columnNames.getColumnName();
    // Check the result rows lenght
    Assert.assertTrue(rows.length <= ROWS);
    // Check the result row value
    Assert.assertTrue(cell[0].equals(RESULT));
    // Check the result column name
    Assert.assertTrue(columnName[0].equals(COLUMN_NAME));
}
Also used : LogicalModel(org.pentaho.metadata.model.LogicalModel) MarshallableResultSet(org.pentaho.commons.connection.marshal.MarshallableResultSet) MarshallableRow(org.pentaho.commons.connection.marshal.MarshallableRow) Query(org.pentaho.platform.dataaccess.metadata.model.impl.Query) MarshallableColumnNames(org.pentaho.commons.connection.marshal.MarshallableColumnNames) Matchers.anyString(org.mockito.Matchers.anyString) LocalizedString(org.pentaho.metadata.model.concept.types.LocalizedString) Test(org.junit.Test)

Example 5 with MarshallableRow

use of org.pentaho.commons.connection.marshal.MarshallableRow in project data-access by pentaho.

the class MetadataServiceTest method testDoXmlQuery.

@Test
public void testDoXmlQuery() {
    String xmlQuery = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><mql><domain_id>" + DOMAIN_ID + "</domain_id><model_id" + ">" + LOGICAL_MODEL_ID + "</model_id><options><disable_distinct>false</disable_distinct><limit>-1</limit></options" + "><parameters><parameter defaultValue=\"" + VALUE + "\" name=\"" + COLUMN_NAME + "\" " + "type=\"STRING\"/></parameters><selections><selection><view>" + CATEGORY_ID + "</view><column>" + COLUMN_ID + "</column" + "><aggregation>NONE</aggregation></selection></selections><constraints><constraint><operator>AND</operator" + "><condition>[" + CATEGORY_NAME + "." + COLUMN_NAME + "] = " + VALUE + "</condition></constraint></constraints><orders/></mql>";
    when(metadataService.doXmlQuery(anyString(), any(Integer.class))).thenCallRealMethod();
    MarshallableResultSet marshallableResultSet = getMarshallableResultSet();
    when(metadataService.getMarshallableResultSet()).thenReturn(marshallableResultSet);
    MarshallableResultSet marshallableResultSetReturned = metadataService.doXmlQuery(xmlQuery, ROWS);
    MarshallableRow[] rows = marshallableResultSetReturned.getRows();
    String[] cell = rows[0].getCell();
    MarshallableColumnNames columnNames = marshallableResultSetReturned.getColumnNames();
    String[] columnName = columnNames.getColumnName();
    // Check the result rows lenght
    Assert.assertTrue(rows.length <= ROWS);
    // Check the result row value
    Assert.assertTrue(cell[0].equals(RESULT));
    // Check the result column name
    Assert.assertTrue(columnName[0].equals(COLUMN_NAME));
}
Also used : MarshallableResultSet(org.pentaho.commons.connection.marshal.MarshallableResultSet) MarshallableRow(org.pentaho.commons.connection.marshal.MarshallableRow) MarshallableColumnNames(org.pentaho.commons.connection.marshal.MarshallableColumnNames) Matchers.anyString(org.mockito.Matchers.anyString) LocalizedString(org.pentaho.metadata.model.concept.types.LocalizedString) Test(org.junit.Test)

Aggregations

MarshallableRow (org.pentaho.commons.connection.marshal.MarshallableRow)5 MarshallableResultSet (org.pentaho.commons.connection.marshal.MarshallableResultSet)4 MarshallableColumnNames (org.pentaho.commons.connection.marshal.MarshallableColumnNames)3 SQLException (java.sql.SQLException)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 Test (org.junit.Test)2 Matchers.anyString (org.mockito.Matchers.anyString)2 IPentahoMetaData (org.pentaho.commons.connection.IPentahoMetaData)2 IPentahoResultSet (org.pentaho.commons.connection.IPentahoResultSet)2 LocalizedString (org.pentaho.metadata.model.concept.types.LocalizedString)2 SerializedResultSet (org.pentaho.platform.dataaccess.datasource.beans.SerializedResultSet)2 DatasourceServiceException (org.pentaho.platform.dataaccess.datasource.wizard.service.DatasourceServiceException)2 SQLConnection (org.pentaho.platform.plugin.services.connections.sql.SQLConnection)2 SQLMetaData (org.pentaho.platform.plugin.services.connections.sql.SQLMetaData)2 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 ModelerException (org.pentaho.agilebi.modeler.ModelerException)1 MarshallableColumnTypes (org.pentaho.commons.connection.marshal.MarshallableColumnTypes)1 MemoryMetaData (org.pentaho.commons.connection.memory.MemoryMetaData)1