Search in sources :

Example 1 with UpdateStream

use of org.wso2.siddhi.query.api.execution.query.output.stream.UpdateStream in project siddhi by wso2.

the class OutputParser method constructOutputCallback.

public static OutputCallback constructOutputCallback(final OutputStream outStream, StreamDefinition outputStreamDefinition, Map<String, Table> tableMap, Map<String, Window> eventWindowMap, SiddhiAppContext siddhiAppContext, boolean convertToStreamEvent, String queryName) {
    String id = outStream.getId();
    Table table = null;
    Window window = null;
    if (id != null) {
        table = tableMap.get(id);
        window = eventWindowMap.get(id);
    }
    StreamEventPool streamEventPool = null;
    StreamEventConverter streamEventConverter = null;
    MetaStreamEvent tableMetaStreamEvent = null;
    if (table != null) {
        tableMetaStreamEvent = new MetaStreamEvent();
        tableMetaStreamEvent.setEventType(MetaStreamEvent.EventType.TABLE);
        TableDefinition matchingTableDefinition = TableDefinition.id("");
        for (Attribute attribute : outputStreamDefinition.getAttributeList()) {
            tableMetaStreamEvent.addOutputData(attribute);
            matchingTableDefinition.attribute(attribute.getName(), attribute.getType());
        }
        matchingTableDefinition.setQueryContextStartIndex(outStream.getQueryContextStartIndex());
        matchingTableDefinition.setQueryContextEndIndex(outStream.getQueryContextEndIndex());
        tableMetaStreamEvent.addInputDefinition(matchingTableDefinition);
        streamEventPool = new StreamEventPool(tableMetaStreamEvent, 10);
        streamEventConverter = new ZeroStreamEventConverter();
    }
    // Construct CallBack
    if (outStream instanceof InsertIntoStream) {
        if (window != null) {
            return new InsertIntoWindowCallback(window, outputStreamDefinition, queryName);
        } else if (table != null) {
            DefinitionParserHelper.validateOutputStream(outputStreamDefinition, table.getTableDefinition());
            return new InsertIntoTableCallback(table, outputStreamDefinition, convertToStreamEvent, streamEventPool, streamEventConverter, queryName);
        } else {
            return new InsertIntoStreamCallback(outputStreamDefinition, queryName);
        }
    } else if (outStream instanceof DeleteStream || outStream instanceof UpdateStream || outStream instanceof UpdateOrInsertStream) {
        if (table != null) {
            if (outStream instanceof UpdateStream) {
                if (((UpdateStream) outStream).getUpdateSet() == null) {
                    TableDefinition tableDefinition = table.getTableDefinition();
                    for (Attribute attribute : outputStreamDefinition.getAttributeList()) {
                        if (!tableDefinition.getAttributeList().contains(attribute)) {
                            throw new SiddhiAppCreationException("Attribute " + attribute + " does not exist on " + "Event Table " + tableDefinition, outStream.getQueryContextStartIndex(), outStream.getQueryContextEndIndex());
                        }
                    }
                }
            }
            if (outStream instanceof UpdateOrInsertStream) {
                TableDefinition tableDefinition = table.getTableDefinition();
                for (Attribute attribute : outputStreamDefinition.getAttributeList()) {
                    if (!tableDefinition.getAttributeList().contains(attribute)) {
                        throw new SiddhiAppCreationException("Attribute " + attribute + " does not exist on " + "Event Table " + tableDefinition, outStream.getQueryContextStartIndex(), outStream.getQueryContextEndIndex());
                    }
                }
            }
            if (outStream instanceof DeleteStream) {
                try {
                    MatchingMetaInfoHolder matchingMetaInfoHolder = MatcherParser.constructMatchingMetaStateHolder(tableMetaStreamEvent, 0, table.getTableDefinition(), 0);
                    CompiledCondition compiledCondition = table.compileCondition((((DeleteStream) outStream).getOnDeleteExpression()), matchingMetaInfoHolder, siddhiAppContext, null, tableMap, queryName);
                    StateEventPool stateEventPool = new StateEventPool(matchingMetaInfoHolder.getMetaStateEvent(), 10);
                    return new DeleteTableCallback(table, compiledCondition, matchingMetaInfoHolder.getMatchingStreamEventIndex(), convertToStreamEvent, stateEventPool, streamEventPool, streamEventConverter, queryName);
                } catch (SiddhiAppValidationException e) {
                    throw new SiddhiAppCreationException("Cannot create delete for table '" + outStream.getId() + "', " + e.getMessageWithOutContext(), e, e.getQueryContextStartIndex(), e.getQueryContextEndIndex(), siddhiAppContext.getName(), siddhiAppContext.getSiddhiAppString());
                }
            } else if (outStream instanceof UpdateStream) {
                try {
                    MatchingMetaInfoHolder matchingMetaInfoHolder = MatcherParser.constructMatchingMetaStateHolder(tableMetaStreamEvent, 0, table.getTableDefinition(), 0);
                    CompiledCondition compiledCondition = table.compileCondition((((UpdateStream) outStream).getOnUpdateExpression()), matchingMetaInfoHolder, siddhiAppContext, null, tableMap, queryName);
                    UpdateSet updateSet = ((UpdateStream) outStream).getUpdateSet();
                    if (updateSet == null) {
                        updateSet = new UpdateSet();
                        for (Attribute attribute : matchingMetaInfoHolder.getMatchingStreamDefinition().getAttributeList()) {
                            updateSet.set(new Variable(attribute.getName()), new Variable(attribute.getName()));
                        }
                    }
                    CompiledUpdateSet compiledUpdateSet = table.compileUpdateSet(updateSet, matchingMetaInfoHolder, siddhiAppContext, null, tableMap, queryName);
                    StateEventPool stateEventPool = new StateEventPool(matchingMetaInfoHolder.getMetaStateEvent(), 10);
                    return new UpdateTableCallback(table, compiledCondition, compiledUpdateSet, matchingMetaInfoHolder.getMatchingStreamEventIndex(), convertToStreamEvent, stateEventPool, streamEventPool, streamEventConverter, queryName);
                } catch (SiddhiAppValidationException e) {
                    throw new SiddhiAppCreationException("Cannot create update for table '" + outStream.getId() + "', " + e.getMessageWithOutContext(), e, e.getQueryContextStartIndex(), e.getQueryContextEndIndex(), siddhiAppContext);
                }
            } else {
                DefinitionParserHelper.validateOutputStream(outputStreamDefinition, table.getTableDefinition());
                try {
                    MatchingMetaInfoHolder matchingMetaInfoHolder = MatcherParser.constructMatchingMetaStateHolder(tableMetaStreamEvent, 0, table.getTableDefinition(), 0);
                    CompiledCondition compiledCondition = table.compileCondition((((UpdateOrInsertStream) outStream).getOnUpdateExpression()), matchingMetaInfoHolder, siddhiAppContext, null, tableMap, queryName);
                    UpdateSet updateSet = ((UpdateOrInsertStream) outStream).getUpdateSet();
                    if (updateSet == null) {
                        updateSet = new UpdateSet();
                        for (Attribute attribute : matchingMetaInfoHolder.getMatchingStreamDefinition().getAttributeList()) {
                            updateSet.set(new Variable(attribute.getName()), new Variable(attribute.getName()));
                        }
                    }
                    CompiledUpdateSet compiledUpdateSet = table.compileUpdateSet(updateSet, matchingMetaInfoHolder, siddhiAppContext, null, tableMap, queryName);
                    StateEventPool stateEventPool = new StateEventPool(matchingMetaInfoHolder.getMetaStateEvent(), 10);
                    return new UpdateOrInsertTableCallback(table, compiledCondition, compiledUpdateSet, matchingMetaInfoHolder.getMatchingStreamEventIndex(), convertToStreamEvent, stateEventPool, streamEventPool, streamEventConverter, queryName);
                } catch (SiddhiAppValidationException e) {
                    throw new SiddhiAppCreationException("Cannot create update or insert into for table '" + outStream.getId() + "', " + e.getMessageWithOutContext(), e, e.getQueryContextStartIndex(), e.getQueryContextEndIndex(), siddhiAppContext);
                }
            }
        } else {
            throw new SiddhiAppCreationException("Event table with id :" + id + " does not exist", outStream.getQueryContextStartIndex(), outStream.getQueryContextEndIndex());
        }
    } else {
        throw new SiddhiAppCreationException(outStream.getClass().getName() + " not supported", outStream.getQueryContextStartIndex(), outStream.getQueryContextEndIndex());
    }
}
Also used : Variable(org.wso2.siddhi.query.api.expression.Variable) Attribute(org.wso2.siddhi.query.api.definition.Attribute) InsertIntoWindowCallback(org.wso2.siddhi.core.query.output.callback.InsertIntoWindowCallback) UpdateStream(org.wso2.siddhi.query.api.execution.query.output.stream.UpdateStream) ZeroStreamEventConverter(org.wso2.siddhi.core.event.stream.converter.ZeroStreamEventConverter) UpdateTableCallback(org.wso2.siddhi.core.query.output.callback.UpdateTableCallback) DeleteStream(org.wso2.siddhi.query.api.execution.query.output.stream.DeleteStream) StreamEventPool(org.wso2.siddhi.core.event.stream.StreamEventPool) UpdateOrInsertTableCallback(org.wso2.siddhi.core.query.output.callback.UpdateOrInsertTableCallback) TableDefinition(org.wso2.siddhi.query.api.definition.TableDefinition) InsertIntoTableCallback(org.wso2.siddhi.core.query.output.callback.InsertIntoTableCallback) Window(org.wso2.siddhi.core.window.Window) Table(org.wso2.siddhi.core.table.Table) StateEventPool(org.wso2.siddhi.core.event.state.StateEventPool) SiddhiAppCreationException(org.wso2.siddhi.core.exception.SiddhiAppCreationException) StreamEventConverter(org.wso2.siddhi.core.event.stream.converter.StreamEventConverter) ZeroStreamEventConverter(org.wso2.siddhi.core.event.stream.converter.ZeroStreamEventConverter) InsertIntoStream(org.wso2.siddhi.query.api.execution.query.output.stream.InsertIntoStream) SiddhiAppValidationException(org.wso2.siddhi.query.api.exception.SiddhiAppValidationException) InsertIntoStreamCallback(org.wso2.siddhi.core.query.output.callback.InsertIntoStreamCallback) CompiledUpdateSet(org.wso2.siddhi.core.table.CompiledUpdateSet) UpdateOrInsertStream(org.wso2.siddhi.query.api.execution.query.output.stream.UpdateOrInsertStream) CompiledCondition(org.wso2.siddhi.core.util.collection.operator.CompiledCondition) MatchingMetaInfoHolder(org.wso2.siddhi.core.util.collection.operator.MatchingMetaInfoHolder) DeleteTableCallback(org.wso2.siddhi.core.query.output.callback.DeleteTableCallback) UpdateSet(org.wso2.siddhi.query.api.execution.query.output.stream.UpdateSet) CompiledUpdateSet(org.wso2.siddhi.core.table.CompiledUpdateSet) MetaStreamEvent(org.wso2.siddhi.core.event.stream.MetaStreamEvent)

