Ejemplo n.º 1
0
            void Init()
            {
                var info1 = _sequence1.ConvertToIndex(null, 0, ConvertFlags.All).ToList();
                var info2 = _sequence2.ConvertToIndex(null, 0, ConvertFlags.All).ToList();

                if (!_isObject)
                {
                    return;
                }

                var members = new List <UnionMember>();

                foreach (var info in info1)
                {
                    if (info.Members.Count == 0)
                    {
                        throw new InvalidOperationException();
                    }

                    var mi = info.Members.First(m => m.DeclaringType.IsSameOrParentOf(_unionParameter.Type));

                    var member = new Member
                    {
                        SequenceInfo     = info,
                        MemberExpression = Expression.MakeMemberAccess(_unionParameter, mi)
                    };

                    members.Add(new UnionMember {
                        Member = member, Info1 = info
                    });
                }

                foreach (var info in info2)
                {
                    if (info.Members.Count == 0)
                    {
                        throw new InvalidOperationException();
                    }

                    var em = members.FirstOrDefault(m =>
                                                    m.Member.SequenceInfo != null &&
                                                    m.Info2 == null &&
                                                    m.Member.SequenceInfo.CompareMembers(info));

                    if (em == null)
                    {
                        em = members.FirstOrDefault(m =>
                                                    m.Member.SequenceInfo != null &&
                                                    m.Info2 == null &&
                                                    m.Member.SequenceInfo.CompareLastMember(info));
                    }

                    if (em == null)
                    {
                        var member = new Member {
                            MemberExpression = Expression.MakeMemberAccess(_unionParameter, info.Members[0])
                        };

                        if (_sequence2.IsExpression(member.MemberExpression, 1, RequestFor.Object).Result)
                        {
                            throw new LinqException("Types in {0} are constructed incompatibly.", _methodCall.Method.Name);
                        }

                        members.Add(new UnionMember {
                            Member = member, Info2 = info
                        });
                    }
                    else
                    {
                        em.Info2 = info;
                    }
                }

                _sequence1.SelectQuery.Select.Columns.Clear();
                _sequence2.SelectQuery.Select.Columns.Clear();

                for (var i = 0; i < members.Count; i++)
                {
                    var member = members[i];

                    if (member.Info1 == null)
                    {
                        var type = members.First(m => m.Info1 != null).Info1.Members.First().GetMemberType();
                        member.Info1 = new SqlInfo(member.Info2.Members)
                        {
                            Sql   = new SqlValue(type, null),
                            Query = _sequence1.SelectQuery,
                        };

                        member.Member.SequenceInfo = member.Info1;
                    }

                    if (member.Info2 == null)
                    {
                        var spam = members.First(m => m.Info2 != null).Info2.Members.First();
                        var type = spam.GetMemberType();

                        member.Info2 = new SqlInfo(member.Info1.Members)
                        {
                            Sql   = new SqlValue(type, null),
                            Query = _sequence2.SelectQuery,
                        };
                    }

                    _sequence1.SelectQuery.Select.Columns.Add(new SelectQuery.Column(_sequence1.SelectQuery, member.Info1.Sql));
                    _sequence2.SelectQuery.Select.Columns.Add(new SelectQuery.Column(_sequence2.SelectQuery, member.Info2.Sql));

                    member.Member.SequenceInfo.Index = i;

                    _members[member.Member.MemberExpression.Member] = member.Member;
                }

                foreach (var key in _sequence1.ColumnIndexes.Keys.ToList())
                {
                    _sequence1.ColumnIndexes[key] = _sequence1.SelectQuery.Select.Add(key);
                }

                foreach (var key in _sequence2.ColumnIndexes.Keys.ToList())
                {
                    _sequence2.ColumnIndexes[key] = _sequence2.SelectQuery.Select.Add(key);
                }
            }
