Search in sources :

Example 6 with ClobType

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

the class JSONFunctionMethods method jsonParse.

@TeiidFunction(category = FunctionCategoryConstants.JSON)
public static ClobType jsonParse(BlobType val, boolean wellformed) throws SQLException, IOException, ParseException {
    InputStreamReader r = XMLSystemFunctions.getJsonReader(val);
    try {
        if (!wellformed) {
            JSONParser parser = new JSONParser();
            parser.parse(r, validatingContentHandler);
        }
        ClobImpl clobImpl = new ClobImpl();
        clobImpl.setStreamFactory(new InputStreamFactory.BlobInputStreamFactory(val.getReference()));
        clobImpl.setEncoding(r.getEncoding());
        ClobType ct = new ClobType(clobImpl);
        ct.setType(Type.JSON);
        return ct;
    } finally {
        r.close();
    }
}
Also used : ClobType(org.teiid.core.types.ClobType) InputStreamReader(java.io.InputStreamReader) JSONParser(org.teiid.json.simple.JSONParser) InputStreamFactory(org.teiid.core.types.InputStreamFactory) FileStoreInputStreamFactory(org.teiid.common.buffer.FileStoreInputStreamFactory) ClobImpl(org.teiid.core.types.ClobImpl)

Example 7 with ClobType

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

the class JSONFunctionMethods method jsonParse.

@TeiidFunction(category = FunctionCategoryConstants.JSON)
public static ClobType jsonParse(ClobType val, boolean wellformed) throws SQLException, IOException, ParseException {
    Reader r = null;
    if (val.getType() == Type.JSON) {
        return val;
    }
    if (!wellformed) {
        r = val.getCharacterStream();
    }
    try {
        if (!wellformed) {
            JSONParser parser = new JSONParser();
            parser.parse(r, validatingContentHandler);
        }
        ClobType ct = new ClobType(val.getReference());
        ct.setType(Type.JSON);
        return ct;
    } finally {
        if (r != null) {
            r.close();
        }
    }
}
Also used : ClobType(org.teiid.core.types.ClobType) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) JSONParser(org.teiid.json.simple.JSONParser)

Example 8 with ClobType

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

the class TestMongoDBQueryExecution method testGeoFunctionInWhereWithGeometry.

@Test
public void testGeoFunctionInWhereWithGeometry() throws Exception {
    Table table = this.utility.createRuntimeMetadata().getTable("northwind.Categories");
    NamedTable namedTable = new NamedTable("Categories", "g0", table);
    ColumnReference colRef = new ColumnReference(namedTable, "CategoryName", table.getColumnByName("CategoryName"), String.class);
    DerivedColumn col = new DerivedColumn("CategoryName", colRef);
    Select select = new Select();
    select.setDerivedColumns(Arrays.asList(col));
    List<TableReference> tables = new ArrayList<TableReference>();
    tables.add(namedTable);
    select.setFrom(tables);
    final GeometryType geo = GeometryUtils.geometryFromClob(new ClobType(new ClobImpl("POLYGON ((1.0 2.0,3.0 4.0,5.0 6.0,1.0 2.0))")));
    Function function = new // $NON-NLS-1$
    Function(// $NON-NLS-1$
    "mongo.geoWithin", // $NON-NLS-1$
    Arrays.asList(colRef, new Literal(geo, GeometryType.class)), // $NON-NLS-2$
    Boolean.class);
    function.setMetadataObject(getFunctionMethod("mongo.geoWithin"));
    Comparison c = new Comparison(function, new Literal(true, Boolean.class), Comparison.Operator.EQ);
    select.setWhere(c);
    DBCollection dbCollection = helpExecute(select, new String[] { "Categories" }, 2);
    BasicDBObjectBuilder builder = new BasicDBObjectBuilder();
    builder.push("CategoryName");
    // $NON-NLS-1$
    builder.push("$geoWithin");
    // $NON-NLS-1$
    builder.add("$geometry", "{\"type\":\"Polygon\",\"coordinates\":[[[1.0,2.0],[3.0,4.0],[5.0,6.0],[1.0,2.0]]]}");
    BasicDBObject result = new BasicDBObject();
    result.append("CategoryName", "$CategoryName");
    List<DBObject> pipeline = buildArray(new BasicDBObject("$match", builder.get()), new BasicDBObject("$project", result));
    Mockito.verify(dbCollection).aggregate(Mockito.eq(pipeline), Mockito.any(AggregationOptions.class));
}
Also used : NamedTable(org.teiid.language.NamedTable) NamedTable(org.teiid.language.NamedTable) Table(org.teiid.metadata.Table) ArrayList(java.util.ArrayList) ClobType(org.teiid.core.types.ClobType) GeometryType(org.teiid.core.types.GeometryType) Function(org.teiid.language.Function) TableReference(org.teiid.language.TableReference) Comparison(org.teiid.language.Comparison) Literal(org.teiid.language.Literal) Select(org.teiid.language.Select) DerivedColumn(org.teiid.language.DerivedColumn) ClobImpl(org.teiid.core.types.ClobImpl) ColumnReference(org.teiid.language.ColumnReference) Test(org.junit.Test)

