Search in sources :

Example 11 with ClobType

use of org.teiid.core.types.ClobType 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 12 with ClobType

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

the class TextTableNode method initReader.

private void initReader() throws ExpressionEvaluationException, BlockedException, TeiidComponentException, TeiidProcessingException {
    setReferenceValues(this.table);
    ClobType file = (ClobType) getEvaluator(Collections.emptyMap()).evaluate(table.getFile(), null);
    if (file == null) {
        return;
    }
    // get the reader
    try {
        // $NON-NLS-1$
        this.systemId = "Unknown";
        if (file.getReference() instanceof ClobImpl) {
            this.systemId = ((ClobImpl) file.getReference()).getStreamFactory().getSystemId();
            if (this.systemId == null) {
                // $NON-NLS-1$
                this.systemId = "Unknown";
            }
        }
        Reader r = file.getCharacterStream();
        if (!(r instanceof BufferedReader)) {
            reader = new BufferedReader(r);
        } else {
            reader = (BufferedReader) r;
        }
    } catch (SQLException e) {
        throw new TeiidProcessingException(QueryPlugin.Event.TEIID30180, e);
    }
    // process the skip field
    if (skip <= 0) {
        return;
    }
    while (textLine < skip) {
        boolean isHeader = textLine == header;
        if (isHeader) {
            StringBuilder line = readLine(DataTypeManager.MAX_STRING_LENGTH * 16, false);
            if (line == null) {
                // just return an empty batch
                reset();
                return;
            }
            processHeader(parseLine(line));
        } else {
            while (readChar() != newLine) {
            }
        }
    }
}
Also used : ClobType(org.teiid.core.types.ClobType) SQLException(java.sql.SQLException) BufferedReader(java.io.BufferedReader) Reader(java.io.Reader) BufferedReader(java.io.BufferedReader) ClobImpl(org.teiid.core.types.ClobImpl) TeiidProcessingException(org.teiid.core.TeiidProcessingException)

Example 13 with ClobType

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

the class DataTierManagerImpl method processSystemQuery.

/**
 * @param command
 * @param workItem
 * @return
 * @throws TeiidComponentException
 * @throws TeiidProcessingException
 */
