use of org.pentaho.commons.connection.memory.MemoryMetaData in project pentaho-platform by pentaho.
the class JavaScriptResultSet method jsFunction_addRow.
public static Object jsFunction_addRow(final Context cx, final Scriptable thisObj, final Object[] args, final Function funObj) {
if (args == null) {
return null;
}
if (args.length == 0) {
return null;
}
// TODO support dates
JavaScriptResultSet resultSet = (JavaScriptResultSet) thisObj;
if ((args.length == 1) && (args[0] instanceof NativeArray)) {
NativeArray array = (NativeArray) args[0];
int length = (int) array.getLength();
Object[] row = new Object[length];
String[] columnTypes = ((MemoryMetaData) resultSet.getMetaData()).getColumnTypes();
for (int i = 0; i < length; i++) {
Object data = array.get(i, thisObj);
if (data == null) {
row[i] = null;
} else if (columnTypes != null) {
if (data instanceof NativeJavaObject) {
// see if we can force a conversion
Object outputClass = null;
if ("string".equalsIgnoreCase(columnTypes[i])) {
// $NON-NLS-1$
outputClass = java.lang.String.class;
} else if ("date".equalsIgnoreCase(columnTypes[i])) {
// $NON-NLS-1$
outputClass = java.util.Date.class;
} else if ("int".equalsIgnoreCase(columnTypes[i])) {
// $NON-NLS-1$
outputClass = java.lang.Integer.class;
} else if ("float".equalsIgnoreCase(columnTypes[i])) {
// $NON-NLS-1$
outputClass = java.lang.Float.class;
} else if ("double".equalsIgnoreCase(columnTypes[i])) {
// $NON-NLS-1$
outputClass = java.lang.Double.class;
}
if ((NativeJavaObject.canConvert(data, outputClass.getClass()))) {
row[i] = Context.jsToJava(data, java.lang.String.class);
} else {
row[i] = null;
}
}
if ("string".equalsIgnoreCase(columnTypes[i])) {
// $NON-NLS-1$
row[i] = data.toString();
} else if ("date".equalsIgnoreCase(columnTypes[i]) && (data instanceof String)) {
// $NON-NLS-1$
// $NON-NLS-1$
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
try {
row[i] = format.parse((String) data);
} catch (Throwable t) {
row[i] = null;
}
} else if ("int".equalsIgnoreCase(columnTypes[i]) && (data instanceof Integer)) {
// $NON-NLS-1$
row[i] = data;
} else if ("int".equalsIgnoreCase(columnTypes[i]) && (data instanceof Double)) {
// $NON-NLS-1$
row[i] = new Integer(((Double) data).intValue());
} else if ("int".equalsIgnoreCase(columnTypes[i]) && (data instanceof String)) {
// $NON-NLS-1$
row[i] = new Integer((String) data);
} else if ("float".equalsIgnoreCase(columnTypes[i]) && (data instanceof Double)) {
// $NON-NLS-1$
row[i] = data;
} else if ("float".equalsIgnoreCase(columnTypes[i]) && (data instanceof Integer)) {
// $NON-NLS-1$
row[i] = new Double(((Integer) data).floatValue());
} else if ("float".equalsIgnoreCase(columnTypes[i]) && (data instanceof String)) {
// $NON-NLS-1$
row[i] = new Integer((String) data);
} else if ("double".equalsIgnoreCase(columnTypes[i]) && (data instanceof Double)) {
// $NON-NLS-1$
row[i] = data;
}
} else if (data instanceof NativeJavaObject) {
Object obj = ((NativeJavaObject) data).unwrap();
row[i] = obj;
} else {
row[i] = data;
}
}
resultSet.addRow(row);
} else {
int length = args.length;
String[] row = new String[length];
for (int i = 0; i < length; i++) {
row[i] = args[i].toString();
}
resultSet.addRow(row);
}
return null;
}
use of org.pentaho.commons.connection.memory.MemoryMetaData in project pentaho-platform by pentaho.
the class MDXResultSet method memoryCopy.
public IPentahoResultSet memoryCopy() {
try {
IPentahoMetaData metadata = getMetaData();
Object[][] columnHeaders = metadata.getColumnHeaders();
Object[][] rowHeaders = metadata.getRowHeaders();
MemoryMetaData cachedMetaData = new MemoryMetaData(columnHeaders, rowHeaders);
MemoryResultSet cachedResultSet = new MemoryResultSet(cachedMetaData);
Object[] rowObjects = next();
while (rowObjects != null) {
cachedResultSet.addRow(rowObjects);
rowObjects = next();
}
return cachedResultSet;
} finally {
close();
}
}
use of org.pentaho.commons.connection.memory.MemoryMetaData in project pentaho-platform by pentaho.
the class XQResultSet method memoryCopy.
public IPentahoResultSet memoryCopy() {
try {
IPentahoMetaData metadata = getMetaData();
Object[][] columnHeaders = metadata.getColumnHeaders();
MemoryMetaData cachedMetaData = new MemoryMetaData(columnHeaders, null);
// set column types of cachedMetaData
String[] columnTypeClones = new String[columnTypes.length];
System.arraycopy(columnTypes, 0, columnTypeClones, 0, columnTypes.length);
cachedMetaData.setColumnTypes(columnTypeClones);
MemoryResultSet cachedResultSet = new MemoryResultSet(cachedMetaData);
Object[] rowObjects = next();
while (rowObjects != null) {
cachedResultSet.addRow(rowObjects);
rowObjects = next();
}
return cachedResultSet;
} finally {
close();
}
}
use of org.pentaho.commons.connection.memory.MemoryMetaData in project pentaho-platform by pentaho.
the class SQLResultSetTest method testMemoryCopy_setMetadaMemory.
@Test
public void testMemoryCopy_setMetadaMemory() throws SQLException {
IPentahoMetaData metadata = new MemoryMetaData(mock(MemoryMetaData.class));
SQLResultSet resultSet = new SQLResultSet(nativeResultSet, nativeConnection);
resultSet.setMetaData(metadata);
IPentahoResultSet memoryCopyResultSet = resultSet.memoryCopy();
assertEquals(metadata.getClass(), memoryCopyResultSet.getMetaData().getClass());
}
use of org.pentaho.commons.connection.memory.MemoryMetaData in project pentaho-platform by pentaho.
the class PentahoFlashChartTest method createFromLists.
public static MemoryResultSet createFromLists(List colHeaders, List rHeaders, List data) {
Object[][] columnHeaders = new String[1][colHeaders.size()];
for (int i = 0; i < colHeaders.size(); i++) {
columnHeaders[0][i] = colHeaders.get(i);
}
Object[][] rowHeaders = new String[rHeaders.size()][1];
for (int i = 0; i < rHeaders.size(); i++) {
rowHeaders[i][0] = rHeaders.get(i);
}
IPentahoMetaData metaData = new MemoryMetaData(columnHeaders, rowHeaders);
MemoryResultSet result = new MemoryResultSet(metaData);
for (int i = 0; i < data.size(); i++) {
result.addRow(((List) data.get(i)).toArray());
}
return result;
}
Aggregations