Search in sources :

Example 11 with SQLConnection

use of org.pentaho.platform.plugin.services.connections.sql.SQLConnection in project data-access by pentaho.

the class DSWDatasourceServiceImpl method executeQuery.

IPentahoResultSet executeQuery(String connectionName, String query, String previewLimit) throws QueryValidationException, SqlQueriesNotSupportedException {
    SQLConnection sqlConnection = null;
    try {
        checkSqlQueriesSupported(connectionName);
        int limit = (previewLimit != null && previewLimit.length() > 0) ? Integer.parseInt(previewLimit) : -1;
        sqlConnection = (SQLConnection) PentahoConnectionFactory.getConnection(IPentahoConnection.SQL_DATASOURCE, connectionName, PentahoSessionHolder.getSession(), new SimpleLogger(DatasourceServiceHelper.class.getName()));
        sqlConnection.setMaxRows(limit);
        sqlConnection.setReadOnly(true);
        return sqlConnection.executeQuery(BEFORE_QUERY + query + AFTER_QUERY);
    } catch (SqlQueriesNotSupportedException e) {
        logger.error(e.getLocalizedMessage());
        throw e;
    } catch (SQLException e) {
        String error = "DatasourceServiceImpl.ERROR_0009_QUERY_VALIDATION_FAILED";
        if (e.getSQLState().equals("S0021")) {
            // Column already exists
            error = "DatasourceServiceImpl.ERROR_0021_DUPLICATE_COLUMN_NAMES";
        }
        logger.error(Messages.getErrorString(error, e.getLocalizedMessage()));
        throw new QueryValidationException(Messages.getString(error, e.getLocalizedMessage()));
    } catch (Exception e) {
        logger.error(Messages.getErrorString(// $NON-NLS-1$
        "DatasourceServiceImpl.ERROR_0009_QUERY_VALIDATION_FAILED", e.getLocalizedMessage()), e);
        throw new QueryValidationException(e.getLocalizedMessage(), e);
    } finally {
        if (sqlConnection != null) {
            sqlConnection.close();
        }
    }
}
Also used : QueryValidationException(org.pentaho.platform.dataaccess.datasource.wizard.service.QueryValidationException) SQLException(java.sql.SQLException) SQLConnection(org.pentaho.platform.plugin.services.connections.sql.SQLConnection) SqlQueriesNotSupportedException(org.pentaho.platform.dataaccess.datasource.wizard.service.SqlQueriesNotSupportedException) SimpleLogger(org.pentaho.platform.util.logging.SimpleLogger) ModelerException(org.pentaho.agilebi.modeler.ModelerException) QueryValidationException(org.pentaho.platform.dataaccess.datasource.wizard.service.QueryValidationException) SQLModelGeneratorException(org.pentaho.metadata.util.SQLModelGeneratorException) DomainStorageException(org.pentaho.metadata.repository.DomainStorageException) DatasourceServiceException(org.pentaho.platform.dataaccess.datasource.wizard.service.DatasourceServiceException) DomainIdNullException(org.pentaho.metadata.repository.DomainIdNullException) SQLException(java.sql.SQLException) SqlQueriesNotSupportedException(org.pentaho.platform.dataaccess.datasource.wizard.service.SqlQueriesNotSupportedException) MondrianCatalogServiceException(org.pentaho.platform.plugin.action.mondrian.catalog.MondrianCatalogServiceException) ConnectionServiceException(org.pentaho.platform.dataaccess.datasource.wizard.service.ConnectionServiceException) DomainAlreadyExistsException(org.pentaho.metadata.repository.DomainAlreadyExistsException) IOException(java.io.IOException) CsvTransformGeneratorException(org.pentaho.platform.dataaccess.datasource.wizard.models.CsvTransformGeneratorException)

Example 12 with SQLConnection

use of org.pentaho.platform.plugin.services.connections.sql.SQLConnection in project pentaho-platform by pentaho.

the class SqlMetadataQueryExec method getActiveDatabaseMeta.

protected DatabaseMeta getActiveDatabaseMeta(DatabaseMeta databaseMeta) {
    if (getForceDbDialect() || driverClassesToForceMeta.contains(databaseMeta.getDriverClass())) {
        return databaseMeta;
    }
    // retrieve a temporary connection to determine if a dialect change is necessary
    // for generating the MQL Query.
    SQLConnection tempConnection = getConnection(databaseMeta);
    try {
        // if the connection type is not of the current dialect, regenerate the query
        DatabaseInterface di = getDatabaseInterface(tempConnection);
        if ((di != null) && (!databaseMeta.getPluginId().equals(di.getPluginId()))) {
            // we need to reinitialize our mqlQuery object and reset the query.
            // note that using this di object wipes out connection info
            DatabaseMeta meta = (DatabaseMeta) databaseMeta.clone();
            DatabaseInterface di2 = (DatabaseInterface) di.clone();
            di2.setAccessType(databaseMeta.getAccessType());
            di2.setDatabaseName(databaseMeta.getDatabaseName());
            di2.setAttributes(databaseMeta.getAttributes());
            di2.setUsername(databaseMeta.getUsername());
            di2.setPassword(databaseMeta.getPassword());
            di2.setHostname(databaseMeta.getHostname());
            meta.setDatabaseInterface(di2);
            return meta;
        } else {
            return databaseMeta;
        }
    } finally {
        if (tempConnection != null) {
            tempConnection.close();
        }
    }
}
Also used : DatabaseInterface(org.pentaho.di.core.database.DatabaseInterface) SQLConnection(org.pentaho.platform.plugin.services.connections.sql.SQLConnection) DatabaseMeta(org.pentaho.di.core.database.DatabaseMeta) GenericDatabaseMeta(org.pentaho.di.core.database.GenericDatabaseMeta)

