示例#1
0
        protected internal override Node VisitDeclareStatement(DeclareStatement node)
        {
            node = (DeclareStatement)base.VisitDeclareStatement(node);

            var plan = node.Declarations.Count == 1
                           ? this.data.GetQueryPlan(node.Declarations[0])
                           : new CombinedQueryPlan(node.Declarations.Select(this.data.GetQueryPlan));

            this.data.SetQueryPlan(node, plan);

            return(node);
        }
示例#2
0
            protected internal override Node VisitDeclareStatement([NotNull] DeclareStatement node)
            {
                this.Builder.Append("DECLARE ");

                for (var i = 0; i < node.Declarations.Count; i++)
                {
                    this.Visit(node.Declarations[i]);

                    if (i < node.Declarations.Count - 1)
                    {
                        this.Builder.Append(", ");
                    }
                }

                return(node);
            }
示例#3
0
        protected override void VisitDeclareStatement(DeclareStatement statement)
        {
            if (statement.Variable != null)
            {
                State.Variables.Add(statement.Variable);

                State.Write(Symbols.DECLARE);
                State.Write(statement.Variable.Name);

                VisitType(statement.Variable);

                if (statement.Initializer != null)
                {
                    State.Write(PostgrSQLSymbols.AssignValSign);
                    VisitToken(statement.Initializer);
                }
            }
        }
示例#4
0
        public void Execute()
        {
            var statements = new List <IStatement>();

            if (_statement.Arguments.Any())
            {
                DeclareStatement declare = new DeclareStatement();
                declare.Definitions.AddRange(
                    _statement.Arguments.Select(a => new VariableDefinition(a.Name, a.Type))
                    );
                statements.Add(declare);

                SelectStatement initialise = new SelectStatement();
                initialise.Fields.AddRange(
                    _statement.Arguments.Select(a =>
                                                new Field
                {
                    Expression = new CriteriaExpression(null)
                    {
                        Left     = new StringExpression(a.Name, null),
                        Operator = Constants.Assignment,
                        Right    = new StringExpression(a.Value.ToString(), null)
                    }
                }
                                                )
                    );

                statements.Add(initialise);
            }

            statements.AddRange(_statement.InnerStatements);

            var last = statements.Last();

            foreach (IStatement statement in statements)
            {
                FormatStatement(statement);
                if (statement != last)
                {
                    NewLine(2);
                }
            }
        }
示例#5
0
        public DeclareStatement ParseDeclareStatement(DeclareVariableContext node)
        {
            DeclareStatement statement = new DeclareStatement();

            statement.Name = new TokenInfo(node.uidList().uid().First())
            {
                Type = TokenType.VariableName
            };
            statement.DataType = new TokenInfo(node.dataType().GetText())
            {
                Type = TokenType.DataType
            };

            var defaultValue = node.defaultValue();

            if (defaultValue != null)
            {
                statement.DefaultValue = new TokenInfo(defaultValue);
            }

            return(statement);
        }
示例#6
0
 /// <summary>
 /// Visits a declare statement.
 /// </summary>
 /// <param name="node">
 /// The node.
 /// </param>
 /// <returns>
 /// The node, or a new version of the node.
 /// </returns>
 protected internal virtual Node VisitDeclareStatement([NotNull] DeclareStatement node)
 {
     return(node.VisitChildren(this));
 }
