static Expr AggImpl(CallExpr ce, Generator.Ctx ctx) { var valsExpr = ce.args[0]; Expr keysExpr; if (ce.args.Count > 1) { keysExpr = ce.args[1]; } else { keysExpr = new ReferenceExpr(sWithKey); } var refAkk = new ReferenceExpr(string.Format("{0}:{1}:{2}", sAkk, valsExpr, keysExpr)); int iAkk = ctx.IndexOf(refAkk.name); Expr resultExpr; if (iAkk >= 0) { resultExpr = refAkk; } else { resultExpr = new CallExpr(FuncDefs_Solver.SolverWeightedSumsAdd, refAkk, new CallExpr(FuncDefs_Solver.SolverWeightedSumsNew, new ReferenceExpr(sFactory), valsExpr, keysExpr)); } return(resultExpr); }
public static T ReplaceValueInfoRefsWithData <T>(T expr, string dataSrcName, Dictionary <string, object> dictValues = null) where T : Expr { var dataRef = new ReferenceExpr(dataSrcName); T res = (T)expr.Visit(Expr.RecursiveModifier(e => { var refExpr = e as ReferenceExpr; if (refExpr == null || !ValueInfo.IsDescriptor(refExpr.name)) { return(e); } if (dictValues != null) { dictValues[refExpr.name] = null; } return(new IndexExpr(dataRef, new ConstExpr(refExpr.name))); })); return(res); }
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()); }