use of org.teiid.metadata.ProcedureParameter in project teiid by teiid.
the class TestSwaggerMetadataProcessor method testObjectArrayTypes.
@Test
public void testObjectArrayTypes() throws Exception {
SwaggerExecutionFactory translator = new SwaggerExecutionFactory();
translator.start();
MetadataFactory mf = getMetadata(translator, UnitTestUtil.getTestDataPath() + "/doubleclick-swagger.json");
Procedure p = mf.getSchema().getProcedure("doubleclicksearch.reports.request");
ProcedureParameter param = p.getParameterByName("filters_values");
assertEquals("string[]", param.getRuntimeType());
}
use of org.teiid.metadata.ProcedureParameter in project teiid by teiid.
the class SwaggerProcedureExecution method buildWSExecution.
private BinaryWSProcedureExecution buildWSExecution(Call obj) throws TranslatorException {
Procedure procedure = obj.getMetadataObject();
String uri = procedure.getProperty(RestMetadataExtension.URI, false);
String method = procedure.getProperty(RestMetadataExtension.METHOD, false);
StringBuilder queryParameters = new StringBuilder();
StringBuilder formParameters = new StringBuilder();
Map<String, List<String>> headers = new HashMap<String, List<String>>();
Object payload = null;
// body payload document
SwaggerBodyInputDocument input = null;
final List<Argument> params = obj.getArguments();
if (params != null && params.size() != 0) {
Argument param = null;
for (int i = 0; i < params.size(); i++) {
param = params.get(i);
ProcedureParameter metadata = param.getMetadataObject();
String argName = WSConnection.Util.httpURLEncode(param.getMetadataObject().getName());
if (param.getDirection() == Direction.IN || param.getDirection() == Direction.INOUT) {
String in = metadata.getProperty(RestMetadataExtension.PARAMETER_TYPE, false);
if (in.equalsIgnoreCase(RestMetadataExtension.ParameterType.QUERY.name())) {
if (queryParameters.length() != 0) {
// $NON-NLS-1$
queryParameters.append("&");
}
Object value = param.getExpression();
if (value instanceof Array) {
addArgumentValue(argName, (Array) value, metadata.getProperty(SwaggerMetadataProcessor.COLLECION_FORMAT, false), queryParameters);
} else {
String argValue = getURLValue((Literal) value);
queryParameters.append(argName);
queryParameters.append(Tokens.EQ);
queryParameters.append(argValue);
}
} else if (in.equalsIgnoreCase(RestMetadataExtension.ParameterType.PATH.name())) {
String argValue = getURLValue(param.getArgumentValue());
// $NON-NLS-1$ //$NON-NLS-2$
String regex = "\\{" + argName + "\\}";
uri = uri.replaceAll(regex, argValue);
} else if (in.equalsIgnoreCase(RestMetadataExtension.ParameterType.FORM.name()) || in.equalsIgnoreCase(RestMetadataExtension.ParameterType.FORMDATA.name())) {
if (formParameters.length() != 0) {
// $NON-NLS-1$
formParameters.append("&");
}
Object value = param.getExpression();
if (value instanceof Array) {
addArgumentValue(argName, (Array) value, metadata.getProperty(SwaggerMetadataProcessor.COLLECION_FORMAT, false), formParameters);
} else {
formParameters.append(argName);
formParameters.append(Tokens.EQ);
formParameters.append(getURLValue((Literal) value));
}
} else if (in.equalsIgnoreCase(RestMetadataExtension.ParameterType.BODY.name())) {
if (input == null) {
input = new SwaggerBodyInputDocument();
}
Object expr = param.getExpression();
if (expr instanceof Literal) {
expr = ((Literal) expr).getValue();
}
input.addArgument(param.getMetadataObject(), expr);
} else if (in.equalsIgnoreCase(RestMetadataExtension.ParameterType.HEADER.name())) {
String argValue = param.getArgumentValue().getValue().toString();
headers.put(argName, Arrays.asList(argValue));
}
} else {
throw new TranslatorException("Not supported parameter");
}
}
}
String consumes = procedure.getProperty(RestMetadataExtension.CONSUMES, false);
if (consumes == null) {
consumes = "application/json";
}
if (input != null) {
try {
SwaggerSerializer serializer = getSerializer(consumes);
InputStream oos = serializer.serialize(input);
payload = ObjectConverterUtil.convertToString(oos);
} catch (IOException e) {
throw new TranslatorException(e);
}
}
if (payload == null && formParameters.length() > 0) {
payload = formParameters.toString();
}
headers.put("Content-Type", Arrays.asList(consumes));
String produces = procedure.getProperty(RestMetadataExtension.PRODUCES, false);
if (produces == null) {
produces = "application/json";
}
headers.put("Accept", Arrays.asList(produces));
if (queryParameters.length() > 0) {
uri = uri + "?" + queryParameters;
}
return buildInvokeHTTP(method, uri, payload, headers);
}
use of org.teiid.metadata.ProcedureParameter in project teiid by teiid.
the class OracleExecutionFactory method getSQLConversionVisitor.
@Override
public SQLConversionVisitor getSQLConversionVisitor() {
return new SQLConversionVisitor(this) {
@Override
public void visit(Select select) {
if (select.getFrom() == null || select.getFrom().isEmpty()) {
select.setFrom(Arrays.asList((TableReference) new NamedTable(DUAL, null, null)));
}
super.visit(select);
}
@Override
public void visit(Comparison obj) {
if (isFixedChar(obj.getLeftExpression())) {
if (obj.getRightExpression() instanceof Literal) {
Literal l = (Literal) obj.getRightExpression();
l.setType(FixedCharType.class);
} else if (obj.getRightExpression() instanceof Parameter) {
Parameter p = (Parameter) obj.getRightExpression();
p.setType(FixedCharType.class);
}
}
if (obj.getLeftExpression().getType() == TypeFacility.RUNTIME_TYPES.BOOLEAN && (obj.getLeftExpression() instanceof Function) && obj.getRightExpression() instanceof Literal) {
Function f = (Function) obj.getLeftExpression();
if (STRING_BOOLEAN_FUNCTIONS.contains(f.getName())) {
Boolean b = (Boolean) ((Literal) obj.getRightExpression()).getValue();
// $NON-NLS-1$ //$NON-NLS-2$
obj.setRightExpression(new Literal(b != null ? (b ? "TRUE" : "FALSE") : null, TypeFacility.RUNTIME_TYPES.STRING));
}
}
super.visit(obj);
}
@Override
protected void appendRightComparison(Comparison obj) {
if (obj.getRightExpression() instanceof Array) {
// oracle needs rhs arrays nested in extra parens
buffer.append(SQLConstants.Tokens.LPAREN);
super.appendRightComparison(obj);
buffer.append(SQLConstants.Tokens.RPAREN);
} else {
super.appendRightComparison(obj);
}
}
private boolean isFixedChar(Expression obj) {
if (!isOracleSuppliedDriver() || !(obj instanceof ColumnReference)) {
return false;
}
ColumnReference cr = (ColumnReference) obj;
return cr.getType() == TypeFacility.RUNTIME_TYPES.STRING && cr.getMetadataObject() != null && (// $NON-NLS-1$
"CHAR".equalsIgnoreCase(cr.getMetadataObject().getNativeType()) || // $NON-NLS-1$
"NCHAR".equalsIgnoreCase(cr.getMetadataObject().getNativeType()));
}
@Override
public void visit(In obj) {
if (isFixedChar(obj.getLeftExpression())) {
for (Expression exp : obj.getRightExpressions()) {
if (exp instanceof Literal) {
Literal l = (Literal) exp;
l.setType(FixedCharType.class);
} else if (exp instanceof Parameter) {
Parameter p = (Parameter) exp;
p.setType(FixedCharType.class);
}
}
}
super.visit(obj);
}
@Override
public void visit(NamedTable table) {
stripDualAlias(table);
super.visit(table);
}
private void stripDualAlias(NamedTable table) {
if (table.getCorrelationName() != null) {
if (isDual(table)) {
table.setCorrelationName(null);
}
}
}
@Override
public void visit(ColumnReference obj) {
if (obj.getTable() != null) {
stripDualAlias(obj.getTable());
}
super.visit(obj);
}
@Override
public void visit(Call call) {
if (oracleSuppliedDriver && call.getResultSetColumnTypes().length > 0 && call.getMetadataObject() != null) {
if (call.getReturnType() == null && call.getMetadataObject().getProperty(SQLConversionVisitor.TEIID_NATIVE_QUERY, false) == null) {
// assume stored function handling
if (!setOutCursorType(call)) {
call.setReturnType(RefCursorType.class);
}
} else {
// TODO we only will allow a single out cursor
if (call.getMetadataObject() != null) {
ProcedureParameter param = call.getReturnParameter();
if (param != null && REF_CURSOR.equalsIgnoreCase(param.getNativeType())) {
call.setReturnType(RefCursorType.class);
}
}
setOutCursorType(call);
}
}
super.visit(call);
}
private boolean setOutCursorType(Call call) {
boolean set = false;
for (Argument arg : call.getArguments()) {
if (arg.getDirection() == Direction.OUT) {
ProcedureParameter param = arg.getMetadataObject();
if (param != null && REF_CURSOR.equalsIgnoreCase(param.getNativeType())) {
arg.setType(RefCursorType.class);
set = true;
}
}
}
return set;
}
@Override
public void visit(Like obj) {
if (obj.getMode() == MatchMode.REGEX) {
if (obj.isNegated()) {
// $NON-NLS-1$
buffer.append("NOT(");
}
// $NON-NLS-1$
buffer.append("REGEXP_LIKE(");
append(obj.getLeftExpression());
// $NON-NLS-1$
buffer.append(", ");
append(obj.getRightExpression());
// $NON-NLS-1$
buffer.append(")");
if (obj.isNegated()) {
// $NON-NLS-1$
buffer.append(")");
}
} else {
super.visit(obj);
}
}
@Override
public void visit(WithItem obj) {
if (obj.getColumns() != null) {
List<ColumnReference> cols = obj.getColumns();
if (!obj.isRecusive()) {
// oracle 10 does not support recursion nor a column list
obj.setColumns(null);
Select select = obj.getSubquery().getProjectedQuery();
List<DerivedColumn> selectClause = select.getDerivedColumns();
for (int i = 0; i < cols.size(); i++) {
selectClause.get(i).setAlias(cols.get(i).getName());
}
}
}
super.visit(obj);
}
};
}
use of org.teiid.metadata.ProcedureParameter in project teiid by teiid.
the class TestOracleTranslator method getOracleSpecificMetadata.
/**
* create fake BQT metadata to test this case, name in source is important
*/
private QueryMetadataInterface getOracleSpecificMetadata() {
MetadataStore metadataStore = new MetadataStore();
// $NON-NLS-1$
Schema foo = RealMetadataFactory.createPhysicalModel("BQT1", metadataStore);
// $NON-NLS-1$
Table table = RealMetadataFactory.createPhysicalGroup("SmallA", foo);
// $NON-NLS-1$
Table x = RealMetadataFactory.createPhysicalGroup("x", foo);
x.setProperty(SQLConversionVisitor.TEIID_NATIVE_QUERY, "select c from d");
// $NON-NLS-1$
Table dual = RealMetadataFactory.createPhysicalGroup("DUAL", foo);
// $NON-NLS-1$
table.setNameInSource("SmallishA");
String[] elemNames = new String[] { // $NON-NLS-1$
"DoubleNum", // $NON-NLS-1$
"ID", // $NON-NLS-1$
"timestampvalue", "description", "ndescription" };
String[] elemTypes = new String[] { DataTypeManager.DefaultDataTypes.DOUBLE, DataTypeManager.DefaultDataTypes.INTEGER, DataTypeManager.DefaultDataTypes.TIMESTAMP, DataTypeManager.DefaultDataTypes.STRING, DataTypeManager.DefaultDataTypes.STRING };
RealMetadataFactory.createElements(x, elemNames, elemTypes);
List<Column> cols = RealMetadataFactory.createElements(table, elemNames, elemTypes);
cols.get(1).setAutoIncremented(true);
// $NON-NLS-1$
cols.get(1).setNameInSource("ID:SEQUENCE=MYSEQUENCE.nextVal");
// $NON-NLS-1$
cols.get(2).setNativeType("date");
cols.get(3).setNativeType("CHAR");
cols.get(4).setNativeType("NCHAR");
// $NON-NLS-1$
List<Column> dualCols = RealMetadataFactory.createElements(dual, new String[] { "something" }, new String[] { DataTypeManager.DefaultDataTypes.STRING });
dualCols.get(0).setNameInSource("seq.nextval");
// $NON-NLS-1$
ProcedureParameter in1 = RealMetadataFactory.createParameter("in1", SPParameter.IN, DataTypeManager.DefaultDataTypes.INTEGER);
// $NON-NLS-1$ //$NON-NLS-2$
ColumnSet<Procedure> rs3 = RealMetadataFactory.createResultSet("proc.rs1", new String[] { "e1" }, new String[] { DataTypeManager.DefaultDataTypes.INTEGER });
Procedure p = RealMetadataFactory.createStoredProcedure("proc", foo, Arrays.asList(in1));
p.setResultSet(rs3);
p.setProperty(SQLConversionVisitor.TEIID_NATIVE_QUERY, "select x from y where z = $1");
p = RealMetadataFactory.createStoredProcedure("proc1", foo, Arrays.asList(RealMetadataFactory.createParameter("in1", SPParameter.IN, DataTypeManager.DefaultDataTypes.STRING)));
// $NON-NLS-1$ //$NON-NLS-2$
p.setResultSet(RealMetadataFactory.createResultSet("proc.rs1", new String[] { "e1" }, new String[] { DataTypeManager.DefaultDataTypes.INTEGER }));
p.setProperty(SQLConversionVisitor.TEIID_NATIVE_QUERY, "select $1 from y");
p.setProperty(SQLConversionVisitor.TEIID_NON_PREPARED, "true");
CompositeMetadataStore store = new CompositeMetadataStore(metadataStore);
return new TransformationMetadata(null, store, null, RealMetadataFactory.SFM.getSystemFunctions(), null);
}
use of org.teiid.metadata.ProcedureParameter in project teiid by teiid.
the class TestODataMetadataProcessor method testFunction.
@Test
public void testFunction() throws Exception {
CsdlReturnType returnType = new CsdlReturnType();
returnType.setType("Edm.String");
MetadataFactory mf = functionMetadata("invoke", returnType, null);
Procedure p = mf.getSchema().getProcedure("invoke");
assertNotNull(p);
assertEquals(3, p.getParameters().size());
assertNull(p.getResultSet());
assertNotNull(getReturnParameter(p));
ProcedureParameter pp = getReturnParameter(p);
assertEquals("string", pp.getRuntimeType());
ODataType type = ODataType.valueOf(p.getProperty(ODataMetadataProcessor.ODATA_TYPE, false));
assertEquals(ODataType.FUNCTION, type);
}
Aggregations