Example 2 with UpdateStream

use of org.wso2.siddhi.query.api.execution.query.output.stream.UpdateStream in project siddhi by wso2.

the class SiddhiQLBaseVisitorImpl method visitQuery_output.

/**
 * {@inheritDoc}
 * <p>The default implementation returns the result of calling
 * {@link #visitChildren} on {@code ctx}.</p>
 *
 * @param ctx
 */
@Override
public OutputStream visitQuery_output(@NotNull SiddhiQLParser.Query_outputContext ctx) {
    if (ctx.INSERT() != null) {
        Source source = (Source) visit(ctx.target());
        if (ctx.UPDATE() != null && ctx.OR() != null) {
            if (source.isInnerStream) {
                throw newSiddhiParserException(ctx, "UPDATE OR INTO INSERT be only used with Tables!");
            }
            if (ctx.output_event_type() != null) {
                if (ctx.set_clause() != null) {
                    OutputStream outputStream = new UpdateOrInsertStream(source.streamId, (OutputStream.OutputEventType) visit(ctx.output_event_type()), (UpdateSet) visit(ctx.set_clause()), (Expression) visit(ctx.expression()));
                    populateQueryContext(outputStream, ctx);
                    return outputStream;
                } else {
                    OutputStream outputStream = new UpdateOrInsertStream(source.streamId, (OutputStream.OutputEventType) visit(ctx.output_event_type()), (Expression) visit(ctx.expression()));
                    populateQueryContext(outputStream, ctx);
                    return outputStream;
                }
            } else {
                if (ctx.set_clause() != null) {
                    OutputStream outputStream = new UpdateOrInsertStream(source.streamId, (UpdateSet) visit(ctx.set_clause()), (Expression) visit(ctx.expression()));
                    populateQueryContext(outputStream, ctx);
                    return outputStream;
                } else {
                    OutputStream outputStream = new UpdateOrInsertStream(source.streamId, (Expression) visit(ctx.expression()));
                    populateQueryContext(outputStream, ctx);
                    return outputStream;
                }
            }
        } else {
            if (ctx.output_event_type() != null) {
                OutputStream outputStream = new InsertIntoStream(source.streamId, source.isInnerStream, (OutputStream.OutputEventType) visit(ctx.output_event_type()));
                populateQueryContext(outputStream, ctx);
                return outputStream;
            } else {
                OutputStream outputStream = new InsertIntoStream(source.streamId, source.isInnerStream);
                populateQueryContext(outputStream, ctx);
                return outputStream;
            }
        }
    } else if (ctx.DELETE() != null) {
        Source source = (Source) visit(ctx.target());
        if (source.isInnerStream) {
            throw newSiddhiParserException(ctx, "DELETE can be only used with Tables!");
        }
        if (ctx.output_event_type() != null) {
            OutputStream outputStream = new DeleteStream(source.streamId, (OutputStream.OutputEventType) visit(ctx.output_event_type()), (Expression) visit(ctx.expression()));
            populateQueryContext(outputStream, ctx);
            return outputStream;
        } else {
            OutputStream outputStream = new DeleteStream(source.streamId, (Expression) visit(ctx.expression()));
            populateQueryContext(outputStream, ctx);
            return outputStream;
        }
    } else if (ctx.UPDATE() != null) {
        Source source = (Source) visit(ctx.target());
        if (source.isInnerStream) {
            throw newSiddhiParserException(ctx, "DELETE can be only used with Tables!");
        }
        if (ctx.output_event_type() != null) {
            if (ctx.set_clause() != null) {
                OutputStream outputStream = new UpdateStream(source.streamId, (OutputStream.OutputEventType) visit(ctx.output_event_type()), (UpdateSet) visit(ctx.set_clause()), (Expression) visit(ctx.expression()));
                populateQueryContext(outputStream, ctx);
                return outputStream;
            } else {
                OutputStream outputStream = new UpdateStream(source.streamId, (OutputStream.OutputEventType) visit(ctx.output_event_type()), (Expression) visit(ctx.expression()));
                populateQueryContext(outputStream, ctx);
                return outputStream;
            }
        } else {
            if (ctx.set_clause() != null) {
                OutputStream outputStream = new UpdateStream(source.streamId, (UpdateSet) visit(ctx.set_clause()), (Expression) visit(ctx.expression()));
                populateQueryContext(outputStream, ctx);
                return outputStream;
            } else {
                OutputStream outputStream = new UpdateStream(source.streamId, (Expression) visit(ctx.expression()));
                populateQueryContext(outputStream, ctx);
                return outputStream;
            }
        }
    } else if (ctx.RETURN() != null) {
        if (ctx.output_event_type() != null) {
            OutputStream outputStream = new ReturnStream((OutputStream.OutputEventType) visit(ctx.output_event_type()));
            populateQueryContext(outputStream, ctx);
            return outputStream;
        } else {
            OutputStream outputStream = new ReturnStream();
            populateQueryContext(outputStream, ctx);
            return outputStream;
        }
    } else {
        throw newSiddhiParserException(ctx);
    }
}
Also used : UpdateOrInsertStream(org.wso2.siddhi.query.api.execution.query.output.stream.UpdateOrInsertStream) Expression(org.wso2.siddhi.query.api.expression.Expression) DeleteStream(org.wso2.siddhi.query.api.execution.query.output.stream.DeleteStream) OutputStream(org.wso2.siddhi.query.api.execution.query.output.stream.OutputStream) InsertIntoStream(org.wso2.siddhi.query.api.execution.query.output.stream.InsertIntoStream) UpdateStream(org.wso2.siddhi.query.api.execution.query.output.stream.UpdateStream) ReturnStream(org.wso2.siddhi.query.api.execution.query.output.stream.ReturnStream)

