Search in sources :

Example 11 with TransformationException

use of org.teiid.core.types.TransformationException in project teiid by teiid.

the class Request method createCommandContext.

protected void createCommandContext() {
    if (this.context != null) {
        return;
    }
    // Create command context, used in rewriting, planning, and processing
    // Identifies a "group" of requests on a per-connection basis to allow later
    // cleanup of all resources in the group on connection shutdown
    String groupName = workContext.getSessionId();
    this.context = new CommandContext(groupName, workContext.getUserName(), requestMsg.getExecutionPayload(), workContext.getVdbName(), workContext.getVdbVersion(), this.requestMsg.getShowPlan() != ShowPlan.OFF || LogManager.isMessageToBeRecorded(LogConstants.CTX_COMMANDLOGGING, MessageLevel.TRACE));
    this.context.setProcessorBatchSize(bufferManager.getProcessorBatchSize());
    this.context.setGlobalTableStore(this.globalTables);
    boolean autoCleanLobs = true;
    if (this.workContext.getSession().isEmbedded()) {
        Object value = this.workContext.getSession().getSessionVariables().get(CLEAN_LOBS_ONCLOSE);
        if (value != null) {
            value = DataTypeManager.convertToRuntimeType(value, false);
            try {
                value = DataTypeManager.transformValue(value, value.getClass(), DataTypeManager.DefaultDataClasses.BOOLEAN);
                if (!(Boolean) value) {
                    autoCleanLobs = false;
                }
            } catch (TransformationException e) {
                // $NON-NLS-1$
                LogManager.logDetail(LogConstants.CTX_DQP, e, "Improper value for", CLEAN_LOBS_ONCLOSE);
            }
        }
    }
    if (!autoCleanLobs) {
        context.disableAutoCleanLobs();
    }
    context.setExecutor(this.executor);
    context.setAuthoriziationValidator(authorizationValidator);
    context.setTempTableStore(tempTableStore);
    context.setQueryProcessorFactory(new QueryProcessorFactoryImpl(this.bufferManager, this.processorDataManager, this.capabilitiesFinder, idGenerator, metadata));
    context.setMetadata(this.metadata);
    context.setBufferManager(this.bufferManager);
    context.setPreparedPlanCache(planCache);
    context.setResultSetCacheEnabled(this.resultSetCacheEnabled);
    context.setUserRequestSourceConcurrency(this.userRequestConcurrency);
    context.setSubject(workContext.getSubject());
    this.context.setOptions(options);
    this.context.setSession(workContext.getSession());
    this.context.setRequestId(this.requestId);
    this.context.setDQPWorkContext(this.workContext);
    this.context.setTransactionService(this.transactionService);
    this.context.setVDBClassLoader(workContext.getVDB().getAttachment(ClassLoader.class));
}
Also used : TransformationException(org.teiid.core.types.TransformationException) CommandContext(org.teiid.query.util.CommandContext)

Example 12 with TransformationException

use of org.teiid.core.types.TransformationException in project teiid by teiid.

the class MongoDBExecutionFactory method retrieveValue.

/**
 * @param field
 * @param expectedClass
 * @return
 * @throws TranslatorException
 */
