Search in sources :

Example 6 with TransformationException

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

the class SQLXMLToStringTransform method transformDirect.

/**
 * This method transforms a value of the source type into a value
 * of the target type.
 * @param value Incoming value of source type
 * @return Outgoing value of target type
 * @throws TransformationException if value is an incorrect input type or
 * the transformation fails
 */
public Object transformDirect(Object value) throws TransformationException {
    XMLType source = (XMLType) value;
    Reader reader = null;
    try {
        char[] result = new char[DataTypeManager.MAX_STRING_LENGTH];
        reader = source.getCharacterStream();
        int read = reader.read(result);
        return new String(result, 0, read);
    } catch (SQLException e) {
        throw new TransformationException(CorePlugin.Event.TEIID10080, e, CorePlugin.Util.gs(CorePlugin.Event.TEIID10080, new Object[] { getSourceType().getName(), getTargetType().getName() }));
    } catch (IOException e) {
        throw new TransformationException(CorePlugin.Event.TEIID10080, e, CorePlugin.Util.gs(CorePlugin.Event.TEIID10080, new Object[] { getSourceType().getName(), getTargetType().getName() }));
    } finally {
        try {
            if (reader != null) {
                reader.close();
            }
        } catch (IOException e) {
        }
    }
}
Also used : XMLType(org.teiid.core.types.XMLType) TransformationException(org.teiid.core.types.TransformationException) SQLException(java.sql.SQLException) Reader(java.io.Reader) IOException(java.io.IOException)

Example 7 with TransformationException

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

the class ClobToStringTransform method transformDirect.

/**
 * This method transforms a value of the source type into a value
 * of the target type.
 * @param value Incoming value of source type
 * @return Outgoing value of target type
 * @throws TransformationException if value is an incorrect input type or
 * the transformation fails
 */
public Object transformDirect(Object value) throws TransformationException {
    ClobType source = (ClobType) value;
    BufferedReader reader = null;
    try {
        reader = new BufferedReader(source.getCharacterStream());
        StringBuffer contents = new StringBuffer();
        int chr = reader.read();
        while (chr != -1 && contents.length() < DataTypeManager.MAX_STRING_LENGTH) {
            contents.append((char) chr);
            chr = reader.read();
        }
        return contents.toString();
    } catch (SQLException e) {
        throw new TransformationException(CorePlugin.Event.TEIID10080, e, CorePlugin.Util.gs(CorePlugin.Event.TEIID10080, new Object[] { getSourceType().getName(), getTargetType().getName() }));
    } catch (IOException e) {
        throw new TransformationException(CorePlugin.Event.TEIID10080, e, CorePlugin.Util.gs(CorePlugin.Event.TEIID10080, new Object[] { getSourceType().getName(), getTargetType().getName() }));
    } finally {
        if (reader != null) {
            try {
                reader.close();
            } catch (IOException e) {
            }
        }
    }
}
Also used : ClobType(org.teiid.core.types.ClobType) TransformationException(org.teiid.core.types.TransformationException) SQLException(java.sql.SQLException) BufferedReader(java.io.BufferedReader) IOException(java.io.IOException)

Example 8 with TransformationException

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

the class StringToDateTransform method transformDirect.

/**
 * This method transforms a value of the source type into a value
 * of the target type.
 * @param value Incoming value of source type
 * @return Outgoing value of target type
 * @throws TransformationException if value is an incorrect input type or
 * the transformation fails
 */
public Object transformDirect(Object value) throws TransformationException {
    value = ((String) value).trim();
    Date result = null;
    try {
        result = Date.valueOf((String) value);
    } catch (Exception e) {
        if (!validate && pattern.matcher((String) value).matches()) {
            throw new TransformationException(CorePlugin.Event.TEIID10060, CorePlugin.Util.gs(CorePlugin.Event.TEIID10060, value, getTargetType().getSimpleName()));
        }
        throw new TransformationException(CorePlugin.Event.TEIID10061, e, CorePlugin.Util.gs(CorePlugin.Event.TEIID10061, value));
    }
    if (!result.toString().equals(value)) {
        throw new TransformationException(CorePlugin.Event.TEIID10060, CorePlugin.Util.gs(CorePlugin.Event.TEIID10060, value, getTargetType().getSimpleName()));
    }
    return result;
}
Also used : TransformationException(org.teiid.core.types.TransformationException) Date(java.sql.Date) TransformationException(org.teiid.core.types.TransformationException)

Example 9 with TransformationException

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

the class TextTableNode method process.

