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();
}
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();
}
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();
}
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);
}
}
Aggregations