Ejemplo n.º 1
0
        public override void ParseInside(ExpressionParser parser)
        {
            var collection = parser.Collection;
            var lex        = collection.CurrentLexem();

            if (lex.LexemText.ToLower() != "insert")
            {
                throw new Exception("Not INSERT statment");
            }
            lex = collection.GotoNextMust();
            if (lex.LexemText.ToLower() != "into")
            {
                throw new Exception("INTO keyword is not found");
            }
            lex = collection.GotoNextMust();
            string[] tablename = CommonParserFunc.ReadTableName(collection);
            TableClause = TableClause.CreateByTable(tablename, collection.TableGetter.GetTableByName(tablename, collection.TableGetterUseCache));
            lex         = collection.GotoNextMust();
            if (lex.IsSkobraOpen())
            {
                while (true)
                {
                    lex = collection.GotoNextMust(); //пропускаем SET или ','
                    //lex = collection.CurrentLexem();

                    var col = CommonParserFunc.ReadColumn(collection);
                    ColumnOfValues.Add(col);
                    lex = collection.GotoNextMust();
                    if (lex == null)
                    {
                        break;
                    }
                    if (lex.LexemType == LexType.Zpt)
                    {
                        continue;
                    }
                    if (lex.IsSkobraClose())
                    {
                        break;
                    }
                    collection.Error("Unknow lexem", collection.CurrentLexem());
                }
                //пропускаем ')'
                lex = collection.GotoNextMust();
            }
            else
            {
                if (lex.LexemType == LexType.Command && lex.LexemText.ToLower() == "default")
                {
                    lex = collection.GotoNextMust();
                    if (lex.LexemType == LexType.Command && lex.LexemText.ToLower() == "values")
                    {
                        DefaultValues = true;
                        return;
                    }
                    else
                    {
                        collection.Error("Expected keyword VALUES", lex);
                    }
                }
            }

            if (lex == null)
            {
                return;
            }
            if (lex.LexemText.ToLower() == "values")
            {
                lex = collection.GotoNextMust();
                if (!lex.IsSkobraOpen())
                {
                    collection.Error("'(' not found", lex);
                }
                while (true)
                {
                    SubExpression sub = new SubExpression();
                    sub.MultiValues = true;
                    while (true)
                    {
                        lex = collection.GotoNextMust(); //пропускаем SET или ','
                        //lex = collection.CurrentLexem();

                        ExpressionParser e = new ExpressionParser(collection);
                        e.Parse();
                        sub.AddChild(e.Single());
                        lex = collection.CurrentLexem();
                        if (lex == null)
                        {
                            break;
                        }
                        if (lex.LexemType == LexType.Zpt)
                        {
                            continue;
                        }
                        if (lex.IsSkobraClose())
                        {
                            break;
                        }
                        collection.Error("Unknow lexem", collection.CurrentLexem());
                    }
                    Values.Add(sub);
                    lex = collection.GotoNext();
                    if (lex != null && lex.LexemType == LexType.Zpt)
                    {
                        lex = collection.GotoNextMust();
                        if (!lex.IsSkobraOpen())
                        {
                            collection.ErrorWaitKeyWord("(", lex);
                        }
                        continue;
                    }
                    else
                    {
                        break;
                    }
                }
            }
            else
            if (lex.LexemText.ToLower() == "select" || lex.IsSkobraOpen())
            {
                ExpressionParser e = new ExpressionParser(collection);
                e.Parse();
                var expr = e.Single();
                var sel  = ParserUtils.FindSelect(expr);
                if (sel == null)
                {
                    throw new Exception("Values in INSERT not found");
                }
                Select = sel;
            }
            ParseReturining(parser);
        }