public Object retrieveValue(Object value, Class<?> expectedClass, DB mongoDB, String fqn, String colName) throws TranslatorException {
    if (value == null) {
        return null;
    }
    if (value.getClass().equals(expectedClass)) {
        return value;
    }
    if (value instanceof DBRef) {
        Object obj = ((DBRef) value).getId();
        if (obj instanceof BasicDBObject) {
            BasicDBObject bdb = (BasicDBObject) obj;
            return bdb.get(colName);
        }
        return obj;
    } else if (value instanceof java.util.Date && expectedClass.equals(java.sql.Date.class)) {
        return new java.sql.Date(((java.util.Date) value).getTime());
    } else if (value instanceof java.util.Date && expectedClass.equals(java.sql.Timestamp.class)) {
        return new java.sql.Timestamp(((java.util.Date) value).getTime());
    } else if (value instanceof java.util.Date && expectedClass.equals(java.sql.Time.class)) {
        return new java.sql.Time(((java.util.Date) value).getTime());
    } else if (value instanceof String && expectedClass.equals(BigDecimal.class)) {
        return new BigDecimal((String) value);
    } else if (value instanceof String && expectedClass.equals(BigInteger.class)) {
        return new BigInteger((String) value);
    } else if (value instanceof String && expectedClass.equals(Character.class)) {
        return new Character(((String) value).charAt(0));
    } else if (value instanceof String && expectedClass.equals(BinaryType.class)) {
        return new BinaryType(((String) value).getBytes());
    } else if (value instanceof String && expectedClass.equals(Blob.class)) {
        GridFS gfs = new GridFS(mongoDB, fqn);
        final GridFSDBFile resource = gfs.findOne((String) value);
        if (resource == null) {
            return null;
        }
        return new BlobImpl(new InputStreamFactory() {

            @Override
            public InputStream getInputStream() throws IOException {
                return resource.getInputStream();
            }
        });
    } else if (value instanceof String && expectedClass.equals(Clob.class)) {
        GridFS gfs = new GridFS(mongoDB, fqn);
        final GridFSDBFile resource = gfs.findOne((String) value);
        if (resource == null) {
            return null;
        }
        return new ClobImpl(new InputStreamFactory() {

            @Override
            public InputStream getInputStream() throws IOException {
                return resource.getInputStream();
            }
        }, -1);
    } else if (value instanceof String && expectedClass.equals(SQLXML.class)) {
        GridFS gfs = new GridFS(mongoDB, fqn);
        final GridFSDBFile resource = gfs.findOne((String) value);
        if (resource == null) {
            return null;
        }
        return new SQLXMLImpl(new InputStreamFactory() {

            @Override
            public InputStream getInputStream() throws IOException {
                return resource.getInputStream();
            }
        });
    } else if (value instanceof BasicDBList) {
        BasicDBList arrayValues = (BasicDBList) value;
        // array
        if (expectedClass.isArray() && !(arrayValues.get(0) instanceof BasicDBObject)) {
            Class arrayType = expectedClass.getComponentType();
            Object array = Array.newInstance(arrayType, arrayValues.size());
            for (int i = 0; i < arrayValues.size(); i++) {
                Object arrayItem = retrieveValue(arrayValues.get(i), arrayType, mongoDB, fqn, colName);
                Array.set(array, i, arrayItem);
            }
            value = array;
        }
    } else if (value instanceof org.bson.types.ObjectId) {
        org.bson.types.ObjectId id = (org.bson.types.ObjectId) value;
        value = id.toHexString();
    } else {
        Transform transform = DataTypeManager.getTransform(value.getClass(), expectedClass);
        if (transform != null) {
            try {
                value = transform.transform(value, expectedClass);
            } catch (TransformationException e) {
                throw new TranslatorException(e);
            }
        }
    }
    return value;
}
Also used : InputStreamFactory(org.teiid.core.types.InputStreamFactory) BasicDBObject(com.mongodb.BasicDBObject) BlobImpl(org.teiid.core.types.BlobImpl) ClobImpl(org.teiid.core.types.ClobImpl) Blob(java.sql.Blob) SQLXMLImpl(org.teiid.core.types.SQLXMLImpl) TransformationException(org.teiid.core.types.TransformationException) BinaryType(org.teiid.core.types.BinaryType) InputStream(java.io.InputStream) DBRef(com.mongodb.DBRef) IOException(java.io.IOException) GridFS(com.mongodb.gridfs.GridFS) BigDecimal(java.math.BigDecimal) BasicDBList(com.mongodb.BasicDBList) SQLXML(java.sql.SQLXML) GridFSDBFile(com.mongodb.gridfs.GridFSDBFile) BigInteger(java.math.BigInteger) BasicDBObject(com.mongodb.BasicDBObject) Transform(org.teiid.core.types.Transform)

Example 13 with TransformationException

use of org.teiid.core.types.TransformationException in project teiid by teiid.

the class MongoDBSelectVisitor method convertGeometryToJson.

private void convertGeometryToJson(BasicDBObjectBuilder builder, GeometryType object) throws TranslatorException {
    try {
        ClobType clob = GeometryUtils.geometryToGeoJson(object);
        ClobToStringTransform clob2str = new ClobToStringTransform();
        String geometry = (String) clob2str.transform(clob, String.class);
        builder.add("$geometry", geometry);
    } catch (FunctionExecutionException | TransformationException e) {
        throw new TranslatorException(e);
    }
}
Also used : ClobType(org.teiid.core.types.ClobType) TransformationException(org.teiid.core.types.TransformationException) FunctionExecutionException(org.teiid.api.exception.query.FunctionExecutionException) ClobToStringTransform(org.teiid.core.types.basic.ClobToStringTransform) TranslatorException(org.teiid.translator.TranslatorException)

Example 14 with TransformationException

use of org.teiid.core.types.TransformationException in project teiid by teiid.

the class ParmHolder method setValue.

