Search in sources :

Example 1 with MarshallableResultSet

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

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

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

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

the class MetadataService method doXmlQuery.

/**
 * Executes a XML query and returns a serializable result set
 *
 * @param rowLimit An optional row limit, -1 or null means all rows
 * @return
 */
public MarshallableResultSet doXmlQuery(String xml, Integer rowLimit) {
    IPentahoResultSet resultSet = executeQuery(xml, rowLimit);
    if (resultSet == null) {
        return null;
    }
    MarshallableResultSet result = getMarshallableResultSet();
    result.setResultSet(resultSet);
    return result;
}
Also used : IPentahoResultSet(org.pentaho.commons.connection.IPentahoResultSet) MarshallableResultSet(org.pentaho.commons.connection.marshal.MarshallableResultSet)

Example 5 with MarshallableResultSet

use of org.pentaho.commons.connection.marshal.MarshallableResultSet 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)

Aggregations

MarshallableResultSet (org.pentaho.commons.connection.marshal.MarshallableResultSet)9 IPentahoResultSet (org.pentaho.commons.connection.IPentahoResultSet)5 MarshallableRow (org.pentaho.commons.connection.marshal.MarshallableRow)4 LocalizedString (org.pentaho.metadata.model.concept.types.LocalizedString)4 Test (org.junit.Test)3 Matchers.anyString (org.mockito.Matchers.anyString)3 IPentahoMetaData (org.pentaho.commons.connection.IPentahoMetaData)3 SQLException (java.sql.SQLException)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 MarshallableColumnNames (org.pentaho.commons.connection.marshal.MarshallableColumnNames)2 MemoryMetaData (org.pentaho.commons.connection.memory.MemoryMetaData)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 JSONSerializer (flexjson.JSONSerializer)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 ModelerException (org.pentaho.agilebi.modeler.ModelerException)1