public static Expression ParseCollection(LexemCollection collection) { ExpressionParser p = new ExpressionParser(collection); p.Parse(); return(p.Single()); }
public void Parse(LexemCollection collection) { while (true) { var idx = collection.IndexLexem; ExpressionParser tonode = new ExpressionParser(collection); tonode.Parse(); if (tonode.Results.Count != 1) { collection.Error("не верное число параметров", collection.Get(idx)); } ColumnClause r = new ColumnClause(); r.ColumnExpression = tonode.Single(); Columns.Add(r); var lex = collection.CurrentLexem(); if (lex == null) { return; } if (lex.LexemText.ToLower() == "as") { collection.GotoNext(); if (lex == null) { return; } r.Alias = CommonParserFunc.ReadAlias(collection); lex = collection.GotoNext(); } else if (nextStrings.Contains(lex.LexemText.ToLower())) { return; } else { if (lex.LexemType == LexType.Command || lex.LexemType == LexType.Text) { r.Alias = CommonParserFunc.ReadAlias(collection); lex = collection.GotoNext(); } } if (lex == null) { return; } if (nextStrings.Contains(lex.LexemText.ToLower())) { return; } if (lex.LexemType == LexType.Zpt) { collection.GotoNext(); continue; } return; } }
public void Parse(LexemCollection collection, bool isOrderBy) { while (true) { var idx = collection.IndexLexem; ExpressionParser tonode = new ExpressionParser(collection); tonode.Parse(); if (tonode.Results.Count != 1) { collection.Error("не верное число параметров", collection.Get(idx)); } GroupByClause r = null; if (isOrderBy) { r = new OrderByClause(); } else { r = new GroupByClause(); } r.Expression = tonode.Single(); Columns.Add(r); var lex = collection.CurrentLexem(); if (lex == null) { return; } if (isOrderBy) { if (lex.LexemText.ToLower() == "desc") { ((OrderByClause)r).Sort = SortType.DESC; lex = collection.GotoNext(); } else if (lex.LexemText.ToLower() == "asc") { ((OrderByClause)r).Sort = SortType.ASC; lex = collection.GotoNext(); } } if (lex == null) { return; } if (lex.LexemType == LexType.Zpt) { collection.GotoNext(); continue; } return; } }
/// <summary> /// Формирует простое выражение, не-SQL типа /// </summary> public static Expression GetSimpleExpression(string text, IExpressionFactory factory) { var collection = ParserLexem.Parse(text); collection.NodeFactory = factory; ExpressionParser expToNode = new ExpressionParser(collection); expToNode.Parse(); var res = expToNode.Single(); res = res.PrepareAndOptimize(); return(res); }
public override void ParseInside(ExpressionParser parser) { var collection = parser.Collection; var lex = collection.CurrentLexem(); if (lex.LexemText.ToLower() != "delete") { throw new Exception("Not DELETE statment"); } lex = collection.GotoNextMust(); if (lex.LexemText.ToLower() != "from") { throw new Exception("Keyword 'FROM' is not found"); } 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.CurrentLexem(); if (lex == null) { return; } if (lex.LexemText.ToLower() == "where") { collection.GotoNextMust(); ExpressionParser e = new ExpressionParser(collection); e.Parse(); Where = e.Single(); } ParseReturining(parser); }
public override void Parse() { var idx = Collection.IndexLexem; var le = Collection.GotoNext(); if (le == null) { Collection.Error("неожиданный конец", Collection.GetPrev()); } if (le.IsSkobraClose()) { Collection.Error("пустое выражение", Collection.CurrentLexem()); } ExpressionParser tonode = new ExpressionParser(Collection); tonode.Parse(); if (Collection.CurrentLexem() == null) { Collection.Error("нет закрывающейся скобки", Collection.GetLast()); } if (tonode.Results.Count == 0) { Collection.Error("нет значений", Collection.Get(idx)); } if (tonode.Results.Count > 1) { Collection.Error("несколько значений", Collection.Get(idx)); } var t = new SubExpression(); t.AddChild(tonode.Single()); Results.Add(t); if (!Collection.CurrentLexem().IsSkobraClose()) { Collection.Error("нет закрывающейся скобки", Collection.CurrentLexem()); } }
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); }
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); }
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); }
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); }
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; } }