private Object setValue(ParmHolder holder, Object cachedObject, Object value, int level) throws TranslatorException {
    // if there are muliple nameNodes,
    // then do "get" for all but the last node, then set
    // if a "container" type object is encountered, then create the object type for the table and add it to the container
    // if a map, do a put
    // if a collection, do an add
    final String columnName = holder.nameNodes.get(level);
    boolean atTheBottom = false;
    if (holder.nodeSize == (level + 1))
        atTheBottom = true;
    if (!atTheBottom) {
        final Object containerObject = ObjectSourceMethodManager.getValue("get" + columnName, cachedObject);
        if (containerObject.getClass().isArray() || containerObject instanceof Collection || containerObject instanceof Map) {
            return cachedObject;
        }
    }
    ArrayList argTypes = new ArrayList(1);
    argTypes.add(holder.attributeType);
    Method m = ObjectSourceMethodManager.getMethod(cachedObject.getClass(), "set" + columnName, argTypes);
    Class[] setTypes = m.getParameterTypes();
    Object newValue = null;
    if (value instanceof Collection || value instanceof Map || value.getClass().isArray()) {
        newValue = value;
    } else {
        try {
            newValue = DataTypeManager.transformValue(value, setTypes[0]);
        } catch (TransformationException e) {
            // TODO Auto-generated catch block
            throw new TranslatorException(e);
        }
    }
    ObjectSourceMethodManager.executeMethod(m, cachedObject, new Object[] { newValue });
    // $NON-NLS-1$
    LogManager.logTrace(LogConstants.CTX_CONNECTOR, "Set value " + newValue);
    return newValue;
}
Also used : TransformationException(org.teiid.core.types.TransformationException) ArrayList(java.util.ArrayList) Collection(java.util.Collection) TranslatorException(org.teiid.translator.TranslatorException) Method(java.lang.reflect.Method) HashMap(java.util.HashMap) Map(java.util.Map)

Example 15 with TransformationException

use of org.teiid.core.types.TransformationException in project teiid by teiid.

the class XQueryEvaluator method evaluate.

public static Object evaluate(XMLType value, XMLCast expression, CommandContext context) throws ExpressionEvaluationException {
    Configuration config = new Configuration();
    Type t = value.getType();
    try {
        Item i = null;
        switch(t) {
            case CONTENT:
            // content could map to an array value, but we aren't handling that case here yet - only in xmltable
            case COMMENT:
            case PI:
                throw new FunctionExecutionException();
            case TEXT:
                i = new StringValue(value.getString());
                break;
            case UNKNOWN:
            case DOCUMENT:
            case ELEMENT:
                StreamSource ss = value.getSource(StreamSource.class);
                try {
                    i = config.buildDocument(ss);
                } finally {
                    if (ss.getInputStream() != null) {
                        ss.getInputStream().close();
                    }
                    if (ss.getReader() != null) {
                        ss.getReader().close();
                    }
                }
                break;
            default:
                // $NON-NLS-1$
                throw new AssertionError("Unknown xml value type " + t);
        }
        return XMLTableNode.getValue(expression.getType(), i, config, context);
    } catch (IOException e) {
        throw new FunctionExecutionException(e);
    } catch (ValidationException e) {
        throw new FunctionExecutionException(e);
    } catch (TransformationException e) {
        throw new FunctionExecutionException(e);
    } catch (XPathException e) {
        throw new FunctionExecutionException(e);
    } catch (SQLException e) {
        throw new FunctionExecutionException(e);
    }
}
Also used : Item(net.sf.saxon.om.Item) BinaryType(org.teiid.core.types.BinaryType) XMLType(org.teiid.core.types.XMLType) DocType(nu.xom.DocType) Type(org.teiid.core.types.XMLType.Type) TransformationException(org.teiid.core.types.TransformationException) FunctionExecutionException(org.teiid.api.exception.query.FunctionExecutionException) ValidationException(net.sf.saxon.type.ValidationException) Configuration(net.sf.saxon.Configuration) XPathException(net.sf.saxon.trans.XPathException) SQLException(java.sql.SQLException) StreamSource(javax.xml.transform.stream.StreamSource) IOException(java.io.IOException) StringValue(net.sf.saxon.value.StringValue)

Aggregations

TransformationException (org.teiid.core.types.TransformationException)22 IOException (java.io.IOException)6 SQLException (java.sql.SQLException)6 Timestamp (java.sql.Timestamp)4 ArrayList (java.util.ArrayList)4 XMLType (org.teiid.core.types.XMLType)4 TranslatorException (org.teiid.translator.TranslatorException)4 InputStream (java.io.InputStream)3 Blob (java.sql.Blob)3 Date (java.sql.Date)3 FunctionExecutionException (org.teiid.api.exception.query.FunctionExecutionException)3 TeiidRuntimeException (org.teiid.core.TeiidRuntimeException)3 BinaryType (org.teiid.core.types.BinaryType)3 Transform (org.teiid.core.types.Transform)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 BigInteger (java.math.BigInteger)2 SQLXML (java.sql.SQLXML)2 BlobImpl (org.teiid.core.types.BlobImpl)2 BlobType (org.teiid.core.types.BlobType)2 ClobImpl (org.teiid.core.types.ClobImpl)2