Search in sources :

Example 16 with TranslatorException

use of org.teiid.translator.TranslatorException in project teiid by teiid.

the class ConnectorHost method readResultsFromExecution.

private List<List> readResultsFromExecution(Execution execution) throws TranslatorException {
    List<List> results = new ArrayList<List>();
    while (true) {
        try {
            if (execution instanceof ResultSetExecution) {
                ResultSetExecution rs = (ResultSetExecution) execution;
                List result = null;
                while ((result = rs.next()) != null) {
                    results.add(result);
                }
                break;
            }
            UpdateExecution rs = (UpdateExecution) execution;
            int[] result = rs.getUpdateCounts();
            for (int i = 0; i < result.length; i++) {
                results.add(Arrays.asList(result[i]));
            }
            break;
        } catch (DataNotAvailableException e) {
            if (e.getRetryDelay() > 0) {
                try {
                    Thread.sleep(e.getRetryDelay());
                } catch (InterruptedException e1) {
                    throw new TranslatorException(e1);
                }
            }
        }
    }
    return results;
}
Also used : ResultSetExecution(org.teiid.translator.ResultSetExecution) ArrayList(java.util.ArrayList) UpdateExecution(org.teiid.translator.UpdateExecution) ArrayList(java.util.ArrayList) List(java.util.List) TranslatorException(org.teiid.translator.TranslatorException) DataNotAvailableException(org.teiid.translator.DataNotAvailableException)

Example 17 with TranslatorException

use of org.teiid.translator.TranslatorException in project teiid by teiid.

the class ODataTypeManager method convertToTeiidRuntimeType.

/**
 * @param type
 * @param value
 * @param odataType type hint if the value could be a string containing a literal value of another type
 * @return
 * @throws TeiidException
 */
public static Object convertToTeiidRuntimeType(Class<?> type, Object value, String odataType) throws TeiidException {
    if (value == null) {
        return null;
    }
    if (type.isAssignableFrom(value.getClass())) {
        return value;
    }
    if (value instanceof UUID) {
        return value.toString();
    }
    if (type.isArray() && value instanceof List<?>) {
        List<?> list = (List<?>) value;
        Class<?> expectedArrayComponentType = type.getComponentType();
        Object array = Array.newInstance(type.getComponentType(), list.size());
        for (int i = 0; i < list.size(); i++) {
            Object arrayItem = convertToTeiidRuntimeType(expectedArrayComponentType, list.get(i), null);
            Array.set(array, i, arrayItem);
        }
        return array;
    }
    if (odataType != null && value instanceof String) {
        try {
            value = ODataTypeManager.parseLiteral(odataType, (String) value);
        } catch (TeiidException e) {
            throw new TranslatorException(e);
        }
    }
    if (value instanceof Geospatial && type == DataTypeManager.DefaultDataClasses.GEOMETRY) {
        final Geospatial val = (Geospatial) value;
        // if (val.getDimension() == Dimension.GEOMETRY) {
        return new GeometryInputSource() {

            @Override
            public Reader getGml() throws Exception {
                AtomGeoValueSerializer serializer = new AtomGeoValueSerializer();
                XMLOutputFactory factory = XMLOutputFactory.newInstance();
                StringWriter sw = new StringWriter();
                final XMLStreamWriter writer = factory.createXMLStreamWriter(sw);
                serializer.serialize(writer, val);
                writer.close();
                return new StringReader(sw.toString());
            }

            @Override
            public Integer getSrid() {
                String srid = val.getSrid().toString();
                try {
                    return Integer.parseInt(srid);
                } catch (NumberFormatException e) {
                    return null;
                }
            }
        };
    // }
    }
    if (value instanceof Calendar) {
        Calendar calender = (Calendar) value;
        if (type.isAssignableFrom(java.sql.Time.class)) {
            calender.set(Calendar.YEAR, 1970);
            calender.set(Calendar.MONTH, Calendar.JANUARY);
            calender.set(Calendar.DAY_OF_MONTH, 1);
            calender.set(Calendar.MILLISECOND, 0);
            return new Time(calender.getTimeInMillis());
        } else if (type.isAssignableFrom(java.sql.Date.class)) {
            calender.set(Calendar.HOUR_OF_DAY, 0);
            calender.set(Calendar.MINUTE, 0);
            calender.set(Calendar.SECOND, 0);
            calender.set(Calendar.MILLISECOND, 0);
            return new java.sql.Date(calender.getTimeInMillis());
        } else if (type.isAssignableFrom(java.sql.Timestamp.class)) {
            return new Timestamp(calender.getTimeInMillis());
        }
    }
    Transform transform = DataTypeManager.getTransform(value.getClass(), type);
    if (transform != null) {
        try {
            value = transform.transform(value, type);
        } catch (TransformationException e) {
            throw new TeiidException(e);
        }
    }
    return value;
}
Also used : XMLOutputFactory(javax.xml.stream.XMLOutputFactory) GeometryInputSource(org.teiid.GeometryInputSource) Time(java.sql.Time) EdmString(org.apache.olingo.commons.core.edm.primitivetype.EdmString) Timestamp(java.sql.Timestamp) StringWriter(java.io.StringWriter) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) StringReader(java.io.StringReader) List(java.util.List) UUID(java.util.UUID) Calendar(java.util.Calendar) Geospatial(org.apache.olingo.commons.api.edm.geo.Geospatial) EdmDate(org.apache.olingo.commons.core.edm.primitivetype.EdmDate) Date(java.sql.Date) TeiidException(org.teiid.core.TeiidException) Date(java.sql.Date) TranslatorException(org.teiid.translator.TranslatorException)

