use of org.pentaho.commons.connection.IPentahoMetaData 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.commons.connection.IPentahoMetaData 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.commons.connection.IPentahoMetaData in project data-access by pentaho.
the class DatasourceServiceHelper method convertToMemoryResultSet.
/**
* Convert the live result set to memory result set.
*
* @param resultSet
* @return
*/
private static IPentahoResultSet convertToMemoryResultSet(IPentahoResultSet resultSet) throws SQLException {
MemoryResultSet cachedResultSet = null;
try {
IPentahoMetaData meta = resultSet.getMetaData();
Object[][] columnHeaders = meta.getColumnHeaders();
MemoryMetaData cachedMetaData = new MemoryMetaData(columnHeaders, null);
String[] colTypesAsString;
// If the IPentahoMetaData is an instanceof SQLMetaData then get the column types from the metadata
if (meta instanceof SQLMetaData) {
SQLMetaData sqlMeta = (SQLMetaData) meta;
// Column Types in SQLMetaData are int. MemoryMetaData stores column types as string. So we will store them
// as string in MemoryMetaData
int[] colTypes = sqlMeta.getJDBCColumnTypes();
colTypesAsString = new String[colTypes.length];
for (int i = 0; i < colTypes.length; i++) {
colTypesAsString[i] = Integer.toString(colTypes[i]);
}
cachedMetaData.setColumnTypes(colTypesAsString);
}
cachedResultSet = new MemoryResultSet(cachedMetaData);
Object[] rowObjects = resultSet.next();
while (rowObjects != null) {
cachedResultSet.addRow(rowObjects);
rowObjects = resultSet.next();
}
} finally {
resultSet.close();
}
return cachedResultSet;
}
use of org.pentaho.commons.connection.IPentahoMetaData in project pentaho-platform by pentaho.
the class SelectionMapper method create.
/**
* Creates a SelectionMapper based on an IPentahoResultSet. The columns to use for the values and display names
* are passed in as column names.
*
* @param resultSet
* The result set to get the data from
* @param valueColName
* The name of the column to use for the values. If null, the first column will be used
* @param dispColName
* The name of the column to use for the display names. If null, the values column will be used
* @param displayName
* The name used to describe the choice for this selection. Usually used as a header
* @return SelectionMapper if successful or null
*/
public static SelectionMapper create(final IPentahoResultSet resultSet, final String valueColName, final String dispColName, final String displayName, final String displayStyle) {
if (resultSet == null) {
return (null);
}
IPentahoMetaData metaData = resultSet.getMetaData();
if ((metaData == null) || (metaData.getColumnCount() < 1)) {
// TODO surface an error
return (null);
}
int valueColumnNo = (valueColName == null) ? 0 : metaData.getColumnIndex(valueColName);
if (valueColumnNo < 0) {
// TODO surface an error
return (null);
}
int dispColumnNo = -1;
if (dispColName != null) {
dispColumnNo = metaData.getColumnIndex(dispColName);
if (dispColumnNo < 0) {
// TODO surface an error
return (null);
}
}
return (SelectionMapper.create(resultSet, ++valueColumnNo, ++dispColumnNo, displayName, displayStyle));
}
use of org.pentaho.commons.connection.IPentahoMetaData in project pentaho-platform by pentaho.
the class MapParameterResolver method resolveParameter.
/**
* This method is called when TemplateUtil.applyTemplate() encounters a parameter.
*
* @param template
* the source string
* @param parameter
* the parameter value
* @param parameterMatcher
* the regex parameter matcher
* @param copyStart
* the start of the copy
* @param results
* the output result
* @return the next copystart
*/
public int resolveParameter(final String template, final String parameter, final Matcher parameterMatcher, int copyStart, final StringBuffer results) {
// $NON-NLS-1$
StringTokenizer tokenizer = new StringTokenizer(parameter, ":");
if (tokenizer.countTokens() == 2) {
// Currently, the component only handles one bit of metadata
String parameterPrefix = tokenizer.nextToken();
String inputName = tokenizer.nextToken();
if (parameterPrefix.equals(prefix)) {
// We know this parameter is for us.
// First, is this a special input
Object parameterValue = TemplateUtil.getSystemInput(inputName, runtimecontext);
if ((parameterValue == null) && lookupMap.containsKey(inputName)) {
parameterValue = lookupMap.get(inputName);
}
if (parameterValue != null) {
// We have a parameter value - now, it's time to create a parameter and build up the
// parameter string
int start = parameterMatcher.start();
int end = parameterMatcher.end();
// We now have a valid start and end. It's time to see whether we're dealing
// with an array, a result set, or a scalar.
StringBuffer parameterBuffer = new StringBuffer();
if (parameterValue instanceof String) {
// $NON-NLS-1$ //$NON-NLS-2$
parameterBuffer.append(((String) parameterValue).replaceAll("'", "\\'"));
} else if (parameterValue instanceof Object[]) {
Object[] pObj = (Object[]) parameterValue;
for (Object element : pObj) {
// TODO: escape quotes!
// $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
parameterBuffer.append((parameterBuffer.length() == 0) ? "'" + element + "'" : ",'" + element + "'");
}
} else if (parameterValue instanceof IPentahoResultSet) {
IPentahoResultSet rs = (IPentahoResultSet) parameterValue;
// See if we can find a column in the metadata with the same
// name as the input
IPentahoMetaData md = rs.getMetaData();
int columnIdx = -1;
if (md.getColumnCount() == 1) {
columnIdx = 0;
} else {
columnIdx = md.getColumnIndex(new String[] { parameter });
}
if (columnIdx < 0) {
// $NON-NLS-1$
error(Messages.getInstance().getErrorString("Template.ERROR_0005_COULD_NOT_DETERMINE_COLUMN"));
return -1;
}
int rowCount = rs.getRowCount();
Object valueCell = null;
// TODO support non-string columns
for (int i = 0; i < rowCount; i++) {
valueCell = rs.getValueAt(i, columnIdx);
// TODO: escape quotes!
parameterBuffer.append((parameterBuffer.length() == 0) ? "'" + valueCell + "'" : // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
",'" + valueCell + "'");
}
} else if (parameterValue instanceof List) {
List pObj = (List) parameterValue;
for (int i = 0; i < pObj.size(); i++) {
parameterBuffer.append((parameterBuffer.length() == 0) ? "'" + pObj.get(i) + "'" : // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
",'" + pObj.get(i) + "'");
}
} else {
// If we're here, we know parameterValue is not null and not a string
// $NON-NLS-1$ //$NON-NLS-2$
parameterBuffer.append(parameterValue.toString().replaceAll("'", "\\'"));
}
// OK - We have a parameterBuffer and have filled out the preparedParameters
// list. It's time to change the SQL to insert our parameter marker and tell
// the caller we've done our job.
results.append(template.substring(copyStart, start));
copyStart = end;
results.append(parameterBuffer);
return copyStart;
}
}
}
// Nothing here for us - let default behavior through
return -1;
}
Aggregations