public static object AGGR(CallExpr call, Generator.Ctx ctx) { CallExpr ce; { var dict = new Dictionary <string, object>(StringComparer.OrdinalIgnoreCase); ce = Filter.ReplaceValueInfoRefsWithData(call, DataLoader.sData, dict); ctx.CreateValue(DataLoader.sUsedParamsDict, dict); } var args = new List <Expr>(); args.Add(CallExpr.let(new ReferenceExpr(sFactory), new CallExpr(FuncDefs_Solver.SolverWeightedSumsFactory, ce.args[0], new ReferenceExpr(sTimeA), new ReferenceExpr(sTimeB)))); args.Add(CallExpr.let(new ReferenceExpr(sWithKey), ce.args[1])); for (int i = 2; i < ce.args.Count; i++) { args.Add(ce.args[i]); } var exprAgg = CallExpr.Eval(args.ToArray()); var expr = new CallExpr(FuncDefs_Solver.SolverTimedObjects, new CallExpr((Fx)FuncDefs_Solver.SolverWeightedSumsFactoryTimes, new ReferenceExpr(sFactory)), exprAgg); return(Generator.Generate(expr, ctx)); }
static void moveArgsToSection(string sectionName, List <Expr> sectionItems, List <Expr> argItems) { if (argItems.Count == 1) { sectionItems.Add(argItems[0]); } else if (argItems.Count > 0) { if (sectionName == "SELECT") { var item0 = argItems[0] as SequenceExpr; if (item0 != null) { var first = item0.args[0] as ReferenceExpr; if (first != null && first.name == "SELECT") { // add brackets to subqueries: "(SELECT ...)" var newArg0 = Expr.RecursiveModifier(e => (e == item0) ? CallExpr.Eval(item0) : e) (argItems[0]); argItems[0] = newArg0; } } } sectionItems.Add(new SequenceExpr(argItems.ToArray())); } argItems.Clear(); }
// F = ( E ) // F = <variable> // F = <function> ( P ) // F = "string" // F = <number> // F = - F void F() { char c = peek(); if (c == '(') { acpt(); Expr[] lst = ParseExprList(')'); c = read(); if (c != ')') { error("Right bracket ')' expected"); } if (lst.Length == 0) { error("Expression expected"); } else if (lst.Length == 1) { stack.Push(lst[0]); } else { stack.Push(CallExpr.Eval(lst)); } } else if (c == '{') { // 'set' = { item1, ..., itemN } acpt(); Expr[] items = ParseExprList('}'); c = read(); if (c != '}') { error("Right brace '}' expected"); } stack.Push(new ArrayExpr(items)); } else if (CanBeFirstCharOfIdentifier(c)) { string id = IdentifierToken(c); if (string.Compare(id, "TRUE", comparison) == 0 || string.Compare(id, "FALSE", comparison) == 0) { bool value = bool.Parse(id); stack.Push(new ConstExpr(value)); } else { if (reservedWords != null) { int i = Array.BinarySearch(reservedWords, id, comparer); if (i > 0) { stack.Push(nullExpr); return; } } // function call c = peek(); if (c == '(') { Expr[] prms = null; acpt(); prms = ParseExprList(')'); c = read(); if (c != ')') { error("Right bracket ')' expected"); } stack.Push(new CallExpr(id, prms)); } else { stack.Push(new ReferenceExpr(id)); } } } else if ((c == '"' && !DoubleQuotedAsIdentifer) || c == '\'') { StringToken(c); } else { string numStr = W.Common.NumberUtils.GetFloatStr(txt, ndx); if (numStr != null) { object v; if (numStr.Contains(".") || numStr.Contains("E") || numStr.Contains("e")) { v = double.Parse(numStr, System.Globalization.CultureInfo.InvariantCulture); } else { int i; if (int.TryParse(numStr, out i)) { v = i; } else { v = long.Parse(numStr); } } ndx += numStr.Length; stack.Push(new ConstExpr(v)); } else if (c == '-') { acpt(); F(); stack.Push(new UnaryExpr(ExprType.Negate, stack.Pop())); } else { stack.Push(nullExpr); // error("Expression expected"); } } }
static IList <Expr> RestructAsSqlSelect(IList <Expr> lst) { var seq = lst[0] as SequenceExpr; if (seq == null) { return(lst); } var ref0 = (seq.args.Count > 0) ? seq.args[0] as ReferenceExpr : null; if (ref0 == null || string.Compare(ref0.name, "SELECT", StringComparison.InvariantCultureIgnoreCase) != 0) { return(lst); } string sectionName = null; string sectionPartialName = string.Empty; var res = new List <Expr>(lst.Count); var sectionItems = new List <Expr>(lst.Count); foreach (var item in lst) { var se = item as SequenceExpr; if (se == null) { se = new SequenceExpr(item); } var argItems = new List <Expr>(); foreach (var exprArg in se.args) { var expr = exprArg; var r = expr as ReferenceExpr; CallExpr ce = null; if (r == null) { ce = expr as CallExpr; if (ce != null && ce.funcName.Length > 0) { r = new ReferenceExpr(ce.funcName); } } if (r != null) { var s = r.name.ToUpperInvariant(); switch (s) { case "SELECT": case "FROM": case "WHERE": case "BY": case "USING": if (argItems.Count > 0) { moveArgsToSection(sectionName, sectionItems, argItems); } if (sectionName != null) { var after = new SqlSectionExpr(sectionName, sectionItems.ToArray()).Postprocess(); if (after == null) { return(null); } res.Add(after); } else if (sectionItems.Count > 0) { res.AddRange(sectionItems); } sectionItems.Clear(); if (s == "BY") { sectionName = sectionPartialName + ' ' + s; sectionPartialName = string.Empty; } else { System.Diagnostics.Trace.Assert(sectionPartialName.Length == 0, "Unknown SQL clause"); sectionName = s; } break; case "JOIN": { System.Diagnostics.Trace.Assert(argItems.Count > 0, "No expressions before JOIN"); var ae = AliasExpr.AsAlias(argItems); if (ae != null) { argItems.Clear(); argItems.Add(ae); } if (sectionPartialName.Length > 0) { argItems.Add(new ReferenceExpr(sectionPartialName)); } argItems.Add(new ReferenceExpr(s)); if (ae == null) { var tmp = new SequenceExpr(argItems.ToArray()); argItems.Clear(); argItems.Add(tmp); } sectionPartialName = string.Empty; } break; case "ON": { int i = argItems.Count - 1; var subseq = new List <Expr>(); while (i >= 0) { var exp = argItems[i]; var re = exp as ReferenceExpr; if (re != null && re.name == "JOIN") { break; } argItems.RemoveAt(i); i--; subseq.Insert(0, exp); } if (subseq.Count > 0) { argItems.Add((Expr)AliasExpr.AsAlias(subseq) ?? new SequenceExpr(subseq)); } argItems.Add(new ReferenceExpr(s)); } break; case "ORDER": case "GROUP": case "INNER": case "OUTER": case "LEFT": case "RIGHT": case "CROSS": case "FULL": if (sectionPartialName.Length == 0) { sectionPartialName = s; } else { sectionPartialName += ' ' + s; } break; default: if (ce == null) { argItems.Add(r); } r = null; break; } if (ce == null) { continue; } if (r != null) { expr = new CallExpr(string.Empty, ce.args); ce = null; } } if (ce != null && ce.funcName == string.Empty) { // no need to modify possible aliases in nested queries var items = RestructAsSqlSelect(ce.args); if (items != ce.args) { argItems.Add(CallExpr.Eval(new SequenceExpr(items))); } } else { argItems.Add(expr); } } moveArgsToSection(sectionName, sectionItems, argItems); } if (sectionName != null) { var after = new SqlSectionExpr(sectionName, sectionItems.ToArray()).Postprocess(); if (after == null) { return(null); } res.Add(after); } else if (sectionItems.Count > 0) { res.AddRange(sectionItems); } return(res.ToArray()); }