use of org.pentaho.commons.connection.marshal.MarshallableRow in project pentaho-platform by pentaho.
the class ResultSetTest method validate.
private void validate(MarshallableResultSet result) {
MarshallableColumnNames colNames = result.getColumnNames();
assertNotNull(colNames);
String[] cols = colNames.getColumnName();
assertNotNull(cols);
assertEquals(2, cols.length);
assertEquals("col1", cols[0]);
assertEquals("col2", cols[1]);
MarshallableColumnTypes colTypes = result.getColumnTypes();
assertNotNull(colTypes);
String[] types = colTypes.getColumnType();
assertNotNull(types);
assertEquals(2, types.length);
assertEquals("string", types[0]);
assertEquals("integer", types[1]);
MarshallableRow[] rows = result.getRows();
assertNotNull(rows);
assertEquals(3, rows.length);
MarshallableRow row = rows[0];
assertNotNull(row);
String[] cells = row.getCell();
assertEquals(2, cells.length);
assertEquals("a", cells[0]);
assertEquals("1", cells[1]);
row = rows[1];
assertNotNull(row);
cells = row.getCell();
assertEquals(2, cells.length);
assertEquals("b", cells[0]);
assertEquals("2", cells[1]);
row = rows[2];
assertNotNull(row);
cells = row.getCell();
assertEquals(2, cells.length);
assertEquals("c", cells[0]);
assertEquals("3", cells[1]);
}
use of org.pentaho.commons.connection.marshal.MarshallableRow 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.MarshallableRow 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.MarshallableRow 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));
}
use of org.pentaho.commons.connection.marshal.MarshallableRow in project data-access by pentaho.
the class MetadataServiceTest method testDoXmlQuery.
@Test
public void testDoXmlQuery() {
String xmlQuery = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><mql><domain_id>" + DOMAIN_ID + "</domain_id><model_id" + ">" + LOGICAL_MODEL_ID + "</model_id><options><disable_distinct>false</disable_distinct><limit>-1</limit></options" + "><parameters><parameter defaultValue=\"" + VALUE + "\" name=\"" + COLUMN_NAME + "\" " + "type=\"STRING\"/></parameters><selections><selection><view>" + CATEGORY_ID + "</view><column>" + COLUMN_ID + "</column" + "><aggregation>NONE</aggregation></selection></selections><constraints><constraint><operator>AND</operator" + "><condition>[" + CATEGORY_NAME + "." + COLUMN_NAME + "] = " + VALUE + "</condition></constraint></constraints><orders/></mql>";
when(metadataService.doXmlQuery(anyString(), any(Integer.class))).thenCallRealMethod();
MarshallableResultSet marshallableResultSet = getMarshallableResultSet();
when(metadataService.getMarshallableResultSet()).thenReturn(marshallableResultSet);
MarshallableResultSet marshallableResultSetReturned = metadataService.doXmlQuery(xmlQuery, 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