public string BuildSqlString() { if (Selects.Count == 0) { throw new InvalidOperationException("A query must have a select part"); } foreach (var qs in Simplifications) { if (qs.CanSimplify(this)) { return(qs.Simplify(this)); } } var countOperator = ResultOperators.FirstOrDefault(it => it is CountResultOperator || it is LongCountResultOperator); if (countOperator != null) { return(BuildCountQuery(countOperator)); } var sb = new StringBuilder(); sb.Append("SELECT "); sb.Append(string.Join(@", " , Selects.Select(it => it.Sql))).AppendLine(); sb.Append(GetFromPart()); sb.Append(GetWherePart()); sb.Append(GetOrderPart()); ProcessResultOperators(sb); return(sb.ToString()); }
public string BuildSqlString(bool canUseOperators) { if (MainFrom == null) { throw new ApplicationException("From !?"); } foreach (var qs in Simplifications) { if (qs.CanSimplify(this)) { return(qs.Simplify(this)); } } var sb = new StringBuilder(); sb.Append("SELECT "); var groupOperator = ResultOperators.FirstOrDefault(it => it is GroupResultOperator) as GroupResultOperator; if (groupOperator != null) { if (Selects.Count != 1) { throw new FrameworkException("Select count can only be two when grouping!"); } sb.AppendFormat( "{0} AS \"Key\", ARRAY_AGG(\"{1}\") AS \"Values\"", GetSqlExpression(groupOperator.KeySelector), Selects[0].QuerySource.ItemName); } else { if (canUseOperators) { var countOperator = ResultOperators.FirstOrDefault(it => it is CountResultOperator || it is LongCountResultOperator); if (countOperator != null) { return(BuildCountQuery(countOperator)); } var op = ResultOperators.FirstOrDefault(it => it is SumResultOperator || it is AverageResultOperator || it is MaxResultOperator || it is MinResultOperator); if (op != null) { var sqlOp = ConvertToSqlOperator(op); if (Selects.Count > 1) { //TOOD hack to fix problem with select Selects.Clear(); Selects.Add(new SelectSource { Sql = GetSqlExpression(Selector), ItemType = Selector.Type }); } sb.Append(sqlOp(Selects[0].Sql)); //TODO use actual type if (Selects[0].ItemType == typeof(int) || Selects[0].ItemType == typeof(int?)) { sb.Append("::int"); } else if (Selects[0].ItemType == typeof(double) || Selects[0].ItemType == typeof(double?)) { sb.Append("::float"); } else if (Selects[0].ItemType == typeof(float) || Selects[0].ItemType == typeof(float?)) { sb.Append("::real"); } else if (Selects[0].ItemType == typeof(long) || Selects[0].ItemType == typeof(long?)) { sb.Append("::bigint"); } if (Selects[0].Name != null) { sb.AppendFormat(" AS \"{0}\"", Selects[0].Name); } sb.AppendLine(); sb.Append(GetFromPart()); sb.Append(GetWherePart()); return(sb.ToString()); } var containsOperator = ResultOperators.FirstOrDefault(it => it is ContainsResultOperator); if (containsOperator != null) { if (Selects.Count > 1) { //TOOD hack to fix problem with select Selects.Clear(); Selects.Add(new SelectSource { Sql = GetSqlExpression(Selector) }); } } } sb.Append(string.Join(@", ", Selects.Select(it => it.Sql))); } sb.AppendLine(); sb.Append(GetFromPart()); sb.Append(GetWherePart()); sb.Append(GetOrderPart()); ProcessResultOperators(sb); return(sb.ToString()); }