private void process() throws TeiidProcessingException {
    while (true) {
        synchronized (this) {
            if (isBatchFull()) {
                return;
            }
            StringBuilder line = readLine(lineWidth, table.isFixedWidth());
            if (line == null) {
                terminateBatches();
                break;
            }
            String parentSelector = null;
            if (table.getSelector() != null) {
                if (line.length() < table.getSelector().length()) {
                    continue;
                }
                if (!line.substring(0, table.getSelector().length()).equals(table.getSelector())) {
                    if (parentLines == null) {
                        // doesn't match any selector
                        continue;
                    }
                    parentSelector = line.substring(0, table.getSelector().length());
                    if (!parentLines.containsKey(parentSelector)) {
                        // doesn't match any selector
                        continue;
                    }
                }
            }
            List<String> vals = parseLine(line);
            if (parentSelector != null) {
                this.parentLines.put(parentSelector, vals);
                continue;
            } else if (table.getSelector() != null && !table.getSelector().equals(vals.get(0))) {
                continue;
            }
            rowNumber++;
            List<Object> tuple = new ArrayList<Object>(projectionIndexes.length);
            for (int output : projectionIndexes) {
                TextColumn col = table.getColumns().get(output);
                String val = null;
                int index = output;
                if (col.isOrdinal()) {
                    if (rowNumber > Integer.MAX_VALUE) {
                        throw new TeiidRuntimeException(new TeiidProcessingException(QueryPlugin.Event.TEIID31174, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID31174)));
                    }
                    tuple.add((int) rowNumber);
                    continue;
                }
                if (col.getSelector() != null) {
                    vals = this.parentLines.get(col.getSelector());
                    index = col.getPosition() - 1;
                } else if (nameIndexes != null) {
                    index = nameIndexes.get(col.getName());
                }
                if (vals == null || index >= vals.size()) {
                    // throw new TeiidProcessingException(QueryPlugin.Util.getString("TextTableNode.no_value", col.getName(), textLine, systemId)); //$NON-NLS-1$
                    tuple.add(null);
                    continue;
                }
                val = vals.get(index);
                try {
                    tuple.add(DataTypeManager.transformValue(val, table.getColumns().get(output).getSymbol().getType()));
                } catch (TransformationException e) {
                    throw new TeiidProcessingException(QueryPlugin.Event.TEIID30176, e, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30176, col.getName(), textLine, systemId));
                }
            }
            addBatchRow(tuple);
            if (rowNumber == limit) {
                terminateBatches();
                break;
            }
        }
    }
}
Also used : TransformationException(org.teiid.core.types.TransformationException) ArrayList(java.util.ArrayList) LanguageObject(org.teiid.query.sql.LanguageObject) TeiidRuntimeException(org.teiid.core.TeiidRuntimeException) TextColumn(org.teiid.query.sql.lang.TextTable.TextColumn) TeiidProcessingException(org.teiid.core.TeiidProcessingException)

Example 10 with TransformationException

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

the class ExcelExecution method convertFromExcelType.

Object convertFromExcelType(final Double value, Cell cell, final Class<?> expectedType) throws TranslatorException {
    if (value == null) {
        return null;
    }
    if (expectedType.isAssignableFrom(Double.class)) {
        return value;
    } else if (expectedType.isAssignableFrom(Timestamp.class)) {
        Date date = cell.getDateCellValue();
        return new Timestamp(date.getTime());
    } else if (expectedType.isAssignableFrom(java.sql.Date.class)) {
        Date date = cell.getDateCellValue();
        return TimestampWithTimezone.createDate(date);
    } else if (expectedType.isAssignableFrom(java.sql.Time.class)) {
        Date date = cell.getDateCellValue();
        return TimestampWithTimezone.createTime(date);
    }
    if (expectedType == String.class && dataFormatter != null) {
        return dataFormatter.formatCellValue(cell);
    }
    Object val = value;
    if (DateUtil.isCellDateFormatted(cell)) {
        Date date = cell.getDateCellValue();
        val = new java.sql.Timestamp(date.getTime());
    }
    try {
        return DataTypeManager.transformValue(val, expectedType);
    } catch (TransformationException e) {
        throw new TranslatorException(e);
    }
}
Also used : TransformationException(org.teiid.core.types.TransformationException) Timestamp(java.sql.Timestamp) TranslatorException(org.teiid.translator.TranslatorException) Timestamp(java.sql.Timestamp) Date(java.util.Date)

Aggregations

TransformationException (org.teiid.core.types.TransformationException)22 IOException (java.io.IOException)6 SQLException (java.sql.SQLException)6 Timestamp (java.sql.Timestamp)4 ArrayList (java.util.ArrayList)4 XMLType (org.teiid.core.types.XMLType)4 TranslatorException (org.teiid.translator.TranslatorException)4 InputStream (java.io.InputStream)3 Blob (java.sql.Blob)3 Date (java.sql.Date)3 FunctionExecutionException (org.teiid.api.exception.query.FunctionExecutionException)3 TeiidRuntimeException (org.teiid.core.TeiidRuntimeException)3 BinaryType (org.teiid.core.types.BinaryType)3 Transform (org.teiid.core.types.Transform)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 BigInteger (java.math.BigInteger)2 SQLXML (java.sql.SQLXML)2 BlobImpl (org.teiid.core.types.BlobImpl)2 BlobType (org.teiid.core.types.BlobType)2 ClobImpl (org.teiid.core.types.ClobImpl)2