Search in sources :

Example 1 with SerializedResultSet

use of org.pentaho.platform.dataaccess.datasource.beans.SerializedResultSet 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 2 with SerializedResultSet

use of org.pentaho.platform.dataaccess.datasource.beans.SerializedResultSet 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 3 with SerializedResultSet

use of org.pentaho.platform.dataaccess.datasource.beans.SerializedResultSet in project data-access by pentaho.

the class DSWDatasourceServiceImpl method doPreview.

public SerializedResultSet doPreview(String connectionName, String query, String previewLimit) throws DatasourceServiceException {
    if (!hasDataAccessPermission()) {
        // $NON-NLS-1$
        logger.error(Messages.getErrorString("DatasourceServiceImpl.ERROR_0001_PERMISSION_DENIED"));
        throw new DatasourceServiceException(Messages.getErrorString(// $NON-NLS-1$
        "DatasourceServiceImpl.ERROR_0001_PERMISSION_DENIED"));
    }
    SerializedResultSet returnResultSet;
    try {
        connectionName = UtilHtmlSanitizer.getInstance().safeEscapeHtml(connectionName);
        executeQuery(connectionName, query, previewLimit);
        returnResultSet = DatasourceServiceHelper.getSerializeableResultSet(connectionName, query, Integer.parseInt(previewLimit), PentahoSessionHolder.getSession());
    } catch (QueryValidationException e) {
        throw new DatasourceServiceException(Messages.getErrorString("DatasourceServiceImpl.ERROR_0009_QUERY_VALIDATION_FAILED", e.getLocalizedMessage()), // $NON-NLS-1$
        e);
    } catch (SqlQueriesNotSupportedException e) {
        // $NON-NLS-1$
        throw new DatasourceServiceException(e.getLocalizedMessage(), e);
    }
    return returnResultSet;
}
Also used : QueryValidationException(org.pentaho.platform.dataaccess.datasource.wizard.service.QueryValidationException) SerializedResultSet(org.pentaho.platform.dataaccess.datasource.beans.SerializedResultSet) SqlQueriesNotSupportedException(org.pentaho.platform.dataaccess.datasource.wizard.service.SqlQueriesNotSupportedException) DatasourceServiceException(org.pentaho.platform.dataaccess.datasource.wizard.service.DatasourceServiceException)

Example 4 with SerializedResultSet

use of org.pentaho.platform.dataaccess.datasource.beans.SerializedResultSet in project data-access by pentaho.

the class DSWDatasourceServiceImpl method loadBusinessData.

public BusinessData loadBusinessData(String domainId, String modelId) throws DatasourceServiceException {
    Domain domain = getMetadataDomainRepository().getDomain(domainId);
    List<List<String>> data = null;
    if (domain.getPhysicalModels().get(0) instanceof InlineEtlPhysicalModel) {
        InlineEtlPhysicalModel model = (InlineEtlPhysicalModel) domain.getPhysicalModels().get(0);
        String relativePath = PentahoSystem.getSystemSetting("file-upload-defaults/relative-path", // $NON-NLS-1$
        String.valueOf(CsvTransformGenerator.DEFAULT_RELATIVE_UPLOAD_FILE_PATH));
        String csvFileLoc = PentahoSystem.getApplicationContext().getSolutionPath(relativePath);
        data = DatasourceServiceHelper.getCsvDataSample(csvFileLoc + model.getFileLocation(), model.getHeaderPresent(), model.getDelimiter(), model.getEnclosure(), 5);
    } else {
        SqlPhysicalModel model = (SqlPhysicalModel) domain.getPhysicalModels().get(0);
        String query = model.getPhysicalTables().get(0).getTargetTable();
        SerializedResultSet resultSet = DatasourceServiceHelper.getSerializeableResultSet(model.getDatasource().getDatabaseName(), query, 5, PentahoSessionHolder.getSession());
        data = resultSet.getData();
    }
    return new BusinessData(domain, data);
}
Also used : BusinessData(org.pentaho.platform.dataaccess.datasource.beans.BusinessData) InlineEtlPhysicalModel(org.pentaho.metadata.model.InlineEtlPhysicalModel) SerializedResultSet(org.pentaho.platform.dataaccess.datasource.beans.SerializedResultSet) List(java.util.List) ArrayList(java.util.ArrayList) Domain(org.pentaho.metadata.model.Domain) SqlPhysicalModel(org.pentaho.metadata.model.SqlPhysicalModel)

