private void Prune(IPlainSelect selectBody) { if (selectBody.SelItems.Select(s => s.Exp).OfType <CountAllFunction>().Any() && !(selectBody.FromItem is SubSelect)) { return; } var expressionsToCheck = selectBody.SelItems.Select(s => s.Exp).ToList(); if (selectBody.WhereExp != null) { expressionsToCheck.Add(selectBody.WhereExp); } var usedAliases = (from selectExp in expressionsToCheck from column in selectExp.Descendants().OfType <Column>() select column.Table.Alias.Name).Distinct(); for (int i = selectBody.Joins.Count - 1; i >= 0; i--) { var join = selectBody.Joins[i]; if (join.RightItem is Table table && !usedAliases.Contains(table.Alias.Name)) { selectBody.Joins.RemoveAt(i); } } if (selectBody.FromItem is SubSelect subSelect) { Prune(subSelect.SelectBody); } }
private void Complete(IPlainSelect body, SelectContext context) { if (body.SelItems[0].Exp == null) { var table = body.FromItem as Table; body.SelItems[0].Exp = CreateColumn(table); } Prune(body); }
private static string Print(ISelectItem selectItem, IPlainSelect selectBody) { var expression = PrintExpression((dynamic)selectItem.Exp, selectBody); if (selectItem.Alias != null) { expression += " " + selectItem.Alias.Name; } return(expression); }
private static string PrintExpression(Column column, IPlainSelect selectBody) { var prefix = column.Table?.Alias?.Name; if (prefix != null) { prefix += "."; } return(prefix + column.Name); }
/// <summary> /// Adds the given element to the collection /// </summary> /// <param name="item">The item to add</param> public override void Add(IModelElement item) { if ((this._parent.SelectBody == null)) { IPlainSelect selectBodyCasted = item.As <IPlainSelect>(); if ((selectBodyCasted != null)) { this._parent.SelectBody = selectBodyCasted; return; } } }
private static string PrintExpression(CountAllFunction countAll, IPlainSelect selectBody) { if (selectBody.Joins.Count > 0) { var lastJoin = selectBody.Joins.Last(); if (lastJoin.OnExp is EqualsToExpression equals && equals.RightExp is Column targetColumn) { return($"COUNT({targetColumn.Table.Alias.Name}.{targetColumn.Name})"); } } return("COUNT(*)"); }
public static string Print(IPlainSelect selectBody) { var resultBuilder = new StringBuilder(); resultBuilder.Append($"SELECT {string.Join(", ", selectBody.SelItems.Select(sel => Print(sel, selectBody)))}"); if (selectBody.FromItem != null) { resultBuilder.Append($" FROM {PrintFrom((dynamic)selectBody.FromItem)}"); } foreach (var join in selectBody.Joins) { resultBuilder.Append($" {(join.Left.GetValueOrDefault() ? "LEFT" : "INNER")} JOIN {PrintFrom((dynamic)join.RightItem)} ON {PrintExpression((dynamic)join.OnExp, selectBody)}"); } if (selectBody.WhereExp != null) { resultBuilder.Append($" WHERE {PrintExpression((dynamic)selectBody.WhereExp, selectBody)}"); } if (selectBody.GroupBy != null) { resultBuilder.Append($" GROUP BY {string.Join(", ", selectBody.GroupBy.GroupByExps.Select(exp => PrintExpression((dynamic)exp, selectBody)))}"); } return(resultBuilder.ToString()); }
private static string PrintExpression(AndExpression andExpression, IPlainSelect selectBody) { return($"{PrintExpression((dynamic)andExpression.LeftExp, selectBody)} and {PrintExpression((dynamic)andExpression.RightExp, selectBody)}"); }
private static string PrintExpression(GreaterThanExpression greaterThan, IPlainSelect selectBody) { return($"{PrintExpression((dynamic)greaterThan.LeftExp, selectBody)} > {PrintExpression((dynamic)greaterThan.RightExp, selectBody)}"); }
private static string PrintExpression(LongValue longValue, IPlainSelect selectBody) { return(longValue.Value.ToString()); }
private static string PrintExpression(StringValue stringValue, IPlainSelect selectBody) { return(stringValue.Value); }
private static string PrintExpression(EqualsToExpression equalsTo, IPlainSelect selectBody) { return($"{PrintExpression((dynamic)equalsTo.LeftExp, selectBody)} = {PrintExpression((dynamic)equalsTo.RightExp, selectBody)}"); }