use of org.pentaho.commons.connection.memory.MemoryMetaData in project pentaho-platform by pentaho.
the class XQueryIT method testIterators.
public void testIterators() {
MemoryMetaData metadata = new MemoryMetaData(new String[][] { { "col1", "col2" } }, null);
MemoryResultSet dataSet = new MemoryResultSet(metadata);
JavaScriptResultSet data = new JavaScriptResultSet();
data.setResultSet(dataSet);
data.addRow(new Object[] { "a", new Integer(1) });
data.addRow(new Object[] { "b", new Integer(2) });
data.addRow(new Object[] { "c", new Integer(3) });
assertEquals(3, data.getRowCount());
assertEquals("a", data.getValueAt(0, 0));
assertEquals(1, data.getValueAt(0, 1));
assertEquals("b", data.getValueAt(1, 0));
assertEquals(2, data.getValueAt(1, 1));
assertEquals("c", data.getValueAt(2, 0));
assertEquals(3, data.getValueAt(2, 1));
assertEquals("a", data.next()[0]);
assertEquals("b", data.next()[0]);
assertEquals("c", data.next()[0]);
assertNull(data.next());
data.beforeFirst();
assertEquals("a", data.next()[0]);
assertEquals("b", data.next()[0]);
assertEquals("c", data.next()[0]);
assertNull(data.next());
data.close();
assertEquals("a", data.next()[0]);
assertEquals("b", data.next()[0]);
assertEquals("c", data.next()[0]);
assertNull(data.next());
data.closeConnection();
assertEquals("a", data.next()[0]);
assertEquals("b", data.next()[0]);
assertEquals("c", data.next()[0]);
assertNull(data.next());
data.dispose();
assertEquals("a", data.next()[0]);
assertEquals("b", data.next()[0]);
assertEquals("c", data.next()[0]);
assertNull(data.next());
}
use of org.pentaho.commons.connection.memory.MemoryMetaData in project pdi-platform-plugin by pentaho.
the class PdiActionTest method testTransformationInjector.
@Test
public void testTransformationInjector() throws Exception {
String[][] columnNames = { { "REGION", "DEPARTMENT", "POSITIONTITLE" } };
MemoryMetaData metadata = new MemoryMetaData(columnNames, null);
MemoryResultSet rowsIn = new MemoryResultSet(metadata);
rowsIn.addRow(new Object[] { "abc", "123", "bogus" });
rowsIn.addRow(new Object[] { "region2", "Sales", "bad" });
rowsIn.addRow(new Object[] { "Central", "Sales", "test title" });
rowsIn.addRow(new Object[] { "Central", "xyz", "bad" });
PdiAction component = new PdiAction();
component.setRepositoryName(KettleFileRepositoryMeta.REPOSITORY_TYPE_ID);
component.setDirectory(SOLUTION_REPOSITORY);
component.setTransformation("/org/pentaho/platform/plugin/kettle/PdiActionTest_testTransformationInjector.ktr");
component.setInjectorRows(rowsIn);
component.setInjectorStep("Injector");
component.setMonitorStep("Output");
component.execute();
assertEquals(1, component.getTransformationOutputRowsCount());
assertEquals(0, component.getTransformationOutputErrorRowsCount());
IPentahoResultSet rows = component.getTransformationOutputRows();
assertNotNull(rows);
assertEquals(1, rows.getRowCount());
assertEquals("Central", rows.getValueAt(0, 0));
assertEquals("Sales", rows.getValueAt(0, 1));
assertEquals("test title", rows.getValueAt(0, 2));
assertEquals("Hello, test title", rows.getValueAt(0, 3));
rows = component.getTransformationOutputErrorRows();
assertNotNull(rows);
String log = component.getLog();
assertTrue(log.contains("Injector"));
assertTrue(log.contains("R=1"));
assertTrue(log.contains("Filter rows"));
assertTrue(log.contains("W=1"));
assertTrue(log.contains("Java Script Value"));
assertTrue(log.contains("W=1"));
assertTrue(log.contains("Output"));
assertTrue(log.contains("W=4"));
}
use of org.pentaho.commons.connection.memory.MemoryMetaData 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.memory.MemoryMetaData 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.memory.MemoryMetaData in project pentaho-platform by pentaho.
the class HQLResultSet method memoryCopy.
public IPentahoResultSet memoryCopy() {
try {
IPentahoMetaData localMetadata = getMetaData();
Object[][] columnHeaders = localMetadata.getColumnHeaders();
MemoryMetaData cachedMetaData = new MemoryMetaData(columnHeaders, null);
MemoryResultSet cachedResultSet = new MemoryResultSet(cachedMetaData);
Object[] rowObjects = next();
while (rowObjects != null) {
cachedResultSet.addRow(rowObjects);
rowObjects = next();
}
return cachedResultSet;
} finally {
close();
}
}
Aggregations