示例#7
0
        public string GenerateRoutineScripts(RoutineScript script)
        {
            StringBuilder sb = new StringBuilder();

            sb.AppendLine($"CREATE {script.Type.ToString()} {script.FullName}");

            sb.AppendLine("(");

            if (script.Parameters.Count > 0)
            {
                int i = 0;
                foreach (Parameter parameter in script.Parameters)
                {
                    ParameterType parameterType = parameter.ParameterType;

                    string strParameterType = "";

                    if (parameterType.HasFlag(ParameterType.IN) && parameterType.HasFlag(ParameterType.OUT))
                    {
                        strParameterType = "INOUT";
                    }
                    else if (parameterType != ParameterType.NONE)
                    {
                        strParameterType = parameterType.ToString();
                    }

                    sb.AppendLine($"{strParameterType} {parameter.Name} {parameter.DataType}{(i == script.Parameters.Count - 1 ? "" : ",")}");

                    i++;
                }
            }

            sb.AppendLine(")");

            if (script.Type == RoutineType.FUNCTION)
            {
                sb.AppendLine($"RETURNS {script.ReturnDataType}");
            }

            int  beginIndex        = sb.Length - 1;
            bool hasLeaveStatement = false;

            sb.AppendLine("BEGIN");

            foreach (Statement statement in script.Statements.Where(item => item is DeclareStatement))
            {
                sb.AppendLine(this.BuildStatement(statement));
            }

            #region Cursor

            Action appendDeclareCursor = () =>
            {
                foreach (Statement statement in script.Statements.Where(item => item is DeclareCursorStatement))
                {
                    sb.AppendLine(this.BuildStatement(statement));
                }
            };

            if (script.Statements.Any(item => item is OpenCursorStatement) && !script.Statements.Any(item => item is DeclareCursorHandlerStatement))
            {
                if (!script.Statements.Any(item => item is DeclareStatement && (item as DeclareStatement).Name.Symbol == "FINISHED"))
                {
                    DeclareStatement declareStatement = new DeclareStatement()
                    {
                        Name = new TokenInfo("FINISHED")
                        {
                            Type = TokenType.VariableName
                        },
                        DataType = new TokenInfo("INT")
                        {
                            Type = TokenType.DataType
                        },
                        DefaultValue = new TokenInfo("0")
                    };

                    sb.AppendLine(this.BuildStatement(declareStatement));
                }

                appendDeclareCursor();

                DeclareCursorHandlerStatement handler = new DeclareCursorHandlerStatement();
                handler.Statements.Add(new SetStatement()
                {
                    Key = new TokenInfo("FINISHED")
                    {
                        Type = TokenType.VariableName
                    }, Value = new TokenInfo("1")
                });

                sb.AppendLine(this.BuildStatement(handler));
            }
            else
            {
                appendDeclareCursor();
            }

            #endregion

            if (script.ReturnTable != null)
            {
                sb.AppendLine(MySqlStatementScriptBuilder.BuildTemporaryTable(script.ReturnTable));
            }

            FetchCursorStatement fetchCursorStatement = null;

            foreach (Statement statement in script.Statements.Where(item => !(item is DeclareStatement || item is DeclareCursorStatement)))
            {
                if (statement is FetchCursorStatement fetch)
                {
                    fetchCursorStatement = fetch;
                    continue;
                }
                else if (statement is WhileStatement @while)
                {
                    FetchCursorStatement fs = @while.Statements.FirstOrDefault(item => item is FetchCursorStatement) as FetchCursorStatement;

                    if (fetchCursorStatement != null && fs != null)
                    {
                        @while.Condition.Symbol = "FINISHED = 0";

                        if (fs.Variables.Count == 0)
                        {
                            @while.Statements.Insert(0, fetchCursorStatement);
                        }
                    }
                }

                if (statement is LeaveStatement)
                {
                    hasLeaveStatement = true;
                }

                sb.AppendLine(this.BuildStatement(statement));
            }

            sb.AppendLine("END");

            if (hasLeaveStatement)
            {
                sb.Insert(beginIndex, "sp:");
            }

            return(this.FormatScripts(sb.ToString()));
        }
 protected override void VisitDeclareStatement(DeclareStatement statement)
 {
     throw new NotImplementedException();
 }
示例#9
0
 protected abstract void VisitDeclareStatement(DeclareStatement statement);
示例#10
0
 /// <summary>
 /// Visits a declare statement.
 /// </summary>
 /// <param name="node">
 /// The node.
 /// </param>
 /// <returns>
 /// The node, or a new version of the node.
 /// </returns>
 protected internal override Node VisitDeclareStatement(DeclareStatement node)
 {
     return(this.VisitImplementation(node) ?? base.VisitDeclareStatement(node));
 }