Aggregations

DeleteStream (org.wso2.siddhi.query.api.execution.query.output.stream.DeleteStream)2 InsertIntoStream (org.wso2.siddhi.query.api.execution.query.output.stream.InsertIntoStream)2 UpdateOrInsertStream (org.wso2.siddhi.query.api.execution.query.output.stream.UpdateOrInsertStream)2 UpdateStream (org.wso2.siddhi.query.api.execution.query.output.stream.UpdateStream)2 StateEventPool (org.wso2.siddhi.core.event.state.StateEventPool)1 MetaStreamEvent (org.wso2.siddhi.core.event.stream.MetaStreamEvent)1 StreamEventPool (org.wso2.siddhi.core.event.stream.StreamEventPool)1 StreamEventConverter (org.wso2.siddhi.core.event.stream.converter.StreamEventConverter)1 ZeroStreamEventConverter (org.wso2.siddhi.core.event.stream.converter.ZeroStreamEventConverter)1 SiddhiAppCreationException (org.wso2.siddhi.core.exception.SiddhiAppCreationException)1 DeleteTableCallback (org.wso2.siddhi.core.query.output.callback.DeleteTableCallback)1 InsertIntoStreamCallback (org.wso2.siddhi.core.query.output.callback.InsertIntoStreamCallback)1 InsertIntoTableCallback (org.wso2.siddhi.core.query.output.callback.InsertIntoTableCallback)1 InsertIntoWindowCallback (org.wso2.siddhi.core.query.output.callback.InsertIntoWindowCallback)1 UpdateOrInsertTableCallback (org.wso2.siddhi.core.query.output.callback.UpdateOrInsertTableCallback)1 UpdateTableCallback (org.wso2.siddhi.core.query.output.callback.UpdateTableCallback)1 CompiledUpdateSet (org.wso2.siddhi.core.table.CompiledUpdateSet)1 Table (org.wso2.siddhi.core.table.Table)1 CompiledCondition (org.wso2.siddhi.core.util.collection.operator.CompiledCondition)1 MatchingMetaInfoHolder (org.wso2.siddhi.core.util.collection.operator.MatchingMetaInfoHolder)1