Search in sources :

Example 1 with IPentahoConnection

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

the class HQLBaseComponent method executeAction.

@Override
protected boolean executeAction() {
    boolean returnValue = true;
    try {
        if (getActionDefinition() instanceof HQLQueryAction) {
            HQLQueryAction queryAction = (HQLQueryAction) getActionDefinition();
            String[] classNames = null;
            String query = queryAction.getQuery().getStringValue();
            if (queryAction.getInputSharedConnection() != ActionInputConstant.NULL_INPUT) {
                connectionOwner = false;
                IPreparedComponent component = (IPreparedComponent) queryAction.getInputSharedConnection().getValue();
                IPentahoConnection conn = component.shareConnection();
                if (IPentahoConnection.HQL_DATASOURCE.equals(conn.getDatasourceType())) {
                    connection = conn;
                } else {
                    connection = null;
                    returnValue = false;
                    error(Messages.getInstance().getErrorString("IPreparedComponent.ERROR_0001_INVALID_CONNECTION_TYPE", // $NON-NLS-1$
                    getActionName()));
                }
            } else {
                createBasicConnection(queryAction, classNames);
            }
            if (connection != null) {
                IActionOutput actionOutput = queryAction.getOutputPreparedStatementParam();
                if (actionOutput != null) {
                    // prepare the query for execution, but don't execute quite yet.
                    prepareQuery(query);
                    // set the output as self, which will be used later by another component.
                    actionOutput.setValue(this);
                } else {
                    return runQuery(connection, classNames, query);
                }
            }
        } else if (getActionDefinition() instanceof HQLConnectionAction) {
            HQLConnectionAction connAction = (HQLConnectionAction) getActionDefinition();
            String[] classNames = null;
            createBasicConnection(connAction, classNames);
            if (connection != null) {
                IActionOutput outputConnection = connAction.getOutputConnectionParam();
                if (outputConnection != null) {
                    outputConnection.setValue(this);
                }
            }
        } else {
            returnValue = false;
            error(Messages.getInstance().getErrorString("HQLBaseComponent.ERROR_00011_INVALID_HQL_COMPONENT", // $NON-NLS-1$
            getActionName()));
        }
    } catch (Exception e) {
        returnValue = false;
        // $NON-NLS-1$
        error(Messages.getInstance().getErrorString("HQLBaseComponent.ERROR_00012_EXECUTE_FAILED", getActionName()), e);
    }
    return returnValue;
}
Also used : IPentahoConnection(org.pentaho.commons.connection.IPentahoConnection) HQLQueryAction(org.pentaho.actionsequence.dom.actions.HQLQueryAction) IActionOutput(org.pentaho.actionsequence.dom.IActionOutput) HQLConnectionAction(org.pentaho.actionsequence.dom.actions.HQLConnectionAction) IPreparedComponent(org.pentaho.platform.api.data.IPreparedComponent)

Example 2 with IPentahoConnection

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

the class HQLBaseComponent method getConnection.

protected IPentahoConnection getConnection(final File hbmCfgFile, final String[] classNames) {
    IPentahoConnection conn = null;
    try {
        // $NON-NLS-1$
        conn = (HQLConnection) PentahoConnectionFactory.getConnection("HQL", getSession(), this);
        HQLConnection hconn = (HQLConnection) conn;
        hconn.setConfigFile(hbmCfgFile);
        hconn.setClassNames(classNames);
        return conn;
    } catch (Exception e) {
        error(Messages.getInstance().getErrorString("HQLBaseComponent.ERROR_0009_COULD_NOT_ESTABLISH_CONNECTION", getActionName()), // $NON-NLS-1$
        e);
    }
    return null;
}
Also used : IPentahoConnection(org.pentaho.commons.connection.IPentahoConnection) HQLConnection(org.pentaho.platform.plugin.services.connections.hql.HQLConnection)

Example 3 with IPentahoConnection

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

the class ConnectionIT method testMDX_VFS_zipped_Schema.

/**
 * This test currently fails, due to Mondrian not understanding the catalog URL. I'm guessing we're not using the
 * latest mondrian libs that support mondrian
 */