Ejemplo n.º 2
0
            void Init()
            {
                var info1 = _sequence1.ConvertToIndex(null, 0, ConvertFlags.All).ToList();
                var info2 = _sequence2.ConvertToIndex(null, 0, ConvertFlags.All).ToList();

                if (!_isObject)
                {
                    return;
                }

                var unionMembers = new List <UnionMember>();

                foreach (var info in info1)
                {
                    if (info.MemberChain.Length == 0)
                    {
                        throw new InvalidOperationException();
                    }

                    var mi = info.MemberChain.First(m => m.DeclaringType !.IsSameOrParentOf(_unionParameter !.Type));

                    var member = new Member
                    {
                        SequenceInfo     = info,
                        MemberExpression = Expression.MakeMemberAccess(_unionParameter, mi)
                    };

                    unionMembers.Add(new UnionMember {
                        Member = member, Info1 = info
                    });
                }

                foreach (var info in info2)
                {
                    if (info.MemberChain.Length == 0)
                    {
                        throw new InvalidOperationException();
                    }

                    var em = unionMembers.FirstOrDefault(m =>
                                                         m.Member.SequenceInfo != null &&
                                                         m.Info2 == null &&
                                                         m.Member.SequenceInfo.CompareMembers(info));

                    if (em == null)
                    {
                        em = unionMembers.FirstOrDefault(m =>
                                                         m.Member.SequenceInfo != null &&
                                                         m.Info2 == null &&
                                                         m.Member.SequenceInfo.CompareLastMember(info));
                    }

                    if (em == null)
                    {
                        var member = new Member {
                            MemberExpression = Expression.MakeMemberAccess(_unionParameter, info.MemberChain[0])
                        };

                        if (_sequence2.IsExpression(member.MemberExpression, 1, RequestFor.Object).Result)
                        {
                            throw new LinqException("Types in {0} are constructed incompatibly.", _methodCall.Method.Name);
                        }

                        unionMembers.Add(new UnionMember {
                            Member = member, Info2 = info
                        });
                    }
                    else
                    {
                        em.Info2 = info;
                    }
                }
Ejemplo n.º 3
0
            void Init()
            {
                var info1 = _sequence1.ConvertToIndex(null, 0, ConvertFlags.All).ToList();
                var info2 = _sequence2.ConvertToIndex(null, 0, ConvertFlags.All).ToList();

                if (!_isObject)
                {
                    return;
                }

                var unionMembers = new List <UnionMember>();

                foreach (var info in info1)
                {
                    if (info.MemberChain.Length == 0)
                    {
                        throw new InvalidOperationException();
                    }

                    var mi = info.MemberChain.First(m => m.DeclaringType !.IsSameOrParentOf(_unionParameter !.Type));

                    var member = new Member
                    {
                        SequenceInfo     = info,
                        MemberExpression = Expression.MakeMemberAccess(_unionParameter, mi)
                    };

                    unionMembers.Add(new UnionMember {
                        Member = member, Info1 = info
                    });
                }

                foreach (var info in info2)
                {
                    if (info.MemberChain.Length == 0)
                    {
                        throw new InvalidOperationException();
                    }

                    var em = unionMembers.FirstOrDefault(m =>
                                                         m.Member.SequenceInfo != null &&
                                                         m.Info2 == null &&
                                                         m.Member.SequenceInfo.CompareMembers(info));

                    if (em == null)
                    {
                        em = unionMembers.FirstOrDefault(m =>
                                                         m.Member.SequenceInfo != null &&
                                                         m.Info2 == null &&
                                                         m.Member.SequenceInfo.CompareLastMember(info));
                    }

                    if (em == null)
                    {
                        var member = new Member {
                            MemberExpression = Expression.MakeMemberAccess(_unionParameter, info.MemberChain[0])
                        };

                        if (_sequence2.IsExpression(member.MemberExpression, 1, RequestFor.Object).Result)
                        {
                            throw new LinqException("Types in {0} are constructed incompatibly.", _methodCall.Method.Name);
                        }

                        unionMembers.Add(new UnionMember {
                            Member = member, Info2 = info
                        });
                    }
                    else
                    {
                        em.Info2 = info;
                    }
                }

                var aliases1 = _sequence1.SelectQuery.Select.Columns.ToLookup(c => c.Expression, c => c.Alias);
                var aliases2 = _sequence2.SelectQuery.Select.Columns.ToLookup(c => c.Expression, c => c.Alias);

                _sequence1.SelectQuery.Select.Columns.Clear();
                _sequence2.SelectQuery.Select.Columns.Clear();

                for (var i = 0; i < unionMembers.Count; i++)
                {
                    var member = unionMembers[i];

                    if (member.Info1 == null)
                    {
                        var type = member.Info2 !.Sql.SystemType;
                        if (type == null)
                        {
                            type = member.Info2 !.MemberChain.Last().GetMemberType();
                        }
                        member.Info1 = new SqlInfo
                                       (
                            member.Info2 !.MemberChain,
                            new SqlValue(type, null),
                            _sequence1.SelectQuery
                                       );

                        member.Member.SequenceInfo = member.Info1;
                    }

                    if (member.Info2 == null)
                    {
                        var spam = unionMembers.First(m => m.Info2 != null).Info2 !.MemberChain.First();
                        var type = spam.GetMemberType();

                        member.Info2 = new SqlInfo
                                       (
                            member.Info1.MemberChain,
                            new SqlValue(type, null),
                            _sequence2.SelectQuery
                                       );
                    }
Ejemplo n.º 4
0
            void Init(SubQueryContext sequence1, SubQueryContext sequence2)
            {
                var info1 = sequence1.ConvertToIndex(null, 0, ConvertFlags.All).ToList();
                var info2 = sequence2.ConvertToIndex(null, 0, ConvertFlags.All).ToList();

                if (!_isObject)
                {
                    return;
                }

                var members = new List <UnionMember>();

                foreach (var info in info1)
                {
                    if (info.Member == null)
                    {
                        throw new InvalidOperationException();
                    }

                    var member = new Member
                    {
                        SequenceInfo     = info,
                        MemberExpression = Expression.MakeMemberAccess(_unionParameter, info.Member)
                    };

                    if (sequence1.IsExpression(member.MemberExpression, 1, RequestFor.Object).Result)
                    {
                        throw new LinqException("Types in {0} are constructed incompatibly.", _methodCall.Method.Name);
                    }

                    members.Add(new UnionMember {
                        Member = member, Info1 = info
                    });
                }

                foreach (var info in info2)
                {
                    if (info.Member == null)
                    {
                        throw new InvalidOperationException();
                    }

                    var em = members.FirstOrDefault(m =>
                                                    m.Member.SequenceInfo != null &&
                                                    m.Member.SequenceInfo.Member == info.Member);

                    if (em == null)
                    {
                        var member = new Member {
                            MemberExpression = Expression.MakeMemberAccess(_unionParameter, info.Member)
                        };

                        if (sequence2.IsExpression(member.MemberExpression, 1, RequestFor.Object).Result)
                        {
                            throw new LinqException("Types in {0} are constructed incompatibly.", _methodCall.Method.Name);
                        }

                        members.Add(new UnionMember {
                            Member = member, Info2 = info
                        });
                    }
                    else
                    {
                        em.Info2 = info;
                    }
                }

                sequence1.SqlQuery.Select.Columns.Clear();
                sequence2.SqlQuery.Select.Columns.Clear();

                for (var i = 0; i < members.Count; i++)
                {
                    var member = members[i];

                    if (member.Info1 == null)
                    {
                        member.Info1 = new SqlInfo
                        {
                            Sql    = new SqlValue(null),
                            Query  = sequence1.SqlQuery,
                            Member = member.Info2.Member
                        };

                        member.Member.SequenceInfo = member.Info1;
                    }

                    if (member.Info2 == null)
                    {
                        member.Info2 = new SqlInfo
                        {
                            Sql    = new SqlValue(null),
                            Query  = sequence2.SqlQuery,
                            Member = member.Info1.Member
                        };
                    }

                    sequence1.SqlQuery.Select.Columns.Add(new SqlQuery.Column(sequence1.SqlQuery, member.Info1.Sql));
                    sequence2.SqlQuery.Select.Columns.Add(new SqlQuery.Column(sequence2.SqlQuery, member.Info2.Sql));

                    member.Member.SequenceInfo.Index = i;
                    //member.Info1.Index = i;
                    //member.Info2.Index = i;

                    _members.Add(member.Member.MemberExpression.Member, member.Member);
                }

                foreach (var key in sequence1.ColumnIndexes.Keys.ToList())
                {
                    sequence1.ColumnIndexes[key] = sequence1.SqlQuery.Select.Add(key);
                }

                foreach (var key in sequence2.ColumnIndexes.Keys.ToList())
                {
                    sequence2.ColumnIndexes[key] = sequence2.SqlQuery.Select.Add(key);
                }

                /*
                 * if (_isObject)
                 * {
                 *      foreach (var info in info1)
                 *      {
                 *              if (info.Member == null)
                 *                      throw new InvalidOperationException();
                 *
                 *              CheckAndAddMember(sequence1, sequence2, info);
                 *      }
                 *
                 *      info2 = sequence2.ConvertToIndex(null, 0, ConvertFlags.All).OrderBy(_ => _.Index).ToList();
                 *
                 *      if (info1.Count != info2.Count)
                 *      {
                 *              for (var i = 0; i < info2.Count; i++)
                 *              {
                 *                      if (i < info1.Count)
                 *                      {
                 *                              if (info1[i].Index != info2[i].Index)
                 *                                      throw new InvalidOperationException();
                 *                      }
                 *                      else
                 *                      {
                 *                              CheckAndAddMember(sequence2, sequence1, info2[i]);
                 *                      }
                 *              }
                 *      }
                 * }
                 * else
                 *      sequence2.ConvertToIndex(null, 0, ConvertFlags.All).OrderBy(_ => _.Index).ToList();
                 */
            }