Search in sources :

Example 26 with IPentahoConnection

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

the class ConnectionIT method testMDXConnectionWithPropertiesFile.

// public void testSQLConnection() {
// startTest();
// OutputStream outputStream = this.getOutputStream("ConnectionTest.testSQLConnection", ".csv"); //$NON-NLS-1$ //$NON-NLS-2$
// IPentahoSession session = new StandaloneSession("Admin");
// IPentahoConnection connection = PentahoConnectionFactory.getConnection(IPentahoConnection.SQL_DATASOURCE, session, this); //$NON-NLS-1$
// try {
// IPentahoResultSet results = connection.executeQuery("select * from DEPARTMENT_MANAGERS"); //$NON-NLS-1$
// 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());
// outputStream.write(",".getBytes()); //$NON-NLS-1$
// }
// outputStream.write("\n".getBytes()); //$NON-NLS-1$
// }
// Object[] row = results.next();
// while (row != null) {
// for (int i = 0; i < row.length; i++) {
// outputStream.write(row[i].toString().getBytes());
// outputStream.write(",".getBytes()); //$NON-NLS-1$
// }
// outputStream.write("\n".getBytes()); //$NON-NLS-1$
// row = results.next();
// }
// 
// } catch (Exception e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
// connection.close();
// finishTest();
// }
/*
   * public void testConnectionWithPropertyName() { startTest(); OutputStream outputStream =
   * this.getOutputStream("ConnectionTest.testConnectionWithPropertyName", ".csv"); //$NON-NLS-1$ //$NON-NLS-2$
   * 
   * IPentahoConnection connection = PentahoConnectionFactory.getConnection("datasource", this); //$NON-NLS-1$ // @TODO
   * Need to know how to use this getConnection method. Where does this property name comes from
   * 
   * try { IPentahoResultSet results = connection.executeQuery("select * from DEPARTMENT_MANAGERS"); //$NON-NLS-1$
   * 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()); outputStream.write(",".getBytes());
   * //$NON-NLS-1$ } outputStream.write("\n".getBytes()); //$NON-NLS-1$ } Object[] row = results.next(); while (row !=
   * null) { for (int i = 0; i < row.length; i++) { outputStream.write(row[i].toString().getBytes());
   * outputStream.write(",".getBytes()); //$NON-NLS-1$ } outputStream.write("\n".getBytes()); //$NON-NLS-1$ row =
   * results.next(); }
   * 
   * } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } connection.close(); finishTest();
   * }
   */
public void testMDXConnectionWithPropertiesFile() {
    startTest();
    // $NON-NLS-1$ //$NON-NLS-2$
    OutputStream outputStream = this.getOutputStream("ConnectionTest.testMDXConnectionWithPropertiesFile", ".csv");
    IPentahoSession session = new StandaloneSession("Admin");
    Properties mdxProperties = new Properties();
    try {
        // $NON-NLS-1$
        mdxProperties.load(new FileInputStream("mdxConnection.properties"));
    } catch (Exception e) {
        e.printStackTrace();
    }
    IPentahoConnection connection = PentahoConnectionFactory.getConnection(IPentahoConnection.MDX_DATASOURCE, mdxProperties, session, 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) Properties(java.util.Properties) FileInputStream(java.io.FileInputStream)

Example 27 with IPentahoConnection

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

the class ConnectionIT method testXQueryConnection.

public void testXQueryConnection() {
    startTest();
    IPentahoSession session = new StandaloneSession("Admin");
    // $NON-NLS-1$ //$NON-NLS-2$
    OutputStream outputStream = this.getOutputStream("ConnectionTest.testSQLConnection", ".csv");
    try {
        IPentahoConnection connection = PentahoConnectionFactory.getConnection(IPentahoConnection.XML_DATASOURCE, session, this);
        String query = // $NON-NLS-1$ //$NON-NLS-2$//$NON-NLS-3$
        "doc(\"" + PentahoSystem.getApplicationContext().getSolutionPath("samples/datasources/books.xml") + "\")/bookstore/book";
        query = query.replace('\\', '/');
        IPentahoResultSet results = connection.executeQuery(query);
        assertNotNull(results);
        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());
                outputStream.write(',');
            }
            outputStream.write('\n');
        }
        Object[] row = results.next();
        while (row != null) {
            for (int i = 0; i < row.length; i++) {
                outputStream.write(row[i].toString().getBytes());
                outputStream.write(',');
            }
            outputStream.write('\n');
            row = results.next();
        }
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    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)

Example 28 with IPentahoConnection

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

the class ConnectionIT method testMDXConnection.

/**
 */
@SuppressWarnings("deprecation")
public void testMDXConnection() {
    startTest();
    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/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$
    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 29 with IPentahoConnection

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

the class ConnectionServiceImpl method testConnection.

public boolean testConnection(IDatabaseConnection connection) throws ConnectionServiceException {
    ensureDataAccessPermission();
    if (connection != null) {
        sanitizer.sanitizeConnectionParameters(connection);
        if (connection.getPassword() == null) {
            // Can have an empty password but not a null one
            // $NON-NLS-1$
            connection.setPassword("");
        }
        IDatabaseDialect dialect = dialectService.getDialect(connection);
        String driverClass = null;
        if (connection.getDatabaseType().getShortName().equals("GENERIC")) {
            driverClass = connection.getAttributes().get(GenericDatabaseDialect.ATTRIBUTE_CUSTOM_DRIVER_CLASS);
        } else {
            driverClass = dialect.getNativeDriver();
        }
        IPentahoConnection pentahoConnection = null;
        try {
            if (connection.getAccessType().equals(DatabaseAccessType.JNDI)) {
                pentahoConnection = PentahoConnectionFactory.getConnection(IPentahoConnection.SQL_DATASOURCE, connection.getDatabaseName(), null, this);
            } else {
                if (connection.isUsingConnectionPool()) {
                    Properties props = new Properties();
                    props.put(IPentahoConnection.CONNECTION_NAME, connection.getName());
                    props.put(IPentahoConnection.CONNECTION, connection);
                    pentahoConnection = PentahoConnectionFactory.getConnection(IPentahoConnection.SQL_DATASOURCE, props, null, this);
                } else {
                    pentahoConnection = PentahoConnectionFactory.getConnection(IPentahoConnection.SQL_DATASOURCE, driverClass, dialect.getURLWithExtraOptions(connection), connection.getUsername(), getConnectionPassword(connection.getName(), connection.getPassword()), null, this);
                }
            }
        } catch (DatabaseDialectException e) {
            throw new ConnectionServiceException(e);
        }
        if (pentahoConnection != null) {
            // make sure we have a native connection behind the SQLConnection object
            // if the native connection is null, the test of the connection failed
            boolean testedOk = ((SQLConnection) pentahoConnection).getNativeConnection() != null;
            pentahoConnection.close();
            return testedOk;
        } else {
            return false;
        }
    } else {
        // $NON-NLS-1$
        String message = Messages.getErrorString("ConnectionServiceImpl.ERROR_0008_UNABLE_TO_TEST_NULL_CONNECTION");
        logger.error(message);
        // $NON-NLS-1$
        throw new ConnectionServiceException(Response.SC_BAD_REQUEST, message);
    }
}
Also used : IPentahoConnection(org.pentaho.commons.connection.IPentahoConnection) ConnectionServiceException(org.pentaho.platform.dataaccess.datasource.wizard.service.ConnectionServiceException) DatabaseDialectException(org.pentaho.database.DatabaseDialectException) IDatabaseDialect(org.pentaho.database.IDatabaseDialect) Properties(java.util.Properties)

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