private TupleSource processSystemQuery(CommandContext context, Command command, DQPWorkContext workContext) throws TeiidComponentException, TeiidProcessingException {
    String vdbName = workContext.getVdbName();
    String vdbVersion = workContext.getVdbVersion();
    final VDBMetaData vdb = workContext.getVDB();
    TransformationMetadata indexMetadata = vdb.getAttachment(TransformationMetadata.class);
    CompositeMetadataStore metadata = indexMetadata.getMetadataStore();
    if (command instanceof Query) {
        Query query = (Query) command;
        UnaryFromClause ufc = (UnaryFromClause) query.getFrom().getClauses().get(0);
        GroupSymbol group = ufc.getGroup();
        if (StringUtil.startsWithIgnoreCase(group.getNonCorrelationName(), CoreConstants.SYSTEM_ADMIN_MODEL)) {
            final SystemAdminTables sysTable = SystemAdminTables.valueOf(group.getNonCorrelationName().substring(CoreConstants.SYSTEM_ADMIN_MODEL.length() + 1).toUpperCase());
            BaseExtractionTable<?> et = systemAdminTables.get(sysTable);
            return et.processQuery(query, vdb, indexMetadata, context);
        }
        final SystemTables sysTable = SystemTables.valueOf(group.getNonCorrelationName().substring(CoreConstants.SYSTEM_MODEL.length() + 1).toUpperCase());
        BaseExtractionTable<?> et = systemTables.get(sysTable);
        return et.processQuery(query, vdb, indexMetadata, context);
    }
    Collection<List<?>> rows = new ArrayList<List<?>>();
    StoredProcedure proc = (StoredProcedure) command;
    if (StringUtil.startsWithIgnoreCase(proc.getProcedureCallableName(), CoreConstants.SYSTEM_ADMIN_MODEL)) {
        final SystemAdminProcs sysProc = SystemAdminProcs.valueOf(proc.getProcedureCallableName().substring(CoreConstants.SYSTEM_ADMIN_MODEL.length() + 1).toUpperCase());
        switch(sysProc) {
            case LOGMSG:
            case ISLOGGABLE:
                String level = (String) ((Constant) proc.getParameter(2).getExpression()).getValue();
                String logContext = (String) ((Constant) proc.getParameter(3).getExpression()).getValue();
                Object message = null;
                if (sysProc == SystemAdminProcs.LOGMSG) {
                    message = ((Constant) proc.getParameter(4).getExpression()).getValue();
                }
                int msgLevel = getLevel(level);
                boolean logged = false;
                if (LogManager.isMessageToBeRecorded(logContext, msgLevel)) {
                    if (message != null) {
                        LogManager.log(msgLevel, logContext, message);
                    }
                    logged = true;
                }
                if (proc.returnParameters()) {
                    rows.add(Arrays.asList(logged));
                }
                return new CollectionTupleSource(rows.iterator());
            case SETPROPERTY:
                try {
                    String uuid = (String) ((Constant) proc.getParameter(2).getExpression()).getValue();
                    String key = (String) ((Constant) proc.getParameter(3).getExpression()).getValue();
                    Clob value = (Clob) ((Constant) proc.getParameter(4).getExpression()).getValue();
                    key = MetadataFactory.resolvePropertyKey(null, key);
                    String strVal = null;
                    String result = null;
                    if (value != null) {
                        if (value.length() > MAX_VALUE_LENGTH) {
                            throw new TeiidProcessingException(QueryPlugin.Event.TEIID30548, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30548, MAX_VALUE_LENGTH));
                        }
                        strVal = ObjectConverterUtil.convertToString(value.getCharacterStream());
                    }
                    final AbstractMetadataRecord target = getByUuid(metadata, uuid);
                    if (target == null) {
                        throw new TeiidProcessingException(QueryPlugin.Event.TEIID30549, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30549, uuid));
                    }
                    AbstractMetadataRecord schema = target;
                    while (!(schema instanceof Schema) && schema.getParent() != null) {
                        schema = schema.getParent();
                    }
                    if (schema instanceof Schema && vdb.getImportedModels().contains(((Schema) schema).getName())) {
                        // $NON-NLS-1$
                        throw new TeiidProcessingException(QueryPlugin.Event.TEIID31098, QueryPlugin.Util.getString("ValidationVisitor.invalid_alter", uuid));
                    }
                    if (getMetadataRepository(target, vdb) != null) {
                        getMetadataRepository(target, vdb).setProperty(vdbName, vdbVersion, target, key, strVal);
                    }
                    result = DdlPlan.setProperty(vdb, target, key, strVal);
                    if (eventDistributor != null) {
                        eventDistributor.setProperty(vdbName, vdbVersion, uuid, key, strVal);
                    }
                    // materialization depends upon the property values
                    // $NON-NLS-1$
                    indexMetadata.addToMetadataCache(target, "transformation/matview", null);
                    if (proc.returnParameters()) {
                        if (result == null) {
                            rows.add(Arrays.asList((Clob) null));
                        } else {
                            rows.add(Arrays.asList(new ClobType(new ClobImpl(result))));
                        }
                    }
                    return new CollectionTupleSource(rows.iterator());
                } catch (SQLException e) {
                    throw new TeiidProcessingException(QueryPlugin.Event.TEIID30550, e);
                } catch (IOException e) {
                    throw new TeiidProcessingException(QueryPlugin.Event.TEIID30551, e);
                }
        }
        final Table table = indexMetadata.getGroupID((String) ((Constant) proc.getParameter(1).getExpression()).getValue());
        switch(sysProc) {
            case SETCOLUMNSTATS:
                final String columnName = (String) ((Constant) proc.getParameter(2).getExpression()).getValue();
                Column c = null;
                for (Column col : table.getColumns()) {
                    if (col.getName().equalsIgnoreCase(columnName)) {
                        c = col;
                        break;
                    }
                }
                if (c == null) {
                    throw new TeiidProcessingException(QueryPlugin.Event.TEIID30552, columnName + TransformationMetadata.NOT_EXISTS_MESSAGE);
                }
                Number distinctVals = (Number) ((Constant) proc.getParameter(3).getExpression()).getValue();
                Number nullVals = (Number) ((Constant) proc.getParameter(4).getExpression()).getValue();
                String max = (String) ((Constant) proc.getParameter(5).getExpression()).getValue();
                String min = (String) ((Constant) proc.getParameter(6).getExpression()).getValue();
                final ColumnStats columnStats = new ColumnStats();
                columnStats.setDistinctValues(distinctVals);
                columnStats.setNullValues(nullVals);
                columnStats.setMaximumValue(max);
                columnStats.setMinimumValue(min);
                if (getMetadataRepository(table, vdb) != null) {
                    getMetadataRepository(table, vdb).setColumnStats(vdbName, vdbVersion, c, columnStats);
                }
                DdlPlan.setColumnStats(vdb, c, columnStats);
                if (eventDistributor != null) {
                    eventDistributor.setColumnStats(vdbName, vdbVersion, table.getParent().getName(), table.getName(), columnName, columnStats);
                }
                break;
            case SETTABLESTATS:
                Constant val = (Constant) proc.getParameter(2).getExpression();
                final Number cardinality = (Number) val.getValue();
                TableStats tableStats = new TableStats();
                tableStats.setCardinality(cardinality);
                if (getMetadataRepository(table, vdb) != null) {
                    getMetadataRepository(table, vdb).setTableStats(vdbName, vdbVersion, table, tableStats);
                }
                DdlPlan.setTableStats(vdb, table, tableStats);
                if (eventDistributor != null) {
                    eventDistributor.setTableStats(vdbName, vdbVersion, table.getParent().getName(), table.getName(), tableStats);
                }
                break;
        }
        return new CollectionTupleSource(rows.iterator());
    }
    final SystemProcs sysTable = SystemProcs.valueOf(proc.getProcedureCallableName().substring(CoreConstants.SYSTEM_MODEL.length() + 1).toUpperCase());
    switch(sysTable) {
        case GETXMLSCHEMAS:
            try {
                Object groupID = indexMetadata.getGroupID((String) ((Constant) proc.getParameter(1).getExpression()).getValue());
                List<SQLXMLImpl> schemas = indexMetadata.getXMLSchemas(groupID);
                for (SQLXMLImpl schema : schemas) {
                    rows.add(Arrays.asList(new XMLType(schema)));
                }
            } catch (QueryMetadataException e) {
                throw new TeiidProcessingException(QueryPlugin.Event.TEIID30553, e);
            }
            break;
        case ARRAYITERATE:
            Object array = ((Constant) proc.getParameter(1).getExpression()).getValue();
            if (array != null) {
                final Object[] vals;
                if (array instanceof Object[]) {
                    vals = (Object[]) array;
                } else {
                    ArrayImpl arrayImpl = (ArrayImpl) array;
                    vals = arrayImpl.getValues();
                }
                return new CollectionTupleSource(new Iterator<List<?>>() {

                    int index = 0;

                    @Override
                    public boolean hasNext() {
                        return index < vals.length;
                    }

                    @Override
                    public List<?> next() {
                        if (!hasNext()) {
                            throw new NoSuchElementException();
                        }
                        return Arrays.asList(vals[index++]);
                    }

                    @Override
                    public void remove() {
                        throw new UnsupportedOperationException();
                    }
                });
            }
    }
    return new CollectionTupleSource(rows.iterator());
}
Also used : Query(org.teiid.query.sql.lang.Query) UnaryFromClause(org.teiid.query.sql.lang.UnaryFromClause) SQLException(java.sql.SQLException) Constant(org.teiid.query.sql.symbol.Constant) ArrayImpl(org.teiid.core.types.ArrayImpl) ArrayList(java.util.ArrayList) CollectionTupleSource(org.teiid.query.processor.CollectionTupleSource) TeiidProcessingException(org.teiid.core.TeiidProcessingException) ArrayList(java.util.ArrayList) List(java.util.List) ClobImpl(org.teiid.core.types.ClobImpl) TransformationMetadata(org.teiid.query.metadata.TransformationMetadata) SQLXMLImpl(org.teiid.core.types.SQLXMLImpl) IOException(java.io.IOException) QueryMetadataException(org.teiid.api.exception.query.QueryMetadataException) ClobType(org.teiid.core.types.ClobType) XMLType(org.teiid.core.types.XMLType) CompositeMetadataStore(org.teiid.query.metadata.CompositeMetadataStore) StoredProcedure(org.teiid.query.sql.lang.StoredProcedure) VDBMetaData(org.teiid.adminapi.impl.VDBMetaData) GroupSymbol(org.teiid.query.sql.symbol.GroupSymbol) Clob(java.sql.Clob) NoSuchElementException(java.util.NoSuchElementException)