Ejemplo n.º 2
0
 public virtual void ParseInside(ExpressionParser parser)
 {
     parser.Collection.GotoNext();
 }
        public Expression GetNode(ExpressionParser parser)
        {
            var        collection = parser.Collection;
            Lexem      lex        = parser.Collection.CurrentLexem();
            bool       uniar      = parser.waitValue;
            Expression res        = null;

            if (lex.LexemType == LexType.Arfimetic)
            {
                switch (lex.LexemText)
                {
                case "+":
                    if (uniar)
                    {
                        res = new UniarPlus_BoolExpr();
                    }
                    else
                    {
                        res = new Plus_Arifmetic();
                    }
                    break;

                case "-":
                    if (uniar)
                    {
                        res = new UniarMinus_BoolExpr();
                    }
                    else
                    {
                        res = new Minus_Arifmetic();
                    }
                    break;

                case "*":
                    if (uniar)
                    {
                        res = new AllColumnExpr();
                    }
                    else
                    {
                        res = new Multi_Arifmetic();
                    }
                    break;

                case "/":
                    res = new Div_Arifmetic();
                    break;

                case "<":
                    res = new Less_CompExpr();
                    break;

                case "<=":
                    res = new LessOrEqual_CompExpr();
                    break;

                case ">=":
                    res = new GreatOrEqual_CompExpr();
                    break;

                case ">":
                    res = new Great_CompExpr();
                    break;

                case "=":
                    res = new Equal_CompExpr();
                    break;

                case "<>":
                case "!=":
                    res = new NotEqual_CompExpr();
                    break;
                }
            }
            if (lex.LexemType == LexType.Number)
            {
                res = new ConstExpr();
                if (lex.LexemText.Contains('.'))
                {
                    (res as ConstExpr).Init(lex.LexemText.ParseDouble(), SimpleTypes.Float);
                }
                else
                {
                    (res as ConstExpr).Init(long.Parse(lex.LexemText), SimpleTypes.Integer);
                }
            }
            if (lex.LexemType == LexType.Text)
            {
                if (lex.LexemText.StartsWith("'"))
                {
                    res = new ConstExpr();
                    (res as ConstExpr).Init(ParserUtils.StandartDecodeEscape(lex.LexemText), SimpleTypes.String);
                }
            }
            if (lex.LexemType == LexType.Command)
            {
                switch (lex.LexemText.ToLower())
                {
                case "not":
                    res = new Not_BoolExpr();
                    break;

                case "and":
                    res = new And_BoolExpr();
                    break;

                case "or":
                    res = new Or_BoolExpr();
                    break;

                case "true":
                    res = new ConstExpr();
                    (res as ConstExpr).Init(true, SimpleTypes.Boolean);
                    break;

                case "false":
                    res = new ConstExpr();
                    (res as ConstExpr).Init(false, SimpleTypes.Boolean);
                    break;

                case "date":
                    if (collection.GetNext() != null)
                    {
                        var c = collection.GetNext();
                        if (c.LexemType == LexType.Text && c.LexemText.StartsWith("'"))
                        {
                            res = new ConstExpr();
                            string s  = ParserUtils.StandartDecodeEscape(c.LexemText);
                            var    dt = CommonUtils.ParseDateTime(s);
                            if (dt != null)
                            {
                                collection.GotoNext();
                                ((ConstExpr)res).Init(dt.Value, SimpleTypes.Date);
                            }
                            else
                            {
                                collection.Error("Can not parse date lexem", lex);
                            }
                        }
                    }
                    break;

                case "datetime":
                case "timestamp":
                    if (collection.GetNext() != null)
                    {
                        var c = collection.GetNext();
                        if (c.LexemType == LexType.Text && c.LexemText.StartsWith("'"))
                        {
                            res = new ConstExpr();
                            string s  = ParserUtils.StandartDecodeEscape(c.LexemText);
                            var    dt = CommonUtils.ParseDateTime(s);
                            if (dt != null)
                            {
                                collection.GotoNext();
                                ((ConstExpr)res).Init(dt.Value, SimpleTypes.DateTime);
                            }
                            else
                            {
                                collection.Error("Can not parse datetime lexem", lex);
                            }
                        }
                    }
                    break;

                case "time":
                    if (collection.GetNext() != null)
                    {
                        var c = collection.GetNext();
                        if (c.LexemType == LexType.Text && c.LexemText.StartsWith("'"))
                        {
                            res = new ConstExpr();
                            string   s = ParserUtils.StandartDecodeEscape(c.LexemText);
                            DateTime dt;
                            var      r = CommonUtils.ParseDateTime(s, out dt);
                            if (r == ParserDateTimeStatus.Time)
                            {
                                collection.GotoNext();
                                ((ConstExpr)res).Init(dt.TimeOfDay, SimpleTypes.Time);
                            }
                            else
                            {
                                collection.Error("Can not parse time lexem", lex);
                            }
                        }
                    }
                    break;
                }
            }
            return(res);
        }