Example 5 with SerializedResultSet

use of org.pentaho.platform.dataaccess.datasource.beans.SerializedResultSet in project data-access by pentaho.

the class InMemoryDSWDatasourceServiceImpl method loadBusinessData.

public BusinessData loadBusinessData(String domainId, String modelId) throws DatasourceServiceException {
    Domain domain = getMetadataDomainRepository().getDomain(domainId);
    List<List<String>> data = null;
    if (domain.getPhysicalModels().get(0) instanceof InlineEtlPhysicalModel) {
        InlineEtlPhysicalModel model = (InlineEtlPhysicalModel) domain.getPhysicalModels().get(0);
        data = DatasourceInMemoryServiceHelper.getCsvDataSample(model.getFileLocation(), model.getHeaderPresent(), model.getDelimiter(), model.getEnclosure(), 5);
    } else {
        SqlPhysicalModel model = (SqlPhysicalModel) domain.getPhysicalModels().get(0);
        String query = model.getPhysicalTables().get(0).getTargetTable();
        SerializedResultSet resultSet = DatasourceInMemoryServiceHelper.getSerializeableResultSet(model.getDatasource().getDatabaseName(), query, 5, null);
        data = resultSet.getData();
    }
    return new BusinessData(domain, data);
}
Also used : BusinessData(org.pentaho.platform.dataaccess.datasource.beans.BusinessData) InlineEtlPhysicalModel(org.pentaho.metadata.model.InlineEtlPhysicalModel) SerializedResultSet(org.pentaho.platform.dataaccess.datasource.beans.SerializedResultSet) ArrayList(java.util.ArrayList) List(java.util.List) Domain(org.pentaho.metadata.model.Domain) SqlPhysicalModel(org.pentaho.metadata.model.SqlPhysicalModel)

Aggregations

SerializedResultSet (org.pentaho.platform.dataaccess.datasource.beans.SerializedResultSet)13 DatasourceServiceException (org.pentaho.platform.dataaccess.datasource.wizard.service.DatasourceServiceException)10 SQLException (java.sql.SQLException)6 Domain (org.pentaho.metadata.model.Domain)6 QueryValidationException (org.pentaho.platform.dataaccess.datasource.wizard.service.QueryValidationException)6 ModelerException (org.pentaho.agilebi.modeler.ModelerException)5 SqlQueriesNotSupportedException (org.pentaho.platform.dataaccess.datasource.wizard.service.SqlQueriesNotSupportedException)5 ArrayList (java.util.ArrayList)4 List (java.util.List)4 DomainAlreadyExistsException (org.pentaho.metadata.repository.DomainAlreadyExistsException)4 DomainIdNullException (org.pentaho.metadata.repository.DomainIdNullException)4 DomainStorageException (org.pentaho.metadata.repository.DomainStorageException)4 SQLModelGenerator (org.pentaho.metadata.util.SQLModelGenerator)4 SQLModelGeneratorException (org.pentaho.metadata.util.SQLModelGeneratorException)4 BusinessData (org.pentaho.platform.dataaccess.datasource.beans.BusinessData)4 IOException (java.io.IOException)3 Test (org.junit.Test)3 MondrianCatalogServiceException (org.pentaho.platform.plugin.action.mondrian.catalog.MondrianCatalogServiceException)3 FileNotFoundException (java.io.FileNotFoundException)2 ModelerWorkspace (org.pentaho.agilebi.modeler.ModelerWorkspace)2