Skip to content

Commit

Permalink
TEIID-2977 fix for function json results
Browse files Browse the repository at this point in the history
  • Loading branch information
shawkins committed May 30, 2014
1 parent ffbcf9b commit 6566d58
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 6 deletions.
6 changes: 3 additions & 3 deletions odata/src/main/java/org/teiid/odata/LocalClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ public BaseResponse executeCall(String sql, List<SQLParam> parameters, EdmType r
boolean results = stmt.execute();
if (results) {
final ResultSet rs = stmt.getResultSet();
OCollection.Builder resultRows = OCollections.newBuilder(returnType);
OCollection.Builder resultRows = OCollections.newBuilder((EdmComplexType)((EdmCollectionType)returnType).getItemType());
while (rs.next()) {
int idx = 1;
List<OProperty<?>> row = new ArrayList<OProperty<?>>();
Expand Down Expand Up @@ -475,11 +475,11 @@ static OProperty<?> buildPropery(String propName, EdmType type, Object value, St
if (!(type instanceof EdmSimpleType)) {
if (type instanceof EdmCollectionType) {
EdmCollectionType collectionType = (EdmCollectionType)type;
Builder<OObject> b = OCollections.newBuilder(collectionType);
EdmType componentType = collectionType.getItemType();
Builder<OObject> b = OCollections.newBuilder(componentType);
if (value instanceof Array) {
value = ((Array)value).getArray();
}
EdmType componentType = collectionType.getItemType();
int length = java.lang.reflect.Array.getLength(value);
for (int i = 0; i < length; i++) {
Object o = java.lang.reflect.Array.get(value, i);
Expand Down
31 changes: 29 additions & 2 deletions odata/src/test/java/org/teiid/odata/TestODataIntegration.java
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ public void testProcedure() throws Exception {
ArgumentCaptor<String> sql = ArgumentCaptor.forClass(String.class);
ArgumentCaptor<List> params = ArgumentCaptor.forClass(List.class);

when(client.executeCall(any(String.class), anyListOf(SQLParam.class), any(EdmType.class))).thenReturn(Responses.simple(EdmSimpleType.INT32, "return", org.odata4j.expression.Expression.null_()));
when(client.executeCall(any(String.class), anyListOf(SQLParam.class), any(EdmType.class))).thenReturn(Responses.simple(EdmSimpleType.INT32, "return", null));

ClientRequest request = new ClientRequest(TestPortProvider.generateURL("/odata/northwind/getCustomers?p2=datetime'2011-09-11T00:00:00'&p3=2.0M"));
ClientResponse<String> response = request.get(String.class);
Expand Down Expand Up @@ -585,6 +585,34 @@ public boolean supportsCompareCriteriaEquals() {
}
}

@Test public void testJsonProcedureResultSet() throws Exception {
EmbeddedServer es = new EmbeddedServer();
HardCodedExecutionFactory hc = new HardCodedExecutionFactory();
hc.addData("EXEC x()", Arrays.asList(Arrays.asList("x"), Arrays.asList("y")));
es.addTranslator("x", hc);
es.start(new EmbeddedConfiguration());
try {
ModelMetaData mmd = new ModelMetaData();
mmd.setName("m");
mmd.setSchemaSourceType("ddl");
mmd.setSchemaText("create foreign procedure x () returns table(y string);");
mmd.addSourceMapping("x", "x", null);
es.deployVDB("northwind", mmd);

TeiidDriver td = es.getDriver();
Properties props = new Properties();
LocalClient lc = new LocalClient("northwind", 1, props);
lc.setDriver(td);
MockProvider.CLIENT = lc;

ClientRequest request = new ClientRequest(TestPortProvider.generateURL("/odata/northwind/x?$format=json"));
ClientResponse<String> response = request.get(String.class);
assertEquals(200, response.getStatus());
} finally {
es.stop();
}
}

@Test public void testBasicTypes() throws Exception {
EmbeddedServer es = new EmbeddedServer();
es.start(new EmbeddedConfiguration());
Expand Down Expand Up @@ -630,7 +658,6 @@ protected List<? extends List<?>> getData(
ClientRequest request = new ClientRequest(TestPortProvider.generateURL("/odata/northwind/SmallA?$format=json&$select=TimeValue"));
ClientResponse<String> response = request.get(String.class);
assertEquals(200, response.getStatus());
System.out.println(response.getEntity());
} finally {
es.stop();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,14 @@
import java.util.List;
import java.util.Map;

import org.teiid.language.Call;
import org.teiid.language.Command;
import org.teiid.language.QueryExpression;
import org.teiid.metadata.RuntimeMetadata;
import org.teiid.translator.DataNotAvailableException;
import org.teiid.translator.ExecutionContext;
import org.teiid.translator.ExecutionFactory;
import org.teiid.translator.ProcedureExecution;
import org.teiid.translator.ResultSetExecution;
import org.teiid.translator.Translator;
import org.teiid.translator.TranslatorException;
Expand All @@ -47,6 +49,48 @@ public HardCodedExecutionFactory() {
setSourceRequired(false);
}

@Override
public ProcedureExecution createProcedureExecution(Call command,
ExecutionContext executionContext, RuntimeMetadata metadata,
Object connection) throws TranslatorException {
List<? extends List<?>> list = getData(command);
if (list == null) {
throw new RuntimeException(command.toString());
}
final Iterator<? extends List<?>> result = list.iterator();
return new ProcedureExecution() {

@Override
public void execute() throws TranslatorException {

}

@Override
public void close() {

}

@Override
public void cancel() throws TranslatorException {

}

@Override
public List<?> next() throws TranslatorException, DataNotAvailableException {
if (result.hasNext()) {
return result.next();
}
return null;
}

@Override
public List<?> getOutputParameterValues()
throws TranslatorException {
return null;
}
};
}

@Override
public ResultSetExecution createResultSetExecution(
final QueryExpression command, ExecutionContext executionContext,
Expand Down Expand Up @@ -83,8 +127,12 @@ public List<?> next() throws TranslatorException, DataNotAvailableException {
}
};
}

protected List<? extends List<?>> getData(final QueryExpression command) {
return getData((Command)command);
}

protected List<? extends List<?>> getData(final Command command) {
return dataMap.get(command.toString());
}

Expand Down

0 comments on commit 6566d58

Please sign in to comment.