Ejemplo n.º 4
0
        public override void ParseInside(ExpressionParser parser)
        {
            var collection = parser.Collection;
            var lex        = collection.CurrentLexem();

            if (lex.LexemText.ToLower() != "update")
            {
                throw new Exception("Not UPDATE statment");
            }
            lex = collection.GotoNextMust();

            FromParser fc = new FromParser();

            fc.Parse(collection);
            if (fc.Tables.Count > 1)
            {
                collection.Error("Multi tables in update", collection.CurrentLexem());
            }
            if (fc.Tables.Count == 0)
            {
                collection.Error("Not table clause", collection.CurrentLexem());
            }
            TableClause = fc.Tables[0];


            //string[] tablename = CommonParserFunc.ReadTableName(collection);
            //TableClause = TableClause.CreateByTable(tablename, collection.TableGetter.GetTableByName(tablename));
            //lex = collection.GotoNextMust();
            lex = collection.CurrentLexem();

            if (lex.LexemText.ToLower() != "set")
            {
                collection.Error("SET keyword is not found", collection.CurrentLexem());
            }

            while (true)
            {
                lex = collection.GotoNextMust();//пропускаем SET или ','
                //lex = collection.CurrentLexem();

                SetClause sc = new SetClause();
                sc.Column = CommonParserFunc.ReadColumn(collection);
                lex       = collection.GotoNextMust();
                if (lex.LexemText != "=")
                {
                    collection.Error("Operator '=' is not found", collection.CurrentLexem());
                }
                lex = collection.GotoNextMust();
                ExpressionParser e = new ExpressionParser(parser.Collection);
                e.Parse();
                sc.Value = e.Single();
                Set.Add(sc);
                lex = collection.CurrentLexem();
                if (lex == null)
                {
                    break;
                }
                if (lex.LexemType == LexType.Zpt)
                {
                    continue;
                }
                break;
            }

            if (lex == null)
            {
                return;
            }
            if (lex.LexemText.ToLower() == "where")
            {
                collection.GotoNextMust();
                ExpressionParser e = new ExpressionParser(parser.Collection);
                e.Parse();
                Where = e.Single();
            }
            ParseReturining(parser);
        }
