Search in sources :

Example 1 with ClobType

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

the class S3ProcedureExecution method next.

@Override
public List<?> next() throws TranslatorException, DataNotAvailableException {
    if (this.command.getProcedureName().equalsIgnoreCase(S3ExecutionFactory.SAVEFILE) || this.command.getProcedureName().equalsIgnoreCase(S3ExecutionFactory.DELETEFILE)) {
        return null;
    }
    if (this.execution == null || this.execution.getResponseCode() < 200 || this.execution.getResponseCode() > 300) {
        return null;
    }
    Blob contents = (Blob) execution.getOutputParameterValues().get(0);
    BlobInputStreamFactory isf = new BlobInputStreamFactory(contents);
    String length = getHeader("Content-Length");
    if (length != null) {
        isf.setLength(Long.parseLong(length));
    }
    Object value = null;
    if (isText) {
        ClobImpl clob = new ClobImpl(isf, -1);
        clob.setCharset(Charset.forName(this.ef.getEncoding()));
        value = new ClobType(clob);
        if (!streaming) {
            value = new InputStreamFactory.ClobInputStreamFactory(clob);
        }
    } else {
        if (streaming) {
            value = new BlobType(contents);
        } else {
            value = isf;
        }
    }
    String lastModified = getHeader("Last-Modified");
    ArrayList<Object> result = new ArrayList<Object>(2);
    result.add(value);
    if (!isList) {
        result.add(endpoint);
        try {
            SimpleDateFormat df = new SimpleDateFormat("EEE, dd MMM yyyy hh:mm:ss zzz");
            result.add(lastModified == null ? null : new Timestamp(df.parse(lastModified).getTime()));
        } catch (ParseException e) {
            result.add(null);
        }
        result.add(getHeader("ETag"));
        result.add(length);
    }
    this.execution = null;
    return result;
}
Also used : Blob(java.sql.Blob) ArrayList(java.util.ArrayList) InputStreamFactory(org.teiid.core.types.InputStreamFactory) BlobInputStreamFactory(org.teiid.core.types.InputStreamFactory.BlobInputStreamFactory) Timestamp(java.sql.Timestamp) BlobInputStreamFactory(org.teiid.core.types.InputStreamFactory.BlobInputStreamFactory) ClobType(org.teiid.core.types.ClobType) BlobType(org.teiid.core.types.BlobType) ParseException(java.text.ParseException) ClobImpl(org.teiid.core.types.ClobImpl) SimpleDateFormat(java.text.SimpleDateFormat)

Example 2 with ClobType

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

the class FunctionMethods method toChars.

@TeiidFunction(category = FunctionCategoryConstants.CONVERSION, name = "to_chars")
public static ClobType toChars(BlobType value, String encoding, boolean wellFormed) throws SQLException, IOException {
    Charset cs = getCharset(encoding);
    BlobInputStreamFactory bisf = new BlobInputStreamFactory(value.getReference());
    ClobImpl clob = new ClobImpl(bisf, -1);
    clob.setCharset(cs);
    if (!wellFormed && !CharsetUtils.BASE64_NAME.equalsIgnoreCase(encoding) && !CharsetUtils.HEX_NAME.equalsIgnoreCase(encoding)) {
        // validate that the charcter conversion is possible
        // TODO: cache the result in a filestore
        Reader r = clob.getCharacterStream();
        try {
            while (r.read() != -1) {
            }
        } catch (IOException e) {
            CharacterCodingException cce = ExceptionUtil.getExceptionOfType(e, CharacterCodingException.class);
            if (cce != null) {
                throw new IOException(CorePlugin.Util.gs(CorePlugin.Event.TEIID10082, cs.displayName()), cce);
            }
            throw e;
        } finally {
            r.close();
        }
    }
    return new ClobType(clob);
}
Also used : ClobType(org.teiid.core.types.ClobType) Charset(java.nio.charset.Charset) FilterReader(java.io.FilterReader) Reader(java.io.Reader) IOException(java.io.IOException) CharacterCodingException(java.nio.charset.CharacterCodingException) ClobImpl(org.teiid.core.types.ClobImpl) BlobInputStreamFactory(org.teiid.core.types.InputStreamFactory.BlobInputStreamFactory)

Example 3 with ClobType

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

the class GeometryUtils method geometryToClob.

public static ClobType geometryToClob(GeometryType geometry, boolean withSrid) throws FunctionExecutionException {
    Geometry jtsGeometry = getGeometry(geometry);
    int srid = jtsGeometry.getSRID();
    StringBuilder geomText = new StringBuilder();
    if (withSrid && srid != GeometryType.UNKNOWN_SRID) {
        // $NON-NLS-1$ //$NON-NLS-2$
        geomText.append("SRID=").append(jtsGeometry.getSRID()).append(";");
    }
    geomText.append(jtsGeometry.toText());
    return new ClobType(new ClobImpl(geomText.toString()));
}
Also used : ClobType(org.teiid.core.types.ClobType) ClobImpl(org.teiid.core.types.ClobImpl)

Example 4 with ClobType

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

the class GeometryUtils method geometryToGeoJson.

public static ClobType geometryToGeoJson(GeometryType geometry) throws FunctionExecutionException {
    Geometry jtsGeometry = getGeometry(geometry);
    GeoJSONWriter writer = new GeoJSONWriter();
    try {
        GeoJSON geoJson = writer.write(jtsGeometry);
        ClobType result = new ClobType(new ClobImpl(geoJson.toString()));
        result.setType(Type.JSON);
        return result;
    } catch (Exception e) {
        throw new FunctionExecutionException(e);
    }
}
Also used : ClobType(org.teiid.core.types.ClobType) FunctionExecutionException(org.teiid.api.exception.query.FunctionExecutionException) GeoJSON(org.wololo.geojson.GeoJSON) GeoJSONWriter(org.wololo.jts2geojson.GeoJSONWriter) ClobImpl(org.teiid.core.types.ClobImpl) SQLException(java.sql.SQLException) ParseException(com.vividsolutions.jts.io.ParseException) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXException(org.xml.sax.SAXException) FunctionExecutionException(org.teiid.api.exception.query.FunctionExecutionException)

Example 5 with ClobType

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

the class GeometryUtils method geometryToGml.

public static ClobType geometryToGml(CommandContext ctx, GeometryType geometry, boolean withGmlPrefix) throws FunctionExecutionException {
    Geometry jtsGeometry = getGeometry(geometry);
    GMLWriter writer = new GMLWriter();
    if (!withGmlPrefix) {
        if (geometry.getSrid() != SRID_4326) {
            if (geometry.getSrid() == GeometryType.UNKNOWN_SRID) {
                throw new FunctionExecutionException(QueryPlugin.Util.gs(QueryPlugin.Event.TEIID31161));
            }
            jtsGeometry = GeometryTransformUtils.transform(ctx, jtsGeometry, SRID_4326);
        }
        writer.setPrefix(null);
    } else if (geometry.getSrid() != GeometryType.UNKNOWN_SRID) {
    // TODO: should include the srsName
    // writer.setSrsName(String.valueOf(geometry.getSrid()));
    }
    String gmlText = writer.write(jtsGeometry);
    return new ClobType(new ClobImpl(gmlText));
}
Also used : ClobType(org.teiid.core.types.ClobType) FunctionExecutionException(org.teiid.api.exception.query.FunctionExecutionException) GMLWriter(com.vividsolutions.jts.io.gml2.GMLWriter) ClobImpl(org.teiid.core.types.ClobImpl)

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