public TResult Calculate(QueryModel queryModel) { var projector = ProjectorBuildingExpressionTreeVisitor <TInput> .BuildProjector(queryModel); var resultItems = GetData().Select(it => projector(it)); var type = typeof(TInput); if (typeof(TInput).IsGenericType) { type = typeof(TInput).GetGenericArguments()[0]; } if (type == typeof(decimal)) { return((TResult)((object)resultItems.Cast <decimal?>().Average())); } else if (type == typeof(long)) { return((TResult)((object)resultItems.Cast <long?>().Average())); } else if (type == typeof(int)) { return((TResult)((object)resultItems.Cast <int?>().Average())); } else if (type == typeof(double)) { return((TResult)((object)resultItems.Cast <double?>().Average())); } else if (type == typeof(float)) { return((TResult)((object)resultItems.Cast <float?>().Average())); } throw new NotSupportedException("Unknown type for sum. Supported types: decimal, long, int, double, float"); }
private T ExecuteSum <T>(QueryModel queryModel) { var projector = ProjectorBuildingExpressionTreeVisitor <T> .BuildProjector(queryModel); var resultItems = LoadData(queryModel).Select(it => projector(it)); var type = typeof(T); if (typeof(T).IsGenericType) { type = typeof(T).GetGenericArguments()[0]; } if (type == typeof(decimal)) { return((T)((object)resultItems.Cast <decimal?>().Sum())); } else if (type == typeof(long)) { return((T)((object)resultItems.Cast <long?>().Sum())); } else if (type == typeof(int)) { return((T)((object)resultItems.Cast <int?>().Sum())); } else if (type == typeof(double)) { return((T)((object)resultItems.Cast <double?>().Sum())); } else if (type == typeof(float)) { return((T)((object)resultItems.Cast <float?>().Sum())); } throw new NotSupportedException("Unknown type for sum. Supported types: decimal, long, int, double, float"); }
private static Func <string, T> ProjectMapping <T>(SubQueryExpression sqe, QueryParts parts, SubqueryParts subquery) { Func <string, T> result; var projector = ProjectorBuildingExpressionTreeVisitor <T> .BuildProjector(sqe.QueryModel); if (subquery.Selects.Count == 1) { var sel = subquery.Selects[0]; var factory = CreateResult(sel.Name, sel.ItemType, sel.QuerySource, parts, true); result = value => (T)factory.Instancer(value); } else { var ssd = SelectSubqueryData.Create(parts, subquery); result = value => { //TODO fix later return(default(T)); /* * var items = OracleRecordConverter.ParseRecord(value); * var rom = ssd.ProcessRow(null, items); * var res = projector(rom); * return res;*/ }; } return(result); }
private static Func <string, BufferedTextReader, T> ProjectMapping <T>(SubQueryExpression sqe, QueryParts parts, SubqueryParts subquery) { Func <string, BufferedTextReader, T> result; var projector = ProjectorBuildingExpressionTreeVisitor <T> .BuildProjector(sqe.QueryModel); if (subquery.Selects.Count == 1) { var sel = subquery.Selects[0]; var factory = CreateResult(sel.Name, sel.ItemType, sel.QuerySource, parts); result = (value, reader) => (T)factory.Instancer(value, reader); } else { var ssd = SelectSubqueryData.Create(parts, subquery); //TODO TextReader/string !? result = (value, reader) => { var items = PostgresRecordConverter.ParseRecord(value); var rom = ssd.ProcessRow(null, reader, items); var res = projector(rom); return(res); }; } return(result); }
private T ExecuteSeedAggregate <T>(QueryModel queryModel, AggregateFromSeedResultOperator seedAggregate) { var projector = ProjectorBuildingExpressionTreeVisitor <T> .BuildAggregateProjector(queryModel, seedAggregate.Func); var resultItems = LoadData(queryModel); var result = seedAggregate.GetConstantSeed <T>(); foreach (var item in resultItems) { result = projector(item)(result); } return(result); }
protected Func <object, T> GetSelectProjector <T>(object firstResult, QueryModel queryModel) { Func <object, T> projector = result => result.Cast <T>(); if (!ShouldBuildResultObjectMapping <T>(firstResult, queryModel)) { return(projector); } var proj = ProjectorBuildingExpressionTreeVisitor.BuildProjector <T>(queryModel.SelectClause.Selector); projector = result => proj(new ResultObjectMapping(queryModel.MainFromClause, result)); return(projector); }
private static Func <string, T> ProjectMapping <T>(SubQueryExpression sqe, QueryParts parts, SubqueryParts subquery) { var ssd = SelectSubqueryData.Create(parts, subquery); var projector = ProjectorBuildingExpressionTreeVisitor <T> .BuildProjector(sqe.QueryModel); //TODO TextReader/string !? Func <string, T> result = value => { var items = PostgresRecordConverter.ParseRecord(value); var rom = ssd.ProcessRow(null, items); var res = projector(rom); return(res); }; return(result); }
// Executes a query with a collection result. public IEnumerable <T> ExecuteCollection <T>(QueryModel queryModel) { var hasUnion = queryModel.ResultOperators.Any(it => it is UnionResultOperator || it is ConcatResultOperator); if (hasUnion) { queryModel = queryModel.ConvertToSubQuery("sq"); } var projector = ProjectorBuildingExpressionTreeVisitor <T> .BuildProjector(queryModel); var resultItems = LoadData(queryModel); foreach (var item in resultItems) { yield return(projector(item)); } }
private T ExecuteAggregate <T>(QueryModel queryModel, AggregateResultOperator aggregate) { var valueProjector = ProjectorBuildingExpressionTreeVisitor <T> .BuildProjector(queryModel); var funcProjector = ProjectorBuildingExpressionTreeVisitor <T> .BuildAggregateProjector(queryModel, aggregate.Func); var resultItems = LoadData(queryModel); if (resultItems.Count == 0) { throw new DataException("For aggregate operation at least one result must be used"); } var result = valueProjector(resultItems[0]); foreach (var item in resultItems.Skip(1)) { result = funcProjector(item)(result); } return(result); }
private static Func <string, T> ProjectMapping <T>(SubQueryExpression sqe, QueryParts parts, SubqueryParts subquery) { var ssd = SelectSubqueryData.Create(parts, subquery); var projector = ProjectorBuildingExpressionTreeVisitor <T> .BuildProjector(sqe.QueryModel); Func <string, T> result = value => { //TODO fix later return(default(T)); /* * var items = OracleRecordConverter.ParseRecord(value); * var rom = ssd.ProcessRow(null, items); * var res = projector(rom); * return res;*/ }; return(result); }