Search in sources :

Example 1 with XMLSerialize

use of org.teiid.query.sql.symbol.XMLSerialize in project teiid by teiid.

the class TeiidServiceHandler method handleLobResult.

private void handleLobResult(String charSet, Object result, ServiceResponse response) throws SQLException {
    if (result == null) {
        // or should this be an empty result?
        return;
    }
    if (result instanceof SQLXML) {
        if (charSet != null) {
            XMLSerialize serialize = new XMLSerialize();
            // $NON-NLS-1$
            serialize.setTypeString("blob");
            serialize.setDeclaration(true);
            serialize.setEncoding(charSet);
            serialize.setDocument(true);
            try {
                InputStream content = ((BlobType) XMLSystemFunctions.serialize(serialize, new XMLType((SQLXML) result))).getBinaryStream();
                response.writeContent(content, 200, false);
                response.writeOK(ContentType.APPLICATION_OCTET_STREAM);
            } catch (TransformationException e) {
                throw new SQLException(e);
            }
        } else {
            InputStream content = ((SQLXML) result).getBinaryStream();
            response.writeContent(content, 200, false);
            response.writeOK(ContentType.APPLICATION_XML);
        }
    } else if (result instanceof Blob) {
        InputStream content = ((Blob) result).getBinaryStream();
        response.writeContent(content, 200, false);
        response.writeOK(ContentType.APPLICATION_OCTET_STREAM);
    } else if (result instanceof Clob) {
        InputStream content = new ReaderInputStream(((Clob) result).getCharacterStream(), charSet == null ? Charset.defaultCharset() : Charset.forName(charSet));
        response.writeContent(content, 200, false);
        response.writeOK(ContentType.TEXT_PLAIN);
    } else {
        InputStream content = new ByteArrayInputStream(result.toString().getBytes(charSet == null ? Charset.defaultCharset() : Charset.forName(charSet)));
        response.writeContent(content, 200, false);
        response.writeOK(ContentType.APPLICATION_OCTET_STREAM);
    }
}
Also used : XMLType(org.teiid.core.types.XMLType) SQLXML(java.sql.SQLXML) XMLSerialize(org.teiid.query.sql.symbol.XMLSerialize) BlobType(org.teiid.core.types.BlobType) TransformationException(org.teiid.core.types.TransformationException) Blob(java.sql.Blob) ReaderInputStream(org.teiid.core.util.ReaderInputStream) SQLException(java.sql.SQLException) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) ReaderInputStream(org.teiid.core.util.ReaderInputStream) InputStream(java.io.InputStream) Clob(java.sql.Clob)

Example 2 with XMLSerialize

use of org.teiid.query.sql.symbol.XMLSerialize in project teiid by teiid.

the class TestFunctionResolving method testXMLSerialize_1.

@Test(expected = QueryResolverException.class)
public void testXMLSerialize_1() throws Exception {
    // $NON-NLS-1$
    String sql = "xmlserialize(DOCUMENT 1 as clob)";
    XMLSerialize xs = (XMLSerialize) getExpression(sql);
    assertEquals(DataTypeManager.DefaultDataClasses.CLOB, xs.getType());
}
Also used : XMLSerialize(org.teiid.query.sql.symbol.XMLSerialize) Test(org.junit.Test)

Example 3 with XMLSerialize

use of org.teiid.query.sql.symbol.XMLSerialize in project teiid by teiid.

the class TeiidRSProvider method handleResult.

private InputStream handleResult(String charSet, Object result) throws SQLException {
    if (result == null) {
        // or should this be an empty result?
        return null;
    }
    if (result instanceof SQLXML) {
        if (charSet != null) {
            XMLSerialize serialize = new XMLSerialize();
            // $NON-NLS-1$
            serialize.setTypeString("blob");
            serialize.setDeclaration(true);
            serialize.setEncoding(charSet);
            serialize.setDocument(true);
            try {
                return ((BlobType) XMLSystemFunctions.serialize(serialize, new XMLType((SQLXML) result))).getBinaryStream();
            } catch (TransformationException e) {
                throw new SQLException(e);
            }
        }
        return ((SQLXML) result).getBinaryStream();
    } else if (result instanceof Blob) {
        return ((Blob) result).getBinaryStream();
    } else if (result instanceof Clob) {
        return new ReaderInputStream(((Clob) result).getCharacterStream(), charSet == null ? Charset.defaultCharset() : Charset.forName(charSet));
    }
    return new ByteArrayInputStream(result.toString().getBytes(charSet == null ? Charset.defaultCharset() : Charset.forName(charSet)));
}
Also used : SQLXML(java.sql.SQLXML) XMLSerialize(org.teiid.query.sql.symbol.XMLSerialize) Blob(java.sql.Blob) ReaderInputStream(org.teiid.core.util.ReaderInputStream) SQLException(java.sql.SQLException) ByteArrayInputStream(java.io.ByteArrayInputStream) Clob(java.sql.Clob)

Example 4 with XMLSerialize

use of org.teiid.query.sql.symbol.XMLSerialize in project teiid by teiid.

the class TestFunctionResolving method testXMLSerialize.

@Test
public void testXMLSerialize() throws Exception {
    // $NON-NLS-1$
    String sql = "xmlserialize(DOCUMENT '<a/>' as clob)";
    XMLSerialize xs = (XMLSerialize) getExpression(sql);
    assertEquals(DataTypeManager.DefaultDataClasses.CLOB, xs.getType());
}
Also used : XMLSerialize(org.teiid.query.sql.symbol.XMLSerialize) Test(org.junit.Test)

Aggregations

XMLSerialize (org.teiid.query.sql.symbol.XMLSerialize)4 ByteArrayInputStream (java.io.ByteArrayInputStream)2 Blob (java.sql.Blob)2 Clob (java.sql.Clob)2 SQLException (java.sql.SQLException)2 SQLXML (java.sql.SQLXML)2 Test (org.junit.Test)2 ReaderInputStream (org.teiid.core.util.ReaderInputStream)2 InputStream (java.io.InputStream)1 BlobType (org.teiid.core.types.BlobType)1 TransformationException (org.teiid.core.types.TransformationException)1 XMLType (org.teiid.core.types.XMLType)1