use of org.pentaho.platform.plugin.services.connections.sql.SQLConnection in project data-access by pentaho.
the class DatasourceInMemoryServiceHelper method getConnection.
public static SQLConnection getConnection(String connectionName) throws DatasourceServiceException {
IDatabaseConnection connection = null;
try {
ConnectionServiceImpl service = new ConnectionServiceImpl();
connection = service.getConnectionByName(connectionName);
DatabaseDialectService dialectService = new DatabaseDialectService();
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();
}
return new SQLConnection(driverClass, dialect.getURLWithExtraOptions(connection), connection.getUsername(), connection.getPassword(), null);
} catch (ConnectionServiceException e1) {
return null;
} catch (DatabaseDialectException e) {
return null;
}
}
use of org.pentaho.platform.plugin.services.connections.sql.SQLConnection 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;
}
use of org.pentaho.platform.plugin.services.connections.sql.SQLConnection 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;
}
use of org.pentaho.platform.plugin.services.connections.sql.SQLConnection in project data-access by pentaho.
the class ConnectionServiceImpl method addConnection.
public boolean addConnection(IDatabaseConnection connection) throws ConnectionServiceException {
ensureDataAccessPermission();
try {
if (connection.getAccessType() != null && connection.getAccessType().equals(DatabaseAccessType.JNDI)) {
IPentahoConnection pentahoConnection = null;
pentahoConnection = PentahoConnectionFactory.getConnection(IPentahoConnection.SQL_DATASOURCE, connection.getDatabaseName(), null, this);
try {
connection.setUsername((((SQLConnection) pentahoConnection).getNativeConnection().getMetaData().getUserName()));
} catch (Exception e) {
logger.warn("Unable to get username from datasource: " + connection.getName());
}
}
datasourceMgmtSvc.createDatasource(connection);
return true;
} catch (DuplicateDatasourceException duplicateDatasourceException) {
String message = Messages.getErrorString(// $NON-NLS-1$
"ConnectionServiceImpl.ERROR_0004_UNABLE_TO_ADD_CONNECTION", connection.getName(), duplicateDatasourceException.getLocalizedMessage());
logger.error(message);
throw new ConnectionServiceException(Response.SC_CONFLICT, message, duplicateDatasourceException);
} catch (Exception e) {
String message = Messages.getErrorString(// $NON-NLS-1$
"ConnectionServiceImpl.ERROR_0004_UNABLE_TO_ADD_CONNECTION", connection.getName(), e.getLocalizedMessage());
logger.error(message);
throw new ConnectionServiceException(message, e);
}
}
use of org.pentaho.platform.plugin.services.connections.sql.SQLConnection in project data-access by pentaho.
the class InMemoryDSWDatasourceServiceImpl method executeQuery.
private IPentahoResultSet executeQuery(String connectionName, String query, String previewLimit) throws QueryValidationException {
SQLConnection sqlConnection = null;
int limit = (previewLimit != null && previewLimit.length() > 0) ? Integer.parseInt(previewLimit) : -1;
try {
sqlConnection = DatasourceInMemoryServiceHelper.getConnection(connectionName);
sqlConnection.setMaxRows(limit);
sqlConnection.setReadOnly(true);
return sqlConnection.executeQuery(BEFORE_QUERY + query + AFTER_QUERY);
} catch (Exception e) {
logger.error(Messages.getErrorString("InMemoryDatasourceServiceImpl.ERROR_0009_QUERY_VALIDATION_FAILED", e.getLocalizedMessage()), // $NON-NLS-1$
e);
// $NON-NLS-1$
throw new QueryValidationException(e.getLocalizedMessage(), e);
} finally {
if (sqlConnection != null) {
sqlConnection.close();
}
}
}
Aggregations