public SqlSectionExpr Postprocess() { switch (sectionName) { case "SELECT": case "FROM": var lst = new Expr[args.Count]; for (int i = 0; i < args.Count; i++) { var expr = args[i]; var seq = expr as SequenceExpr; Expr item; if (seq != null && seq.args.Count > 1) { item = AliasExpr.AsAlias(seq.args) ?? expr; } else { item = expr; } lst[i] = item; } var res = new SqlSectionExpr(sectionName, lst); return(res); default: return(this); } }
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()); }