public void TestBinaryMaterializationWithNullable() { GetExpressionTextVisitor expVisitor = new GetExpressionTextVisitor(); Guid?nullableValue = Guid.NewGuid(); // Nullable has some special handling in .Net var exp = LambdaBuilder.Lambda((CORE_PERSON person) => person.fk_core_user == nullableValue.Value); ExpressionText expText; expText = expVisitor.GetExpressionText(exp); expText.AssertExpTextCanBeConvertedToLambda <bool>(); Assert.Equal($"person.fk_core_user == @p0", expText.Expression); this.AssertHasArgument("@p0", nullableValue.Value, expText); nullableValue = null; expText = expVisitor.GetExpressionText(exp); expText.AssertExpTextCanBeConvertedToLambda <bool>(); Assert.Equal($"person.fk_core_user == @p0", expText.Expression); this.AssertHasArgument("@p0", null, expText); }
public void TestObjectCreate() { GetExpressionTextVisitor expVisitor = new GetExpressionTextVisitor(); ExpressionText expText = null; expText = expVisitor.GetExpressionText( LambdaBuilder.Lambda((CORE_PERSON i) => new { i.CORE_USER.mail })); expText.AssertExpTextCanBeConvertedToLambda <object>(); Assert.Equal("new(i.CORE_USER.mail as mail)", expText.Expression); expText = expVisitor.GetExpressionText( LambdaBuilder.Lambda((CORE_PERSON i) => new { mail2 = i.CORE_USER.mail })); expText.AssertExpTextCanBeConvertedToLambda <object>(); Assert.Equal("new(i.CORE_USER.mail as mail2)", expText.Expression); }
public async Task <ActionResult <IEnumerable <Bicycle> > > GetAll([FromQuery] UserParams userParams) { string qString = Request.QueryString.Value; var lambda = LambdaBuilder <Bicycle> .Builder(Request.QueryString.Value); var bicycles = _context.Bicycle.Include(p => p.Paint).OrderByDescending(u => u.Customerid).AsQueryable(); if (lambda != null) { bicycles = bicycles.Where(lambda); if (Request.Query["paint"].Count > 0) { Expression paintEx = Expression.Default(typeof(bool)); var parameters = Expression.Parameter(typeof(Bicycle), typeof(Bicycle).Name); MemberExpression mem = Expression.Property(parameters, typeof(Bicycle).GetProperty("Paint")); var prop = Expression.Property(mem, "Colorlist"); string[] paintFilters = Request.Query["paint"][0].Split("|"); foreach (string f in paintFilters) { ConstantExpression cons = Expression.Constant(f); paintEx = Expression.Or(paintEx, Expression.Equal(prop, cons)); } Expression <Func <Bicycle, bool> > ex = Expression.Lambda <Func <Bicycle, bool> >(paintEx, parameters); bicycles = bicycles.Where(ex); } } // do some filtering... // ... // .. await TimeStampTransaction(); return(Ok(await PageList <Bicycle> .CreateAsync(bicycles, userParams.PageNumber, userParams.PageSize))); }
public Resolver(ISerializer serializer, DocumentMapping mapping) { _serializer = serializer; _mapping = mapping; IdType = TypeMappings.ToDbType(mapping.IdMember.GetMemberType()); _loaderSql = $"select {_mapping.SelectFields().Join(", ")} from {_mapping.Table.QualifiedName} as d where id = :id"; _deleteSql = $"delete from {_mapping.Table.QualifiedName} where id = :id"; _loadArraySql = $"select {_mapping.SelectFields().Join(", ")} from {_mapping.Table.QualifiedName} as d where id = ANY(:ids)"; _identity = LambdaBuilder.Getter <T, object>(mapping.IdMember); _sprocWriter = buildSprocWriter(mapping); _upsertName = mapping.UpsertFunction; if (mapping.DeleteStyle == DeleteStyle.Remove) { DeleteByIdSql = $"delete from {_mapping.Table.QualifiedName} where id = ?"; DeleteByWhereSql = $"delete from {_mapping.Table.QualifiedName} as d where ?"; } else { DeleteByIdSql = $"update {_mapping.Table.QualifiedName} set {DocumentMapping.DeletedColumn} = True, {DocumentMapping.DeletedAtColumn} = now() where id = ?"; DeleteByWhereSql = $"update {_mapping.Table.QualifiedName} as d set {DocumentMapping.DeletedColumn} = True, {DocumentMapping.DeletedAtColumn} = now() where ?"; } }
private static ICode RegisterSerializerResolver() => MethodCallBuilder.New() .SetMethodName(TypeNames.AddSingleton) .AddGeneric(TypeNames.ISerializerResolver) .AddArgument("services") .AddArgument(LambdaBuilder.New() .AddArgument("sp") .SetCode( MethodCallBuilder.New() .SetPrefix("new ") .SetMethodName(TypeNames.SerializerResolver) .SetDetermineStatement(false) .SetWrapArguments() .AddArgument(MethodCallBuilder.New() .SetMethodName(TypeNames.Concat) .SetDetermineStatement(false) .AddArgument( MethodCallBuilder.New() .SetMethodName(TypeNames.GetRequiredService) .SetDetermineStatement(false) .SetWrapArguments() .AddGeneric( TypeNames.IEnumerable.WithGeneric(TypeNames .ISerializer)) .AddArgument("parentServices")) .AddArgument(MethodCallBuilder.New() .SetMethodName(TypeNames.GetRequiredService) .SetDetermineStatement(false) .SetWrapArguments() .AddGeneric( TypeNames.IEnumerable.WithGeneric(TypeNames.ISerializer)) .AddArgument("sp")))));
private static ICode GenerateMethodBody( CSharpSyntaxGeneratorSettings settings, DependencyInjectionDescriptor descriptor) => CodeBlockBuilder .New() .AddMethodCall(x => x .SetMethodName(TypeNames.AddSingleton) .AddArgument(_services) .AddArgument(LambdaBuilder .New() .SetBlock(true) .AddArgument(_sp) .SetCode(GenerateClientServiceProviderFactory(descriptor)))) .AddEmptyLine() .AddCode(RegisterStoreAccessor(settings, descriptor.StoreAccessor)) .AddEmptyLine() .ForEach( descriptor.Operations, (builder, operation) => builder.AddCode(ForwardSingletonToClientServiceProvider( operation.RuntimeType.ToString()))) .AddEmptyLine() .AddCode(ForwardSingletonToClientServiceProvider( descriptor.ClientDescriptor.RuntimeType.ToString())) .AddCode(ForwardSingletonToClientServiceProvider( descriptor.ClientDescriptor.InterfaceType.ToString())) .AddEmptyLine() .AddMethodCall(x => x .SetReturn() .SetNew() .SetMethodName( TypeNames.ClientBuilder.WithGeneric(descriptor.StoreAccessor.RuntimeType)) .AddArgument(descriptor.Name.AsStringToken()) .AddArgument(_services));
private static ICode RegisterSerializerResolver() => MethodCallBuilder .New() .SetMethodName(TypeNames.AddSingleton) .AddGeneric(TypeNames.ISerializerResolver) .AddArgument(_services) .AddArgument(LambdaBuilder .New() .AddArgument(_sp) .SetCode( MethodCallBuilder .Inline() .SetNew() .SetMethodName(TypeNames.SerializerResolver) .SetWrapArguments() .AddArgument(MethodCallBuilder .Inline() .SetMethodName(TypeNames.Concat) .AddArgument( MethodCallBuilder .Inline() .SetMethodName(TypeNames.GetRequiredService) .SetWrapArguments() .AddGeneric( TypeNames.IEnumerable.WithGeneric( TypeNames.ISerializer)) .AddArgument(_parentServices)) .AddArgument(MethodCallBuilder .Inline() .SetMethodName(TypeNames.GetRequiredService) .SetWrapArguments() .AddGeneric( TypeNames.IEnumerable.WithGeneric(TypeNames.ISerializer)) .AddArgument(_sp)))));
private static ICode RegisterHttpConnection(string clientName) => MethodCallBuilder .New() .SetMethodName(TypeNames.AddSingleton) .AddArgument(_services) .AddGeneric(TypeNames.IHttpConnection) .AddArgument(LambdaBuilder .New() .AddArgument(_sp) .SetBlock(true) .SetCode(CodeBlockBuilder .New() .AddCode(AssignmentBuilder .New() .SetLefthandSide($"var {_clientFactory}") .SetRighthandSide(MethodCallBuilder .Inline() .SetMethodName(TypeNames.GetRequiredService) .AddGeneric(TypeNames.IHttpClientFactory) .AddArgument(_parentServices))) .AddCode(MethodCallBuilder .New() .SetReturn() .SetNew() .SetMethodName(TypeNames.HttpConnection) .AddArgument(LambdaBuilder .New() .SetCode(MethodCallBuilder .Inline() .SetMethodName( _clientFactory, nameof(IHttpClientFactory.CreateClient)) .AddArgument(clientName.AsStringToken()))))));
public static Expression Coalesce(LambdaBuilder builder, Expression left, Expression right) { ParameterExpression temp; Expression result = Coalesce(left, right, out temp); builder.AddHiddenVariable(temp); return result; }
public TEntity Get(Expression <Func <TEntity, bool> > filter) { LambdaBuilder lambdaBuilder = new LambdaBuilder(); var query = lambdaBuilder.ToSql(filter); return(connection.QueryFirstOrDefault <TEntity>($"{SELECT_FIRST_ENTITY_QUERY} WHERE {query.Sql}", query.Parameters)); }
private static ICode RegisterWebSocketConnection(string clientName) => MethodCallBuilder .New() .SetMethodName(TypeNames.AddSingleton) .AddArgument(_services) .AddArgument(LambdaBuilder .New() .AddArgument(_sp) .SetBlock(true) .SetCode(CodeBlockBuilder .New() .AddCode(AssignmentBuilder .New() .SetLefthandSide($"var {_sessionPool}") .SetRighthandSide(MethodCallBuilder .Inline() .SetMethodName(TypeNames.GetRequiredService) .AddGeneric(TypeNames.ISessionPool) .AddArgument(_parentServices))) .AddCode(MethodCallBuilder .New() .SetReturn() .SetNew() .SetMethodName(TypeNames.WebSocketConnection) .AddArgument(LambdaBuilder .New() .SetCode(MethodCallBuilder .Inline() .SetMethodName( _sessionPool, nameof(ISessionPool.CreateAsync)) .AddArgument(clientName.AsStringToken()) .AddArgument("default"))))));
public DictionaryElement(EnumStorage storage, string[] keys, MemberInfo member) { Keys = keys; _getter = LambdaBuilder.Getter <TQuery, TElement>(storage, new [] { member }); Member = member; }
/// <summary> /// False coalescing expression. /// {result} ::= IsTrue(tmp = {left}) ? tmp : {right} /// Generalized OR semantics. /// </summary> public static Expression CoalesceFalse(LambdaBuilder builder, Expression left, Expression right, MethodInfo isTrue) { ContractUtils.RequiresNotNull(isTrue, "isTrue"); ParameterExpression temp; Expression result = CoalesceFalse(left, right, isTrue, out temp); builder.AddHiddenVariable(temp); return result; }
public QueryStatisticsFinder(PropertyInfo property) { _getter = LambdaBuilder.GetProperty <TObject, QueryStatistics>(property); if (property.CanWrite) { _setter = LambdaBuilder.SetProperty <TObject, QueryStatistics>(property); } }
public void CanCreatePropertyAccessor() { var prop = typeof(TesterClass).GetProperty("TestProp", BindingFlags.GetProperty | BindingFlags.NonPublic | BindingFlags.Instance); var func = LambdaBuilder.CreatePropertyFunction(prop); var z = (string)func(_tester, null); Assert.IsTrue(_tester.TestPropHit); Assert.AreEqual("Hello", z); }
static InvokeMemberBinderExtensions() { Type type = typeof(CSharpBinderFlags) .Assembly .GetType("Microsoft.CSharp.RuntimeBinder.ICSharpInvokeOrInvokeMemberBinder"); PropertyInfo prop = type .GetProperty("ResultDiscarded"); ResultDiscardedFunc = LambdaBuilder.CreatePropertyFunction(prop); }
public void CanCreateFunc() { var meth = typeof(TesterClass).GetMethod("TestInt"); var func = LambdaBuilder.CreateFunction(meth); const int input = 5; var z = (int)func(_tester, new object[] { input }); Assert.IsTrue(_tester.TestIntHit); Assert.AreEqual(input * 100, z); }
public ScriptScope(ScriptScope parent, string name, SymbolDocumentInfo document) { _parent = parent; _block = Utils.Lambda(typeof(object), name ?? MakeLambdaName()); if (parent != null && document == null) { document = _parent.Document; } _document = document; }
public void CanCreateAction() { var meth = typeof(TesterClass).GetMethod("TestVoid"); var action = LambdaBuilder.CreateAction(meth); action(_tester, null); Assert.IsTrue(_tester.TestVoidHit); }
public void can_build_getter_for_field() { var guy = new GuyWithField(); var field = typeof(GuyWithField).GetField("Id"); var getter = LambdaBuilder.GetField <GuyWithField, Guid>(field); getter(guy).ShouldBe(guy.Id); }
public void Add(GeneratedType generatedType, MemberInfo member, string setterFieldName) { var writer = LambdaBuilder.Setter <TTarget, TMember>(member); var setter = new Setter(typeof(Action <TTarget, TMember>), setterFieldName) { InitialValue = writer, Type = SetterType.ReadWrite }; generatedType.Setters.Add(setter); }
public void can_build_getter_for_property() { var target = new Target { Number = 5 }; var prop = ReflectionHelper.GetProperty <Target>(x => x.Number); var getter = LambdaBuilder.GetProperty <Target, int>(prop); getter(target).ShouldBe(target.Number); }
public void can_set_a_private_id() { var member = ReflectionHelper.GetProperty <UserWithPrivateId>(x => x.Id); var setter = LambdaBuilder.Setter <UserWithPrivateId, Guid>(member); var newGuid = Guid.NewGuid(); var userWithPrivateId = new UserWithPrivateId(); setter(userWithPrivateId, newGuid); userWithPrivateId.Id.ShouldBe(newGuid); }
public List <TEntity> GetAll(Expression <Func <TEntity, bool> > filter = null) { if (filter == null) { return(connection.Query <TEntity>(SELECT_QUERY).ToList()); } else { LambdaBuilder lambdaBuilder = new LambdaBuilder(); var query = lambdaBuilder.ToSql(filter); return(connection.Query <TEntity>($"{SELECT_QUERY} WHERE {query.Sql}", query.Parameters).ToList()); } }
public int Count(Expression <Func <TEntity, bool> > filter = null) { if (filter == null) { return(connection.ExecuteScalar <int>(COUNT_QUERY)); } else { LambdaBuilder lambdaBuilder = new LambdaBuilder(); var query = lambdaBuilder.ToSql(filter); return(connection.ExecuteScalar <int>($"{COUNT_QUERY} WHERE {query.Sql}", query.Parameters)); } }
public void TestPropertySelector() { GetExpressionTextVisitor expVisitor = new GetExpressionTextVisitor(); ExpressionText expText = null; expText = expVisitor.GetExpressionText( LambdaBuilder.Lambda((CORE_PERSON i) => i.CORE_USER.mail)); expText.AssertExpTextCanBeConvertedToLambda <string>(); Assert.Equal("i.CORE_USER.mail", expText.Expression); }
public void can_build_setter_for_field() { var guy = new GuyWithField(); var field = typeof(GuyWithField).GetField("Id"); var setter = LambdaBuilder.SetField <GuyWithField, Guid>(field); var newGuid = Guid.NewGuid(); setter(guy, newGuid); guy.Id.ShouldBe(newGuid); }
public void TestNullCompare() { GetExpressionTextVisitor expVisitor = new GetExpressionTextVisitor(); ExpressionText expText = null; expText = expVisitor.GetExpressionText( LambdaBuilder.Lambda((CORE_PERSON i) => i.deletedAt != null)); expText.AssertExpTextCanBeConvertedToLambda <bool>(); Assert.Equal("i.deletedAt != @p0", expText.Expression); this.AssertHasArgument("@p0", null, expText); }
public void TestModulus() { GetExpressionTextVisitor expVisitor = new GetExpressionTextVisitor(); ExpressionText expText = null; expText = expVisitor.GetExpressionText( LambdaBuilder.Lambda((CORE_PERSON i) => i.deletedAt % 5)); expText.AssertExpTextCanBeConvertedToLambda <long?>(); Assert.Equal("i.deletedAt % @p0", expText.Expression); this.AssertHasArgument("@p0", 5, expText); }
public void TestBinaryMaterializationSimple2() { GetExpressionTextVisitor expVisitor = new GetExpressionTextVisitor(); var exp = LambdaBuilder.Lambda((CORE_PERSON person) => person.deletedAt + (2 + 3)); var expText = expVisitor.GetExpressionText(exp); expText.AssertExpTextCanBeConvertedToLambda <long?>(); Assert.Equal("person.deletedAt + @p0", expText.Expression); this.AssertHasArgument("@p0", 5, expText); }
private IDbParameterSetter CreateParameterSetter <TObject, TProperty>(PropertyInfo property) { var getter = LambdaBuilder.GetProperty <TObject, object>(property); if (property.PropertyType.IsEnum && _enumStorage == EnumStorage.AsString) { getter = o => { var number = getter(o); return(Enum.GetName(property.PropertyType, number)); }; } return(new DbParameterSetter <TObject, object>(getter)); }
public virtual Expression CompileUpdateExpression(EnumStorage enumStorage, ParameterExpression call, ParameterExpression doc, ParameterExpression updateBatch, ParameterExpression mapping, ParameterExpression currentVersion, ParameterExpression newVersion, bool useCharBufferPooling) { var argName = Expression.Constant(Arg); var memberType = Members.Last().GetMemberType(); var body = LambdaBuilder.ToExpression(enumStorage, Members, doc); if (!memberType.GetTypeInfo().IsClass) { body = Expression.Convert(body, typeof(object)); } return(Expression.Call(call, _paramMethod, argName, body, Expression.Constant(DbType))); }
private AstGenerator(string name, bool generator, bool print) { _print = print; _generatorLabel = generator ? Ast.Label(typeof(object)) : null; _block = AstUtils.Lambda(typeof(object), name); }