Search in sources :

Example 6 with SQLXMLImpl

use of org.teiid.core.types.SQLXMLImpl 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 7 with SQLXMLImpl

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

the class WSProcedureExecution method getOutputParameterValues.

@Override
public List<?> getOutputParameterValues() throws TranslatorException {
    Object result = returnValue;
    if (returnValue != null && (returnValue instanceof StAXSource) && procedure.getArguments().size() > 4 && procedure.getArguments().get(4).getDirection() == Direction.IN && Boolean.TRUE.equals(procedure.getArguments().get(4).getArgumentValue().getValue())) {
        SQLXMLImpl sqlXml = new StAXSQLXML((StAXSource) returnValue);
        XMLType xml = new XMLType(sqlXml);
        xml.setType(Type.DOCUMENT);
        result = xml;
    } else if (returnValue != null && returnValue instanceof DOMSource) {
        final DOMSource xmlSource = (DOMSource) returnValue;
        SQLXMLImpl sqlXml = new SQLXMLImpl(new InputStreamFactory() {

            @Override
            public InputStream getInputStream() throws IOException {
                ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
                Result outputTarget = new StreamResult(outputStream);
                try {
                    TransformerFactory.newInstance().newTransformer().transform(xmlSource, outputTarget);
                } catch (Exception e) {
                    throw new IOException(e);
                }
                return new ByteArrayInputStream(outputStream.toByteArray());
            }
        });
        XMLType xml = new XMLType(sqlXml);
        xml.setType(Type.DOCUMENT);
        result = xml;
    }
    return Arrays.asList(result);
}
Also used : SQLXMLImpl(org.teiid.core.types.SQLXMLImpl) DOMSource(javax.xml.transform.dom.DOMSource) StreamResult(javax.xml.transform.stream.StreamResult) StAXSource(javax.xml.transform.stax.StAXSource) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) InputStreamFactory(org.teiid.core.types.InputStreamFactory) TransformerException(javax.xml.transform.TransformerException) SQLException(java.sql.SQLException) XMLStreamException(javax.xml.stream.XMLStreamException) TranslatorException(org.teiid.translator.TranslatorException) DataNotAvailableException(org.teiid.translator.DataNotAvailableException) IOException(java.io.IOException) WebServiceException(javax.xml.ws.WebServiceException) StreamResult(javax.xml.transform.stream.StreamResult) Result(javax.xml.transform.Result) XMLType(org.teiid.core.types.XMLType) ByteArrayInputStream(java.io.ByteArrayInputStream) StAXSQLXML(org.teiid.util.StAXSQLXML)

Example 8 with SQLXMLImpl

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

the class WSWSDLProcedureExecution method getOutputParameterValues.

@Override
public List<?> getOutputParameterValues() throws TranslatorException {
    Object result = returnValue;
    if (returnValue != null && procedure.getArguments().size() > 1 && Boolean.TRUE.equals(procedure.getArguments().get(1).getArgumentValue().getValue())) {
        SQLXMLImpl sqlXml = new StAXSQLXML(returnValue);
        XMLType xml = new XMLType(sqlXml);
        xml.setType(Type.DOCUMENT);
        result = xml;
    }
    return Arrays.asList(result);
}
Also used : XMLType(org.teiid.core.types.XMLType) SQLXMLImpl(org.teiid.core.types.SQLXMLImpl) StAXSQLXML(org.teiid.util.StAXSQLXML)

Example 9 with SQLXMLImpl

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

the class SaxonXQueryExpression method createXMLType.

public XMLType createXMLType(final SequenceIterator iter, BufferManager bufferManager, boolean emptyOnEmpty, CommandContext context) throws XPathException, TeiidComponentException, TeiidProcessingException {
    final Item item = iter.next();
    if (item == null && !emptyOnEmpty) {
        return null;
    }
    XMLType.Type type = Type.CONTENT;
    if (item instanceof NodeInfo) {
        NodeInfo info = (NodeInfo) item;
        type = getType(info);
    }
    final Item next = iter.next();
    if (next != null) {
        type = Type.CONTENT;
    }
    SQLXMLImpl xml = XMLSystemFunctions.saveToBufferManager(bufferManager, new XMLTranslator() {

        @Override
        public void translate(Writer writer) throws TransformerException, IOException {
            QueryResult.serializeSequence(new PushBackSequenceIterator(iter, item, next), config, writer, DEFAULT_OUTPUT_PROPERTIES);
        }
    }, context);
    XMLType value = new XMLType(xml);
    value.setType(type);
    return value;
}
Also used : Item(net.sf.saxon.om.Item) NamespaceItem(org.teiid.query.sql.symbol.XMLNamespaces.NamespaceItem) XMLType(org.teiid.core.types.XMLType) SQLXMLImpl(org.teiid.core.types.SQLXMLImpl) NodeInfo(net.sf.saxon.om.NodeInfo) Type(org.teiid.core.types.XMLType.Type) IOException(java.io.IOException) XMLTranslator(org.teiid.core.types.XMLTranslator) Writer(java.io.Writer) TransformerException(javax.xml.transform.TransformerException)

Example 10 with SQLXMLImpl

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

the class StatementImpl method executeShow.