Example 18 with TranslatorException

use of org.teiid.translator.TranslatorException in project teiid by teiid.

the class ODataMetadataProcessor method addEntitySetAsTable.

protected Table addEntitySetAsTable(MetadataFactory mf, EdmEntitySet entitySet) throws TranslatorException {
    Table table = buildTable(mf, entitySet);
    table.setProperty(ENTITY_TYPE, entitySet.getType().getFullyQualifiedTypeName());
    // add columns
    for (EdmProperty ep : entitySet.getType().getProperties().toList()) {
        if (ep.getType().isSimple() || (ep.getType() instanceof EdmCollectionType && ((EdmCollectionType) ep.getType()).getItemType().isSimple())) {
            addPropertyAsColumn(mf, table, ep, entitySet);
        } else {
            // this is complex type, i.e treat them as embeddable in the same table add all columns.
            // Have tried adding this as separate table with 1 to 1 mapping to parent table, however
            // that model fails when there are two instances of single complex type as column. This
            // creates verbose columns but safe.
            EdmComplexType embedded = (EdmComplexType) ep.getType();
            for (EdmProperty property : embedded.getProperties().toList()) {
                if (property.getType().isSimple() || (property.getType() instanceof EdmCollectionType && ((EdmCollectionType) property.getType()).getItemType().isSimple())) {
                    Column column = addPropertyAsColumn(mf, table, property, entitySet, ep.getName());
                    // complex type
                    column.setProperty(COMPLEX_TYPE, embedded.getFullyQualifiedTypeName());
                    // name of parent column
                    column.setProperty(COLUMN_GROUP, ep.getName());
                } else {
                    throw new TranslatorException(ODataPlugin.Util.gs(ODataPlugin.Event.TEIID17002, entitySet.getName(), ep.getName()));
                }
            }
        }
    }
    // add PK
    // $NON-NLS-1$
    mf.addPrimaryKey("PK", entitySet.getType().getKeys(), table);
    return table;
}
Also used : Table(org.teiid.metadata.Table) Column(org.teiid.metadata.Column) TranslatorException(org.teiid.translator.TranslatorException)

Example 19 with TranslatorException

use of org.teiid.translator.TranslatorException in project teiid by teiid.

the class ODataMetadataProcessor method getEds.