Example 9 with ClobType

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

the class TestFunctionLibrary method testClobConcat.

@Test
public void testClobConcat() throws Exception {
    CommandContext c = new CommandContext();
    c.setBufferManager(BufferManagerFactory.getStandaloneBufferManager());
    ClobType result = (ClobType) helpInvokeMethod("concat", new Class<?>[] { DataTypeManager.DefaultDataClasses.CLOB, DataTypeManager.DefaultDataClasses.CLOB }, new Object[] { DataTypeManager.transformValue("<?xml version=\"1.0\" encoding=\"UTF-8\"?><Catalogs xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"><Catalog><Items><Item ItemID=\"001\"><Name>Lamp</Name><Quantity>5</Quantity></Item></Items></Catalog></Catalogs>", DataTypeManager.DefaultDataClasses.CLOB), DataTypeManager.transformValue("<?xml version=\"1.0\" encoding=\"UTF-8\"?><xsl:stylesheet version=\"1.0\" xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\"><xsl:template match=\"@*|node()\"><xsl:copy><xsl:apply-templates select=\"@*|node()\"/></xsl:copy></xsl:template><xsl:template match=\"Quantity\"/></xsl:stylesheet>", DataTypeManager.DefaultDataClasses.CLOB) }, c);
    String xml = ObjectConverterUtil.convertToString(result.getCharacterStream());
    assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\"?><Catalogs xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"><Catalog><Items><Item ItemID=\"001\">" + "<Name>Lamp</Name><Quantity>5</Quantity></Item></Items></Catalog></Catalogs><?xml version=\"1.0\" encoding=\"UTF-8\"?><xsl:stylesheet version=\"1.0\" xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\">" + "<xsl:template match=\"@*|node()\"><xsl:copy><xsl:apply-templates select=\"@*|node()\"/></xsl:copy></xsl:template><xsl:template match=\"Quantity\"/></xsl:stylesheet>", xml);
}
Also used : ClobType(org.teiid.core.types.ClobType) CommandContext(org.teiid.query.util.CommandContext) Test(org.junit.Test)

Example 10 with ClobType

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

the class TestFunctionMethods method testUpperLowerClob.

@Test
public void testUpperLowerClob() throws Exception {
    char[] val = new char[] { 87, 122, 147, 0xD801, 0xDC37 };
    assertEquals(new String(val).toUpperCase(), ClobType.getString(FunctionMethods.upperCase(new ClobType(ClobImpl.createClob(val)))));
    assertEquals(new String(val).toLowerCase(), ClobType.getString(FunctionMethods.lowerCase(new ClobType(ClobImpl.createClob(val)))));
}
Also used : ClobType(org.teiid.core.types.ClobType) Test(org.junit.Test)

Aggregations

ClobType (org.teiid.core.types.ClobType)49 Test (org.junit.Test)31 ClobImpl (org.teiid.core.types.ClobImpl)20 Expression (org.teiid.query.sql.symbol.Expression)16 SQLException (java.sql.SQLException)8 InputStreamFactory (org.teiid.core.types.InputStreamFactory)8 IOException (java.io.IOException)7 BlobType (org.teiid.core.types.BlobType)6 Reader (java.io.Reader)4 Blob (java.sql.Blob)4 ArrayList (java.util.ArrayList)4 SerialBlob (javax.sql.rowset.serial.SerialBlob)4 SerialClob (javax.sql.rowset.serial.SerialClob)4 BlobImpl (org.teiid.core.types.BlobImpl)4 XMLType (org.teiid.core.types.XMLType)4 FunctionExecutionException (org.teiid.api.exception.query.FunctionExecutionException)3 TeiidProcessingException (org.teiid.core.TeiidProcessingException)3 BlobInputStreamFactory (org.teiid.core.types.InputStreamFactory.BlobInputStreamFactory)3 SQLXMLImpl (org.teiid.core.types.SQLXMLImpl)3 ReaderInputStream (org.teiid.core.util.ReaderInputStream)3