ResultsFuture<Boolean> executeShow(Matcher match) throws SQLException {
    String show = match.group(1);
    show = unescapeId(show);
    if (show.equalsIgnoreCase("PLAN")) {
        // $NON-NLS-1$
        List<ArrayList<Object>> records = new ArrayList<ArrayList<Object>>(1);
        PlanNode plan = driverConnection.getCurrentPlanDescription();
        String connDebugLog = driverConnection.getDebugLog();
        if (plan != null || connDebugLog != null) {
            ArrayList<Object> row = new ArrayList<Object>(3);
            if (plan != null) {
                row.add(DataTypeTransformer.getClob(plan.toString()));
                row.add(new SQLXMLImpl(plan.toXml()));
            } else {
                row.add(null);
                row.add(null);
            }
            row.add(DataTypeTransformer.getClob(connDebugLog));
            records.add(row);
        }
        createResultSet(// $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
        records, // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
        new String[] { "PLAN_TEXT", "PLAN_XML", "DEBUG_LOG" }, new String[] { DataTypeManager.DefaultDataTypes.CLOB, DataTypeManager.DefaultDataTypes.XML, DataTypeManager.DefaultDataTypes.CLOB });
        return booleanFuture(true);
    }
    if (show.equalsIgnoreCase("ANNOTATIONS")) {
        // $NON-NLS-1$
        List<ArrayList<Object>> records = new ArrayList<ArrayList<Object>>(1);
        Collection<Annotation> annos = driverConnection.getAnnotations();
        for (Annotation annotation : annos) {
            ArrayList<Object> row = new ArrayList<Object>(4);
            row.add(annotation.getCategory());
            row.add(annotation.getPriority().name());
            row.add(annotation.getAnnotation());
            row.add(annotation.getResolution());
            records.add(row);
        }
        createResultSet(// $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
        records, // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
        new String[] { "CATEGORY", "PRIORITY", "ANNOTATION", "RESOLUTION" }, new String[] { DataTypeManager.DefaultDataTypes.STRING, DataTypeManager.DefaultDataTypes.STRING, DataTypeManager.DefaultDataTypes.STRING, DataTypeManager.DefaultDataTypes.STRING });
        return booleanFuture(true);
    }
    if (show.equalsIgnoreCase("ALL")) {
        // $NON-NLS-1$
        List<ArrayList<Object>> records = new ArrayList<ArrayList<Object>>(1);
        for (String key : driverConnection.getExecutionProperties().stringPropertyNames()) {
            ArrayList<Object> row = new ArrayList<Object>(4);
            row.add(key);
            row.add(driverConnection.getExecutionProperties().get(key));
            records.add(row);
        }
        createResultSet(// $NON-NLS-1$ //$NON-NLS-2$
        records, // $NON-NLS-1$ //$NON-NLS-2$
        new String[] { "NAME", "VALUE" }, new String[] { DataTypeManager.DefaultDataTypes.STRING, DataTypeManager.DefaultDataTypes.STRING });
        return booleanFuture(true);
    }
    if (show.equalsIgnoreCase("transaction isolation level")) {
        // $NON-NLS-1$
        List<ArrayList<Object>> records = new ArrayList<ArrayList<Object>>(1);
        ArrayList<Object> row = new ArrayList<Object>(1);
        switch(driverConnection.getTransactionIsolation()) {
            case Connection.TRANSACTION_READ_COMMITTED:
                // $NON-NLS-1$
                row.add("READ COMMITTED");
                break;
            case Connection.TRANSACTION_READ_UNCOMMITTED:
                // $NON-NLS-1$
                row.add("READ UNCOMMITTED");
                break;
            case Connection.TRANSACTION_REPEATABLE_READ:
                // $NON-NLS-1$
                row.add("REPEATABLE READ");
                break;
            case Connection.TRANSACTION_SERIALIZABLE:
                // $NON-NLS-1$
                row.add("SERIALIZABLE");
                break;
            default:
                // $NON-NLS-1$
                row.add("UNKNOWN");
        }
        records.add(row);
        createResultSet(// $NON-NLS-1$
        records, // $NON-NLS-1$
        new String[] { "TRANSACTION ISOLATION" }, new String[] { DataTypeManager.DefaultDataTypes.STRING });
        return booleanFuture(true);
    }
    List<List<String>> records = Collections.singletonList(Collections.singletonList(driverConnection.getExecutionProperty(show)));
    createResultSet(records, new String[] { show }, new String[] { DataTypeManager.DefaultDataTypes.STRING });
    return booleanFuture(true);
}
Also used : SQLXMLImpl(org.teiid.core.types.SQLXMLImpl) PlanNode(org.teiid.client.plan.PlanNode) Annotation(org.teiid.client.plan.Annotation)

Aggregations

SQLXMLImpl (org.teiid.core.types.SQLXMLImpl)14 XMLType (org.teiid.core.types.XMLType)8 IOException (java.io.IOException)7 InputStreamFactory (org.teiid.core.types.InputStreamFactory)7 ClobImpl (org.teiid.core.types.ClobImpl)6 BlobImpl (org.teiid.core.types.BlobImpl)5 InputStream (java.io.InputStream)4 ClobType (org.teiid.core.types.ClobType)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 SQLException (java.sql.SQLException)3 BinaryType (org.teiid.core.types.BinaryType)3 BlobType (org.teiid.core.types.BlobType)3 BigInteger (java.math.BigInteger)2 Blob (java.sql.Blob)2 Clob (java.sql.Clob)2 SQLXML (java.sql.SQLXML)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 TransformerException (javax.xml.transform.TransformerException)2 QueryMetadataException (org.teiid.api.exception.query.QueryMetadataException)2