Example 14 with ClobType

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

the class LobManager method updateReferences.

@SuppressWarnings("unchecked")
public void updateReferences(List<?> tuple, ReferenceMode mode) throws TeiidComponentException {
    for (int i = 0; i < lobIndexes.length; i++) {
        Object anObj = tuple.get(lobIndexes[i]);
        if (!(anObj instanceof Streamable<?>)) {
            continue;
        }
        Streamable lob = (Streamable) anObj;
        String id = lob.getReferenceStreamId();
        LobHolder lobHolder = this.lobReferences.get(id);
        switch(mode) {
            case REMOVE:
                if (lobHolder != null) {
                    lobHolder.referenceCount--;
                    if (lobHolder.referenceCount < 1) {
                        this.lobReferences.remove(id);
                    }
                }
                break;
            case ATTACH:
                if (lob.getReference() == null) {
                    if (lobHolder == null) {
                        throw new TeiidComponentException(QueryPlugin.Event.TEIID30033, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30033));
                    }
                    lob.setReference(lobHolder.lob.getReference());
                }
                break;
            case CREATE:
                try {
                    StorageMode storageMode = InputStreamFactory.getStorageMode(lob);
                    if (lob.getReferenceStreamId() == null || (inlineLobs && (storageMode == StorageMode.MEMORY || (storageMode != StorageMode.FREE && lob.length() * (lob instanceof ClobType ? 2 : 1) <= maxMemoryBytes)))) {
                        lob.setReferenceStreamId(null);
                        // since this is untracked at this point, we must detach if possible
                        if (inlineLobs && storageMode == StorageMode.OTHER) {
                            persistLob(lob, null, null, true, maxMemoryBytes);
                        }
                        continue;
                    }
                } catch (SQLException e) {
                // presumably the lob is bad, but let it slide for now
                }
                if (lob.getReference() == null) {
                    throw new TeiidComponentException(QueryPlugin.Event.TEIID30034, QueryPlugin.Util.gs(QueryPlugin.Event.TEIID30034));
                }
                if (lobHolder == null) {
                    this.lobReferences.put(id, new LobHolder(lob));
                } else {
                    lobHolder.referenceCount++;
                }
                break;
        }
    }
}
Also used : ClobType(org.teiid.core.types.ClobType) SQLException(java.sql.SQLException) TeiidComponentException(org.teiid.core.TeiidComponentException) Streamable(org.teiid.core.types.Streamable) StorageMode(org.teiid.core.types.InputStreamFactory.StorageMode)

