static void Main(string[] args) { var freesql = new FreeSql.FreeSqlBuilder() .UseConnectionString(FreeSql.DataType.PostgreSQL, "") .Build(); TableInfomationInitor.Initialize(freesql, typeof(Test), typeof(DomainFlags)); OrmNavigate <Test> .Join <Test2>(item => item.DomainFlags, item => item.Id); var sql = new JoinTemplate <Test>(freesql); var result = sql.ToList(item => new { item.Id, item.DomainFlags, DomainName = RightJoin <DomainFlags> .MapFrom(item => item.Name) }); sql = new JoinTemplate <Test>(freesql); var result1 = sql.ToList(item => new { item.Id, item.DomainFlags, DomainName = InnerJoin <DomainFlags> .MapFrom(item => item.Name) }); Console.ReadKey(); }
/// <summary> /// Adds a right join to the query. /// </summary> /// <param name="alias">The alias with tablename/alias name.</param> /// <param name="onCriteria">The ON criteria.</param> /// <returns></returns> /// <exception cref="ArgumentNullException"> /// alias is null /// or /// alias.table is null /// </exception> public SqlQuery RightJoin(IAlias alias, ICriteria onCriteria) { if (alias == null) { throw new ArgumentNullException("alias"); } if (string.IsNullOrEmpty(alias.Table)) { throw new ArgumentNullException("alias.table"); } var join = new RightJoin(alias.Table, alias.Name, onCriteria); Join(join); if (alias as IHaveJoins != null) { AliasWithJoins[alias.Name] = alias as IHaveJoins; } return(this); }
public IEnumerable <object> ToList <TReturn>(Expression <Func <TEntity, TReturn> > expression) { var code = expression.GetHashCode(); if (SelectHandler == null) { SelectHandler = SqlHandler.Select <TEntity>(); } var type = typeof(TReturn); if (!JoinExpressionMapping.Contains(code)) { var nclass = NClass.DefaultDomain().Public(); StringBuilder script = new StringBuilder(); // 获取构造函数参数 var arguments = ((NewExpression)expression.Body).Arguments; //获取匿名类成员 var members = ((NewExpression)expression.Body).Members; var joinTypeFlagMapping = new Dictionary <Type, JoinTypeFlag>(); for (int i = 0; i < arguments.Count; i++) { // 方法类型参数 if (arguments[i].NodeType == ExpressionType.Call) { var methodExpression = (MethodCallExpression)arguments[i]; var methodDeclaringType = methodExpression.Method.DeclaringType; if (methodDeclaringType.IsGenericType && methodDeclaringType.GetGenericTypeDefinition().Name.Contains("Join")) { var joinType = methodDeclaringType.GetGenericArguments()[0]; if (methodExpression.Arguments[0].NodeType == ExpressionType.Quote) { var quoteExpression = (UnaryExpression)methodExpression.Arguments[0]; if (quoteExpression.Operand.NodeType == ExpressionType.Lambda) { var lambdaExpression = (LambdaExpression)quoteExpression.Operand; if (lambdaExpression.Body.NodeType == ExpressionType.MemberAccess) { var memberExpression = (MemberExpression)lambdaExpression.Body; nclass.Property(item => item .Public() .Type(((PropertyInfo)memberExpression.Member).PropertyType) .Name(memberExpression.Member.Name)); var definitionType = methodDeclaringType.GetGenericTypeDefinition(); if (definitionType == typeof(InnerJoin <>)) { joinTypeFlagMapping[joinType] = JoinTypeFlag.Inner; script.Append(InnerJoin.GetJoinScript(joinType)); } else if (definitionType == typeof(LeftJoin <>)) { joinTypeFlagMapping[joinType] = JoinTypeFlag.Left; script.Append(LeftJoin.GetJoinScript(joinType)); } else if (definitionType == typeof(RightJoin <>)) { joinTypeFlagMapping[joinType] = JoinTypeFlag.Right; script.Append(RightJoin.GetJoinScript(joinType)); } script.Append($".\"{memberExpression.Member.Name}\" AS \"{members[i].Name}\","); } } } } } else if (arguments[i].NodeType == ExpressionType.MemberAccess) { var memberExpression = (MemberExpression)arguments[i]; nclass.Property(item => item .Public() .Type(((PropertyInfo)memberExpression.Member).PropertyType) .Name(memberExpression.Member.Name)); script.Append($"a.\"{memberExpression.Member.Name}\","); } //JoinObjectCache<ISelect<T>, TReturn>.GetObjects = NDelegate.RandomDomain().Func<ISelect<T>, object>(builder.ToString()); } if (script.Length > 1) { script.Length -= 1; var joinScript = script.ToString(); JoinExpressionMapping = JoinExpressionMapping.Add(code); var tempClass = nclass.GetType(); ProxyCaller <TEntity, TReturn> .Add(code, NDelegate .DefaultDomain(item => item.LogSyntaxError()) .Func <ISelect <TEntity>, IEnumerable <object> >($"return arg.ToList<{tempClass.GetDevelopName()}>(\"{joinScript.Replace("\"","\\\"")}\");")); var builder = new StringBuilder(); foreach (var item in joinTypeFlagMapping) { var joinFieldCache = OrmNavigate <TEntity> .JoinScriptMapping[item.Key]; string joinAlias = string.Empty; switch (item.Value) { case JoinTypeFlag.Left: joinAlias = LeftJoin.GetJoinScript(item.Key); builder.Append($"obj.LeftJoin(\""); break; case JoinTypeFlag.Inner: joinAlias = InnerJoin.GetJoinScript(item.Key); builder.Append($"obj.InnerJoin(\""); break; case JoinTypeFlag.Right: joinAlias = RightJoin.GetJoinScript(item.Key); builder.Append($"obj.RightJoin(\""); break; default: break; } var joinFieldScript = $"\"{item.Key.Name}\" AS {joinAlias} ON a.\"{joinFieldCache.src}\" = {joinAlias}.\"{joinFieldCache.dst}\""; builder.Append(joinFieldScript.Replace("\"", "\\\"")); builder.AppendLine("\");"); } //$"\"{typeof(TJoinEntity).Name}\" AS {InnerJoinHelper<TJoinEntity>.JoinAliasName} ON a.\"{srcFieldName}\" = {InnerJoinHelper<TJoinEntity>.JoinAliasName}.\"{destFieldName}\"") JoinFiller <TEntity, TReturn> .Add(code, NDelegate .DefaultDomain() .Action <ISelect <TEntity> >(builder.ToString())); } } JoinFiller <TEntity, TReturn> .HandlerSelect(code, SelectHandler); return(ProxyCaller <TEntity, TReturn> .ToList(code, SelectHandler)); }
protected override void SetupJoinConditions() { RightJoin.Left(_keys).Right(_keys); }