Search in sources :

Example 21 with XMLType

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

the class TestStringToXmlTransform method testGoodElement.

@Test
public void testGoodElement() throws Exception {
    String xml = // $NON-NLS-1$
    "<customer>\n" + // $NON-NLS-1$
    "<name>ABC</name>" + // $NON-NLS-1$
    "<age>32</age>" + // $NON-NLS-1$
    "</customer>";
    StringToSQLXMLTransform transform = new StringToSQLXMLTransform();
    XMLType xmlValue = (XMLType) transform.transformDirect(xml);
    assertEquals(xml.replaceAll("[\r]", ""), xmlValue.getString().replaceAll("[\r]", ""));
    assertEquals(XMLType.Type.ELEMENT, xmlValue.getType());
}
Also used : XMLType(org.teiid.core.types.XMLType) Test(org.junit.Test)

Example 22 with XMLType

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

the class TestResultSet method testXML.

@Test
public void testXML() throws Exception {
    StatementImpl statement = createMockStatement(ResultSet.TYPE_FORWARD_ONLY);
    ResultsFuture<LobChunk> future = new ResultsFuture<LobChunk>();
    future.getResultsReceiver().receiveResults(new LobChunk("<a/>".getBytes(Charset.forName("UTF-8")), true));
    XMLType result = new XMLType();
    Mockito.stub(statement.getDQP().requestNextLobChunk(0, 0, result.getReferenceStreamId())).toReturn(future);
    ResultsMessage resultsMsg = new ResultsMessage();
    result.setEncoding("UTF-8");
    resultsMsg.setResults(new List<?>[] { Arrays.asList(result) });
    resultsMsg.setLastRow(1);
    resultsMsg.setFirstRow(1);
    resultsMsg.setFinalRow(1);
    // $NON-NLS-1$
    resultsMsg.setColumnNames(new String[] { "x" });
    // $NON-NLS-1$
    resultsMsg.setDataTypes(new String[] { "xml" });
    ResultSetImpl cs = new ResultSetImpl(resultsMsg, statement);
    cs.next();
    assertEquals("<a/>", cs.getString(1));
}
Also used : ResultsFuture(org.teiid.client.util.ResultsFuture) XMLType(org.teiid.core.types.XMLType) ResultsMessage(org.teiid.client.ResultsMessage) LobChunk(org.teiid.client.lob.LobChunk) Test(org.junit.Test)

Example 23 with XMLType

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

the class TestProcessor method examineResults.

/**
 * @param expectedResults
 * @param bufferMgr
 * @param tsID
 * @throws TeiidComponentException
 * @throws TeiidProcessingException
 * @throws IOException
 * @since 4.3
 */
static void examineResults(List[] expectedResults, BufferManager bufferMgr, TupleBuffer tsID) throws TeiidComponentException, SQLException, TeiidProcessingException, IOException {
    // Create QueryResults from TupleSource
    TupleSource ts = tsID.createIndexedTupleSource();
    long count = tsID.getRowCount();
    // Compare actual to expected row count
    // $NON-NLS-1$
    assertEquals("Did not get expected row count: ", expectedResults.length, count);
    // Walk results and compare
    for (int i = 0; i < count; i++) {
        List record = ts.nextTuple();
        // handle xml
        if (record.size() == 1 && expectedResults[i].size() == 1) {
            Object cellValue = record.get(0);
            if (cellValue instanceof XMLType) {
                XMLType id = (XMLType) cellValue;
                String actualDoc = id.getString();
                if (expectedResults[i].size() == 1) {
                    compareDocuments((String) expectedResults[i].get(0), actualDoc);
                    continue;
                }
            } else if (cellValue instanceof Clob) {
                Clob id = (Clob) cellValue;
                String actualDoc = ClobType.getString(id);
                if (expectedResults[i].size() == 1) {
                    compareDocuments((String) expectedResults[i].get(0), actualDoc);
                    continue;
                }
            }
        }
        // $NON-NLS-1$ //$NON-NLS-2$
        assertEquals("Row " + i + " does not match expected: ", expectedResults[i], record);
    }
    ts.closeSource();
}
Also used : XMLType(org.teiid.core.types.XMLType) TupleSource(org.teiid.common.buffer.TupleSource) Clob(java.sql.Clob)