Example 15 with ClobType

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

the class LobManager method persistLob.

public static void persistLob(final Streamable<?> lob, final FileStore store, byte[] bytes, boolean inlineLobs, int maxMemoryBytes) throws TeiidComponentException {
    long byteLength = Integer.MAX_VALUE;
    try {
        byteLength = lob.length() * (lob instanceof ClobType ? 2 : 1);
    } catch (SQLException e) {
    // just ignore for now - for a single read resource computing the length invalidates
    // TODO - inline small persisted lobs
    }
    try {
        // inline
        if (lob.getReferenceStreamId() == null || (inlineLobs && (byteLength <= maxMemoryBytes))) {
            lob.setReferenceStreamId(null);
            if (InputStreamFactory.getStorageMode(lob) == StorageMode.MEMORY) {
                return;
            }
            if (lob instanceof BlobType) {
                BlobType b = (BlobType) lob;
                byte[] blobBytes = b.getBytes(1, (int) byteLength);
                b.setReference(new SerialBlob(blobBytes));
            } else if (lob instanceof ClobType) {
                ClobType c = (ClobType) lob;
                // $NON-NLS-1$
                String s = "";
                // some clob impls return null for 0 length
                if (byteLength != 0) {
                    s = c.getSubString(1, (int) (byteLength >>> 1));
                }
                c.setReference(new ClobImpl(s));
            } else {
                XMLType x = (XMLType) lob;
                String s = x.getString();
                x.setReference(new SQLXMLImpl(s));
            }
            return;
        }
        InputStream is = null;
        if (lob instanceof BlobType) {
            is = new BlobInputStreamFactory((Blob) lob).getInputStream();
        } else if (lob instanceof ClobType) {
            is = new ClobInputStreamFactory((Clob) lob).getInputStream();
        } else {
            is = new SQLXMLInputStreamFactory((SQLXML) lob).getInputStream();
        }
        long offset = store.getLength();
        OutputStream fsos = store.createOutputStream();
        byteLength = ObjectConverterUtil.write(fsos, is, bytes, -1);
        // re-construct the new lobs based on the file store
        final long lobOffset = offset;
        final long lobLength = byteLength;
        /*
			 * Using an inner class here will hold a reference to the LobManager
			 * which prevents the removal of the FileStore until all of the
			 * lobs have been gc'd
			 */
        InputStreamFactory isf = new InputStreamFactory() {

            @Override
            public InputStream getInputStream() throws IOException {
                return store.createInputStream(lobOffset, lobLength);
            }

            @Override
            public StorageMode getStorageMode() {
                return StorageMode.PERSISTENT;
            }
        };
        isf.setLength(byteLength);
        if (lob instanceof BlobType) {
            ((BlobType) lob).setReference(new BlobImpl(isf));
        } else if (lob instanceof ClobType) {
            long length = -1;
            try {
                length = ((ClobType) lob).length();
            } catch (SQLException e) {
            // could be streaming
            }
            ((ClobType) lob).setReference(new ClobImpl(isf, length));
        } else {
            ((XMLType) lob).setReference(new SQLXMLImpl(isf));
        }
    } catch (SQLException e) {
        throw new TeiidComponentException(QueryPlugin.Event.TEIID30037, e);
    } catch (IOException e) {
        throw new TeiidComponentException(QueryPlugin.Event.TEIID30036, e);
    }
}
Also used : SQLXMLInputStreamFactory(org.teiid.core.types.InputStreamFactory.SQLXMLInputStreamFactory) SQLXMLImpl(org.teiid.core.types.SQLXMLImpl) ClobInputStreamFactory(org.teiid.core.types.InputStreamFactory.ClobInputStreamFactory) SQLException(java.sql.SQLException) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) SerialBlob(javax.sql.rowset.serial.SerialBlob) IOException(java.io.IOException) InputStreamFactory(org.teiid.core.types.InputStreamFactory) ClobInputStreamFactory(org.teiid.core.types.InputStreamFactory.ClobInputStreamFactory) BlobInputStreamFactory(org.teiid.core.types.InputStreamFactory.BlobInputStreamFactory) SQLXMLInputStreamFactory(org.teiid.core.types.InputStreamFactory.SQLXMLInputStreamFactory) BlobInputStreamFactory(org.teiid.core.types.InputStreamFactory.BlobInputStreamFactory) ClobType(org.teiid.core.types.ClobType) XMLType(org.teiid.core.types.XMLType) BlobType(org.teiid.core.types.BlobType) SQLXML(java.sql.SQLXML) TeiidComponentException(org.teiid.core.TeiidComponentException) Clob(java.sql.Clob) ClobImpl(org.teiid.core.types.ClobImpl) BlobImpl(org.teiid.core.types.BlobImpl)

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