Ejemplo n.º 5
0
        public Expression GetNode(ExpressionParser parser)
        {
            Lexem      lex   = parser.Collection.CurrentLexem();
            Expression ex    = null;
            bool       uniar = parser.waitValue;

            if (lex.LexemType == LexType.Arfimetic)
            {
                switch (lex.LexemText)
                {
                case "+":
                    if (uniar)
                    {
                        ex = new UniarPlus_BoolExpr();
                    }
                    else
                    {
                        ex = new Plus_Arifmetic();
                    }
                    break;

                case "-":
                    if (uniar)
                    {
                        ex = new UniarMinus_BoolExpr();
                    }
                    else
                    {
                        ex = new Minus_Arifmetic();
                    }
                    break;

                case "*":
                    if (uniar)
                    {
                        ex = new AllColumnExpr();
                    }
                    else
                    {
                        ex = new Multi_Arifmetic();
                    }
                    break;

                case "/":
                    ex = new Div_Arifmetic();
                    break;

                case "<":
                    ex = new Less_CompExpr();
                    break;

                case "<=":
                    ex = new LessOrEqual_CompExpr();
                    break;

                case ">=":
                    ex = new GreatOrEqual_CompExpr();
                    break;

                case ">":
                    ex = new Great_CompExpr();
                    break;

                case "=":
                    ex = new Equal_CompExpr();
                    break;

                case "<>":
                case "!=":
                    ex = new NotEqual_CompExpr();
                    break;
                }
            }
            if (lex.LexemType == LexType.Number)
            {
                ex = new ConstExpr();
                if (lex.LexemText.Contains('.'))
                {
                    (ex as ConstExpr).Init(lex.LexemText.ParseDouble(), SimpleTypes.Float);
                }
                else
                {
                    (ex as ConstExpr).Init(long.Parse(lex.LexemText), SimpleTypes.Integer);
                }
            }
            if (lex.LexemType == LexType.Text)
            {
                if (lex.LexemText.StartsWith("'"))
                {
                    ex = new ConstExpr();
                    (ex as ConstExpr).Init(ParserUtils.StandartDecodeEscape(lex.LexemText), SimpleTypes.String);
                }
            }
            Lexem n1;

            if (lex.LexemType == LexType.Command)
            {
                switch (lex.LexemText.ToLower())
                {
                case "not":
                    n1 = parser.Collection.GetNext();
                    if (n1 != null && n1.LexemType == LexType.Command && n1.LexemText.ToLower() == "in")
                    {
                        ex = new NotInExpr();
                        parser.Collection.GotoNext();
                        break;
                    }
                    ex = new Not_BoolExpr();
                    break;

                case "case":
                    ex = new CaseExpr();
                    break;

                case "contains":
                    ex = new Contains();
                    break;

                case "containsic":     //ic = ignore case
                case "containscase":
                    ex = new ContainsIgnoreCase();
                    break;

                case "startwith":
                case "startswith":
                    ex = new StartsWith();
                    break;

                case "endwith":
                case "endswith":
                    ex = new EndsWith();
                    break;
                }
                if (parser.Collection.GetNext() != null && parser.Collection.GetNext().IsSkobraOpen())
                {
                    switch (lex.LexemText.ToLower())
                    {
                    case "cast":
                        ex = new Cast();
                        break;

                    case "in":
                        ex = new InExpr();
                        break;

                    case "abs":
                        ex = new Abs();
                        break;

                    case "substring":
                        ex = new SubString();
                        break;

                    case "position":
                        ex = new Position();
                        break;

                    case "ltrim":
                        ex = new LTrim();
                        break;

                    case "rtrim":
                        ex = new RTrim();
                        break;

                    case "trim":
                        ex = new Trim();
                        break;

                    case "length":
                        ex = new Length();
                        break;

                    case "upper":
                        ex = new Upper_operation();
                        break;

                    case "lower":
                        ex = new Lower_operation();
                        break;

                    case "left":
                        ex = new Left();
                        break;

                    case "right":
                        ex = new Right();
                        break;

                    case "replace":
                        ex = new Replace();
                        break;

                    case "now":
                        ex = new Now();
                        break;

                    case "tostr":
                        ex = new ToString();
                        break;

                    case "strtotime":
                        ex = new StrToTime();
                        break;

                    case "strtodatetime":
                        ex = new StrToDateTime();
                        break;

                    case "addseconds":
                        ex = new AddSeconds();
                        break;

                    case "addminutes":
                        ex = new AddMinutes();
                        break;

                    case "addhours":
                        ex = new AddHours();
                        break;

                    case "adddays":
                        ex = new AddDays();
                        break;

                    case "day":
                        ex = new Day();
                        break;

                    case "month":
                        ex = new Month();
                        break;

                    case "year":
                        ex = new Year();
                        break;

                    case "round":
                        ex = new ParserCore.Expr.Extend.Math.Round();
                        break;

                    case "ceiling":
                        ex = new ParserCore.Expr.Extend.Math.Ceiling();
                        break;

                    case "floor":
                        ex = new ParserCore.Expr.Extend.Math.Floor();
                        break;

                    case "pi":
                        ex = new ParserCore.Expr.Extend.Math.Pi();
                        break;

                    case "tan":
                        ex = new ParserCore.Expr.Extend.Math.Tan();
                        break;

                    case "log":
                        ex = new ParserCore.Expr.Extend.Math.Log();
                        break;
                    }
                }
            }
            return(ex);
        }
Ejemplo n.º 6
0
        public static Expression AddFiled(string fieldAlias, string tableAlias, string schema, Lexem lexem, ExpressionParser parser)
        {
            if (fieldAlias == "*")
            {
                if (schema != null)
                {
                    parser.Collection.Error("Can not use schema in * expression", lexem);
                }
                AllColumnExpr a = new AllColumnExpr();
                a.Prefix = tableAlias;
                return(a);
            }
            FieldExpr res = new FieldExpr();

            res.FieldName  = fieldAlias;
            res.TableAlias = tableAlias;
            res.Schema     = schema;
            res.Lexem      = lexem;
            return(res);
        }
