Ejemplo n.º 1
0
        /// <summary>
        ///     Renders the object model in it's EPL syntax textual representation, using a whitespace-formatter as provided.
        /// </summary>
        /// <param name="formatter">the formatter to use</param>
        /// <param name="writer">writer to use</param>
        /// <throws>IllegalStateException if required clauses do not exist</throws>
        public void ToEPL(
            EPStatementFormatter formatter,
            TextWriter writer)
        {
            AnnotationPart.ToEPL(writer, Annotations, formatter);
            ExpressionDeclaration.ToEPL(writer, ExpressionDeclarations, formatter);
            ScriptExpression.ToEPL(writer, ScriptExpressions, formatter);

            if (ContextName != null)
            {
                formatter.BeginContext(writer);
                writer.Write("context ");
                writer.Write(ContextName);
            }

            if (CreateIndex != null)
            {
                formatter.BeginCreateIndex(writer);
                CreateIndex.ToEPL(writer);
                return;
            }

            if (CreateSchema != null)
            {
                formatter.BeginCreateSchema(writer);
                CreateSchema.ToEPL(writer);
                return;
            }

            if (CreateExpression != null)
            {
                formatter.BeginCreateExpression(writer);
                CreateExpression.ToEPL(writer);
                return;
            }

            if (CreateContext != null)
            {
                formatter.BeginCreateContext(writer);
                CreateContext.ToEPL(writer, formatter);
                return;
            }

            if (CreateWindow != null)
            {
                formatter.BeginCreateWindow(writer);
                CreateWindow.ToEPL(writer);

                writer.Write(" as ");

                if (SelectClause == null || SelectClause.SelectList.IsEmpty() && !CreateWindow.Columns.IsEmpty())
                {
                    CreateWindow.ToEPLCreateTablePart(writer);
                }
                else
                {
                    SelectClause.ToEPL(writer, formatter, false, false);
                    if (CreateWindow.AsEventTypeName != null)
                    {
                        writer.Write(" from ");
                        writer.Write(CreateWindow.AsEventTypeName);
                    }

                    CreateWindow.ToEPLInsertPart(writer);
                }

                return;
            }

            if (CreateVariable != null)
            {
                formatter.BeginCreateVariable(writer);
                CreateVariable.ToEPL(writer);
                return;
            }

            if (CreateTable != null)
            {
                formatter.BeginCreateTable(writer);
                CreateTable.ToEPL(writer);
                return;
            }

            if (CreateDataFlow != null)
            {
                formatter.BeginCreateDataFlow(writer);
                CreateDataFlow.ToEPL(writer, formatter);
                return;
            }

            var displayWhereClause = true;

            if (UpdateClause != null)
            {
                formatter.BeginUpdate(writer);
                UpdateClause.ToEPL(writer);
            }
            else if (OnExpr != null)
            {
                formatter.BeginOnTrigger(writer);
                writer.Write("on ");
                FromClause.Streams[0].ToEPL(writer, formatter);

                if (OnExpr is OnDeleteClause onDeleteClause)
                {
                    formatter.BeginOnDelete(writer);
                    writer.Write("delete from ");
                    onDeleteClause.ToEPL(writer);
                }
                else if (OnExpr is OnUpdateClause onUpdateClause)
                {
                    formatter.BeginOnUpdate(writer);
                    writer.Write("update ");
                    onUpdateClause.ToEPL(writer);
                }
                else if (OnExpr is OnSelectClause onSelectClause)
                {
                    InsertInto?.ToEPL(writer, formatter, true);
                    SelectClause.ToEPL(writer, formatter, true, onSelectClause.IsDeleteAndSelect);
                    writer.Write(" from ");
                    onSelectClause.ToEPL(writer);
                }
                else if (OnExpr is OnSetClause onSetClause)
                {
                    onSetClause.ToEPL(writer, formatter);
                }
                else if (OnExpr is OnMergeClause onMergeClause)
                {
                    onMergeClause.ToEPL(writer, WhereClause, formatter);
                    displayWhereClause = false;
                }
                else
                {
                    var split = (OnInsertSplitStreamClause)OnExpr;
                    InsertInto.ToEPL(writer, formatter, true);
                    SelectClause.ToEPL(writer, formatter, true, false);
                    if (WhereClause != null)
                    {
                        writer.Write(" where ");
                        WhereClause.ToEPL(writer, ExpressionPrecedenceEnum.MINIMUM);
                    }

                    split.ToEPL(writer, formatter);
                    displayWhereClause = false;
                }
            }
            else
            {
                IntoTableClause?.ToEPL(writer);

                if (SelectClause == null)
                {
                    throw new IllegalStateException("Select-clause has not been defined");
                }

                if (FromClause == null)
                {
                    throw new IllegalStateException("From-clause has not been defined");
                }

                if (FireAndForgetClause is FireAndForgetUpdate fireAndForgetUpdate)
                {
                    writer.Write("update ");
                    FromClause.ToEPLOptions(writer, formatter, false);
                    writer.Write(" ");
                    UpdateClause.RenderEPLAssignments(writer, fireAndForgetUpdate.Assignments);
                }
                else if (FireAndForgetClause is FireAndForgetInsert fireAndForgetInsert)
                {
                    InsertInto.ToEPL(writer, formatter, true);
                    if (fireAndForgetInsert.IsUseValuesKeyword)
                    {
                        writer.Write(" values (");
                        var delimiter = "";
                        foreach (var element in SelectClause.SelectList)
                        {
                            writer.Write(delimiter);
                            element.ToEPLElement(writer);
                            delimiter = ", ";
                        }

                        writer.Write(")");
                    }
                    else
                    {
                        SelectClause.ToEPL(writer, formatter, true, false);
                    }
                }
                else if (FireAndForgetClause is FireAndForgetDelete)
                {
                    writer.Write("delete ");
                    FromClause.ToEPLOptions(writer, formatter, true);
                }
                else
                {
                    InsertInto?.ToEPL(writer, formatter, true);
                    SelectClause.ToEPL(writer, formatter, true, false);
                    FromClause.ToEPLOptions(writer, formatter, true);
                }
            }

            MatchRecognizeClause?.ToEPL(writer);

            if (WhereClause != null && displayWhereClause)
            {
                formatter.BeginWhere(writer);
                writer.Write("where ");
                WhereClause.ToEPL(writer, ExpressionPrecedenceEnum.MINIMUM);
            }

            if (GroupByClause != null)
            {
                formatter.BeginGroupBy(writer);
                writer.Write("group by ");
                GroupByClause.ToEPL(writer);
            }

            if (HavingClause != null)
            {
                formatter.BeginHaving(writer);
                writer.Write("having ");
                HavingClause.ToEPL(writer, ExpressionPrecedenceEnum.MINIMUM);
            }

            if (OutputLimitClause != null)
            {
                formatter.BeginOutput(writer);
                writer.Write("output ");
                OutputLimitClause.ToEPL(writer);
            }

            if (OrderByClause != null)
            {
                formatter.BeginOrderBy(writer);
                writer.Write("order by ");
                OrderByClause.ToEPL(writer);
            }

            if (RowLimitClause != null)
            {
                formatter.BeginLimit(writer);
                writer.Write("limit ");
                RowLimitClause.ToEPL(writer);
            }

            if (ForClause != null)
            {
                formatter.BeginFor(writer);
                ForClause.ToEPL(writer);
            }
        }
Ejemplo n.º 2
0
 /// <summary>
 ///     Specify an order-by-clause.
 /// </summary>
 /// <param name="orderByClause">specifies the order-by-clause, which is optional and can be null</param>
 /// <returns>model</returns>
 public EPStatementObjectModel SetOrderBy(OrderByClause orderByClause)
 {
     OrderByClause = orderByClause;
     return(this);
 }