Example 24 with XMLType

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

the class TestSQLXMLProcessing method testStaxComment.

@Test
public void testStaxComment() throws Exception {
    // $NON-NLS-1$
    String sql = "select * from xmltable('/*:Person/phoneNumber' passing cast(? as xml) columns x string path 'type', y string path 'number') as x";
    List<?>[] expected = new List<?>[] { Arrays.asList(null, "8881112222") };
    XMLInputFactory factory = XMLInputFactory.newFactory();
    XMLEventReader reader = factory.createXMLEventReader(new StringReader("<Person><!--hello--><phoneNumber><number>8881112222</number></phoneNumber></Person>"));
    XMLType value = new XMLType(new StAXSQLXML(new StAXSource(reader)));
    value.setType(Type.DOCUMENT);
    Command command = helpParse(sql);
    CommandContext context = createCommandContext();
    QueryMetadataInterface metadata = RealMetadataFactory.example1Cached();
    context.setMetadata(metadata);
    CapabilitiesFinder capFinder = new DefaultCapabilitiesFinder();
    ProcessorPlan plan = helpGetPlan(command, metadata, capFinder, context);
    setParameterValues(Arrays.asList(value), command, context);
    doProcess(plan, dataManager, expected, context);
}
Also used : CommandContext(org.teiid.query.util.CommandContext) StAXSource(javax.xml.transform.stax.StAXSource) DefaultCapabilitiesFinder(org.teiid.query.optimizer.capabilities.DefaultCapabilitiesFinder) XMLType(org.teiid.core.types.XMLType) CapabilitiesFinder(org.teiid.query.optimizer.capabilities.CapabilitiesFinder) DefaultCapabilitiesFinder(org.teiid.query.optimizer.capabilities.DefaultCapabilitiesFinder) Command(org.teiid.query.sql.lang.Command) StringReader(java.io.StringReader) XMLEventReader(javax.xml.stream.XMLEventReader) List(java.util.List) QueryMetadataInterface(org.teiid.query.metadata.QueryMetadataInterface) XMLInputFactory(javax.xml.stream.XMLInputFactory) StAXSQLXML(org.teiid.util.StAXSQLXML) Test(org.junit.Test)

Example 25 with XMLType

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

the class TestFunctionResolving method assertEval.

/**
 * Helper to verify the result of an expression.
 *
 * @param expr SQL expression.
 * @param result Expected result.
 * @throws Exception
 */
public static void assertEval(String expr, String result) throws Exception {
    Expression ex = TestFunctionResolving.getExpression(expr);
    Object val = Evaluator.evaluate(ex);
    String valStr;
    if (val instanceof Clob) {
        valStr = ClobType.getString((Clob) val);
    } else if (val instanceof XMLType) {
        valStr = ((XMLType) val).getString();
    } else {
        valStr = val.toString();
    }
    assertEquals(result, valStr);
}
Also used : XMLType(org.teiid.core.types.XMLType) Expression(org.teiid.query.sql.symbol.Expression) Clob(java.sql.Clob)

Aggregations

XMLType (org.teiid.core.types.XMLType)25 SQLException (java.sql.SQLException)9 IOException (java.io.IOException)8 Test (org.junit.Test)7 SQLXMLImpl (org.teiid.core.types.SQLXMLImpl)7 Clob (java.sql.Clob)6 BlobType (org.teiid.core.types.BlobType)6 StringReader (java.io.StringReader)5 StAXSource (javax.xml.transform.stax.StAXSource)5 ClobType (org.teiid.core.types.ClobType)4 TranslatorException (org.teiid.translator.TranslatorException)4 XMLStreamException (javax.xml.stream.XMLStreamException)3 TransformerException (javax.xml.transform.TransformerException)3 StreamResult (javax.xml.transform.stream.StreamResult)3 WebServiceException (javax.xml.ws.WebServiceException)3 Item (net.sf.saxon.om.Item)3 InputStreamFactory (org.teiid.core.types.InputStreamFactory)3 TransformationException (org.teiid.core.types.TransformationException)3 Type (org.teiid.core.types.XMLType.Type)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2