@SuppressWarnings("deprecation")
public void testMDX_VFS_zipped_Schema() {
    startTest();
    // StandaloneSession session = new StandaloneSession(Messages.getString("BaseTest.DEBUG_JUNIT_SESSION")); //$NON-NLS-1$
    // SolutionRepositoryVfs.setSolutionRepository( PentahoSystem.getSolutionRepository( session ) );
    IPentahoSession session = new StandaloneSession("Admin");
    // $NON-NLS-1$ //$NON-NLS-2$
    OutputStream outputStream = this.getOutputStream("ConnectionTest.testSQLConnection", ".csv");
    File file = // $NON-NLS-1$
    new File(PentahoSystem.getApplicationContext().getSolutionPath("test/datasources/SampleDataSchema.zip"));
    // $NON-NLS-1$ //$NON-NLS-2$
    String catalog = "zip:" + file.toURI().toString() + "!/SampleData.mondrian.xml";
    // $NON-NLS-1$
    catalog = "solution:/test/datasources/SampleData.mondrian.xml;vfs=true";
    IPentahoConnection connection = PentahoConnectionFactory.getConnection(IPentahoConnection.MDX_DATASOURCE, "jdbc:hsqldb:hsql://localhost:9001/sampledata; Catalog=" + catalog, "mondrian", "sa", "", session, // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
    null);
    String query = // $NON-NLS-1$
    "select {[Measures].[Actual], [Measures].[Budget]} on rows, {[Region].[All Regions]} ON columns from [Quadrant Analysis] WHERE ([Positions].[All Positions])";
    try {
        IPentahoResultSet results = connection.executeQuery(query);
        Object[][] columnHeaders = results.getMetaData().getColumnHeaders();
        for (int row = columnHeaders.length - 1; row >= 0; row--) {
            for (int col = 0; col < columnHeaders[row].length; col++) {
                // $NON-NLS-1$
                outputStream.write((columnHeaders[row][col] + "\t").getBytes());
            }
            outputStream.write('\n');
        }
        Object[][] rowHeaders = results.getMetaData().getRowHeaders();
        int rowIdx = 0;
        Object[] row = results.next();
        while (row != null) {
            for (int colIdx = rowHeaders[rowIdx].length - 1; colIdx >= 0; colIdx--) {
                // $NON-NLS-1$
                outputStream.write((rowHeaders[rowIdx][colIdx].toString() + "\t").getBytes());
            }
            for (int colIdx = 0; colIdx < row.length; colIdx++) {
                // $NON-NLS-1$
                outputStream.write((row[colIdx] + "\t").getBytes());
            }
            outputStream.write('\n');
            row = results.next();
            rowIdx++;
        }
        results.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    connection.close();
    finishTest();
}
Also used : IPentahoConnection(org.pentaho.commons.connection.IPentahoConnection) IPentahoResultSet(org.pentaho.commons.connection.IPentahoResultSet) StandaloneSession(org.pentaho.platform.engine.core.system.StandaloneSession) IPentahoSession(org.pentaho.platform.api.engine.IPentahoSession) OutputStream(java.io.OutputStream) File(java.io.File)

Example 4 with IPentahoConnection

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

the class ConnectionIT method testSQLConnectionWithAllInfo.

public void testSQLConnectionWithAllInfo() {
    startTest();
    IPentahoSession session = new StandaloneSession("Admin");
    // $NON-NLS-1$ //$NON-NLS-2$
    OutputStream outputStream = this.getOutputStream("ConnectionTest.testConnectionWithPropertyName", ".csv");
    File file = // $NON-NLS-1$
    new File(PentahoSystem.getApplicationContext().getSolutionPath("test/datasources/SampleData.mondrian.xml"));
    IPentahoConnection connection = PentahoConnectionFactory.getConnection(IPentahoConnection.MDX_DATASOURCE, "jdbc:hsqldb:hsql://localhost:9001/sampledata; Catalog=" + file.toURI().toString(), "mondrian", "sa", "", session, // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
    this);
    try {
        // $NON-NLS-1$
        IPentahoResultSet results = connection.executeQuery("select * from DEPARTMENT_MANAGERS");
        Object[][] columnHeaders = results.getMetaData().getColumnHeaders();
        for (int row = 0; row < columnHeaders.length; row++) {
            for (int col = 0; col < columnHeaders[0].length; col++) {
                outputStream.write(columnHeaders[row][col].toString().getBytes());
                // $NON-NLS-1$
                outputStream.write(",".getBytes());
            }
            // $NON-NLS-1$
            outputStream.write("\n".getBytes());
        }
        Object[] row = results.next();
        while (row != null) {
            for (int i = 0; i < row.length; i++) {
                outputStream.write(row[i].toString().getBytes());
                // $NON-NLS-1$
                outputStream.write(",".getBytes());
            }
            // $NON-NLS-1$
            outputStream.write("\n".getBytes());
            row = results.next();
        }
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    connection.close();
    finishTest();
}
Also used : IPentahoConnection(org.pentaho.commons.connection.IPentahoConnection) IPentahoResultSet(org.pentaho.commons.connection.IPentahoResultSet) StandaloneSession(org.pentaho.platform.engine.core.system.StandaloneSession) IPentahoSession(org.pentaho.platform.api.engine.IPentahoSession) OutputStream(java.io.OutputStream) File(java.io.File)

Example 5 with IPentahoConnection

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

the class MDXBaseComponentTest method testGetDatasourceConnection.

@Test
public void testGetDatasourceConnection() throws Exception {
    doReturn(conn).when(mdxBaseComponent).getConnection();
    IPentahoConnection datasourceConnection = mdxBaseComponent.getDatasourceConnection();
    assertEquals(conn, datasourceConnection);
    verify(conn).clearWarnings();
}
Also used : IPentahoConnection(org.pentaho.commons.connection.IPentahoConnection) Test(org.junit.Test)

Aggregations

IPentahoConnection (org.pentaho.commons.connection.IPentahoConnection)29 IPentahoSession (org.pentaho.platform.api.engine.IPentahoSession)11 Test (org.junit.Test)9 IPentahoResultSet (org.pentaho.commons.connection.IPentahoResultSet)9 Properties (java.util.Properties)6 OutputStream (java.io.OutputStream)5 StandaloneSession (org.pentaho.platform.engine.core.system.StandaloneSession)5 File (java.io.File)4 IDBDatasourceService (org.pentaho.platform.api.data.IDBDatasourceService)3 IPreparedComponent (org.pentaho.platform.api.data.IPreparedComponent)3 UnifiedRepositoryException (org.pentaho.platform.api.repository2.unified.UnifiedRepositoryException)3 IActionOutput (org.pentaho.actionsequence.dom.IActionOutput)2 AbstractRelationalDbAction (org.pentaho.actionsequence.dom.actions.AbstractRelationalDbAction)2 MdxConnectionAction (org.pentaho.actionsequence.dom.actions.MdxConnectionAction)2 SqlConnectionAction (org.pentaho.actionsequence.dom.actions.SqlConnectionAction)2 DatabaseDialectException (org.pentaho.database.DatabaseDialectException)2 ConnectionServiceException (org.pentaho.platform.dataaccess.datasource.wizard.service.ConnectionServiceException)2 FileInputStream (java.io.FileInputStream)1 Format (java.text.Format)1 ArrayList (java.util.ArrayList)1