Ejemplo n.º 7
0
        public Expression GetNode(ExpressionParser parser)
        {
            Lexem      lex = parser.Collection.CurrentLexem();
            Expression res = null;

            if (lex.LexemType == LexType.Command)
            {
                string lowerLexem = lex.LexemText.ToLower();
                if (ParserUtils.ParseCommandPhrase(parser.Collection, "create view", false, false))
                {
                    res = new CreateView();
                }
                if (ParserUtils.ParseCommandPhrase(parser.Collection, "create table", false, false))
                {
                    res = new CreateTable();
                }
                if (res == null && ParserUtils.ParseCommandPhrase(parser.Collection, "alter table", false, false))
                {
                    res = new AlterTable();
                }
                if (res == null && ParserUtils.ParseCommandPhrase(parser.Collection, "drop table", false, false))
                {
                    res = new DropTable();
                }
                if (res == null && ParserUtils.ParseCommandPhrase(parser.Collection, "drop index", false, false))
                {
                    res = new DropIndex();
                }
                if (res == null && (ParserUtils.ParseCommandPhrase(parser.Collection, "create unique index", false, false) ||
                                    ParserUtils.ParseCommandPhrase(parser.Collection, "create index", false, false)))
                {
                    res = new CreateIndex();
                }
                if (res == null)
                {
                    if (parser.Collection.GetNext() != null && parser.Collection.GetNext().IsSkobraOpen())
                    {
                        switch (lowerLexem)
                        {
                        case "count":
                            res = new CountExpr();
                            break;

                        case "sum":
                            res = new SumExpr();
                            break;

                        case "min":
                            res = new MinExpr();
                            break;

                        case "max":
                            res = new MaxExpr();
                            break;

                        case "avg":
                            res = new AvgExpr();
                            break;

                        case "lastinsertrowid":
                            res = new LastInsertRowidExpr();
                            break;

                        case "exists":
                            res = new ExistsExpr();
                            break;

                        case "any":
                            res = new AnyExpr();
                            break;
                        }
                    }

                    switch (lowerLexem)
                    {
                    case "between":    //не функция
                        res = new Between();
                        break;

                    case "select":
                        res = new SelectExpresion();
                        break;

                    case "update":
                        res = new UpdateStatement();
                        break;

                    case "insert":
                        res = new InsertStatement();
                        break;

                    case "delete":
                        res = new DeleteStatement();
                        break;
                    }
                }
            }
            if (res != null)
            {
                return(res);
            }
            return(res);
        }
