use of org.pentaho.commons.connection.marshal.MarshallableResultSet in project pentaho-platform by pentaho.
the class ResultSetTest method testWSResultSet1.
public void testWSResultSet1() {
MemoryMetaData metadata = new MemoryMetaData(new String[][] { { "col1", "col2" } }, null);
metadata.setColumnTypes(new String[] { "string", "int" });
MemoryResultSet data = new MemoryResultSet(metadata);
data.addRow(new Object[] { "a", new Integer(1) });
data.addRow(new Object[] { "b", new Integer(2) });
data.addRow(new Object[] { "c", new Integer(3) });
MarshallableResultSet result = new MarshallableResultSet();
result.setResultSet(data);
validate(result);
}
use of org.pentaho.commons.connection.marshal.MarshallableResultSet 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.marshal.MarshallableResultSet 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.marshal.MarshallableResultSet in project data-access by pentaho.
the class MetadataService method doXmlQuery.
/**
* Executes a XML query and returns a serializable result set
*
* @param rowLimit An optional row limit, -1 or null means all rows
* @return
*/
public MarshallableResultSet doXmlQuery(String xml, Integer rowLimit) {
IPentahoResultSet resultSet = executeQuery(xml, rowLimit);
if (resultSet == null) {
return null;
}
MarshallableResultSet result = getMarshallableResultSet();
result.setResultSet(resultSet);
return result;
}
use of org.pentaho.commons.connection.marshal.MarshallableResultSet in project data-access by pentaho.
the class MetadataServiceTest method testDoQuery.
@Test
public void testDoQuery() {
when(metadataService.doQuery(any(Query.class), anyInt())).thenCallRealMethod();
when(metadataService.doXmlQuery(anyString(), any(Integer.class))).thenCallRealMethod();
when(metadataServiceUtil.convertQuery(any(Query.class))).thenCallRealMethod();
when(metadataServiceUtil.getCategory(anyString(), any(LogicalModel.class))).thenCallRealMethod();
MarshallableResultSet marshallableResultSet = getMarshallableResultSet();
when(metadataService.getMarshallableResultSet()).thenReturn(marshallableResultSet);
Query query = buildQuery();
MarshallableResultSet marshallableResultSetReturned = metadataService.doQuery(query, ROWS);
MarshallableRow[] rows = marshallableResultSetReturned.getRows();
String[] cell = rows[0].getCell();
MarshallableColumnNames columnNames = marshallableResultSetReturned.getColumnNames();
String[] columnName = columnNames.getColumnName();
// Check the result rows lenght
Assert.assertTrue(rows.length <= ROWS);
// Check the result row value
Assert.assertTrue(cell[0].equals(RESULT));
// Check the result column name
Assert.assertTrue(columnName[0].equals(COLUMN_NAME));
}
Aggregations