Search in sources :

Example 6 with IPentahoMetaData

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

use of org.pentaho.commons.connection.IPentahoMetaData 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 8 with IPentahoMetaData

use of org.pentaho.commons.connection.IPentahoMetaData 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 9 with IPentahoMetaData

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

the class SelectionMapper method create.

/**
 * Creates a SelectionMapper based on an IPentahoResultSet. The columns to use for the values and display names
 * are passed in as column names.
 *
 * @param resultSet
 *          The result set to get the data from
 * @param valueColName
 *          The name of the column to use for the values. If null, the first column will be used
 * @param dispColName
 *          The name of the column to use for the display names. If null, the values column will be used
 * @param displayName
 *          The name used to describe the choice for this selection. Usually used as a header
 * @return SelectionMapper if successful or null
 */
public static SelectionMapper create(final IPentahoResultSet resultSet, final String valueColName, final String dispColName, final String displayName, final String displayStyle) {
    if (resultSet == null) {
        return (null);
    }
    IPentahoMetaData metaData = resultSet.getMetaData();
    if ((metaData == null) || (metaData.getColumnCount() < 1)) {
        // TODO surface an error
        return (null);
    }
    int valueColumnNo = (valueColName == null) ? 0 : metaData.getColumnIndex(valueColName);
    if (valueColumnNo < 0) {
        // TODO surface an error
        return (null);
    }
    int dispColumnNo = -1;
    if (dispColName != null) {
        dispColumnNo = metaData.getColumnIndex(dispColName);
        if (dispColumnNo < 0) {
            // TODO surface an error
            return (null);
        }
    }
    return (SelectionMapper.create(resultSet, ++valueColumnNo, ++dispColumnNo, displayName, displayStyle));
}
Also used : IPentahoMetaData(org.pentaho.commons.connection.IPentahoMetaData)

Example 10 with IPentahoMetaData

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

the class MapParameterResolver method resolveParameter.

/**
 * This method is called when TemplateUtil.applyTemplate() encounters a parameter.
 *
 * @param template
 *          the source string
 * @param parameter
 *          the parameter value
 * @param parameterMatcher
 *          the regex parameter matcher
 * @param copyStart
 *          the start of the copy
 * @param results
 *          the output result
 * @return the next copystart
 */