Ejemplo n.º 8
0
        private TableClause Read_table_reference(LexemCollection collection, bool isFirst)
        {
            var lex = collection.CurrentLexem();

            if (lex == null)
            {
                return(null);
            }
            //+left join
            //+left outer join
            //+inner join
            //+cross join
            //+join
            //+right join
            //+right outer join
            // full join
            JoinType jt = JoinType.Cross;

            if (!isFirst)
            {
                string s1 = collection.CurrentLexem().LexemText.ToLower();
                string s2 = null;
                string s3 = null;
                if (joinStrings.Contains(s1))
                {
                    lex = collection.GotoNext();
                    if (lex != null && joinStrings.Contains(lex.LexemText.ToLower()))
                    {
                        s2  = lex.LexemText.ToLower();
                        lex = collection.GotoNext();
                        if (lex != null && joinStrings.Contains(lex.LexemText.ToLower()))
                        {
                            s3  = lex.LexemText.ToLower();
                            lex = collection.GotoNext();
                        }
                    }
                }

                if (!joinStrings.Contains(s1))// случай ","
                {
                    jt = JoinType.Cross;
                }
                else
                if ((s3 == null && s2 == null && s1 == "join") ||
                    (s3 == null && s1 == "cross" && s2 == "join"))
                {
                    jt = JoinType.Cross;
                }
                else if ((s3 == null && s1 == "left" && s2 == "join") ||
                         (s1 == "left" && s2 == "outer" && s3 == "join"))
                {
                    jt = JoinType.Left;
                }
                else if ((s3 == null && s1 == "right" && s2 == "join") ||
                         (s1 == "right" && s2 == "outer" && s3 == "join"))
                {
                    jt = JoinType.Left;
                }
                else if (s3 == null && s1 == "inner" && s2 == "join")
                {
                    jt = JoinType.Inner;
                }
                else if (s3 == null && s1 == "full" && s2 == "join")
                {
                    jt = JoinType.Full;
                }
                else
                {
                    collection.Error("Unknow JOIN clause", collection.CurrentLexem());
                }
            }
            TableClause st = null;

            if (lex == null)
            {
                collection.Error("Error in table clause", collection.GetPrev());
            }
            if (!lex.IsSkobraOpen())
            {
                string[] tableName = CommonParserFunc.ReadTableName(collection);
                var      v         = collection.TableGetter.GetTableByName(tableName, collection.TableGetterUseCache);
                st  = TableClause.CreateByTable(tableName, v);
                lex = collection.GotoNext();
            }
            else
            {
                lex = collection.GotoNext();
                if (lex == null)
                {
                    collection.Error("Expression is not found", collection.GetLast());
                }
                //подзапрос
                var idx = collection.IndexLexem;
                ExpressionParser tonode = new ExpressionParser(collection);
                tonode.Parse();
                if (tonode.Results.Count != 1)
                {
                    collection.Error("не верное число параметров", collection.Get(idx));
                }
                lex = collection.CurrentLexem();
                if (lex == null || !lex.IsSkobraClose())
                {
                    collection.Error("Expression is not closed", collection.CurrentLexem());
                }
                lex = collection.GotoNext();

                //ITableDesc subselect = CommonUtils.FindParentSelect(tonode.Single()); Это штука работает не правильно
                ITableDesc subselect = (ITableDesc)tonode.Single();
                if (subselect == null)
                {
                    collection.Error("Subselect not found", collection.Get(idx));
                }
                st = TableClause.CreateBySelect(subselect);
            }
            st.Join = jt;
            if (lex == null)
            {
                return(st);
            }
            if (lex.LexemText.ToLower() == "as")
            {
                collection.GotoNext();
                if (lex == null)
                {
                    collection.Error("Alias not found", collection.GetPrev());
                }
                st.Alias = CommonParserFunc.ReadAlias(collection);
                if (st.Alias == null)
                {
                    collection.Error("Alias not found", collection.GetPrev());
                }
                lex = collection.GotoNext();
            }
            else
            {
                if (lex.LexemType == LexType.Text)
                {
                    st.Alias = CommonParserFunc.ReadAlias(collection);
                    if (st.Alias == null)
                    {
                        collection.Error("Alias not found", collection.GetPrev());
                    }
                    lex = collection.GotoNext();
                }
                else
                if (lex.LexemType == LexType.Command &&
                    !nextStrings.Contains(lex.LexemText.ToLower()) &&
                    !joinStrings.Contains(lex.LexemText.ToLower()) &&
                    lex.LexemText.ToLower() != "on")
                {
                    st.Alias = CommonParserFunc.ReadAlias(collection);
                    if (st.Alias == null)
                    {
                        collection.Error("Alias not found", collection.GetPrev());
                    }
                    lex = collection.GotoNext();
                }
            }
            lex = collection.CurrentLexem();
            if (lex == null)
            {
                return(st);
            }
            if (lex.LexemType == LexType.Command && lex.LexemText.ToLower() == "on")
            {
                lex = collection.GotoNext();
                var idx = collection.IndexLexem;
                ExpressionParser tonode = new ExpressionParser(collection);
                tonode.Parse();
                if (tonode.Results.Count != 1)
                {
                    collection.Error("не верное число параметров", collection.Get(idx));
                }
                st.OnExpression = tonode.Single();
            }
            return(st);
        }