Example 13 with SQLConnection

use of org.pentaho.platform.plugin.services.connections.sql.SQLConnection in project pentaho-platform by pentaho.

the class SQLBaseComponent method runQuery.

/**
 * executes the specified query template. The query template is first formatted and then executed. If live, the
 * original result set is made available as an output. If not live, the result set is converted into memory and the
 * connection and live result set are closed.
 *
 * @param rawQuery
 *          query template
 * @param live
 *          returns original result set if true, memory result set if false
 * @return true if successful
 */
protected boolean runQuery(final String rawQuery, boolean live) {
    try {
        if ((connection == null) || !connection.initialized()) {
            // $NON-NLS-1$
            error(Messages.getInstance().getErrorString("SQLBaseComponent.ERROR_0007_NO_CONNECTION"));
            return false;
        }
        String query = applyInputsToFormat(rawQuery);
        SQLConnection sqlConnection = null;
        if ((connection instanceof SQLConnection)) {
            sqlConnection = (SQLConnection) connection;
        }
        // Some of the following Added by Arijit Chatterjee passing the timeout value to SQLConnection class
        if (sqlConnection != null) {
            if (this.getQueryTimeout() >= 0) {
                sqlConnection.setQueryTimeout(this.getQueryTimeout());
            }
            if (this.getMaxRows() >= 0) {
                sqlConnection.setMaxRows(this.getMaxRows());
            }
            if (this.getReadOnly()) {
                sqlConnection.setReadOnly(true);
            }
        }
        AbstractRelationalDbAction relationalDbAction = (AbstractRelationalDbAction) getActionDefinition();
        IPentahoResultSet resultSet = null;
        boolean isForwardOnly = relationalDbAction.getUseForwardOnlyResultSet().getBooleanValue(false);
        resultSet = doQuery(sqlConnection, query, isForwardOnly);
        if (sqlConnection.isForcedForwardOnly()) {
            isForwardOnly = true;
            live = false;
            // $NON-NLS-1$
            warn(Messages.getInstance().getString("SQLBaseComponent.WARN_FALL_BACK_TO_NONSCROLLABLE"));
        }
        if (live) {
            // set the result set as the output
            rSet = resultSet;
            // After preparation and execution, we need to clear out the
            // prepared parameters.
            preparedParameters.clear();
            if (resultSet != null) {
                getMetadata(resultSet, true);
                IActionOutput actionOutput = relationalDbAction.getOutputResultSet();
                if (actionOutput != null) {
                    actionOutput.setValue(resultSet);
                }
                return true;
            } else {
                // close the connection if owner
                error(Messages.getInstance().getErrorString("SQLBaseComponent.ERROR_0006_EXECUTE_FAILED", // $NON-NLS-1$
                getActionName()));
                if (connectionOwner) {
                    connection.close();
                }
                return false;
            }
        } else {
            // execute the query, read the results and cache them
            try {
                // After preparation and execution, we need to clear out the
                // prepared parameters.
                preparedParameters.clear();
                IPentahoResultSet cachedResultSet = resultSet.memoryCopy();
                rSet = cachedResultSet;
                IActionOutput actionOutput = relationalDbAction.getOutputResultSet();
                if (actionOutput != null) {
                    actionOutput.setValue(cachedResultSet);
                }
            } finally {
                // close the connection if owner
                if (connectionOwner) {
                    connection.close();
                    connection = null;
                }
            }
        }
        return true;
    } catch (Exception e) {
        // $NON-NLS-1$
        error(Messages.getInstance().getErrorString("SQLBaseComponent.ERROR_0006_EXECUTE_FAILED", getActionName()), e);
    }
    return false;
}
Also used : IPentahoResultSet(org.pentaho.commons.connection.IPentahoResultSet) SQLConnection(org.pentaho.platform.plugin.services.connections.sql.SQLConnection) IActionOutput(org.pentaho.actionsequence.dom.IActionOutput) AbstractRelationalDbAction(org.pentaho.actionsequence.dom.actions.AbstractRelationalDbAction)

Aggregations

SQLConnection (org.pentaho.platform.plugin.services.connections.sql.SQLConnection)13 SQLException (java.sql.SQLException)6 IOException (java.io.IOException)5 IPentahoResultSet (org.pentaho.commons.connection.IPentahoResultSet)5 IPentahoMetaData (org.pentaho.commons.connection.IPentahoMetaData)4 ConnectionServiceException (org.pentaho.platform.dataaccess.datasource.wizard.service.ConnectionServiceException)4 DatasourceServiceException (org.pentaho.platform.dataaccess.datasource.wizard.service.DatasourceServiceException)4 ArrayList (java.util.ArrayList)3 ModelerException (org.pentaho.agilebi.modeler.ModelerException)3 DatabaseDialectException (org.pentaho.database.DatabaseDialectException)3 FileNotFoundException (java.io.FileNotFoundException)2 List (java.util.List)2 AbstractRelationalDbAction (org.pentaho.actionsequence.dom.actions.AbstractRelationalDbAction)2 MarshallableResultSet (org.pentaho.commons.connection.marshal.MarshallableResultSet)2 MarshallableRow (org.pentaho.commons.connection.marshal.MarshallableRow)2 MemoryMetaData (org.pentaho.commons.connection.memory.MemoryMetaData)2 DatabaseMeta (org.pentaho.di.core.database.DatabaseMeta)2 GenericDatabaseMeta (org.pentaho.di.core.database.GenericDatabaseMeta)2 DomainAlreadyExistsException (org.pentaho.metadata.repository.DomainAlreadyExistsException)2 DomainIdNullException (org.pentaho.metadata.repository.DomainIdNullException)2