public int resolveParameter(final String template, final String parameter, final Matcher parameterMatcher, int copyStart, final StringBuffer results) {
    // $NON-NLS-1$
    StringTokenizer tokenizer = new StringTokenizer(parameter, ":");
    if (tokenizer.countTokens() == 2) {
        // Currently, the component only handles one bit of metadata
        String parameterPrefix = tokenizer.nextToken();
        String inputName = tokenizer.nextToken();
        if (parameterPrefix.equals(prefix)) {
            // We know this parameter is for us.
            // First, is this a special input
            Object parameterValue = TemplateUtil.getSystemInput(inputName, runtimecontext);
            if ((parameterValue == null) && lookupMap.containsKey(inputName)) {
                parameterValue = lookupMap.get(inputName);
            }
            if (parameterValue != null) {
                // We have a parameter value - now, it's time to create a parameter and build up the
                // parameter string
                int start = parameterMatcher.start();
                int end = parameterMatcher.end();
                // We now have a valid start and end. It's time to see whether we're dealing
                // with an array, a result set, or a scalar.
                StringBuffer parameterBuffer = new StringBuffer();
                if (parameterValue instanceof String) {
                    // $NON-NLS-1$ //$NON-NLS-2$
                    parameterBuffer.append(((String) parameterValue).replaceAll("'", "\\'"));
                } else if (parameterValue instanceof Object[]) {
                    Object[] pObj = (Object[]) parameterValue;
                    for (Object element : pObj) {
                        // TODO: escape quotes!
                        // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
                        parameterBuffer.append((parameterBuffer.length() == 0) ? "'" + element + "'" : ",'" + element + "'");
                    }
                } else if (parameterValue instanceof IPentahoResultSet) {
                    IPentahoResultSet rs = (IPentahoResultSet) parameterValue;
                    // See if we can find a column in the metadata with the same
                    // name as the input
                    IPentahoMetaData md = rs.getMetaData();
                    int columnIdx = -1;
                    if (md.getColumnCount() == 1) {
                        columnIdx = 0;
                    } else {
                        columnIdx = md.getColumnIndex(new String[] { parameter });
                    }
                    if (columnIdx < 0) {
                        // $NON-NLS-1$
                        error(Messages.getInstance().getErrorString("Template.ERROR_0005_COULD_NOT_DETERMINE_COLUMN"));
                        return -1;
                    }
                    int rowCount = rs.getRowCount();
                    Object valueCell = null;
                    // TODO support non-string columns
                    for (int i = 0; i < rowCount; i++) {
                        valueCell = rs.getValueAt(i, columnIdx);
                        // TODO: escape quotes!
                        parameterBuffer.append((parameterBuffer.length() == 0) ? "'" + valueCell + "'" : // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
                        ",'" + valueCell + "'");
                    }
                } else if (parameterValue instanceof List) {
                    List pObj = (List) parameterValue;
                    for (int i = 0; i < pObj.size(); i++) {
                        parameterBuffer.append((parameterBuffer.length() == 0) ? "'" + pObj.get(i) + "'" : // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
                        ",'" + pObj.get(i) + "'");
                    }
                } else {
                    // If we're here, we know parameterValue is not null and not a string
                    // $NON-NLS-1$ //$NON-NLS-2$
                    parameterBuffer.append(parameterValue.toString().replaceAll("'", "\\'"));
                }
                // OK - We have a parameterBuffer and have filled out the preparedParameters
                // list. It's time to change the SQL to insert our parameter marker and tell
                // the caller we've done our job.
                results.append(template.substring(copyStart, start));
                copyStart = end;
                results.append(parameterBuffer);
                return copyStart;
            }
        }
    }
    // Nothing here for us - let default behavior through
    return -1;
}
Also used : IPentahoResultSet(org.pentaho.commons.connection.IPentahoResultSet) StringTokenizer(java.util.StringTokenizer) List(java.util.List) IPentahoMetaData(org.pentaho.commons.connection.IPentahoMetaData)

Aggregations

IPentahoMetaData (org.pentaho.commons.connection.IPentahoMetaData)26 IPentahoResultSet (org.pentaho.commons.connection.IPentahoResultSet)12 MemoryMetaData (org.pentaho.commons.connection.memory.MemoryMetaData)9 MemoryResultSet (org.pentaho.commons.connection.memory.MemoryResultSet)7 ArrayList (java.util.ArrayList)4 List (java.util.List)4 SQLConnection (org.pentaho.platform.plugin.services.connections.sql.SQLConnection)4 IOException (java.io.IOException)3 SQLException (java.sql.SQLException)3 MarshallableResultSet (org.pentaho.commons.connection.marshal.MarshallableResultSet)3 IRuntimeContext (org.pentaho.platform.api.engine.IRuntimeContext)3 SQLMetaData (org.pentaho.platform.plugin.services.connections.sql.SQLMetaData)3 StringTokenizer (java.util.StringTokenizer)2 Element (org.dom4j.Element)2 Test (org.junit.Test)2 MarshallableRow (org.pentaho.commons.connection.marshal.MarshallableRow)2 SerializedResultSet (org.pentaho.platform.dataaccess.datasource.beans.SerializedResultSet)2 DatasourceServiceException (org.pentaho.platform.dataaccess.datasource.wizard.service.DatasourceServiceException)2 ModifiableConfiguration (org.pentaho.reporting.libraries.base.config.ModifiableConfiguration)2 FileNotFoundException (java.io.FileNotFoundException)1