Ejemplo n.º 9
0
        public override void ParseInside(ExpressionParser parser)
        {
            var collection = parser.Collection;

            var le = collection.GotoNextMust();

            if (le.LexemType != LexType.Command || le.LexemText.ToLower() != "when")
            {
                int idx = collection.IndexLexem;
                ExpressionParser tonode = new ExpressionParser(collection);
                tonode.Parse();
                if (tonode.Results.Count != 1)
                {
                    collection.Error("не верное число параметров", collection.Get(idx));
                }
                CaseArg = tonode.Single();
                le      = collection.CurrentLexem();
            }

            if (le.LexemType != LexType.Command || le.LexemText.ToLower() != "when")
            {
                collection.Error("Ожидалось WHEN", collection.CurrentOrLast());
            }
            while (le.LexemType == LexType.Command && le.LexemText.ToLower() == "when")
            {
                WhenThen wt = new WhenThen();
                collection.GotoNextMust();
                int idx = collection.IndexLexem;
                ExpressionParser tonode = new ExpressionParser(collection);
                tonode.Parse();
                if (tonode.Results.Count != 1)
                {
                    collection.Error("не верное число параметров", collection.Get(idx));
                }
                wt.When = tonode.Single();
                le      = collection.CurrentLexem();
                if (le == null || le.LexemType != LexType.Command || le.LexemText.ToLower() != "then")
                {
                    collection.Error("ожидалось THEN", collection.CurrentOrLast());
                }
                le     = collection.GotoNextMust();
                idx    = collection.IndexLexem;
                tonode = new ExpressionParser(collection);
                tonode.Parse();
                if (tonode.Results.Count != 1)
                {
                    collection.Error("не верное число параметров", collection.Get(idx));
                }
                wt.Then = tonode.Single();
                ListWhenThen.Add(wt);
                le = collection.CurrentLexem();
                if (le == null)
                {
                    collection.Error("Не найден END", collection.GetLast());
                }
                if (le.LexemText.ToLower() == "end")
                {
                    break;
                }
                if (le.LexemText.ToLower() == "when")
                {
                    continue;
                }
                if (le.LexemText.ToLower() == "else")
                {
                    le     = collection.GotoNextMust();
                    idx    = collection.IndexLexem;
                    tonode = new ExpressionParser(collection);
                    tonode.Parse();
                    if (tonode.Results.Count != 1)
                    {
                        collection.Error("не верное число параметров", collection.Get(idx));
                    }
                    Else = tonode.Single();
                    le   = collection.CurrentLexem();
                    if (le == null || le.LexemText.ToLower() != "end")
                    {
                        collection.Error("Ожидался END", collection.CurrentOrLast());
                    }
                    break;
                }
                collection.Error("Не ожиданный конец", collection.CurrentOrLast());
            }
            base.ParseInside(parser);
        }
