public async ValueTask <object> ExecuteScalar(string sql, SqlParameters parameters = null, object data = null) { sql = OffsetLimitUtils.TransformQuery(sql); var tree = _sqlParser.Parse(sql); var mainVisitor = new MainVisitor(new VisitorMetadata(parameters, _tablesMetadata, _searchExpressionProvider, _operationsProvider)); tree.Accept(mainVisitor); //Convert into execute stages var executeStages = _stageConverter.Convert(mainVisitor.Stages); var result = await _queryExecutor.Execute(executeStages, data); var enumerator = result.Result.GetEnumerator(); if (!enumerator.MoveNext()) { return(null); } else { var obj = enumerator.Current; return(AnonTypeUtils.GetDelegates(obj.GetType())[0](obj)); } }
public async ValueTask <SelectResult> Execute(IQueryable queryable, ExecuteAggregateFunctionStage executeAggregateFunctionStage) { var result = await ExecuteAggregateFunction((IQueryable <Entity>) queryable, executeAggregateFunctionStage); var outType = typeof(OutType); var anonType = AnonType.GetAnonType(outType); var getDelegate = AnonTypeUtils.GetDelegates(anonType)[0]; var columnMetadata = new ColumnMetadata(executeAggregateFunctionStage.ColumnName, result.GetType(), getDelegate); List <AnonType <OutType> > output = new List <AnonType <OutType> >() { new AnonType <OutType>() { P0 = result } }; return(new SelectResult(output.AsQueryable(), ImmutableList.Create(columnMetadata))); }
public static SelectStage GetSelectStage( IQueryStage previousStage, IList <SqlParser.Expressions.SelectExpression> selectElements, VisitorMetadata visitorMetadata, HashSet <PropertyInfo> usedProperties) { ISelectVisitor selectVisitor; if (previousStage is GroupedStage groupedStage) { selectVisitor = new SelectAggregationVisitor(groupedStage, visitorMetadata); } else { selectVisitor = new SelectPlainVisitor(previousStage, visitorMetadata); } foreach (var selectElement in selectElements) { selectVisitor.VisitSelect(selectElement); } foreach (var property in selectVisitor.UsedProperties) { usedProperties.Add(property); } var selects = selectVisitor.SelectExpressions; Debug.Assert(selects != null); Debug.Assert(selects.Count > 0); var typeBuilder = SqlTypeInfo.NewBuilder(); Type[] propertyTypes = new Type[selects.Count]; for (int i = 0; i < selects.Count; i++) { propertyTypes[i] = selects[i].Expression.Type; } var anonType = AnonType.GetAnonType(propertyTypes); var getDelegates = AnonTypeUtils.GetDelegates(anonType); NewExpression newExpression = Expression.New(anonType); List <MemberAssignment> assignments = new List <MemberAssignment>(); var columnsBuilder = ImmutableList.CreateBuilder <ColumnMetadata>(); for (int i = 0; i < selects.Count; i++) { columnsBuilder.Add(new ColumnMetadata(selects[i].Alias, selects[i].Expression.Type, getDelegates[i])); var propertyInfo = anonType.GetProperty($"P{i}"); var assignment = Expression.Bind(propertyInfo, selects[i].Expression); assignments.Add(assignment); typeBuilder.AddProperty(selects[i].Alias, propertyInfo); } var memberInit = Expression.MemberInit(newExpression, assignments); return(new SelectStage(typeBuilder.Build(), Expression.Parameter(anonType), memberInit, anonType, previousStage.CurrentType, previousStage.ParameterExpression, previousStage.FromAliases, columnsBuilder.ToImmutable())); }