private EdmDataServices getEds(WSConnection conn) throws TranslatorException {
    try {
        BaseQueryExecution execution = new BaseQueryExecution(ef, null, null, conn);
        // $NON-NLS-1$ //$NON-NLS-2$
        BinaryWSProcedureExecution call = execution.executeDirect("GET", "$metadata", null, execution.getDefaultHeaders());
        if (call.getResponseCode() != Status.OK.getStatusCode()) {
            throw execution.buildError(call);
        }
        Blob out = (Blob) call.getOutputParameterValues().get(0);
        EdmDataServices eds = new EdmxFormatParser().parseMetadata(StaxUtil.newXMLEventReader(new InputStreamReader(out.getBinaryStream())));
        return eds;
    } catch (SQLException e) {
        throw new TranslatorException(e);
    } catch (Exception e) {
        throw new TranslatorException(e);
    }
}
Also used : Blob(java.sql.Blob) InputStreamReader(java.io.InputStreamReader) SQLException(java.sql.SQLException) BinaryWSProcedureExecution(org.teiid.translator.ws.BinaryWSProcedureExecution) EdmxFormatParser(org.odata4j.format.xml.EdmxFormatParser) TranslatorException(org.teiid.translator.TranslatorException) SQLException(java.sql.SQLException) TranslatorException(org.teiid.translator.TranslatorException)

Example 20 with TranslatorException

use of org.teiid.translator.TranslatorException in project teiid by teiid.

the class ODataMetadataProcessor method addFunctionImportAsProcedure.

void addFunctionImportAsProcedure(MetadataFactory mf, EdmFunctionImport function) throws TranslatorException {
    Procedure procedure = mf.addProcedure(function.getName());
    procedure.setProperty(HTTP_METHOD, function.getHttpMethod());
    // add parameters
    for (EdmFunctionParameter fp : function.getParameters()) {
        ProcedureParameter.Type type = ProcedureParameter.Type.In;
        if (fp.getMode().equals(EdmFunctionParameter.Mode.InOut)) {
            type = ProcedureParameter.Type.InOut;
        } else if (fp.getMode().equals(EdmFunctionParameter.Mode.Out)) {
            type = ProcedureParameter.Type.Out;
        }
        mf.addProcedureParameter(fp.getName(), ODataTypeManager.teiidType(fp.getType().getFullyQualifiedTypeName()), type, procedure);
    }
    // add return type
    EdmType returnType = function.getReturnType();
    if (returnType != null) {
        if (returnType.isSimple()) {
            // $NON-NLS-1$
            mf.addProcedureParameter("return", ODataTypeManager.teiidType(((EdmSimpleType) returnType).getFullyQualifiedTypeName()), ProcedureParameter.Type.ReturnValue, procedure);
        } else if (returnType instanceof EdmComplexType) {
            procedure.setProperty(ENTITY_TYPE, function.getReturnType().getFullyQualifiedTypeName());
            addProcedureTableReturn(mf, procedure, returnType);
        } else if (returnType instanceof EdmEntityType) {
            procedure.setProperty(ENTITY_TYPE, function.getReturnType().getFullyQualifiedTypeName());
            addProcedureTableReturn(mf, procedure, returnType);
        } else if (returnType instanceof EdmCollectionType) {
            procedure.setProperty(ENTITY_TYPE, ((EdmCollectionType) returnType).getItemType().getFullyQualifiedTypeName());
            addProcedureTableReturn(mf, procedure, ((EdmCollectionType) returnType).getItemType());
        } else {
            throw new TranslatorException(ODataPlugin.Util.gs(ODataPlugin.Event.TEIID17005, function.getName(), returnType.getFullyQualifiedTypeName()));
        }
    }
}
Also used : ProcedureParameter(org.teiid.metadata.ProcedureParameter) Procedure(org.teiid.metadata.Procedure) TranslatorException(org.teiid.translator.TranslatorException)

Aggregations

TranslatorException (org.teiid.translator.TranslatorException)227 ArrayList (java.util.ArrayList)51 Column (org.teiid.metadata.Column)47 List (java.util.List)32 Table (org.teiid.metadata.Table)30 IOException (java.io.IOException)26 SQLException (java.sql.SQLException)26 ResourceException (javax.resource.ResourceException)26 Test (org.junit.Test)16 Expression (org.teiid.language.Expression)16 Literal (org.teiid.language.Literal)16 DataNotAvailableException (org.teiid.translator.DataNotAvailableException)16 Blob (java.sql.Blob)15 Argument (org.teiid.language.Argument)13 DBObject (com.mongodb.DBObject)11 HashMap (java.util.HashMap)11 ColumnReference (org.teiid.language.ColumnReference)11 ExecutionContext (org.teiid.translator.ExecutionContext)11 BasicDBObject (com.mongodb.BasicDBObject)10 RuntimeMetadata (org.teiid.metadata.RuntimeMetadata)10