Ejemplo n.º 10
0
        public override void Parse()
        {
            //текщая лексема SELECT
            var le = Collection.GotoNext();
            if (le == null) Collection.Error("неожиданный конец", Collection.GetPrev());
            if (le.LexemText.ToLower() == "distinct")
            {
                SelectExpresion.Distinct = true;
                le = Collection.GotoNextMust();
            }
            else if (le.LexemText.ToLower() == "all")
            {
                le = Collection.GotoNextMust();
            }
            var idx = Collection.IndexLexem;
            ColumnClauseParser colsParser = new ColumnClauseParser();
            colsParser.Parse(Collection);
            if (colsParser.Columns.Count == 0) Collection.Error("Columnn not found", Collection.Get(idx));
            Results.Add(SelectExpresion);
            SelectExpresion.Columns.Replace(colsParser.Columns);
            var lex = Collection.CurrentLexem();
            if (lex == null) return;
            if (lex.LexemText.ToLower() != "from") return;

            lex = Collection.GotoNext();
            FromParser fc = new FromParser();
            fc.Parse(Collection);
            SelectExpresion.Tables.Replace(fc.Tables);
            lex = Collection.CurrentLexem();
            if (lex == null) return;
            if (lex.LexemText.ToLower() == "where")
            {
                lex = Collection.GotoNext();
                if (lex == null) Collection.Error("Where clause not found", Collection.GetPrev());
                idx = Collection.IndexLexem;
                ExpressionParser tonode = new ExpressionParser(Collection);
                tonode.Parse();
                if (tonode.Results.Count != 1) Collection.Error("не верное число параметров", Collection.Get(idx));
                SelectExpresion.WhereExpr = tonode.Single();
            }
            lex = Collection.CurrentLexem();
            if (lex == null) return;
            if (lex.LexemText.ToLower() == "having")
            {
                lex = Collection.GotoNext();
                if (lex == null) Collection.Error("Having clause not found", Collection.GetPrev());
                idx = Collection.IndexLexem;
                ExpressionParser tonode = new ExpressionParser(Collection);
                tonode.Parse();
                if (tonode.Results.Count != 1) Collection.Error("не верное число параметров", Collection.Get(idx));
                SelectExpresion.Having = tonode.Single();
            }
            lex = Collection.CurrentLexem();
            if (lex == null) return;
            if (lex.LexemText.ToLower() == "group")
            {
                lex = Collection.GotoNext();
                if (lex == null) Collection.Error("Group by clause error", Collection.GetPrev());
                if (lex.LexemText.ToLower() != "by") Collection.Error("Group by clause error", Collection.GetPrev());
                lex = Collection.GotoNext();
                OrderByParser gb = new OrderByParser();
                gb.Parse(Collection, false);
                SelectExpresion.GroupBys.Replace(gb.Columns);
                if (SelectExpresion.GroupBys.Count == 0) Collection.Error("\"Group by\" columns not found", Collection.Get(idx));
                lex = Collection.CurrentLexem();
            }
            lex = Collection.CurrentLexem();
            if (lex == null) return;
            if (lex.LexemText.ToLower() == "order")
            {
                lex = Collection.GotoNext();
                if (lex == null) Collection.Error("Order by clause error", Collection.GetPrev());
                if (lex.LexemText.ToLower() != "by") Collection.ErrorWaitKeyWord("BY", Collection.GetPrev());
                lex = Collection.GotoNext();
                OrderByParser gb = new OrderByParser();
                gb.Parse(Collection, true);
                SelectExpresion.OrderBys.Replace(gb.Columns.Select(a => (OrderByClause)a).ToList());
                if (SelectExpresion.OrderBys.Count == 0) Collection.Error("\"Order by\" columns not found", Collection.Get(idx));
                lex = Collection.CurrentLexem();
            }
            lex = Collection.CurrentLexem();
            if (lex == null) return;
            if (lex.LexemText.ToLower() == "limit")
            {
                lex = Collection.GotoNext();
                if (lex == null) Collection.Error("Limit clause not found", Collection.GetPrev());
                ExpressionParser tonode = new ExpressionParser(Collection);
                tonode.Parse();
                SelectExpresion.LimitRecords = tonode.Single().GetIntResultOut(null);
            }
            lex = Collection.CurrentLexem();
            if (lex == null) return;
            if (lex.LexemText.ToLower() == "offset")
            {
                lex = Collection.GotoNext();
                if (lex == null) Collection.Error("Offset clause not found", Collection.GetPrev());
                ExpressionParser tonode = new ExpressionParser(Collection);
                tonode.Parse();
                SelectExpresion.SkipRecords = tonode.Single().GetIntResultOut(null);
            }
            while (true)
            {
                lex = Collection.CurrentLexem();
                if (lex == null) return;
                string s = lex.LexemText.ToLower();
                if (s == "union" || s == "intersect" || s == "except")
                {
                    lex = Collection.GotoNextMust();
                    ExtSelectClause extS = new ExtSelectClause();
                    extS.Select = new SelectExpresion();
                    if (s == "union" && lex.LexemText.ToLower() == "all")
                    {
                        extS.Operation = SelectOperation.UnionAll;
                        lex = Collection.GotoNextMust();
                    }
                    else if (s == "union") extS.Operation = SelectOperation.Union;
                    if (s == "intersect") extS.Operation = SelectOperation.Intersect;
                    if (s == "except") extS.Operation = SelectOperation.Except;
                    if (lex.LexemText.ToLower() != "select") Collection.Error("\"SELECT\" clouse is not found", lex);
                    SelectParser sp = new SelectParser(Collection);
                    sp.SelectExpresion = extS.Select;
                    sp.Parse();
                    SelectExpresion.ExtSelects.Add(extS);
                }
                else break;
            }
        }